KiCad PCB EDA Suite
KIFONT::FONT Class Referenceabstract

FONT is an abstract base class for both outline and stroke fonts. More...

#include <font.h>

Inheritance diagram for KIFONT::FONT:
KIFONT::OUTLINE_FONT KIFONT::STROKE_FONT

Public Member Functions

 FONT ()
 
virtual ~FONT ()
 
virtual bool IsStroke () const
 
virtual bool IsOutline () const
 
virtual bool IsBold () const
 
virtual bool IsItalic () const
 
const wxString & GetName () const
 
const char * NameAsToken () const
 
void Draw (KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttrs) const
 Draw a string. More...
 
void Draw (KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const TEXT_ATTRIBUTES &aAttributes) const
 
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). More...
 
void LinebreakText (wxString &aText, int aColumnWidth, const VECTOR2I &aGlyphSize, int aThickness, bool aBold, bool aItalic) const
 Insert
characters into text to ensure that no lines are wider than aColumnWidth. More...
 
virtual double ComputeOverbarVerticalPosition (double aGlyphHeight) const =0
 Compute the vertical position of an overbar. More...
 
virtual double ComputeUnderlineVerticalPosition (double aGlyphHeight) const =0
 Compute the vertical position of an underline. More...
 
virtual double GetInterline (double aGlyphHeight, double aLineSpacing=1.0) const =0
 Compute the distance (interline) between 2 lines of text (for multiline texts). More...
 
