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( google::protobuf::Any& aContainer, const EDA_IU_SCALE& aScale ) const
172{
173 using namespace kiapi::common;
174 types::Text text;
175
176 text.set_text( GetText().ToUTF8() );
177 text.set_hyperlink( GetHyperlink().ToUTF8() );
178 PackVector2( *text.mutable_position(), GetTextPos(), aScale );
179
180 types::TextAttributes* attrs = text.mutable_attributes();
181
182 if( GetFont() )
183 attrs->set_font_name( GetFont()->GetName().ToUTF8() );
184
186
188
189 attrs->mutable_angle()->set_value_degrees( GetTextAngleDegrees() );
190 attrs->set_line_spacing( GetLineSpacing() );
191 PackDistance( *attrs->mutable_stroke_width(), GetTextThickness(), aScale );
192 attrs->set_italic( IsItalic() );
193 attrs->set_bold( IsBold() );
194 attrs->set_underlined( GetAttributes().m_Underlined );
195 attrs->set_visible( true );
196 attrs->set_mirrored( IsMirrored() );
197 attrs->set_multiline( IsMultilineAllowed() );
198 attrs->set_keep_upright( IsKeepUpright() );
199 PackVector2( *attrs->mutable_size(), GetTextSize(), aScale );
200
202 PackColor( *attrs->mutable_color(), GetTextColor() );
203
204 aContainer.PackFrom( text );
205}
206
207
208bool EDA_TEXT::Deserialize( const google::protobuf::Any& aContainer )
209{
210 return Deserialize( aContainer, pcbIUScale );
211}
212
213
214bool EDA_TEXT::Deserialize( const google::protobuf::Any& aContainer, const EDA_IU_SCALE& aScale )
215{
216 using namespace kiapi::common;
217 types::Text text;
218
219 if( !aContainer.UnpackTo( &text ) )
220 return false;
221
222 SetText( wxString( text.text().c_str(), wxConvUTF8 ) );
223 SetHyperlink( wxString( text.hyperlink().c_str(), wxConvUTF8 ) );
224 SetTextPos( UnpackVector2( text.position(), aScale ) );
225
226 if( text.has_attributes() )
227 {
229
230 attrs.m_Bold = text.attributes().bold();
231 attrs.m_Italic = text.attributes().italic();
232 attrs.m_Underlined = text.attributes().underlined();
233 attrs.m_Mirrored = text.attributes().mirrored();
234 attrs.m_Multiline = text.attributes().multiline();
235 attrs.m_KeepUpright = text.attributes().keep_upright();
236 attrs.m_Size = UnpackVector2( text.attributes().size(), aScale );
237
238 if( text.attributes().has_color() )
239 attrs.m_Color = UnpackColor( text.attributes().color() );
240 else
242
243 if( !text.attributes().font_name().empty() )
244 {
245 attrs.m_Font = KIFONT::FONT::GetFont( wxString( text.attributes().font_name().c_str(), wxConvUTF8 ),
246 attrs.m_Bold, attrs.m_Italic );
247 }
248
249 attrs.m_Angle = EDA_ANGLE( text.attributes().angle().value_degrees(), DEGREES_T );
250 attrs.m_LineSpacing = text.attributes().line_spacing();
251 attrs.m_StrokeWidth = UnpackDistance( text.attributes().stroke_width(), aScale );
253 text.attributes().horizontal_alignment() );
254
255 attrs.m_Valign =
256 FromProtoEnum<GR_TEXT_V_ALIGN_T, types::VerticalAlignment>( text.attributes().vertical_alignment() );
257
258 SetAttributes( attrs );
259 }
260
261 return true;
262}
263
264
265void EDA_TEXT::SetText( const wxString& aText )
266{
267 m_text = aText;
269}
270
271
272void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
273{
274 m_text = aSrc.m_text;
276}
277
278
280{
281 m_attributes.m_StrokeWidth = aWidth;
284}
285
286
288{
289 if( GetAutoThickness() != aAuto )
291}
292
293
295{
296 m_attributes.m_Angle = aAngle;
299}
300
301
302void EDA_TEXT::SetItalic( bool aItalic )
303{
304 if( m_attributes.m_Italic != aItalic )
305 {
306 const KIFONT::FONT* font = GetFont();
307
308 if( !font || font->IsStroke() )
309 {
310 // For stroke fonts, just need to set the attribute.
311 }
312 else
313 {
314 // For outline fonts, italic-ness is determined by the font itself.
315 SetFont( KIFONT::FONT::GetFont( font->GetName(), IsBold(), aItalic ) );
316 }
317 }
318
319 SetItalicFlag( aItalic );
320}
321
322void EDA_TEXT::SetItalicFlag( bool aItalic )
323{
324 m_attributes.m_Italic = aItalic;
327}
328
329
330void EDA_TEXT::SetBold( bool aBold )
331{
332 if( m_attributes.m_Bold != aBold )
333 {
334 const KIFONT::FONT* font = GetFont();
335
336 if( !font || font->IsStroke() )
337 {
338 // For stroke fonts, boldness is determined by the pen size.
339 const int size = std::min( m_attributes.m_Size.x, m_attributes.m_Size.y );
340
341 if( aBold )
342 {
343 m_attributes.m_StoredStrokeWidth = m_attributes.m_StrokeWidth;
344 m_attributes.m_StrokeWidth = GetPenSizeForBold( size );
345 }
346 else
347 {
348 // Restore the original stroke width from `m_StoredStrokeWidth` if it was
349 // previously stored, resetting the width after unbolding.
350 if( m_attributes.m_StoredStrokeWidth )
351 m_attributes.m_StrokeWidth = m_attributes.m_StoredStrokeWidth;
352 else
353 {
354 m_attributes.m_StrokeWidth = GetPenSizeForNormal( size );
355 // Sets `m_StrokeWidth` to the normal pen size and stores it in
356 // `m_StoredStrokeWidth` as the default, but only if the bold option was
357 // applied before this feature was implemented.
358 m_attributes.m_StoredStrokeWidth = m_attributes.m_StrokeWidth;
359 }
360 }
361 }
362 else
363 {
364 // For outline fonts, boldness is determined by the font itself.
365 SetFont( KIFONT::FONT::GetFont( font->GetName(), aBold, IsItalic() ) );
366 }
367 }
368
369 SetBoldFlag( aBold );
370}
371
372
373void EDA_TEXT::SetBoldFlag( bool aBold )
374{
375 m_attributes.m_Bold = aBold;
378}
379
380
381void EDA_TEXT::SetVisible( bool aVisible )
382{
383 m_visible = aVisible;
385}
386
387
388void EDA_TEXT::SetMirrored( bool isMirrored )
389{
390 m_attributes.m_Mirrored = isMirrored;
393}
394
395
397{
398 m_attributes.m_Multiline = aAllow;
401}
402
403
405{
406 m_attributes.m_Halign = aType;
409}
410
411
413{
414 m_attributes.m_Valign = aType;
417}
418
419
420void EDA_TEXT::SetKeepUpright( bool aKeepUpright )
421{
422 m_attributes.m_KeepUpright = aKeepUpright;
425}
426
427
428void EDA_TEXT::SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition )
429{
431
432 if( aSetPosition )
433 m_pos = aSrc.m_pos;
434
437}
438
439
440void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
441{
442 std::swap( m_text, aTradingPartner.m_text );
444 aTradingPartner.cacheShownText();
445}
446
447
448void EDA_TEXT::SwapAttributes( EDA_TEXT& aTradingPartner )
449{
450 std::swap( m_attributes, aTradingPartner.m_attributes );
451 std::swap( m_pos, aTradingPartner.m_pos );
452
454 aTradingPartner.ClearRenderCache();
455
457 aTradingPartner.ClearBoundingBoxCache();
458}
459
460
461int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultPenWidth ) const
462{
463 int penWidth = GetTextThickness();
464
465 if( penWidth <= 1 )
466 {
467 penWidth = aDefaultPenWidth;
468
469 if( IsBold() )
470 penWidth = GetPenSizeForBold( GetTextWidth() );
471 else if( penWidth <= 1 )
472 penWidth = GetPenSizeForNormal( GetTextWidth() );
473 }
474
475 // Clip pen size for small texts:
476 penWidth = ClampTextPenSize( penWidth, GetTextSize() );
477
478 return penWidth;
479}
480
481
482bool EDA_TEXT::Replace( const EDA_SEARCH_DATA& aSearchData )
483{
484 bool retval = EDA_ITEM::Replace( aSearchData, m_text );
485
487
490
491 return retval;
492}
493
494
496{
497 m_attributes.m_Font = aFont;
500}
501
502
503bool EDA_TEXT::ResolveFont( const std::vector<wxString>* aEmbeddedFonts )
504{
505 if( !m_unresolvedFontName.IsEmpty() )
506 {
508
509 if( m_render_cache && !m_render_cache->glyphs.empty() )
510 m_render_cache->font = m_attributes.m_Font;
511
512 // The bbox cache isn't keyed on the font, so a box measured against the fallback font
513 // before resolution would otherwise survive until the next setter.
515
516 m_unresolvedFontName = wxEmptyString;
517 return true;
518 }
519
520 return false;
521}
522
523
524void EDA_TEXT::SetLineSpacing( double aLineSpacing )
525{
526 m_attributes.m_LineSpacing = aLineSpacing;
529}
530
531
532void EDA_TEXT::SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize )
533{
534 // Plotting uses unityScale and independently scales the text. If we clamp here we'll
535 // clamp to *really* small values.
536 if( m_IuScale.get().IU_PER_MM == unityScale.IU_PER_MM )
537 aEnforceMinTextSize = false;
538
539 if( aEnforceMinTextSize )
540 {
541 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
542 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
543
544 aNewSize = VECTOR2I( std::clamp( aNewSize.x, min, max ), std::clamp( aNewSize.y, min, max ) );
545 }
546
547 m_attributes.m_Size = aNewSize;
548
551}
552
553
554void EDA_TEXT::SetTextWidth( int aWidth )
555{
556 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
557 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
558
559 m_attributes.m_Size.x = std::clamp( aWidth, min, max );
562}
563
564
565void EDA_TEXT::SetTextHeight( int aHeight )
566{
567 int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
568 int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
569
570 m_attributes.m_Size.y = std::clamp( aHeight, min, max );
573}
574
575
576void EDA_TEXT::SetTextPos( const VECTOR2I& aPoint )
577{
578 VECTOR2I current = GetTextPos();
579 Offset( VECTOR2I( aPoint.x - current.x, aPoint.y - current.y ) );
580}
581
582
583void EDA_TEXT::SetTextX( int aX )
584{
585 Offset( VECTOR2I( aX - GetTextPos().x, 0 ) );
586}
587
588
589void EDA_TEXT::SetTextY( int aY )
590{
591 Offset( VECTOR2I( 0, aY - GetTextPos().y ) );
592}
593
594
595void EDA_TEXT::Offset( const VECTOR2I& aOffset )
596{
597 if( aOffset.x == 0 && aOffset.y == 0 )
598 return;
599
600 m_pos += aOffset;
601
602 if( m_render_cache )
603 {
604 for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_render_cache->glyphs )
605 {
606 if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
607 outline->Move( aOffset );
608 else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
609 glyph = stroke->Transform( { 1.0, 1.0 }, aOffset, 0, ANGLE_0, false, { 0, 0 } );
610 }
611 }
612
614}
615
616
618{
619 m_text.Empty();
621}
622
623
625{
626 if( m_text.IsEmpty() )
627 {
628 m_shown_text = wxEmptyString;
630 }
631 else
632 {
634 m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) ) || m_shown_text.Contains( wxT( "@{" ) );
635 }
636
637 // Extract against raw m_text so backslash-escaped ${...} literals do not
638 // fabricate dependency edges. Eager population keeps the read path
639 // lock-free for concurrent workers.
640 if( m_text.IsEmpty() )
641 m_text_var_refs.clear();
642 else
644
647}
648
649
650const std::vector<TEXT_VAR_REF_KEY>& EDA_TEXT::GetTextVarReferences() const
651{
652 return m_text_var_refs;
653}
654
655
656wxString EDA_TEXT::EvaluateText( const wxString& aText ) const
657{
658 // Must not be static. EvaluateText runs on parallel workers (e.g.
659 // CONNECTION_GRAPH resolving label text) and a shared evaluator races on
660 // its internal error collector.
661 EXPRESSION_EVALUATOR evaluator;
662
663 return evaluator.Evaluate( aText );
664}
665
666
668{
669 KIFONT::FONT* font = GetFont();
670
671 if( !font )
672 {
673 if( aSettings )
674 font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
675 else
676 font = KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() );
677 }
678
679 return font;
680}
681
682
687
688
690{
691 m_render_cache.reset();
692}
693
694
696{
697 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
698 m_bbox_cache.clear();
699}
700
701
702std::vector<std::unique_ptr<KIFONT::GLYPH>>*
703EDA_TEXT::GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText, const VECTOR2I& aOffset ) const
704{
705 if( aFont->IsOutline() )
706 {
707 EDA_ANGLE resolvedAngle = GetDrawRotation();
708 bool mirrored = IsMirrored();
709
710 if( !m_render_cache )
711 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
712
713 if( m_render_cache->glyphs.empty() || m_render_cache->font != aFont
714 || m_render_cache->text != forResolvedText
715 || m_render_cache->angle != resolvedAngle || m_render_cache->offset != aOffset
716 || m_render_cache->mirrored != mirrored )
717 {
718 m_render_cache->glyphs.clear();
719
720 const KIFONT::OUTLINE_FONT* font = static_cast<const KIFONT::OUTLINE_FONT*>( aFont );
722
723 attrs.m_Angle = resolvedAngle;
724 attrs.m_Size = GetTextSize();
725
726 font->GetLinesAsGlyphs( &m_render_cache->glyphs, forResolvedText, GetDrawPos() + aOffset, attrs,
727 getFontMetrics() );
728 m_render_cache->font = aFont;
729 m_render_cache->angle = resolvedAngle;
730 m_render_cache->text = forResolvedText;
731 m_render_cache->offset = aOffset;
732 m_render_cache->mirrored = mirrored;
733 }
734
735 return &m_render_cache->glyphs;
736 }
737
738 return nullptr;
739}
740
741
742void EDA_TEXT::SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont, const EDA_ANGLE& aAngle,
743 const VECTOR2I& aOffset )
744{
745 if( !m_render_cache )
746 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
747
748 m_render_cache->text = aResolvedText;
749 m_render_cache->font = aFont;
750 m_render_cache->angle = aAngle;
751 m_render_cache->offset = aOffset;
752 m_render_cache->mirrored = IsMirrored();
753 m_render_cache->glyphs.clear();
754}
755
756
758{
759 if( !m_render_cache )
760 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
761
762 m_render_cache->glyphs.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( aPoly ) );
763 static_cast<KIFONT::OUTLINE_GLYPH*>( m_render_cache->glyphs.back().get() )->CacheTriangulation();
764}
765
766
767int EDA_TEXT::GetInterline( const RENDER_SETTINGS* aSettings ) const
768{
769 return KiROUND( GetDrawFont( aSettings )->GetInterline( GetTextHeight(), getFontMetrics() ) );
770}
771
772
773BOX2I EDA_TEXT::GetTextBox( const RENDER_SETTINGS* aSettings, int aLine ) const
774{
775 VECTOR2I drawPos = GetDrawPos();
776
777 {
778 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
779 auto cache_it = m_bbox_cache.find( aLine );
780
781 if( cache_it != m_bbox_cache.end() && cache_it->second.m_pos == drawPos )
782 return cache_it->second.m_bbox;
783 }
784
785 BOX2I bbox;
786 wxArrayString strings;
787 wxString text = GetShownText( true );
788 int thickness = GetEffectiveTextPenWidth();
789
790 if( IsMultilineAllowed() )
791 {
792 wxStringSplit( text, strings, '\n' );
793
794 if( strings.GetCount() ) // GetCount() == 0 for void strings with multilines allowed
795 {
796 if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
797 text = strings.Item( aLine );
798 else
799 text = strings.Item( 0 );
800 }
801 }
802
803 // calculate the H and V size
804 KIFONT::FONT* font = GetDrawFont( aSettings );
805 VECTOR2D fontSize( GetTextSize() );
806 bool bold = IsBold();
807 bool italic = IsItalic();
808 VECTOR2I extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
809 int overbarOffset = 0;
810
811 // Creates bounding box (rectangle) for horizontal, left and top justified text. The
812 // bounding box will be moved later according to the actual text options
813 VECTOR2I textsize = VECTOR2I( extents.x, extents.y );
814 VECTOR2I pos = drawPos;
815 int fudgeFactor = KiROUND( extents.y * 0.17 );
816
817 if( font->IsStroke() )
818 textsize.y += fudgeFactor;
819
820 if( IsMultilineAllowed() && aLine > 0 && aLine < (int) strings.GetCount() )
821 pos.y -= KiROUND( aLine * font->GetInterline( fontSize.y, getFontMetrics() ) );
822
823 if( text.Contains( wxT( "~{" ) ) )
824 overbarOffset = extents.y / 6;
825
826 bbox.SetOrigin( pos );
827
828 // for multiline texts and aLine < 0, merge all rectangles (aLine == -1 signals all lines)
829 if( IsMultilineAllowed() && aLine < 0 && strings.GetCount() > 1 )
830 {
831 for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
832 {
833 text = strings.Item( ii );
834 extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
835 textsize.x = std::max( textsize.x, extents.x );
836 }
837
838 // interline spacing is only *between* lines, so total height is the height of the first
839 // line plus the interline distance (with interline spacing) for all subsequent lines
840 textsize.y += KiROUND( ( strings.GetCount() - 1 ) * font->GetInterline( fontSize.y, getFontMetrics() ) );
841 }
842
843 textsize.y += overbarOffset;
844
845 bbox.SetSize( textsize );
846
847 /*
848 * At this point the rectangle origin is the text origin (m_Pos). This is correct only for
849 * left and top justified, non-mirrored, non-overbarred texts. Recalculate for all others.
850 */
851 int italicOffset = IsItalic() ? KiROUND( fontSize.y * ITALIC_TILT ) : 0;
852
853 switch( GetHorizJustify() )
854 {
856 if( IsMirrored() )
857 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
858
859 break;
860
861 case GR_TEXT_H_ALIGN_CENTER: bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) / 2 ); break;
862
864 if( !IsMirrored() )
865 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
866 break;
867
868 case GR_TEXT_H_ALIGN_INDETERMINATE: wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) ); break;
869 }
870
871 switch( GetVertJustify() )
872 {
874 bbox.Offset( 0, -fudgeFactor );
875 break;
876
878 bbox.SetY( bbox.GetY() - bbox.GetHeight() / 2 );
879 break;
880
882 bbox.SetY( bbox.GetY() - bbox.GetHeight() );
883 bbox.Offset( 0, fudgeFactor );
884 break;
885
887 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
888 break;
889 }
890
891 bbox.Normalize(); // Make h and v sizes always >= 0
892
893 {
894 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
895 m_bbox_cache[aLine] = { drawPos, bbox };
896 }
897
898 return bbox;
899}
900
901
902bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
903{
904 const BOX2I rect = GetTextBox( nullptr ).GetInflated( aAccuracy );
905 const VECTOR2I location = GetRotated( aPoint, GetDrawPos(), -GetDrawRotation() );
906 return rect.Contains( location );
907}
908
909
910bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
911{
912 const BOX2I rect = aRect.GetInflated( aAccuracy );
913
914 if( aContains )
915 return rect.Contains( GetTextBox( nullptr ) );
916
917 return rect.Intersects( GetTextBox( nullptr ), GetDrawRotation() );
918}
919
920
921void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor )
922{
923 if( IsMultilineAllowed() )
924 {
925 std::vector<VECTOR2I> positions;
926 wxArrayString strings;
927 wxStringSplit( GetShownText( true ), strings, '\n' );
928
929 positions.reserve( strings.Count() );
930
931 GetLinePositions( aSettings, positions, (int) strings.Count() );
932
933 for( unsigned ii = 0; ii < strings.Count(); ii++ )
934 printOneLineOfText( aSettings, aOffset, aColor, strings[ii], positions[ii] );
935 }
936 else
937 {
938 printOneLineOfText( aSettings, aOffset, aColor, GetShownText( true ), GetDrawPos() );
939 }
940}
941
942
943void EDA_TEXT::GetLinePositions( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPositions,
944 int aLineCount ) const
945{
946 VECTOR2I pos = GetDrawPos(); // Position of first line of the multiline text according
947 // to the center of the multiline text block
948
949 VECTOR2I offset; // Offset to next line.
950
951 offset.y = GetInterline( aSettings );
952
953 if( aLineCount > 1 )
954 {
955 switch( GetVertJustify() )
956 {
958 break;
959
961 pos.y -= ( aLineCount - 1 ) * offset.y / 2;
962 break;
963
965 pos.y -= ( aLineCount - 1 ) * offset.y;
966 break;
967
969 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
970 break;
971 }
972 }
973
974 // Rotate the position of the first line around the center of the multiline text block
976
977 // Rotate the offset lines to increase happened in the right direction
978 RotatePoint( offset, GetDrawRotation() );
979
980 for( int ii = 0; ii < aLineCount; ii++ )
981 {
982 aPositions.push_back( (VECTOR2I) pos );
983 pos += offset;
984 }
985}
986
987
988void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor,
989 const wxString& aText, const VECTOR2I& aPos )
990{
991 wxDC* DC = aSettings->GetPrintDC();
992 int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
993
994 VECTOR2I size = GetTextSize();
995
996 if( IsMirrored() )
997 size.x = -size.x;
998
999 KIFONT::FONT* font = GetDrawFont( aSettings );
1000
1001 GRPrintText( DC, aOffset + aPos, aColor, aText, GetDrawRotation(), size, GetHorizJustify(), GetVertJustify(),
1002 penWidth, IsItalic(), IsBold(), font, getFontMetrics() );
1003}
1004
1005
1006bool recursiveDescent( const std::unique_ptr<MARKUP::NODE>& aNode )
1007{
1008 if( aNode->isURL() )
1009 return true;
1010
1011 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
1012 {
1013 if( recursiveDescent( child ) )
1014 return true;
1015 }
1016
1017 return false;
1018}
1019
1020
1022{
1023 wxString showntext = GetShownText( false );
1024 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( showntext ) );
1025 return recursiveDescent( markupParser.Parse() );
1026}
1027
1028
1030{
1031 int style = 0;
1032
1033 if( IsItalic() )
1034 style = 1;
1035
1036 if( IsBold() )
1037 style += 2;
1038
1039 wxString stylemsg[4] = { _( "Normal" ), _( "Italic" ), _( "Bold" ), _( "Bold+Italic" ) };
1040
1041 return stylemsg[style];
1042}
1043
1044
1046{
1047 if( GetFont() )
1048 return GetFont()->GetName();
1049 else
1050 return wxEmptyString;
1051}
1052
1053
1055{
1056 if( KIFONT::FONT* font = GetFont() )
1057 return font->GetName();
1058
1059 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1060 return _( "Default Font" );
1061 else
1062 return KICAD_FONT_NAME;
1063}
1064
1065
1066void EDA_TEXT::SetFontProp( const wxString& aFontName )
1067{
1068 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1069 {
1070 if( aFontName == _( "Default Font" ) )
1071 SetFont( nullptr );
1072 else
1073 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1074 }
1075 else
1076 {
1077 if( aFontName == KICAD_FONT_NAME )
1078 SetFont( nullptr );
1079 else
1080 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1081 }
1082}
1083
1084
1090
1091
1092void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const
1093{
1094 aFormatter->Print( "(effects" );
1095
1096 aFormatter->Print( "(font" );
1097
1098 if( GetFont() && !GetFont()->GetName().IsEmpty() )
1099 aFormatter->Print( "(face %s)", aFormatter->Quotew( GetFont()->NameAsToken() ).c_str() );
1100
1101 // Text size
1102 aFormatter->Print( "(size %s %s)", EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextHeight() ).c_str(),
1104
1105 if( GetLineSpacing() != 1.0 )
1106 {
1107 aFormatter->Print( "(line_spacing %s)", FormatDouble2Str( GetLineSpacing() ).c_str() );
1108 }
1109
1110 if( !GetAutoThickness() )
1111 {
1112 aFormatter->Print( "(thickness %s)",
1114 }
1115
1116 if( IsBold() )
1117 KICAD_FORMAT::FormatBool( aFormatter, "bold", true );
1118
1119 if( IsItalic() )
1120 KICAD_FORMAT::FormatBool( aFormatter, "italic", true );
1121
1122 if( !( aControlBits & CTL_OMIT_COLOR ) && GetTextColor() != COLOR4D::UNSPECIFIED )
1123 {
1124 aFormatter->Print( "(color %d %d %d %s)", KiROUND( GetTextColor().r * 255.0 ),
1125 KiROUND( GetTextColor().g * 255.0 ), KiROUND( GetTextColor().b * 255.0 ),
1126 FormatDouble2Str( GetTextColor().a ).c_str() );
1127 }
1128
1129 aFormatter->Print( ")" ); // (font
1130
1132 {
1133 aFormatter->Print( "(justify" );
1134
1136 aFormatter->Print( GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
1137
1139 aFormatter->Print( GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
1140
1141 if( IsMirrored() )
1142 aFormatter->Print( " mirror" );
1143
1144 aFormatter->Print( ")" ); // (justify
1145 }
1146
1147 if( !( aControlBits & CTL_OMIT_HYPERLINK ) && HasHyperlink() )
1148 aFormatter->Print( "(href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
1149
1150 aFormatter->Print( ")" ); // (effects
1151}
1152
1153
1154std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate, const BOX2I& aBBox,
1155 const EDA_ANGLE& aAngle ) const
1156{
1157 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
1158 KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
1159 KIFONT::FONT* font = GetDrawFont( nullptr );
1160 int penWidth = GetEffectiveTextPenWidth();
1161 wxString shownText( GetShownText( true ) );
1162 VECTOR2I drawPos = GetDrawPos();
1164
1165 attrs.m_Size = GetTextSize();
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:121
constexpr EDA_IU_SCALE unityScale
Definition base_units.h:124
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:233
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:142
constexpr coord_type GetY() const
Definition box2.h:204
constexpr size_type GetWidth() const
Definition box2.h:210
constexpr coord_type GetX() const
Definition box2.h:203
constexpr const Vec GetCenter() const
Definition box2.h:226
constexpr void SetSize(const SizeVec &size)
Definition box2.h:244
constexpr size_type GetHeight() const
Definition box2.h:211
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:164
constexpr void SetX(coord_type val)
Definition box2.h:273
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:634
constexpr void SetY(coord_type val)
Definition box2.h:278
constexpr void Offset(coord_type dx, coord_type dy)
Definition box2.h:255
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:307
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
virtual const std::vector< wxString > * GetEmbeddedFonts()
Definition eda_item.h:484
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:261
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:89
std::unique_ptr< EDA_TEXT_RENDER_CACHE_DATA > m_render_cache
Definition eda_text.h:493
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:282
void SetTextColor(const COLOR4D &aColor)
Definition eda_text.h:290
COLOR4D GetTextColor() const
Definition eda_text.h:291
wxString GetTextStyleName() const
virtual VECTOR2I GetTextPos() const
Definition eda_text.h:294
VECTOR2I m_pos
Definition eda_text.h:506
wxString m_text
Definition eda_text.h:481
std::map< int, BBOX_CACHE_ENTRY > m_bbox_cache
Definition eda_text.h:501
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:190
const std::vector< TEXT_VAR_REF_KEY > & GetTextVarReferences() const
Return the set of ${...} references extracted from the source text.
Definition eda_text.cpp:650
bool m_visible
Definition eda_text.h:507
virtual void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:532
bool IsMultilineAllowed() const
Definition eda_text.h:218
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:110
bool IsKeepUpright() const
Definition eda_text.h:227
virtual bool IsVisible() const
Definition eda_text.h:208
virtual void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:576
virtual void SetTextX(int aX)
Definition eda_text.cpp:583
virtual int GetTextHeight() const
Definition eda_text.h:288
bool m_shown_text_has_text_var_refs
Definition eda_text.h:483
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition eda_text.cpp:208
KIFONT::FONT * GetFont() const
Definition eda_text.h:268
bool ResolveFont(const std::vector< wxString > *aEmbeddedFonts)
Definition eda_text.cpp:503
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition eda_text.cpp:428
wxString m_shown_text
Definition eda_text.h:482
void SetMirrored(bool isMirrored)
Definition eda_text.cpp:388
wxString GetFontProp() const
virtual void SetTextY(int aY)
Definition eda_text.cpp:589
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:703
virtual EDA_ANGLE GetDrawRotation() const
Definition eda_text.h:400
wxString m_unresolvedFontName
Definition eda_text.h:505
virtual VECTOR2I GetDrawPos() const
Definition eda_text.h:401
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:773
virtual bool HasHyperlink() const
Definition eda_text.h:423
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:412
wxString GetHyperlink() const
Definition eda_text.h:424
virtual void Offset(const VECTOR2I &aOffset)
Definition eda_text.cpp:595
virtual int GetTextWidth() const
Definition eda_text.h:285
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:221
virtual void SetTextWidth(int aWidth)
Definition eda_text.cpp:554
virtual KIFONT::FONT * GetDrawFont(const RENDER_SETTINGS *aSettings) const
Definition eda_text.cpp:667
void SetBoldFlag(bool aBold)
Set only the bold flag, without changing the font.
Definition eda_text.cpp:373
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition eda_text.cpp:482
void SetupRenderCache(const wxString &aResolvedText, const KIFONT::FONT *aFont, const EDA_ANGLE &aAngle, const VECTOR2I &aOffset)
Definition eda_text.cpp:742
int Compare(const EDA_TEXT *aOther) const
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition eda_text.h:491
std::mutex m_bbox_cacheMutex
Definition eda_text.h:502
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:381
bool containsURL() const
EDA_TEXT(const EDA_IU_SCALE &aIuScale, const wxString &aText=wxEmptyString)
Definition eda_text.cpp:98
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
virtual void ClearBoundingBoxCache()
Definition eda_text.cpp:695
bool GetAutoThickness() const
Definition eda_text.h:160
double GetLineSpacing() const
Definition eda_text.h:279
double Similarity(const EDA_TEXT &aOther) const
void SetLineSpacing(double aLineSpacing)
Definition eda_text.cpp:524
wxString EvaluateText(const wxString &aText) const
Definition eda_text.cpp:656
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition eda_text.cpp:757
void Empty()
Definition eda_text.cpp:617
virtual void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition eda_text.cpp:279
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition eda_text.cpp:902
virtual void SetTextHeight(int aHeight)
Definition eda_text.cpp:565
virtual void cacheShownText()
Definition eda_text.cpp:624
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition eda_text.cpp:70
virtual void ClearRenderCache()
Definition eda_text.cpp:689
const TEXT_ATTRIBUTES & GetAttributes() const
Definition eda_text.h:252
void SetBold(bool aBold)
Set the text to be bold - this will also update the font if needed.
Definition eda_text.cpp:330
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:322
void SetAutoThickness(bool aAuto)
Definition eda_text.cpp:287
bool IsMirrored() const
Definition eda_text.h:211
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition eda_text.cpp:461
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition eda_text.cpp:448
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:921
double GetTextAngleDegrees() const
Definition eda_text.h:175
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:943
virtual const KIFONT::METRICS & getFontMetrics() const
Definition eda_text.cpp:683
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:205
void SetTextAngleDegrees(double aOrientation)
Definition eda_text.h:171
void SetHyperlink(wxString aLink)
Definition eda_text.h:425
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition eda_text.cpp:84
void SetKeepUpright(bool aKeepUpright)
Definition eda_text.cpp:420
void CopyText(const EDA_TEXT &aSrc)
Definition eda_text.cpp:272
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:224
int GetInterline(const RENDER_SETTINGS *aSettings) const
Return the distance between two lines of text.
Definition eda_text.cpp:767
int GetTextThicknessProperty() const
Definition eda_text.h:151
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition eda_text.h:121
virtual int GetTextThickness() const
Definition eda_text.h:149
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:265
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:294
std::vector< TEXT_VAR_REF_KEY > m_text_var_refs
Definition eda_text.h:489
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition eda_text.cpp:302
void SwapText(EDA_TEXT &aTradingPartner)
Definition eda_text.cpp:440
void SetMultilineAllowed(bool aAllow)
Definition eda_text.cpp:396
void SetFont(KIFONT::FONT *aFont)
Definition eda_text.cpp:495
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:988
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition eda_text.cpp:404
TEXT_ATTRIBUTES m_attributes
Definition eda_text.h:504
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: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:246
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:447
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:291
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:507
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:422
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:431
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:56
#define TEXT_MAX_SIZE_MM
Maximum text size in mm (~10 inches)
Definition eda_text.h:57
#define DEFAULT_SIZE_TEXT
This is the "default-of-the-default" hardcoded text size; individual application define their own def...
Definition eda_text.h:79
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
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 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: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:828
@ 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:147
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:83
constexpr bool IsEeschemaType(const KICAD_T aType)
Definition typeinfo.h:378
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682