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 The 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <list>
24#include <mutex>
25#include <unordered_map>
26
27#include <macros.h>
28#include <string_utils.h>
30#include <font/stroke_font.h>
31#include <font/outline_font.h>
32#include <trigo.h>
33#include <markup_parser.h>
34
35// The "official" name of the Kicad stroke font (always existing)
37#include <wx/tokenzr.h>
38
39// markup_parser.h includes pegtl.hpp which includes windows.h... which leaks #define DrawText
40#undef DrawText
41
42
43using namespace KIFONT;
44
46
47
49{
50 return g_defaultMetrics;
51}
52
53
54FONT* FONT::s_defaultFont = nullptr;
55
56std::map< std::tuple<wxString, bool, bool, bool>, FONT*> FONT::s_fontMap;
57
59{
60public:
61 struct ENTRY
62 {
63 std::string source;
64 std::unique_ptr<MARKUP::NODE> root;
65 };
66
67 MARKUP_CACHE( size_t aMaxSize ) :
68 m_maxSize( aMaxSize )
69 {
70 }
71
72 ENTRY& Put( const wxString& aQuery, ENTRY&& aResult )
73 {
74 auto it = m_cache.find( aQuery );
75
76 m_cacheMru.emplace_front( std::make_pair( aQuery, std::move( aResult ) ) );
77
78 if( it != m_cache.end() )
79 {
80 m_cacheMru.erase( it->second );
81 m_cache.erase( it );
82 }
83
84 m_cache[aQuery] = m_cacheMru.begin();
85
86 if( m_cache.size() > m_maxSize )
87 {
88 auto last = m_cacheMru.end();
89 last--;
90 m_cache.erase( last->first );
91 m_cacheMru.pop_back();
92 }
93
94 return m_cacheMru.begin()->second;
95 }
96
97 ENTRY* Get( const wxString& aQuery )
98 {
99 auto it = m_cache.find( aQuery );
100
101 if( it == m_cache.end() )
102 return nullptr;
103
104 m_cacheMru.splice( m_cacheMru.begin(), m_cacheMru, it->second );
105
106 return &m_cacheMru.begin()->second;
107 }
108
109 void Clear()
110 {
111 m_cacheMru.clear();
112 m_cache.clear();
113 }
114
115private:
116 size_t m_maxSize;
117 std::list<std::pair<wxString, ENTRY>> m_cacheMru;
118 std::unordered_map<wxString, std::list<std::pair<wxString, ENTRY>>::iterator> m_cache;
119};
120
121
123static std::mutex s_markupCacheMutex;
124static std::mutex s_defaultFontMutex;;
125
126
128{
129}
130
131
133{
134 std::lock_guard lock( s_defaultFontMutex );
135
136 if( !s_defaultFont )
137 s_defaultFont = STROKE_FONT::LoadFont( wxEmptyString );
138
139 return s_defaultFont;
140}
141
142
143FONT* FONT::GetFont( const wxString& aFontName, bool aBold, bool aItalic,
144 const std::vector<wxString>* aEmbeddedFiles, bool aForDrawingSheet )
145{
146 if( aFontName.empty() || aFontName.StartsWith( KICAD_FONT_NAME ) )
147 return getDefaultFont();
148
149 std::tuple<wxString, bool, bool, bool> key = { aFontName, aBold, aItalic, aForDrawingSheet };
150
151 FONT* font = nullptr;
152
153 if( s_fontMap.find( key ) != s_fontMap.end() )
154 font = s_fontMap[key];
155
156 if( !font )
157 font = OUTLINE_FONT::LoadFont( aFontName, aBold, aItalic, aEmbeddedFiles,
158 aForDrawingSheet );
159
160 if( !font )
161 font = getDefaultFont();
162
163 s_fontMap[key] = font;
164
165 return font;
166}
167
168
169bool FONT::IsStroke( const wxString& aFontName )
170{
171 // This would need a more complex implementation if we ever support more stroke fonts
172 // than the KiCad Font.
173 return aFontName == _( "Default Font" ) || aFontName == KICAD_FONT_NAME;
174}
175
176
177void FONT::getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
178 wxArrayString& aTextLines, std::vector<VECTOR2I>& aPositions,
179 std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs,
180 const METRICS& aFontMetrics ) const
181{
182 wxStringSplit( aText, aTextLines, '\n' );
183 int lineCount = aTextLines.Count();
184 aPositions.reserve( lineCount );
185
186 int interline = GetInterline( aAttrs.m_Size.y, aFontMetrics ) * aAttrs.m_LineSpacing;
187 int height = 0;
188
189 for( int i = 0; i < lineCount; i++ )
190 {
191 VECTOR2I pos( aPosition.x, aPosition.y + i * interline );
192 VECTOR2I end = boundingBoxSingleLine( nullptr, aTextLines[i], pos, aAttrs.m_Size,
193 aAttrs.m_Italic, aFontMetrics );
194 VECTOR2I bBox( end - pos );
195
196 aExtents.push_back( bBox );
197
198 if( i == 0 )
199 height += ( aAttrs.m_Size.y * 1.17 ); // 1.17 is a fudge to match 6.0 positioning
200 else
201 height += interline;
202 }
203
204 VECTOR2I offset( 0, 0 );
205 offset.y += aAttrs.m_Size.y;
206
207 if( IsStroke() )
208 {
209 // Fudge factors to match 6.0 positioning
210 offset.x += aAttrs.m_StrokeWidth / 1.52;
211 offset.y -= aAttrs.m_StrokeWidth * 0.052;
212 }
213
214 switch( aAttrs.m_Valign )
215 {
216 case GR_TEXT_V_ALIGN_TOP: break;
217 case GR_TEXT_V_ALIGN_CENTER: offset.y -= height / 2; break;
218 case GR_TEXT_V_ALIGN_BOTTOM: offset.y -= height; break;
220 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
221 break;
222 }
223
224 for( int i = 0; i < lineCount; i++ )
225 {
226 VECTOR2I lineSize = aExtents.at( i );
227 VECTOR2I lineOffset( offset );
228
229 lineOffset.y += i * interline;
230
231 switch( aAttrs.m_Halign )
232 {
233 case GR_TEXT_H_ALIGN_LEFT: break;
234 case GR_TEXT_H_ALIGN_CENTER: lineOffset.x = -lineSize.x / 2; break;
235 case GR_TEXT_H_ALIGN_RIGHT: lineOffset.x = -( lineSize.x + offset.x ); break;
237 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
238 break;
239 }
240
241 aPositions.push_back( aPosition + lineOffset );
242 }
243}
244
245
246void FONT::Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition, const VECTOR2I& aCursor,
247 const TEXT_ATTRIBUTES& aAttrs, const METRICS& aFontMetrics, std::optional<VECTOR2I> aMousePos,
248 wxString* aActiveUrl ) const
249{
250 if( !aGal || aText.empty() )
251 return;
252
253 VECTOR2I position( aPosition - aCursor );
254
255 // Split multiline strings into separate ones and draw them line by line
256 wxArrayString strings_list;
257 std::vector<VECTOR2I> positions;
258 std::vector<VECTOR2I> extents;
259
260 getLinePositions( aText, position, strings_list, positions, extents, aAttrs, aFontMetrics );
261
262 aGal->SetLineWidth( aAttrs.m_StrokeWidth );
263
264 for( size_t i = 0; i < strings_list.GetCount(); i++ )
265 {
266 drawSingleLineText( aGal, nullptr, strings_list[i], positions[i], aAttrs.m_Size, aAttrs.m_Angle,
267 aAttrs.m_Mirrored, aPosition, aAttrs.m_Italic, aAttrs.m_Underlined, aAttrs.m_Hover,
268 aFontMetrics, aMousePos, aActiveUrl );
269 }
270}
271
272
276VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs, const MARKUP::NODE* aNode,
277 const VECTOR2I& aPosition, const KIFONT::FONT* aFont, const VECTOR2I& aSize,
278 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle,
279 const METRICS& aFontMetrics, std::optional<VECTOR2I> aMousePos, wxString* aActiveUrl )
280{
281 VECTOR2I nextPosition = aPosition;
282 bool drawUnderline = false;
283 bool drawOverbar = false;
284 bool useHoverColor = false;
285 int start = aGlyphs ? (int) aGlyphs->size() : 0;
286
287
288 if( aNode )
289 {
290 TEXT_STYLE_FLAGS textStyle = aTextStyle;
291
292 if( !aNode->is_root() )
293 {
294 if( aNode->isSubscript() )
295 textStyle |= TEXT_STYLE::SUBSCRIPT;
296 else if( aNode->isSuperscript() )
297 textStyle |= TEXT_STYLE::SUPERSCRIPT;
298
299 if( aNode->isOverbar() )
300 drawOverbar = true;
301
302 if( aTextStyle & TEXT_STYLE::UNDERLINE )
303 drawUnderline = 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, textStyle );
311
312 if( aBoundingBox )
313 aBoundingBox->Merge( bbox );
314
315 if( aNode->isURL() && aMousePos.has_value() && bbox.Contains( aMousePos.value() ) )
316 {
317 useHoverColor = true;
318 drawUnderline = true;
319 }
320 }
321 }
322
323 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
324 {
325 nextPosition = drawMarkup( aBoundingBox, aGlyphs, child.get(), nextPosition, aFont, aSize,
326 aAngle, aMirror, aOrigin, textStyle, aFontMetrics, aMousePos,
327 aActiveUrl );
328 }
329 }
330
331 if( drawUnderline )
332 {
333 // Shorten the bar a little so its rounded ends don't make it over-long
334 double barTrim = aSize.x * 0.1;
335 double barOffset = aFontMetrics.GetUnderlineVerticalPosition( aSize.y );
336
337 VECTOR2D barStart( aPosition.x + barTrim, aPosition.y - barOffset );
338 VECTOR2D barEnd( nextPosition.x - barTrim, nextPosition.y - barOffset );
339
340 if( aGlyphs )
341 {
342 STROKE_GLYPH barGlyph;
343
344 barGlyph.AddPoint( barStart );
345 barGlyph.AddPoint( barEnd );
346 barGlyph.Finalize();
347
348 aGlyphs->push_back( barGlyph.Transform( { 1.0, 1.0 }, { 0, 0 }, false, aAngle, aMirror,
349 aOrigin ) );
350 }
351 }
352
353 if( drawOverbar )
354 {
355 // Shorten the bar a little so its rounded ends don't make it over-long
356 double barTrim = aSize.x * 0.1;
357 double barOffset = aFontMetrics.GetOverbarVerticalPosition( aSize.y );
358
359 VECTOR2D barStart( aPosition.x + barTrim, aPosition.y - barOffset );
360 VECTOR2D barEnd( nextPosition.x - barTrim, nextPosition.y - barOffset );
361
362 if( aGlyphs )
363 {
364 STROKE_GLYPH barGlyph;
365
366 barGlyph.AddPoint( barStart );
367 barGlyph.AddPoint( barEnd );
368 barGlyph.Finalize();
369
370 aGlyphs->push_back( barGlyph.Transform( { 1.0, 1.0 }, { 0, 0 }, false, aAngle, aMirror,
371 aOrigin ) );
372 }
373 }
374
375 if( useHoverColor )
376 {
377 for( int ii = start; ii < (int) aGlyphs->size(); ++ii )
378 (*aGlyphs)[ii]->SetIsHover( true );
379
380 if( aActiveUrl && aActiveUrl->IsEmpty() )
381 *aActiveUrl = aNode->asWxString();
382 }
383
384 return nextPosition;
385}
386
387
388VECTOR2I FONT::drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
389 const wxString& aText, const VECTOR2I& aPosition, const VECTOR2I& aSize,
390 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
391 TEXT_STYLE_FLAGS aTextStyle, const METRICS& aFontMetrics,
392 std::optional<VECTOR2I> aMousePos, wxString* aActiveUrl ) const
393{
394 std::lock_guard<std::mutex> lock( s_markupCacheMutex );
395
396 MARKUP_CACHE::ENTRY* markup = s_markupCache.Get( aText );
397
398 if( !markup || !markup->root )
399 {
400 MARKUP_CACHE::ENTRY& cached = s_markupCache.Put( aText, {} );
401
402 cached.source = TO_UTF8( aText );
403 MARKUP::MARKUP_PARSER markupParser( &cached.source );
404 cached.root = markupParser.Parse();
405 markup = &cached;
406 }
407
408 wxASSERT( markup && markup->root );
409
410 return ::drawMarkup( aBoundingBox, aGlyphs, markup->root.get(), aPosition, this, aSize, aAngle,
411 aMirror, aOrigin, aTextStyle, aFontMetrics, aMousePos, aActiveUrl );
412}
413
414
415void FONT::drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
416 const VECTOR2I& aPosition, const VECTOR2I& aSize, const EDA_ANGLE& aAngle,
417 bool aMirror, const VECTOR2I& aOrigin, bool aItalic, bool aUnderline,
418 bool aHover, const METRICS& aFontMetrics, std::optional<VECTOR2I> aMousePos,
419 wxString* aActiveUrl ) const
420{
421 if( !aGal )
422 return;
423
424 TEXT_STYLE_FLAGS textStyle = 0;
425
426 if( aItalic )
427 textStyle |= TEXT_STYLE::ITALIC;
428
429 if( aUnderline )
430 textStyle |= TEXT_STYLE::UNDERLINE;
431
432 std::vector<std::unique_ptr<GLYPH>> glyphs;
433
434 (void) drawMarkup( aBoundingBox, &glyphs, aText, aPosition, aSize, aAngle, aMirror, aOrigin,
435 textStyle, aFontMetrics, aMousePos, aActiveUrl );
436
437 if( aHover )
438 {
439 for( std::unique_ptr<GLYPH>& glyph : glyphs )
440 glyph->SetIsHover( true );
441 }
442
443 aGal->DrawGlyphs( glyphs );
444}
445
446
447VECTOR2I FONT::StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSize, int aThickness,
448 bool aBold, bool aItalic, const METRICS& aFontMetrics ) const
449{
450 // TODO do we need to parse every time - have we already parsed?
452 TEXT_STYLE_FLAGS textStyle = 0;
453
454 if( aBold )
455 textStyle |= TEXT_STYLE::BOLD;
456
457 if( aItalic )
458 textStyle |= TEXT_STYLE::ITALIC;
459
460 (void) drawMarkup( &boundingBox, nullptr, aText, VECTOR2I(), aSize, ANGLE_0, false, VECTOR2I(),
461 textStyle, aFontMetrics );
462
463 if( IsStroke() )
464 {
465 // Inflate by a bit more than thickness/2 to catch diacriticals, descenders, etc.
466 boundingBox.Inflate( KiROUND( aThickness * 1.5 ) );
467 }
468 else if( IsOutline() )
469 {
470 // Outline fonts have thickness built in, and *usually* stay within their ascent/descent
471 }
472
473 return boundingBox.GetSize();
474}
475
476
477VECTOR2I FONT::boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText,
478 const VECTOR2I& aPosition, const VECTOR2I& aSize,
479 bool aItalic, const METRICS& aFontMetrics ) const
480{
481 TEXT_STYLE_FLAGS textStyle = 0;
482
483 if( aItalic )
484 textStyle |= TEXT_STYLE::ITALIC;
485
486 VECTOR2I extents = drawMarkup( aBBox, nullptr, aText, aPosition, aSize, ANGLE_0, false,
487 VECTOR2I(), textStyle, aFontMetrics );
488
489 return extents;
490}
491
492
499void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords,
500 const std::unique_ptr<MARKUP::NODE>& aNode, const KIFONT::FONT* aFont,
501 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle,
502 bool aInsideMarkup = false )
503{
504 TEXT_STYLE_FLAGS textStyle = aTextStyle;
505
506 if( !aNode->is_root() )
507 {
508 wxChar escapeChar = 0;
509
510 if( aNode->isSubscript() )
511 {
512 escapeChar = '_';
513 textStyle = TEXT_STYLE::SUBSCRIPT;
514 }
515 else if( aNode->isSuperscript() )
516 {
517 escapeChar = '^';
518 textStyle = TEXT_STYLE::SUPERSCRIPT;
519 }
520
521 if( aNode->isOverbar() )
522 {
523 escapeChar = '~';
524 textStyle |= TEXT_STYLE::OVERBAR;
525 }
526
527 if( escapeChar )
528 {
529 wxString word = wxString::Format( wxT( "%c{" ), escapeChar );
530 int width = 0;
531
532 if( aNode->has_content() )
533 {
534 VECTOR2I next = aFont->GetTextAsGlyphs( nullptr, nullptr, aNode->asWxString(),
535 aSize, { 0, 0 }, ANGLE_0, false, { 0, 0 },
536 textStyle );
537 word += aNode->asWxString();
538 width += next.x;
539 }
540
541 std::vector<std::pair<wxString, int>> childWords;
542
543 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
544 wordbreakMarkup( &childWords, child, aFont, aSize, textStyle, true );
545
546 for( const std::pair<wxString, int>& childWord : childWords )
547 {
548 word += childWord.first;
549 width += childWord.second;
550 }
551
552 word += wxT( "}" );
553 aWords->emplace_back( std::make_pair( word, width ) );
554 return;
555 }
556 else if( aInsideMarkup )
557 {
558 // Inside a markup node, preserve the content verbatim (no tokenization).
559 // wxStringTokenizer collapses consecutive spaces, which corrupts content
560 // like ~{ } where spaces are intentional.
561 wxString content = aNode->asWxString();
562 int w = aFont->GetTextAsGlyphs( nullptr, nullptr, content, aSize, { 0, 0 },
563 ANGLE_0, false, { 0, 0 }, textStyle ).x;
564
565 aWords->emplace_back( std::make_pair( content, w ) );
566 }
567 else
568 {
569 wxString textRun = aNode->asWxString();
570 wxStringTokenizer tokenizer( textRun, " ", wxTOKEN_RET_DELIMS );
571 std::vector<wxString> words;
572
573 while( tokenizer.HasMoreTokens() )
574 words.emplace_back( tokenizer.GetNextToken() );
575
576 for( const wxString& word : words )
577 {
578 wxString chars = word;
579 chars.Trim();
580
581 int w = aFont->GetTextAsGlyphs( nullptr, nullptr, chars.IsEmpty() ? word : chars,
582 aSize, { 0, 0 }, ANGLE_0, false, { 0, 0 },
583 textStyle ).x;
584
585 aWords->emplace_back( std::make_pair( word, w ) );
586 }
587 }
588 }
589
590 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
591 wordbreakMarkup( aWords, child, aFont, aSize, textStyle, aInsideMarkup );
592}
593
594
595void FONT::wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
596 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const
597{
598 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( aText ) );
599 std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
600
601 ::wordbreakMarkup( aWords, root, this, aSize, aTextStyle );
602}
603
604
605void FONT::LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aSize, int aThickness,
606 bool aBold, bool aItalic ) const
607{
608 TEXT_STYLE_FLAGS textStyle = 0;
609
610 if( aBold )
611 textStyle |= TEXT_STYLE::BOLD;
612
613 if( aItalic )
614 textStyle |= TEXT_STYLE::ITALIC;
615
616 int spaceWidth = GetTextAsGlyphs( nullptr, nullptr, wxS( " " ), aSize, VECTOR2I(), ANGLE_0,
617 false, VECTOR2I(), textStyle ).x;
618
619 wxArrayString textLines;
620 wxStringSplit( aText, textLines, '\n' );
621
622 aText = wxEmptyString;
623
624 for( size_t ii = 0; ii < textLines.Count(); ++ii )
625 {
626 std::vector<std::pair<wxString, int>> markup;
627 std::vector<std::pair<wxString, int>> words;
628
629 wordbreakMarkup( &markup, textLines[ii], aSize, textStyle );
630
631 for( const auto& [ run, runWidth ] : markup )
632 {
633 if( !words.empty() && !words.back().first.EndsWith( ' ' ) )
634 {
635 words.back().first += run;
636 words.back().second += runWidth;
637 }
638 else
639 {
640 words.emplace_back( std::make_pair( run, runWidth ) );
641 }
642 }
643
644 bool buryMode = false;
645 int lineWidth = 0;
646 wxString pendingSpaces;
647
648 for( const auto& [ word, wordWidth ] : words )
649 {
650 int pendingSpaceWidth = (int) pendingSpaces.Length() * spaceWidth;
651 bool overflow = lineWidth + pendingSpaceWidth + wordWidth > aColumnWidth - aThickness;
652
653 if( overflow && pendingSpaces.Length() > 0 )
654 {
655 aText += '\n';
656 lineWidth = 0;
657 pendingSpaces = wxEmptyString;
658 pendingSpaceWidth = 0;
659 buryMode = true;
660 }
661
662 if( word == wxS( " " ) )
663 {
664 pendingSpaces += word;
665 }
666 else
667 {
668 if( buryMode )
669 {
670 buryMode = false;
671 }
672 else
673 {
674 aText += pendingSpaces;
675 lineWidth += pendingSpaceWidth;
676 }
677
678 if( word.EndsWith( ' ' ) )
679 {
680 aText += word.Left( word.Length() - 1 );
681 pendingSpaces = wxS( " " );
682 }
683 else
684 {
685 aText += word;
686 pendingSpaces = wxEmptyString;
687 }
688
689 lineWidth += wordWidth;
690 }
691 }
692
693 // Add the newlines back onto the string
694 if( ii != ( textLines.Count() - 1 ) )
695 aText += '\n';
696 }
697}
698
699
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:654
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:164
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:94
VECTOR2I boundingBoxSingleLine(BOX2I *aBBox, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, bool aItalic, const METRICS &aFontMetrics) const
Compute the bounding box for a single line of text.
Definition font.cpp:477
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:143
virtual bool IsStroke() const
Definition font.h:101
static FONT * s_defaultFont
Definition font.h:266
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:177
void wordbreakMarkup(std::vector< std::pair< wxString, int > > *aWords, const wxString &aText, const VECTOR2I &aSize, TEXT_STYLE_FLAGS aTextStyle) const
Definition font.cpp:595
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics, std::optional< VECTOR2I > aMousePos=std::nullopt, wxString *aActiveUrl=nullptr) const
Draw a string.
Definition font.cpp:246
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, bool aHover, const METRICS &aFontMetrics, std::optional< VECTOR2I > aMousePos, wxString *aActiveUrl) const
Draw a single line of text.
Definition font.cpp:415
virtual bool IsOutline() const
Definition font.h:102
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, std::optional< VECTOR2I > aMousePos=std::nullopt, wxString *aActiveUrl=nullptr) const
Definition font.cpp:388
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:447
static std::map< std::tuple< wxString, bool, bool, bool >, FONT * > s_fontMap
Definition font.h:268
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:132
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:605
double GetUnderlineVerticalPosition(double aGlyphHeight) const
Compute the vertical position of an underline.
double GetOverbarVerticalPosition(double aGlyphHeight) const
Compute the vertical position of an overbar.
static const METRICS & Default()
Definition font.cpp:48
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.
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()
std::list< std::pair< wxString, ENTRY > > m_cacheMru
Definition font.cpp:117
MARKUP_CACHE(size_t aMaxSize)
Definition font.cpp:67
ENTRY & Put(const wxString &aQuery, ENTRY &&aResult)
Definition font.cpp:72
void Clear()
Definition font.cpp:109
size_t m_maxSize
Definition font.cpp:116
ENTRY * Get(const wxString &aQuery)
Definition font.cpp:97
std::unordered_map< wxString, std::list< std::pair< wxString, ENTRY > >::iterator > m_cache
Definition font.cpp:118
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:411
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, bool aInsideMarkup=false)
Break marked-up text into "words".
Definition font.cpp:499
METRICS g_defaultMetrics
Definition font.cpp:45
static std::mutex s_markupCacheMutex
Definition font.cpp:123
static std::mutex s_defaultFontMutex
Definition font.cpp:124
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, std::optional< VECTOR2I > aMousePos, wxString *aActiveUrl)
Definition font.cpp:276
@ BOLD
Definition font.h:43
@ SUBSCRIPT
Definition font.h:45
@ OVERBAR
Definition font.h:47
@ UNDERLINE
Definition font.h:48
@ ITALIC
Definition font.h:44
@ SUPERSCRIPT
Definition font.h:46
unsigned int TEXT_STYLE_FLAGS
Definition font.h:61
#define KICAD_FONT_NAME
This file contains miscellaneous commonly used macros and functions.
CITER next(CITER it)
Definition ptree.cpp:120
BOX2I boundingBox(T aObject, int aLayer)
Used by SHAPE_INDEX to get the bounding box of a generic T object.
Definition shape_index.h:58
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.
bool isOverbar() const
bool isURL() const
bool isSuperscript() const
wxString asWxString() const
bool isSubscript() const
std::unique_ptr< MARKUP::NODE > root
Definition font.cpp:64
std::string source
Definition font.cpp:63
VECTOR2I end
@ 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:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682