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 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 Offset( VECTOR2I( aPoint.x - m_pos.x, aPoint.y - m_pos.y ) );
579}
580
581
582void EDA_TEXT::SetTextX( int aX )
583{
584 Offset( VECTOR2I( aX - m_pos.x, 0 ) );
585}
586
587
588void EDA_TEXT::SetTextY( int aY )
589{
590 Offset( VECTOR2I( 0, aY - m_pos.y ) );
591}
592
593
594void EDA_TEXT::Offset( const VECTOR2I& aOffset )
595{
596 if( aOffset.x == 0 && aOffset.y == 0 )
597 return;
598
599 m_pos += aOffset;
600
601 if( m_render_cache )
602 {
603 for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_render_cache->glyphs )
604 {
605 if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
606 outline->Move( aOffset );
607 else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
608 glyph = stroke->Transform( { 1.0, 1.0 }, aOffset, 0, ANGLE_0, false, { 0, 0 } );
609 }
610 }
611
613}
614
615
617{
618 m_text.Empty();
620}
621
622
624{
625 if( m_text.IsEmpty() )
626 {
627 m_shown_text = wxEmptyString;
629 }
630 else
631 {
633 m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) ) || m_shown_text.Contains( wxT( "@{" ) );
634 }
635
636 // Extract against raw m_text so backslash-escaped ${...} literals do not
637 // fabricate dependency edges. Eager population keeps the read path
638 // lock-free for concurrent workers.
639 if( m_text.IsEmpty() )
640 m_text_var_refs.clear();
641 else
643
646}
647
648
649const std::vector<TEXT_VAR_REF_KEY>& EDA_TEXT::GetTextVarReferences() const
650{
651 return m_text_var_refs;
652}
653
654
655wxString EDA_TEXT::EvaluateText( const wxString& aText ) const
656{
657 // Must not be static. EvaluateText runs on parallel workers (e.g.
658 // CONNECTION_GRAPH resolving label text) and a shared evaluator races on
659 // its internal error collector.
660 EXPRESSION_EVALUATOR evaluator;
661
662 return evaluator.Evaluate( aText );
663}
664
665
667{
668 KIFONT::FONT* font = GetFont();
669
670 if( !font )
671 {
672 if( aSettings )
673 font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
674 else
675 font = KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() );
676 }
677
678 return font;
679}
680
681
686
687
689{
690 m_render_cache.reset();
691}
692
693
695{
696 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
697 m_bbox_cache.clear();
698}
699
700
701std::vector<std::unique_ptr<KIFONT::GLYPH>>*
702EDA_TEXT::GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText, const VECTOR2I& aOffset ) const
703{
704 if( aFont->IsOutline() )
705 {
706 EDA_ANGLE resolvedAngle = GetDrawRotation();
707 bool mirrored = IsMirrored();
708
709 if( !m_render_cache )
710 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
711
712 if( m_render_cache->glyphs.empty() || m_render_cache->font != aFont
713 || m_render_cache->text != forResolvedText
714 || m_render_cache->angle != resolvedAngle || m_render_cache->offset != aOffset
715 || m_render_cache->mirrored != mirrored )
716 {
717 m_render_cache->glyphs.clear();
718
719 const KIFONT::OUTLINE_FONT* font = static_cast<const KIFONT::OUTLINE_FONT*>( aFont );
721
722 attrs.m_Angle = resolvedAngle;
723
724 font->GetLinesAsGlyphs( &m_render_cache->glyphs, forResolvedText, GetDrawPos() + aOffset, attrs,
725 getFontMetrics() );
726 m_render_cache->font = aFont;
727 m_render_cache->angle = resolvedAngle;
728 m_render_cache->text = forResolvedText;
729 m_render_cache->offset = aOffset;
730 m_render_cache->mirrored = mirrored;
731 }
732
733 return &m_render_cache->glyphs;
734 }
735
736 return nullptr;
737}
738
739
740void EDA_TEXT::SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont, const EDA_ANGLE& aAngle,
741 const VECTOR2I& aOffset )
742{
743 if( !m_render_cache )
744 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
745
746 m_render_cache->text = aResolvedText;
747 m_render_cache->font = aFont;
748 m_render_cache->angle = aAngle;
749 m_render_cache->offset = aOffset;
750 m_render_cache->mirrored = IsMirrored();
751 m_render_cache->glyphs.clear();
752}
753
754
756{
757 if( !m_render_cache )
758 m_render_cache = std::make_unique<EDA_TEXT_RENDER_CACHE_DATA>();
759
760 m_render_cache->glyphs.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( aPoly ) );
761 static_cast<KIFONT::OUTLINE_GLYPH*>( m_render_cache->glyphs.back().get() )->CacheTriangulation();
762}
763
764
765int EDA_TEXT::GetInterline( const RENDER_SETTINGS* aSettings ) const
766{
767 return KiROUND( GetDrawFont( aSettings )->GetInterline( GetTextHeight(), getFontMetrics() ) );
768}
769
770
771BOX2I EDA_TEXT::GetTextBox( const RENDER_SETTINGS* aSettings, int aLine ) const
772{
773 VECTOR2I drawPos = GetDrawPos();
774
775 {
776 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
777 auto cache_it = m_bbox_cache.find( aLine );
778
779 if( cache_it != m_bbox_cache.end() && cache_it->second.m_pos == drawPos )
780 return cache_it->second.m_bbox;
781 }
782
783 BOX2I bbox;
784 wxArrayString strings;
785 wxString text = GetShownText( true );
786 int thickness = GetEffectiveTextPenWidth();
787
788 if( IsMultilineAllowed() )
789 {
790 wxStringSplit( text, strings, '\n' );
791
792 if( strings.GetCount() ) // GetCount() == 0 for void strings with multilines allowed
793 {
794 if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
795 text = strings.Item( aLine );
796 else
797 text = strings.Item( 0 );
798 }
799 }
800
801 // calculate the H and V size
802 KIFONT::FONT* font = GetDrawFont( aSettings );
803 VECTOR2D fontSize( GetTextSize() );
804 bool bold = IsBold();
805 bool italic = IsItalic();
806 VECTOR2I extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
807 int overbarOffset = 0;
808
809 // Creates bounding box (rectangle) for horizontal, left and top justified text. The
810 // bounding box will be moved later according to the actual text options
811 VECTOR2I textsize = VECTOR2I( extents.x, extents.y );
812 VECTOR2I pos = drawPos;
813 int fudgeFactor = KiROUND( extents.y * 0.17 );
814
815 if( font->IsStroke() )
816 textsize.y += fudgeFactor;
817
818 if( IsMultilineAllowed() && aLine > 0 && aLine < (int) strings.GetCount() )
819 pos.y -= KiROUND( aLine * font->GetInterline( fontSize.y, getFontMetrics() ) );
820
821 if( text.Contains( wxT( "~{" ) ) )
822 overbarOffset = extents.y / 6;
823
824 bbox.SetOrigin( pos );
825
826 // for multiline texts and aLine < 0, merge all rectangles (aLine == -1 signals all lines)
827 if( IsMultilineAllowed() && aLine < 0 && strings.GetCount() > 1 )
828 {
829 for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
830 {
831 text = strings.Item( ii );
832 extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic, getFontMetrics() );
833 textsize.x = std::max( textsize.x, extents.x );
834 }
835
836 // interline spacing is only *between* lines, so total height is the height of the first
837 // line plus the interline distance (with interline spacing) for all subsequent lines
838 textsize.y += KiROUND( ( strings.GetCount() - 1 ) * font->GetInterline( fontSize.y, getFontMetrics() ) );
839 }
840
841 textsize.y += overbarOffset;
842
843 bbox.SetSize( textsize );
844
845 /*
846 * At this point the rectangle origin is the text origin (m_Pos). This is correct only for
847 * left and top justified, non-mirrored, non-overbarred texts. Recalculate for all others.
848 */
849 int italicOffset = IsItalic() ? KiROUND( fontSize.y * ITALIC_TILT ) : 0;
850
851 switch( GetHorizJustify() )
852 {
854 if( IsMirrored() )
855 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
856
857 break;
858
859 case GR_TEXT_H_ALIGN_CENTER: bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) / 2 ); break;
860
862 if( !IsMirrored() )
863 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
864 break;
865
866 case GR_TEXT_H_ALIGN_INDETERMINATE: wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) ); break;
867 }
868
869 switch( GetVertJustify() )
870 {
872 bbox.Offset( 0, -fudgeFactor );
873 break;
874
876 bbox.SetY( bbox.GetY() - bbox.GetHeight() / 2 );
877 break;
878
880 bbox.SetY( bbox.GetY() - bbox.GetHeight() );
881 bbox.Offset( 0, fudgeFactor );
882 break;
883
885 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
886 break;
887 }
888
889 bbox.Normalize(); // Make h and v sizes always >= 0
890
891 {
892 std::lock_guard<std::mutex> bboxLock( m_bbox_cacheMutex );
893 m_bbox_cache[aLine] = { drawPos, bbox };
894 }
895
896 return bbox;
897}
898
899
900bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
901{
902 const BOX2I rect = GetTextBox( nullptr ).GetInflated( aAccuracy );
903 const VECTOR2I location = GetRotated( aPoint, GetDrawPos(), -GetDrawRotation() );
904 return rect.Contains( location );
905}
906
907
908bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
909{
910 const BOX2I rect = aRect.GetInflated( aAccuracy );
911
912 if( aContains )
913 return rect.Contains( GetTextBox( nullptr ) );
914
915 return rect.Intersects( GetTextBox( nullptr ), GetDrawRotation() );
916}
917
918
919void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor )
920{
921 if( IsMultilineAllowed() )
922 {
923 std::vector<VECTOR2I> positions;
924 wxArrayString strings;
925 wxStringSplit( GetShownText( true ), strings, '\n' );
926
927 positions.reserve( strings.Count() );
928
929 GetLinePositions( aSettings, positions, (int) strings.Count() );
930
931 for( unsigned ii = 0; ii < strings.Count(); ii++ )
932 printOneLineOfText( aSettings, aOffset, aColor, strings[ii], positions[ii] );
933 }
934 else
935 {
936 printOneLineOfText( aSettings, aOffset, aColor, GetShownText( true ), GetDrawPos() );
937 }
938}
939
940
941void EDA_TEXT::GetLinePositions( const RENDER_SETTINGS* aSettings, std::vector<VECTOR2I>& aPositions,
942 int aLineCount ) const
943{
944 VECTOR2I pos = GetDrawPos(); // Position of first line of the multiline text according
945 // to the center of the multiline text block
946
947 VECTOR2I offset; // Offset to next line.
948
949 offset.y = GetInterline( aSettings );
950
951 if( aLineCount > 1 )
952 {
953 switch( GetVertJustify() )
954 {
956 break;
957
959 pos.y -= ( aLineCount - 1 ) * offset.y / 2;
960 break;
961
963 pos.y -= ( aLineCount - 1 ) * offset.y;
964 break;
965
967 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
968 break;
969 }
970 }
971
972 // Rotate the position of the first line around the center of the multiline text block
974
975 // Rotate the offset lines to increase happened in the right direction
976 RotatePoint( offset, GetDrawRotation() );
977
978 for( int ii = 0; ii < aLineCount; ii++ )
979 {
980 aPositions.push_back( (VECTOR2I) pos );
981 pos += offset;
982 }
983}
984
985
986void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor,
987 const wxString& aText, const VECTOR2I& aPos )
988{
989 wxDC* DC = aSettings->GetPrintDC();
990 int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
991
992 VECTOR2I size = GetTextSize();
993
994 if( IsMirrored() )
995 size.x = -size.x;
996
997 KIFONT::FONT* font = GetDrawFont( aSettings );
998
999 GRPrintText( DC, aOffset + aPos, aColor, aText, GetDrawRotation(), size, GetHorizJustify(), GetVertJustify(),
1000 penWidth, IsItalic(), IsBold(), font, getFontMetrics() );
1001}
1002
1003
1004bool recursiveDescent( const std::unique_ptr<MARKUP::NODE>& aNode )
1005{
1006 if( aNode->isURL() )
1007 return true;
1008
1009 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
1010 {
1011 if( recursiveDescent( child ) )
1012 return true;
1013 }
1014
1015 return false;
1016}
1017
1018
1020{
1021 wxString showntext = GetShownText( false );
1022 MARKUP::MARKUP_PARSER markupParser( TO_UTF8( showntext ) );
1023 return recursiveDescent( markupParser.Parse() );
1024}
1025
1026
1028{
1029 int style = 0;
1030
1031 if( IsItalic() )
1032 style = 1;
1033
1034 if( IsBold() )
1035 style += 2;
1036
1037 wxString stylemsg[4] = { _( "Normal" ), _( "Italic" ), _( "Bold" ), _( "Bold+Italic" ) };
1038
1039 return stylemsg[style];
1040}
1041
1042
1044{
1045 if( GetFont() )
1046 return GetFont()->GetName();
1047 else
1048 return wxEmptyString;
1049}
1050
1051
1053{
1054 if( KIFONT::FONT* font = GetFont() )
1055 return font->GetName();
1056
1057 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1058 return _( "Default Font" );
1059 else
1060 return KICAD_FONT_NAME;
1061}
1062
1063
1064void EDA_TEXT::SetFontProp( const wxString& aFontName )
1065{
1066 if( IsEeschemaType( dynamic_cast<const EDA_ITEM*>( this )->Type() ) )
1067 {
1068 if( aFontName == _( "Default Font" ) )
1069 SetFont( nullptr );
1070 else
1071 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1072 }
1073 else
1074 {
1075 if( aFontName == KICAD_FONT_NAME )
1076 SetFont( nullptr );
1077 else
1078 SetFont( KIFONT::FONT::GetFont( aFontName, IsBold(), IsItalic() ) );
1079 }
1080}
1081
1082
1088
1089
1090void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const
1091{
1092 aFormatter->Print( "(effects" );
1093
1094 aFormatter->Print( "(font" );
1095
1096 if( GetFont() && !GetFont()->GetName().IsEmpty() )
1097 aFormatter->Print( "(face %s)", aFormatter->Quotew( GetFont()->NameAsToken() ).c_str() );
1098
1099 // Text size
1100 aFormatter->Print( "(size %s %s)", EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextHeight() ).c_str(),
1102
1103 if( GetLineSpacing() != 1.0 )
1104 {
1105 aFormatter->Print( "(line_spacing %s)", FormatDouble2Str( GetLineSpacing() ).c_str() );
1106 }
1107
1108 if( !GetAutoThickness() )
1109 {
1110 aFormatter->Print( "(thickness %s)",
1112 }
1113
1114 if( IsBold() )
1115 KICAD_FORMAT::FormatBool( aFormatter, "bold", true );
1116
1117 if( IsItalic() )
1118 KICAD_FORMAT::FormatBool( aFormatter, "italic", true );
1119
1120 if( !( aControlBits & CTL_OMIT_COLOR ) && GetTextColor() != COLOR4D::UNSPECIFIED )
1121 {
1122 aFormatter->Print( "(color %d %d %d %s)", KiROUND( GetTextColor().r * 255.0 ),
1123 KiROUND( GetTextColor().g * 255.0 ), KiROUND( GetTextColor().b * 255.0 ),
1124 FormatDouble2Str( GetTextColor().a ).c_str() );
1125 }
1126
1127 aFormatter->Print( ")" ); // (font
1128
1130 {
1131 aFormatter->Print( "(justify" );
1132
1134 aFormatter->Print( GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
1135
1137 aFormatter->Print( GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
1138
1139 if( IsMirrored() )
1140 aFormatter->Print( " mirror" );
1141
1142 aFormatter->Print( ")" ); // (justify
1143 }
1144
1145 if( !( aControlBits & CTL_OMIT_HYPERLINK ) && HasHyperlink() )
1146 aFormatter->Print( "(href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
1147
1148 aFormatter->Print( ")" ); // (effects
1149}
1150
1151
1152std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate, const BOX2I& aBBox,
1153 const EDA_ANGLE& aAngle ) const
1154{
1155 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
1156 KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
1157 KIFONT::FONT* font = GetDrawFont( nullptr );
1158 int penWidth = GetEffectiveTextPenWidth();
1159 wxString shownText( GetShownText( true ) );
1160 VECTOR2I drawPos = GetDrawPos();
1162
1163 std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = nullptr;
1164
1165 if( aBBox.GetWidth() )
1166 {
1167 drawPos = aBBox.GetCenter();
1170 attrs.m_Angle = aAngle;
1171 }
1172 else
1173 {
1174 attrs.m_Angle = GetDrawRotation();
1175
1176 if( font->IsOutline() )
1177 cache = GetRenderCache( font, shownText, VECTOR2I() );
1178 }
1179
1180 if( aTriangulate )
1181 {
1182 CALLBACK_GAL callback_gal(
1183 empty_opts,
1184 // Stroke callback
1185 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
1186 {
1187 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
1188 },
1189 // Triangulation callback
1190 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
1191 {
1192 SHAPE_SIMPLE* triShape = new SHAPE_SIMPLE;
1193
1194 for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
1195 triShape->Append( point.x, point.y );
1196
1197 shape->AddShape( triShape );
1198 } );
1199
1200 if( cache )
1201 callback_gal.DrawGlyphs( *cache );
1202 else
1203 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
1204 }
1205 else
1206 {
1207 CALLBACK_GAL callback_gal(
1208 empty_opts,
1209 // Stroke callback
1210 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
1211 {
1212 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
1213 },
1214 // Outline callback
1215 [&]( const SHAPE_LINE_CHAIN& aPoly )
1216 {
1217 shape->AddShape( aPoly.Clone() );
1218 } );
1219
1220 if( cache )
1221 callback_gal.DrawGlyphs( *cache );
1222 else
1223 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
1224 }
1225
1226 return shape;
1227}
1228
1229
1230int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
1231{
1232 wxCHECK( aOther, 1 );
1233
1234 int val = m_attributes.Compare( aOther->m_attributes );
1235
1236 if( val != 0 )
1237 return val;
1238
1239 if( m_pos.x != aOther->m_pos.x )
1240 return m_pos.x - aOther->m_pos.x;
1241
1242 if( m_pos.y != aOther->m_pos.y )
1243 return m_pos.y - aOther->m_pos.y;
1244
1245 val = GetFontName().Cmp( aOther->GetFontName() );
1246
1247 if( val != 0 )
1248 return val;
1249
1250 return m_text.Cmp( aOther->m_text );
1251}
1252
1253
1254bool EDA_TEXT::ValidateHyperlink( const wxString& aURL )
1255{
1256 if( aURL.IsEmpty() || IsGotoPageHref( aURL ) )
1257 return true;
1258
1259 wxURI uri;
1260
1261 return ( uri.Create( aURL ) && uri.HasScheme() );
1262}
1263
1264double EDA_TEXT::Levenshtein( const EDA_TEXT& aOther ) const
1265{
1266 // Compute the Levenshtein distance between the two strings
1267 const wxString& str1 = GetText();
1268 const wxString& str2 = aOther.GetText();
1269
1270 int m = str1.length();
1271 int n = str2.length();
1272
1273 if( n == 0 || m == 0 )
1274 return 0.0;
1275
1276 // Create a matrix to store the distance values
1277 std::vector<std::vector<int>> distance( m + 1, std::vector<int>( n + 1 ) );
1278
1279 // Initialize the matrix
1280 for( int i = 0; i <= m; i++ )
1281 distance[i][0] = i;
1282 for( int j = 0; j <= n; j++ )
1283 distance[0][j] = j;
1284
1285 // Calculate the distance
1286 for( int i = 1; i <= m; i++ )
1287 {
1288 for( int j = 1; j <= n; j++ )
1289 {
1290 if( str1[i - 1] == str2[j - 1] )
1291 {
1292 distance[i][j] = distance[i - 1][j - 1];
1293 }
1294 else
1295 {
1296 distance[i][j] = std::min( { distance[i - 1][j], distance[i][j - 1], distance[i - 1][j - 1] } ) + 1;
1297 }
1298 }
1299 }
1300
1301 // Calculate similarity score
1302 int maxLen = std::max( m, n );
1303 double similarity = 1.0 - ( static_cast<double>( distance[m][n] ) / maxLen );
1304
1305 return similarity;
1306}
1307
1308
1309double EDA_TEXT::Similarity( const EDA_TEXT& aOther ) const
1310{
1311 double retval = 1.0;
1312
1313 if( !( m_attributes == aOther.m_attributes ) )
1314 retval *= 0.9;
1315
1316 if( m_pos != aOther.m_pos )
1317 retval *= 0.9;
1318
1319 retval *= Levenshtein( aOther );
1320
1321 return retval;
1322}
1323
1324
1325bool EDA_TEXT::IsGotoPageHref( const wxString& aHref, wxString* aDestination )
1326{
1327 return aHref.StartsWith( wxT( "#" ), aDestination );
1328}
1329
1330
1331wxString EDA_TEXT::GotoPageHref( const wxString& aDestination )
1332{
1333 return wxT( "#" ) + aDestination;
1334}
1335
1336
1337std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aText )
1338{
1339 aStream << aText.GetText();
1340
1341 return aStream;
1342}
1343
1344
1345static struct EDA_TEXT_DESC
1346{
1348 {
1349 // These are defined in SCH_FIELD as well but initialization order is
1350 // not defined, so this needs to be conditional. Defining in both
1351 // places leads to duplicate symbols.
1353
1354 if( h_inst.Choices().GetCount() == 0 )
1355 {
1356 h_inst.Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) );
1357 h_inst.Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) );
1358 h_inst.Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
1359 }
1360
1362
1363 if( v_inst.Choices().GetCount() == 0 )
1364 {
1365 v_inst.Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) );
1366 v_inst.Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) );
1367 v_inst.Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
1368 }
1369
1372
1376
1377 const wxString textProps = _HKI( "Text Properties" );
1378
1380 textProps );
1381
1384 textProps )
1387 []( INSPECTABLE* aItem )
1388 {
1389 EDA_ITEM* eda_item = static_cast<EDA_ITEM*>( aItem );
1390 wxPGChoices fonts;
1391 std::vector<std::string> fontNames;
1392
1393 Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ),
1394 eda_item->GetEmbeddedFonts() );
1395
1396 if( IsEeschemaType( eda_item->Type() ) )
1397 fonts.Add( _( "Default Font" ) );
1398
1399 fonts.Add( KICAD_FONT_NAME );
1400
1401 for( const std::string& fontName : fontNames )
1402 fonts.Add( wxString( fontName ) );
1403
1404 return fonts;
1405 } );
1406
1407 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Auto Thickness" ), &EDA_TEXT::SetAutoThickness,
1409 textProps );
1413 textProps );
1414 propMgr.AddProperty(
1416 textProps );
1418 textProps );
1419 propMgr.AddProperty(
1421 textProps );
1422
1423 auto isField = []( INSPECTABLE* aItem ) -> bool
1424 {
1425 if( EDA_ITEM* item = dynamic_cast<EDA_ITEM*>( aItem ) )
1426 return item->Type() == SCH_FIELD_T || item->Type() == PCB_FIELD_T;
1427
1428 return false;
1429 };
1430
1431 propMgr.AddProperty(
1433 textProps )
1434 .SetAvailableFunc( isField );
1435
1438 textProps );
1439
1442 textProps );
1443
1444 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_H_ALIGN_T>( _HKI( "Horizontal Justification" ),
1447 textProps );
1448 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_V_ALIGN_T>( _HKI( "Vertical Justification" ),
1451 textProps );
1452
1453 propMgr.AddProperty(
1455 textProps );
1456
1459 textProps );
1460 }
1462
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:44
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:481
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:246
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:649
bool m_visible
Definition eda_text.h:511
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:532
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:576
void SetTextX(int aX)
Definition eda_text.cpp:582
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:588
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:702
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:771
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:594
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:554
virtual KIFONT::FONT * GetDrawFont(const RENDER_SETTINGS *aSettings) const
Definition eda_text.cpp:666
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:740
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:694
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:524
wxString EvaluateText(const wxString &aText) const
Definition eda_text.cpp:655
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition eda_text.cpp:755
void Empty()
Definition eda_text.cpp:616
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:900
void SetTextHeight(int aHeight)
Definition eda_text.cpp:565
virtual void cacheShownText()
Definition eda_text.cpp:623
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition eda_text.cpp:74
virtual void ClearRenderCache()
Definition eda_text.cpp:688
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:919
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:941
virtual const KIFONT::METRICS & getFontMetrics() const
Definition eda_text.cpp:682
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:765
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:986
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:370
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
VECTOR2< double > VECTOR2D
Definition vector2d.h:686