KiCad PCB EDA Suite
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 (C) 2004-2023 KiCad Developers, see change_log.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
30#include <algorithm> // for max
31#include <stddef.h> // for NULL
32#include <type_traits> // for swap
33#include <vector>
34
35#include <eda_item.h>
36#include <base_units.h>
37#include <callback_gal.h>
38#include <eda_text.h> // for EDA_TEXT, TEXT_EFFECTS, GR_TEXT_VJUSTIF...
39#include <gal/color4d.h> // for COLOR4D, COLOR4D::BLACK
40#include <gr_text.h>
41#include <string_utils.h> // for UnescapeString
42#include <math/util.h> // for KiROUND
43#include <math/vector2d.h>
44#include <richio.h>
45#include <render_settings.h>
46#include <trigo.h> // for RotatePoint
47#include <i18n_utility.h>
51#include <font/outline_font.h>
54#include <pcbnew.h> // Text limits are in here for some reason
55
56#include <wx/debug.h> // for wxASSERT
57#include <wx/string.h>
58#include <wx/url.h> // for wxURL
59
60class OUTPUTFORMATTER;
61class wxFindReplaceData;
62
63
65{
66 wxASSERT( aHorizJustify >= GR_TEXT_H_ALIGN_LEFT && aHorizJustify <= GR_TEXT_H_ALIGN_RIGHT );
67
68 if( aHorizJustify > GR_TEXT_H_ALIGN_RIGHT )
70
71 if( aHorizJustify < GR_TEXT_H_ALIGN_LEFT )
73
74 return static_cast<GR_TEXT_H_ALIGN_T>( aHorizJustify );
75}
76
77
79{
80 wxASSERT( aVertJustify >= GR_TEXT_V_ALIGN_TOP && aVertJustify <= GR_TEXT_V_ALIGN_BOTTOM );
81
82 if( aVertJustify > GR_TEXT_V_ALIGN_BOTTOM )
84
85 if( aVertJustify < GR_TEXT_V_ALIGN_TOP )
87
88 return static_cast<GR_TEXT_V_ALIGN_T>( aVertJustify );
89}
90
91
92EDA_TEXT::EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText ) :
93 m_text( aText ),
94 m_IuScale( aIuScale ),
95 m_bounding_box_cache_valid( false ),
96 m_bounding_box_cache_line( -1 ),
97 m_bounding_box_cache_inverted( false )
98{
102}
103
104
106 m_IuScale( aText.m_IuScale )
107{
108 m_text = aText.m_text;
111
113 m_pos = aText.m_pos;
114
117
118 m_render_cache.clear();
119
120 for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aText.m_render_cache )
121 {
122 KIFONT::OUTLINE_GLYPH* outline_glyph = static_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() );
123 m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline_glyph ) );
124 }
125
130}
131
132
134{
135}
136
137
139{
140 m_text = aText.m_text;
143
145 m_pos = aText.m_pos;
146
149
150 m_render_cache.clear();
151
152 for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aText.m_render_cache )
153 {
154 KIFONT::OUTLINE_GLYPH* outline_glyph = static_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() );
155 m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline_glyph ) );
156 }
157
160
161 return *this;
162}
163
164
165void EDA_TEXT::SetText( const wxString& aText )
166{
167 m_text = aText;
168
170
173}
174
175
176void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
177{
178 m_text = aSrc.m_text;
181
184}
185
186
188{
192}
193
194
196{
197 m_attributes.m_Angle = aAngle;
200}
201
202
203void EDA_TEXT::SetItalic( bool aItalic )
204{
205 m_attributes.m_Italic = aItalic;
208}
209
210
211void EDA_TEXT::SetBold( bool aBold )
212{
213 m_attributes.m_Bold = aBold;
216}
217
218
219void EDA_TEXT::SetVisible( bool aVisible )
220{
221 m_attributes.m_Visible = aVisible;
223}
224
225
226void EDA_TEXT::SetMirrored( bool isMirrored )
227{
228 m_attributes.m_Mirrored = isMirrored;
231}
232
233
235{
236 m_attributes.m_Multiline = aAllow;
239}
240
241
243{
244 m_attributes.m_Halign = aType;
247}
248
249
251{
252 m_attributes.m_Valign = aType;
255}
256
257
258void EDA_TEXT::SetKeepUpright( bool aKeepUpright )
259{
260 m_attributes.m_KeepUpright = aKeepUpright;
263}
264
265
267{
269 m_pos = aSrc.m_pos;
272}
273
274
275void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
276{
277 std::swap( m_text, aTradingPartner.m_text );
278 std::swap( m_shown_text, aTradingPartner.m_shown_text );
280
283}
284
285
286void EDA_TEXT::SwapAttributes( EDA_TEXT& aTradingPartner )
287{
288 std::swap( m_attributes, aTradingPartner.m_attributes );
289 std::swap( m_pos, aTradingPartner.m_pos );
290
292 aTradingPartner.ClearRenderCache();
293
295 aTradingPartner.m_bounding_box_cache_valid = false;
296}
297
298
299int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultPenWidth ) const
300{
301 int penWidth = GetTextThickness();
302
303 if( penWidth <= 1 )
304 {
305 penWidth = aDefaultPenWidth;
306
307 if( IsBold() )
308 penWidth = GetPenSizeForBold( GetTextWidth() );
309 else if( penWidth <= 1 )
310 penWidth = GetPenSizeForNormal( GetTextWidth() );
311 }
312
313 // Clip pen size for small texts:
314 penWidth = Clamp_Text_PenSize( penWidth, GetTextSize() );
315
316 return penWidth;
317}
318
319
320bool EDA_TEXT::Replace( const EDA_SEARCH_DATA& aSearchData )
321{
322 bool retval = EDA_ITEM::Replace( aSearchData, m_text );
323
325
328
329 return retval;
330}
331
332
334{
335 m_attributes.m_Font = aFont;
338}
339
340
341void EDA_TEXT::SetLineSpacing( double aLineSpacing )
342{
343 m_attributes.m_LineSpacing = aLineSpacing;
346}
347
348
349void EDA_TEXT::SetTextSize( const VECTOR2I& aNewSize )
350{
351 m_attributes.m_Size = aNewSize;
354}
355
356
357void EDA_TEXT::SetTextWidth( int aWidth )
358{
359 m_attributes.m_Size.x = aWidth;
362}
363
364
365void EDA_TEXT::SetTextHeight( int aHeight )
366{
367 m_attributes.m_Size.y = aHeight;
370}
371
372
373void EDA_TEXT::SetTextPos( const VECTOR2I& aPoint )
374{
375 Offset( VECTOR2I( aPoint.x - m_pos.x, aPoint.y - m_pos.y ) );
376}
377
378
379void EDA_TEXT::SetTextX( int aX )
380{
381 Offset( VECTOR2I( aX - m_pos.x, 0 ) );
382}
383
384
385void EDA_TEXT::SetTextY( int aY )
386{
387 Offset( VECTOR2I( 0, aY - m_pos.y ) );
388}
389
390
391void EDA_TEXT::Offset( const VECTOR2I& aOffset )
392{
393 m_pos += aOffset;
394
395 for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_render_cache )
396 static_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() )->Move( aOffset );
397
399}
400
401
403{
404 m_text.Empty();
407}
408
409
411{
412 if( m_text.IsEmpty() )
413 {
414 m_shown_text = wxEmptyString;
416 }
417 else
418 {
420 m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
421 }
422
425}
426
427
429{
430 KIFONT::FONT* font = GetFont();
431
432 if( !font )
433 font = KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() );
434
435 return font;
436}
437
438
439
441{
442 m_render_cache.clear();
443}
444
445
447{
449}
450
451
452std::vector<std::unique_ptr<KIFONT::GLYPH>>*
453EDA_TEXT::GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
454 const VECTOR2I& aOffset ) const
455{
456 if( getDrawFont()->IsOutline() )
457 {
458 EDA_ANGLE resolvedAngle = GetDrawRotation();
459
460 if( m_render_cache.empty()
461 || m_render_cache_text != forResolvedText
462 || m_render_cache_angle != resolvedAngle
463 || m_render_cache_offset != aOffset )
464 {
465 m_render_cache.clear();
466
467 KIFONT::OUTLINE_FONT* font = static_cast<KIFONT::OUTLINE_FONT*>( getDrawFont() );
469
470 attrs.m_Angle = resolvedAngle;
471
472 font->GetLinesAsGlyphs( &m_render_cache, GetShownText(), GetDrawPos() + aOffset, attrs );
473 m_render_cache_angle = resolvedAngle;
474 m_render_cache_text = forResolvedText;
475 m_render_cache_offset = aOffset;
476 }
477
478 return &m_render_cache;
479 }
480
481 return nullptr;
482}
483
484
485void EDA_TEXT::SetupRenderCache( const wxString& aResolvedText, const EDA_ANGLE& aAngle )
486{
487 m_render_cache_text = aResolvedText;
488 m_render_cache_angle = aAngle;
489 m_render_cache.clear();
490}
491
492
494{
495 m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( aPoly ) );
496}
497
498
500{
502}
503
504
505BOX2I EDA_TEXT::GetTextBox( int aLine, bool aInvertY ) const
506{
507 VECTOR2I drawPos = GetDrawPos();
508
510 && m_bounding_box_cache_pos == drawPos
511 && m_bounding_box_cache_line == aLine
512 && m_bounding_box_cache_inverted == aInvertY )
513 {
515 }
516
517 BOX2I bbox;
518 wxArrayString strings;
519 wxString text = GetShownText();
520 int thickness = GetEffectiveTextPenWidth();
521
522 if( IsMultilineAllowed() )
523 {
524 wxStringSplit( text, strings, '\n' );
525
526 if( strings.GetCount() ) // GetCount() == 0 for void strings with multilines allowed
527 {
528 if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
529 text = strings.Item( aLine );
530 else
531 text = strings.Item( 0 );
532 }
533 }
534
535 // calculate the H and V size
536 KIFONT::FONT* font = getDrawFont();
537 VECTOR2D fontSize( GetTextSize() );
538 bool bold = IsBold();
539 bool italic = IsItalic();
540 VECTOR2I extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic );
541 int overbarOffset = 0;
542
543 // Creates bounding box (rectangle) for horizontal, left and top justified text. The
544 // bounding box will be moved later according to the actual text options
545 VECTOR2I textsize = VECTOR2I( extents.x, extents.y );
546 VECTOR2I pos = drawPos;
547
548 if( IsMultilineAllowed() && aLine > 0 && aLine < (int) strings.GetCount() )
549 pos.y -= KiROUND( aLine * font->GetInterline( fontSize.y ) );
550
551 if( text.Contains( wxT( "~{" ) ) )
552 overbarOffset = extents.y / 14;
553
554 if( aInvertY )
555 pos.y = -pos.y;
556
557 bbox.SetOrigin( pos );
558
559 // for multiline texts and aLine < 0, merge all rectangles (aLine == -1 signals all lines)
560 if( IsMultilineAllowed() && aLine < 0 && strings.GetCount() )
561 {
562 for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
563 {
564 text = strings.Item( ii );
565 extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic );
566 textsize.x = std::max( textsize.x, extents.x );
567 }
568
569 // interline spacing is only *between* lines, so total height is the height of the first
570 // line plus the interline distance (with interline spacing) for all subsequent lines
571 textsize.y += KiROUND( ( strings.GetCount() - 1 ) * font->GetInterline( fontSize.y ) );
572 }
573
574 bbox.SetSize( textsize );
575
576 /*
577 * At this point the rectangle origin is the text origin (m_Pos). This is correct only for
578 * left and top justified, non-mirrored, non-overbarred texts. Recalculate for all others.
579 */
580 int italicOffset = IsItalic() ? KiROUND( fontSize.y * ITALIC_TILT ) : 0;
581
582 switch( GetHorizJustify() )
583 {
585 if( IsMirrored() )
586 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
587 break;
588
590 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) / 2 );
591 break;
592
594 if( !IsMirrored() )
595 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
596 break;
597 }
598
599 switch( GetVertJustify() )
600 {
602 break;
603
605 bbox.SetY( bbox.GetY() - ( bbox.GetHeight() + overbarOffset ) / 2 );
606 break;
607
609 bbox.SetY( bbox.GetY() - ( bbox.GetHeight() + overbarOffset ) );
610 break;
611 }
612
613 bbox.Normalize(); // Make h and v sizes always >= 0
614
616 m_bounding_box_cache_pos = drawPos;
620
621 return bbox;
622}
623
624
625bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
626{
627 BOX2I rect = GetTextBox();
628 VECTOR2I location = aPoint;
629
630 rect.Inflate( aAccuracy );
631 RotatePoint( location, GetDrawPos(), -GetDrawRotation() );
632
633 return rect.Contains( location );
634}
635
636
637bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
638{
639 BOX2I rect = aRect;
640
641 rect.Inflate( aAccuracy );
642
643 if( aContains )
644 return rect.Contains( GetTextBox() );
645
646 return rect.Intersects( GetTextBox(), GetDrawRotation() );
647}
648
649
650void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
651 const COLOR4D& aColor, OUTLINE_MODE aFillMode )
652{
653 if( IsMultilineAllowed() )
654 {
655 std::vector<VECTOR2I> positions;
656 wxArrayString strings;
657 wxStringSplit( GetShownText(), strings, '\n' );
658
659 positions.reserve( strings.Count() );
660
661 GetLinePositions( positions, (int) strings.Count() );
662
663 for( unsigned ii = 0; ii < strings.Count(); ii++ )
664 printOneLineOfText( aSettings, aOffset, aColor, aFillMode, strings[ii], positions[ii] );
665 }
666 else
667 {
668 printOneLineOfText( aSettings, aOffset, aColor, aFillMode, GetShownText(), GetDrawPos() );
669 }
670}
671
672
673void EDA_TEXT::GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const
674{
675 VECTOR2I pos = GetDrawPos(); // Position of first line of the multiline text according
676 // to the center of the multiline text block
677
678 VECTOR2I offset; // Offset to next line.
679
680 offset.y = GetInterline();
681
682 if( aLineCount > 1 )
683 {
684 switch( GetVertJustify() )
685 {
687 break;
688
690 pos.y -= ( aLineCount - 1 ) * offset.y / 2;
691 break;
692
694 pos.y -= ( aLineCount - 1 ) * offset.y;
695 break;
696 }
697 }
698
699 // Rotate the position of the first line around the center of the multiline text block
701
702 // Rotate the offset lines to increase happened in the right direction
703 RotatePoint( offset, GetDrawRotation() );
704
705 for( int ii = 0; ii < aLineCount; ii++ )
706 {
707 aPositions.push_back( (VECTOR2I) pos );
708 pos += offset;
709 }
710}
711
712
713void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
714 const COLOR4D& aColor, OUTLINE_MODE aFillMode,
715 const wxString& aText, const VECTOR2I& aPos )
716{
717 wxDC* DC = aSettings->GetPrintDC();
718 int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
719
720 if( aFillMode == SKETCH )
721 penWidth = -penWidth;
722
723 VECTOR2I size = GetTextSize();
724
725 if( IsMirrored() )
726 size.x = -size.x;
727
728 KIFONT::FONT* font = GetFont();
729
730 if( !font )
731 font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
732
733 GRPrintText( DC, aOffset + aPos, aColor, aText, GetDrawRotation(), size, GetHorizJustify(),
734 GetVertJustify(), penWidth, IsItalic(), IsBold(), font );
735}
736
737
739{
740 int style = 0;
741
742 if( IsItalic() )
743 style = 1;
744
745 if( IsBold() )
746 style += 2;
747
748 wxString stylemsg[4] = {
749 _("Normal"),
750 _("Italic"),
751 _("Bold"),
752 _("Bold+Italic")
753 };
754
755 return stylemsg[style];
756}
757
758
759wxString EDA_TEXT::GetFontName() const
760{
761 if( GetFont() )
762 return GetFont()->GetName();
763 else
764 return wxEmptyString;
765}
766
767
769{
770 return ( IsVisible()
771 && !IsMirrored()
774 && GetTextThickness() == 0
775 && !IsItalic()
776 && !IsBold()
778 && GetFontName().IsEmpty()
779 );
780}
781
782
783void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
784{
785 aFormatter->Print( aNestLevel + 1, "(effects" );
786
787 aFormatter->Print( 0, " (font" );
788
789 if( GetFont() && !GetFont()->GetName().IsEmpty() )
790 aFormatter->Print( 0, " (face \"%s\")", GetFont()->NameAsToken() );
791
792 // Text size
793 aFormatter->Print( 0, " (size %s %s)",
796
797 if( GetLineSpacing() != 1.0 )
798 {
799 aFormatter->Print( 0, " (line_spacing %s)",
800 FormatDouble2Str( GetLineSpacing() ).c_str() );
801 }
802
803 if( GetTextThickness() )
804 {
805 aFormatter->Print( 0, " (thickness %s)",
807 }
808
809 if( IsBold() )
810 aFormatter->Print( 0, " bold" );
811
812 if( IsItalic() )
813 aFormatter->Print( 0, " italic" );
814
815 if( GetTextColor() != COLOR4D::UNSPECIFIED )
816 {
817 aFormatter->Print( 0, " (color %d %d %d %s)",
818 KiROUND( GetTextColor().r * 255.0 ),
819 KiROUND( GetTextColor().g * 255.0 ),
820 KiROUND( GetTextColor().b * 255.0 ),
821 FormatDouble2Str( GetTextColor().a ).c_str() );
822 }
823
824 aFormatter->Print( 0, ")"); // (font
825
828 {
829 aFormatter->Print( 0, " (justify");
830
832 aFormatter->Print( 0, GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
833
835 aFormatter->Print( 0, GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
836
837 if( IsMirrored() )
838 aFormatter->Print( 0, " mirror" );
839
840 aFormatter->Print( 0, ")" ); // (justify
841 }
842
843 if( !( aControlBits & CTL_OMIT_HIDE ) && !IsVisible() )
844 aFormatter->Print( 0, " hide" );
845
846 if( HasHyperlink() )
847 {
848 aFormatter->Print( 0, " (href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
849 }
850
851 aFormatter->Print( 0, ")\n" ); // (effects
852}
853
854
855std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate,
856 bool aUseTextRotation ) const
857{
858 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
860 KIFONT::FONT* font = getDrawFont();
861 int penWidth = GetEffectiveTextPenWidth();
863
864 if( aUseTextRotation )
865 attrs.m_Angle = GetDrawRotation();
866 else
867 attrs.m_Angle = ANGLE_0;
868
869 if( aTriangulate )
870 {
871 CALLBACK_GAL callback_gal(
872 empty_opts,
873 // Stroke callback
874 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
875 {
876 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
877 },
878 // Triangulation callback
879 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
880 {
881 SHAPE_SIMPLE* triShape = new SHAPE_SIMPLE;
882
883 for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
884 triShape->Append( point.x, point.y );
885
886 shape->AddShape( triShape );
887 } );
888
889 font->Draw( &callback_gal, GetShownText(), GetDrawPos(), attrs );
890 }
891 else
892 {
893 CALLBACK_GAL callback_gal(
894 empty_opts,
895 // Stroke callback
896 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
897 {
898 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
899 },
900 // Outline callback
901 [&]( const SHAPE_LINE_CHAIN& aPoly )
902 {
903 shape->AddShape( aPoly.Clone() );
904 } );
905
906 font->Draw( &callback_gal, GetShownText(), GetDrawPos(), attrs );
907 }
908
909 return shape;
910}
911
912
913int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
914{
915#define EPSILON 2 // Should be enough for rounding errors on calculated items
916
917#define TEST( a, b ) { if( a != b ) return a - b; }
918#define TEST_E( a, b ) { if( abs( a - b ) > EPSILON ) return a - b; }
919#define TEST_PT( a, b ) { TEST_E( a.x, b.x ); TEST_E( a.y, b.y ); }
920
921 TEST_PT( m_pos, aOther->m_pos );
922
927
937
938 int val = GetFontName().Cmp( aOther->GetFontName() );
939
940 if( val != 0 )
941 return val;
942
943 return m_text.Cmp( aOther->m_text );
944}
945
946
947void EDA_TEXT::TransformBoundingBoxToPolygon( SHAPE_POLY_SET* aBuffer, int aClearance ) const
948{
949 if( GetText().Length() == 0 )
950 return;
951
952 VECTOR2I corners[4]; // Buffer of polygon corners
953 BOX2I rect = GetTextBox();
954
955 // TrueType bounding boxes aren't guaranteed to include all descenders, diacriticals, etc.
956 // Since we use this for zone knockouts and DRC, we need something more accurate.
957 if( getDrawFont()->IsOutline() )
958 rect = GetEffectiveTextShape( false, false )->BBox();
959
960 rect.Inflate( aClearance );
961
962 corners[0].x = rect.GetOrigin().x;
963 corners[0].y = rect.GetOrigin().y;
964 corners[1].y = corners[0].y;
965 corners[1].x = rect.GetRight();
966 corners[2].x = corners[1].x;
967 corners[2].y = rect.GetBottom();
968 corners[3].y = corners[2].y;
969 corners[3].x = corners[0].x;
970
971 aBuffer->NewOutline();
972
973 for( VECTOR2I& corner : corners )
974 {
975 RotatePoint( corner, GetDrawPos(), GetDrawRotation() );
976
977 aBuffer->Append( corner.x, corner.y );
978 }
979}
980
981
982bool EDA_TEXT::ValidateHyperlink( const wxString& aURL )
983{
984 if( aURL.IsEmpty() || IsGotoPageHref( aURL ) )
985 return true;
986
987 // Limit valid urls to file, http and https for now. Note wxURL doesn't support https
988 wxURI uri;
989
990 if( uri.Create( aURL ) && uri.HasScheme() )
991 {
992 const wxString& scheme = uri.GetScheme();
993 return scheme == wxT( "file" ) || scheme == wxT( "http" ) || scheme == wxT( "https" );
994 }
995
996 return false;
997}
998
999
1000bool EDA_TEXT::IsGotoPageHref( const wxString& aHref, wxString* aDestination )
1001{
1002 return aHref.StartsWith( wxT( "#" ), aDestination );
1003}
1004
1005
1006wxString EDA_TEXT::GotoPageHref( const wxString& aDestination )
1007{
1008 return wxT( "#" ) + aDestination;
1009}
1010
1011
1012static struct EDA_TEXT_DESC
1013{
1015 {
1017 .Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) )
1018 .Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) )
1019 .Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
1021 .Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) )
1022 .Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) )
1023 .Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
1024
1027
1028 propMgr.AddProperty( new PROPERTY<EDA_TEXT, double>( _HKI( "Orientation" ),
1031
1032 const wxString textProps = _( "Text Properties" );
1033
1034 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Text" ),
1037 textProps );
1038 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Hyperlink" ),
1041 textProps );
1042 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Thickness" ),
1046 textProps );
1047 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Italic" ),
1050 textProps );
1051 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Bold" ),
1053 textProps );
1054 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Mirrored" ),
1057 textProps );
1058 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Visible" ),
1061 textProps );
1062 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Width" ),
1066 textProps )
1068 TEXTS_MAX_SIZE> );
1069
1070 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Height" ),
1074 textProps )
1076 TEXTS_MAX_SIZE> );
1077
1078 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
1079 GR_TEXT_H_ALIGN_T>( _HKI( "Horizontal Justification" ),
1082 textProps );
1083 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
1084 GR_TEXT_V_ALIGN_T>( _HKI( "Vertical Justification" ),
1087 textProps );
1088 }
1090
void SetOrigin(const Vec &pos)
Definition: box2.h:202
BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:119
const Vec & GetOrigin() const
Definition: box2.h:183
void SetX(coord_type val)
Definition: box2.h:235
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:269
void SetY(coord_type val)
Definition: box2.h:240
coord_type GetHeight() const
Definition: box2.h:188
coord_type GetY() const
Definition: box2.h:181
coord_type GetWidth() const
Definition: box2.h:187
void SetSize(const Vec &size)
Definition: box2.h:213
bool Contains(const Vec &aPoint) const
Definition: box2.h:141
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
coord_type GetX() const
Definition: box2.h:180
coord_type GetRight() const
Definition: box2.h:189
coord_type GetBottom() const
Definition: box2.h:190
double AsDegrees() const
Definition: eda_angle.h:149
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:168
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:72
void TransformBoundingBoxToPolygon(SHAPE_POLY_SET *aBuffer, int aClearance) const
Convert the text bounding box to a rectangular polygon depending on the text orientation,...
Definition: eda_text.cpp:947
int GetTextHeight() const
Definition: eda_text.h:202
BOX2I GetTextBox(int aLine=-1, bool aInvertY=false) const
Useful in multiline texts to calculate the full text or a line area (for zones filling,...
Definition: eda_text.cpp:505
COLOR4D GetTextColor() const
Definition: eda_text.h:205
wxString GetTextStyleName() const
Definition: eda_text.cpp:738
VECTOR2I m_pos
Definition: eda_text.h:407
wxString m_text
Definition: eda_text.h:389
bool IsDefaultFormatting() const
Definition: eda_text.cpp:768
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
Definition: eda_text.cpp:1000
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_render_cache
Definition: eda_text.h:398
wxString GetFontName() const
Definition: eda_text.cpp:759
void SetupRenderCache(const wxString &aResolvedText, const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:485
virtual ~EDA_TEXT()
Definition: eda_text.cpp:133
bool IsItalic() const
Definition: eda_text.h:130
BOX2I m_bounding_box_cache
Definition: eda_text.h:404
bool IsMultilineAllowed() const
Definition: eda_text.h:146
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:87
void SetAttributes(const EDA_TEXT &aSrc)
Set the text attributes from another instance.
Definition: eda_text.cpp:266
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:499
virtual bool IsVisible() const
Definition: eda_text.h:136
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:373
void SetTextX(int aX)
Definition: eda_text.cpp:379
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:391
KIFONT::FONT * GetFont() const
Definition: eda_text.h:188
wxString m_shown_text
Definition: eda_text.h:390
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:783
void SetMirrored(bool isMirrored)
Definition: eda_text.cpp:226
void SetTextY(int aY)
Definition: eda_text.cpp:385
int m_bounding_box_cache_line
Definition: eda_text.h:402
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:453
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:317
virtual VECTOR2I GetDrawPos() const
Definition: eda_text.h:318
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition: eda_text.cpp:138
void printOneLineOfText(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset, const COLOR4D &aColor, OUTLINE_MODE aFillMode, const wxString &aText, const VECTOR2I &aPos)
Print each line of this EDA_TEXT.
Definition: eda_text.cpp:713
int GetTextWidth() const
Definition: eda_text.h:199
virtual bool HasHyperlink() const
Definition: eda_text.h:333
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:250
wxString GetHyperlink() const
Definition: eda_text.h:334
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:391
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:149
void SetTextWidth(int aWidth)
Definition: eda_text.cpp:357
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:320
int Compare(const EDA_TEXT *aOther) const
Definition: eda_text.cpp:913
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition: eda_text.h:393
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:219
EDA_TEXT(const EDA_IU_SCALE &aIuScale, const wxString &aText=wxEmptyString)
Definition: eda_text.cpp:92
bool m_bounding_box_cache_valid
Definition: eda_text.h:400
void GetLinePositions(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:673
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
Definition: eda_text.cpp:1006
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:446
virtual KIFONT::FONT * getDrawFont() const
Definition: eda_text.cpp:428
double GetLineSpacing() const
Definition: eda_text.h:193
VECTOR2I m_render_cache_offset
Definition: eda_text.h:397
void SetLineSpacing(double aLineSpacing)
Definition: eda_text.cpp:341
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition: eda_text.cpp:493
void Empty()
Definition: eda_text.cpp:402
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.cpp:187
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:625
void SetTextHeight(int aHeight)
Definition: eda_text.cpp:365
void cacheShownText()
Definition: eda_text.cpp:410
EDA_ANGLE m_render_cache_angle
Definition: eda_text.h:396
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:64
bool m_bounding_box_cache_inverted
Definition: eda_text.h:403
virtual void ClearRenderCache()
Definition: eda_text.cpp:440
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:172
void SetBold(bool aBold)
Definition: eda_text.cpp:211
static bool ValidateHyperlink(const wxString &aURL)
Check if aURL is a valid hyperlink.
Definition: eda_text.cpp:982
VECTOR2I m_bounding_box_cache_pos
Definition: eda_text.h:401
bool IsMirrored() const
Definition: eda_text.h:139
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:299
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition: eda_text.cpp:286
wxString m_render_cache_text
Definition: eda_text.h:395
double GetTextAngleDegrees() const
Definition: eda_text.h:127
bool IsBold() const
Definition: eda_text.h:133
void SetTextAngleDegrees(double aOrientation)
Definition: eda_text.h:123
std::shared_ptr< SHAPE_COMPOUND > GetEffectiveTextShape(bool aTriangulate=true, bool aUseTextRotation=true) const
build a list of segments (SHAPE_SEGMENT) to describe a text shape.
Definition: eda_text.cpp:855
void SetHyperlink(wxString aLink)
Definition: eda_text.h:335
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:78
void SetKeepUpright(bool aKeepUpright)
Definition: eda_text.cpp:258
void CopyText(const EDA_TEXT &aSrc)
Definition: eda_text.cpp:176
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:152
void SetTextSize(const VECTOR2I &aNewSize)
Definition: eda_text.cpp:349
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:165
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:195
int GetTextThickness() const
Definition: eda_text.h:112
virtual wxString GetShownText(int aDepth=0, bool aAllowExtraText=true) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:98
void Print(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset, const COLOR4D &aColor, OUTLINE_MODE aDisplay_mode=FILLED)
Print this text object to the device context aDC.
Definition: eda_text.cpp:650
void SetItalic(bool aItalic)
Definition: eda_text.cpp:203
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:275
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.cpp:234
void SetFont(KIFONT::FONT *aFont)
Definition: eda_text.cpp:333
VECTOR2I GetTextSize() const
Definition: eda_text.h:196
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:242
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:406
static ENUM_MAP< T > & Instance()
Definition: property.h:623
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
virtual double GetInterline(double aGlyphHeight, double aLineSpacing=1.0) const =0
Compute the distance (interline) between 2 lines of text (for multiline texts).
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:138
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttrs) const
Draw a string.
Definition: font.cpp:232
const wxString & GetName() const
Definition: font.h:121
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic) const
Compute the boundary limits of aText (the bounding box of all shapes).
Definition: font.cpp:357
Class OUTLINE_FONT implements outline font drawing.
Definition: outline_font.h:52
void GetLinesAsGlyphs(std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aPosition, const TEXT_ATTRIBUTES &aAttrs) const
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:102
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
int GetDefaultPenWidth() const
const wxString & GetDefaultFont() const
wxDC * GetPrintDC() const
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:501
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:433
PROPERTY_BASE & SetValidator(PROPERTY_VALIDATOR_FN &&aValidator)
Definition: property.h:307
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
static VALIDATOR_RESULT RangeIntValidator(const wxAny &&aValue, EDA_ITEM *aItem)
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.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Add a new vertex to the contour indexed by aOutline and aHole (defaults to the outline of the last po...
int NewOutline()
Creates a new hole in a given outline.
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
Definition: shape_simple.h:42
void Append(int aX, int aY)
Append a new point at the end of the polygon.
Definition: shape_simple.h:135
bool m_KeepUpright
If true, keep rotation angle between -90...90 degrees for readability.
GR_TEXT_H_ALIGN_T m_Halign
GR_TEXT_V_ALIGN_T m_Valign
KIFONT::FONT * m_Font
#define _HKI(x)
#define _(s)
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:429
#define TEST_PT(a, b)
#define TEST(a, b)
#define TEST_E(a, b)
static struct EDA_TEXT_DESC _EDA_TEXT_DESC
#define CTL_OMIT_HIDE
Definition: eda_text.h:53
#define DEFAULT_SIZE_TEXT
This is the "default-of-the-default" hardcoded text size; individual application define their own def...
Definition: eda_text.h:61
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:60
int GetPenSizeForBold(int aTextSize)
Definition: gr_text.cpp:40
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)
Print a graphic text through wxDC.
Definition: gr_text.cpp:141
int GetPenSizeForNormal(int aTextSize)
Definition: gr_text.cpp:64
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aStrict)
Pen width should not allow characters to become cluttered up in their own fatness.
Definition: gr_text.cpp:87
Some functions to handle hotkeys in KiCad.
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition: eda_units.h:122
std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue)
Converts aValue from internal units to a string appropriate for writing to file.
Definition: eda_units.cpp:142
OUTLINE_MODE
Definition: outline_mode.h:25
@ SKETCH
Definition: outline_mode.h:26
#define TEXTS_MAX_SIZE
Maximum text size in internal units (10 inches)
Definition: pcbnew.h:32
#define TEXTS_MIN_SIZE
Minimum text size in internal units (1 mil)
Definition: pcbnew.h:31
#define ENUM_TO_WXANY(type)
Macro to define read-only fields (no setter method available)
Definition: property.h:725
@ PT_DEGREE
Angle expressed in degrees.
Definition: property.h:58
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition: property.h:56
#define REGISTER_TYPE(x)
Definition: property_mgr.h:328
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...
GR_TEXT_H_ALIGN_T
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
GR_TEXT_V_ALIGN_T
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590