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