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-2022 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>
53
54#include <wx/debug.h> // for wxASSERT
55#include <wx/string.h>
56#include <wx/url.h> // for wxURL
57
58class OUTPUTFORMATTER;
59class wxFindReplaceData;
60
61
63{
64 wxASSERT( aHorizJustify >= GR_TEXT_H_ALIGN_LEFT && aHorizJustify <= GR_TEXT_H_ALIGN_RIGHT );
65
66 if( aHorizJustify > GR_TEXT_H_ALIGN_RIGHT )
68
69 if( aHorizJustify < GR_TEXT_H_ALIGN_LEFT )
71
72 return static_cast<GR_TEXT_H_ALIGN_T>( aHorizJustify );
73}
74
75
77{
78 wxASSERT( aVertJustify >= GR_TEXT_V_ALIGN_TOP && aVertJustify <= GR_TEXT_V_ALIGN_BOTTOM );
79
80 if( aVertJustify > GR_TEXT_V_ALIGN_BOTTOM )
82
83 if( aVertJustify < GR_TEXT_V_ALIGN_TOP )
85
86 return static_cast<GR_TEXT_V_ALIGN_T>( aVertJustify );
87}
88
89
90EDA_TEXT::EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText ) :
91 m_text( aText ),
92 m_IuScale( aIuScale ),
93 m_bounding_box_cache_valid( false ),
94 m_bounding_box_cache_line( -1 ),
95 m_bounding_box_cache_inverted( false )
96{
100}
101
102
104 m_IuScale( aText.m_IuScale )
105{
106 m_text = aText.m_text;
109
111 m_pos = aText.m_pos;
112
115
116 m_render_cache.clear();
117
118 for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aText.m_render_cache )
119 {
120 KIFONT::OUTLINE_GLYPH* outline_glyph = static_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() );
121 m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline_glyph ) );
122 }
123
128}
129
130
132{
133}
134
135
137{
138 m_text = aText.m_text;
141
143 m_pos = aText.m_pos;
144
147
148 m_render_cache.clear();
149
150 for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aText.m_render_cache )
151 {
152 KIFONT::OUTLINE_GLYPH* outline_glyph = static_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() );
153 m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline_glyph ) );
154 }
155
158
159 return *this;
160}
161
162
163void EDA_TEXT::SetText( const wxString& aText )
164{
165 m_text = aText;
166
168
171}
172
173
174void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
175{
176 m_text = aSrc.m_text;
179
182}
183
184
186{
190}
191
192
194{
195 m_attributes.m_Angle = aAngle;
198}
199
200
201void EDA_TEXT::SetItalic( bool aItalic )
202{
203 m_attributes.m_Italic = aItalic;
206}
207
208
209void EDA_TEXT::SetBold( bool aBold )
210{
211 m_attributes.m_Bold = aBold;
214}
215
216
217void EDA_TEXT::SetVisible( bool aVisible )
218{
219 m_attributes.m_Visible = aVisible;
221}
222
223
224void EDA_TEXT::SetMirrored( bool isMirrored )
225{
226 m_attributes.m_Mirrored = isMirrored;
229}
230
231
233{
234 m_attributes.m_Multiline = aAllow;
237}
238
239
241{
242 m_attributes.m_Halign = aType;
245}
246
247
249{
250 m_attributes.m_Valign = aType;
253}
254
255
256void EDA_TEXT::SetKeepUpright( bool aKeepUpright )
257{
258 m_attributes.m_KeepUpright = aKeepUpright;
261}
262
263
265{
267 m_pos = aSrc.m_pos;
270}
271
272
273void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
274{
275 std::swap( m_text, aTradingPartner.m_text );
276 std::swap( m_shown_text, aTradingPartner.m_shown_text );
278
281}
282
283
284void EDA_TEXT::SwapAttributes( EDA_TEXT& aTradingPartner )
285{
286 std::swap( m_attributes, aTradingPartner.m_attributes );
287 std::swap( m_pos, aTradingPartner.m_pos );
288
290 aTradingPartner.ClearRenderCache();
291
293 aTradingPartner.m_bounding_box_cache_valid = false;
294}
295
296
297int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultPenWidth ) const
298{
299 int penWidth = GetTextThickness();
300
301 if( penWidth <= 1 )
302 {
303 penWidth = aDefaultPenWidth;
304
305 if( IsBold() )
306 penWidth = GetPenSizeForBold( GetTextWidth() );
307 else if( penWidth <= 1 )
308 penWidth = GetPenSizeForNormal( GetTextWidth() );
309 }
310
311 // Clip pen size for small texts:
312 penWidth = Clamp_Text_PenSize( penWidth, GetTextSize() );
313
314 return penWidth;
315}
316
317
318bool EDA_TEXT::Replace( const EDA_SEARCH_DATA& aSearchData )
319{
320 bool retval = EDA_ITEM::Replace( aSearchData, m_text );
321
323
326
327 return retval;
328}
329
330
332{
333 m_attributes.m_Font = aFont;
336}
337
338
339void EDA_TEXT::SetLineSpacing( double aLineSpacing )
340{
341 m_attributes.m_LineSpacing = aLineSpacing;
344}
345
346
347void EDA_TEXT::SetTextSize( const VECTOR2I& aNewSize )
348{
349 m_attributes.m_Size = aNewSize;
352}
353
354
355void EDA_TEXT::SetTextWidth( int aWidth )
356{
357 m_attributes.m_Size.x = aWidth;
360}
361
362
363void EDA_TEXT::SetTextHeight( int aHeight )
364{
365 m_attributes.m_Size.y = aHeight;
368}
369
370
371void EDA_TEXT::SetTextPos( const VECTOR2I& aPoint )
372{
373 Offset( VECTOR2I( aPoint.x - m_pos.x, aPoint.y - m_pos.y ) );
374}
375
376
377void EDA_TEXT::SetTextX( int aX )
378{
379 Offset( VECTOR2I( aX - m_pos.x, 0 ) );
380}
381
382
383void EDA_TEXT::SetTextY( int aY )
384{
385 Offset( VECTOR2I( 0, aY - m_pos.y ) );
386}
387
388
389void EDA_TEXT::Offset( const VECTOR2I& aOffset )
390{
391 m_pos += aOffset;
392
393 for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_render_cache )
394 static_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() )->Move( aOffset );
395
397}
398
399
401{
402 m_text.Empty();
405}
406
407
409{
410 if( m_text.IsEmpty() )
411 {
412 m_shown_text = wxEmptyString;
414 }
415 else
416 {
418 m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
419 }
420
423}
424
425
427{
428 KIFONT::FONT* font = GetFont();
429
430 if( !font )
431 font = KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() );
432
433 return font;
434}
435
436
437
439{
440 m_render_cache.clear();
441}
442
443
445{
447}
448
449
450std::vector<std::unique_ptr<KIFONT::GLYPH>>*
451EDA_TEXT::GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
452 const VECTOR2I& aOffset ) const
453{
454 if( getDrawFont()->IsOutline() )
455 {
456 EDA_ANGLE resolvedAngle = GetDrawRotation();
457
458 if( m_render_cache.empty()
459 || m_render_cache_text != forResolvedText
460 || m_render_cache_angle != resolvedAngle
461 || m_render_cache_offset != aOffset )
462 {
463 m_render_cache.clear();
464
465 KIFONT::OUTLINE_FONT* font = static_cast<KIFONT::OUTLINE_FONT*>( getDrawFont() );
467
468 attrs.m_Angle = resolvedAngle;
469
470 font->GetLinesAsGlyphs( &m_render_cache, GetShownText(), GetDrawPos() + aOffset, attrs );
471 m_render_cache_angle = resolvedAngle;
472 m_render_cache_text = forResolvedText;
473 m_render_cache_offset = aOffset;
474 }
475
476 return &m_render_cache;
477 }
478
479 return nullptr;
480}
481
482
483void EDA_TEXT::SetupRenderCache( const wxString& aResolvedText, const EDA_ANGLE& aAngle )
484{
485 m_render_cache_text = aResolvedText;
486 m_render_cache_angle = aAngle;
487 m_render_cache.clear();
488}
489
490
492{
493 m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( aPoly ) );
494}
495
496
498{
500}
501
502
503BOX2I EDA_TEXT::GetTextBox( int aLine, bool aInvertY ) const
504{
505 VECTOR2I drawPos = GetDrawPos();
506
508 && m_bounding_box_cache_pos == drawPos
509 && m_bounding_box_cache_line == aLine
510 && m_bounding_box_cache_inverted == aInvertY )
511 {
513 }
514
515 BOX2I bbox;
516 wxArrayString strings;
517 wxString text = GetShownText();
518 int thickness = GetEffectiveTextPenWidth();
519
520 if( IsMultilineAllowed() )
521 {
522 wxStringSplit( text, strings, '\n' );
523
524 if( strings.GetCount() ) // GetCount() == 0 for void strings with multilines allowed
525 {
526 if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
527 text = strings.Item( aLine );
528 else
529 text = strings.Item( 0 );
530 }
531 }
532
533 // calculate the H and V size
534 KIFONT::FONT* font = getDrawFont();
535 VECTOR2D fontSize( GetTextSize() );
536 bool bold = IsBold();
537 bool italic = IsItalic();
538 VECTOR2I extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic );
539 int overbarOffset = 0;
540
541 // Creates bounding box (rectangle) for horizontal, left and top justified text. The
542 // bounding box will be moved later according to the actual text options
543 wxSize textsize = wxSize( extents.x, extents.y );
544 VECTOR2I pos = drawPos;
545
546 if( IsMultilineAllowed() && aLine > 0 && aLine < (int) strings.GetCount() )
547 pos.y -= KiROUND( aLine * font->GetInterline( fontSize.y ) );
548
549 if( text.Contains( wxT( "~{" ) ) )
550 overbarOffset = extents.y / 14;
551
552 if( aInvertY )
553 pos.y = -pos.y;
554
555 bbox.SetOrigin( pos );
556
557 // for multiline texts and aLine < 0, merge all rectangles (aLine == -1 signals all lines)
558 if( IsMultilineAllowed() && aLine < 0 && strings.GetCount() )
559 {
560 for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
561 {
562 text = strings.Item( ii );
563 extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic );
564 textsize.x = std::max( textsize.x, extents.x );
565 }
566
567 // interline spacing is only *between* lines, so total height is the height of the first
568 // line plus the interline distance (with interline spacing) for all subsequent lines
569 textsize.y += KiROUND( ( strings.GetCount() - 1 ) * font->GetInterline( fontSize.y ) );
570 }
571
572 bbox.SetSize( textsize );
573
574 /*
575 * At this point the rectangle origin is the text origin (m_Pos). This is correct only for
576 * left and top justified, non-mirrored, non-overbarred texts. Recalculate for all others.
577 */
578 int italicOffset = IsItalic() ? fontSize.y * ITALIC_TILT : 0;
579
580 switch( GetHorizJustify() )
581 {
583 if( IsMirrored() )
584 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
585 break;
586
588 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) / 2 );
589 break;
590
592 if( !IsMirrored() )
593 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
594 break;
595 }
596
597 switch( GetVertJustify() )
598 {
600 break;
601
603 bbox.SetY( bbox.GetY() - ( bbox.GetHeight() + overbarOffset ) / 2 );
604 break;
605
607 bbox.SetY( bbox.GetY() - ( bbox.GetHeight() + overbarOffset ) );
608 break;
609 }
610
611 bbox.Normalize(); // Make h and v sizes always >= 0
612
614 m_bounding_box_cache_pos = drawPos;
618
619 return bbox;
620}
621
622
623bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
624{
625 BOX2I rect = GetTextBox();
626 VECTOR2I location = aPoint;
627
628 rect.Inflate( aAccuracy );
629 RotatePoint( location, GetDrawPos(), -GetDrawRotation() );
630
631 return rect.Contains( location );
632}
633
634
635bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
636{
637 BOX2I rect = aRect;
638
639 rect.Inflate( aAccuracy );
640
641 if( aContains )
642 return rect.Contains( GetTextBox() );
643
644 return rect.Intersects( GetTextBox(), GetDrawRotation() );
645}
646
647
648void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
649 const COLOR4D& aColor, OUTLINE_MODE aFillMode )
650{
651 if( IsMultilineAllowed() )
652 {
653 std::vector<VECTOR2I> positions;
654 wxArrayString strings;
655 wxStringSplit( GetShownText(), strings, '\n' );
656
657 positions.reserve( strings.Count() );
658
659 GetLinePositions( positions, strings.Count() );
660
661 for( unsigned ii = 0; ii < strings.Count(); ii++ )
662 printOneLineOfText( aSettings, aOffset, aColor, aFillMode, strings[ii], positions[ii] );
663 }
664 else
665 {
666 printOneLineOfText( aSettings, aOffset, aColor, aFillMode, GetShownText(), GetDrawPos() );
667 }
668}
669
670
671void EDA_TEXT::GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const
672{
673 VECTOR2I pos = GetDrawPos(); // Position of first line of the multiline text according
674 // to the center of the multiline text block
675
676 VECTOR2I offset; // Offset to next line.
677
678 offset.y = GetInterline();
679
680 if( aLineCount > 1 )
681 {
682 switch( GetVertJustify() )
683 {
685 break;
686
688 pos.y -= ( aLineCount - 1 ) * offset.y / 2;
689 break;
690
692 pos.y -= ( aLineCount - 1 ) * offset.y;
693 break;
694 }
695 }
696
697 // Rotate the position of the first line around the center of the multiline text block
699
700 // Rotate the offset lines to increase happened in the right direction
701 RotatePoint( offset, GetDrawRotation() );
702
703 for( int ii = 0; ii < aLineCount; ii++ )
704 {
705 aPositions.push_back( (VECTOR2I) pos );
706 pos += offset;
707 }
708}
709
710
711void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
712 const COLOR4D& aColor, OUTLINE_MODE aFillMode,
713 const wxString& aText, const VECTOR2I& aPos )
714{
715 wxDC* DC = aSettings->GetPrintDC();
716 int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
717
718 if( aFillMode == SKETCH )
719 penWidth = -penWidth;
720
721 VECTOR2I size = GetTextSize();
722
723 if( IsMirrored() )
724 size.x = -size.x;
725
726 KIFONT::FONT* font = GetFont();
727
728 if( !font )
729 font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
730
731 GRPrintText( DC, aOffset + aPos, aColor, aText, GetDrawRotation(), size, GetHorizJustify(),
732 GetVertJustify(), penWidth, IsItalic(), IsBold(), font );
733}
734
735
737{
738 int style = 0;
739
740 if( IsItalic() )
741 style = 1;
742
743 if( IsBold() )
744 style += 2;
745
746 wxString stylemsg[4] = {
747 _("Normal"),
748 _("Italic"),
749 _("Bold"),
750 _("Bold+Italic")
751 };
752
753 return stylemsg[style];
754}
755
756
757wxString EDA_TEXT::GetFontName() const
758{
759 if( GetFont() )
760 return GetFont()->GetName();
761 else
762 return wxEmptyString;
763}
764
765
767{
768 return ( IsVisible()
769 && !IsMirrored()
772 && GetTextThickness() == 0
773 && !IsItalic()
774 && !IsBold()
776 && GetFontName().IsEmpty()
777 );
778}
779
780
781void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
782{
783 aFormatter->Print( aNestLevel + 1, "(effects" );
784
785 aFormatter->Print( 0, " (font" );
786
787 if( GetFont() && !GetFont()->GetName().IsEmpty() )
788 aFormatter->Print( 0, " (face \"%s\")", GetFont()->NameAsToken() );
789
790 // Text size
791 aFormatter->Print( 0, " (size %s %s)",
794
795 if( GetLineSpacing() != 1.0 )
796 {
797 aFormatter->Print( 0, " (line_spacing %s)",
798 FormatDouble2Str( GetLineSpacing() ).c_str() );
799 }
800
801 if( GetTextThickness() )
802 {
803 aFormatter->Print( 0, " (thickness %s)",
805 }
806
807 if( IsBold() )
808 aFormatter->Print( 0, " bold" );
809
810 if( IsItalic() )
811 aFormatter->Print( 0, " italic" );
812
813 if( GetTextColor() != COLOR4D::UNSPECIFIED )
814 {
815 aFormatter->Print( 0, " (color %d %d %d %s)",
816 KiROUND( GetTextColor().r * 255.0 ),
817 KiROUND( GetTextColor().g * 255.0 ),
818 KiROUND( GetTextColor().b * 255.0 ),
819 FormatDouble2Str( GetTextColor().a ).c_str() );
820 }
821
822 aFormatter->Print( 0, ")"); // (font
823
826 {
827 aFormatter->Print( 0, " (justify");
828
830 aFormatter->Print( 0, GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
831
833 aFormatter->Print( 0, GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
834
835 if( IsMirrored() )
836 aFormatter->Print( 0, " mirror" );
837
838 aFormatter->Print( 0, ")" ); // (justify
839 }
840
841 if( !( aControlBits & CTL_OMIT_HIDE ) && !IsVisible() )
842 aFormatter->Print( 0, " hide" );
843
844 if( HasHyperlink() )
845 {
846 aFormatter->Print( 0, " (href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
847 }
848
849 aFormatter->Print( 0, ")\n" ); // (effects
850}
851
852
853std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate,
854 bool aUseTextRotation ) const
855{
856 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
858 KIFONT::FONT* font = getDrawFont();
859 int penWidth = GetEffectiveTextPenWidth();
861
862 if( aUseTextRotation )
863 attrs.m_Angle = GetDrawRotation();
864 else
865 attrs.m_Angle = ANGLE_0;
866
867 if( aTriangulate )
868 {
869 CALLBACK_GAL callback_gal(
870 empty_opts,
871 // Stroke callback
872 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
873 {
874 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
875 },
876 // Triangulation callback
877 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
878 {
879 SHAPE_SIMPLE* triShape = new SHAPE_SIMPLE;
880
881 for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
882 triShape->Append( point.x, point.y );
883
884 shape->AddShape( triShape );
885 } );
886
887 font->Draw( &callback_gal, GetShownText(), GetDrawPos(), attrs );
888 }
889 else
890 {
891 CALLBACK_GAL callback_gal(
892 empty_opts,
893 // Stroke callback
894 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
895 {
896 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
897 },
898 // Outline callback
899 [&]( const SHAPE_LINE_CHAIN& aPoly )
900 {
901 shape->AddShape( aPoly.Clone() );
902 } );
903
904 font->Draw( &callback_gal, GetShownText(), GetDrawPos(), attrs );
905 }
906
907 return shape;
908}
909
910
911int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
912{
913#define EPSILON 2 // Should be enough for rounding errors on calculated items
914
915#define TEST( a, b ) { if( a != b ) return a - b; }
916#define TEST_E( a, b ) { if( abs( a - b ) > EPSILON ) return a - b; }
917#define TEST_PT( a, b ) { TEST_E( a.x, b.x ); TEST_E( a.y, b.y ); }
918
919 TEST_PT( m_pos, aOther->m_pos );
920
925
935
936 int val = GetFontName().Cmp( aOther->GetFontName() );
937
938 if( val != 0 )
939 return val;
940
941 return m_text.Cmp( aOther->m_text );
942}
943
944
945void EDA_TEXT::TransformBoundingBoxToPolygon( SHAPE_POLY_SET* aBuffer, int aClearance ) const
946{
947 if( GetText().Length() == 0 )
948 return;
949
950 VECTOR2I corners[4]; // Buffer of polygon corners
951 BOX2I rect = GetTextBox();
952
953 // TrueType bounding boxes aren't guaranteed to include all descenders, diacriticals, etc.
954 // Since we use this for zone knockouts and DRC, we need something more accurate.
955 if( getDrawFont()->IsOutline() )
956 rect = GetEffectiveTextShape( false, false )->BBox();
957
958 rect.Inflate( aClearance );
959
960 corners[0].x = rect.GetOrigin().x;
961 corners[0].y = rect.GetOrigin().y;
962 corners[1].y = corners[0].y;
963 corners[1].x = rect.GetRight();
964 corners[2].x = corners[1].x;
965 corners[2].y = rect.GetBottom();
966 corners[3].y = corners[2].y;
967 corners[3].x = corners[0].x;
968
969 aBuffer->NewOutline();
970
971 for( VECTOR2I& corner : corners )
972 {
973 RotatePoint( corner, GetDrawPos(), GetDrawRotation() );
974
975 aBuffer->Append( corner.x, corner.y );
976 }
977}
978
979
980bool EDA_TEXT::ValidateHyperlink( const wxString& aURL )
981{
982 if( aURL.IsEmpty() || IsGotoPageHref( aURL ) )
983 return true;
984
985 // Limit valid urls to file, http and https for now. Note wxURL doesn't support https
986 wxURI uri;
987
988 if( uri.Create( aURL ) && uri.HasScheme() )
989 {
990 wxString scheme = uri.GetScheme();
991 return scheme == wxT( "file" ) || scheme == wxT( "http" ) || scheme == wxT( "https" );
992 }
993
994 return false;
995}
996
997
998bool EDA_TEXT::IsGotoPageHref( const wxString& aHref, wxString* aDestination )
999{
1000 return aHref.StartsWith( wxT( "#" ), aDestination );
1001}
1002
1003
1004wxString EDA_TEXT::GotoPageHref( const wxString& aDestination )
1005{
1006 return wxT( "#" ) + aDestination;
1007}
1008
1009
1010static struct EDA_TEXT_DESC
1011{
1013 {
1015 .Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) )
1016 .Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) )
1017 .Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
1019 .Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) )
1020 .Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) )
1021 .Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
1022
1025
1026 const wxString textProps = _( "Text Properties" );
1027
1028 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Text" ),
1031 textProps );
1032 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Hyperlink" ),
1035 textProps );
1036 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Thickness" ),
1040 textProps );
1041 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Italic" ),
1044 textProps );
1045 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Bold" ),
1047 textProps );
1048 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Mirrored" ),
1051 textProps );
1052 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Visible" ),
1055 textProps );
1056 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Width" ),
1060 textProps );
1061 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Height" ),
1065 textProps );
1066 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
1067 GR_TEXT_H_ALIGN_T>( _HKI( "Horizontal Justification" ),
1070 textProps );
1071 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
1072 GR_TEXT_V_ALIGN_T>( _HKI( "Vertical Justification" ),
1075 textProps );
1076 }
1078
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:945
int GetTextHeight() const
Definition: eda_text.h:195
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:503
COLOR4D GetTextColor() const
Definition: eda_text.h:198
wxString GetTextStyleName() const
Definition: eda_text.cpp:736
VECTOR2I m_pos
Definition: eda_text.h:400
wxString m_text
Definition: eda_text.h:382
bool IsDefaultFormatting() const
Definition: eda_text.cpp:766
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
Definition: eda_text.cpp:998
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_render_cache
Definition: eda_text.h:391
wxString GetFontName() const
Definition: eda_text.cpp:757
void SetupRenderCache(const wxString &aResolvedText, const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:483
virtual ~EDA_TEXT()
Definition: eda_text.cpp:131
bool IsItalic() const
Definition: eda_text.h:123
BOX2I m_bounding_box_cache
Definition: eda_text.h:397
bool IsMultilineAllowed() const
Definition: eda_text.h:139
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:264
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:497
virtual bool IsVisible() const
Definition: eda_text.h:129
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:371
void SetTextX(int aX)
Definition: eda_text.cpp:377
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:384
KIFONT::FONT * GetFont() const
Definition: eda_text.h:181
wxString m_shown_text
Definition: eda_text.h:383
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:781
void SetMirrored(bool isMirrored)
Definition: eda_text.cpp:224
void SetTextY(int aY)
Definition: eda_text.cpp:383
int m_bounding_box_cache_line
Definition: eda_text.h:395
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:451
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:310
virtual VECTOR2I GetDrawPos() const
Definition: eda_text.h:311
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition: eda_text.cpp:136
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:711
int GetTextWidth() const
Definition: eda_text.h:192
virtual bool HasHyperlink() const
Definition: eda_text.h:326
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:248
wxString GetHyperlink() const
Definition: eda_text.h:327
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:389
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:142
void SetTextWidth(int aWidth)
Definition: eda_text.cpp:355
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:318
int Compare(const EDA_TEXT *aOther) const
Definition: eda_text.cpp:911
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition: eda_text.h:386
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:217
EDA_TEXT(const EDA_IU_SCALE &aIuScale, const wxString &aText=wxEmptyString)
Definition: eda_text.cpp:90
bool m_bounding_box_cache_valid
Definition: eda_text.h:393
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:671
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
Definition: eda_text.cpp:1004
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:444
virtual KIFONT::FONT * getDrawFont() const
Definition: eda_text.cpp:426
double GetLineSpacing() const
Definition: eda_text.h:186
VECTOR2I m_render_cache_offset
Definition: eda_text.h:390
void SetLineSpacing(double aLineSpacing)
Definition: eda_text.cpp:339
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition: eda_text.cpp:491
void Empty()
Definition: eda_text.cpp:400
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.cpp:185
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:623
void SetTextHeight(int aHeight)
Definition: eda_text.cpp:363
void cacheShownText()
Definition: eda_text.cpp:408
EDA_ANGLE m_render_cache_angle
Definition: eda_text.h:389
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:62
bool m_bounding_box_cache_inverted
Definition: eda_text.h:396
virtual void ClearRenderCache()
Definition: eda_text.cpp:438
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:165
void SetBold(bool aBold)
Definition: eda_text.cpp:209
static bool ValidateHyperlink(const wxString &aURL)
Check if aURL is a valid hyperlink.
Definition: eda_text.cpp:980
VECTOR2I m_bounding_box_cache_pos
Definition: eda_text.h:394
bool IsMirrored() const
Definition: eda_text.h:132
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:297
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition: eda_text.cpp:284
wxString m_render_cache_text
Definition: eda_text.h:388
bool IsBold() const
Definition: eda_text.h:126
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:853
void SetHyperlink(wxString aLink)
Definition: eda_text.h:328
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:76
void SetKeepUpright(bool aKeepUpright)
Definition: eda_text.cpp:256
void CopyText(const EDA_TEXT &aSrc)
Definition: eda_text.cpp:174
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:145
void SetTextSize(const VECTOR2I &aNewSize)
Definition: eda_text.cpp:347
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:163
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:193
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:648
void SetItalic(bool aItalic)
Definition: eda_text.cpp:201
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:273
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.cpp:232
void SetFont(KIFONT::FONT *aFont)
Definition: eda_text.cpp:331
VECTOR2I GetTextSize() const
Definition: eda_text.h:189
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:240
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:399
static ENUM_MAP< T > & Instance()
Definition: property.h:546
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:65
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:159
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:271
Class OUTLINE_FONT implements outline font drawing.
Definition: outline_font.h:50
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:104
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:499
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:431
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:67
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:69
void 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.
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:412
#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
E_SERIE r
Definition: eserie.cpp:41
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:129
int GetPenSizeForNormal(int aTextSize)
Definition: gr_text.cpp:52
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:75
Some functions to handle hotkeys in KiCad.
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition: eda_units.h:123
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:139
OUTLINE_MODE
Definition: outline_mode.h:25
@ SKETCH
Definition: outline_mode.h:26
#define ENUM_TO_WXANY(type)
Macro to define read-only fields (no setter method available)
Definition: property.h:648
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition: property.h:54
#define REGISTER_TYPE(x)
Definition: property_mgr.h:292
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:618