KiCad PCB EDA Suite
gr_text.h File Reference
#include <eda_item.h>
#include <eda_text.h>

Go to the source code of this file.

Macros

#define MIN_TEXT_SIZE   5
 Minimum dimension in pixel for drawing/no drawing a text used in Pcbnew to decide to draw (or not) some texts ( like net names on pads/tracks ). More...
 
#define MIN_DRAWABLE_TEXT_SIZE   3
 

Functions

int Clamp_Text_PenSize (int aPenSize, int aSize, bool aBold=true)
 As a rule, pen width should not be >1/4em, otherwise the character will be cluttered up in its own fatness. More...
 
float Clamp_Text_PenSize (float aPenSize, int aSize, bool aBold=true)
 
int Clamp_Text_PenSize (int aPenSize, const wxSize &aSize, bool aBold=true)
 
int GetPenSizeForBold (int aTextSize)
 
int GetPenSizeForNormal (int aTextSize)
 
int GraphicTextWidth (const wxString &aText, const wxSize &aSize, bool italic, bool bold)
 The full X size is GraphicTextWidth + the thickness of graphic lines. More...
 
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)=nullptr, void *aCallbackData=nullptr, PLOTTER *aPlotter=nullptr)
 Draw a graphic text (like footprint text) More...
 
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)=nullptr, void *aCallbackData=nullptr, PLOTTER *aPlotter=nullptr)
 Draw graphic text with a border so that it can be read on different backgrounds. More...
 

Macro Definition Documentation

◆ MIN_DRAWABLE_TEXT_SIZE

#define MIN_DRAWABLE_TEXT_SIZE   3

Definition at line 49 of file gr_text.h.

◆ MIN_TEXT_SIZE

#define MIN_TEXT_SIZE   5

Minimum dimension in pixel for drawing/no drawing a text used in Pcbnew to decide to draw (or not) some texts ( like net names on pads/tracks ).

When a text height is smaller than MIN_TEXT_SIZE, it is not drawn by Pcbnew.

Definition at line 43 of file gr_text.h.

Function Documentation

◆ Clamp_Text_PenSize() [1/3]

int Clamp_Text_PenSize ( int  aPenSize,
int  aSize,
bool  aBold 
)

As a rule, pen width should not be >1/4em, otherwise the character will be cluttered up in its own fatness.

The pen width max is aSize/4 for bold texts, and aSize/6 for normal texts. The "best" pen width is aSize/5 for bold texts so the clamp is consistent with bold option.

Parameters
aPenSizethe pen size to clamp.
aSizethe char size (height or width, or its wxSize).
aBoldtrue if text accept bold pen size.
Returns
the max pen size allowed.

As a rule, pen width should not be >1/4em, otherwise the character will be cluttered up in its own fatness.

Bold fonts are generally around aSize/5 in width, so we limit them to aSize/4, and normal text to aSize/6.

Parameters
aPenSizeis the pen size to clamp.
aSizeis the character size (height or width).
aBolduse true if text accept bold pen size.
Returns
the max pen size allowed.

Definition at line 67 of file gr_text.cpp.

68 {
69  double scale = aBold ? 4.0 : 6.0;
70  int maxWidth = KiROUND( (double) aSize / scale );
71 
72  return std::min( aPenSize, maxWidth );
73 }
const int scale
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

References KiROUND(), and scale.

Referenced by Clamp_Text_PenSize(), KIGFX::SCH_PAINTER::draw(), EDA_TEXT::GetEffectiveTextPenWidth(), LIB_PIN::PlotPinTexts(), LIB_PIN::printPinTexts(), and DIALOG_TEXT_PROPERTIES::TransferDataFromWindow().

◆ Clamp_Text_PenSize() [2/3]

float Clamp_Text_PenSize ( float  aPenSize,
int  aSize,
bool  aBold = true 
)

Definition at line 76 of file gr_text.cpp.

77 {
78  float scale = aBold ? 4.0 : 6.0;
79  float maxWidth = (float) aSize / scale;
80 
81  return std::min( aPenSize, maxWidth );
82 }
const int scale

References scale.

◆ Clamp_Text_PenSize() [3/3]

int Clamp_Text_PenSize ( int  aPenSize,
const wxSize &  aSize,
bool  aBold = true 
)

Definition at line 85 of file gr_text.cpp.

86 {
87  int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
88 
89  return Clamp_Text_PenSize( aPenSize, size, aBold );
90 }
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

References Clamp_Text_PenSize().

◆ GetPenSizeForBold()

int GetPenSizeForBold ( int  aTextSize)
Parameters
aTextSizethe char size (height or width).
Returns
the "best" value for a pen size to draw/plot a bold text.
Parameters
aTextSizeis the char size (height or width).
Returns
the "best" value for a pen size to draw/plot a bold text.

Definition at line 46 of file gr_text.cpp.

47 {
48  return KiROUND( aTextSize / 5.0 );
49 }
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

References KiROUND().

Referenced by EDA_TEXT::GetEffectiveTextPenWidth(), GRText(), SCH_LEGACY_PLUGIN::loadText(), SCH_EAGLE_PLUGIN::loadTextAttributes(), DS_DATA_ITEM_TEXT::SyncDrawItems(), and DIALOG_TEXT_AND_LABEL_PROPERTIES::TransferDataFromWindow().

◆ GetPenSizeForNormal()

int GetPenSizeForNormal ( int  aTextSize)
Parameters
aTextSize= the char size (height or width).
Returns
the "best" value for a pen size to draw/plot a non-bold text.

Definition at line 52 of file gr_text.cpp.

53 {
54  return KiROUND( aTextSize / 8.0 );
55 }
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

