KiCad PCB EDA Suite
gr_text.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
10  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
11  * Copyright (C) 2012 Wayne Stambaugh <[email protected]>
12  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, you may find one here:
26  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
27  * or you may search the http://www.gnu.org website for the version 2 license,
28  * or you may write to the Free Software Foundation, Inc.,
29  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
30  */
31 
32 #include <gr_basic.h>
33 #include <plotters/plotter.h>
34 #include <eda_text.h> // EDA_TEXT_HJUSTIFY_T and EDA_TEXT_VJUSTIFY_T
35 #include <trigo.h>
36 #include <base_screen.h>
37 #include <math/util.h> // for KiROUND
38 
39 #include <basic_gal.h>
40 
41 
46 int GetPenSizeForBold( int aTextSize )
47 {
48  return KiROUND( aTextSize / 5.0 );
49 }
50 
51 
52 int GetPenSizeForNormal( int aTextSize )
53 {
54  return KiROUND( aTextSize / 8.0 );
55 }
56 
57 
67 int Clamp_Text_PenSize( int aPenSize, int aSize, bool aBold )
68 {
69  double scale = aBold ? 4.0 : 6.0;
70  int maxWidth = KiROUND( (double) aSize / scale );
71 
72  return std::min( aPenSize, maxWidth );
73 }
74 
75 
76 float Clamp_Text_PenSize( float aPenSize, int aSize, bool aBold )
77 {
78  float scale = aBold ? 4.0 : 6.0;
79  float maxWidth = (float) aSize / scale;
80 
81  return std::min( aPenSize, maxWidth );
82 }
83 
84 
85 int Clamp_Text_PenSize( int aPenSize, const wxSize& aSize, bool aBold )
86 {
87  int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
88 
89  return Clamp_Text_PenSize( aPenSize, size, aBold );
90 }
91 
92 
93 int GraphicTextWidth( const wxString& aText, const wxSize& aSize, bool aItalic, bool aBold )
94 {
95  basic_gal.SetFontItalic( aItalic );
96  basic_gal.SetFontBold( aBold );
97  basic_gal.SetGlyphSize( VECTOR2D( aSize ) );
98 
99  VECTOR2D tsize = basic_gal.GetTextLineSize( aText );
100 
101  return KiROUND( tsize.x );
102 }
103 
104 
129 void GRText( wxDC* aDC, const wxPoint& aPos, const COLOR4D& aColor, const wxString& aText,
130  double aOrient, const wxSize& aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify,
131  enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold,
132  void (* aCallback)( int x0, int y0, int xf, int yf, void* aData ),
133  void* aCallbackData, PLOTTER* aPlotter )
134 {
135  bool fill_mode = true;
136 
137  if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
138  aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
139 
140  if( aWidth < 0 )
141  {
142  aWidth = -aWidth;
143  fill_mode = false;
144  }
145 
146  basic_gal.SetIsFill( fill_mode );
147  basic_gal.SetLineWidth( aWidth );
148 
149  EDA_TEXT dummy;
150  dummy.SetItalic( aItalic );
151  dummy.SetBold( aBold );
152  dummy.SetHorizJustify( aH_justify );
153  dummy.SetVertJustify( aV_justify );
154 
155  wxSize size = aSize;
156  dummy.SetMirrored( size.x < 0 );
157 
158  if( size.x < 0 )
159  size.x = - size.x;
160 
161  dummy.SetTextSize( size );
162 
164  basic_gal.SetPlotter( aPlotter );
165  basic_gal.SetCallback( aCallback, aCallbackData );
166  basic_gal.m_DC = aDC;
167  basic_gal.m_Color = aColor;
168  basic_gal.SetClipBox( nullptr );
169  basic_gal.StrokeText( aText, VECTOR2D( aPos ), aOrient * M_PI/1800 );
170 }
171 
172 
173 void GRHaloText( wxDC* aDC, const wxPoint &aPos, const COLOR4D& aBgColor, const COLOR4D& aColor1,
174  const COLOR4D& aColor2, const wxString &aText, double aOrient, const wxSize &aSize,
175  enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify,
176  int aWidth, bool aItalic, bool aBold,
177  void (*aCallback)( int x0, int y0, int xf, int yf, void* aData ),
178  void* aCallbackData, PLOTTER * aPlotter )
179 {
180  COLOR4D color1 = aColor1;
181  COLOR4D color2 = aColor2;
182 
183  // Swap color if contrast would be better
184  // TODO: Maybe calculate contrast some way other than brightness
185  if( aBgColor.GetBrightness() > 0.5 )
186  {
187  COLOR4D c = color1;
188  color1 = color2;
189  color2 = c;
190  }
191 
192  // Draw the background
193  GRText( aDC, aPos, color1, aText, aOrient, aSize, aH_justify, aV_justify, aWidth, aItalic,
194  aBold, aCallback, aCallbackData, aPlotter );
195 
196  // Draw the text
197  GRText( aDC, aPos, color2, aText, aOrient, aSize, aH_justify, aV_justify, aWidth / 4, aItalic,
198  aBold, aCallback, aCallbackData, aPlotter );
199 }
200 
201 
219 void PLOTTER::Text( const wxPoint& aPos,
220  const COLOR4D& aColor,
221  const wxString& aText,
222  double aOrient,
223  const wxSize& aSize,
224  enum EDA_TEXT_HJUSTIFY_T aH_justify,
225  enum EDA_TEXT_VJUSTIFY_T aV_justify,
226  int aPenWidth,
227  bool aItalic,
228  bool aBold,
229  bool aMultilineAllowed,
230  void* aData )
231 {
232  SetColor( aColor );
233  SetCurrentLineWidth( aPenWidth, aData );
234 
235  GRText( nullptr, aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aPenWidth,
236  aItalic, aBold, nullptr, nullptr, this );
237 }
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:82
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition: color4d.h:336
void GRHaloText(wxDC *aDC, const wxPoint &aPos, const COLOR4D &aBgColor, const COLOR4D &aColor1, const COLOR4D &aColor2, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, void(*aCallback)(int x0, int y0, int xf, int yf, void *aData), void *aCallbackData, PLOTTER *aPlotter)
Draw graphic text with a border so that it can be read on different backgrounds.
Definition: gr_text.cpp:173
int GetPenSizeForBold(int aTextSize)
Definition: gr_text.cpp:46
void SetCallback(void(*aCallback)(int x0, int y0, int xf, int yf, void *aData), void *aData)
Definition: basic_gal.h:81
virtual void SetTextAttributes(const EDA_TEXT *aText)
Loads attributes of the given text (bold/italic/underline/mirrored and so on).
void SetFontBold(bool aBold)
Set bold property of current font.
void GRText(wxDC *aDC, const wxPoint &aPos, const COLOR4D &aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, void(*aCallback)(int x0, int y0, int xf, int yf, void *aData), void *aCallbackData, PLOTTER *aPlotter)
Draw a graphic text (like footprint texts).
Definition: gr_text.cpp:129
int GraphicTextWidth(const wxString &aText, const wxSize &aSize, bool aItalic, bool aBold)
The full X size is GraphicTextWidth + the thickness of graphic lines.
Definition: gr_text.cpp:93
VECTOR2D GetTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:140
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:72
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aBold)
Don't allow text to become cluttered up in its own fatness.
Definition: gr_text.cpp:67
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
COLOR4D m_Color
Definition: basic_gal.h:156
virtual void StrokeText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draw a vector type text using preloaded Newstroke font.
int GetPenSizeForNormal(int aTextSize)
Definition: gr_text.cpp:52
void SetPlotter(PLOTTER *aPlotter)
Definition: basic_gal.h:76
void SetFontItalic(bool aItalic)
Set italic property of current font.
void SetGlyphSize(const VECTOR2D &aSize)
Set the font glyph size.
virtual void SetColor(const COLOR4D &color)=0
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:89
Base plotter engine class.
Definition: plotter.h:121
const int scale
wxDC * m_DC
Definition: basic_gal.h:155
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:73
virtual void Text(const wxPoint &aPos, const COLOR4D &aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=nullptr)
Draw text with the plotter.
Definition: gr_text.cpp:219
BASIC_GAL basic_gal(basic_displayOptions)
BASE_SCREEN class implementation.
virtual void SetCurrentLineWidth(int width, void *aData=nullptr)=0
Set the line width for the next drawing.
void SetClipBox(EDA_RECT *aClipBox)
Set a clip box for drawings If NULL, no clip will be made.
Definition: basic_gal.h:90
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103