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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <algorithm> // for max
26#include <stddef.h> // for NULL
27#include <type_traits> // for swap
28#include <vector>
29#include <mutex>
30
31#include <eda_item.h>
32#include <base_units.h>
33#include <callback_gal.h>
34#include <api/api_utils.h>
35#include <eda_text.h> // for EDA_TEXT, TEXT_EFFECTS, GR_TEXT_VJUSTIF...
36#include <gal/color4d.h> // for COLOR4D, COLOR4D::BLACK
37#include <font/glyph.h>
38#include <gr_text.h>
39#include <string_utils.h> // for UnescapeString
41#include <common.h>
42#include <math/util.h> // for KiROUND
43#include <math/vector2d.h>
44#include <core/kicad_algo.h>
45#include <richio.h>
46#include <render_settings.h>
47#include <trigo.h> // for RotatePoint
48#include <i18n_utility.h>
52#include <font/outline_font.h>
55#include <properties/property.h>
57#include <ctl_flags.h>
58#include <markup_parser.h>
59#include <api/api_enums.h>
60#include <api/api_utils.h>
61#include <api/common/types/base_types.pb.h>
62
63#include <wx/debug.h> // for wxASSERT
64#include <wx/string.h>
65#include <wx/url.h> // for wxURL
68#include "font/fontconfig.h"
69#include "pgm_base.h"
70
71class OUTPUTFORMATTER;
72
73
75{
76 wxASSERT( aHorizJustify >= GR_TEXT_H_ALIGN_LEFT && aHorizJustify <= GR_TEXT_H_ALIGN_RIGHT );
77
78 if( aHorizJustify > GR_TEXT_H_ALIGN_RIGHT )
80
81 if( aHorizJustify < GR_TEXT_H_ALIGN_LEFT )
83
84 return static_cast<GR_TEXT_H_ALIGN_T>( aHorizJustify );
85}
86
87
89{
90 wxASSERT( aVertJustify >= GR_TEXT_V_ALIGN_TOP && aVertJustify <= GR_TEXT_V_ALIGN_BOTTOM );
91
92 if( aVertJustify > GR_TEXT_V_ALIGN_BOTTOM )
94
95 if( aVertJustify < GR_TEXT_V_ALIGN_TOP )
97
98 return static_cast<GR_TEXT_V_ALIGN_T>( aVertJustify );
99}
100
101
102EDA_TEXT::EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText ) :
103 m_text( aText ),
104 m_IuScale( aIuScale ),
105 m_visible( true )
106{
109
111}
112
113
115 m_IuScale( aText.m_IuScale )
116{
117 m_text = aText.m_text;
121
123 m_pos = aText.m_pos;
124 m_visible = aText.m_visible;
125
126 m_render_cache.reset();
127
128 {
129 std::lock_guard<std::mutex> bboxLock( aText.m_bbox_cacheMutex );
131 }
132
134}
135
136
140
141
143{
144 if( this == &aText )
145 return *this;
146
147 m_text = aText.m_text;
151
153 m_pos = aText.m_pos;
154 m_visible = aText.m_visible;
155
156 m_render_cache.reset();
157
158 {
159 std::scoped_lock<std::mutex, std::mutex> bboxLock( m_bbox_cacheMutex, aText.m_bbox_cacheMutex );
161 }
162
164
165 return *this;
166}
167
168
169void EDA_TEXT::Serialize( google::protobuf::Any& aContainer ) const
170{
171 Serialize( aContainer, pcbIUScale );
172}
173
174
175void EDA_TEXT::Serialize( google::protobuf::Any& aContainer, const EDA_IU_SCALE& aScale ) const
176{
177 using namespace kiapi::common;
178 types::Text text;
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 if( GetFont() )
187 attrs->set_font_name( GetFont()->GetName().ToUTF8() );
188
190
192
193 attrs->mutable_angle()->set_value_degrees( GetTextAngleDegrees() );
194 attrs->set_line_spacing( GetLineSpacing() );
195 PackDistance( *attrs->mutable_stroke_width(), GetTextThickness(), aScale );
196 attrs->set_italic( IsItalic() );
197 attrs->set_bold( IsBold() );
198 attrs->set_underlined( GetAttributes().m_Underlined );
199 attrs->set_visible( true );
200 attrs->set_mirrored( IsMirrored() );
201 attrs->set_multiline( IsMultilineAllowed() );
202 attrs->set_keep_upright( IsKeepUpright() );
203 PackVector2( *attrs->mutable_size(), GetTextSize(), aScale );
204
206 PackColor( *attrs->mutable_color(), GetTextColor() );
207
208 aContainer.PackFrom( text );
209}
210
211
212bool EDA_TEXT::Deserialize( const google::protobuf::Any& aContainer )
213{
214 return Deserialize( aContainer, pcbIUScale );
215}
216
217
218bool EDA_TEXT::Deserialize( const google::protobuf::Any& aContainer, const EDA_IU_SCALE& aScale )
219{
220 using namespace kiapi::common;
221 types::Text text;
222
223 if( !aContainer.UnpackTo( &text ) )
224 return false;
225
226 SetText( wxString( text.text().c_str(), wxConvUTF8 ) );
227 SetHyperlink( wxString( text.hyperlink().c_str(), wxConvUTF8 ) );
228 SetTextPos( UnpackVector2( text.position(), aScale ) );
229
230 if( text.has_attributes() )
231 {
233
234 attrs.m_Bold = text.attributes().bold();
235 attrs.m_Italic = text.attributes().italic();
236 attrs.m_Underlined = text.attributes().underlined();
237 attrs.m_Mirrored = text.attributes().mirrored();
238 attrs.m_Multiline = text.attributes().multiline();
239 attrs.m_KeepUpright = text.attributes().keep_upright();
240 attrs.m_Size = UnpackVector2( text.attributes().size(), aScale );
241
242 if( text.attributes().has_color() )
243 attrs.m_Color = UnpackColor( text.attributes().color() );
244 else
246
247 if( !text.attributes().font_name().empty() )
248 {
249 attrs.m_Font = KIFONT::FONT::GetFont( wxString( text.attributes().font_name().c_str(), wxConvUTF8 ),
250 attrs.m_Bold, attrs.m_Italic );
251 }
252
253 attrs.m_Angle = EDA_ANGLE( text.attributes().angle().value_degrees(), DEGREES_T );
254 attrs.m_LineSpacing = text.attributes().line_spacing();
255 attrs.m_StrokeWidth = UnpackDistance( text.attributes().stroke_width(), aScale );
257 text.attributes().horizontal_alignment() );
258
259 attrs.m_Valign =
260 FromProtoEnum<GR_TEXT_V_ALIGN_T, types::VerticalAlignment>( text.attributes().vertical_alignment() );
261
262 SetAttributes( attrs );
263 }
264
265 return true;
266}
267
268
269void EDA_TEXT::SetText( const wxString& aText )
270{
271 m_text = aText;
273}
274
275
276void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
277{
278 m_text = aSrc.m_text;
280}
281
282
284{
285 m_attributes.m_StrokeWidth = aWidth;
288}
289
290
292{
293 if( GetAutoThickness() != aAuto )
295}
296
297
299{
300 m_attributes.m_Angle = aAngle;
303}
304
305
306void EDA_TEXT::SetItalic( bool aItalic )
307{
308 if( m_attributes.m_Italic != aItalic )
309 {
310 const KIFONT::FONT* font = GetFont();
311
312 if( !font || font->IsStroke() )
313 {
314 // For stroke fonts, just need to set the attribute.
315 }
316 else
317 {
318 // For outline fonts, italic-ness is determined by the font itself.
319 SetFont( KIFONT::FONT::GetFont( font->GetName(), IsBold(), aItalic ) );
320 }
321 }
322
323 SetItalicFlag( aItalic );
324}
325
326void EDA_TEXT::SetItalicFlag( bool aItalic )
327{
328 m_attributes.m_Italic = aItalic;
331}
332
333
334void EDA_TEXT::SetBold( bool aBold )
335{
336 if( m_attributes.m_Bold != aBold )
337 {
338 const KIFONT::FONT* font = GetFont();
339
340 if( !font || font->IsStroke() )
341 {
342 // For stroke fonts, boldness is determined by the pen size.
343 const int size = std::min( m_attributes.m_Size.x, m_attributes.m_Size.y );
344
345 if( aBold )
346 {
347 m_attributes.m_StoredStrokeWidth = m_attributes.m_StrokeWidth;
348 m_attributes.m_StrokeWidth = GetPenSizeForBold( size );
349 }
350 else
351 {
352 // Restore the original stroke width from `m_StoredStrokeWidth` if it was
353 // previously stored, resetting the width after unbolding.
354 if( m_attributes.m_StoredStrokeWidth )
355 m_attributes.m_StrokeWidth = m_attributes.m_StoredStrokeWidth;
356 else
357 {
358 m_attributes.m_StrokeWidth = GetPenSizeForNormal( size );
359 // Sets `m_StrokeWidth` to the normal pen size and stores it in
360 // `m_StoredStrokeWidth` as the default, but only if the bold option was
361 // applied before this feature was implemented.
362 m_attributes.m_StoredStrokeWidth = m_attributes.m_StrokeWidth;
363 }
364 }
365 }
366 else
367 {
368 // For outline fonts, boldness is determined by the font itself.
369 SetFont( KIFONT::FONT::GetFont( font->GetName(), aBold, IsItalic() ) );
370 }
371 }
372
373 SetBoldFlag( aBold );
374}
375
376
377void EDA_TEXT::SetBoldFlag( bool aBold )
378{
379 m_attributes.m_Bold = aBold;
382}
383
384
385void EDA_TEXT::SetVisible( bool aVisible )
386{
387 m_visible = aVisible;
389}
390
391
392void EDA_TEXT::SetMirrored( bool isMirrored )
393{
394 m_attributes.m_Mirrored = isMirrored;
397}
398
399
401{
402 m_attributes.m_Multiline = aAllow;
405}
406
407
409{
410 m_attributes.m_Halign = aType;
413}
414
415
417{
418 m_attributes.m_Valign = aType;
421}
422
423
424void EDA_TEXT::SetKeepUpright( bool aKeepUpright )
425{
426 m_attributes.m_KeepUpright = aKeepUpright;
429}
430
431
432void EDA_TEXT::SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition )
433{
435
436 if( aSetPosition )
437 m_pos = aSrc.m_pos;
438
441}
442
443
444void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
445{
446 std::swap( m_text, aTradingPartner.m_text );
448 aTradingPartner.cacheShownText();
449}
450
451
452void EDA_TEXT::SwapAttributes( EDA_TEXT& aTradingPartner )
453{
454 std::swap( m_attributes, aTradingPartner.m_attributes );
455 std::swap( m_pos, aTradingPartner.m_pos );
456
458 aTradingPartner.ClearRenderCache();
459
461 aTradingPartner.ClearBoundingBoxCache();
462}
463
464
465int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultPenWidth ) const
466{
467 int penWidth = GetTextThickness();
468
469 if( penWidth <= 1 )
470 {
471 penWidth = aDefaultPenWidth;
472
473 if( IsBold() )
474 penWidth = GetPenSizeForBold( GetTextWidth() );
475 else if( penWidth <= 1 )
476 penWidth = GetPenSizeForNormal( GetTextWidth() );
477 }
478
479 // Clip pen size for small texts:
480 penWidth = ClampTextPenSize( penWidth, GetTextSize() );
481
482 return penWidth;
483}
484
485
486bool EDA_TEXT::Replace( const EDA_SEARCH_DATA& aSearchData )
487{
488 bool retval = EDA_ITEM::Replace( aSearchData, m_text );
489
491
494
495 return retval;
496}
497
498
500{
501 m_attributes.m_Font = aFont;
504}
505
506
507bool EDA_TEXT::ResolveFont( const std::vector<wxString>* aEmbeddedFonts )
508{
509 if( !m_unresolvedFontName.IsEmpty() )
510 {
512
513 if( m_render_cache && !m_render_cache->glyphs.empty() )
514 m_render_cache->font = m_attributes.m_Font;
515
516 // The bbox cache isn't keyed on the font, so a box measured against the fallback font
517 // before resolution would otherwise survive until the next setter.
519
520 m_unresolvedFontName = wxEmptyString;
521 return true;
522 }
523
524 return false;
525}
526
527
528void EDA_TEXT::SetLineSpacing( double aLineSpacing )
529{
530 m_attributes.m_LineSpacing = aLineSpacing;
533}
534
535
536void EDA_TEXT::SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize )
537{
538 // Plotting uses unityScale and independently scales the text. If we clamp here we'll
539 // clamp to *really* small values.
540 if( m_IuScale.get().IU_PER_MM == unityScale.IU_PER_MM )
541 aEnforceMinTextSize = false;
542
543 if( aEnforceMinTextSize )
544 {
545 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
546 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
547
548 aNewSize = VECTOR2I( std::clamp( aNewSize.x, min, max ), std::clamp( aNewSize.y, min, max ) );
549 }
550
551 m_attributes.m_Size = aNewSize;
552
555}
556
557
558void EDA_TEXT::SetTextWidth( int aWidth )
559{
560 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
561 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
562
563 m_attributes.m_Size.x = std::clamp( aWidth, min, max );
566}
567
568
569void EDA_TEXT::SetTextHeight( int aHeight )
570{
571 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
572 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
573
574 m_attributes.m_Size.y = std::clamp( aHeight, min, max );
577}
578
579
580void EDA_TEXT::SetTextPos( const VECTOR2I& aPoint )
581{
582 Offset( VECTOR2I( aPoint.x - m_pos.x, aPoint.y - m_pos.y ) );
583}
584
585
586void EDA_TEXT::SetTextX( int aX )
587{
588 Offset( VECTOR2I( aX - m_pos.x, 0 ) );
589}
590
591
592void EDA_TEXT::SetTextY( int aY )
593{
594 Offset( VECTOR2I( 0, aY - m_pos.y ) );
595}
596
597
598void EDA_TEXT::Offset( const VECTOR2I& aOffset )
599{
600 if( aOffset.x == 0 && aOffset.y == 0 )
601 return;
602
603 m_pos += aOffset;
604
605 if( m_render_cache )
606 {
607 for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_render_cache->glyphs )
608 {
609 if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
610 outline->Move( aOffset );
611 else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
612 glyph = stroke->Transform( { 1.0, 1.0 }, aOffset, 0, ANGLE_0, false, { 0, 0 } );
613 }
614 }
615
617}
618
619
621{
622 m_text.Empty();
624}
625
626
628{
629 if( m_text.IsEmpty() )
630 {
631 m_shown_text = wxEmptyString;
633 }
634 else
635 {
637 m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) ) || m_shown_text.Contains( wxT( "@{" ) );
638 }
639
640 // Extract against raw m_text so backslash-escaped ${...} literals do not
641 // fabricate dependency edges. Eager population keeps the read path
642 // lock-free for concurrent workers.
643 if( m_text.IsEmpty() )
644 m_text_var_refs.clear();
645 else
647
650}
651
652
653const std::vector<TEXT_VAR_REF_KEY>& EDA_TEXT::GetTextVarReferences() const
654{
655 return m_text_var_refs;
656}
657
658
659wxString EDA_TEXT::EvaluateText( const wxString& aText ) const
660{
661 // Must not be static. EvaluateText runs on parallel workers (e.g.
662 // CONNECTION_GRAPH resolving label text) and a shared evaluator races on
663 // its internal error collector.
664 EXPRESSION_EVALUATOR evaluator;
665
666 return evaluator.Evaluate( aText );
667}
668
669
671{
672 KIFONT::FONT* font = GetFont();
673
674 if( !font )
675 {
676 if( aSettings )
677 font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
678 else
679 font = KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() );
680 }
681
682 return font;
683}
684
685
690
691
693{
694 m_render_cache.reset();
695}
696
697
699{
700 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
701 m_bbox_cache.clear();
702}
703
704
705std::vector<std::unique_ptr<KIFONT::GLYPH>>*
706EDA_TEXT::GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText, const VECTOR2I& aOffset ) const
707{
708 if( aFont->IsOutline() )
709 {
710 EDA_ANGLE resolvedAngle = GetDrawRotation();
711 bool mirrored = IsMirrored();
712
713 if( !m_render_cache )
714 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
715
716 if( m_render_cache->glyphs.empty() || m_render_cache->font != aFont
717 || m_render_cache->text != forResolvedText
718 || m_render_cache->angle != resolvedAngle || m_render_cache->offset != aOffset
719 || m_render_cache->mirrored != mirrored )
720 {
721 m_render_cache->glyphs.clear();
722
723 const KIFONT::OUTLINE_FONT* font = static_cast<const KIFONT::OUTLINE_FONT*>( aFont );
725
726 attrs.m_Angle = resolvedAngle;
727
728 font->GetLinesAsGlyphs( &m_render_cache->glyphs, forResolvedText, GetDrawPos() + aOffset, attrs,
729 getFontMetrics() );
730 m_render_cache->font = aFont;
731 m_render_cache->angle = resolvedAngle;
732 m_render_cache->text = forResolvedText;
733 m_render_cache->offset = aOffset;
734 m_render_cache->mirrored = mirrored;
735 }
736
737 return &m_render_cache->glyphs;
738 }
739
740 return nullptr;
741}
742
743
744void EDA_TEXT::SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont, const EDA_ANGLE& aAngle,
745 const VECTOR2I& aOffset )
746{
747 if( !m_render_cache )
748 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
749
750 m_render_cache->text = aResolvedText;
751 m_render_cache->font = aFont;
752 m_render_cache->angle = aAngle;
753 m_render_cache->offset = aOffset;
754 m_render_cache->mirrored = IsMirrored();
755 m_render_cache->glyphs.clear();
756}
757
758
760{
761 if( !m_render_cache )
762 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
763
764 m_render_cache->glyphs.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( aPoly ) );
765 static_cast<KIFONT::OUTLINE_GLYPH*>( m_render_cache->glyphs.back().get() )->CacheTriangulation();
766}
767
768
769int EDA_TEXT::GetInterline( const RENDER_SETTINGS* aSettings ) const
770{
771 return KiROUND( GetDrawFont( aSettings )->GetInterline( GetTextHeight(), getFontMetrics() ) );
772}
773
774
775BOX2I EDA_TEXT::GetTextBox( const RENDER_SETTINGS* aSettings, int aLine ) const
776{
777 VECTOR2I drawPos = GetDrawPos();
778
779 {
780 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
781 auto cache_it = m_bbox_cache.find( aLine );
782
783 if( cache_it != m_bbox_cache.end() && cache_it->second.m_pos == drawPos )
784 return cache_it->second.m_bbox;
785 }
786
787 BOX2I bbox;
788 wxArrayString strings;
789 wxString text = GetShownText( true );
790 int thickness = GetEffectiveTextPenWidth();
791
792 if( IsMultilineAllowed() )
793 {
794 wxStringSplit( text, strings, '\n' );
795
796 if( strings.GetCount() ) // GetCount() == 0 for void strings with multilines allowed
797 {
798 if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
799 text = strings.Item( aLine );
800 else
801 text = strings.Item( 0 );
802 }
803 }
804
805 // calculate the H and V size
806 KIFONT::FONT* font = GetDrawFont( aSettings );
807 VECTOR2D fontSize( GetTextSize() );
808 bool bold = IsBold();
809 bool italic = IsItalic();
810 VECTOR2I extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
811 int overbarOffset = 0;
812
813 // Creates bounding box (rectangle) for horizontal, left and top justified text. The
814 // bounding box will be moved later according to the actual text options
815 VECTOR2I textsize = VECTOR2I( extents.x, extents.y );
816 VECTOR2I pos = drawPos;
817 int fudgeFactor = KiROUND( extents.y * 0.17 );
818
819 if( font->IsStroke() )
820 textsize.y += fudgeFactor;
821
822 if( IsMultilineAllowed() && aLine > 0 && aLine < (int) strings.GetCount() )
823 pos.y -= KiROUND( aLine * font->GetInterline( fontSize.y, getFontMetrics() ) );
824
825 if( text.Contains( wxT( "~{" ) ) )
826 overbarOffset = extents.y / 6;
827
828 bbox.SetOrigin( pos );
829
830 // for multiline texts and aLine < 0, merge all rectangles (aLine == -1 signals all lines)
831 if( IsMultilineAllowed() && aLine < 0 && strings.GetCount() > 1 )
832 {
833 for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
834 {
835 text = strings.Item( ii );
836 extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
837 textsize.x = std::max( textsize.x, extents.x );
838 }
839
840 // interline spacing is only *between* lines, so total height is the height of the first
841 // line plus the interline distance (with interline spacing) for all subsequent lines
842 textsize.y += KiROUND( ( strings.GetCount() - 1 ) * font->GetInterline( fontSize.y, getFontMetrics() ) );
843 }
844
845 textsize.y += overbarOffset;
846
847 bbox.SetSize( textsize );
848
849 /*
850 * At this point the rectangle origin is the text origin (m_Pos). This is correct only for
851 * left and top justified, non-mirrored, non-overbarred texts. Recalculate for all others.
852 */
853 int italicOffset = IsItalic() ? KiROUND( fontSize.y * ITALIC_TILT ) : 0;
854
855 switch( GetHorizJustify() )
856 {
858 if( IsMirrored() )
859 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
860
861 break;
862
863 case GR_TEXT_H_ALIGN_CENTER: bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) / 2 ); break;
864
866 if( !IsMirrored() )
867 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
868 break;
869
870 case GR_TEXT_H_ALIGN_INDETERMINATE: wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) ); break;
871 }
872
873 switch( GetVertJustify() )
874 {
876 bbox.Offset( 0, -fudgeFactor );
877 break;
878
880 bbox.SetY( bbox.GetY() - bbox.GetHeight() / 2 );
881 break;
882
884 bbox.SetY( bbox.GetY() - bbox.GetHeight() );
885 bbox.Offset( 0, fudgeFactor );
886 break;
887
889 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
890 break;
891 }
892
893 bbox.Normalize(); // Make h and v sizes always >= 0
894
895 {
896 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
897 m_bbox_cache[aLine] = { drawPos, bbox };
898 }
899
900 return bbox;
901}
902
903
904bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
905{
906 const BOX2I rect = GetTextBox( nullptr ).GetInflated( aAccuracy );
907 const VECTOR2I location = GetRotated( aPoint, GetDrawPos(), -GetDrawRotation() );
908 return rect.Contains( location );
909}
910
911
912bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
913{
914 const BOX2I rect = aRect.GetInflated( aAccuracy );
915
916 if( aContains )
917 return rect.Contains( GetTextBox( nullptr ) );
918
919 return rect.Intersects( GetTextBox( nullptr ), GetDrawRotation() );
920}
921
922
923void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor )
924{
925 if( IsMultilineAllowed() )
926 {
927 std::vector<VECTOR2I> positions;
928 wxArrayString strings;
929 wxStringSplit( GetShownText( true ), strings, '\n' );
930
931 positions.reserve( strings.Count() );
932
933 GetLinePositions( aSettings, positions, (int) strings.Count() );
934
935 for( unsigned ii = 0; ii < strings.Count(); ii++ )
936 printOneLineOfText( aSettings, aOffset, aColor, strings[ii], positions[ii] );
937 }
938 else
939 {
940 printOneLineOfText( aSettings, aOffset, aColor, GetShownText( true ), GetDrawPos() );
941 }
942}
943
944
945void EDA_TEXT::GetLinePositions( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPositions,
946 int aLineCount ) const
947{
948 VECTOR2I pos = GetDrawPos(); // Position of first line of the multiline text according
949 // to the center of the multiline text block
950
951 VECTOR2I offset; // Offset to next line.
952
953 offset.y = GetInterline( aSettings );
954
955 if( aLineCount > 1 )
956 {
957 switch( GetVertJustify() )
958 {
960 break;
961
963 pos.y -= ( aLineCount - 1 ) * offset.y / 2;
964 break;
965
967 pos.y -= ( aLineCount - 1 ) * offset.y;
968 break;
969
971 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
972 break;
973 }
974 }
975
976 // Rotate the position of the first line around the center of the multiline text block
978
979 // Rotate the offset lines to increase happened in the right direction
980 RotatePoint( offset, GetDrawRotation() );
981
982 for( int ii = 0; ii < aLineCount; ii++ )
983 {
984 aPositions.push_back( (VECTOR2I) pos );
985 pos += offset;
986 }
987}
988
989
990void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor,
991 const wxString& aText, const VECTOR2I& aPos )
992{
993 wxDC* DC = aSettings->GetPrintDC();
994 int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
995
996 VECTOR2I size = GetTextSize();
997
998 if( IsMirrored() )
999 size.x = -size.x;
1000
1001 KIFONT::FONT* font = GetDrawFont( aSettings );
1002
1003 GRPrintText( DC, aOffset + aPos, aColor, aText, GetDrawRotation(), size, GetHorizJustify(), GetVertJustify(),
1004 penWidth, IsItalic(), IsBold(), font, getFontMetrics() );
1005}
1006
1007
1008bool recursiveDescent( const std::unique_ptr<MARKUP::NODE>& aNode )
1009{
1010 if( aNode->isURL() )
1011 return true;
1012
1013 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
1014 {
1015 if( recursiveDescent( child ) )
1016 return true;
1017 }
1018
1019 return false;
1020}
1021
1022
1024{
1025 wxString showntext = GetShownText( false );
1026 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( showntext ) );
1027 return recursiveDescent( markupParser.Parse() );
1028}
1029
1030
1032{
1033 int style = 0;
1034
1035 if( IsItalic() )
1036 style = 1;
1037
1038 if( IsBold() )
1039 style += 2;
1040
1041 wxString stylemsg[4] = { _( "Normal" ), _( "Italic" ), _( "Bold" ), _( "Bold+Italic" ) };
1042
1043 return stylemsg[style];
1044}
1045
1046
1048{
1049 if( GetFont() )
1050 return GetFont()->GetName();
1051 else
1052 return wxEmptyString;
1053}
1054
1055
1057{
1058 if( KIFONT::FONT* font = GetFont() )
1059 return font->GetName();
1060
1061 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1062 return _( "Default Font" );
1063 else
1064 return KICAD_FONT_NAME;
1065}
1066
1067
1068void EDA_TEXT::SetFontProp( const wxString& aFontName )
1069{
1070 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1071 {
1072 if( aFontName == _( "Default Font" ) )
1073 SetFont( nullptr );
1074 else
1075 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1076 }
1077 else
1078 {
1079 if( aFontName == KICAD_FONT_NAME )
1080 SetFont( nullptr );
1081 else
1082 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1083 }
1084}
1085
1086
1092
1093
1094void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const
1095{
1096 aFormatter->Print( "(effects" );
1097
1098 aFormatter->Print( "(font" );
1099
1100 if( GetFont() && !GetFont()->GetName().IsEmpty() )
1101 aFormatter->Print( "(face %s)", aFormatter->Quotew( GetFont()->NameAsToken() ).c_str() );
1102
1103 // Text size
1104 aFormatter->Print( "(size %s %s)", EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextHeight() ).c_str(),
1106
1107 if( GetLineSpacing() != 1.0 )
1108 {
1109 aFormatter->Print( "(line_spacing %s)", FormatDouble2Str( GetLineSpacing() ).c_str() );
1110 }
1111
1112 if( !GetAutoThickness() )
1113 {
1114 aFormatter->Print( "(thickness %s)",
1116 }
1117
1118 if( IsBold() )
1119 KICAD_FORMAT::FormatBool( aFormatter, "bold", true );
1120
1121 if( IsItalic() )
1122 KICAD_FORMAT::FormatBool( aFormatter, "italic", true );
1123
1124 if( !( aControlBits & CTL_OMIT_COLOR ) && GetTextColor() != COLOR4D::UNSPECIFIED )
1125 {
1126 aFormatter->Print( "(color %d %d %d %s)", KiROUND( GetTextColor().r * 255.0 ),
1127 KiROUND( GetTextColor().g * 255.0 ), KiROUND( GetTextColor().b * 255.0 ),
1128 FormatDouble2Str( GetTextColor().a ).c_str() );
1129 }
1130
1131 aFormatter->Print( ")" ); // (font
1132
1134 {
1135 aFormatter->Print( "(justify" );
1136
1138 aFormatter->Print( GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
1139
1141 aFormatter->Print( GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
1142
1143 if( IsMirrored() )
1144 aFormatter->Print( " mirror" );
1145
1146 aFormatter->Print( ")" ); // (justify
1147 }
1148
1149 if( !( aControlBits & CTL_OMIT_HYPERLINK ) && HasHyperlink() )
1150 aFormatter->Print( "(href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
1151
1152 aFormatter->Print( ")" ); // (effects
1153}
1154
1155
1156std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate, const BOX2I& aBBox,
1157 const EDA_ANGLE& aAngle ) const
1158{
1159 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
1160 KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
1161 KIFONT::FONT* font = GetDrawFont( nullptr );
1162 int penWidth = GetEffectiveTextPenWidth();
1163 wxString shownText( GetShownText( true ) );
1164 VECTOR2I drawPos = GetDrawPos();
1166
1167 std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = nullptr;
1168
1169 if( aBBox.GetWidth() )
1170 {
1171 drawPos = aBBox.GetCenter();
1174 attrs.m_Angle = aAngle;
1175 }
1176 else
1177 {
1178 attrs.m_Angle = GetDrawRotation();
1179
1180 if( font->IsOutline() )
1181 cache = GetRenderCache( font, shownText, VECTOR2I() );
1182 }
1183
1184 if( aTriangulate )
1185 {
1186 CALLBACK_GAL callback_gal(
1187 empty_opts,
1188 // Stroke callback
1189 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
1190 {
1191 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
1192 },
1193 // Triangulation callback
1194 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
1195 {
1196 SHAPE_SIMPLE* triShape = new SHAPE_SIMPLE;
1197
1198 for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
1199 triShape->Append( point.x, point.y );
1200
1201 shape->AddShape( triShape );
1202 } );
1203
1204 if( cache )
1205 callback_gal.DrawGlyphs( *cache );
1206 else
1207 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
1208 }
1209 else
1210 {
1211 CALLBACK_GAL callback_gal(
1212 empty_opts,
1213 // Stroke callback
1214 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
1215 {
1216 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
1217 },
1218 // Outline callback
1219 [&]( const SHAPE_LINE_CHAIN& aPoly )
1220 {
1221 shape->AddShape( aPoly.Clone() );
1222 } );
1223
1224 if( cache )
1225 callback_gal.DrawGlyphs( *cache );
1226 else
1227 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
1228 }
1229
1230 return shape;
1231}
1232
1233
1234int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
1235{
1236 wxCHECK( aOther, 1 );
1237
1238 int val = m_attributes.Compare( aOther->m_attributes );
1239
1240 if( val != 0 )
1241 return val;
1242
1243 if( m_pos.x != aOther->m_pos.x )
1244 return m_pos.x - aOther->m_pos.x;
1245
1246 if( m_pos.y != aOther->m_pos.y )
1247 return m_pos.y - aOther->m_pos.y;
1248
1249 val = GetFontName().Cmp( aOther->GetFontName() );
1250
1251 if( val != 0 )
1252 return val;
1253
1254 return m_text.Cmp( aOther->m_text );
1255}
1256
1257
1258bool EDA_TEXT::ValidateHyperlink( const wxString& aURL )
1259{
1260 if( aURL.IsEmpty() || IsGotoPageHref( aURL ) )
1261 return true;
1262
1263 wxURI uri;
1264
1265 return ( uri.Create( aURL ) && uri.HasScheme() );
1266}
1267
1268double EDA_TEXT::Levenshtein( const EDA_TEXT& aOther ) const
1269{
1270 // Compute the Levenshtein distance between the two strings
1271 const wxString& str1 = GetText();
1272 const wxString& str2 = aOther.GetText();
1273
1274 int m = str1.length();
1275 int n = str2.length();
1276
1277 if( n == 0 || m == 0 )
1278 return 0.0;
1279
1280 // Create a matrix to store the distance values
1281 std::vector<std::vector<int>> distance( m + 1, std::vector<int>( n + 1 ) );
1282
1283 // Initialize the matrix
1284 for( int i = 0; i <= m; i++ )
1285 distance[i][0] = i;
1286 for( int j = 0; j <= n; j++ )
1287 distance[0][j] = j;
1288
1289 // Calculate the distance
1290 for( int i = 1; i <= m; i++ )
1291 {
1292 for( int j = 1; j <= n; j++ )
1293 {
1294 if( str1[i - 1] == str2[j - 1] )
1295 {
1296 distance[i][j] = distance[i - 1][j - 1];
1297 }
1298 else
1299 {
1300 distance[i][j] = std::min( { distance[i - 1][j], distance[i][j - 1], distance[i - 1][j - 1] } ) + 1;
1301 }
1302 }
1303 }
1304
1305 // Calculate similarity score
1306 int maxLen = std::max( m, n );
1307 double similarity = 1.0 - ( static_cast<double>( distance[m][n] ) / maxLen );
1308
1309 return similarity;
1310}
1311
1312
1313double EDA_TEXT::Similarity( const EDA_TEXT& aOther ) const
1314{
1315 double retval = 1.0;
1316
1317 if( !( m_attributes == aOther.m_attributes ) )
1318 retval *= 0.9;
1319
1320 if( m_pos != aOther.m_pos )
1321 retval *= 0.9;
1322
1323 retval *= Levenshtein( aOther );
1324
1325 return retval;
1326}
1327
1328
1329bool EDA_TEXT::IsGotoPageHref( const wxString& aHref, wxString* aDestination )
1330{
1331 return aHref.StartsWith( wxT( "#" ), aDestination );
1332}
1333
1334
1335wxString EDA_TEXT::GotoPageHref( const wxString& aDestination )
1336{
1337 return wxT( "#" ) + aDestination;
1338}
1339
1340
1341std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aText )
1342{
1343 aStream << aText.GetText();
1344
1345 return aStream;
1346}
1347
1348
1349static struct EDA_TEXT_DESC
1350{
1352 {
1353 // These are defined in SCH_FIELD as well but initialization order is
1354 // not defined, so this needs to be conditional. Defining in both
1355 // places leads to duplicate symbols.
1357
1358 if( h_inst.Choices().GetCount() == 0 )
1359 {
1360 h_inst.Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) );
1361 h_inst.Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) );
1362 h_inst.Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
1363 }
1364
1366
1367 if( v_inst.Choices().GetCount() == 0 )
1368 {
1369 v_inst.Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) );
1370 v_inst.Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) );
1371 v_inst.Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
1372 }
1373
1376
1380
1381 const wxString textProps = _HKI( "Text Properties" );
1382
1384 textProps );
1385
1388 textProps )
1391 []( INSPECTABLE* aItem )
1392 {
1393 EDA_ITEM* eda_item = static_cast<EDA_ITEM*>( aItem );
1394 wxPGChoices fonts;
1395 std::vector<std::string> fontNames;
1396
1397 Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ),
1398 eda_item->GetEmbeddedFonts() );
1399
1400 if( IsEeschemaType( eda_item->Type() ) )
1401 fonts.Add( _( "Default Font" ) );
1402
1403 fonts.Add( KICAD_FONT_NAME );
1404
1405 for( const std::string& fontName : fontNames )
1406 fonts.Add( wxString( fontName ) );
1407
1408 return fonts;
1409 } );
1410
1411 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Auto Thickness" ), &EDA_TEXT::SetAutoThickness,
1413 textProps );
1417 textProps );
1418 propMgr.AddProperty(
1420 textProps );
1422 textProps );
1423 propMgr.AddProperty(
1425 textProps );
1426
1427 auto isField = []( 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(
1437 textProps )
1438 .SetAvailableFunc( isField );
1439
1442 textProps );
1443
1446 textProps );
1447
1448 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_H_ALIGN_T>( _HKI( "Horizontal Justification" ),
1451 textProps );
1452 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_V_ALIGN_T>( _HKI( "Vertical Justification" ),
1455 textProps );
1456
1457 propMgr.AddProperty(
1459 textProps );
1460
1463 textProps );
1464 }
1466
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:47
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
constexpr EDA_IU_SCALE unityScale
Definition base_units.h:128
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:237
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:146
constexpr coord_type GetY() const
Definition box2.h:208
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr coord_type GetX() const
Definition box2.h:207
constexpr const Vec GetCenter() const
Definition box2.h:230
constexpr void SetSize(const SizeVec &size)
Definition box2.h:248
constexpr size_type GetHeight() const
Definition box2.h:215
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:168
constexpr void SetX(coord_type val)
Definition box2.h:277
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:638
constexpr void SetY(coord_type val)
Definition box2.h:282
constexpr void Offset(coord_type dx, coord_type dy)
Definition box2.h:259
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:402
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:100
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
virtual const std::vector< wxString > * GetEmbeddedFonts()
Definition eda_item.h:488
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:265
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:93
std::unique_ptr< EDA_TEXT_RENDER_CACHE_DATA > m_render_cache
Definition eda_text.h:497
int GetTextHeight() const
Definition eda_text.h:292
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition eda_text.cpp:169
void SetTextColor(const COLOR4D &aColor)
Definition eda_text.h:294
const VECTOR2I & GetTextPos() const
Definition eda_text.h:298
COLOR4D GetTextColor() const
Definition eda_text.h:295
wxString GetTextStyleName() const
VECTOR2I m_pos
Definition eda_text.h:510
wxString m_text
Definition eda_text.h:485
std::map< int, BBOX_CACHE_ENTRY > m_bbox_cache
Definition eda_text.h:505
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:137
bool IsItalic() const
Definition eda_text.h:194
const std::vector< TEXT_VAR_REF_KEY > & GetTextVarReferences() const
Return the set of ${...} references extracted from the source text.
Definition eda_text.cpp:653
bool m_visible
Definition eda_text.h:511
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:536
bool IsMultilineAllowed() const
Definition eda_text.h:222
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:114
bool IsKeepUpright() const
Definition eda_text.h:231
virtual bool IsVisible() const
Definition eda_text.h:212
void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:580
void SetTextX(int aX)
Definition eda_text.cpp:586
bool m_shown_text_has_text_var_refs
Definition eda_text.h:487
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition eda_text.cpp:212
KIFONT::FONT * GetFont() const
Definition eda_text.h:272
bool ResolveFont(const std::vector< wxString > *aEmbeddedFonts)
Definition eda_text.cpp:507
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition eda_text.cpp:432
wxString m_shown_text
Definition eda_text.h:486
void SetMirrored(bool isMirrored)
Definition eda_text.cpp:392
wxString GetFontProp() const
void SetTextY(int aY)
Definition eda_text.cpp:592
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:706
virtual EDA_ANGLE GetDrawRotation() const
Definition eda_text.h:404
wxString m_unresolvedFontName
Definition eda_text.h:509
virtual VECTOR2I GetDrawPos() const
Definition eda_text.h:405
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition eda_text.cpp:142
int GetTextWidth() const
Definition eda_text.h:289
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:775
virtual bool HasHyperlink() const
Definition eda_text.h:427
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:416
wxString GetHyperlink() const
Definition eda_text.h:428
void Offset(const VECTOR2I &aOffset)
Definition eda_text.cpp:598
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:225
void SetTextWidth(int aWidth)
Definition eda_text.cpp:558
virtual KIFONT::FONT * GetDrawFont(const RENDER_SETTINGS *aSettings) const
Definition eda_text.cpp:670
void SetBoldFlag(bool aBold)
Set only the bold flag, without changing the font.
Definition eda_text.cpp:377
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition eda_text.cpp:486
void SetupRenderCache(const wxString &aResolvedText, const KIFONT::FONT *aFont, const EDA_ANGLE &aAngle, const VECTOR2I &aOffset)
Definition eda_text.cpp:744
int Compare(const EDA_TEXT *aOther) const
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition eda_text.h:495
std::mutex m_bbox_cacheMutex
Definition eda_text.h:506
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:385
bool containsURL() const
EDA_TEXT(const EDA_IU_SCALE &aIuScale, const wxString &aText=wxEmptyString)
Definition eda_text.cpp:102
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
virtual void ClearBoundingBoxCache()
Definition eda_text.cpp:698
bool GetAutoThickness() const
Definition eda_text.h:164
double GetLineSpacing() const
Definition eda_text.h:283
double Similarity(const EDA_TEXT &aOther) const
void SetLineSpacing(double aLineSpacing)
Definition eda_text.cpp:528
wxString EvaluateText(const wxString &aText) const
Definition eda_text.cpp:659
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition eda_text.cpp:759
void Empty()
Definition eda_text.cpp:620
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition eda_text.cpp:283
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition eda_text.cpp:904
void SetTextHeight(int aHeight)
Definition eda_text.cpp:569
virtual void cacheShownText()
Definition eda_text.cpp:627
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition eda_text.cpp:74
virtual void ClearRenderCache()
Definition eda_text.cpp:692
const TEXT_ATTRIBUTES & GetAttributes() const
Definition eda_text.h:256
void SetBold(bool aBold)
Set the text to be bold - this will also update the font if needed.
Definition eda_text.cpp:334
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:326
void SetAutoThickness(bool aAuto)
Definition eda_text.cpp:291
bool IsMirrored() const
Definition eda_text.h:215
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition eda_text.cpp:465
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition eda_text.cpp:452
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:923
double GetTextAngleDegrees() const
Definition eda_text.h:179
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:945
virtual const KIFONT::METRICS & getFontMetrics() const
Definition eda_text.cpp:686
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:209
void SetTextAngleDegrees(double aOrientation)
Definition eda_text.h:175
void SetHyperlink(wxString aLink)
Definition eda_text.h:429
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition eda_text.cpp:88
void SetKeepUpright(bool aKeepUpright)
Definition eda_text.cpp:424
void CopyText(const EDA_TEXT &aSrc)
Definition eda_text.cpp:276
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:228
int GetInterline(const RENDER_SETTINGS *aSettings) const
Return the distance between two lines of text.
Definition eda_text.cpp:769
int GetTextThicknessProperty() const
Definition eda_text.h:155
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition eda_text.h:125
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:269
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:298
int GetTextThickness() const
Definition eda_text.h:153
std::vector< TEXT_VAR_REF_KEY > m_text_var_refs
Definition eda_text.h:493
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition eda_text.cpp:306
void SwapText(EDA_TEXT &aTradingPartner)
Definition eda_text.cpp:444
void SetMultilineAllowed(bool aAllow)
Definition eda_text.cpp:400
void SetFont(KIFONT::FONT *aFont)
Definition eda_text.cpp:499
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:990
VECTOR2I GetTextSize() const
Definition eda_text.h:286
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition eda_text.cpp:408
TEXT_ATTRIBUTES m_attributes
Definition eda_text.h:508
static ENUM_MAP< T > & Instance()
Definition property.h:721
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:38
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:98
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:147
virtual bool IsStroke() const
Definition font.h:105
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:250
const wxString & GetName() const
Definition font.h:116
virtual bool IsOutline() const
Definition font.h:106
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:451
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:52
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:105
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:295
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:511
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:426
PROPERTY_BASE & SetChoicesFunc(std::function< wxPGChoices(INSPECTABLE *)> aFunc)
Definition property.h:276
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
Definition property.h:262
PROPERTY_BASE & SetIsHiddenFromRulesEditor(bool aHide=true)
Definition property.h:326
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.
KIGFX::COLOR4D m_Color
GR_TEXT_H_ALIGN_T m_Halign
GR_TEXT_V_ALIGN_T m_Valign
KIFONT::FONT * m_Font
std::vector< TEXT_VAR_REF_KEY > ExtractTextVarReferences(const wxString &aSource)
Lex-scan aSource and return every ${...} reference that appears, without resolving.
Definition common.cpp:435
The common library.
#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:411
@ DEGREES_T
Definition eda_angle.h:31
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
bool recursiveDescent(const std::unique_ptr< MARKUP::NODE > &aNode)
static struct EDA_TEXT_DESC _EDA_TEXT_DESC
#define TEXT_MIN_SIZE_MM
Minimum text size (1 micron).
Definition eda_text.h:60
#define TEXT_MAX_SIZE_MM
Maximum text size in mm (~10 inches)
Definition eda_text.h:61
#define DEFAULT_SIZE_TEXT
This is the "default-of-the-default" hardcoded text size; individual application define their own def...
Definition eda_text.h:83
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:62
FONTCONFIG * Fontconfig()
int GetPenSizeForBold(int aTextSize)
Definition gr_text.cpp:37
int GetPenSizeForNormal(int aTextSize)
Definition gr_text.cpp:61
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:117
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:73
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:175
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 void PackColor(types::Color &aOutput, const KIGFX::COLOR4D &aInput)
KICOMMON_API int UnpackDistance(const types::Distance &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API KIGFX::COLOR4D UnpackColor(const types::Color &aInput)
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackDistance(types::Distance &aOutput, int aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
#define _HKI(x)
Definition page_info.cpp:44
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:823
@ 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:77
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:229
@ SCH_FIELD_T
Definition typeinfo.h:151
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:87
constexpr bool IsEeschemaType(const KICAD_T aType)
Definition typeinfo.h:382
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
VECTOR2< double > VECTOR2D
Definition vector2d.h:686