References KiROUND().

Referenced by EDA_TEXT::GetEffectiveTextPenWidth().

◆ GraphicTextWidth()

int GraphicTextWidth ( const wxString &  aText,
const wxSize &  aSize,
bool  italic,
bool  bold 
)

The full X size is GraphicTextWidth + the thickness of graphic lines.

Returns
the X size of the graphic text.

Definition at line 93 of file gr_text.cpp.

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 }
void SetFontBold(bool aBold)
Set bold property of current font.
VECTOR2D GetTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
void SetFontItalic(bool aItalic)
Set italic property of current font.
void SetGlyphSize(const VECTOR2D &aSize)
Set the font glyph size.
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
BASIC_GAL basic_gal(basic_displayOptions)

References basic_gal, KIGFX::GAL::GetTextLineSize(), KiROUND(), KIGFX::GAL::SetFontBold(), KIGFX::GAL::SetFontItalic(), KIGFX::GAL::SetGlyphSize(), and VECTOR2< T >::x.

Referenced by SVG_PLOTTER::Text().

◆ GRHaloText()

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(*)(int x0, int y0, int xf, int yf, void *aData)  aCallback = nullptr,
void *  aCallbackData = nullptr,
PLOTTER aPlotter = nullptr 
)

Draw graphic text with a border so that it can be read on different backgrounds.

See GRText for most of the parameters. If aBgColor is a dark color text is drawn in aColor2 with aColor1 border. Otherwise colors are swapped.

Definition at line 173 of file gr_text.cpp.

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 }
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition: color4d.h:336
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
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103

References KIGFX::COLOR4D::GetBrightness(), and GRText().

◆ GRText()

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(*)(int x0, int y0, int xf, int yf, void *aData)  aCallback,
void *  aCallbackData,
PLOTTER aPlotter 
)

Draw a graphic text (like footprint text)

Parameters
aClipBoxthe clipping rect, or NULL if no clipping.
aDCthe current Device Context. NULL if draw within a 3D GL Canvas.
aPostext position (according to h_justify, v_justify).
aColor(COLOR4D) = text color.
aTexttext to draw.
aOrientangle in 0.1 degree.
aSizetext size (size.x or size.y can be < 0 for mirrored texts).
aH_justifyhorizontal justification (Left, center, right).
aV_justifyvertical justification (bottom, center, top).
aWidthline width (pen width) (default = 0). If width < 0 : draw segments in sketch mode, width = abs(width). Use a value min(aSize.x, aSize.y) / 5 for a bold text.
aItalictrue to simulate an italic font.
aBoldtrue to use a bold font.
aCallback( int x0, int y0, int xf, int yf, void* aData ) is a function called (if non null) to draw each segment. used to draw 3D texts or for plotting. NULL for normal drawings.
aCallbackDatais the auxiliary parameter aData for the callback function. This can be nullptr if no auxiliary parameter is needed.
aPlotter= a pointer to a PLOTTER instance, when this function is used to plot the text. NULL to draw this text.

Draw a graphic text (like footprint text)

Parameters
aDCis the current Device Context. NULL if draw within a 3D GL Canvas.
aPosis the text position (according to h_justify, v_justify).
aColoris the text color.
aTextis the text to draw.
aOrientis the angle in 0.1 degree.
aSizeis the text size (size.x or size.y can be < 0 for mirrored texts).
aH_justifyis the horizontal justification (Left, center, right).
aV_justifyis the vertical justification (bottom, center, top).
aWidthis the line width (pen width) (use default width if aWidth = 0). if width < 0 : draw segments in sketch mode, width = abs(width) Use a value min(aSize.x, aSize.y) / 5 for a bold text.
aItalicis the true to simulate an italic font.
aBolduse true to use a bold font. Useful only with default width value (aWidth = 0).
aCallback(int x0, int y0, int xf, int yf, void* aData ) is a function called (if non null) to draw each segment. used to draw 3D texts or for plotting. NULL for normal drawings
aCallbackDatais the auxiliary parameter aData for the callback function. can be nullptr if no auxiliary parameter is needed
aPlotteris a PLOTTER instance, when this function is used to plot the text. NULL to draw this text.

Definition at line 129 of file gr_text.cpp.

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 }
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).
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:119
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:72
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.
void SetPlotter(PLOTTER *aPlotter)
Definition: basic_gal.h:76
wxDC * m_DC
Definition: basic_gal.h:155
BASIC_GAL basic_gal(basic_displayOptions)
void SetClipBox(EDA_RECT *aClipBox)
Set a clip box for drawings If NULL, no clip will be made.
Definition: basic_gal.h:90

References basic_gal, dummy(), GetPenSizeForBold(), BASIC_GAL::m_Color, BASIC_GAL::m_DC, BASIC_GAL::SetCallback(), BASIC_GAL::SetClipBox(), KIGFX::GAL::SetIsFill(), KIGFX::GAL::SetLineWidth(), BASIC_GAL::SetPlotter(), KIGFX::GAL::SetTextAttributes(), and KIGFX::GAL::StrokeText().

Referenced by BOARD_ADAPTER::addFootprintShapesWithClearance(), BOARD_ADAPTER::addShapeWithClearance(), GRHaloText(), LIB_TEXT::print(), SCH_FIELD::Print(), LIB_FIELD::print(), EDA_TEXT::printOneLineOfText(), LIB_PIN::printPinElectricalTypeName(), LIB_PIN::printPinTexts(), PLOTTER::Text(), PCB_TEXT::TransformTextShapeWithClearanceToPolygon(), FP_TEXT::TransformTextShapeWithClearanceToPolygon(), and EDA_TEXT::TransformToSegmentList().