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 <dick@softplc.com>
11  * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
12  * Copyright (C) 1992-2018 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 <plotter.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 <base_screen.h>
40 #include <gr_text.h>
41 #include <math/util.h> // for KiROUND
42 
43 #include <basic_gal.h>
44 
45 
46 static int s_textMarkupFlags = 0;
47 
48 
49 void SetTextMarkupFlags( int aMarkupFlags )
50 {
51  s_textMarkupFlags = aMarkupFlags;
52 }
53 
54 
56 {
57  return s_textMarkupFlags;
58 }
59 
60 
66 int GetPenSizeForBold( int aTextSize )
67 {
68  return KiROUND( aTextSize / 5.0 );
69 }
70 
71 
81 int Clamp_Text_PenSize( int aPenSize, int aSize, bool aBold )
82 {
83  double scale = aBold ? 4.0 : 6.0;
84  int maxWidth = KiROUND( (double) aSize / scale );
85 
86  return std::min( aPenSize, maxWidth );
87 }
88 
89 
90 float Clamp_Text_PenSize( float aPenSize, int aSize, bool aBold )
91 {
92  float scale = aBold ? 4.0 : 6.0;
93  float maxWidth = (float) aSize / scale;
94 
95  return std::min( aPenSize, maxWidth );
96 }
97 
98 
99 int Clamp_Text_PenSize( int aPenSize, wxSize aSize, bool aBold )
100 {
101  int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
102 
103  return Clamp_Text_PenSize( aPenSize, size, aBold );
104 }
105 
106 
107 int GraphicTextWidth( const wxString& aText, const wxSize& aSize, bool aItalic, bool aBold )
108 {
109  basic_gal.SetFontItalic( aItalic );
110  basic_gal.SetFontBold( aBold );
111  basic_gal.SetGlyphSize( VECTOR2D( aSize ) );
112 
113  VECTOR2D tsize = basic_gal.GetTextLineSize( aText );
114 
115  return KiROUND( tsize.x );
116 }
117 
118 
143 void GRText( wxDC* aDC, const wxPoint& aPos, COLOR4D aColor, const wxString& aText,
144  double aOrient, const wxSize& aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify,
145  enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold,
146  void (* aCallback)( int x0, int y0, int xf, int yf, void* aData ),
147  void* aCallbackData, PLOTTER* aPlotter )
148 {
149  bool fill_mode = true;
150 
151  if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
152  aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
153 
154  if( aWidth < 0 )
155  {
156  aWidth = -aWidth;
157  fill_mode = false;
158  }
159 
160  basic_gal.SetIsFill( fill_mode );
161  basic_gal.SetLineWidth( aWidth );
162 
163  EDA_TEXT dummy;
164  dummy.SetItalic( aItalic );
165  dummy.SetBold( aBold );
166  dummy.SetHorizJustify( aH_justify );
167  dummy.SetVertJustify( aV_justify );
168 
169  wxSize size = aSize;
170  dummy.SetMirrored( size.x < 0 );
171 
172  if( size.x < 0 )
173  size.x = - size.x;
174 
175  dummy.SetTextSize( size );
176 
178  basic_gal.SetPlotter( aPlotter );
179  basic_gal.SetCallback( aCallback, aCallbackData );
180  basic_gal.m_DC = aDC;
181  basic_gal.m_Color = aColor;
182  basic_gal.SetClipBox( nullptr );
183 
184  basic_gal.StrokeText( aText, VECTOR2D( aPos ), aOrient * M_PI/1800, GetTextMarkupFlags() );
185 }
186 
187 
188 void GRHaloText( wxDC * aDC, const wxPoint &aPos, const COLOR4D aBgColor, COLOR4D aColor1,
189  COLOR4D aColor2, const wxString &aText, double aOrient, const wxSize &aSize,
190  enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify,
191  int aWidth, bool aItalic, bool aBold,
192  void (*aCallback)( int x0, int y0, int xf, int yf, void* aData ),
193  void* aCallbackData, PLOTTER * aPlotter )
194 {
195  // Swap color if contrast would be better
196  // TODO: Maybe calculate contrast some way other than brightness
197  if( aBgColor.GetBrightness() > 0.5 )
198  {
199  COLOR4D c = aColor1;
200  aColor1 = aColor2;
201  aColor2 = c;
202  }
203 
204  // Draw the background
205  GRText( aDC, aPos, aColor1, aText, aOrient, aSize, aH_justify, aV_justify, aWidth, aItalic,
206  aBold, aCallback, aCallbackData, aPlotter );
207 
208  // Draw the text
209  GRText( aDC, aPos, aColor2, aText, aOrient, aSize, aH_justify, aV_justify, aWidth/4, aItalic,
210  aBold, aCallback, aCallbackData, aPlotter );
211 }
212 
232 void PLOTTER::Text( const wxPoint& aPos,
233  const COLOR4D aColor,
234  const wxString& aText,
235  double aOrient,
236  const wxSize& aSize,
237  enum EDA_TEXT_HJUSTIFY_T aH_justify,
238  enum EDA_TEXT_VJUSTIFY_T aV_justify,
239  int aWidth,
240  bool aItalic,
241  bool aBold,
242  bool aMultilineAllowed,
243  void* aData )
244 {
245  int textPensize = aWidth;
246 
247  if( textPensize == 0 && aBold ) // Use default values if aWidth == 0
248  textPensize = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
249 
250  if( textPensize >= 0 )
251  textPensize = Clamp_Text_PenSize( aWidth, aSize, aBold );
252  else
253  textPensize = -Clamp_Text_PenSize( -aWidth, aSize, aBold );
254 
255  SetCurrentLineWidth( textPensize, aData );
256 
257  SetColor( aColor );
258 
259  GRText( NULL, aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, textPensize,
260  aItalic, aBold, nullptr, nullptr, this );
261 
262  if( aWidth != textPensize )
263  SetCurrentLineWidth( aWidth, aData );
264 }
void SetFontItalic(const bool aItalic)
Set italic property of current font.
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:44
double GetBrightness() const
Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1....
Definition: color4d.h:270
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
Definition: gr_text.cpp:66
virtual void SetColor(COLOR4D color)=0
void SetCallback(void(*aCallback)(int x0, int y0, int xf, int yf, void *aData), void *aData)
Definition: basic_gal.h:86
virtual void SetTextAttributes(const EDA_TEXT *aText)
Loads attributes of the given text (bold/italic/underline/mirrored and so on).
int GraphicTextWidth(const wxString &aText, const wxSize &aSize, bool aItalic, bool aBold)
Function GraphicTextWidth.
Definition: gr_text.cpp:107
virtual void SetLineWidth(float aLineWidth)
Set the line width.
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: gr_text.cpp:232
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:109
void SetFontBold(const bool aBold)
Set bold property of current font.
int GetTextMarkupFlags()
Definition: gr_text.cpp:55
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aBold)
Function Clamp_Text_PenSize Don't allow text to become cluttered up in its own fatness.
Definition: gr_text.cpp:81
#define NULL
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
COLOR4D m_Color
Definition: basic_gal.h:63
virtual void StrokeText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle, int aMarkupFlags=0)
Draws a vector type text using preloaded Newstroke font.
void SetPlotter(PLOTTER *aPlotter)
Definition: basic_gal.h:81
void GRText(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, void *aData), void *aCallbackData, PLOTTER *aPlotter)
Function GRText Draw a graphic text (like module texts)
Definition: gr_text.cpp:143
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:51
Base plotter engine class.
Definition: plotter.h:104
void GRHaloText(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, 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:188
const int scale
wxDC * m_DC
Definition: basic_gal.h:62
static int s_textMarkupFlags
Definition: gr_text.cpp:46
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
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:61
The common library.
void SetGlyphSize(const VECTOR2D aGlyphSize)
Set the font glyph size.
BASIC_GAL basic_gal(basic_displayOptions)
BASE_SCREEN class implementation.
void SetTextMarkupFlags(int aMarkupFlags)
Definition: gr_text.cpp:49
void SetClipBox(EDA_RECT *aClipBox)
Set a clip box for drawings If NULL, no clip will be made.
Definition: basic_gal.h:94
virtual void SetCurrentLineWidth(int width, void *aData=NULL)=0
Set the line width for the next drawing.
VECTOR2D GetTextLineSize(const UTF8 &aText, int aMarkupFlags=0) const
Compute the X and Y size of a given text.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40