KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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 <gal/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::MICROMETRES: fmtStr = wxT( "%.0f" ); break; // 1um
49 case EDA_UNITS::MILLIMETRES: fmtStr = wxT( "%.3f" ); break; // 1um
50 case EDA_UNITS::CENTIMETRES: fmtStr = wxT( "%.4f" ); break; // 1um
51 case EDA_UNITS::MILS: fmtStr = wxT( "%.1f" ); break; // 0.1mil
52 case EDA_UNITS::INCHES: fmtStr = wxT( "%.4f" ); break; // 0.1mil
53 case EDA_UNITS::DEGREES: fmtStr = wxT( "%.1f" ); break; // 0.1deg
54 case EDA_UNITS::PERCENT: fmtStr = wxT( "%.1f" ); break; // 0.1%
55 case EDA_UNITS::UNSCALED: fmtStr = wxT( "%f" ); break;
56 }
57
58 str << wxString::Format( fmtStr, EDA_UNIT_UTILS::UI::ToUserUnit( aIuScale, aUnits, aVal ) );
59
60 if( aIncludeUnits )
61 str << EDA_UNIT_UTILS::GetText( aUnits );
62
63 return str;
64}
65
66
68 int aRelativeSize )
69{
70 constexpr double aspectRatio = 1.0;
71 constexpr double hdpiSizes[] = { 7, 8, 9, 11, 13, 14, 16 };
72 constexpr double sizes[] = { 8, 10, 12, 14, 15, 16, 18 };
73
74 double height;
75 double thicknessFactor;
76 double shadowFactor;
77 double linePitchFactor;
78
79 HIDPI_GL_CANVAS* canvas = dynamic_cast<HIDPI_GL_CANVAS*>( aGal );
80
81 if( canvas && canvas->GetScaleFactor() > 1 )
82 {
83 height = hdpiSizes[ 3 + aRelativeSize ];
84 thicknessFactor = 0.15;
85 shadowFactor = 0.10;
86 linePitchFactor = 1.7;
87 }
88 else
89 {
90 height = sizes[ 3 + aRelativeSize ];
91 thicknessFactor = 0.20;
92 shadowFactor = 0.15;
93 linePitchFactor = 1.9;
94 }
95
96 height /= aGal->GetWorldScale();
97
98 TEXT_DIMS textDims;
99
100 textDims.GlyphSize = VECTOR2I( height * aspectRatio, height );
101 textDims.StrokeWidth = height * thicknessFactor;
102 textDims.ShadowWidth = height * shadowFactor;
103 textDims.LinePitch = height * linePitchFactor;
104
105 return textDims;
106}
107
108
110{
111 if( aColor.GetBrightness() > 0.5 )
112 return COLOR4D::BLACK;
113 else
114 return COLOR4D::WHITE;
115}
116
117
119 const VECTOR2D& aTextQuadrant,
120 const std::vector<wxString>& aStrings,
121 bool aDrawingDropShadows )
122{
123 KIGFX::GAL* gal = aView->GetGAL();
125
126 // constant text size on screen
127 TEXT_DIMS textDims = GetConstantGlyphHeight( gal );
128 TEXT_ATTRIBUTES textAttrs;
129
130 // radius string goes on the right of the cursor centre line with a small horizontal
131 // offset (enough to keep clear of a system cursor if present)
132 VECTOR2D textPos = aCursorPos;
133
134 bool viewFlipped = gal->IsFlippedX();
135
136 // if the text goes above the cursor, shift it up
137 if( aTextQuadrant.y > 0 )
138 textPos.y -= textDims.LinePitch * ( aStrings.size() + 1 );
139
140 if( aTextQuadrant.x < 0 )
141 {
142 if( viewFlipped )
144 else
145 textAttrs.m_Halign = GR_TEXT_H_ALIGN_LEFT;
146
147 textPos.x += 15.0 / gal->GetWorldScale();
148 }
149 else
150 {
151 if( viewFlipped )
152 textAttrs.m_Halign = GR_TEXT_H_ALIGN_LEFT;
153 else
155
156 textPos.x -= 15.0 / gal->GetWorldScale();
157 }
158
159 // text is left (or right) aligned, so a shadow text need a small offset to be draw
160 // around the basic text
161 int shadowXoffset = aDrawingDropShadows ? textDims.ShadowWidth : 0;
162
163 // Due to the fact a shadow text is drawn left or right aligned,
164 // it needs an offset = shadowWidth/2 to be drawn at the same place as normal text
165 // But for some reason we need to slightly modify this offset
166 // for a better look for KiCad font (better alignment of shadow shape)
167 const float adjust = 1.2f; // Value chosen after tests
168 shadowXoffset *= adjust;
169
170 if( ( textAttrs.m_Halign == GR_TEXT_H_ALIGN_LEFT ) != viewFlipped )
171 textPos.x -= shadowXoffset;
172 else
173 textPos.x += shadowXoffset;
174
176 textAttrs.m_Mirrored = viewFlipped; // Prevent text flipping when view is flipped
177 textAttrs.m_Size = textDims.GlyphSize;
178 textAttrs.m_StrokeWidth = textDims.StrokeWidth;
179 gal->SetIsFill( false );
180 gal->SetIsStroke( true );
181
182 if( aDrawingDropShadows )
183 {
184 textAttrs.m_StrokeWidth = textDims.StrokeWidth + ( 2 * textDims.ShadowWidth );
186 }
187
188 // write strings top-to-bottom
189 for( const wxString& str : aStrings )
190 {
191 textPos.y += textDims.LinePitch;
192 font->Draw( gal, str, textPos, textAttrs, KIFONT::METRICS::Default() );
193 }
194}
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:131
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition: font.cpp:146
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics) const
Draw a string.
Definition: font.cpp:258
static const METRICS & Default()
Definition: font.cpp:52
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:333
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:68
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:203
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:221
GR_TEXT_H_ALIGN_T m_Halign
EDA_UNITS
Definition: eda_units.h:46
@ LAYER_AUX_ITEMS
Auxiliary items (guides, rule, etc)
Definition: layer_ids.h:224
KICOMMON_API 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:259
KICOMMON_API 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:126
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.
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691