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