KiCad PCB EDA Suite
preview_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2019-2021 Kicad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
22#include <base_units.h>
23#include <painter.h>
24#include <view/view.h>
25#include <gal/hidpi_gl_canvas.h>
26
28{
29 return aDeemph ? 0.5 : 1.0;
30}
31
32
33wxString KIGFX::PREVIEW::DimensionLabel( const wxString& prefix, double aVal,
34 const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
35 bool aIncludeUnits )
36{
37 wxString str;
38
39 if( prefix.size() )
40 str << prefix << ": ";
41
42 wxString fmtStr;
43
44 // show a sane precision for the preview, which doesn't need to be accurate down to the
45 // nanometre
46 switch( aUnits )
47 {
48 case EDA_UNITS::MILLIMETRES: fmtStr = wxT( "%.3f" ); break; // 1um
49 case EDA_UNITS::MILS: fmtStr = wxT( "%.1f" ); break; // 0.1mil
50 case EDA_UNITS::INCHES: fmtStr = wxT( "%.4f" ); break; // 0.1mil
51 case EDA_UNITS::DEGREES: fmtStr = wxT( "%.1f" ); break; // 0.1deg
52 case EDA_UNITS::PERCENT: fmtStr = wxT( "%.1f" ); break; // 0.1%
53 case EDA_UNITS::UNSCALED: fmtStr = wxT( "%f" ); break;
54 }
55
56 str << wxString::Format( fmtStr, EDA_UNIT_UTILS::UI::ToUserUnit( aIuScale, aUnits, aVal ) );
57
58 if( aIncludeUnits )
59 str << EDA_UNIT_UTILS::GetText( aUnits );
60
61 return str;
62}
63
64
66 int aRelativeSize )
67{
68 constexpr double aspectRatio = 1.0;
69 constexpr double hdpiSizes[] = { 8, 9, 11, 13, 15 };
70 constexpr double sizes[] = { 10, 12, 14, 16, 18 };
71
72 double height;
73 double thicknessFactor;
74 double shadowFactor;
75 double linePitchFactor;
76
77 HIDPI_GL_CANVAS* canvas = dynamic_cast<HIDPI_GL_CANVAS*>( aGal );
78
79 if( canvas && canvas->GetScaleFactor() > 1 )
80 {
81 height = hdpiSizes[ 2 + aRelativeSize ];
82 thicknessFactor = 0.15;
83 shadowFactor = 0.10;
84 linePitchFactor = 1.7;
85 }
86 else
87 {
88 height = sizes[ 2 + aRelativeSize ];
89 thicknessFactor = 0.20;
90 shadowFactor = 0.15;
91 linePitchFactor = 1.9;
92 }
93
94 height /= aGal->GetWorldScale();
95
96 TEXT_DIMS textDims;
97
98 textDims.GlyphSize = VECTOR2I( height * aspectRatio, height );
99 textDims.StrokeWidth = height * thicknessFactor;
100 textDims.ShadowWidth = height * shadowFactor;
101 textDims.LinePitch = height * linePitchFactor;
102
103 return textDims;
104}
105
106
108{
109 if( aColor.GetBrightness() > 0.5 )
110 return COLOR4D::BLACK;
111 else
112 return COLOR4D::WHITE;
113}
114
115
117 const VECTOR2D& aTextQuadrant,
118 const std::vector<wxString>& aStrings,
119 bool aDrawingDropShadows )
120{
121 KIGFX::GAL* gal = aView->GetGAL();
123
124 // constant text size on screen
125 TEXT_DIMS textDims = GetConstantGlyphHeight( gal );
126 TEXT_ATTRIBUTES textAttrs;
127
128 // radius string goes on the right of the cursor centre line with a small horizontal
129 // offset (enough to keep clear of a system cursor if present)
130 VECTOR2D textPos = aCursorPos;
131
132 bool viewFlipped = gal->IsFlippedX();
133
134 // if the text goes above the cursor, shift it up
135 if( aTextQuadrant.y > 0 )
136 textPos.y -= textDims.LinePitch * ( aStrings.size() + 1 );
137
138 if( aTextQuadrant.x < 0 )
139 {
140 if( viewFlipped )
142 else
143 textAttrs.m_Halign = GR_TEXT_H_ALIGN_LEFT;
144
145 textPos.x += 15.0 / gal->GetWorldScale();
146 }
147 else
148 {
149 if( viewFlipped )
150 textAttrs.m_Halign = GR_TEXT_H_ALIGN_LEFT;
151 else
153
154 textPos.x -= 15.0 / gal->GetWorldScale();
155 }
156
158 textAttrs.m_Mirrored = viewFlipped; // Prevent text flipping when view is flipped
159 textAttrs.m_Size = textDims.GlyphSize;
160 textAttrs.m_StrokeWidth = textDims.StrokeWidth;
161 gal->SetIsFill( false );
162 gal->SetIsStroke( true );
163
164 if( aDrawingDropShadows )
165 {
166 textAttrs.m_StrokeWidth = textDims.StrokeWidth + ( 2 * textDims.ShadowWidth );
168 }
169
170 // write strings top-to-bottom
171 for( const wxString& str : aStrings )
172 {
173 textPos.y += textDims.LinePitch;
174 font->Draw( gal, str, textPos, textAttrs );
175 }
176}
wxGLCanvas wrapper for HiDPI/Retina support.
double GetScaleFactor() const
Get the current scale factor.
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:65
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttrs) const
Draw a string.
Definition: font.cpp:159
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition: color4d.h:343
Abstract interface for drawing on a 2D-surface.
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
const COLOR4D & GetStrokeColor() const
Get the stroke color.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
double GetWorldScale() const
Get the world scale.
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:69
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:195
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:213
GR_TEXT_H_ALIGN_T m_Halign
@ WHITE
Definition: color4d.h:48
@ BLACK
Definition: color4d.h:44
EDA_UNITS
Definition: eda_units.h:43
@ LAYER_AUX_ITEMS
Auxiliary items (guides, rule, etc)
Definition: layer_ids.h:222
double ToUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: eda_units.cpp:194
wxString GetText(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
Definition: eda_units.cpp:98
COLOR4D GetShadowColor(const COLOR4D &aColor)
wxString DimensionLabel(const wxString &prefix, double aVal, const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, bool aIncludeUnits=true)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings, bool aDrawingDropShadows)
Draw strings next to the cursor.
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
TEXT_DIMS GetConstantGlyphHeight(KIGFX::GAL *aGal, int aRelativeSize=0)
Set the GAL glyph height to a constant scaled value, so that it always looks the same on screen.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618