KiCad PCB EDA Suite
Loading...
Searching...
No Matches
font.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2021 Ola Rinta-Koski
5 * Copyright (C) 2021-2023 Kicad Developers, see AUTHORS.txt for contributors.
6 *
7 * Font abstract base class
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <list>
28#include <mutex>
29#include <unordered_map>
30
31#include <macros.h>
32#include <string_utils.h>
34#include <font/stroke_font.h>
35#include <font/outline_font.h>
36#include <trigo.h>
37#include <markup_parser.h>
38
39// The "official" name of the Kicad stroke font (always existing)
41#include <wx/tokenzr.h>
42
43// markup_parser.h includes pegtl.hpp which includes windows.h... which leaks #define DrawText
44#undef DrawText
45
46
47using namespace KIFONT;
48
50
51
53{
54 return g_defaultMetrics;
55}
56
57
58FONT* FONT::s_defaultFont = nullptr;
59
60std::map< std::tuple<wxString, bool, bool, bool>, FONT*> FONT::s_fontMap;
61
63{
64public:
65 struct ENTRY
66 {
67 std::string source;
68 std::unique_ptr<MARKUP::NODE> root;
69 };
70
71 typedef std::pair<wxString, ENTRY> CACHE_ENTRY;
72
73 MARKUP_CACHE( size_t aMaxSize ) :
74 m_maxSize( aMaxSize )
75 {
76 }
77
78 ENTRY& Put( const CACHE_ENTRY::first_type& aQuery, ENTRY&& aResult )
79 {
80 auto it = m_cache.find( aQuery );
81
82 m_cacheMru.emplace_front( CACHE_ENTRY( aQuery, std::move( aResult ) ) );
83
84 if( it != m_cache.end() )
85 {
86 m_cacheMru.erase( it->second );
87 m_cache.erase( it );
88 }
89
90 m_cache[aQuery] = m_cacheMru.begin();
91
92 if( m_cache.size() > m_maxSize )
93 {
94 auto last = m_cacheMru.end();
95 last--;
96 m_cache.erase( last->first );
97 m_cacheMru.pop_back();
98 }
99
100 return m_cacheMru.begin()->second;
101 }
102
103 ENTRY* Get( const CACHE_ENTRY::first_type& aQuery )
104 {
105 auto it = m_cache.find( aQuery );
106
107 if( it == m_cache.end() )
108 return nullptr;
109
110 m_cacheMru.splice( m_cacheMru.begin(), m_cacheMru, it->second );
111
112 return &m_cacheMru.begin()->second;
113 }
114
115 void Clear()
116 {
117 m_cacheMru.clear();
118 m_cache.clear();
119 }
120
121private:
122 size_t m_maxSize;
123 std::list<CACHE_ENTRY> m_cacheMru;
124 std::unordered_map<wxString, std::list<CACHE_ENTRY>::iterator> m_cache;
125};
126
127
129static std::mutex s_markupCacheMutex;
130
131
133{
134}
135
136
138{
139 if( !s_defaultFont )
140 s_defaultFont = STROKE_FONT::LoadFont( wxEmptyString );
141
142 return s_defaultFont;
143}
144
145
146FONT* FONT::GetFont( const wxString& aFontName, bool aBold, bool aItalic,
147 const std::vector<wxString>* aEmbeddedFiles, bool aForDrawingSheet )
148{
149 if( aFontName.empty() || aFontName.StartsWith( KICAD_FONT_NAME ) )
150 return getDefaultFont();
151
152 std::tuple<wxString, bool, bool, bool> key = { aFontName, aBold, aItalic, aForDrawingSheet };
153
154 FONT* font = nullptr;
155
156 if( s_fontMap.find( key ) != s_fontMap.end() )
157 font = s_fontMap[key];
158
159 if( !font )
160 font = OUTLINE_FONT::LoadFont( aFontName, aBold, aItalic, aEmbeddedFiles, aForDrawingSheet );
161
162 if( !font )
163 font = getDefaultFont();
164
165 s_fontMap[key] = font;
166
167 return font;
168}
169
170
171bool FONT::IsStroke( const wxString& aFontName )
172{
173 // This would need a more complex implementation if we ever support more stroke fonts
174 // than the KiCad Font.
175 return aFontName == _( "Default Font" ) || aFontName == KICAD_FONT_NAME;
176}
177
178
179void FONT::getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
180 wxArrayString& aTextLines, std::vector<VECTOR2I>& aPositions,
181 std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs,
182 const METRICS& aFontMetrics ) const
183{
184 wxStringSplit( aText, aTextLines, '\n' );
185 int lineCount = aTextLines.Count();
186 aPositions.reserve( lineCount );
187
188 int interline = GetInterline( aAttrs.m_Size.y, aFontMetrics ) * aAttrs.m_LineSpacing;
189 int height = 0;
190
191 for( int i = 0; i < lineCount; i++ )
192 {
193 VECTOR2I pos( aPosition.x, aPosition.y + i * interline );
194 VECTOR2I end = boundingBoxSingleLine( nullptr, aTextLines[i], pos, aAttrs.m_Size,
195 aAttrs.m_Italic, aFontMetrics );
196 VECTOR2I bBox( end - pos );
197
198 aExtents.push_back( bBox );
199
200 if( i == 0 )
201 height += ( aAttrs.m_Size.y * 1.17 ); // 1.17 is a fudge to match 6.0 positioning
202 else
203 height += interline;
204 }
205
206 VECTOR2I offset( 0, 0 );
207 offset.y += aAttrs.m_Size.y;
208
209 if( IsStroke() )
210 {
211 // Fudge factors to match 6.0 positioning
212 offset.x += aAttrs.m_StrokeWidth / 1.52;
213 offset.y -= aAttrs.m_StrokeWidth * 0.052;
214 }
215
216 switch( aAttrs.m_Valign )
217 {
218 case GR_TEXT_V_ALIGN_TOP: break;
219 case GR_TEXT_V_ALIGN_CENTER: offset.y -= height / 2; break;
220 case GR_TEXT_V_ALIGN_BOTTOM: offset.y -= height; break;
222 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
223 break;
224 }
225
226 for( int i = 0; i < lineCount; i++ )
227 {
228 VECTOR2I lineSize = aExtents.at( i );
229 VECTOR2I lineOffset( offset );
230
231 lineOffset.y += i * interline;
232
233 switch( aAttrs.m_Halign )
234 {
235 case GR_TEXT_H_ALIGN_LEFT: break;
236 case GR_TEXT_H_ALIGN_CENTER: lineOffset.x = -lineSize.x / 2; break;
237 case GR_TEXT_H_ALIGN_RIGHT: lineOffset.x = -( lineSize.x + offset.x ); break;
239 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
240 break;
241 }
242
243 aPositions.push_back( aPosition + lineOffset );
244 }
245}
246
247
258void FONT::Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
259 const VECTOR2I& aCursor, const TEXT_ATTRIBUTES& aAttrs,
260 const METRICS& aFontMetrics ) const
261{
262 if( !aGal || aText.empty() )
263 return;
264
265 VECTOR2I position( aPosition - aCursor );
266
267 // Split multiline strings into separate ones and draw them line by line
268 wxArrayString strings_list;
269 std::vector<VECTOR2I> positions;
270 std::vector<VECTOR2I> extents;
271
272 getLinePositions( aText, position, strings_list, positions, extents, aAttrs, aFontMetrics );
273
274 aGal->SetLineWidth( aAttrs.m_StrokeWidth );
275
276 for( size_t i = 0; i < strings_list.GetCount(); i++ )
277 {
278 drawSingleLineText( aGal, nullptr, strings_list[i], positions[i], aAttrs.m_Size,
279 aAttrs.m_Angle, aAttrs.m_Mirrored, aPosition, aAttrs.m_Italic,
280 aAttrs.m_Underlined, aFontMetrics );
281 }
282}
283
284
288VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
289 const MARKUP::NODE* aNode, const VECTOR2I& aPosition,
290 const KIFONT::FONT* aFont, const VECTOR2I& aSize, const EDA_ANGLE& aAngle,
291 bool aMirror, const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle,
292 const METRICS& aFontMetrics )
293{
294 VECTOR2I nextPosition = aPosition;
295 bool drawUnderline = false;
296 bool drawOverbar = false;
297
298 if( aNode )
299 {
300 TEXT_STYLE_FLAGS textStyle = aTextStyle;
301
302 if( !aNode->is_root() )
303 {
304 if( aNode->isSubscript() )
305 textStyle |= TEXT_STYLE::SUBSCRIPT;
306 else if( aNode->isSuperscript() )
307 textStyle |= TEXT_STYLE::SUPERSCRIPT;
308
309 if( aNode->isOverbar() )
310 drawOverbar = true;
311
312 if( aNode->has_content() )
313 {
314 BOX2I bbox;
315
316 nextPosition = aFont->GetTextAsGlyphs( &bbox, aGlyphs, aNode->asWxString(), aSize,
317 nextPosition, aAngle, aMirror, aOrigin,
318 textStyle );
319
320 if( aBoundingBox )
321 aBoundingBox->Merge( bbox );
322 }
323 }
324 else if( aTextStyle & TEXT_STYLE::UNDERLINE )
325 {
326 drawUnderline = true;
327 }
328
329 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
330 {
331 nextPosition = drawMarkup( aBoundingBox, aGlyphs, child.get(), nextPosition, aFont,
332 aSize, aAngle, aMirror, aOrigin, textStyle, aFontMetrics );
333 }
334 }
335
336 if( drawUnderline )
337 {
338 // Shorten the bar a little so its rounded ends don't make it over-long
339 double barTrim = aSize.x * 0.1;
340 double barOffset = aFontMetrics.GetUnderlineVerticalPosition( aSize.y );
341
342 VECTOR2D barStart( aPosition.x + barTrim, aPosition.y - barOffset );
343 VECTOR2D barEnd( nextPosition.x - barTrim, nextPosition.y - barOffset );
344
345 if( aGlyphs )
346 {
347 STROKE_GLYPH barGlyph;
348
349 barGlyph.AddPoint( barStart );
350 barGlyph.AddPoint( barEnd );
351 barGlyph.Finalize();
352
353 aGlyphs->push_back( barGlyph.Transform( { 1.0, 1.0 }, { 0, 0 }, false, aAngle, aMirror,
354 aOrigin ) );
355 }
356 }
357
358 if( drawOverbar )
359 {
360 // Shorten the bar a little so its rounded ends don't make it over-long
361 double barTrim = aSize.x * 0.1;
362 double barOffset = aFontMetrics.GetOverbarVerticalPosition( aSize.y );
363
364 VECTOR2D barStart( aPosition.x + barTrim, aPosition.y - barOffset );
365 VECTOR2D barEnd( nextPosition.x - barTrim, nextPosition.y - barOffset );
366
367 if( aGlyphs )
368 {
369 STROKE_GLYPH barGlyph;
370
371 barGlyph.AddPoint( barStart );
372 barGlyph.AddPoint( barEnd );
373 barGlyph.Finalize();
374
375 aGlyphs->push_back( barGlyph.Transform( { 1.0, 1.0 }, { 0, 0 }, false, aAngle, aMirror,
376 aOrigin ) );
377 }
378 }
379
380 return nextPosition;
381}
382
383
384VECTOR2I FONT::drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
385 const wxString& aText, const VECTOR2I& aPosition, const VECTOR2I& aSize,
386 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
387 TEXT_STYLE_FLAGS aTextStyle, const METRICS& aFontMetrics ) const
388{
389 std::lock_guard<std::mutex> lock( s_markupCacheMutex );
390
391 MARKUP_CACHE::ENTRY* markup = s_markupCache.Get( aText );
392
393 if( !markup || !markup->root )
394 {
395 MARKUP_CACHE::ENTRY& cached = s_markupCache.Put( aText, {} );
396
397 cached.source = TO_UTF8( aText );
398 MARKUP::MARKUP_PARSER markupParser( &cached.source );
399 cached.root = markupParser.Parse();
400 markup = &cached;
401 }
402
403 wxASSERT( markup && markup->root );
404
405 return ::drawMarkup( aBoundingBox, aGlyphs, markup->root.get(), aPosition, this, aSize, aAngle,
406 aMirror, aOrigin, aTextStyle, aFontMetrics );
407}
408
409
410void FONT::drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
411 const VECTOR2I& aPosition, const VECTOR2I& aSize,
412 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
413 bool aItalic, bool aUnderline, const METRICS& aFontMetrics ) const
414{
415 if( !aGal )
416 return;
417
418 TEXT_STYLE_FLAGS textStyle = 0;
419
420 if( aItalic )
421 textStyle |= TEXT_STYLE::ITALIC;
422
423 if( aUnderline )
424 textStyle |= TEXT_STYLE::UNDERLINE;
425
426 std::vector<std::unique_ptr<GLYPH>> glyphs;
427
428 (void) drawMarkup( aBoundingBox, &glyphs, aText, aPosition, aSize, aAngle, aMirror, aOrigin,
429 textStyle, aFontMetrics );
430
431 aGal->DrawGlyphs( glyphs );
432}
433
434
435VECTOR2I FONT::StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSize, int aThickness,
436 bool aBold, bool aItalic, const METRICS& aFontMetrics ) const
437{
438 // TODO do we need to parse every time - have we already parsed?
440 TEXT_STYLE_FLAGS textStyle = 0;
441
442 if( aBold )
443 textStyle |= TEXT_STYLE::BOLD;
444
445 if( aItalic )
446 textStyle |= TEXT_STYLE::ITALIC;
447
448 (void) drawMarkup( &boundingBox, nullptr, aText, VECTOR2I(), aSize, ANGLE_0, false, VECTOR2I(),
449 textStyle, aFontMetrics );
450
451 if( IsStroke() )
452 {
453 // Inflate by a bit more than thickness/2 to catch diacriticals, descenders, etc.
454 boundingBox.Inflate( KiROUND( aThickness * 1.5 ) );
455 }
456 else if( IsOutline() )
457 {
458 // Outline fonts have thickness built in, and *usually* stay within their ascent/descent
459 }
460
461 return boundingBox.GetSize();
462}
463
464
465VECTOR2I FONT::boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText,
466 const VECTOR2I& aPosition, const VECTOR2I& aSize,
467 bool aItalic, const METRICS& aFontMetrics ) const
468{
469 TEXT_STYLE_FLAGS textStyle = 0;
470
471 if( aItalic )
472 textStyle |= TEXT_STYLE::ITALIC;
473
474 VECTOR2I extents = drawMarkup( aBBox, nullptr, aText, aPosition, aSize, ANGLE_0, false,
475 VECTOR2I(), textStyle, aFontMetrics );
476
477 return extents;
478}
479
480
481/*
482 * Break marked-up text into "words". In this context, a "word" is EITHER a run of marked-up
483 * text (subscript, superscript or overbar), OR a run of non-marked-up text separated by spaces.
484 */
485void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords,
486 const std::unique_ptr<MARKUP::NODE>& aNode, const KIFONT::FONT* aFont,
487 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle )
488{
489 TEXT_STYLE_FLAGS textStyle = aTextStyle;
490
491 if( !aNode->is_root() )
492 {
493 wxChar escapeChar = 0;
494
495 if( aNode->isSubscript() )
496 {
497 escapeChar = '_';
498 textStyle = TEXT_STYLE::SUBSCRIPT;
499 }
500 else if( aNode->isSuperscript() )
501 {
502 escapeChar = '^';
503 textStyle = TEXT_STYLE::SUPERSCRIPT;
504 }
505
506 if( aNode->isOverbar() )
507 {
508 escapeChar = '~';
509 textStyle |= TEXT_STYLE::OVERBAR;
510 }
511
512 if( escapeChar )
513 {
514 wxString word = wxString::Format( wxT( "%c{" ), escapeChar );
515 int width = 0;
516
517 if( aNode->has_content() )
518 {
519 VECTOR2I next = aFont->GetTextAsGlyphs( nullptr, nullptr, aNode->asWxString(),
520 aSize, { 0, 0 }, ANGLE_0, false, { 0, 0 },
521 textStyle );
522 word += aNode->asWxString();
523 width += next.x;
524 }
525
526 std::vector<std::pair<wxString, int>> childWords;
527
528 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
529 wordbreakMarkup( &childWords, child, aFont, aSize, textStyle );
530
531 for( const std::pair<wxString, int>& childWord : childWords )
532 {
533 word += childWord.first;
534 width += childWord.second;
535 }
536
537 word += wxT( "}" );
538 aWords->emplace_back( std::make_pair( word, width ) );
539 return;
540 }
541 else
542 {
543 wxString textRun = aNode->asWxString();
544 wxStringTokenizer tokenizer( textRun, " ", wxTOKEN_RET_DELIMS );
545 std::vector<wxString> words;
546
547 while( tokenizer.HasMoreTokens() )
548 words.emplace_back( tokenizer.GetNextToken() );
549
550 for( const wxString& word : words )
551 {
552 wxString chars = word;
553 chars.Trim();
554
555 int w = aFont->GetTextAsGlyphs( nullptr, nullptr, chars, aSize, { 0, 0 },
556 ANGLE_0, false, { 0, 0 }, textStyle ).x;
557
558 aWords->emplace_back( std::make_pair( word, w ) );
559 }
560 }
561 }
562
563 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
564 wordbreakMarkup( aWords, child, aFont, aSize, textStyle );
565}
566
567
568void FONT::wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
569 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const
570{
571 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( aText ) );
572 std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
573
574 ::wordbreakMarkup( aWords, root, this, aSize, aTextStyle );
575}
576
577
578/*
579 * This is a highly simplified line-breaker. KiCad is an EDA tool, not a word processor.
580 *
581 * 1) It breaks only on spaces. If you type a word wider than the column width then you get
582 * overflow.
583 * 2) It treats runs of formatted text (superscript, subscript, overbar) as single words.
584 * 3) It does not perform justification.
585 *
586 * The results of the linebreaking are the addition of \n in the text. It is presumed that this
587 * function is called on m_shownText (or equivalent) rather than the original source text.
588 */
589void FONT::LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aSize, int aThickness,
590 bool aBold, bool aItalic ) const
591{
592 TEXT_STYLE_FLAGS textStyle = 0;
593
594 if( aBold )
595 textStyle |= TEXT_STYLE::BOLD;
596
597 if( aItalic )
598 textStyle |= TEXT_STYLE::ITALIC;
599
600 int spaceWidth = GetTextAsGlyphs( nullptr, nullptr, wxS( " " ), aSize, VECTOR2I(), ANGLE_0,
601 false, VECTOR2I(), textStyle ).x;
602
603 wxArrayString textLines;
604 wxStringSplit( aText, textLines, '\n' );
605
606 aText = wxEmptyString;
607
608 for( size_t ii = 0; ii < textLines.Count(); ++ii )
609 {
610 std::vector<std::pair<wxString, int>> markup;
611 std::vector<std::pair<wxString, int>> words;
612
613 wordbreakMarkup( &markup, textLines[ii], aSize, textStyle );
614
615 for( const auto& [ run, runWidth ] : markup )
616 {
617 if( !words.empty() && !words.back().first.EndsWith( ' ' ) )
618 {
619 words.back().first += run;
620 words.back().second += runWidth;
621 }
622 else
623 {
624 words.emplace_back( std::make_pair( run, runWidth ) );
625 }
626 }
627
628 bool buryMode = false;
629 int lineWidth = 0;
630 wxString pendingSpaces;
631
632 for( const auto& [ word, wordWidth ] : words )
633 {
634 int pendingSpaceWidth = (int) pendingSpaces.Length() * spaceWidth;
635 bool overflow = lineWidth + pendingSpaceWidth + wordWidth > aColumnWidth - aThickness;
636
637 if( overflow && pendingSpaces.Length() > 0 )
638 {
639 aText += '\n';
640 lineWidth = 0;
641 pendingSpaces = wxEmptyString;
642 pendingSpaceWidth = 0;
643 buryMode = true;
644 }
645
646 if( word == wxS( " " ) )
647 {
648 pendingSpaces += word;
649 }
650 else
651 {
652 if( buryMode )
653 {
654 buryMode = false;
655 }
656 else
657 {
658 aText += pendingSpaces;
659 lineWidth += pendingSpaceWidth;
660 }
661
662 if( word.EndsWith( ' ' ) )
663 {
664 aText += word.Left( word.Length() - 1 );
665 pendingSpaces = wxS( " " );
666 }
667 else
668 {
669 aText += word;
670 pendingSpaces = wxEmptyString;
671 }
672
673 lineWidth += wordWidth;
674 }
675 }
676
677 // Add the newlines back onto the string
678 if( ii != ( textLines.Count() - 1 ) )
679 aText += '\n';
680 }
681}
682
683
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:558
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:658
constexpr const SizeVec & GetSize() const
Definition: box2.h:206
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
VECTOR2I boundingBoxSingleLine(BOX2I *aBBox, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, bool aItalic, const METRICS &aFontMetrics) const
Computes the bounding box for a single line of text.
Definition: font.cpp:465
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition: font.cpp:146
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 METRICS &aFontMetrics) const
Draws a single line of text.
Definition: font.cpp:410
virtual bool IsStroke() const
Definition: font.h:138
static FONT * s_defaultFont
Definition: font.h:282
void getLinePositions(const wxString &aText, const VECTOR2I &aPosition, wxArrayString &aTextLines, std::vector< VECTOR2I > &aPositions, std::vector< VECTOR2I > &aExtents, const TEXT_ATTRIBUTES &aAttrs, const METRICS &aFontMetrics) const
Definition: font.cpp:179
void wordbreakMarkup(std::vector< std::pair< wxString, int > > *aWords, const wxString &aText, const VECTOR2I &aSize, TEXT_STYLE_FLAGS aTextStyle) const
Definition: font.cpp:568
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics) const
Draw a string.
Definition: font.cpp:258
virtual bool IsOutline() const
Definition: font.h:139
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 METRICS &aFontMetrics) const
Definition: font.cpp:384
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic, const METRICS &aFontMetrics) const
Compute the boundary limits of aText (the bounding box of all shapes).
Definition: font.cpp:435
static std::map< std::tuple< wxString, bool, bool, bool >, FONT * > s_fontMap
Definition: font.h:284
virtual double GetInterline(double aGlyphHeight, const METRICS &aFontMetrics) const =0
Compute the distance (interline) between 2 lines of text (for multiline texts).
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.
static FONT * getDefaultFont()
Definition: font.cpp:137
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.
Definition: font.cpp:589
double GetUnderlineVerticalPosition(double aGlyphHeight) const
Compute the vertical position of an underline.
Definition: font.h:109
double GetOverbarVerticalPosition(double aGlyphHeight) const
Compute the vertical position of an overbar.
Definition: font.h:100
static const METRICS & Default()
Definition: font.cpp:52
static OUTLINE_FONT * LoadFont(const wxString &aFontFileName, bool aBold, bool aItalic, const std::vector< wxString > *aEmbeddedFiles, bool aForDrawingSheet)
Load an outline font.
static STROKE_FONT * LoadFont(const wxString &aFontName)
Load a stroke font.
Definition: stroke_font.cpp:65
void AddPoint(const VECTOR2D &aPoint)
Definition: glyph.cpp:39
std::unique_ptr< GLYPH > Transform(const VECTOR2D &aGlyphSize, const VECTOR2I &aOffset, double aTilt, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin)
Definition: glyph.cpp:74
Abstract interface for drawing on a 2D-surface.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
virtual void DrawGlyphs(const std::vector< std::unique_ptr< KIFONT::GLYPH > > &aGlyphs)
Draw polygons representing font glyphs.
std::unique_ptr< NODE > Parse()
MARKUP_CACHE(size_t aMaxSize)
Definition: font.cpp:73
void Clear()
Definition: font.cpp:115
std::unordered_map< wxString, std::list< CACHE_ENTRY >::iterator > m_cache
Definition: font.cpp:124
ENTRY & Put(const CACHE_ENTRY::first_type &aQuery, ENTRY &&aResult)
Definition: font.cpp:78
std::pair< wxString, ENTRY > CACHE_ENTRY
Definition: font.cpp:71
std::list< CACHE_ENTRY > m_cacheMru
Definition: font.cpp:123
size_t m_maxSize
Definition: font.cpp:122
ENTRY * Get(const CACHE_ENTRY::first_type &aQuery)
Definition: font.cpp:103
GR_TEXT_H_ALIGN_T m_Halign
GR_TEXT_V_ALIGN_T m_Valign
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:401
METRICS g_defaultMetrics
Definition: font.cpp:49
static std::mutex s_markupCacheMutex
Definition: font.cpp:129
void wordbreakMarkup(std::vector< std::pair< wxString, int > > *aWords, const std::unique_ptr< MARKUP::NODE > &aNode, const KIFONT::FONT *aFont, const VECTOR2I &aSize, TEXT_STYLE_FLAGS aTextStyle)
Definition: font.cpp:485
static MARKUP_CACHE s_markupCache(1024)
VECTOR2I drawMarkup(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const 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, const METRICS &aFontMetrics)
Definition: font.cpp:288
unsigned int TEXT_STYLE_FLAGS
Definition: font.h:64
#define KICAD_FONT_NAME
This file contains miscellaneous commonly used macros and functions.
CITER next(CITER it)
Definition: ptree.cpp:126
BOX2I boundingBox(T aObject, int aLayer)
Used by SHAPE_INDEX to get the bounding box of a generic T object.
Definition: shape_index.h:62
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398
bool isOverbar() const
Definition: markup_parser.h:48
bool isSuperscript() const
Definition: markup_parser.h:50
wxString asWxString() const
bool isSubscript() const
Definition: markup_parser.h:49
std::unique_ptr< MARKUP::NODE > root
Definition: font.cpp:68
std::string source
Definition: font.cpp:67
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_H_ALIGN_INDETERMINATE
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_INDETERMINATE
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691