KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_text.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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <algorithm> // for max
22#include <stddef.h> // for NULL
23#include <type_traits> // for swap
24#include <vector>
25#include <mutex>
26
27#include <eda_item.h>
28#include <base_units.h>
29#include <callback_gal.h>
30#include <api/api_utils.h>
31#include <eda_text.h> // for EDA_TEXT, TEXT_EFFECTS, GR_TEXT_VJUSTIF...
32#include <gal/color4d.h> // for COLOR4D, COLOR4D::BLACK
33#include <font/glyph_types.h>
34#include <gr_text.h>
35#include <string_utils.h> // for UnescapeString
37#include <common.h>
38#include <math/util.h> // for KiROUND
39#include <math/vector2d.h>
40#include <core/kicad_algo.h>
41#include <richio.h>
42#include <render_settings.h>
43#include <trigo.h> // for RotatePoint
44#include <i18n_utility.h>
48#include <font/outline_font.h>
51#include <properties/property.h>
53#include <ctl_flags.h>
54#include <markup_parser.h>
55#include <api/api_enums.h>
56#include <api/api_utils.h>
57#include <api/common/types/base_types.pb.h>
58
59#include <wx/debug.h> // for wxASSERT
60#include <wx/string.h>
61#include <wx/url.h> // for wxURL
64#include "font/fontconfig.h"
65#include "pgm_base.h"
66
67class OUTPUTFORMATTER;
68
69
71{
72 wxASSERT( aHorizJustify >= GR_TEXT_H_ALIGN_LEFT && aHorizJustify <= GR_TEXT_H_ALIGN_RIGHT );
73
74 if( aHorizJustify > GR_TEXT_H_ALIGN_RIGHT )
76
77 if( aHorizJustify < GR_TEXT_H_ALIGN_LEFT )
79
80 return static_cast<GR_TEXT_H_ALIGN_T>( aHorizJustify );
81}
82
83
85{
86 wxASSERT( aVertJustify >= GR_TEXT_V_ALIGN_TOP && aVertJustify <= GR_TEXT_V_ALIGN_BOTTOM );
87
88 if( aVertJustify > GR_TEXT_V_ALIGN_BOTTOM )
90
91 if( aVertJustify < GR_TEXT_V_ALIGN_TOP )
93
94 return static_cast<GR_TEXT_V_ALIGN_T>( aVertJustify );
95}
96
97
98EDA_TEXT::EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText ) :
99 m_text( aText ),
100 m_IuScale( aIuScale ),
101 m_visible( true )
102{
105
107}
108
109
111 m_IuScale( aText.m_IuScale )
112{
113 m_text = aText.m_text;
117
118 m_hyperlink = aText.m_hyperlink;
119
121 m_pos = aText.m_pos;
122 m_visible = aText.m_visible;
123
124 m_render_cache.reset();
125
126 {
127 std::lock_guard<std::mutex> bboxLock( aText.m_bbox_cacheMutex );
129 }
130
132}
133
134
138
139
141{
142 if( this == &aText )
143 return *this;
144
145 m_text = aText.m_text;
149
150 m_hyperlink = aText.m_hyperlink;
151 m_activeUrl = wxEmptyString;
152
154 m_pos = aText.m_pos;
155 m_visible = aText.m_visible;
156
157 m_render_cache.reset();
158
159 {
160 std::scoped_lock<std::mutex, std::mutex> bboxLock( m_bbox_cacheMutex, aText.m_bbox_cacheMutex );
162 }
163
165
166 return *this;
167}
168
169
170void EDA_TEXT::Serialize( google::protobuf::Any& aContainer ) const
171{
172 Serialize( aContainer, pcbIUScale );
173}
174
175
176void EDA_TEXT::Serialize( kiapi::common::types::Text& text, const EDA_IU_SCALE& aScale ) const
177{
178 using namespace kiapi::common;
179
180 text.set_text( GetText().ToUTF8() );
181 text.set_hyperlink( GetHyperlink().ToUTF8() );
182 PackVector2( *text.mutable_position(), GetTextPos(), aScale );
183
184 types::TextAttributes* attrs = text.mutable_attributes();
185
186 PackTextAttributes( *attrs, GetAttributes(), aScale );
187 attrs->set_visible( true );
188}
189
190
191void EDA_TEXT::Serialize( google::protobuf::Any& aContainer, const EDA_IU_SCALE& aScale ) const
192{
193 kiapi::common::types::Text text;
194 Serialize( text, aScale );
195
196 if( !aContainer.PackFrom( text ) )
197 wxFAIL_MSG( wxT( "Failed to pack EDA_TEXT" ) );
198}
199
200
201bool EDA_TEXT::Deserialize( const google::protobuf::Any& aContainer )
202{
203 return Deserialize( aContainer, pcbIUScale );
204}
205
206
207bool EDA_TEXT::Deserialize( const kiapi::common::types::Text& text, const EDA_IU_SCALE& aScale )
208{
209 using namespace kiapi::common;
210
211 SetText( wxString( text.text().c_str(), wxConvUTF8 ) );
212 SetHyperlink( wxString( text.hyperlink().c_str(), wxConvUTF8 ) );
213 SetTextPos( UnpackVector2( text.position(), aScale ) );
214
215 if( text.has_attributes() )
216 {
218 UnpackTextAttributes( attrs, text.attributes(), aScale );
219 SetAttributes( attrs );
220 }
221
222 return true;
223}
224
225
226bool EDA_TEXT::Deserialize( const google::protobuf::Any& aContainer, const EDA_IU_SCALE& aScale )
227{
228 kiapi::common::types::Text text;
229
230 if( !aContainer.UnpackTo( &text ) )
231 return false;
232
233 return Deserialize( text, aScale );
234}
235
236
237void EDA_TEXT::SetText( const wxString& aText )
238{
239 m_text = aText;
241}
242
243
244void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
245{
246 m_text = aSrc.m_text;
248}
249
250
252{
253 m_attributes.m_StrokeWidth = aWidth;
256}
257
258
260{
261 if( GetAutoThickness() != aAuto )
262 {
263 // Freeze the base width, not the effective one; Bold still multiplies at render time.
265 }
266}
267
268
270{
271 m_attributes.m_Angle = aAngle;
274}
275
276
278{
279 if( const KIFONT::FONT* font = GetFont() )
280 return font->IsStroke();
281
282 // The font is usually unresolved during load; key off the stored face name so callers
283 // that run before font resolution (e.g. migration) still classify correctly.
284 if( !m_unresolvedFontName.IsEmpty() )
286
287 return true;
288}
289
290
291void EDA_TEXT::SetItalic( bool aItalic )
292{
293 if( m_attributes.m_Italic != aItalic )
294 {
295 // For outline fonts, italic-ness is determined by the font itself.
296 if( const KIFONT::FONT* font = GetFont(); font && !isStrokeFont() )
297 SetFont( KIFONT::FONT::GetFont( font->GetName(), IsBold(), aItalic ) );
298 }
299
300 SetItalicFlag( aItalic );
301}
302
303void EDA_TEXT::SetItalicFlag( bool aItalic )
304{
305 m_attributes.m_Italic = aItalic;
308}
309
310
311void EDA_TEXT::SetBold( bool aBold )
312{
313 if( m_attributes.m_Bold != aBold )
314 {
315 // Outline fonts carry weight in the typeface; switch variant. Stroke fonts leave the
316 // stored width alone and scale it at render time.
317 if( const KIFONT::FONT* font = GetFont(); font && !isStrokeFont() )
318 SetFont( KIFONT::FONT::GetFont( font->GetName(), aBold, IsItalic() ) );
319 }
320
321 SetBoldFlag( aBold );
322}
323
324
325void EDA_TEXT::SetBoldFlag( bool aBold )
326{
327 m_attributes.m_Bold = aBold;
330}
331
332
334{
335 int thickness = GetTextThickness();
336
337 // Outline faces never baked bold into the width; skip them.
338 if( !IsBold() || thickness <= 1 || !isStrokeFont() )
339 return;
340
341 // Keep the base above the auto threshold so a very thin width doesn't become auto. Legacy
342 // widths below ~3 IU (well under any physically meaningful stroke) can't round-trip exactly
343 // through this floor; the effective width comes out slightly larger than before migration.
344 SetTextThickness( std::max( 2, KiROUND( thickness / BOLD_STROKE_MULTIPLIER ) ) );
345}
346
347
348void EDA_TEXT::SetVisible( bool aVisible )
349{
350 m_visible = aVisible;
352}
353
354
355void EDA_TEXT::SetMirrored( bool isMirrored )
356{
357 m_attributes.m_Mirrored = isMirrored;
360}
361
362
364{
365 m_attributes.m_Multiline = aAllow;
368}
369
370
372{
373 m_attributes.m_Halign = aType;
376}
377
378
380{
381 m_attributes.m_Valign = aType;
384}
385
386
387void EDA_TEXT::SetKeepUpright( bool aKeepUpright )
388{
389 m_attributes.m_KeepUpright = aKeepUpright;
392}
393
394
395void EDA_TEXT::SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition )
396{
398
399 if( aSetPosition )
400 m_pos = aSrc.m_pos;
401
404}
405
406
407void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
408{
409 std::swap( m_text, aTradingPartner.m_text );
411 aTradingPartner.cacheShownText();
412}
413
414
415void EDA_TEXT::SwapAttributes( EDA_TEXT& aTradingPartner )
416{
417 std::swap( m_attributes, aTradingPartner.m_attributes );
418 std::swap( m_pos, aTradingPartner.m_pos );
419
421 aTradingPartner.ClearRenderCache();
422
424 aTradingPartner.ClearBoundingBoxCache();
425}
426
427
428int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultPenWidth ) const
429{
430 int penWidth = GetTextThickness();
431
432 if( penWidth <= 1 )
433 {
434 penWidth = aDefaultPenWidth;
435
436 if( IsBold() )
437 penWidth = GetPenSizeForBold( GetTextWidth() );
438 else if( penWidth <= 1 )
439 penWidth = GetPenSizeForNormal( GetTextWidth() );
440 }
441 else if( IsBold() && isStrokeFont() )
442 penWidth = KiROUND( penWidth * BOLD_STROKE_MULTIPLIER );
443
444 // Clip pen size for small texts:
445 penWidth = ClampTextPenSize( penWidth, GetTextSize() );
446
447 return penWidth;
448}
449
450
451bool EDA_TEXT::Replace( const EDA_SEARCH_DATA& aSearchData )
452{
453 bool retval = EDA_ITEM::Replace( aSearchData, m_text );
454
456
459
460 return retval;
461}
462
463
465{
466 m_attributes = aTextAttrs;
469}
470
471
473{
474 m_attributes.m_Font = aFont;
477}
478
479
480bool EDA_TEXT::ResolveFont( const std::vector<wxString>* aEmbeddedFonts )
481{
482 if( !m_unresolvedFontName.IsEmpty() )
483 {
485
486 // The resolved font may not be the same as the original font, so we want to continue to
487 // use the existing glyph cache to preserve the existing geometry (important on the board,
488 // even if not so much on the schematic). We need to update the font so that KiCad will
489 // decide the cache is still valid. (It will get regenerated if the user edits the text.)
490 if( m_render_cache && !m_render_cache->glyphs.empty() )
491 m_render_cache->font = m_attributes.m_Font;
492
493 // The bbox cache isn't keyed on the font, so a box measured against the fallback font
494 // before resolution would otherwise survive until the next setter.
496
497 m_unresolvedFontName = wxEmptyString;
498 return true;
499 }
500
501 return false;
502}
503
504
505void EDA_TEXT::SetLineSpacing( double aLineSpacing )
506{
507 m_attributes.m_LineSpacing = aLineSpacing;
510}
511
512
513void EDA_TEXT::SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize )
514{
515 // Plotting uses unityScale and independently scales the text. If we clamp here we'll
516 // clamp to *really* small values.
517 if( m_IuScale.get().IU_PER_MM == unityScale.IU_PER_MM )
518 aEnforceMinTextSize = false;
519
520 if( aEnforceMinTextSize )
521 {
522 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
523 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
524
525 aNewSize = VECTOR2I( std::clamp( aNewSize.x, min, max ), std::clamp( aNewSize.y, min, max ) );
526 }
527
528 m_attributes.m_Size = aNewSize;
529
532}
533
534
535void EDA_TEXT::SetTextWidth( int aWidth )
536{
537 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
538 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
539
540 m_attributes.m_Size.x = std::clamp( aWidth, min, max );
543}
544
545
546void EDA_TEXT::SetTextHeight( int aHeight )
547{
548 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
549 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
550
551 m_attributes.m_Size.y = std::clamp( aHeight, min, max );
554}
555
556
557void EDA_TEXT::SetTextPos( const VECTOR2I& aPoint )
558{
559 VECTOR2I current = GetTextPos();
560 Offset( VECTOR2I( aPoint.x - current.x, aPoint.y - current.y ) );
561}
562
563
564void EDA_TEXT::SetTextX( int aX )
565{
566 Offset( VECTOR2I( aX - GetTextPos().x, 0 ) );
567}
568
569
570void EDA_TEXT::SetTextY( int aY )
571{
572 Offset( VECTOR2I( 0, aY - GetTextPos().y ) );
573}
574
575
576void EDA_TEXT::Offset( const VECTOR2I& aOffset )
577{
578 if( aOffset.x == 0 && aOffset.y == 0 )
579 return;
580
581 m_pos += aOffset;
582
583 if( m_render_cache )
584 {
585 for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_render_cache->glyphs )
586 {
587 if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
588 outline->Move( aOffset );
589 else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
590 glyph = stroke->Transform( { 1.0, 1.0 }, aOffset, 0, ANGLE_0, false, { 0, 0 } );
591 }
592 }
593
595}
596
597
599{
600 m_text.Empty();
602}
603
604
606{
607 if( m_text.IsEmpty() )
608 {
609 m_shown_text = wxEmptyString;
611 }
612 else
613 {
615 m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) ) || m_shown_text.Contains( wxT( "@{" ) );
616 }
617
618 // Extract against raw m_text so backslash-escaped ${...} literals do not
619 // fabricate dependency edges. Eager population keeps the read path
620 // lock-free for concurrent workers.
621 if( m_text.IsEmpty() )
622 m_text_var_refs.clear();
623 else
625
628}
629
630
631const std::vector<TEXT_VAR_REF_KEY>& EDA_TEXT::GetTextVarReferences() const
632{
633 return m_text_var_refs;
634}
635
636
637wxString EDA_TEXT::EvaluateText( const wxString& aText ) const
638{
639 // Must not be static. EvaluateText runs on parallel workers (e.g.
640 // CONNECTION_GRAPH resolving label text) and a shared evaluator races on
641 // its internal error collector.
642 EXPRESSION_EVALUATOR evaluator;
643
644 return evaluator.Evaluate( aText );
645}
646
647
649{
650 KIFONT::FONT* font = GetFont();
651
652 if( !font )
653 {
654 if( aSettings )
655 font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
656 else
657 font = KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() );
658 }
659
660 return font;
661}
662
663
668
669
671{
672 m_render_cache.reset();
673}
674
675
677{
678 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
679 m_bbox_cache.clear();
680}
681
682
683std::vector<std::unique_ptr<KIFONT::GLYPH>>*
684EDA_TEXT::GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText, const VECTOR2I& aOffset ) const
685{
686 if( aFont->IsOutline() )
687 {
688 EDA_ANGLE resolvedAngle = GetDrawRotation();
689 bool mirrored = IsMirrored();
690
691 if( !m_render_cache )
692 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
693
694 if( m_render_cache->glyphs.empty() || m_render_cache->font != aFont
695 || m_render_cache->text != forResolvedText
696 || m_render_cache->angle != resolvedAngle || m_render_cache->offset != aOffset
697 || m_render_cache->mirrored != mirrored )
698 {
699 m_render_cache->glyphs.clear();
700
701 const KIFONT::OUTLINE_FONT* font = static_cast<const KIFONT::OUTLINE_FONT*>( aFont );
703
704 attrs.m_Angle = resolvedAngle;
705 attrs.m_Size = GetTextSize();
706
707 font->GetLinesAsGlyphs( &m_render_cache->glyphs, forResolvedText, GetDrawPos() + aOffset, attrs,
708 getFontMetrics() );
709 m_render_cache->font = aFont;
710 m_render_cache->angle = resolvedAngle;
711 m_render_cache->text = forResolvedText;
712 m_render_cache->offset = aOffset;
713 m_render_cache->mirrored = mirrored;
714 }
715
716 return &m_render_cache->glyphs;
717 }
718
719 return nullptr;
720}
721
722
723void EDA_TEXT::SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont, const EDA_ANGLE& aAngle,
724 const VECTOR2I& aOffset )
725{
726 if( !m_render_cache )
727 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
728
729 m_render_cache->text = aResolvedText;
730 m_render_cache->font = aFont;
731 m_render_cache->angle = aAngle;
732 m_render_cache->offset = aOffset;
733 m_render_cache->mirrored = IsMirrored();
734 m_render_cache->glyphs.clear();
735}
736
737
739{
740 if( !m_render_cache )
741 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
742
743 m_render_cache->glyphs.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( aPoly ) );
744 static_cast<KIFONT::OUTLINE_GLYPH*>( m_render_cache->glyphs.back().get() )->CacheTriangulation();
745}
746
747
748int EDA_TEXT::GetInterline( const RENDER_SETTINGS* aSettings ) const
749{
750 return KiROUND( GetDrawFont( aSettings )->GetInterline( GetTextHeight(), getFontMetrics() )
751 * GetLineSpacing() );
752}
753
754
755BOX2I EDA_TEXT::GetTextBox( const RENDER_SETTINGS* aSettings, int aLine ) const
756{
757 VECTOR2I drawPos = GetDrawPos();
758
759 {
760 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
761 auto cache_it = m_bbox_cache.find( aLine );
762
763 if( cache_it != m_bbox_cache.end() && cache_it->second.m_pos == drawPos )
764 return cache_it->second.m_bbox;
765 }
766
767 BOX2I bbox;
768 wxArrayString strings;
769 wxString text = GetShownText( FOR_CANVAS );
770 int thickness = GetEffectiveTextPenWidth();
771
772 if( IsMultilineAllowed() )
773 {
774 wxStringSplit( text, strings, '\n' );
775
776 if( strings.GetCount() ) // GetCount() == 0 for void strings with multilines allowed
777 {
778 if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
779 text = strings.Item( aLine );
780 else
781 text = strings.Item( 0 );
782 }
783 }
784
785 // calculate the H and V size
786 KIFONT::FONT* font = GetDrawFont( aSettings );
787 VECTOR2D fontSize( GetTextSize() );
788 bool bold = IsBold();
789 bool italic = IsItalic();
790 VECTOR2I extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
791 int overbarOffset = 0;
792
793 // Creates bounding box (rectangle) for horizontal, left and top justified text. The
794 // bounding box will be moved later according to the actual text options
795 VECTOR2I textsize = VECTOR2I( extents.x, extents.y );
796 VECTOR2I pos = drawPos;
797 int fudgeFactor = KiROUND( extents.y * 0.17 );
798
799 if( font->IsStroke() )
800 textsize.y += fudgeFactor;
801
802 int interline = KiROUND( font->GetInterline( fontSize.y, getFontMetrics() ) * GetLineSpacing() );
803
804 if( IsMultilineAllowed() && aLine > 0 && aLine < (int) strings.GetCount() )
805 pos.y -= aLine * interline;
806
807 if( text.Contains( wxT( "~{" ) ) )
808 overbarOffset = extents.y / 6;
809
810 bbox.SetOrigin( pos );
811
812 // for multiline texts and aLine < 0, merge all rectangles (aLine == -1 signals all lines)
813 if( IsMultilineAllowed() && aLine < 0 && strings.GetCount() > 1 )
814 {
815 for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
816 {
817 text = strings.Item( ii );
818 extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
819 textsize.x = std::max( textsize.x, extents.x );
820 }
821
822 // interline spacing is only *between* lines, so total height is the height of the first
823 // line plus the interline distance (with interline spacing) for all subsequent lines
824 textsize.y += ( strings.GetCount() - 1 ) * interline;
825 }
826
827 textsize.y += overbarOffset;
828
829 bbox.SetSize( textsize );
830
831 /*
832 * At this point the rectangle origin is the text origin (m_Pos). This is correct only for
833 * left and top justified, non-mirrored, non-overbarred texts. Recalculate for all others.
834 */
835 int italicOffset = IsItalic() ? KiROUND( fontSize.y * ITALIC_TILT ) : 0;
836
837 switch( GetHorizJustify() )
838 {
840 if( IsMirrored() )
841 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
842
843 break;
844
846 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) / 2 );
847 break;
848
850 if( !IsMirrored() )
851 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
852
853 break;
854
856 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
857 break;
858 }
859
860 switch( GetVertJustify() )
861 {
863 bbox.Offset( 0, -fudgeFactor );
864 break;
865
867 bbox.SetY( bbox.GetY() - bbox.GetHeight() / 2 );
868 break;
869
871 bbox.SetY( bbox.GetY() - bbox.GetHeight() );
872 bbox.Offset( 0, fudgeFactor );
873 break;
874
876 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
877 break;
878 }
879
880 bbox.Normalize(); // Make h and v sizes always >= 0
881
882 {
883 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
884 m_bbox_cache[aLine] = { drawPos, bbox };
885 }
886
887 return bbox;
888}
889
890
891bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
892{
893 const BOX2I rect = GetTextBox( nullptr ).GetInflated( aAccuracy );
894 const VECTOR2I location = GetRotated( aPoint, GetDrawPos(), -GetDrawRotation() );
895 return rect.Contains( location );
896}
897
898
899bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
900{
901 const BOX2I rect = aRect.GetInflated( aAccuracy );
902
903 if( aContains )
904 return rect.Contains( GetTextBox( nullptr ) );
905
906 return rect.Intersects( GetTextBox( nullptr ), GetDrawRotation() );
907}
908
909
910void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor )
911{
912 if( IsMultilineAllowed() )
913 {
914 std::vector<VECTOR2I> positions;
915 wxArrayString strings;
916 wxStringSplit( GetShownText( FOR_CANVAS ), strings, '\n' );
917
918 positions.reserve( strings.Count() );
919
920 GetLinePositions( aSettings, positions, (int) strings.Count() );
921
922 for( unsigned ii = 0; ii < strings.Count(); ii++ )
923 printOneLineOfText( aSettings, aOffset, aColor, strings[ii], positions[ii] );
924 }
925 else
926 {
927 printOneLineOfText( aSettings, aOffset, aColor, GetShownText( FOR_CANVAS ), GetDrawPos() );
928 }
929}
930
931
932void EDA_TEXT::GetLinePositions( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPositions,
933 int aLineCount ) const
934{
935 VECTOR2I pos = GetDrawPos(); // Position of first line of the multiline text according
936 // to the center of the multiline text block
937
938 VECTOR2I offset; // Offset to next line.
939
940 offset.y = GetInterline( aSettings );
941
942 if( aLineCount > 1 )
943 {
944 switch( GetVertJustify() )
945 {
947 break;
948
950 pos.y -= ( aLineCount - 1 ) * offset.y / 2;
951 break;
952
954 pos.y -= ( aLineCount - 1 ) * offset.y;
955 break;
956
958 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
959 break;
960 }
961 }
962
963 // Rotate the position of the first line around the center of the multiline text block
965
966 // Rotate the offset lines to increase happened in the right direction
967 RotatePoint( offset, GetDrawRotation() );
968
969 for( int ii = 0; ii < aLineCount; ii++ )
970 {
971 aPositions.push_back( (VECTOR2I) pos );
972 pos += offset;
973 }
974}
975
976
977void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor,
978 const wxString& aText, const VECTOR2I& aPos )
979{
980 wxDC* DC = aSettings->GetPrintDC();
981 int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
982
983 VECTOR2I size = GetTextSize();
984
985 if( IsMirrored() )
986 size.x = -size.x;
987
988 KIFONT::FONT* font = GetDrawFont( aSettings );
989
990 GRPrintText( DC, aOffset + aPos, aColor, aText, GetDrawRotation(), size, GetHorizJustify(), GetVertJustify(),
991 penWidth, IsItalic(), IsBold(), font, getFontMetrics() );
992}
993
994
995bool recursiveDescent( const std::unique_ptr<MARKUP::NODE>& aNode )
996{
997 if( aNode->isURL() )
998 return true;
999
1000 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
1001 {
1002 if( recursiveDescent( child ) )
1003 return true;
1004 }
1005
1006 return false;
1007}
1008
1009
1011{
1012 wxString showntext = GetShownText( FOR_GUI );
1013 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( showntext ) );
1014 return recursiveDescent( markupParser.Parse() );
1015}
1016
1017
1019{
1020 int style = 0;
1021
1022 if( IsItalic() )
1023 style = 1;
1024
1025 if( IsBold() )
1026 style += 2;
1027
1028 wxString stylemsg[4] = { _( "Normal" ), _( "Italic" ), _( "Bold" ), _( "Bold+Italic" ) };
1029
1030 return stylemsg[style];
1031}
1032
1033
1035{
1036 if( GetFont() )
1037 return GetFont()->GetName();
1038 else
1039 return wxEmptyString;
1040}
1041
1042
1044{
1045 if( KIFONT::FONT* font = GetFont() )
1046 return font->GetName();
1047
1048 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1049 return _( "Default Font" );
1050 else
1051 return KICAD_FONT_NAME;
1052}
1053
1054
1055void EDA_TEXT::SetFontProp( const wxString& aFontName )
1056{
1057 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1058 {
1059 if( aFontName == _( "Default Font" ) )
1060 SetFont( nullptr );
1061 else
1062 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1063 }
1064 else
1065 {
1066 if( aFontName == KICAD_FONT_NAME )
1067 SetFont( nullptr );
1068 else
1069 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1070 }
1071}
1072
1073
1079
1080
1081void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const
1082{
1083 aFormatter->Print( "(effects" );
1084
1085 aFormatter->Print( "(font" );
1086
1087 if( GetFont() && !GetFont()->GetName().IsEmpty() )
1088 aFormatter->Print( "(face %s)", aFormatter->Quotew( GetFont()->NameAsToken() ).c_str() );
1089
1090 // Text size
1091 aFormatter->Print( "(size %s %s)", EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextHeight() ).c_str(),
1093
1094 if( GetLineSpacing() != 1.0 )
1095 {
1096 aFormatter->Print( "(line_spacing %s)", FormatDouble2Str( GetLineSpacing() ).c_str() );
1097 }
1098
1099 if( !GetAutoThickness() )
1100 {
1101 aFormatter->Print( "(thickness %s)",
1103 }
1104
1105 if( IsBold() )
1106 KICAD_FORMAT::FormatBool( aFormatter, "bold", true );
1107
1108 if( IsItalic() )
1109 KICAD_FORMAT::FormatBool( aFormatter, "italic", true );
1110
1111 if( !( aControlBits & CTL_OMIT_COLOR ) && GetTextColor() != COLOR4D::UNSPECIFIED )
1112 {
1113 aFormatter->Print( "(color %d %d %d %s)", KiROUND( GetTextColor().r * 255.0 ),
1114 KiROUND( GetTextColor().g * 255.0 ), KiROUND( GetTextColor().b * 255.0 ),
1115 FormatDouble2Str( GetTextColor().a ).c_str() );
1116 }
1117
1118 aFormatter->Print( ")" ); // (font
1119
1121 {
1122 aFormatter->Print( "(justify" );
1123
1125 aFormatter->Print( GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
1126
1128 aFormatter->Print( GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
1129
1130 if( IsMirrored() )
1131 aFormatter->Print( " mirror" );
1132
1133 aFormatter->Print( ")" ); // (justify
1134 }
1135
1136 if( !( aControlBits & CTL_OMIT_HYPERLINK ) && HasHyperlink() )
1137 aFormatter->Print( "(href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
1138
1139 aFormatter->Print( ")" ); // (effects
1140}
1141
1142
1143std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate, const BOX2I& aBBox,
1144 const EDA_ANGLE& aAngle ) const
1145{
1146 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
1147 KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
1148 KIFONT::FONT* font = GetDrawFont( nullptr );
1149 int penWidth = GetEffectiveTextPenWidth();
1150 wxString shownText( GetShownText( FOR_CANVAS ) );
1151 VECTOR2I drawPos = GetDrawPos();
1153
1154 attrs.m_Size = GetTextSize();
1155
1156 std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = nullptr;
1157
1158 if( aBBox.GetWidth() )
1159 {
1160 drawPos = aBBox.GetCenter();
1163 attrs.m_Angle = aAngle;
1164 }
1165 else
1166 {
1167 attrs.m_Angle = GetDrawRotation();
1168
1169 if( font->IsOutline() )
1170 cache = GetRenderCache( font, shownText, VECTOR2I() );
1171 }
1172
1173 if( aTriangulate )
1174 {
1175 CALLBACK_GAL callback_gal(
1176 empty_opts,
1177 // Stroke callback
1178 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
1179 {
1180 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
1181 },
1182 // Triangulation callback
1183 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
1184 {
1185 SHAPE_SIMPLE* triShape = new SHAPE_SIMPLE;
1186
1187 for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
1188 triShape->Append( point.x, point.y );
1189
1190 shape->AddShape( triShape );
1191 } );
1192
1193 if( cache )
1194 callback_gal.DrawGlyphs( *cache );
1195 else
1196 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
1197 }
1198 else
1199 {
1200 CALLBACK_GAL callback_gal(
1201 empty_opts,
1202 // Stroke callback
1203 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
1204 {
1205 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
1206 },
1207 // Outline callback
1208 [&]( const SHAPE_LINE_CHAIN& aPoly )
1209 {
1210 shape->AddShape( aPoly.Clone() );
1211 } );
1212
1213 if( cache )
1214 callback_gal.DrawGlyphs( *cache );
1215 else
1216 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
1217 }
1218
1219 return shape;
1220}
1221
1222
1223int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
1224{
1225 wxCHECK( aOther, 1 );
1226
1227 int val = m_attributes.Compare( aOther->m_attributes );
1228
1229 if( val != 0 )
1230 return val;
1231
1232 const VECTOR2I pos = GetTextPos();
1233 const VECTOR2I otherPos = aOther->GetTextPos();
1234
1235 if( pos.x != otherPos.x )
1236 return pos.x - otherPos.x;
1237
1238 if( pos.y != otherPos.y )
1239 return pos.y - otherPos.y;
1240
1241 val = GetFontName().Cmp( aOther->GetFontName() );
1242
1243 if( val != 0 )
1244 return val;
1245
1246 val = m_hyperlink.Cmp( aOther->m_hyperlink );
1247
1248 if( val != 0 )
1249 return val;
1250
1251 return m_text.Cmp( aOther->m_text );
1252}
1253
1254
1255bool EDA_TEXT::ValidateHyperlink( const wxString& aURL )
1256{
1257 if( aURL.IsEmpty() || IsGotoPageHref( aURL ) )
1258 return true;
1259
1260 wxURI uri;
1261
1262 return ( uri.Create( aURL ) && uri.HasScheme() );
1263}
1264
1265double EDA_TEXT::Levenshtein( const EDA_TEXT& aOther ) const
1266{
1267 // Compute the Levenshtein distance between the two strings
1268 const wxString& str1 = GetText();
1269 const wxString& str2 = aOther.GetText();
1270
1271 int m = str1.length();
1272 int n = str2.length();
1273
1274 if( n == 0 || m == 0 )
1275 return 0.0;
1276
1277 // Create a matrix to store the distance values
1278 std::vector<std::vector<int>> distance( m + 1, std::vector<int>( n + 1 ) );
1279
1280 // Initialize the matrix
1281 for( int i = 0; i <= m; i++ )
1282 distance[i][0] = i;
1283 for( int j = 0; j <= n; j++ )
1284 distance[0][j] = j;
1285
1286 // Calculate the distance
1287 for( int i = 1; i <= m; i++ )
1288 {
1289 for( int j = 1; j <= n; j++ )
1290 {
1291 if( str1[i - 1] == str2[j - 1] )
1292 {
1293 distance[i][j] = distance[i - 1][j - 1];
1294 }
1295 else
1296 {
1297 distance[i][j] = std::min( { distance[i - 1][j], distance[i][j - 1], distance[i - 1][j - 1] } ) + 1;
1298 }
1299 }
1300 }
1301
1302 // Calculate similarity score
1303 int maxLen = std::max( m, n );
1304 double similarity = 1.0 - ( static_cast<double>( distance[m][n] ) / maxLen );
1305
1306 return similarity;
1307}
1308
1309
1310double EDA_TEXT::Similarity( const EDA_TEXT& aOther ) const
1311{
1312 double retval = 1.0;
1313
1314 if( !( m_attributes == aOther.m_attributes ) )
1315 retval *= 0.9;
1316
1317 if( !m_hyperlink.IsSameAs( aOther.m_hyperlink, false ) )
1318 retval *= 0.9;
1319
1320 if( m_pos != aOther.m_pos )
1321 retval *= 0.9;
1322
1323 retval *= Levenshtein( aOther );
1324
1325 return retval;
1326}
1327
1328
1329bool EDA_TEXT::IsGotoPageHref( const wxString& aHref, wxString* aDestination )
1330{
1331 return aHref.StartsWith( wxT( "#" ), aDestination );
1332}
1333
1334
1335wxString EDA_TEXT::GotoPageHref( const wxString& aDestination )
1336{
1337 return wxT( "#" ) + aDestination;
1338}
1339
1340
1341std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aText )
1342{
1343 aStream << aText.GetText();
1344
1345 return aStream;
1346}
1347
1348
1349static struct EDA_TEXT_DESC
1350{
1352 {
1353 // These are defined in SCH_FIELD as well but initialization order is
1354 // not defined, so this needs to be conditional. Defining in both
1355 // places leads to duplicate symbols.
1357
1358 if( h_inst.Choices().GetCount() == 0 )
1359 {
1360 h_inst.Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) );
1361 h_inst.Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) );
1362 h_inst.Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
1363 }
1364
1366
1367 if( v_inst.Choices().GetCount() == 0 )
1368 {
1369 v_inst.Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) );
1370 v_inst.Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) );
1371 v_inst.Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
1372 }
1373
1376
1377 propMgr.AddProperty( new PROPERTY<EDA_TEXT, double>( _HKI( "Orientation" ),
1379
1380 const wxString textProps = _HKI( "Text Properties" );
1381
1382 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Text" ),
1384 textProps );
1385
1386 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Font" ),
1388 textProps )
1391 []( INSPECTABLE* aItem )
1392 {
1393 EDA_ITEM* eda_item = static_cast<EDA_ITEM*>( aItem );
1394 wxPGChoices fonts;
1395 std::vector<std::string> fontNames;
1396
1397 Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ),
1398 eda_item->GetEmbeddedFonts() );
1399
1400 if( IsEeschemaType( eda_item->Type() ) )
1401 fonts.Add( _( "Default Font" ) );
1402
1403 fonts.Add( KICAD_FONT_NAME );
1404
1405 for( const std::string& fontName : fontNames )
1406 fonts.Add( wxString( fontName ) );
1407
1408 return fonts;
1409 } )
1410 .SetIsCopyable();
1411
1412 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Auto Thickness" ),
1414 textProps );
1415 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Thickness" ),
1417 textProps )
1418 .SetIsCopyable();
1419 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Italic" ),
1421 textProps )
1422 .SetIsCopyable();
1423 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Bold" ),
1425 textProps )
1426 .SetIsCopyable();
1427 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Mirrored" ),
1429 textProps );
1430
1431 auto isField =
1432 []( INSPECTABLE* aItem ) -> bool
1433 {
1434 if( EDA_ITEM* item = dynamic_cast<EDA_ITEM*>( aItem ) )
1435 return item->Type() == SCH_FIELD_T || item->Type() == PCB_FIELD_T;
1436
1437 return false;
1438 };
1439
1440 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Visible" ),
1442 textProps )
1443 .SetAvailableFunc( isField )
1444 .SetIsCopyable();
1445
1446 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Width" ),
1448 textProps )
1449 .SetIsCopyable();
1450
1451 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Height" ),
1453 textProps )
1454 .SetIsCopyable();
1455
1456 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_H_ALIGN_T>( _HKI( "Horizontal Justification" ),
1458 textProps )
1459 .SetIsCopyable();
1460 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_V_ALIGN_T>( _HKI( "Vertical Justification" ),
1462 textProps )
1463 .SetIsCopyable();
1464
1465 propMgr.AddProperty( new PROPERTY<EDA_TEXT, COLOR4D>( _HKI( "Color" ),
1467 textProps );
1468
1469 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Hyperlink" ),
1471 textProps );
1472 }
1474
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
constexpr EDA_IU_SCALE unityScale
Definition base_units.h:131
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:240
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:149
constexpr coord_type GetY() const
Definition box2.h:211
constexpr size_type GetWidth() const
Definition box2.h:217
constexpr coord_type GetX() const
Definition box2.h:210
constexpr const Vec GetCenter() const
Definition box2.h:233
constexpr void SetSize(const SizeVec &size)
Definition box2.h:251
constexpr size_type GetHeight() const
Definition box2.h:218
constexpr void SetX(coord_type val)
Definition box2.h:280
constexpr BOX2< Vec > GetInflated(coord_type aDx, coord_type aDy) const
Get a new rectangle that is this one, inflated by aDx and aDy.
Definition box2.h:577
constexpr void SetY(coord_type val)
Definition box2.h:285
constexpr void Offset(coord_type dx, coord_type dy)
Definition box2.h:262
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:400
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual const std::vector< wxString > * GetEmbeddedFonts()
Definition eda_item.h:552
static bool Replace(const EDA_SEARCH_DATA &aSearchData, wxString &aText)
Perform a text replace on aText using the find and replace criteria in aSearchData on items that supp...
Definition eda_item.cpp:406
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:94
std::unique_ptr< EDA_TEXT_RENDER_CACHE_DATA > m_render_cache
Definition eda_text.h:525
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition eda_text.cpp:170
virtual VECTOR2I GetTextSize() const
Definition eda_text.h:301
void SetTextColor(const COLOR4D &aColor)
Definition eda_text.h:309
COLOR4D GetTextColor() const
Definition eda_text.h:310
wxString GetTextStyleName() const
virtual VECTOR2I GetTextPos() const
Definition eda_text.h:313
VECTOR2I m_pos
Definition eda_text.h:538
wxString m_text
Definition eda_text.h:513
std::map< int, BBOX_CACHE_ENTRY > m_bbox_cache
Definition eda_text.h:533
bool IsDefaultFormatting() const
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
void SetFontProp(const wxString &aFontName)
wxString GetFontName() const
virtual ~EDA_TEXT()
Definition eda_text.cpp:135
bool IsItalic() const
Definition eda_text.h:200
const std::vector< TEXT_VAR_REF_KEY > & GetTextVarReferences() const
Return the set of ${...} references extracted from the source text.
Definition eda_text.cpp:631
bool m_visible
Definition eda_text.h:539
virtual void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:513
bool IsMultilineAllowed() const
Definition eda_text.h:236
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:118
virtual bool IsVisible() const
Definition eda_text.h:226
virtual void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:557
virtual void SetTextX(int aX)
Definition eda_text.cpp:564
virtual int GetTextHeight() const
Definition eda_text.h:307
bool m_shown_text_has_text_var_refs
Definition eda_text.h:515
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition eda_text.cpp:201
KIFONT::FONT * GetFont() const
Definition eda_text.h:286
bool ResolveFont(const std::vector< wxString > *aEmbeddedFonts)
Definition eda_text.cpp:480
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition eda_text.cpp:395
wxString m_shown_text
Definition eda_text.h:514
void SetMirrored(bool isMirrored)
Definition eda_text.cpp:355
wxString GetFontProp() const
virtual void SetTextY(int aY)
Definition eda_text.cpp:570
std::vector< std::unique_ptr< KIFONT::GLYPH > > * GetRenderCache(const KIFONT::FONT *aFont, const wxString &forResolvedText, const VECTOR2I &aOffset={ 0, 0 }) const
Definition eda_text.cpp:684
virtual EDA_ANGLE GetDrawRotation() const
Definition eda_text.h:425
wxString m_unresolvedFontName
Definition eda_text.h:537
virtual VECTOR2I GetDrawPos() const
Definition eda_text.h:426
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition eda_text.cpp:140
wxString m_hyperlink
A hyperlink URL.
Definition eda_text.h:508
BOX2I GetTextBox(const RENDER_SETTINGS *aSettings, int aLine=-1) const
Useful in multiline texts to calculate the full text or a line area (for zones filling,...
Definition eda_text.cpp:755
virtual bool HasHyperlink() const
Definition eda_text.h:448
virtual wxString GetShownText(RESOLUTION_CONTEXT aContext, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition eda_text.h:128
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:379
wxString GetHyperlink() const
Definition eda_text.h:449
virtual void Offset(const VECTOR2I &aOffset)
Definition eda_text.cpp:576
virtual int GetTextWidth() const
Definition eda_text.h:304
virtual void Format(OUTPUTFORMATTER *aFormatter, int aControlBits) const
Output the object to aFormatter in s-expression form.
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:239
virtual void SetTextWidth(int aWidth)
Definition eda_text.cpp:535
virtual KIFONT::FONT * GetDrawFont(const RENDER_SETTINGS *aSettings) const
Definition eda_text.cpp:648
void SetBoldFlag(bool aBold)
Set only the bold flag, without changing the font.
Definition eda_text.cpp:325
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition eda_text.cpp:451
void SetupRenderCache(const wxString &aResolvedText, const KIFONT::FONT *aFont, const EDA_ANGLE &aAngle, const VECTOR2I &aOffset)
Definition eda_text.cpp:723
int Compare(const EDA_TEXT *aOther) const
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition eda_text.h:523
std::mutex m_bbox_cacheMutex
Definition eda_text.h:534
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:348
bool containsURL() const
EDA_TEXT(const EDA_IU_SCALE &aIuScale, const wxString &aText=wxEmptyString)
Definition eda_text.cpp:98
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
void MigrateLegacyBoldStrokeWidth()
Migrate a pre-v11 bold stroke text so its stored thickness holds the base (non-bold) width.
Definition eda_text.cpp:333
wxString m_activeUrl
Definition eda_text.h:510
virtual void ClearBoundingBoxCache()
Definition eda_text.cpp:676
bool GetAutoThickness() const
Definition eda_text.h:170
double GetLineSpacing() const
Definition eda_text.h:298
double Similarity(const EDA_TEXT &aOther) const
void SetLineSpacing(double aLineSpacing)
Definition eda_text.cpp:505
wxString EvaluateText(const wxString &aText) const
Definition eda_text.cpp:637
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition eda_text.cpp:738
void Empty()
Definition eda_text.cpp:598
virtual void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition eda_text.cpp:251
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition eda_text.cpp:891
virtual void SetTextHeight(int aHeight)
Definition eda_text.cpp:546
virtual void cacheShownText()
Definition eda_text.cpp:605
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition eda_text.cpp:70
virtual void ClearRenderCache()
Definition eda_text.cpp:670
const TEXT_ATTRIBUTES & GetAttributes() const
Definition eda_text.h:270
void SetBold(bool aBold)
Set the text to be bold - this will also update the font if needed.
Definition eda_text.cpp:311
static bool ValidateHyperlink(const wxString &aURL)
Check if aURL is a valid hyperlink.
void SetItalicFlag(bool aItalic)
Set only the italic flag, without changing the font.
Definition eda_text.cpp:303
void SetAutoThickness(bool aAuto)
Definition eda_text.cpp:259
bool IsMirrored() const
Definition eda_text.h:229
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition eda_text.cpp:428
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition eda_text.cpp:415
void Print(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset, const COLOR4D &aColor)
Print this text object to the device context aDC.
Definition eda_text.cpp:910
double GetTextAngleDegrees() const
Definition eda_text.h:185
void GetLinePositions(const RENDER_SETTINGS *aSettings, std::vector< VECTOR2I > &aPositions, int aLineCount) const
Populate aPositions with the position of each line of a multiline text, according to the vertical jus...
Definition eda_text.cpp:932
virtual const KIFONT::METRICS & getFontMetrics() const
Definition eda_text.cpp:664
std::shared_ptr< SHAPE_COMPOUND > GetEffectiveTextShape(bool aTriangulate=true, const BOX2I &aBBox=BOX2I(), const EDA_ANGLE &aAngle=ANGLE_0) const
Build a list of segments (SHAPE_SEGMENT) to describe a text shape.
bool IsBold() const
Definition eda_text.h:215
bool isStrokeFont() const
Return true if this text is (or, while a font resolution is still pending, is named as) a stroke font...
Definition eda_text.cpp:277
void SetTextAngleDegrees(double aOrientation)
Definition eda_text.h:181
void SetHyperlink(wxString aLink)
Definition eda_text.h:450
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition eda_text.cpp:84
void SetKeepUpright(bool aKeepUpright)
Definition eda_text.cpp:387
void CopyText(const EDA_TEXT &aSrc)
Definition eda_text.cpp:244
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:242
int GetInterline(const RENDER_SETTINGS *aSettings) const
Return the distance between two lines of text.
Definition eda_text.cpp:748
int GetTextThicknessProperty() const
Definition eda_text.h:161
virtual int GetTextThickness() const
Definition eda_text.h:159
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:237
double Levenshtein(const EDA_TEXT &aOther) const
Return the levenstein distance between two texts.
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:269
std::vector< TEXT_VAR_REF_KEY > m_text_var_refs
Definition eda_text.h:521
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition eda_text.cpp:291
void SwapText(EDA_TEXT &aTradingPartner)
Definition eda_text.cpp:407
void SetMultilineAllowed(bool aAllow)
Definition eda_text.cpp:363
void SetFont(KIFONT::FONT *aFont)
Definition eda_text.cpp:472
void printOneLineOfText(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset, const COLOR4D &aColor, const wxString &aText, const VECTOR2I &aPos)
Print each line of this EDA_TEXT.
Definition eda_text.cpp:977
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition eda_text.cpp:371
TEXT_ATTRIBUTES m_attributes
Definition eda_text.h:536
static ENUM_MAP< T > & Instance()
Definition property.h:811
High-level wrapper for evaluating mathematical and string expressions in wxString format.
wxString Evaluate(const wxString &aInput)
Main evaluation function - processes input string and evaluates all '@{}' expressions.
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:39
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:94
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:144
virtual bool IsStroke() const
Definition font.h:101
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:243
const wxString & GetName() const
Definition font.h:112
virtual bool IsOutline() const
Definition font.h:102
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:442
virtual double GetInterline(double aGlyphHeight, const METRICS &aFontMetrics) const =0
Compute the distance (interline) between 2 lines of text (for multiline texts).
static const METRICS & Default()
Definition font.cpp:49
Implement outline font drawing.
void GetLinesAsGlyphs(std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aPosition, const TEXT_ATTRIBUTES &aAttrs, const METRICS &aFontMetrics) const
void CacheTriangulation(bool aSimplify=false, const TASK_SUBMITTER &aSubmitter={}, bool aBuildIndexes=false) override
Build a polygon triangulation, needed to draw a polygon on OpenGL and in some other calculations.
Definition glyph.cpp:154
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
virtual void DrawGlyphs(const std::vector< std::unique_ptr< KIFONT::GLYPH > > &aGlyphs)
Draw polygons representing font glyphs.
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const wxString & GetDefaultFont() const
std::unique_ptr< NODE > Parse()
An interface used to output 8 bit text in a convenient way.
Definition richio.h:294
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:505
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:432
PROPERTY_BASE & SetChoicesFunc(std::function< wxPGChoices(INSPECTABLE *)> aFunc)
Definition property.h:289
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
Definition property.h:275
PROPERTY_BASE & SetIsCopyable(bool aIsCopyable=true)
Definition property.h:388
PROPERTY_BASE & SetIsHiddenFromRulesEditor(bool aHide=true)
Definition property.h:361
Provide class metadata.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Represent a set of closed polygons.
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
void Append(int aX, int aY)
Append a new point at the end of the polygon.
EDA_ORIENTATION m_Angle
GR_TEXT_H_ALIGN_T m_Halign
GR_TEXT_V_ALIGN_T m_Valign
std::vector< TEXT_VAR_REF_KEY > ExtractTextVarReferences(const wxString &aSource)
Lex-scan aSource and return every ${...} reference that appears, without resolving.
Definition common.cpp:468
@ FOR_GUI
Definition common.h:93
@ FOR_CANVAS
Definition common.h:92
#define CTL_OMIT_HYPERLINK
Omit the hyperlink attribute in .kicad_xxx files.
Definition ctl_flags.h:46
#define CTL_OMIT_COLOR
Omit the color attribute in .kicad_xxx files.
Definition ctl_flags.h:45
static void recursiveDescent(wxSizer *aSizer, std::map< int, wxString > &aLabels)
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:472
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
bool recursiveDescent(const std::unique_ptr< MARKUP::NODE > &aNode)
Definition eda_text.cpp:995
static struct EDA_TEXT_DESC _EDA_TEXT_DESC
#define TEXT_MIN_SIZE_MM
Minimum text size (1 micron).
Definition eda_text.h:61
#define TEXT_MAX_SIZE_MM
Maximum text size in mm (~10 inches)
Definition eda_text.h:62
#define DEFAULT_SIZE_TEXT
This is the "default-of-the-default" hardcoded text size; individual application define their own def...
Definition eda_text.h:84
static constexpr double ITALIC_TILT
Tilt factor for italic style (this is the scaling factor on dY relative coordinates to give a tilted ...
Definition font.h:58
FONTCONFIG * Fontconfig()
int GetPenSizeForBold(int aTextSize)
Definition gr_text.cpp:34
int GetPenSizeForNormal(int aTextSize)
Definition gr_text.cpp:58
void GRPrintText(wxDC *aDC, const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aWidth, bool aItalic, bool aBold, KIFONT::FONT *aFont, const KIFONT::METRICS &aFontMetrics)
Print a graphic text through wxDC.
Definition gr_text.cpp:114
int ClampTextPenSize(int aPenSize, int aSize, bool aStrict)
Pen width should not allow characters to become cluttered up in their own fatness.
Definition gr_text.cpp:70
constexpr double BOLD_STROKE_MULTIPLIER
Factor a bold stroke font applies to its base pen width.
Definition gr_text.h:82
Some functions to handle hotkeys in KiCad.
#define KICAD_FONT_NAME
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition eda_units.h:169
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
Converts aValue from internal units to a string appropriate for writing to file.
void FormatBool(OUTPUTFORMATTER *aOut, const wxString &aKey, bool aValue)
Writes a boolean to the formatter, in the style (aKey [yes|no])
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
void PackTextAttributes(types::TextAttributes &aOutput, const TEXT_ATTRIBUTES &aInput, const EDA_IU_SCALE &aScale)
void UnpackTextAttributes(TEXT_ATTRIBUTES &aOutput, const types::TextAttributes &aInput, const EDA_IU_SCALE &aScale)
#define _HKI(x)
Definition page_info.cpp:40
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
#define ENUM_TO_WXANY(type)
Definition property.h:918
@ PT_DEGREE
Angle expressed in degrees.
Definition property.h:66
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition property.h:63
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
wxString UnescapeString(const wxString &aSource)
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
VECTOR2I location
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_H_ALIGN_INDETERMINATE
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_INDETERMINATE
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
VECTOR2I GetRotated(const VECTOR2I &aVector, const EDA_ANGLE &aAngle)
Return a new VECTOR2I that is the result of rotating aVector by aAngle.
Definition trigo.h:73
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:227
@ SCH_FIELD_T
Definition typeinfo.h:146
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:82
constexpr bool IsEeschemaType(const KICAD_T aType)
Definition typeinfo.h:403
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
static PIECE rect(double aLeft, double aTop, double aRight, double aBottom, bool aMixed=false)