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