virtual VECTOR2I GetTextAsGlyphs (BOX2I *aBBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aSize, const VECTOR2I &aPosition, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const =0
 Convert text string to an array of GLYPHs. More...
 

Static Public Member Functions

static FONTGetFont (const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
 
static bool IsStroke (const wxString &aFontName)
 

Protected Member Functions

unsigned linesCount (const wxString &aText) const
 Returns number of lines for a given text. More...
 
void drawSingleLineText (KIGFX::GAL *aGal, BOX2I *aBoundingBox, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, bool aItalic, bool aUnderline) const
 Draws a single line of text. More...
 
VECTOR2I boundingBoxSingleLine (BOX2I *aBBox, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, bool aItalic) const
 Computes the bounding box for a single line of text. More...
 
void getLinePositions (const wxString &aText, const VECTOR2I &aPosition, wxArrayString &aTextLines, std::vector< VECTOR2I > &aPositions, std::vector< VECTOR2I > &aExtents, const TEXT_ATTRIBUTES &aAttrs) const
 
VECTOR2I drawMarkup (BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const
 
void wordbreakMarkup (std::vector< std::pair< wxString, int > > *aWords, const wxString &aText, const VECTOR2I &aSize, TEXT_STYLE_FLAGS aTextStyle) const
 Factor that determines the pitch between 2 lines. More...
 

Protected Attributes

wxString m_fontName
 Font name. More...
 
wxString m_fontFileName
 Font file name. More...
 

Static Protected Attributes

static constexpr double INTERLINE_PITCH_RATIO = 1.62
 

Static Private Member Functions

static FONTgetDefaultFont ()
 

Static Private Attributes

static FONTs_defaultFont = nullptr
 
static std::map< std::tuple< wxString, bool, bool >, FONT * > s_fontMap
 

Detailed Description

FONT is an abstract base class for both outline and stroke fonts.

Definition at line 104 of file font.h.

Constructor & Destructor Documentation

◆ FONT()

FONT::FONT ( )
explicit

Definition at line 51 of file font.cpp.

52{
53}

◆ ~FONT()

virtual KIFONT::FONT::~FONT ( )
inlinevirtual

Definition at line 109 of file font.h.

110 { }

Member Function Documentation

◆ boundingBoxSingleLine()

VECTOR2I FONT::boundingBoxSingleLine ( BOX2I aBBox,
const wxString &  aText,
const VECTOR2I aPosition,
const VECTOR2I aSize,
bool  aItalic 
) const
protected

Computes the bounding box for a single line of text.

Multiline texts should be split before using the function.

Parameters
aBBoxis an optional pointer to be filled with the bounding box.
aTextis the text to be drawn.
aPositionis text position.
aSizeis the cap-height and em-width of the text.
Returns
new cursor position

Definition at line 301 of file font.cpp.

304{
305 TEXT_STYLE_FLAGS textStyle = 0;
306
307 if( aItalic )
308 textStyle |= TEXT_STYLE::ITALIC;
309
310 VECTOR2I extents = drawMarkup( aBBox, nullptr, aText, aPosition, aSize, ANGLE_0, false,
311 VECTOR2I(), textStyle );
312
313 return extents;
314}
VECTOR2I drawMarkup(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const
Definition: font.cpp:233
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:412
@ ITALIC
Definition: font.h:46
unsigned int TEXT_STYLE_FLAGS
Definition: font.h:63
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618

References ANGLE_0, drawMarkup(), and ITALIC.

Referenced by getLinePositions().

◆ ComputeOverbarVerticalPosition()

virtual double KIFONT::FONT::ComputeOverbarVerticalPosition ( double  aGlyphHeight) const
pure virtual

Compute the vertical position of an overbar.

This is the distance between the text baseline and the overbar.

Implemented in KIFONT::OUTLINE_FONT, and KIFONT::STROKE_FONT.

◆ ComputeUnderlineVerticalPosition()

virtual double KIFONT::FONT::ComputeUnderlineVerticalPosition ( double  aGlyphHeight) const
pure virtual

Compute the vertical position of an underline.

This is the distance between the text baseline and the underline.

Implemented in KIFONT::OUTLINE_FONT, and KIFONT::STROKE_FONT.

◆ Draw() [1/2]

void KIFONT::FONT::Draw ( KIGFX::GAL aGal,
const wxString &  aText,
const VECTOR2I aPosition,
const TEXT_ATTRIBUTES aAttributes 
) const
inline

Definition at line 137 of file font.h.

139 {
140 Draw( aGal, aText, aPosition, VECTOR2I( 0, 0 ), aAttributes );
141 }
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

References Draw().

◆ Draw() [2/2]

void FONT::Draw ( KIGFX::GAL aGal,
const wxString &  aText,
const VECTOR2I aPosition,
const VECTOR2I aCursor,
const TEXT_ATTRIBUTES aAttrs 
) const

Draw a string.

Parameters
aGalis the graphics context.
aTextis the text to be drawn.
aPositionis the text position in world coordinates.
aCursoris the current text position (for multiple text blocks within a single text object, such as a run of superscript characters)
aAttrsare the styling attributes of the text, including its rotation
aGal
aTextis the text to be drawn.
aPositionis the text object position in world coordinates.
aCursoris the current text position (for multiple text blocks within a single text object, such as a run of superscript characters)
aAttrsare the styling attributes of the text, including its rotation

Definition at line 159 of file font.cpp.

161{
162 if( !aGal || aText.empty() )
163 return;
164
165 VECTOR2I position( aPosition - aCursor );
166
167 // Split multiline strings into separate ones and draw them line by line
168 wxArrayString strings_list;
169 std::vector<VECTOR2I> positions;
170 std::vector<VECTOR2I> extents;
171
172 getLinePositions( aText, position, strings_list, positions, extents, aAttrs );
173
174 aGal->SetLineWidth( aAttrs.m_StrokeWidth );
175
176 for( size_t i = 0; i < strings_list.GetCount(); i++ )
177 {
178 drawSingleLineText( aGal, nullptr, strings_list[i], positions[i], aAttrs.m_Size,
179 aAttrs.m_Angle, aAttrs.m_Mirrored, aPosition, aAttrs.m_Italic,
180 aAttrs.m_Underlined );
181 }
182}
void drawSingleLineText(KIGFX::GAL *aGal, BOX2I *aBoundingBox, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, bool aItalic, bool aUnderline) const
Draws a single line of text.
Definition: font.cpp:246
void getLinePositions(const wxString &aText, const VECTOR2I &aPosition, wxArrayString &aTextLines, std::vector< VECTOR2I > &aPositions, std::vector< VECTOR2I > &aExtents, const TEXT_ATTRIBUTES &aAttrs) const
Definition: font.cpp:94
virtual void SetLineWidth(float aLineWidth)
Set the line width.

References drawSingleLineText(), getLinePositions(), TEXT_ATTRIBUTES::m_Angle, TEXT_ATTRIBUTES::m_Italic, TEXT_ATTRIBUTES::m_Mirrored, TEXT_ATTRIBUTES::m_Size, TEXT_ATTRIBUTES::m_StrokeWidth, TEXT_ATTRIBUTES::m_Underlined, and KIGFX::GAL::SetLineWidth().

Referenced by BOARD_ADAPTER::addText(), KIGFX::GAL::BitmapText(), KIGFX::DS_PAINTER::draw(), KIGFX::PCB_PAINTER::draw(), Draw(), KIGFX::PREVIEW::DrawTextNextToCursor(), drawTicksAlongLine(), EDA_TEXT::GetEffectiveTextShape(), GRPrintText(), BRDITEMS_PLOTTER::PlotPcbText(), KIGFX::SCH_PAINTER::strokeText(), KIGFX::PCB_PAINTER::strokeText(), PLOTTER::Text(), FP_TEXT::TransformTextShapeWithClearanceToPolygon(), FP_TEXTBOX::TransformTextShapeWithClearanceToPolygon(), PCB_TEXT::TransformTextShapeWithClearanceToPolygon(), and PCB_TEXTBOX::TransformTextShapeWithClearanceToPolygon().

◆ drawMarkup()

VECTOR2I FONT::drawMarkup ( BOX2I aBoundingBox,
std::vector< std::unique_ptr< GLYPH > > *  aGlyphs,
const wxString &  aText,
const VECTOR2I aPosition,
const VECTOR2I aSize,
const EDA_ANGLE aAngle,
bool  aMirror,
const VECTOR2I aOrigin,
TEXT_STYLE_FLAGS  aTextStyle 
) const
protected

Definition at line 233 of file font.cpp.

237{
238 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( aText ) );
239 std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
240
241 return ::drawMarkup( aBoundingBox, aGlyphs, root, aPosition, this, aSize, aAngle, aMirror,
242 aOrigin, aTextStyle );
243}
VECTOR2I drawMarkup(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const std::unique_ptr< MARKUP::NODE > &aNode, const VECTOR2I &aPosition, const KIFONT::FONT *aFont, const VECTOR2I &aSize, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle)
Definition: font.cpp:188
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96

References drawMarkup(), MARKUP::MARKUP_PARSER::Parse(), and TO_UTF8.

Referenced by boundingBoxSingleLine(), drawSingleLineText(), KIFONT::OUTLINE_FONT::GetLinesAsGlyphs(), and StringBoundaryLimits().

◆ drawSingleLineText()

void FONT::drawSingleLineText ( KIGFX::GAL aGal,
BOX2I aBoundingBox,
const wxString &  aText,
const VECTOR2I aPosition,
const VECTOR2I aSize,
const EDA_ANGLE aAngle,
bool  aMirror,
const VECTOR2I aOrigin,
bool  aItalic,
bool  aUnderline 
) const
protected

Draws a single line of text.

Multiline texts should be split before using the function.

Parameters
aGalis a pointer to the graphics abstraction layer, or nullptr (nothing is drawn)
aBBoxis an optional pointer to be filled with the bounding box.
aTextis the text to be drawn.
aPositionis text position.
aSizeis the cap-height and em-width of the text
aAngleis text angle.
aMirroris true if text should be drawn mirrored, false otherwise.
aOriginis the point around which the text should be rotated, mirrored, etc.
Returns
new cursor position in non-rotated, non-mirrored coordinates

Definition at line 246 of file font.cpp.

250{
251 if( !aGal )
252 return;
253
254 TEXT_STYLE_FLAGS textStyle = 0;
255
256 if( aItalic )
257 textStyle |= TEXT_STYLE::ITALIC;
258
259 if( aUnderline )
260 textStyle |= TEXT_STYLE::UNDERLINE;
261
262 std::vector<std::unique_ptr<GLYPH>> glyphs;
263
264 (void) drawMarkup( aBoundingBox, &glyphs, aText, aPosition, aSize, aAngle, aMirror, aOrigin,
265 textStyle );
266
267 aGal->DrawGlyphs( glyphs );
268}
virtual void DrawGlyphs(const std::vector< std::unique_ptr< KIFONT::GLYPH > > &aGlyphs)
Draw polygons representing font glyphs.
@ UNDERLINE
Definition: font.h:50

References KIGFX::GAL::DrawGlyphs(), drawMarkup(), ITALIC, and UNDERLINE.

Referenced by Draw().

◆ getDefaultFont()

FONT * FONT::getDefaultFont ( )
staticprivate

Definition at line 56 of file font.cpp.

57{
58 if( !s_defaultFont )
59 s_defaultFont = STROKE_FONT::LoadFont( wxEmptyString );
60
61 return s_defaultFont;
62}
static FONT * s_defaultFont
Definition: font.h:266
static STROKE_FONT * LoadFont(const wxString &aFontName)
Load a stroke font.
Definition: stroke_font.cpp:72

References KIFONT::STROKE_FONT::LoadFont(), and s_defaultFont.

Referenced by GetFont().

◆ GetFont()

FONT * FONT::GetFont ( const wxString &  aFontName = wxEmptyString,
bool  aBold = false,
bool  aItalic = false 
)
static

Definition at line 65 of file font.cpp.

66{
67 if( aFontName.empty() || aFontName.StartsWith( KICAD_FONT_NAME ) )
68 return getDefaultFont();
69
70 std::tuple<wxString, bool, bool> key = { aFontName, aBold, aItalic };
71
72 FONT* font = s_fontMap[key];
73
74 if( !font )
75 font = OUTLINE_FONT::LoadFont( aFontName, aBold, aItalic );
76
77 if( !font )
78 font = getDefaultFont();
79
80 s_fontMap[key] = font;
81
82 return font;
83}
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
static std::map< std::tuple< wxString, bool, bool >, FONT * > s_fontMap
Definition: font.h:268
static FONT * getDefaultFont()
Definition: font.cpp:56
static OUTLINE_FONT * LoadFont(const wxString &aFontFileName, bool aBold, bool aItalic)
Load an outline font.
#define KICAD_FONT_NAME

References getDefaultFont(), KICAD_FONT_NAME, KIFONT::OUTLINE_FONT::LoadFont(), and s_fontMap.

Referenced by LABEL_MANAGER::Add(), KIGFX::GAL::BitmapText(), KIGFX::SCH_PAINTER::boxText(), KIGFX::DS_PAINTER::draw(), KIGFX::PCB_PAINTER::draw(), KIGFX::PREVIEW::DrawTextNextToCursor(), drawTicksAlongLine(), LIB_PIN::GetBoundingBox(), LIB_PIN::GetDrawFont(), EDA_TEXT::GetDrawFont(), LIB_FIELD::GetDrawFont(), LIB_TEXT::GetDrawFont(), LIB_TEXTBOX::GetDrawFont(), SCH_FIELD::GetDrawFont(), SCH_TEXT::GetDrawFont(), SCH_TEXTBOX::GetDrawFont(), FONT_CHOICE::GetFontSelection(), GraphicTextWidth(), GRPrintText(), PCB_PARSER::parseEDA_TEXT(), SCH_SEXPR_PARSER::parseEDA_TEXT(), DRAWING_SHEET_PARSER::parseText(), DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS::processItem(), FIELDS_GRID_TABLE< T >::SetValue(), KIGFX::SCH_PAINTER::strokeText(), KIGFX::PCB_PAINTER::strokeText(), and PLOTTER::Text().

◆ GetInterline()

virtual double KIFONT::FONT::GetInterline ( double  aGlyphHeight,
double  aLineSpacing = 1.0 
) const
pure virtual

Compute the distance (interline) between 2 lines of text (for multiline texts).

This is the distance between baselines, not the space between line bounding boxes.

Implemented in KIFONT::STROKE_FONT, and KIFONT::OUTLINE_FONT.

Referenced by getLinePositions(), and EDA_TEXT::GetTextBox().

◆ getLinePositions()

void FONT::getLinePositions ( const wxString &  aText,
const VECTOR2I aPosition,
wxArrayString &  aTextLines,
std::vector< VECTOR2I > &  aPositions,
std::vector< VECTOR2I > &  aExtents,
const TEXT_ATTRIBUTES aAttrs 
) const
protected

Definition at line 94 of file font.cpp.

97{
98 wxStringSplit( aText, aTextLines, '\n' );
99 int lineCount = aTextLines.Count();
100 aPositions.reserve( lineCount );
101
102 int interline = GetInterline( aAttrs.m_Size.y, aAttrs.m_LineSpacing );
103 int height = 0;
104
105 for( int i = 0; i < lineCount; i++ )
106 {
107 VECTOR2I pos( aPosition.x, aPosition.y + i * interline );
108 VECTOR2I end = boundingBoxSingleLine( nullptr, aTextLines[i], pos, aAttrs.m_Size,
109 aAttrs.m_Italic );
110 VECTOR2I bBox( end - pos );
111
112 aExtents.push_back( bBox );
113
114 if( i == 0 )
115 height += ( aAttrs.m_Size.y * 1.17 ); // 1.17 is a fudge to match 6.0 positioning
116 else
117 height += interline;
118 }
119
120 VECTOR2I offset( 0, 0 );
121 offset.y += aAttrs.m_Size.y;
122
123 switch( aAttrs.m_Valign )
124 {
125 case GR_TEXT_V_ALIGN_TOP: break;
126 case GR_TEXT_V_ALIGN_CENTER: offset.y -= height / 2; break;
127 case GR_TEXT_V_ALIGN_BOTTOM: offset.y -= height; break;
128 }
129
130 for( int i = 0; i < lineCount; i++ )
131 {
132 VECTOR2I lineSize = aExtents.at( i );
133 VECTOR2I lineOffset( offset );
134
135 lineOffset.y += i * interline;
136
137 switch( aAttrs.m_Halign )
138 {
139 case GR_TEXT_H_ALIGN_LEFT: break;
140 case GR_TEXT_H_ALIGN_CENTER: lineOffset.x = -lineSize.x / 2; break;
141 case GR_TEXT_H_ALIGN_RIGHT: lineOffset.x = -lineSize.x; break;
142 }
143
144 aPositions.push_back( aPosition + lineOffset );
145 }
146}
virtual double GetInterline(double aGlyphHeight, double aLineSpacing=1.0) const =0
Compute the distance (interline) between 2 lines of text (for multiline texts).
VECTOR2I boundingBoxSingleLine(BOX2I *aBBox, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, bool aItalic) const
Computes the bounding box for a single line of text.
Definition: font.cpp:301
GR_TEXT_H_ALIGN_T m_Halign
GR_TEXT_V_ALIGN_T m_Valign
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP

References boundingBoxSingleLine(), GetInterline(), GR_TEXT_H_ALIGN_CENTER, GR_TEXT_H_ALIGN_LEFT, GR_TEXT_H_ALIGN_RIGHT, GR_TEXT_V_ALIGN_BOTTOM, GR_TEXT_V_ALIGN_CENTER, GR_TEXT_V_ALIGN_TOP, TEXT_ATTRIBUTES::m_Halign, TEXT_ATTRIBUTES::m_Italic, TEXT_ATTRIBUTES::m_LineSpacing, TEXT_ATTRIBUTES::m_Size, TEXT_ATTRIBUTES::m_Valign, wxStringSplit(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by Draw(), and KIFONT::OUTLINE_FONT::GetLinesAsGlyphs().

◆ GetName()

const wxString & KIFONT::FONT::GetName ( void  ) const
inline

◆ GetTextAsGlyphs()

virtual VECTOR2I KIFONT::FONT::GetTextAsGlyphs ( BOX2I aBBox,
std::vector< std::unique_ptr< GLYPH > > *  aGlyphs,
const wxString &  aText,
const VECTOR2I aSize,
const VECTOR2I aPosition,
const EDA_ANGLE aAngle,
bool  aMirror,
const VECTOR2I aOrigin,
TEXT_STYLE_FLAGS  aTextStyle 
) const
pure virtual

Convert text string to an array of GLYPHs.

Parameters
aBBoxpointer to a BOX2I that will set to the bounding box, or nullptr
aGlyphsstorage for the returned GLYPHs
aTexttext to convert to polygon/polyline
aSizeis the cap-height and em-width of the text
aPositionposition of text (cursor position before this text)
aAngletext angle
aMirroris true if text should be drawn mirrored, false otherwise.
aOriginis the point around which the text should be rotated, mirrored, etc.
aTextStyletext style flags
Returns
text cursor position after this text

Implemented in KIFONT::OUTLINE_FONT, and KIFONT::STROKE_FONT.

Referenced by drawMarkup(), LinebreakText(), and wordbreakMarkup().

◆ IsBold()

virtual bool KIFONT::FONT::IsBold ( ) const
inlinevirtual

Reimplemented in KIFONT::OUTLINE_FONT.

Definition at line 114 of file font.h.

114{ return false; }

Referenced by operator<<().

◆ IsItalic()

virtual bool KIFONT::FONT::IsItalic ( ) const
inlinevirtual

Reimplemented in KIFONT::OUTLINE_FONT.

Definition at line 115 of file font.h.

115{ return false; }

Referenced by operator<<().

◆ IsOutline()

◆ IsStroke() [1/2]

◆ IsStroke() [2/2]

bool FONT::IsStroke ( const wxString &  aFontName)
static

Definition at line 86 of file font.cpp.

87{
88 // This would need a more complex implementation if we ever support more stroke fonts
89 // than the KiCad Font.
90 return aFontName == _( "Default Font" ) || aFontName == KICAD_FONT_NAME;
91}
#define _(s)

References _, and KICAD_FONT_NAME.

◆ LinebreakText()

void FONT::LinebreakText ( wxString &  aText,
int  aColumnWidth,
const VECTOR2I aGlyphSize,
int  aThickness,
bool  aBold,
bool  aItalic 
) const

Insert
characters into text to ensure that no lines are wider than aColumnWidth.

Definition at line 423 of file font.cpp.

425{
426 TEXT_STYLE_FLAGS textStyle = 0;
427
428 if( aBold )
429 textStyle |= TEXT_STYLE::BOLD;
430
431 if( aItalic )
432 textStyle |= TEXT_STYLE::ITALIC;
433
434 int spaceWidth = GetTextAsGlyphs( nullptr, nullptr, wxS( " " ), aSize, VECTOR2I(), ANGLE_0,
435 false, VECTOR2I(), textStyle ).x;
436
437 wxArrayString textLines;
438 wxStringSplit( aText, textLines, '\n' );
439
440 aText = wxEmptyString;
441
442 for( size_t ii = 0; ii < textLines.Count(); ++ii )
443 {
444 bool virginLine = true;
445 int lineWidth = 0;
446 std::vector<std::pair<wxString, int>> words;
447
448 wordbreakMarkup( &words, textLines[ii], aSize, textStyle );
449
450 for( size_t jj = 0; jj < words.size(); /* advance in loop */ )
451 {
452 if( virginLine )
453 {
454 // First word is always placed, even when wider than columnWidth.
455 aText += words[jj].first;
456 lineWidth += words[jj].second;
457 jj++;
458
459 virginLine = false;
460 }
461 else if( lineWidth + spaceWidth + words[jj].second < aColumnWidth - aThickness )
462 {
463 aText += " " + words[jj].first;
464 lineWidth += spaceWidth + words[jj].second;
465 jj++;
466 }
467 else
468 {
469 aText += '\n';
470
471 lineWidth = 0;
472 virginLine = true;
473 }
474 }
475
476 // Add the newlines back onto the string
477 if( ii != ( textLines.Count() - 1 ) )
478 aText += '\n';
479 }
480}
void wordbreakMarkup(std::vector< std::pair< wxString, int > > *aWords, const wxString &aText, const VECTOR2I &aSize, TEXT_STYLE_FLAGS aTextStyle) const
Factor that determines the pitch between 2 lines.
Definition: font.cpp:402
virtual VECTOR2I GetTextAsGlyphs(BOX2I *aBBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aSize, const VECTOR2I &aPosition, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const =0
Convert text string to an array of GLYPHs.
@ BOLD
Definition: font.h:45

References ANGLE_0, BOLD, GetTextAsGlyphs(), ITALIC, wordbreakMarkup(), wxStringSplit(), and VECTOR2< T >::x.

Referenced by LIB_TEXTBOX::GetShownText(), SCH_TEXTBOX::GetShownText(), FP_TEXTBOX::GetShownText(), and PCB_TEXTBOX::GetShownText().

◆ linesCount()

unsigned KIFONT::FONT::linesCount ( const wxString &  aText) const
inlineprotected

Returns number of lines for a given text.

Parameters
aTextis the text to be checked.
Returns
unsigned - The number of lines in aText.

Definition at line 202 of file font.h.

203 {
204 if( aText.empty() )
205 return 0; // std::count does not work well with empty strings
206 else
207 // aText.end() - 1 is to skip a newline character that is potentially at the end
208 return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
209 }

◆ NameAsToken()

const char * KIFONT::FONT::NameAsToken ( ) const
inline

Definition at line 122 of file font.h.

122{ return GetName().utf8_str().data(); }
const wxString & GetName() const
Definition: font.h:121

References GetName().

Referenced by DS_DATA_MODEL_IO::format().

◆ StringBoundaryLimits()

VECTOR2I FONT::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).

Returns
a VECTOR2I giving the width and height of text.

Definition at line 271 of file font.cpp.

273{
274 // TODO do we need to parse every time - have we already parsed?
276 TEXT_STYLE_FLAGS textStyle = 0;
277
278 if( aBold )
279 textStyle |= TEXT_STYLE::BOLD;
280
281 if( aItalic )
282 textStyle |= TEXT_STYLE::ITALIC;
283
284 (void) drawMarkup( &boundingBox, nullptr, aText, VECTOR2I(), aSize, ANGLE_0, false,
285 VECTOR2I(), textStyle );
286
287 if( IsStroke() )
288 {
289 // Inflate by a bit more than thickness/2 to catch diacriticals, descenders, etc.
290 boundingBox.Inflate( KiROUND( aThickness * 0.75 ) );
291 }
292 else if( IsOutline() )
293 {
294 // Outline fonts have thickness built in
295 }
296
297 return boundingBox.GetSize();
298}
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
const Vec & GetSize() const
Definition: box2.h:179
virtual bool IsStroke() const
Definition: font.h:112
virtual bool IsOutline() const
Definition: font.h:113
BOX2I boundingBox(T aObject)
Used by SHAPE_INDEX to get the bounding box of a generic T object.
Definition: shape_index.h:77
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:80

References ANGLE_0, BOLD, boundingBox(), drawMarkup(), BOX2< Vec >::GetSize(), BOX2< Vec >::Inflate(), IsOutline(), IsStroke(), ITALIC, and KiROUND().

Referenced by LABEL_MANAGER::Add(), KIGFX::SCH_PAINTER::boxText(), EDA_TEXT::GetTextBox(), and GraphicTextWidth().

◆ wordbreakMarkup()

void FONT::wordbreakMarkup ( std::vector< std::pair< wxString, int > > *  aWords,
const wxString &  aText,
const VECTOR2I aSize,
TEXT_STYLE_FLAGS  aTextStyle 
) const
protected

Factor that determines the pitch between 2 lines.

Definition at line 402 of file font.cpp.

404{
405 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( aText ) );
406 std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
407
408 ::wordbreakMarkup( aWords, root, this, aSize, aTextStyle );
409}

References MARKUP::MARKUP_PARSER::Parse(), TO_UTF8, and wordbreakMarkup().

Referenced by LinebreakText(), and wordbreakMarkup().

Member Data Documentation

◆ INTERLINE_PITCH_RATIO

constexpr double KIFONT::FONT::INTERLINE_PITCH_RATIO = 1.62
staticconstexprprotected

◆ m_fontFileName

wxString KIFONT::FONT::m_fontFileName
protected

Font file name.

Definition at line 263 of file font.h.

Referenced by KIFONT::OUTLINE_FONT::LoadFont(), and KIFONT::STROKE_FONT::loadNewStrokeFont().

◆ m_fontName

wxString KIFONT::FONT::m_fontName
protected

Font name.

Definition at line 262 of file font.h.

Referenced by GetName(), KIFONT::OUTLINE_FONT::LoadFont(), and KIFONT::STROKE_FONT::loadNewStrokeFont().

◆ s_defaultFont

FONT * FONT::s_defaultFont = nullptr
staticprivate

Definition at line 266 of file font.h.

Referenced by getDefaultFont().

◆ s_fontMap

std::map< std::tuple< wxString, bool, bool >, FONT * > FONT::s_fontMap
staticprivate

Definition at line 268 of file font.h.

Referenced by GetFont().


The documentation for this class was generated from the following files: