KiCad PCB EDA Suite
drawtxt.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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
10  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
11  * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
12  * Copyright (C) 1992-2016 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 <fctsys.h>
33 #include <gr_basic.h>
34 #include <common.h>
35 #include <plot_common.h>
36 #include <eda_text.h> // EDA_TEXT_HJUSTIFY_T and EDA_TEXT_VJUSTIFY_T
37 #include <trigo.h>
38 #include <macros.h>
39 #include <class_drawpanel.h>
40 #include <class_base_screen.h>
41 
42 #include <basic_gal.h>
43 
49 int GetPenSizeForBold( int aTextSize )
50 {
51  return KiROUND( aTextSize / 5.0 );
52 }
53 
54 
67 int Clamp_Text_PenSize( int aPenSize, int aSize, bool aBold )
68 {
69  int penSize = aPenSize;
70  double scale = aBold ? 4.0 : 6.0;
71  int maxWidth = KiROUND( std::abs( aSize ) / scale );
72 
73  if( penSize > maxWidth )
74  penSize = maxWidth;
75 
76  return penSize;
77 }
78 
79 
80 int Clamp_Text_PenSize( int aPenSize, wxSize aSize, bool aBold )
81 {
82  int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
83 
84  return Clamp_Text_PenSize( aPenSize, size, aBold );
85 }
86 
87 
88 int GraphicTextWidth( const wxString& aText, const wxSize& aSize, bool aItalic, bool aBold )
89 {
90  basic_gal.SetFontItalic( aItalic );
91  basic_gal.SetFontBold( aBold );
92  basic_gal.SetGlyphSize( VECTOR2D( aSize ) );
93 
94  VECTOR2D tsize = basic_gal.GetTextLineSize( aText );
95 
96  return KiROUND( tsize.x );
97 }
98 
99 
122 void DrawGraphicText( EDA_RECT* aClipBox,
123  wxDC* aDC,
124  const wxPoint& aPos,
125  COLOR4D aColor,
126  const wxString& aText,
127  double aOrient,
128  const wxSize& aSize,
129  enum EDA_TEXT_HJUSTIFY_T aH_justify,
130  enum EDA_TEXT_VJUSTIFY_T aV_justify,
131  int aWidth,
132  bool aItalic,
133  bool aBold,
134  void (* aCallback)( int x0, int y0, int xf, int yf ),
135  PLOTTER* aPlotter )
136 {
137  bool fill_mode = true;
138 
139  if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
140  aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
141 
142  if( aWidth < 0 )
143  {
144  aWidth = -aWidth;
145  fill_mode = false;
146  }
147 
148  basic_gal.SetIsFill( fill_mode );
149  basic_gal.SetLineWidth( aWidth );
150 
151  EDA_TEXT dummy;
152  dummy.SetItalic( aItalic );
153  dummy.SetBold( aBold );
154  dummy.SetHorizJustify( aH_justify );
155  dummy.SetVertJustify( aV_justify );
156 
157  wxSize size = aSize;
158  dummy.SetMirrored( size.x < 0 );
159 
160  if( size.x < 0 )
161  size.x = - size.x;
162 
163  dummy.SetTextSize( size );
164 
165  basic_gal.SetTextAttributes( &dummy );
166  basic_gal.SetPlotter( aPlotter );
167  basic_gal.SetCallback( aCallback );
168  basic_gal.m_DC = aDC;
169  basic_gal.m_Color = aColor;
170  basic_gal.SetClipBox( aClipBox );
171 
172  basic_gal.StrokeText( aText, VECTOR2D( aPos ), aOrient * M_PI/1800 );
173 }
174 
175 
176 void DrawGraphicHaloText( EDA_RECT* aClipBox, wxDC * aDC,
177  const wxPoint &aPos,
178  const COLOR4D aBgColor,
179  COLOR4D aColor1,
180  COLOR4D aColor2,
181  const wxString &aText,
182  double aOrient,
183  const wxSize &aSize,
184  enum EDA_TEXT_HJUSTIFY_T aH_justify,
185  enum EDA_TEXT_VJUSTIFY_T aV_justify,
186  int aWidth, bool aItalic, bool aBold,
187  void (*aCallback)( int x0, int y0, int xf, int yf ),
188  PLOTTER * aPlotter )
189 {
190  // Swap color if contrast would be better
191  // TODO: Maybe calculate contrast some way other than brightness
192  if( aBgColor.GetBrightness() > 0.5 )
193  {
194  COLOR4D c = aColor1;
195  aColor1 = aColor2;
196  aColor2 = c;
197  }
198 
199  // Draw the background
200  DrawGraphicText( aClipBox, aDC, aPos, aColor1, aText, aOrient, aSize,
201  aH_justify, aV_justify, aWidth, aItalic, aBold,
202  aCallback, aPlotter );
203 
204  // Draw the text
205  DrawGraphicText( aClipBox, aDC, aPos, aColor2, aText, aOrient, aSize,
206  aH_justify, aV_justify, aWidth/4, aItalic, aBold,
207  aCallback, aPlotter );
208 }
209 
227 void PLOTTER::Text( const wxPoint& aPos,
228  const COLOR4D aColor,
229  const wxString& aText,
230  double aOrient,
231  const wxSize& aSize,
232  enum EDA_TEXT_HJUSTIFY_T aH_justify,
233  enum EDA_TEXT_VJUSTIFY_T aV_justify,
234  int aWidth,
235  bool aItalic,
236  bool aBold,
237  bool aMultilineAllowed,
238  void* aData )
239 {
240  int textPensize = aWidth;
241 
242  if( textPensize == 0 && aBold ) // Use default values if aWidth == 0
243  textPensize = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
244 
245  if( textPensize >= 0 )
246  textPensize = Clamp_Text_PenSize( aWidth, aSize, aBold );
247  else
248  textPensize = -Clamp_Text_PenSize( -aWidth, aSize, aBold );
249 
250  SetCurrentLineWidth( textPensize, aData );
251 
252  SetColor( aColor );
253 
254  DrawGraphicText( NULL, NULL, aPos, aColor, aText,
255  aOrient, aSize,
256  aH_justify, aV_justify,
257  textPensize, aItalic, aBold, NULL, this );
258 
259  if( aWidth != textPensize )
260  SetCurrentLineWidth( aWidth, aData );
261 }
void SetMirrored(bool isMirrored)
Definition: eda_text.h:178
void SetFontItalic(const bool aItalic)
Set italic property of current font.
void DrawGraphicHaloText(EDA_RECT *aClipBox, wxDC *aDC, const wxPoint &aPos, const COLOR4D aBgColor, COLOR4D aColor1, 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), PLOTTER *aPlotter)
Draw graphic text with a border, so that it can be read on different backgrounds. ...
Definition: drawtxt.cpp:176
void DrawGraphicText(EDA_RECT *aClipBox, wxDC *aDC, const wxPoint &aPos, 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), PLOTTER *aPlotter)
Function DrawGraphicText Draw a graphic text (like module texts)
Definition: drawtxt.cpp:122
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:47
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
virtual void SetColor(COLOR4D color)=0
virtual void SetTextAttributes(const EDA_TEXT *aText)
Loads attributes of the given text (bold/italic/underline/mirrored and so on).
void SetItalic(bool isItalic)
Definition: eda_text.h:169
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
Definition of base KiCad text object.
#define abs(a)
Definition: auxiliary.h:84
This file contains miscellaneous commonly used macros and functions.
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=NULL)
Draws text with the plotter.
Definition: drawtxt.cpp:227
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:114
void SetFontBold(const bool aBold)
Set bold property of current font.
virtual void SetLineWidth(double aLineWidth)
Set the line width.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:589
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
COLOR4D m_Color
Definition: basic_gal.h:61
virtual void StrokeText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draws a vector type text using preloaded Newstroke font.
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aBold)
Function Clamp_Text_PenSize As a rule, pen width should not be >1/4em, otherwise the character will b...
Definition: drawtxt.cpp:67
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:194
double GetBrightness() const
Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1...
Definition: color4d.h:254
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
int GraphicTextWidth(const wxString &aText, const wxSize &aSize, bool aItalic, bool aBold)
Function GraphicTextWidth.
Definition: drawtxt.cpp:88
void SetPlotter(PLOTTER *aPlotter)
Definition: basic_gal.h:78
void SetCallback(void(*aCallback)(int x0, int y0, int xf, int yf))
Definition: basic_gal.h:83
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:54
Base plotter engine class.
Definition: plot_common.h:86
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
Definition: drawtxt.cpp:49
const int scale
BASE_SCREEN class implementation.
wxDC * m_DC
Definition: basic_gal.h:60
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:193
Class EDA_RECT handles the component boundary box.
The common library.
void SetGlyphSize(const VECTOR2D aGlyphSize)
Set the font glyph size.
BASIC_GAL basic_gal(basic_displayOptions)
VECTOR2D GetTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
void SetBold(bool aBold)
Definition: eda_text.h:172
void SetClipBox(EDA_RECT *aClipBox)
Set a clip box for drawings If NULL, no clip will be made.
Definition: basic_gal.h:90
#define min(a, b)
Definition: auxiliary.h:85
virtual void SetCurrentLineWidth(int width, void *aData=NULL)=0
Set the line width for the next drawing.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39