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