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 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 
33 wxString KIGFX::PREVIEW::DimensionLabel( const wxString& prefix, double aVal, EDA_UNITS aUnits,
34  bool aIncludeUnits )
35 {
36  wxString str;
37 
38  if( prefix.size() )
39  str << prefix << ": ";
40 
41  wxString fmtStr;
42 
43  // show a sane precision for the preview, which doesn't need to be accurate down to the
44  // nanometre
45  switch( aUnits )
46  {
47  case EDA_UNITS::MILLIMETRES: fmtStr = wxT( "%.3f" ); break; // 1um
48  case EDA_UNITS::MILS: fmtStr = wxT( "%.1f" ); break; // 0.1mil
49  case EDA_UNITS::INCHES: fmtStr = wxT( "%.4f" ); break; // 0.1mil
50  case EDA_UNITS::DEGREES: fmtStr = wxT( "%.1f" ); break; // 0.1deg
51  case EDA_UNITS::PERCENT: fmtStr = wxT( "%.1f" ); break; // 0.1%
52  case EDA_UNITS::UNSCALED: fmtStr = wxT( "%f" ); break;
53  }
54 
55  str << wxString::Format( fmtStr, To_User_Unit( aUnits, aVal ) );
56 
57  if( aIncludeUnits )
58  str << " " << GetAbbreviatedUnitsLabel( aUnits );
59 
60  return str;
61 }
62 
63 
65  int aRelativeSize )
66 {
67  constexpr double hdpiSizes[] = { 8, 9, 11, 13, 15 };
68  constexpr double sizes[] = { 10, 12, 14, 16, 18 };
69 
70  double height;
71  double thicknessFactor;
72  double shadowFactor;
73  double linePitchFactor;
74 
75  HIDPI_GL_CANVAS* canvas = dynamic_cast<HIDPI_GL_CANVAS*>( aGal );
76 
77  if( canvas && canvas->GetScaleFactor() > 1 )
78  {
79  height = hdpiSizes[ 2 + aRelativeSize ];
80  thicknessFactor = 0.15;
81  shadowFactor = 0.10;
82  linePitchFactor = 1.7;
83  }
84  else
85  {
86  height = sizes[ 2 + aRelativeSize ];
87  thicknessFactor = 0.20;
88  shadowFactor = 0.15;
89  linePitchFactor = 1.9;
90  }
91 
92  height /= aGal->GetWorldScale();
93 
94  VECTOR2D glyphSize = aGal->GetGlyphSize();
95  glyphSize = glyphSize * ( height / glyphSize.y );
96  aGal->SetGlyphSize( glyphSize );
97 
98  TEXT_DIMS textDims;
99 
100  textDims.StrokeWidth = glyphSize.x * thicknessFactor;
101  textDims.ShadowWidth = glyphSize.x * shadowFactor;
102  textDims.LinePitch = glyphSize.y * linePitchFactor;
103 
104  return textDims;
105 }
106 
107 
109 {
110  if( aColor.GetBrightness() > 0.5 )
111  return COLOR4D::BLACK;
112  else
113  return COLOR4D::WHITE;
114 }
115 
116 
118  const VECTOR2D& aTextQuadrant,
119  const std::vector<wxString>& aStrings,
120  bool aDrawingDropShadows )
121 {
122  KIGFX::GAL* gal = aView->GetGAL();
123  RENDER_SETTINGS* rs = aView->GetPainter()->GetSettings();
124 
125  // constant text size on screen
126  TEXT_DIMS textDims = SetConstantGlyphHeight( gal );
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  {
137  textPos.y -= textDims.LinePitch * ( aStrings.size() + 1 );
138  }
139 
140  if( aTextQuadrant.x < 0 )
141  {
142  if( viewFlipped )
144  else
146 
147  textPos.x += 15.0 / gal->GetWorldScale();
148 
149  if( aDrawingDropShadows )
150  textPos.x -= textDims.ShadowWidth;
151  }
152  else
153  {
154  if( viewFlipped )
156  else
158 
159  textPos.x -= 15.0 / gal->GetWorldScale();
160 
161  if( aDrawingDropShadows )
162  textPos.x += textDims.ShadowWidth;
163  }
164 
165  gal->SetIsFill( false );
167  gal->SetLineWidth( textDims.StrokeWidth );
168  gal->SetTextMirrored( viewFlipped ); // Prevent text flipping when view is flipped
169 
170  if( aDrawingDropShadows )
171  {
172  gal->SetStrokeColor( GetShadowColor( gal->GetStrokeColor() ) );
173  gal->SetLineWidth( gal->GetLineWidth() + 2 * textDims.ShadowWidth );
174  }
175 
176  // write strings top-to-bottom
177  for( const wxString& str : aStrings )
178  {
179  textPos.y += textDims.LinePitch;
180  gal->StrokeText( str, textPos, 0.0 );
181  }
182 }
float GetLineWidth() const
Get the line width.
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition: color4d.h:322
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
Implementation of conversion functions that require both schematic and board internal units.
TEXT_DIMS SetConstantGlyphHeight(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.
bool IsFlippedX() const
Return true if flip flag for the X axis is set.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
wxGLCanvas wrapper for HiDPI/Retina support.
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
double GetScaleFactor() const
Get the current scale factor.
wxString GetAbbreviatedUnitsLabel(EDA_UNITS aUnit, EDA_DATA_TYPE aType)
Get the units string for a given units type.
Definition: base_units.cpp:472
Definition: color4d.h:44
wxString DimensionLabel(const wxString &prefix, double aVal, EDA_UNITS aUnits, bool aIncludeUnits=true)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:207
virtual void SetLineWidth(float aLineWidth)
Set the line width.
const VECTOR2D & GetGlyphSize() const
COLOR4D GetShadowColor(COLOR4D aColor)
const COLOR4D & GetStrokeColor() const
Get the stroke color.
Auxiliary items (guides, rule, etc)
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
virtual void StrokeText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draw a vector type text using preloaded Newstroke font.
void SetTextMirrored(const bool aMirrored)
Set a mirrored property of text.
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
Definition: color4d.h:48
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
EDA_UNITS
Definition: eda_units.h:38
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
void SetGlyphSize(const VECTOR2D aSize)
Set the font glyph size.
double To_User_Unit(EDA_UNITS aUnit, double aValue)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:91
void SetHorizontalJustify(const EDA_TEXT_HJUSTIFY_T aHorizontalJustify)
Set the horizontal justify for text drawing.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
double GetWorldScale() const
Get the world scale.
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings, bool aDrawingDropShadows)
Draw strings next to the cursor.
Abstract interface for drawing on a 2D-surface.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98