KiCad PCB EDA Suite
gr_text.cpp File Reference
#include <gr_basic.h>
#include <plotters/plotter.h>
#include <trigo.h>
#include <math/util.h>
#include <font/font.h>
#include <callback_gal.h>

Go to the source code of this file.


int GetPenSizeForBold (int aTextSize)
int GetPenSizeForBold (const wxSize &aTextSize)
int GetPenSizeForNormal (int aTextSize)
int GetPenSizeForNormal (const wxSize &aTextSize)
int Clamp_Text_PenSize (int aPenSize, int aSize, bool aStrict)
 Pen width should not allow characters to become cluttered up in their own fatness. More...
float Clamp_Text_PenSize (float aPenSize, int aSize, bool aStrict)
int Clamp_Text_PenSize (int aPenSize, const VECTOR2I &aSize, bool aStrict)
int GraphicTextWidth (const wxString &aText, KIFONT::FONT *aFont, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic)
 The full X size is GraphicTextWidth + the thickness of graphic lines. More...
void GRPrintText (wxDC *aDC, const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aWidth, bool aItalic, bool aBold, KIFONT::FONT *aFont)
 Print a graphic text through wxDC. More...

Function Documentation

◆ Clamp_Text_PenSize() [1/3]

float Clamp_Text_PenSize ( float  aPenSize,
int  aSize,
bool  aStrict 

Definition at line 84 of file gr_text.cpp.

86 double scale = aStrict ? 0.18 : 0.25;
87 float maxWidth = (float) aSize * scale;
89 return std::min( aPenSize, maxWidth );
const int scale

References scale.

◆ Clamp_Text_PenSize() [2/3]

int Clamp_Text_PenSize ( int  aPenSize,
const VECTOR2I aSize,
bool  aStrict 

Definition at line 93 of file gr_text.cpp.

95 int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
97 return Clamp_Text_PenSize( aPenSize, size, aStrict );
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aStrict)
Pen width should not allow characters to become cluttered up in their own fatness.
Definition: gr_text.cpp:75
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:401

References std::abs(), Clamp_Text_PenSize(), VECTOR2< T >::x, and VECTOR2< T >::y.

◆ Clamp_Text_PenSize() [3/3]

int Clamp_Text_PenSize ( int  aPenSize,
int  aSize,
bool  aStrict 

Pen width should not allow characters to become cluttered up in their own fatness.

Normal text is normally around 15% the fontsize, and bold text around 20%. So we set a hard limit at 25%, and a secondary limit for non-decorative text that must be readable at small sizes at 18%.

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

Definition at line 75 of file gr_text.cpp.

77 double scale = aStrict ? 0.18 : 0.25;
78 int maxWidth = KiROUND( (double) aSize * scale );
80 return std::min( aPenSize, maxWidth );
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:85

References KiROUND(), and scale.

Referenced by Clamp_Text_PenSize(), KIGFX::SCH_PAINTER::draw(), EDA_TEXT::GetEffectiveTextPenWidth(), LIB_PIN::PlotPinTexts(), LIB_PIN::printPinTexts(), DIALOG_TEXTBOX_PROPERTIES::TransferDataFromWindow(), DIALOG_FOOTPRINT_PROPERTIES::Validate(), and DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::Validate().

◆ GetPenSizeForBold() [1/2]

int GetPenSizeForBold ( const wxSize &  aTextSize)

Definition at line 46 of file gr_text.cpp.

48 return GetPenSizeForBold( std::min( aTextSize.x, aTextSize.y ) );
int GetPenSizeForBold(int aTextSize)
Definition: gr_text.cpp:40

References GetPenSizeForBold().

◆ GetPenSizeForBold() [2/2]

◆ GetPenSizeForNormal() [1/2]

int GetPenSizeForNormal ( const wxSize &  aTextSize)

Definition at line 58 of file gr_text.cpp.

60 return GetPenSizeForNormal( std::min( aTextSize.x, aTextSize.y ) );
int GetPenSizeForNormal(int aTextSize)
Definition: gr_text.cpp:52

References GetPenSizeForNormal().

◆ GetPenSizeForNormal() [2/2]

◆ GraphicTextWidth()

int GraphicTextWidth ( const wxString &  aText,
const VECTOR2I aSize,
int  aThickness,
bool  aBold,
bool  aItalic 

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

the X size of the graphic text.

Definition at line 101 of file gr_text.cpp.

104 if( !aFont )
105 aFont = KIFONT::FONT::GetFont();
107 return KiROUND( aFont->StringBoundaryLimits( aText, aSize, aThickness, aBold, aItalic ).x );
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:65
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic) const
Compute the boundary limits of aText (the bounding box of all shapes).
Definition: font.cpp:271

References KIFONT::FONT::GetFont(), KiROUND(), KIFONT::FONT::StringBoundaryLimits(), and VECTOR2< T >::x.

Referenced by SVG_PLOTTER::Text().

◆ GRPrintText()

void GRPrintText ( wxDC *  aDC,
const VECTOR2I aPos,
const COLOR4D aColor,
const wxString &  aText,
const EDA_ANGLE aOrient,
const VECTOR2I aSize,
enum GR_TEXT_H_ALIGN_T  aH_justify,
enum GR_TEXT_V_ALIGN_T  aV_justify,
int  aWidth,
bool  aItalic,
bool  aBold,

Print a graphic text through wxDC.

aDCis the current Device Context.
aPosis the text position (according to h_justify, v_justify).
aColoris the text color.
aTextis the text to draw.
aOrientis the angle.
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).
aFontis the font to use, or nullptr for the KiCad stroke font

Definition at line 129 of file gr_text.cpp.

135 bool fill_mode = true;
137 if( !aFont )
138 aFont = KIFONT::FONT::GetFont();
140 if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
141 aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
143 if( aWidth < 0 )
144 {
145 aWidth = -aWidth;
146 fill_mode = false;
147 }
149 CALLBACK_GAL callback_gal( empty_opts,
150 // Stroke callback
151 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
152 {
153 if( fill_mode )
154 GRLine( aDC, aPt1, aPt2, aWidth, aColor );
155 else
156 GRCSegm( aDC, aPt1, aPt2, aWidth, aColor );
157 },
158 // Polygon callback
159 [&]( const SHAPE_LINE_CHAIN& aPoly )
160 {
161 GRClosedPoly( aDC, aPoly.PointCount(), aPoly.CPoints().data(), true, aColor );
162 } );
164 TEXT_ATTRIBUTES attributes;
165 attributes.m_Angle = aOrient;
166 attributes.m_StrokeWidth = aWidth;
167 attributes.m_Italic = aItalic;
168 attributes.m_Bold = aBold;
169 attributes.m_Halign = aH_justify;
170 attributes.m_Valign = aV_justify;
171 attributes.m_Size = aSize;
173 aFont->Draw( &callback_gal, aText, aPos, attributes );
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttrs) const
Draw a string.
Definition: font.cpp:159
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void GRCSegm(wxDC *DC, const VECTOR2I &A, const VECTOR2I &B, int width, const COLOR4D &Color)
Definition: gr_basic.cpp:192
void GRLine(wxDC *DC, int x1, int y1, int x2, int y2, int width, const COLOR4D &Color, wxPenStyle aStyle)
Definition: gr_basic.cpp:162
void GRClosedPoly(wxDC *DC, int n, const VECTOR2I *Points, bool Fill, const COLOR4D &Color)
Draw a closed polyline and fill it if Fill, in object space.
Definition: gr_basic.cpp:342

References KIFONT::FONT::Draw(), KIFONT::FONT::GetFont(), GetPenSizeForBold(), GRClosedPoly(), GRCSegm(), GRLine(), TEXT_ATTRIBUTES::m_Angle, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by SCH_FIELD::Print(), LIB_FIELD::print(), LIB_TEXT::print(), LIB_TEXTBOX::print(), EDA_TEXT::printOneLineOfText(), LIB_PIN::printPinElectricalTypeName(), and LIB_PIN::printPinTexts().