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