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