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