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.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
191
192void EDA_TEXT::Serialize( google::protobuf::Any& aContainer, const EDA_IU_SCALE& aScale ) const
193{
194 kiapi::common::types::Text text;
195 Serialize( text, aScale );
196 aContainer.PackFrom( text );
197}
198
199
200bool EDA_TEXT::Deserialize( const google::protobuf::Any& aContainer )
201{
202 return Deserialize( aContainer, pcbIUScale );
203}
204
205
206bool EDA_TEXT::Deserialize( const kiapi::common::types::Text& text, const EDA_IU_SCALE& aScale )
207{
208 using namespace kiapi::common;
209
210 SetText( wxString( text.text().c_str(), wxConvUTF8 ) );
211 SetHyperlink( wxString( text.hyperlink().c_str(), wxConvUTF8 ) );
212 SetTextPos( UnpackVector2( text.position(), aScale ) );
213
214 if( text.has_attributes() )
215 {
217 UnpackTextAttributes( attrs, text.attributes(), aScale );
218 SetAttributes( attrs );
219 }
220
221 return true;
222}
223
224
225bool EDA_TEXT::Deserialize( const google::protobuf::Any& aContainer, const EDA_IU_SCALE& aScale )
226{
227 kiapi::common::types::Text text;
228
229 if( !aContainer.UnpackTo( &text ) )
230 return false;
231
232 return Deserialize( text, aScale );
233}
234
235
236void EDA_TEXT::SetText( const wxString& aText )
237{
238 m_text = aText;
240}
241
242
243void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
244{
245 m_text = aSrc.m_text;
247}
248
249
251{
252 m_attributes.m_StrokeWidth = aWidth;
255}
256
257
259{
260 if( GetAutoThickness() != aAuto )
261 {
262 // Freeze the base width, not the effective one; Bold still multiplies at render time.
264 }
265}
266
267
269{
270 m_attributes.m_Angle = aAngle;
273}
274
275
277{
278 if( const KIFONT::FONT* font = GetFont() )
279 return font->IsStroke();
280
281 // The font is usually unresolved during load; key off the stored face name so callers
282 // that run before font resolution (e.g. migration) still classify correctly.
283 if( !m_unresolvedFontName.IsEmpty() )
285
286 return true;
287}
288
289
290void EDA_TEXT::SetItalic( bool aItalic )
291{
292 if( m_attributes.m_Italic != aItalic )
293 {
294 // For outline fonts, italic-ness is determined by the font itself.
295 if( const KIFONT::FONT* font = GetFont(); font && !isStrokeFont() )
296 SetFont( KIFONT::FONT::GetFont( font->GetName(), IsBold(), aItalic ) );
297 }
298
299 SetItalicFlag( aItalic );
300}
301
302void EDA_TEXT::SetItalicFlag( bool aItalic )
303{
304 m_attributes.m_Italic = aItalic;
307}
308
309
310void EDA_TEXT::SetBold( bool aBold )
311{
312 if( m_attributes.m_Bold != aBold )
313 {
314 // Outline fonts carry weight in the typeface; switch variant. Stroke fonts leave the
315 // stored width alone and scale it at render time.
316 if( const KIFONT::FONT* font = GetFont(); font && !isStrokeFont() )
317 SetFont( KIFONT::FONT::GetFont( font->GetName(), aBold, IsItalic() ) );
318 }
319
320 SetBoldFlag( aBold );
321}
322
323
324void EDA_TEXT::SetBoldFlag( bool aBold )
325{
326 m_attributes.m_Bold = aBold;
329}
330
331
333{
334 int thickness = GetTextThickness();
335
336 // Outline faces never baked bold into the width; skip them.
337 if( !IsBold() || thickness <= 1 || !isStrokeFont() )
338 return;
339
340 // Keep the base above the auto threshold so a very thin width doesn't become auto. Legacy
341 // widths below ~3 IU (well under any physically meaningful stroke) can't round-trip exactly
342 // through this floor; the effective width comes out slightly larger than before migration.
343 SetTextThickness( std::max( 2, KiROUND( thickness / BOLD_STROKE_MULTIPLIER ) ) );
344}
345
346
347void EDA_TEXT::SetVisible( bool aVisible )
348{
349 m_visible = aVisible;
351}
352
353
354void EDA_TEXT::SetMirrored( bool isMirrored )
355{
356 m_attributes.m_Mirrored = isMirrored;
359}
360
361
363{
364 m_attributes.m_Multiline = aAllow;
367}
368
369
371{
372 m_attributes.m_Halign = aType;
375}
376
377
379{
380 m_attributes.m_Valign = aType;
383}
384
385
386void EDA_TEXT::SetKeepUpright( bool aKeepUpright )
387{
388 m_attributes.m_KeepUpright = aKeepUpright;
391}
392
393
394void EDA_TEXT::SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition )
395{
397
398 if( aSetPosition )
399 m_pos = aSrc.m_pos;
400
403}
404
405
406void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
407{
408 std::swap( m_text, aTradingPartner.m_text );
410 aTradingPartner.cacheShownText();
411}
412
413
414void EDA_TEXT::SwapAttributes( EDA_TEXT& aTradingPartner )
415{
416 std::swap( m_attributes, aTradingPartner.m_attributes );
417 std::swap( m_pos, aTradingPartner.m_pos );
418
420 aTradingPartner.ClearRenderCache();
421
423 aTradingPartner.ClearBoundingBoxCache();
424}
425
426
427int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultPenWidth ) const
428{
429 int penWidth = GetTextThickness();
430
431 if( penWidth <= 1 )
432 {
433 penWidth = aDefaultPenWidth;
434
435 if( IsBold() )
436 penWidth = GetPenSizeForBold( GetTextWidth() );
437 else if( penWidth <= 1 )
438 penWidth = GetPenSizeForNormal( GetTextWidth() );
439 }
440 else if( IsBold() && isStrokeFont() )
441 penWidth = KiROUND( penWidth * BOLD_STROKE_MULTIPLIER );
442
443 // Clip pen size for small texts:
444 penWidth = ClampTextPenSize( penWidth, GetTextSize() );
445
446 return penWidth;
447}
448
449
450bool EDA_TEXT::Replace( const EDA_SEARCH_DATA& aSearchData )
451{
452 bool retval = EDA_ITEM::Replace( aSearchData, m_text );
453
455
458
459 return retval;
460}
461
462
464{
465 m_attributes = aTextAttrs;
468}
469
470
472{
473 m_attributes.m_Font = aFont;
476}
477
478
479bool EDA_TEXT::ResolveFont( const std::vector<wxString>* aEmbeddedFonts )
480{
481 if( !m_unresolvedFontName.IsEmpty() )
482 {
484
485 // The resolved font may not be the same as the original font, so we want to continue to
486 // use the existing glyph cache to preserve the existing geometry (important on the board,
487 // even if not so much on the schematic). We need to update the font so that KiCad will
488 // decide the cache is still valid. (It will get regenerated if the user edits the text.)
489 if( m_render_cache && !m_render_cache->glyphs.empty() )
490 m_render_cache->font = m_attributes.m_Font;
491
492 // The bbox cache isn't keyed on the font, so a box measured against the fallback font
493 // before resolution would otherwise survive until the next setter.
495
496 m_unresolvedFontName = wxEmptyString;
497 return true;
498 }
499
500 return false;
501}
502
503
504void EDA_TEXT::SetLineSpacing( double aLineSpacing )
505{
506 m_attributes.m_LineSpacing = aLineSpacing;
509}
510
511
512void EDA_TEXT::SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize )
513{
514 // Plotting uses unityScale and independently scales the text. If we clamp here we'll
515 // clamp to *really* small values.
516 if( m_IuScale.get().IU_PER_MM == unityScale.IU_PER_MM )
517 aEnforceMinTextSize = false;
518
519 if( aEnforceMinTextSize )
520 {
521 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
522 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
523
524 aNewSize = VECTOR2I( std::clamp( aNewSize.x, min, max ), std::clamp( aNewSize.y, min, max ) );
525 }
526
527 m_attributes.m_Size = aNewSize;
528
531}
532
533
534void EDA_TEXT::SetTextWidth( int aWidth )
535{
536 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
537 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
538
539 m_attributes.m_Size.x = std::clamp( aWidth, min, max );
542}
543
544
545void EDA_TEXT::SetTextHeight( int aHeight )
546{
547 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
548 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
549
550 m_attributes.m_Size.y = std::clamp( aHeight, min, max );
553}
554
555
556void EDA_TEXT::SetTextPos( const VECTOR2I& aPoint )
557{
558 VECTOR2I current = GetTextPos();
559 Offset( VECTOR2I( aPoint.x - current.x, aPoint.y - current.y ) );
560}
561
562
563void EDA_TEXT::SetTextX( int aX )
564{
565 Offset( VECTOR2I( aX - GetTextPos().x, 0 ) );
566}
567
568
569void EDA_TEXT::SetTextY( int aY )
570{
571 Offset( VECTOR2I( 0, aY - GetTextPos().y ) );
572}
573
574
575void EDA_TEXT::Offset( const VECTOR2I& aOffset )
576{
577 if( aOffset.x == 0 && aOffset.y == 0 )
578 return;
579
580 m_pos += aOffset;
581
582 if( m_render_cache )
583 {
584 for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_render_cache->glyphs )
585 {
586 if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
587 outline->Move( aOffset );
588 else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
589 glyph = stroke->Transform( { 1.0, 1.0 }, aOffset, 0, ANGLE_0, false, { 0, 0 } );
590 }
591 }
592
594}
595
596
598{
599 m_text.Empty();
601}
602
603
605{
606 if( m_text.IsEmpty() )
607 {
608 m_shown_text = wxEmptyString;
610 }
611 else
612 {
614 m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) ) || m_shown_text.Contains( wxT( "@{" ) );
615 }
616
617 // Extract against raw m_text so backslash-escaped ${...} literals do not
618 // fabricate dependency edges. Eager population keeps the read path
619 // lock-free for concurrent workers.
620 if( m_text.IsEmpty() )
621 m_text_var_refs.clear();
622 else
624
627}
628
629
630const std::vector<TEXT_VAR_REF_KEY>& EDA_TEXT::GetTextVarReferences() const
631{
632 return m_text_var_refs;
633}
634
635
636wxString EDA_TEXT::EvaluateText( const wxString& aText ) const
637{
638 // Must not be static. EvaluateText runs on parallel workers (e.g.
639 // CONNECTION_GRAPH resolving label text) and a shared evaluator races on
640 // its internal error collector.
641 EXPRESSION_EVALUATOR evaluator;
642
643 return evaluator.Evaluate( aText );
644}
645
646
648{
649 KIFONT::FONT* font = GetFont();
650
651 if( !font )
652 {
653 if( aSettings )
654 font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
655 else
656 font = KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() );
657 }
658
659 return font;
660}
661
662
667
668
670{
671 m_render_cache.reset();
672}
673
674
676{
677 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
678 m_bbox_cache.clear();
679}
680
681
682std::vector<std::unique_ptr<KIFONT::GLYPH>>*
683EDA_TEXT::GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText, const VECTOR2I& aOffset ) const
684{
685 if( aFont->IsOutline() )
686 {
687 EDA_ANGLE resolvedAngle = GetDrawRotation();
688 bool mirrored = IsMirrored();
689
690 if( !m_render_cache )
691 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
692
693 if( m_render_cache->glyphs.empty() || m_render_cache->font != aFont
694 || m_render_cache->text != forResolvedText
695 || m_render_cache->angle != resolvedAngle || m_render_cache->offset != aOffset
696 || m_render_cache->mirrored != mirrored )
697 {
698 m_render_cache->glyphs.clear();
699
700 const KIFONT::OUTLINE_FONT* font = static_cast<const KIFONT::OUTLINE_FONT*>( aFont );
702
703 attrs.m_Angle = resolvedAngle;
704 attrs.m_Size = GetTextSize();
705
706 font->GetLinesAsGlyphs( &m_render_cache->glyphs, forResolvedText, GetDrawPos() + aOffset, attrs,
707 getFontMetrics() );
708 m_render_cache->font = aFont;
709 m_render_cache->angle = resolvedAngle;
710 m_render_cache->text = forResolvedText;
711 m_render_cache->offset = aOffset;
712 m_render_cache->mirrored = mirrored;
713 }
714
715 return &m_render_cache->glyphs;
716 }
717
718 return nullptr;
719}
720
721
722void EDA_TEXT::SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont, const EDA_ANGLE& aAngle,
723 const VECTOR2I& aOffset )
724{
725 if( !m_render_cache )
726 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
727
728 m_render_cache->text = aResolvedText;
729 m_render_cache->font = aFont;
730 m_render_cache->angle = aAngle;
731 m_render_cache->offset = aOffset;
732 m_render_cache->mirrored = IsMirrored();
733 m_render_cache->glyphs.clear();
734}
735
736
738{
739 if( !m_render_cache )
740 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
741
742 m_render_cache->glyphs.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( aPoly ) );
743 static_cast<KIFONT::OUTLINE_GLYPH*>( m_render_cache->glyphs.back().get() )->CacheTriangulation();
744}
745
746
747int EDA_TEXT::GetInterline( const RENDER_SETTINGS* aSettings ) const
748{
749 return KiROUND( GetDrawFont( aSettings )->GetInterline( GetTextHeight(), getFontMetrics() )
750 * GetLineSpacing() );
751}
752
753
754BOX2I EDA_TEXT::GetTextBox( const RENDER_SETTINGS* aSettings, int aLine ) const
755{
756 VECTOR2I drawPos = GetDrawPos();
757
758 {
759 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
760 auto cache_it = m_bbox_cache.find( aLine );
761
762 if( cache_it != m_bbox_cache.end() && cache_it->second.m_pos == drawPos )
763 return cache_it->second.m_bbox;
764 }
765
766 BOX2I bbox;
767 wxArrayString strings;
768 wxString text = GetShownText( FOR_CANVAS );
769 int thickness = GetEffectiveTextPenWidth();
770
771 if( IsMultilineAllowed() )
772 {
773 wxStringSplit( text, strings, '\n' );
774
775 if( strings.GetCount() ) // GetCount() == 0 for void strings with multilines allowed
776 {
777 if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
778 text = strings.Item( aLine );
779 else
780 text = strings.Item( 0 );
781 }
782 }
783
784 // calculate the H and V size
785 KIFONT::FONT* font = GetDrawFont( aSettings );
786 VECTOR2D fontSize( GetTextSize() );
787 bool bold = IsBold();
788 bool italic = IsItalic();
789 VECTOR2I extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
790 int overbarOffset = 0;
791
792 // Creates bounding box (rectangle) for horizontal, left and top justified text. The
793 // bounding box will be moved later according to the actual text options
794 VECTOR2I textsize = VECTOR2I( extents.x, extents.y );
795 VECTOR2I pos = drawPos;
796 int fudgeFactor = KiROUND( extents.y * 0.17 );
797
798 if( font->IsStroke() )
799 textsize.y += fudgeFactor;
800
801 int interline = KiROUND( font->GetInterline( fontSize.y, getFontMetrics() ) * GetLineSpacing() );
802
803 if( IsMultilineAllowed() && aLine > 0 && aLine < (int) strings.GetCount() )
804 pos.y -= aLine * interline;
805
806 if( text.Contains( wxT( "~{" ) ) )
807 overbarOffset = extents.y / 6;
808
809 bbox.SetOrigin( pos );
810
811 // for multiline texts and aLine < 0, merge all rectangles (aLine == -1 signals all lines)
812 if( IsMultilineAllowed() && aLine < 0 && strings.GetCount() > 1 )
813 {
814 for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
815 {
816 text = strings.Item( ii );
817 extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
818 textsize.x = std::max( textsize.x, extents.x );
819 }
820
821 // interline spacing is only *between* lines, so total height is the height of the first
822 // line plus the interline distance (with interline spacing) for all subsequent lines
823 textsize.y += ( strings.GetCount() - 1 ) * interline;
824 }
825
826 textsize.y += overbarOffset;
827
828 bbox.SetSize( textsize );
829
830 /*
831 * At this point the rectangle origin is the text origin (m_Pos). This is correct only for
832 * left and top justified, non-mirrored, non-overbarred texts. Recalculate for all others.
833 */
834 int italicOffset = IsItalic() ? KiROUND( fontSize.y * ITALIC_TILT ) : 0;
835
836 switch( GetHorizJustify() )
837 {
839 if( IsMirrored() )
840 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
841
842 break;
843
845 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) / 2 );
846 break;
847
849 if( !IsMirrored() )
850 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
851
852 break;
853
855 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
856 break;
857 }
858
859 switch( GetVertJustify() )
860 {
862 bbox.Offset( 0, -fudgeFactor );
863 break;
864
866 bbox.SetY( bbox.GetY() - bbox.GetHeight() / 2 );
867 break;
868
870 bbox.SetY( bbox.GetY() - bbox.GetHeight() );
871 bbox.Offset( 0, fudgeFactor );
872 break;
873
875 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
876 break;
877 }
878
879 bbox.Normalize(); // Make h and v sizes always >= 0
880
881 {
882 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
883 m_bbox_cache[aLine] = { drawPos, bbox };
884 }
885
886 return bbox;
887}
888
889
890bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
891{
892 const BOX2I rect = GetTextBox( nullptr ).GetInflated( aAccuracy );
893 const VECTOR2I location = GetRotated( aPoint, GetDrawPos(), -GetDrawRotation() );
894 return rect.Contains( location );
895}
896
897
898bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
899{
900 const BOX2I rect = aRect.GetInflated( aAccuracy );
901
902 if( aContains )
903 return rect.Contains( GetTextBox( nullptr ) );
904
905 return rect.Intersects( GetTextBox( nullptr ), GetDrawRotation() );
906}
907
908
909void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor )
910{
911 if( IsMultilineAllowed() )
912 {
913 std::vector<VECTOR2I> positions;
914 wxArrayString strings;
915 wxStringSplit( GetShownText( FOR_CANVAS ), strings, '\n' );
916
917 positions.reserve( strings.Count() );
918
919 GetLinePositions( aSettings, positions, (int) strings.Count() );
920
921 for( unsigned ii = 0; ii < strings.Count(); ii++ )
922 printOneLineOfText( aSettings, aOffset, aColor, strings[ii], positions[ii] );
923 }
924 else
925 {
926 printOneLineOfText( aSettings, aOffset, aColor, GetShownText( FOR_CANVAS ), GetDrawPos() );
927 }
928}
929
930
931void EDA_TEXT::GetLinePositions( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPositions,
932 int aLineCount ) const
933{
934 VECTOR2I pos = GetDrawPos(); // Position of first line of the multiline text according
935 // to the center of the multiline text block
936
937 VECTOR2I offset; // Offset to next line.
938
939 offset.y = GetInterline( aSettings );
940
941 if( aLineCount > 1 )
942 {
943 switch( GetVertJustify() )
944 {
946 break;
947
949 pos.y -= ( aLineCount - 1 ) * offset.y / 2;
950 break;
951
953 pos.y -= ( aLineCount - 1 ) * offset.y;
954 break;
955
957 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
958 break;
959 }
960 }
961
962 // Rotate the position of the first line around the center of the multiline text block
964
965 // Rotate the offset lines to increase happened in the right direction
966 RotatePoint( offset, GetDrawRotation() );
967
968 for( int ii = 0; ii < aLineCount; ii++ )
969 {
970 aPositions.push_back( (VECTOR2I) pos );
971 pos += offset;
972 }
973}
974
975
976void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor,
977 const wxString& aText, const VECTOR2I& aPos )
978{
979 wxDC* DC = aSettings->GetPrintDC();
980 int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
981
982 VECTOR2I size = GetTextSize();
983
984 if( IsMirrored() )
985 size.x = -size.x;
986
987 KIFONT::FONT* font = GetDrawFont( aSettings );
988
989 GRPrintText( DC, aOffset + aPos, aColor, aText, GetDrawRotation(), size, GetHorizJustify(), GetVertJustify(),
990 penWidth, IsItalic(), IsBold(), font, getFontMetrics() );
991}
992
993
994bool recursiveDescent( const std::unique_ptr<MARKUP::NODE>& aNode )
995{
996 if( aNode->isURL() )
997 return true;
998
999 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
1000 {
1001 if( recursiveDescent( child ) )
1002 return true;
1003 }
1004
1005 return false;
1006}
1007
1008
1010{
1011 wxString showntext = GetShownText( FOR_GUI );
1012 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( showntext ) );
1013 return recursiveDescent( markupParser.Parse() );
1014}
1015
1016
1018{
1019 int style = 0;
1020
1021 if( IsItalic() )
1022 style = 1;
1023
1024 if( IsBold() )
1025 style += 2;
1026
1027 wxString stylemsg[4] = { _( "Normal" ), _( "Italic" ), _( "Bold" ), _( "Bold+Italic" ) };
1028
1029 return stylemsg[style];
1030}
1031
1032
1034{
1035 if( GetFont() )
1036 return GetFont()->GetName();
1037 else
1038 return wxEmptyString;
1039}
1040
1041
1043{
1044 if( KIFONT::FONT* font = GetFont() )
1045 return font->GetName();
1046
1047 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1048 return _( "Default Font" );
1049 else
1050 return KICAD_FONT_NAME;
1051}
1052
1053
1054void EDA_TEXT::SetFontProp( const wxString& aFontName )
1055{
1056 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1057 {
1058 if( aFontName == _( "Default Font" ) )
1059 SetFont( nullptr );
1060 else
1061 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1062 }
1063 else
1064 {
1065 if( aFontName == KICAD_FONT_NAME )
1066 SetFont( nullptr );
1067 else
1068 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1069 }
1070}
1071
1072
1078
1079
1080void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const
1081{
1082 aFormatter->Print( "(effects" );
1083
1084 aFormatter->Print( "(font" );
1085
1086 if( GetFont() && !GetFont()->GetName().IsEmpty() )
1087 aFormatter->Print( "(face %s)", aFormatter->Quotew( GetFont()->NameAsToken() ).c_str() );
1088
1089 // Text size
1090 aFormatter->Print( "(size %s %s)", EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextHeight() ).c_str(),
1092
1093 if( GetLineSpacing() != 1.0 )
1094 {
1095 aFormatter->Print( "(line_spacing %s)", FormatDouble2Str( GetLineSpacing() ).c_str() );
1096 }
1097
1098 if( !GetAutoThickness() )
1099 {
1100 aFormatter->Print( "(thickness %s)",
1102 }
1103
1104 if( IsBold() )
1105 KICAD_FORMAT::FormatBool( aFormatter, "bold", true );
1106
1107 if( IsItalic() )
1108 KICAD_FORMAT::FormatBool( aFormatter, "italic", true );
1109
1110 if( !( aControlBits & CTL_OMIT_COLOR ) && GetTextColor() != COLOR4D::UNSPECIFIED )
1111 {
1112 aFormatter->Print( "(color %d %d %d %s)", KiROUND( GetTextColor().r * 255.0 ),
1113 KiROUND( GetTextColor().g * 255.0 ), KiROUND( GetTextColor().b * 255.0 ),
1114 FormatDouble2Str( GetTextColor().a ).c_str() );
1115 }
1116
1117 aFormatter->Print( ")" ); // (font
1118
1120 {
1121 aFormatter->Print( "(justify" );
1122
1124 aFormatter->Print( GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
1125
1127 aFormatter->Print( GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
1128
1129 if( IsMirrored() )
1130 aFormatter->Print( " mirror" );
1131
1132 aFormatter->Print( ")" ); // (justify
1133 }
1134
1135 if( !( aControlBits & CTL_OMIT_HYPERLINK ) && HasHyperlink() )
1136 aFormatter->Print( "(href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
1137
1138 aFormatter->Print( ")" ); // (effects
1139}
1140
1141
1142std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate, const BOX2I& aBBox,
1143 const EDA_ANGLE& aAngle ) const
1144{
1145 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
1146 KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
1147 KIFONT::FONT* font = GetDrawFont( nullptr );
1148 int penWidth = GetEffectiveTextPenWidth();
1149 wxString shownText( GetShownText( FOR_CANVAS ) );
1150 VECTOR2I drawPos = GetDrawPos();
1152
1153 attrs.m_Size = GetTextSize();
1154
1155 std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = nullptr;
1156
1157 if( aBBox.GetWidth() )
1158 {
1159 drawPos = aBBox.GetCenter();
1162 attrs.m_Angle = aAngle;
1163 }
1164 else
1165 {
1166 attrs.m_Angle = GetDrawRotation();
1167
1168 if( font->IsOutline() )
1169 cache = GetRenderCache( font, shownText, VECTOR2I() );
1170 }
1171
1172 if( aTriangulate )
1173 {
1174 CALLBACK_GAL callback_gal(
1175 empty_opts,
1176 // Stroke callback
1177 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
1178 {
1179 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
1180 },
1181 // Triangulation callback
1182 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
1183 {
1184 SHAPE_SIMPLE* triShape = new SHAPE_SIMPLE;
1185
1186 for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
1187 triShape->Append( point.x, point.y );
1188
1189 shape->AddShape( triShape );
1190 } );
1191
1192 if( cache )
1193 callback_gal.DrawGlyphs( *cache );
1194 else
1195 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
1196 }
1197 else
1198 {
1199 CALLBACK_GAL callback_gal(
1200 empty_opts,
1201 // Stroke callback
1202 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
1203 {
1204 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
1205 },
1206 // Outline callback
1207 [&]( const SHAPE_LINE_CHAIN& aPoly )
1208 {
1209 shape->AddShape( aPoly.Clone() );
1210 } );
1211
1212 if( cache )
1213 callback_gal.DrawGlyphs( *cache );
1214 else
1215 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
1216 }
1217
1218 return shape;
1219}
1220
1221
1222int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
1223{
1224 wxCHECK( aOther, 1 );
1225
1226 int val = m_attributes.Compare( aOther->m_attributes );
1227
1228 if( val != 0 )
1229 return val;
1230
1231 const VECTOR2I pos = GetTextPos();
1232 const VECTOR2I otherPos = aOther->GetTextPos();
1233
1234 if( pos.x != otherPos.x )
1235 return pos.x - otherPos.x;
1236
1237 if( pos.y != otherPos.y )
1238 return pos.y - otherPos.y;
1239
1240 val = GetFontName().Cmp( aOther->GetFontName() );
1241
1242 if( val != 0 )
1243 return val;
1244
1245 val = m_hyperlink.Cmp( aOther->m_hyperlink );
1246
1247 if( val != 0 )
1248 return val;
1249
1250 return m_text.Cmp( aOther->m_text );
1251}
1252
1253
1254bool EDA_TEXT::ValidateHyperlink( const wxString& aURL )
1255{
1256 if( aURL.IsEmpty() || IsGotoPageHref( aURL ) )
1257 return true;
1258
1259 wxURI uri;
1260
1261 return ( uri.Create( aURL ) && uri.HasScheme() );
1262}
1263
1264double EDA_TEXT::Levenshtein( const EDA_TEXT& aOther ) const
1265{
1266 // Compute the Levenshtein distance between the two strings
1267 const wxString& str1 = GetText();
1268 const wxString& str2 = aOther.GetText();
1269
1270 int m = str1.length();
1271 int n = str2.length();
1272
1273 if( n == 0 || m == 0 )
1274 return 0.0;
1275
1276 // Create a matrix to store the distance values
1277 std::vector<std::vector<int>> distance( m + 1, std::vector<int>( n + 1 ) );
1278
1279 // Initialize the matrix
1280 for( int i = 0; i <= m; i++ )
1281 distance[i][0] = i;
1282 for( int j = 0; j <= n; j++ )
1283 distance[0][j] = j;
1284
1285 // Calculate the distance
1286 for( int i = 1; i <= m; i++ )
1287 {
1288 for( int j = 1; j <= n; j++ )
1289 {
1290 if( str1[i - 1] == str2[j - 1] )
1291 {
1292 distance[i][j] = distance[i - 1][j - 1];
1293 }
1294 else
1295 {
1296 distance[i][j] = std::min( { distance[i - 1][j], distance[i][j - 1], distance[i - 1][j - 1] } ) + 1;
1297 }
1298 }
1299 }
1300
1301 // Calculate similarity score
1302 int maxLen = std::max( m, n );
1303 double similarity = 1.0 - ( static_cast<double>( distance[m][n] ) / maxLen );
1304
1305 return similarity;
1306}
1307
1308
1309double EDA_TEXT::Similarity( const EDA_TEXT& aOther ) const
1310{
1311 double retval = 1.0;
1312
1313 if( !( m_attributes == aOther.m_attributes ) )
1314 retval *= 0.9;
1315
1316 if( !m_hyperlink.IsSameAs( aOther.m_hyperlink, false ) )
1317 retval *= 0.9;
1318
1319 if( m_pos != aOther.m_pos )
1320 retval *= 0.9;
1321
1322 retval *= Levenshtein( aOther );
1323
1324 return retval;
1325}
1326
1327
1328bool EDA_TEXT::IsGotoPageHref( const wxString& aHref, wxString* aDestination )
1329{
1330 return aHref.StartsWith( wxT( "#" ), aDestination );
1331}
1332
1333
1334wxString EDA_TEXT::GotoPageHref( const wxString& aDestination )
1335{
1336 return wxT( "#" ) + aDestination;
1337}
1338
1339
1340std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aText )
1341{
1342 aStream << aText.GetText();
1343
1344 return aStream;
1345}
1346
1347
1348static struct EDA_TEXT_DESC
1349{
1351 {
1352 // These are defined in SCH_FIELD as well but initialization order is
1353 // not defined, so this needs to be conditional. Defining in both
1354 // places leads to duplicate symbols.
1356
1357 if( h_inst.Choices().GetCount() == 0 )
1358 {
1359 h_inst.Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) );
1360 h_inst.Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) );
1361 h_inst.Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
1362 }
1363
1365
1366 if( v_inst.Choices().GetCount() == 0 )
1367 {
1368 v_inst.Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) );
1369 v_inst.Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) );
1370 v_inst.Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
1371 }
1372
1375
1376 propMgr.AddProperty( new PROPERTY<EDA_TEXT, double>( _HKI( "Orientation" ),
1378
1379 const wxString textProps = _HKI( "Text Properties" );
1380
1381 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Text" ),
1383 textProps );
1384
1385 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Font" ),
1387 textProps )
1390 []( INSPECTABLE* aItem )
1391 {
1392 EDA_ITEM* eda_item = static_cast<EDA_ITEM*>( aItem );
1393 wxPGChoices fonts;
1394 std::vector<std::string> fontNames;
1395
1396 Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ),
1397 eda_item->GetEmbeddedFonts() );
1398
1399 if( IsEeschemaType( eda_item->Type() ) )
1400 fonts.Add( _( "Default Font" ) );
1401
1402 fonts.Add( KICAD_FONT_NAME );
1403
1404 for( const std::string& fontName : fontNames )
1405 fonts.Add( wxString( fontName ) );
1406
1407 return fonts;
1408 } ).SetIsCopyable();
1409
1410 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Auto Thickness" ),
1412 textProps );
1413 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Thickness" ),
1415 textProps ).SetIsCopyable();
1416 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Italic" ),
1418 textProps ).SetIsCopyable();
1419 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Bold" ),
1421 textProps ).SetIsCopyable();
1422 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Mirrored" ),
1424 textProps );
1425
1426 auto isField =
1427 []( INSPECTABLE* aItem ) -> bool
1428 {
1429 if( EDA_ITEM* item = dynamic_cast<EDA_ITEM*>( aItem ) )
1430 return item->Type() == SCH_FIELD_T || item->Type() == PCB_FIELD_T;
1431
1432 return false;
1433 };
1434
1435 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Visible" ),
1437 textProps )
1438 .SetAvailableFunc( isField ).SetIsCopyable();
1439
1440 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Width" ),
1442 textProps ).SetIsCopyable();
1443
1444 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Height" ),
1446 textProps ).SetIsCopyable();
1447
1448 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_H_ALIGN_T>( _HKI( "Horizontal Justification" ),
1450 textProps ).SetIsCopyable();
1451 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_V_ALIGN_T>( _HKI( "Vertical Justification" ),
1453 textProps ).SetIsCopyable();
1454
1455 propMgr.AddProperty( new PROPERTY<EDA_TEXT, COLOR4D>( _HKI( "Color" ),
1457 textProps );
1458
1459 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Hyperlink" ),
1461 textProps );
1462 }
1464
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:914
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:234
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:143
constexpr coord_type GetY() const
Definition box2.h:205
constexpr size_type GetWidth() const
Definition box2.h:211
constexpr coord_type GetX() const
Definition box2.h:204
constexpr const Vec GetCenter() const
Definition box2.h:227
constexpr void SetSize(const SizeVec &size)
Definition box2.h:245
constexpr size_type GetHeight() const
Definition box2.h:212
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:165
constexpr void SetX(coord_type val)
Definition box2.h:274
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:574
constexpr void SetY(coord_type val)
Definition box2.h:279
constexpr void Offset(coord_type dx, coord_type dy)
Definition box2.h:256
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:308
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:396
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:630
bool m_visible
Definition eda_text.h:539
virtual void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:512
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:556
virtual void SetTextX(int aX)
Definition eda_text.cpp:563
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:200
KIFONT::FONT * GetFont() const
Definition eda_text.h:286
bool ResolveFont(const std::vector< wxString > *aEmbeddedFonts)
Definition eda_text.cpp:479
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition eda_text.cpp:394
wxString m_shown_text
Definition eda_text.h:514
void SetMirrored(bool isMirrored)
Definition eda_text.cpp:354
wxString GetFontProp() const
virtual void SetTextY(int aY)
Definition eda_text.cpp:569
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:683
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:754
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:378
wxString GetHyperlink() const
Definition eda_text.h:449
virtual void Offset(const VECTOR2I &aOffset)
Definition eda_text.cpp:575
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:534
virtual KIFONT::FONT * GetDrawFont(const RENDER_SETTINGS *aSettings) const
Definition eda_text.cpp:647
void SetBoldFlag(bool aBold)
Set only the bold flag, without changing the font.
Definition eda_text.cpp:324
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition eda_text.cpp:450
void SetupRenderCache(const wxString &aResolvedText, const KIFONT::FONT *aFont, const EDA_ANGLE &aAngle, const VECTOR2I &aOffset)
Definition eda_text.cpp:722
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:347
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:332
wxString m_activeUrl
Definition eda_text.h:510
virtual void ClearBoundingBoxCache()
Definition eda_text.cpp:675
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:504
wxString EvaluateText(const wxString &aText) const
Definition eda_text.cpp:636
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition eda_text.cpp:737
void Empty()
Definition eda_text.cpp:597
virtual void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition eda_text.cpp:250
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition eda_text.cpp:890
virtual void SetTextHeight(int aHeight)
Definition eda_text.cpp:545
virtual void cacheShownText()
Definition eda_text.cpp:604
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition eda_text.cpp:70
virtual void ClearRenderCache()
Definition eda_text.cpp:669
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:310
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:302
void SetAutoThickness(bool aAuto)
Definition eda_text.cpp:258
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:427
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition eda_text.cpp:414
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:909
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:931
virtual const KIFONT::METRICS & getFontMetrics() const
Definition eda_text.cpp:663
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:276
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:386
void CopyText(const EDA_TEXT &aSrc)
Definition eda_text.cpp:243
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:747
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:236
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:268
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:290
void SwapText(EDA_TEXT &aTradingPartner)
Definition eda_text.cpp:406
void SetMultilineAllowed(bool aAllow)
Definition eda_text.cpp:362
void SetFont(KIFONT::FONT *aFont)
Definition eda_text.cpp:471
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:976
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition eda_text.cpp:370
TEXT_ATTRIBUTES m_attributes
Definition eda_text.h:536
static ENUM_MAP< T > & Instance()
Definition property.h:783
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:143
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:240
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:439
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:48
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={}) override
Build a polygon triangulation, needed to draw a polygon on OpenGL and in some other calculations.
Definition glyph.cpp:153
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:277
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
Definition property.h:263
PROPERTY_BASE & SetIsCopyable(bool aIsCopyable=true)
Definition property.h:359
PROPERTY_BASE & SetIsHiddenFromRulesEditor(bool aHide=true)
Definition property.h:332
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:91
@ FOR_CANVAS
Definition common.h:90
#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:448
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
bool recursiveDescent(const std::unique_ptr< MARKUP::NODE > &aNode)
Definition eda_text.cpp:994
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:33
int GetPenSizeForNormal(int aTextSize)
Definition gr_text.cpp:57
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:113
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:69
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 TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition idf_helpers.h:37
#define KICAD_FONT_NAME
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition eda_units.h:172
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:890
@ 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...
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:392
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707