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