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