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>, 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{
148 if( aFontName.empty() || aFontName.StartsWith( KICAD_FONT_NAME ) )
149 return getDefaultFont();
150
151 std::tuple<wxString, bool, bool> key = { aFontName, aBold, aItalic };
152
153 FONT* font = nullptr;
154
155 if( s_fontMap.find( key ) != s_fontMap.end() )
156 font = s_fontMap[key];
157
158 if( !font )
159 font = OUTLINE_FONT::LoadFont( aFontName, aBold, aItalic );
160
161 if( !font )
162 font = getDefaultFont();
163
164 s_fontMap[key] = font;
165
166 return font;
167}
168
169
170bool FONT::IsStroke( const wxString& aFontName )
171{
172 // This would need a more complex implementation if we ever support more stroke fonts
173 // than the KiCad Font.
174 return aFontName == _( "Default Font" ) || aFontName == KICAD_FONT_NAME;
175}
176
177
178void FONT::getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
179 wxArrayString& aTextLines, std::vector<VECTOR2I>& aPositions,
180 std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs,
181 const METRICS& aFontMetrics ) const
182{
183 wxStringSplit( aText, aTextLines, '\n' );
184 int lineCount = aTextLines.Count();
185 aPositions.reserve( lineCount );
186
187 int interline = GetInterline( aAttrs.m_Size.y, aFontMetrics ) * aAttrs.m_LineSpacing;
188 int height = 0;
189
190 for( int i = 0; i < lineCount; i++ )
191 {
192 VECTOR2I pos( aPosition.x, aPosition.y + i * interline );
193 VECTOR2I end = boundingBoxSingleLine( nullptr, aTextLines[i], pos, aAttrs.m_Size,
194 aAttrs.m_Italic, aFontMetrics );
195 VECTOR2I bBox( end - pos );
196
197 aExtents.push_back( bBox );
198
199 if( i == 0 )
200 height += ( aAttrs.m_Size.y * 1.17 ); // 1.17 is a fudge to match 6.0 positioning
201 else
202 height += interline;
203 }
204
205 VECTOR2I offset( 0, 0 );
206 offset.y += aAttrs.m_Size.y;
207
208 if( IsStroke() )
209 {
210 // Fudge factors to match 6.0 positioning
211 offset.x += aAttrs.m_StrokeWidth / 1.52;
212 offset.y -= aAttrs.m_StrokeWidth * 0.052;
213 }
214
215 switch( aAttrs.m_Valign )
216 {
217 case GR_TEXT_V_ALIGN_TOP: break;
218 case GR_TEXT_V_ALIGN_CENTER: offset.y -= height / 2; break;
219 case GR_TEXT_V_ALIGN_BOTTOM: offset.y -= height; break;
221 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
222 break;
223 }
224
225 for( int i = 0; i < lineCount; i++ )
226 {
227 VECTOR2I lineSize = aExtents.at( i );
228 VECTOR2I lineOffset( offset );
229
230 lineOffset.y += i * interline;
231
232 switch( aAttrs.m_Halign )
233 {
234 case GR_TEXT_H_ALIGN_LEFT: break;
235 case GR_TEXT_H_ALIGN_CENTER: lineOffset.x = -lineSize.x / 2; break;
236 case GR_TEXT_H_ALIGN_RIGHT: lineOffset.x = -( lineSize.x + offset.x ); break;
238 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
239 break;
240 }
241
242 aPositions.push_back( aPosition + lineOffset );
243 }
244}
245
246
257void FONT::Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
258 const VECTOR2I& aCursor, const TEXT_ATTRIBUTES& aAttrs,
259 const METRICS& aFontMetrics ) const
260{
261 if( !aGal || aText.empty() )
262 return;
263
264 VECTOR2I position( aPosition - aCursor );
265
266 // Split multiline strings into separate ones and draw them line by line
267 wxArrayString strings_list;
268 std::vector<VECTOR2I> positions;
269 std::vector<VECTOR2I> extents;
270
271 getLinePositions( aText, position, strings_list, positions, extents, aAttrs, aFontMetrics );
272
273 aGal->SetLineWidth( aAttrs.m_StrokeWidth );
274
275 for( size_t i = 0; i < strings_list.GetCount(); i++ )
276 {
277 drawSingleLineText( aGal, nullptr, strings_list[i], positions[i], aAttrs.m_Size,
278 aAttrs.m_Angle, aAttrs.m_Mirrored, aPosition, aAttrs.m_Italic,
279 aAttrs.m_Underlined, aFontMetrics );
280 }
281}
282
283
287VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
288 const MARKUP::NODE* aNode, const VECTOR2I& aPosition,
289 const KIFONT::FONT* aFont, const VECTOR2I& aSize, const EDA_ANGLE& aAngle,
290 bool aMirror, const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle,
291 const METRICS& aFontMetrics )
292{
293 VECTOR2I nextPosition = aPosition;
294 bool drawUnderline = false;
295 bool drawOverbar = false;
296
297 if( aNode )
298 {
299 TEXT_STYLE_FLAGS textStyle = aTextStyle;
300
301 if( !aNode->is_root() )
302 {
303 if( aNode->isSubscript() )
304 textStyle |= TEXT_STYLE::SUBSCRIPT;
305 else if( aNode->isSuperscript() )
306 textStyle |= TEXT_STYLE::SUPERSCRIPT;
307
308 if( aNode->isOverbar() )
309 drawOverbar = true;
310
311 if( aNode->has_content() )
312 {
313 BOX2I bbox;
314
315 nextPosition = aFont->GetTextAsGlyphs( &bbox, aGlyphs, aNode->asWxString(), aSize,
316 nextPosition, aAngle, aMirror, aOrigin,
317 textStyle );
318
319 if( aBoundingBox )
320 aBoundingBox->Merge( bbox );
321 }
322 }
323 else if( aTextStyle & TEXT_STYLE::UNDERLINE )
324 {
325 drawUnderline = true;
326 }
327
328 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
329 {
330 nextPosition = drawMarkup( aBoundingBox, aGlyphs, child.get(), nextPosition, aFont,
331 aSize, aAngle, aMirror, aOrigin, textStyle, aFontMetrics );
332 }
333 }
334
335 if( drawUnderline )
336 {
337 // Shorten the bar a little so its rounded ends don't make it over-long
338 double barTrim = aSize.x * 0.1;
339 double barOffset = aFontMetrics.GetUnderlineVerticalPosition( aSize.y );
340
341 VECTOR2D barStart( aPosition.x + barTrim, aPosition.y - barOffset );
342 VECTOR2D barEnd( nextPosition.x - barTrim, nextPosition.y - barOffset );
343
344 if( aGlyphs )
345 {
346 STROKE_GLYPH barGlyph;
347
348 barGlyph.AddPoint( barStart );
349 barGlyph.AddPoint( barEnd );
350 barGlyph.Finalize();
351
352 aGlyphs->push_back( barGlyph.Transform( { 1.0, 1.0 }, { 0, 0 }, false, aAngle, aMirror,
353 aOrigin ) );
354 }
355 }
356
357 if( drawOverbar )
358 {
359 // Shorten the bar a little so its rounded ends don't make it over-long
360 double barTrim = aSize.x * 0.1;
361 double barOffset = aFontMetrics.GetOverbarVerticalPosition( aSize.y );
362
363 VECTOR2D barStart( aPosition.x + barTrim, aPosition.y - barOffset );
364 VECTOR2D barEnd( nextPosition.x - barTrim, nextPosition.y - barOffset );
365
366 if( aGlyphs )
367 {
368 STROKE_GLYPH barGlyph;
369
370 barGlyph.AddPoint( barStart );
371 barGlyph.AddPoint( barEnd );
372 barGlyph.Finalize();
373
374 aGlyphs->push_back( barGlyph.Transform( { 1.0, 1.0 }, { 0, 0 }, false, aAngle, aMirror,
375 aOrigin ) );
376 }
377 }
378
379 return nextPosition;
380}
381
382
383VECTOR2I FONT::drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
384 const wxString& aText, const VECTOR2I& aPosition, const VECTOR2I& aSize,
385 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
386 TEXT_STYLE_FLAGS aTextStyle, const METRICS& aFontMetrics ) const
387{
388 std::lock_guard<std::mutex> lock( s_markupCacheMutex );
389
390 MARKUP_CACHE::ENTRY* markup = s_markupCache.Get( aText );
391
392 if( !markup || !markup->root )
393 {
394 MARKUP_CACHE::ENTRY& cached = s_markupCache.Put( aText, {} );
395
396 cached.source = TO_UTF8( aText );
397 MARKUP::MARKUP_PARSER markupParser( &cached.source );
398 cached.root = markupParser.Parse();
399 markup = &cached;
400 }
401
402 wxASSERT( markup && markup->root );
403
404 return ::drawMarkup( aBoundingBox, aGlyphs, markup->root.get(), aPosition, this, aSize, aAngle,
405 aMirror, aOrigin, aTextStyle, aFontMetrics );
406}
407
408
409void FONT::drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
410 const VECTOR2I& aPosition, const VECTOR2I& aSize,
411 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
412 bool aItalic, bool aUnderline, const METRICS& aFontMetrics ) const
413{
414 if( !aGal )
415 return;
416
417 TEXT_STYLE_FLAGS textStyle = 0;
418
419 if( aItalic )
420 textStyle |= TEXT_STYLE::ITALIC;
421
422 if( aUnderline )
423 textStyle |= TEXT_STYLE::UNDERLINE;
424
425 std::vector<std::unique_ptr<GLYPH>> glyphs;
426
427 (void) drawMarkup( aBoundingBox, &glyphs, aText, aPosition, aSize, aAngle, aMirror, aOrigin,
428 textStyle, aFontMetrics );
429
430 aGal->DrawGlyphs( glyphs );
431}
432
433
434VECTOR2I FONT::StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSize, int aThickness,
435 bool aBold, bool aItalic, const METRICS& aFontMetrics ) const
436{
437 // TODO do we need to parse every time - have we already parsed?
439 TEXT_STYLE_FLAGS textStyle = 0;
440
441 if( aBold )
442 textStyle |= TEXT_STYLE::BOLD;
443
444 if( aItalic )
445 textStyle |= TEXT_STYLE::ITALIC;
446
447 (void) drawMarkup( &boundingBox, nullptr, aText, VECTOR2I(), aSize, ANGLE_0, false, VECTOR2I(),
448 textStyle, aFontMetrics );
449
450 if( IsStroke() )
451 {
452 // Inflate by a bit more than thickness/2 to catch diacriticals, descenders, etc.
453 boundingBox.Inflate( KiROUND( aThickness * 1.5 ) );
454 }
455 else if( IsOutline() )
456 {
457 // Outline fonts have thickness built in, and *usually* stay within their ascent/descent
458 }
459
460 return boundingBox.GetSize();
461}
462
463
464VECTOR2I FONT::boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText,
465 const VECTOR2I& aPosition, const VECTOR2I& aSize,
466 bool aItalic, const METRICS& aFontMetrics ) const
467{
468 TEXT_STYLE_FLAGS textStyle = 0;
469
470 if( aItalic )
471 textStyle |= TEXT_STYLE::ITALIC;
472
473 VECTOR2I extents = drawMarkup( aBBox, nullptr, aText, aPosition, aSize, ANGLE_0, false,
474 VECTOR2I(), textStyle, aFontMetrics );
475
476 return extents;
477}
478
479
480/*
481 * Break marked-up text into "words". In this context, a "word" is EITHER a run of marked-up
482 * text (subscript, superscript or overbar), OR a run of non-marked-up text separated by spaces.
483 */
484void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords,
485 const std::unique_ptr<MARKUP::NODE>& aNode, const KIFONT::FONT* aFont,
486 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle )
487{
488 TEXT_STYLE_FLAGS textStyle = aTextStyle;
489
490 if( !aNode->is_root() )
491 {
492 wxChar escapeChar = 0;
493
494 if( aNode->isSubscript() )
495 {
496 escapeChar = '_';
497 textStyle = TEXT_STYLE::SUBSCRIPT;
498 }
499 else if( aNode->isSuperscript() )
500 {
501 escapeChar = '^';
502 textStyle = TEXT_STYLE::SUPERSCRIPT;
503 }
504
505 if( aNode->isOverbar() )
506 {
507 escapeChar = '~';
508 textStyle |= TEXT_STYLE::OVERBAR;
509 }
510
511 if( escapeChar )
512 {
513 wxString word = wxString::Format( wxT( "%c{" ), escapeChar );
514 int width = 0;
515
516 if( aNode->has_content() )
517 {
518 VECTOR2I next = aFont->GetTextAsGlyphs( nullptr, nullptr, aNode->asWxString(),
519 aSize, { 0, 0 }, ANGLE_0, false, { 0, 0 },
520 textStyle );
521 word += aNode->asWxString();
522 width += next.x;
523 }
524
525 std::vector<std::pair<wxString, int>> childWords;
526
527 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
528 wordbreakMarkup( &childWords, child, aFont, aSize, textStyle );
529
530 for( const std::pair<wxString, int>& childWord : childWords )
531 {
532 word += childWord.first;
533 width += childWord.second;
534 }
535
536 word += wxT( "}" );
537 aWords->emplace_back( std::make_pair( word, width ) );
538 return;
539 }
540 else
541 {
542 wxString textRun = aNode->asWxString();
543 wxStringTokenizer tokenizer( textRun, " ", wxTOKEN_RET_DELIMS );
544 std::vector<wxString> words;
545
546 while( tokenizer.HasMoreTokens() )
547 words.emplace_back( tokenizer.GetNextToken() );
548
549 for( const wxString& word : words )
550 {
551 wxString chars = word;
552 chars.Trim();
553
554 int w = aFont->GetTextAsGlyphs( nullptr, nullptr, chars, aSize, { 0, 0 },
555 ANGLE_0, false, { 0, 0 }, textStyle ).x;
556
557 aWords->emplace_back( std::make_pair( word, w ) );
558 }
559 }
560 }
561
562 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
563 wordbreakMarkup( aWords, child, aFont, aSize, textStyle );
564}
565
566
567void FONT::wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
568 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const
569{
570 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( aText ) );
571 std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
572
573 ::wordbreakMarkup( aWords, root, this, aSize, aTextStyle );
574}
575
576
577/*
578 * This is a highly simplified line-breaker. KiCad is an EDA tool, not a word processor.
579 *
580 * 1) It breaks only on spaces. If you type a word wider than the column width then you get
581 * overflow.
582 * 2) It treats runs of formatted text (superscript, subscript, overbar) as single words.
583 * 3) It does not perform justification.
584 *
585 * The results of the linebreaking are the addition of \n in the text. It is presumed that this
586 * function is called on m_shownText (or equivalent) rather than the original source text.
587 */
588void FONT::LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aSize, int aThickness,
589 bool aBold, bool aItalic ) const
590{
591 TEXT_STYLE_FLAGS textStyle = 0;
592
593 if( aBold )
594 textStyle |= TEXT_STYLE::BOLD;
595
596 if( aItalic )
597 textStyle |= TEXT_STYLE::ITALIC;
598
599 int spaceWidth = GetTextAsGlyphs( nullptr, nullptr, wxS( " " ), aSize, VECTOR2I(), ANGLE_0,
600 false, VECTOR2I(), textStyle ).x;
601
602 wxArrayString textLines;
603 wxStringSplit( aText, textLines, '\n' );
604
605 aText = wxEmptyString;
606
607 for( size_t ii = 0; ii < textLines.Count(); ++ii )
608 {
609 std::vector<std::pair<wxString, int>> markup;
610 std::vector<std::pair<wxString, int>> words;
611
612 wordbreakMarkup( &markup, textLines[ii], aSize, textStyle );
613
614 for( const auto& [ run, runWidth ] : markup )
615 {
616 if( !words.empty() && !words.back().first.EndsWith( ' ' ) )
617 {
618 words.back().first += run;
619 words.back().second += runWidth;
620 }
621 else
622 {
623 words.emplace_back( std::make_pair( run, runWidth ) );
624 }
625 }
626
627 bool buryMode = false;
628 int lineWidth = 0;
629 wxString pendingSpaces;
630
631 for( const auto& [ word, wordWidth ] : words )
632 {
633 int pendingSpaceWidth = (int) pendingSpaces.Length() * spaceWidth;
634 bool overflow = lineWidth + pendingSpaceWidth + wordWidth > aColumnWidth - aThickness;
635
636 if( overflow && pendingSpaces.Length() > 0 )
637 {
638 aText += '\n';
639 lineWidth = 0;
640 pendingSpaces = wxEmptyString;
641 pendingSpaceWidth = 0;
642 buryMode = true;
643 }
644
645 if( word == wxS( " " ) )
646 {
647 pendingSpaces += word;
648 }
649 else
650 {
651 if( buryMode )
652 {
653 buryMode = false;
654 }
655 else
656 {
657 aText += pendingSpaces;
658 lineWidth += pendingSpaceWidth;
659 }
660
661 if( word.EndsWith( ' ' ) )
662 {
663 aText += word.Left( word.Length() - 1 );
664 pendingSpaces = wxS( " " );
665 }
666 else
667 {
668 aText += word;
669 pendingSpaces = wxEmptyString;
670 }
671
672 lineWidth += wordWidth;
673 }
674 }
675
676 // Add the newlines back onto the string
677 if( ii != ( textLines.Count() - 1 ) )
678 aText += '\n';
679 }
680}
681
682
const SizeVec & GetSize() const
Definition: box2.h:196
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:541
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:623
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:146
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:464
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:409
virtual bool IsStroke() const
Definition: font.h:138
static FONT * s_defaultFont
Definition: font.h:280
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:178
void wordbreakMarkup(std::vector< std::pair< wxString, int > > *aWords, const wxString &aText, const VECTOR2I &aSize, TEXT_STYLE_FLAGS aTextStyle) const
Definition: font.cpp:567
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:257
virtual bool IsOutline() const
Definition: font.h:139
static std::map< std::tuple< wxString, bool, bool >, FONT * > s_fontMap
Definition: font.h:282
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:383
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:434
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:588
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)
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:435
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:484
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:287
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)
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:391
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
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:118
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588