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 
51 int GetPenSizeForBold( int aTextSize )
52 {
53  return KiROUND( aTextSize / 5.0 );
54 }
55 
56 
57 int GetPenSizeForNormal( int aTextSize )
58 {
59  return KiROUND( aTextSize / 8.0 );
60 }
61 
62 
72 int Clamp_Text_PenSize( int aPenSize, int aSize, bool aBold )
73 {
74  double scale = aBold ? 4.0 : 6.0;
75  int maxWidth = KiROUND( (double) aSize / scale );
76 
77  return std::min( aPenSize, maxWidth );
78 }
79 
80 
81 float Clamp_Text_PenSize( float aPenSize, int aSize, bool aBold )
82 {
83  float scale = aBold ? 4.0 : 6.0;
84  float maxWidth = (float) aSize / scale;
85 
86  return std::min( aPenSize, maxWidth );
87 }
88 
89 
90 int Clamp_Text_PenSize( int aPenSize, wxSize aSize, bool aBold )
91 {
92  int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
93 
94  return Clamp_Text_PenSize( aPenSize, size, aBold );
95 }
96 
97 
98 int GraphicTextWidth( const wxString& aText, const wxSize& aSize, bool aItalic, bool aBold )
99 {
100  basic_gal.SetFontItalic( aItalic );
101  basic_gal.SetFontBold( aBold );
102  basic_gal.SetGlyphSize( VECTOR2D( aSize ) );
103 
104  VECTOR2D tsize = basic_gal.GetTextLineSize( aText );
105 
106  return KiROUND( tsize.x );
107 }
108 
109 
134 void GRText( wxDC* aDC, const wxPoint& aPos, COLOR4D aColor, const wxString& aText,
135  double aOrient, const wxSize& aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify,
136  enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold,
137  void (* aCallback)( int x0, int y0, int xf, int yf, void* aData ),
138  void* aCallbackData, PLOTTER* aPlotter )
139 {
140  bool fill_mode = true;
141 
142  if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
143  aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
144 
145  if( aWidth < 0 )
146  {
147  aWidth = -aWidth;
148  fill_mode = false;
149  }
150 
151  basic_gal.SetIsFill( fill_mode );
152  basic_gal.SetLineWidth( aWidth );
153 
154  EDA_TEXT dummy;
155  dummy.SetItalic( aItalic );
156  dummy.SetBold( aBold );
157  dummy.SetHorizJustify( aH_justify );
158  dummy.SetVertJustify( aV_justify );
159 
160  wxSize size = aSize;
161  dummy.SetMirrored( size.x < 0 );
162 
163  if( size.x < 0 )
164  size.x = - size.x;
165 
166  dummy.SetTextSize( size );
167 
169  basic_gal.SetPlotter( aPlotter );
170  basic_gal.SetCallback( aCallback, aCallbackData );
171  basic_gal.m_DC = aDC;
172  basic_gal.m_Color = aColor;
173  basic_gal.SetClipBox( nullptr );
174 
175  basic_gal.StrokeText( aText, VECTOR2D( aPos ), aOrient * M_PI/1800 );
176 }
177 
178 
179 void GRHaloText( wxDC * aDC, const wxPoint &aPos, COLOR4D aBgColor, COLOR4D aColor1,
180  COLOR4D aColor2, const wxString &aText, double aOrient, const wxSize &aSize,
181  enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify,
182  int aWidth, bool aItalic, bool aBold,
183  void (*aCallback)( int x0, int y0, int xf, int yf, void* aData ),
184  void* aCallbackData, PLOTTER * aPlotter )
185 {
186  // Swap color if contrast would be better
187  // TODO: Maybe calculate contrast some way other than brightness
188  if( aBgColor.GetBrightness() > 0.5 )
189  {
190  COLOR4D c = aColor1;
191  aColor1 = aColor2;
192  aColor2 = c;
193  }
194 
195  // Draw the background
196  GRText( aDC, aPos, aColor1, aText, aOrient, aSize, aH_justify, aV_justify, aWidth, aItalic,
197  aBold, aCallback, aCallbackData, aPlotter );
198 
199  // Draw the text
200  GRText( aDC, aPos, aColor2, aText, aOrient, aSize, aH_justify, aV_justify, aWidth/4, aItalic,
201  aBold, aCallback, aCallbackData, aPlotter );
202 }
203 
204 
222 void PLOTTER::Text( const wxPoint& aPos,
223  const COLOR4D aColor,
224  const wxString& aText,
225  double aOrient,
226  const wxSize& aSize,
227  enum EDA_TEXT_HJUSTIFY_T aH_justify,
228  enum EDA_TEXT_VJUSTIFY_T aV_justify,
229  int aPenWidth,
230  bool aItalic,
231  bool aBold,
232  bool aMultilineAllowed,
233  void* aData )
234 {
235  SetColor( aColor );
236  SetCurrentLineWidth( aPenWidth );
237 
238  GRText( NULL, aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aPenWidth,
239  aItalic, aBold, nullptr, nullptr, this );
240 }
void SetFontItalic(const bool aItalic)
Set italic property of current font.
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:48
double GetBrightness() const
Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1....
Definition: color4d.h:329
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
Definition: gr_text.cpp:51
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:98
VECTOR2D GetTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
void GRHaloText(wxDC *aDC, const wxPoint &aPos, 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:179
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:222
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:113
void SetFontBold(const bool aBold)
Set bold property of current font.
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:72
#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)
Draws a vector type text using preloaded Newstroke font.
int GetPenSizeForNormal(int aTextSize)
Function GetPensizeForNormal.
Definition: gr_text.cpp:57
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:134
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:55
Base plotter engine class.
Definition: plotter.h:114
const int scale
wxDC * m_DC
Definition: basic_gal.h:62
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:68
The common library.
void SetGlyphSize(const VECTOR2D aGlyphSize)
Set the font glyph size.
BASIC_GAL basic_gal(basic_displayOptions)
BASE_SCREEN class implementation.
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.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99