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 (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
25#include <algorithm> // for max
26#include <stddef.h> // for NULL
27#include <type_traits> // for swap
28#include <vector>
29
30#include <eda_item.h>
31#include <base_units.h>
32#include <callback_gal.h>
33#include <eda_text.h> // for EDA_TEXT, TEXT_EFFECTS, GR_TEXT_VJUSTIF...
34#include <gal/color4d.h> // for COLOR4D, COLOR4D::BLACK
35#include <font/glyph.h>
36#include <gr_text.h>
37#include <string_utils.h> // for UnescapeString
38#include <math/util.h> // for KiROUND
39#include <math/vector2d.h>
40#include <core/kicad_algo.h>
41#include <richio.h>
42#include <render_settings.h>
43#include <trigo.h> // for RotatePoint
44#include <i18n_utility.h>
48#include <font/outline_font.h>
51
52#include <wx/debug.h> // for wxASSERT
53#include <wx/string.h>
54#include <wx/url.h> // for wxURL
55
56class OUTPUTFORMATTER;
57class wxFindReplaceData;
58
59
61{
62 wxASSERT( aHorizJustify >= GR_TEXT_H_ALIGN_LEFT && aHorizJustify <= GR_TEXT_H_ALIGN_RIGHT );
63
64 if( aHorizJustify > GR_TEXT_H_ALIGN_RIGHT )
66
67 if( aHorizJustify < GR_TEXT_H_ALIGN_LEFT )
69
70 return static_cast<GR_TEXT_H_ALIGN_T>( aHorizJustify );
71}
72
73
75{
76 wxASSERT( aVertJustify >= GR_TEXT_V_ALIGN_TOP && aVertJustify <= GR_TEXT_V_ALIGN_BOTTOM );
77
78 if( aVertJustify > GR_TEXT_V_ALIGN_BOTTOM )
80
81 if( aVertJustify < GR_TEXT_V_ALIGN_TOP )
83
84 return static_cast<GR_TEXT_V_ALIGN_T>( aVertJustify );
85}
86
87
88EDA_TEXT::EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText ) :
89 m_text( aText ),
90 m_IuScale( aIuScale ),
91 m_render_cache_font( nullptr ),
92 m_bounding_box_cache_valid( false ),
93 m_bounding_box_cache_line( -1 ),
94 m_bounding_box_cache_inverted( false )
95{
98
99 if( m_text.IsEmpty() )
100 {
101 m_shown_text = wxEmptyString;
103 }
104 else
105 {
107 m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
108 }
109}
110
111
113 m_IuScale( aText.m_IuScale )
114{
115 m_text = aText.m_text;
118
120 m_pos = aText.m_pos;
121
126
127 m_render_cache.clear();
128
129 for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aText.m_render_cache )
130 {
131 if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
132 m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline ) );
133 else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
134 m_render_cache.emplace_back( std::make_unique<KIFONT::STROKE_GLYPH>( *stroke ) );
135 }
136
141}
142
143
145{
146}
147
148
150{
151 m_text = aText.m_text;
154
156 m_pos = aText.m_pos;
157
162
163 m_render_cache.clear();
164
165 for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aText.m_render_cache )
166 {
167 if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
168 m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline ) );
169 else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
170 m_render_cache.emplace_back( std::make_unique<KIFONT::STROKE_GLYPH>( *stroke ) );
171 }
172
175
176 return *this;
177}
178
179
180void EDA_TEXT::SetText( const wxString& aText )
181{
182 m_text = aText;
184}
185
186
187void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
188{
189 m_text = aSrc.m_text;
191}
192
193
195{
199}
200
201
203{
204 m_attributes.m_Angle = aAngle;
207}
208
209
210void EDA_TEXT::SetItalic( bool aItalic )
211{
212 m_attributes.m_Italic = aItalic;
215}
216
217
218void EDA_TEXT::SetBold( bool aBold )
219{
220 m_attributes.m_Bold = aBold;
223}
224
225
226void EDA_TEXT::SetVisible( bool aVisible )
227{
228 m_attributes.m_Visible = aVisible;
230}
231
232
233void EDA_TEXT::SetMirrored( bool isMirrored )
234{
235 m_attributes.m_Mirrored = isMirrored;
238}
239
240
242{
243 m_attributes.m_Multiline = aAllow;
246}
247
248
250{
251 m_attributes.m_Halign = aType;
254}
255
256
258{
259 m_attributes.m_Valign = aType;
262}
263
264
265void EDA_TEXT::SetKeepUpright( bool aKeepUpright )
266{
267 m_attributes.m_KeepUpright = aKeepUpright;
270}
271
272
273void EDA_TEXT::SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition )
274{
276
277 if( aSetPosition )
278 m_pos = aSrc.m_pos;
279
282}
283
284
285void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
286{
287 std::swap( m_text, aTradingPartner.m_text );
289}
290
291
292void EDA_TEXT::SwapAttributes( EDA_TEXT& aTradingPartner )
293{
294 std::swap( m_attributes, aTradingPartner.m_attributes );
295 std::swap( m_pos, aTradingPartner.m_pos );
296
298 aTradingPartner.ClearRenderCache();
299
301 aTradingPartner.m_bounding_box_cache_valid = false;
302}
303
304
305int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultPenWidth ) const
306{
307 int penWidth = GetTextThickness();
308
309 if( penWidth <= 1 )
310 {
311 penWidth = aDefaultPenWidth;
312
313 if( IsBold() )
314 penWidth = GetPenSizeForBold( GetTextWidth() );
315 else if( penWidth <= 1 )
316 penWidth = GetPenSizeForNormal( GetTextWidth() );
317 }
318
319 // Clip pen size for small texts:
320 penWidth = Clamp_Text_PenSize( penWidth, GetTextSize() );
321
322 return penWidth;
323}
324
325
326bool EDA_TEXT::Replace( const EDA_SEARCH_DATA& aSearchData )
327{
328 bool retval = EDA_ITEM::Replace( aSearchData, m_text );
329
331
334
335 return retval;
336}
337
338
340{
341 m_attributes.m_Font = aFont;
344}
345
346
347void EDA_TEXT::SetLineSpacing( double aLineSpacing )
348{
349 m_attributes.m_LineSpacing = aLineSpacing;
352}
353
354
356{
357 if( m_IuScale.get().IU_PER_MM != unityScale.IU_PER_MM )
358 {
359 // Plotting uses unityScale and independently scales the text. If we clamp here we'll
360 // clamp to *really* small values.
361
362 int min = m_IuScale.get().MilsToIU( TEXT_MIN_SIZE_MILS );
363 int max = m_IuScale.get().MilsToIU( TEXT_MAX_SIZE_MILS );
364
365 aNewSize = VECTOR2I( alg::clamp( min, aNewSize.x, max ),
366 alg::clamp( min, aNewSize.y, max ) );
367 }
368
369 m_attributes.m_Size = aNewSize;
370
373}
374
375
376void EDA_TEXT::SetTextWidth( int aWidth )
377{
378 int min = m_IuScale.get().MilsToIU( TEXT_MIN_SIZE_MILS );
379 int max = m_IuScale.get().MilsToIU( TEXT_MAX_SIZE_MILS );
380
381 m_attributes.m_Size.x = alg::clamp( min, aWidth, max );
384}
385
386
387void EDA_TEXT::SetTextHeight( int aHeight )
388{
389 int min = m_IuScale.get().MilsToIU( TEXT_MIN_SIZE_MILS );
390 int max = m_IuScale.get().MilsToIU( TEXT_MAX_SIZE_MILS );
391
392 m_attributes.m_Size.y = alg::clamp( min, aHeight, max );
395}
396
397
398void EDA_TEXT::SetTextPos( const VECTOR2I& aPoint )
399{
400 Offset( VECTOR2I( aPoint.x - m_pos.x, aPoint.y - m_pos.y ) );
401}
402
403
404void EDA_TEXT::SetTextX( int aX )
405{
406 Offset( VECTOR2I( aX - m_pos.x, 0 ) );
407}
408
409
410void EDA_TEXT::SetTextY( int aY )
411{
412 Offset( VECTOR2I( 0, aY - m_pos.y ) );
413}
414
415
416void EDA_TEXT::Offset( const VECTOR2I& aOffset )
417{
418 if( aOffset.x == 0 && aOffset.y == 0 )
419 return;
420
421 m_pos += aOffset;
422
423 for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_render_cache )
424 {
425 if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
426 outline->Move( aOffset );
427 else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
428 glyph = stroke->Transform( { 1.0, 1.0 }, aOffset, 0, ANGLE_0, false, { 0, 0 } );
429 }
430
432}
433
434
436{
437 m_text.Empty();
440}
441
442
444{
445 if( m_text.IsEmpty() )
446 {
447 m_shown_text = wxEmptyString;
449 }
450 else
451 {
453 m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
454 }
455
458}
459
460
462{
463 KIFONT::FONT* font = GetFont();
464
465 if( !font )
466 font = KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() );
467
468 return font;
469}
470
471
473{
475}
476
477
479{
480 m_render_cache.clear();
481}
482
483
485{
487}
488
489
490std::vector<std::unique_ptr<KIFONT::GLYPH>>*
491EDA_TEXT::GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
492 const VECTOR2I& aOffset ) const
493{
494 if( aFont->IsOutline() )
495 {
496 EDA_ANGLE resolvedAngle = GetDrawRotation();
497
498 if( m_render_cache.empty()
499 || m_render_cache_font != aFont
500 || m_render_cache_text != forResolvedText
501 || m_render_cache_angle != resolvedAngle
502 || m_render_cache_offset != aOffset )
503 {
504 m_render_cache.clear();
505
506 const KIFONT::OUTLINE_FONT* font = static_cast<const KIFONT::OUTLINE_FONT*>( aFont );
508
509 attrs.m_Angle = resolvedAngle;
510
511 font->GetLinesAsGlyphs( &m_render_cache, forResolvedText, GetDrawPos() + aOffset,
512 attrs, getFontMetrics() );
513 m_render_cache_font = aFont;
514 m_render_cache_angle = resolvedAngle;
515 m_render_cache_text = forResolvedText;
516 m_render_cache_offset = aOffset;
517 }
518
519 return &m_render_cache;
520 }
521
522 return nullptr;
523}
524
525
526void EDA_TEXT::SetupRenderCache( const wxString& aResolvedText, const EDA_ANGLE& aAngle )
527{
528 m_render_cache_text = aResolvedText;
529 m_render_cache_angle = aAngle;
530 m_render_cache.clear();
531}
532
533
535{
536 m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( aPoly ) );
537}
538
539
541{
543}
544
545
546BOX2I EDA_TEXT::GetTextBox( int aLine, bool aInvertY ) const
547{
548 VECTOR2I drawPos = GetDrawPos();
549
551 && m_bounding_box_cache_pos == drawPos
552 && m_bounding_box_cache_line == aLine
553 && m_bounding_box_cache_inverted == aInvertY )
554 {
556 }
557
558 BOX2I bbox;
559 wxArrayString strings;
560 wxString text = GetShownText( true );
561 int thickness = GetEffectiveTextPenWidth();
562
563 if( IsMultilineAllowed() )
564 {
565 wxStringSplit( text, strings, '\n' );
566
567 if( strings.GetCount() ) // GetCount() == 0 for void strings with multilines allowed
568 {
569 if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
570 text = strings.Item( aLine );
571 else
572 text = strings.Item( 0 );
573 }
574 }
575
576 // calculate the H and V size
577 KIFONT::FONT* font = getDrawFont();
578 VECTOR2D fontSize( GetTextSize() );
579 bool bold = IsBold();
580 bool italic = IsItalic();
581 VECTOR2I extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic,
582 getFontMetrics() );
583 int overbarOffset = 0;
584
585 // Creates bounding box (rectangle) for horizontal, left and top justified text. The
586 // bounding box will be moved later according to the actual text options
587 VECTOR2I textsize = VECTOR2I( extents.x, extents.y );
588 VECTOR2I pos = drawPos;
589 int fudgeFactor = KiROUND( extents.y * 0.17 );
590
591 if( font->IsStroke() )
592 textsize.y += fudgeFactor;
593
594 if( IsMultilineAllowed() && aLine > 0 && aLine < (int) strings.GetCount() )
595 pos.y -= KiROUND( aLine * font->GetInterline( fontSize.y, getFontMetrics() ) );
596
597 if( text.Contains( wxT( "~{" ) ) )
598 overbarOffset = extents.y / 6;
599
600 if( aInvertY )
601 pos.y = -pos.y;
602
603 bbox.SetOrigin( pos );
604
605 // for multiline texts and aLine < 0, merge all rectangles (aLine == -1 signals all lines)
606 if( IsMultilineAllowed() && aLine < 0 && strings.GetCount() > 1 )
607 {
608 for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
609 {
610 text = strings.Item( ii );
611 extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic,
612 getFontMetrics() );
613 textsize.x = std::max( textsize.x, extents.x );
614 }
615
616 // interline spacing is only *between* lines, so total height is the height of the first
617 // line plus the interline distance (with interline spacing) for all subsequent lines
618 textsize.y += KiROUND( ( strings.GetCount() - 1 ) * font->GetInterline( fontSize.y,
619 getFontMetrics() ) );
620 }
621
622 textsize.y += overbarOffset;
623
624 bbox.SetSize( textsize );
625
626 /*
627 * At this point the rectangle origin is the text origin (m_Pos). This is correct only for
628 * left and top justified, non-mirrored, non-overbarred texts. Recalculate for all others.
629 */
630 int italicOffset = IsItalic() ? KiROUND( fontSize.y * ITALIC_TILT ) : 0;
631
632 switch( GetHorizJustify() )
633 {
635 if( IsMirrored() )
636 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
637 break;
638
640 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) / 2 );
641 break;
642
644 if( !IsMirrored() )
645 bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
646 break;
647 }
648
649 switch( GetVertJustify() )
650 {
652 bbox.Offset( 0, -fudgeFactor );
653 break;
654
656 bbox.SetY( bbox.GetY() - bbox.GetHeight() / 2 );
657 break;
658
660 bbox.SetY( bbox.GetY() - bbox.GetHeight() );
661 bbox.Offset( 0, fudgeFactor );
662 break;
663 }
664
665 bbox.Normalize(); // Make h and v sizes always >= 0
666
668 m_bounding_box_cache_pos = drawPos;
672
673 return bbox;
674}
675
676
677bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
678{
679 BOX2I rect = GetTextBox();
680 VECTOR2I location = aPoint;
681
682 rect.Inflate( aAccuracy );
683 RotatePoint( location, GetDrawPos(), -GetDrawRotation() );
684
685 return rect.Contains( location );
686}
687
688
689bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
690{
691 BOX2I rect = aRect;
692
693 rect.Inflate( aAccuracy );
694
695 if( aContains )
696 return rect.Contains( GetTextBox() );
697
698 return rect.Intersects( GetTextBox(), GetDrawRotation() );
699}
700
701
702void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
703 const COLOR4D& aColor, OUTLINE_MODE aFillMode )
704{
705 if( IsMultilineAllowed() )
706 {
707 std::vector<VECTOR2I> positions;
708 wxArrayString strings;
709 wxStringSplit( GetShownText( true ), strings, '\n' );
710
711 positions.reserve( strings.Count() );
712
713 GetLinePositions( positions, (int) strings.Count() );
714
715 for( unsigned ii = 0; ii < strings.Count(); ii++ )
716 printOneLineOfText( aSettings, aOffset, aColor, aFillMode, strings[ii], positions[ii] );
717 }
718 else
719 {
720 printOneLineOfText( aSettings, aOffset, aColor, aFillMode, GetShownText( true ),
721 GetDrawPos() );
722 }
723}
724
725
726void EDA_TEXT::GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const
727{
728 VECTOR2I pos = GetDrawPos(); // Position of first line of the multiline text according
729 // to the center of the multiline text block
730
731 VECTOR2I offset; // Offset to next line.
732
733 offset.y = GetInterline();
734
735 if( aLineCount > 1 )
736 {
737 switch( GetVertJustify() )
738 {
740 break;
741
743 pos.y -= ( aLineCount - 1 ) * offset.y / 2;
744 break;
745
747 pos.y -= ( aLineCount - 1 ) * offset.y;
748 break;
749 }
750 }
751
752 // Rotate the position of the first line around the center of the multiline text block
754
755 // Rotate the offset lines to increase happened in the right direction
756 RotatePoint( offset, GetDrawRotation() );
757
758 for( int ii = 0; ii < aLineCount; ii++ )
759 {
760 aPositions.push_back( (VECTOR2I) pos );
761 pos += offset;
762 }
763}
764
765
766void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
767 const COLOR4D& aColor, OUTLINE_MODE aFillMode,
768 const wxString& aText, const VECTOR2I& aPos )
769{
770 wxDC* DC = aSettings->GetPrintDC();
771 int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
772
773 if( aFillMode == SKETCH )
774 penWidth = -penWidth;
775
776 VECTOR2I size = GetTextSize();
777
778 if( IsMirrored() )
779 size.x = -size.x;
780
781 KIFONT::FONT* font = GetFont();
782
783 if( !font )
784 font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
785
786 GRPrintText( DC, aOffset + aPos, aColor, aText, GetDrawRotation(), size, GetHorizJustify(),
787 GetVertJustify(), penWidth, IsItalic(), IsBold(), font, getFontMetrics() );
788}
789
790
792{
793 int style = 0;
794
795 if( IsItalic() )
796 style = 1;
797
798 if( IsBold() )
799 style += 2;
800
801 wxString stylemsg[4] = {
802 _("Normal"),
803 _("Italic"),
804 _("Bold"),
805 _("Bold+Italic")
806 };
807
808 return stylemsg[style];
809}
810
811
812wxString EDA_TEXT::GetFontName() const
813{
814 if( GetFont() )
815 return GetFont()->GetName();
816 else
817 return wxEmptyString;
818}
819
820
822{
823 return ( IsVisible()
824 && !IsMirrored()
827 && GetTextThickness() == 0
828 && !IsItalic()
829 && !IsBold()
831 && GetFontName().IsEmpty()
832 );
833}
834
835
836void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
837{
838 aFormatter->Print( aNestLevel + 1, "(effects" );
839
840 aFormatter->Print( 0, " (font" );
841
842 if( GetFont() && !GetFont()->GetName().IsEmpty() )
843 aFormatter->Print( 0, " (face \"%s\")", GetFont()->NameAsToken() );
844
845 // Text size
846 aFormatter->Print( 0, " (size %s %s)",
849
850 if( GetLineSpacing() != 1.0 )
851 {
852 aFormatter->Print( 0, " (line_spacing %s)",
853 FormatDouble2Str( GetLineSpacing() ).c_str() );
854 }
855
856 if( GetTextThickness() )
857 {
858 aFormatter->Print( 0, " (thickness %s)",
860 }
861
862 if( IsBold() )
863 aFormatter->Print( 0, " bold" );
864
865 if( IsItalic() )
866 aFormatter->Print( 0, " italic" );
867
868 if( GetTextColor() != COLOR4D::UNSPECIFIED )
869 {
870 aFormatter->Print( 0, " (color %d %d %d %s)",
871 KiROUND( GetTextColor().r * 255.0 ),
872 KiROUND( GetTextColor().g * 255.0 ),
873 KiROUND( GetTextColor().b * 255.0 ),
874 FormatDouble2Str( GetTextColor().a ).c_str() );
875 }
876
877 aFormatter->Print( 0, ")"); // (font
878
881 {
882 aFormatter->Print( 0, " (justify");
883
885 aFormatter->Print( 0, GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
886
888 aFormatter->Print( 0, GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
889
890 if( IsMirrored() )
891 aFormatter->Print( 0, " mirror" );
892
893 aFormatter->Print( 0, ")" ); // (justify
894 }
895
896 if( !( aControlBits & CTL_OMIT_HIDE ) && !IsVisible() )
897 aFormatter->Print( 0, " hide" );
898
899 if( HasHyperlink() )
900 {
901 aFormatter->Print( 0, " (href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
902 }
903
904 aFormatter->Print( 0, ")\n" ); // (effects
905}
906
907
908std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate,
909 bool aUseTextRotation ) const
910{
911 std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
913 KIFONT::FONT* font = getDrawFont();
914 int penWidth = GetEffectiveTextPenWidth();
915 wxString shownText( GetShownText( true ) );
916 VECTOR2I drawPos = GetDrawPos();
918
919 std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = nullptr;
920
921 if( aUseTextRotation )
922 {
923 attrs.m_Angle = GetDrawRotation();
924
925 if( font->IsOutline() )
926 cache = GetRenderCache( font, shownText, VECTOR2I() );
927 }
928 else
929 {
930 attrs.m_Angle = ANGLE_0;
931 }
932
933 if( aTriangulate )
934 {
935 CALLBACK_GAL callback_gal(
936 empty_opts,
937 // Stroke callback
938 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
939 {
940 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
941 },
942 // Triangulation callback
943 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
944 {
945 SHAPE_SIMPLE* triShape = new SHAPE_SIMPLE;
946
947 for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
948 triShape->Append( point.x, point.y );
949
950 shape->AddShape( triShape );
951 } );
952
953 if( cache )
954 callback_gal.DrawGlyphs( *cache );
955 else
956 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
957 }
958 else
959 {
960 CALLBACK_GAL callback_gal(
961 empty_opts,
962 // Stroke callback
963 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
964 {
965 shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
966 },
967 // Outline callback
968 [&]( const SHAPE_LINE_CHAIN& aPoly )
969 {
970 shape->AddShape( aPoly.Clone() );
971 } );
972
973 if( cache )
974 callback_gal.DrawGlyphs( *cache );
975 else
976 font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
977 }
978
979 return shape;
980}
981
982
983int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
984{
985 wxCHECK( aOther, 1 );
986
987 int val = m_attributes.Compare( aOther->m_attributes );
988
989 if( val != 0 )
990 return val;
991
992 if( m_pos.x != aOther->m_pos.x )
993 return m_pos.x - aOther->m_pos.x;
994
995 if( m_pos.y != aOther->m_pos.y )
996 return m_pos.y - aOther->m_pos.y;
997
998 val = GetFontName().Cmp( aOther->GetFontName() );
999
1000 if( val != 0 )
1001 return val;
1002
1003 return m_text.Cmp( aOther->m_text );
1004}
1005
1006
1007bool EDA_TEXT::ValidateHyperlink( const wxString& aURL )
1008{
1009 if( aURL.IsEmpty() || IsGotoPageHref( aURL ) )
1010 return true;
1011
1012 // Limit valid urls to file, http and https for now. Note wxURL doesn't support https
1013 wxURI uri;
1014
1015 if( uri.Create( aURL ) && uri.HasScheme() )
1016 {
1017 const wxString& scheme = uri.GetScheme();
1018 return scheme == wxT( "file" ) || scheme == wxT( "http" ) || scheme == wxT( "https" );
1019 }
1020
1021 return false;
1022}
1023
1024
1025bool EDA_TEXT::IsGotoPageHref( const wxString& aHref, wxString* aDestination )
1026{
1027 return aHref.StartsWith( wxT( "#" ), aDestination );
1028}
1029
1030
1031wxString EDA_TEXT::GotoPageHref( const wxString& aDestination )
1032{
1033 return wxT( "#" ) + aDestination;
1034}
1035
1036
1037std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aText )
1038{
1039 aStream << aText.GetText();
1040
1041 return aStream;
1042}
1043
1044
1045static struct EDA_TEXT_DESC
1046{
1048 {
1050 .Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) )
1051 .Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) )
1052 .Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
1054 .Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) )
1055 .Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) )
1056 .Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
1057
1060
1061 propMgr.AddProperty( new PROPERTY<EDA_TEXT, double>( _HKI( "Orientation" ),
1063 PROPERTY_DISPLAY::PT_DEGREE ) );
1064
1065 const wxString textProps = _( "Text Properties" );
1066
1067 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Text" ),
1070 textProps );
1071 propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Hyperlink" ),
1074 textProps );
1075 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Thickness" ),
1078 PROPERTY_DISPLAY::PT_SIZE ),
1079 textProps );
1080 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Italic" ),
1083 textProps );
1084 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Bold" ),
1086 textProps );
1087 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Mirrored" ),
1090 textProps );
1091 propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Visible" ),
1094 textProps );
1095 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Width" ),
1098 PROPERTY_DISPLAY::PT_SIZE ),
1099 textProps );
1100
1101 propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Height" ),
1104 PROPERTY_DISPLAY::PT_SIZE ),
1105 textProps );
1106
1107 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
1108 GR_TEXT_H_ALIGN_T>( _HKI( "Horizontal Justification" ),
1111 textProps );
1112 propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
1113 GR_TEXT_V_ALIGN_T>( _HKI( "Vertical Justification" ),
1116 textProps );
1117 }
1119
constexpr EDA_IU_SCALE unityScale
Definition: base_units.h:112
void SetOrigin(const Vec &pos)
Definition: box2.h:203
BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:120
void Offset(coord_type dx, coord_type dy)
Definition: box2.h:225
void SetX(coord_type val)
Definition: box2.h:236
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:270
void SetY(coord_type val)
Definition: box2.h:241
coord_type GetHeight() const
Definition: box2.h:189
coord_type GetY() const
Definition: box2.h:182
coord_type GetWidth() const
Definition: box2.h:188
void SetSize(const Vec &size)
Definition: box2.h:214
bool Contains(const Vec &aPoint) const
Definition: box2.h:142
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:507
coord_type GetX() const
Definition: box2.h:181
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:186
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:213
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:546
COLOR4D GetTextColor() const
Definition: eda_text.h:216
wxString GetTextStyleName() const
Definition: eda_text.cpp:791
VECTOR2I m_pos
Definition: eda_text.h:412
wxString m_text
Definition: eda_text.h:393
bool IsDefaultFormatting() const
Definition: eda_text.cpp:821
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
Definition: eda_text.cpp:1025
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_render_cache
Definition: eda_text.h:403
void SetTextSize(VECTOR2I aNewSize)
Definition: eda_text.cpp:355
wxString GetFontName() const
Definition: eda_text.cpp:812
void SetupRenderCache(const wxString &aResolvedText, const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:526
virtual ~EDA_TEXT()
Definition: eda_text.cpp:144
bool IsItalic() const
Definition: eda_text.h:141
BOX2I m_bounding_box_cache
Definition: eda_text.h:409
bool IsMultilineAllowed() const
Definition: eda_text.h:157
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:95
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:540
virtual bool IsVisible() const
Definition: eda_text.h:147
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:398
void SetTextX(int aX)
Definition: eda_text.cpp:404
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:395
KIFONT::FONT * GetFont() const
Definition: eda_text.h:199
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition: eda_text.cpp:273
wxString m_shown_text
Definition: eda_text.h:394
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:836
void SetMirrored(bool isMirrored)
Definition: eda_text.cpp:233
void SetTextY(int aY)
Definition: eda_text.cpp:410
int m_bounding_box_cache_line
Definition: eda_text.h:407
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:491
const KIFONT::FONT * m_render_cache_font
Definition: eda_text.h:400
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:315
virtual VECTOR2I GetDrawPos() const
Definition: eda_text.h:316
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition: eda_text.cpp:149
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:766
int GetTextWidth() const
Definition: eda_text.h:210
virtual bool HasHyperlink() const
Definition: eda_text.h:335
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:257
wxString GetHyperlink() const
Definition: eda_text.h:336
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:416
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:160
void SetTextWidth(int aWidth)
Definition: eda_text.cpp:376
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:326
int Compare(const EDA_TEXT *aOther) const
Definition: eda_text.cpp:983
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition: eda_text.h:397
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:226
EDA_TEXT(const EDA_IU_SCALE &aIuScale, const wxString &aText=wxEmptyString)
Definition: eda_text.cpp:88
bool m_bounding_box_cache_valid
Definition: eda_text.h:405
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:726
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
Definition: eda_text.cpp:1031
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:484
virtual KIFONT::FONT * getDrawFont() const
Definition: eda_text.cpp:461
double GetLineSpacing() const
Definition: eda_text.h:204
VECTOR2I m_render_cache_offset
Definition: eda_text.h:402
void SetLineSpacing(double aLineSpacing)
Definition: eda_text.cpp:347
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition: eda_text.cpp:534
void Empty()
Definition: eda_text.cpp:435
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.cpp:194
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:677
void SetTextHeight(int aHeight)
Definition: eda_text.cpp:387
virtual void cacheShownText()
Definition: eda_text.cpp:443
EDA_ANGLE m_render_cache_angle
Definition: eda_text.h:401
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:60
bool m_bounding_box_cache_inverted
Definition: eda_text.h:408
virtual void ClearRenderCache()
Definition: eda_text.cpp:478
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:183
void SetBold(bool aBold)
Definition: eda_text.cpp:218
static bool ValidateHyperlink(const wxString &aURL)
Check if aURL is a valid hyperlink.
Definition: eda_text.cpp:1007
VECTOR2I m_bounding_box_cache_pos
Definition: eda_text.h:406
bool IsMirrored() const
Definition: eda_text.h:150
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:305
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition: eda_text.cpp:292
wxString m_render_cache_text
Definition: eda_text.h:399
double GetTextAngleDegrees() const
Definition: eda_text.h:138
virtual const KIFONT::METRICS & getFontMetrics() const
Definition: eda_text.cpp:472
bool IsBold() const
Definition: eda_text.h:144
void SetTextAngleDegrees(double aOrientation)
Definition: eda_text.h:134
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:908
void SetHyperlink(wxString aLink)
Definition: eda_text.h:337
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:74
void SetKeepUpright(bool aKeepUpright)
Definition: eda_text.cpp:265
void CopyText(const EDA_TEXT &aSrc)
Definition: eda_text.cpp:187
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:163
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:106
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:180
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:202
int GetTextThickness() const
Definition: eda_text.h:123
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:702
void SetItalic(bool aItalic)
Definition: eda_text.cpp:210
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:285
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.cpp:241
void SetFont(KIFONT::FONT *aFont)
Definition: eda_text.cpp:339
VECTOR2I GetTextSize() const
Definition: eda_text.h:207
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:249
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:411
static ENUM_MAP< T > & Instance()
Definition: property.h:640
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)
Definition: font.cpp:146
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:251
const wxString & GetName() const
Definition: font.h:147
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:428
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.
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 METRICS &aFontMetrics) const
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.
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:322
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:543
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:475
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.
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.
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
int Compare(const TEXT_ATTRIBUTES &aRhs) const
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:437
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1037
static struct EDA_TEXT_DESC _EDA_TEXT_DESC
#define TEXT_MIN_SIZE_MILS
Minimum text size in mils.
Definition: eda_text.h:42
#define CTL_OMIT_HIDE
Definition: eda_text.h:63
#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
#define TEXT_MAX_SIZE_MILS
Maximum text size in mils (10 inches)
Definition: eda_text.h:43
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
int GetPenSizeForBold(int aTextSize)
Definition: gr_text.cpp:40
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
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:142
Some functions to handle hotkeys in KiCad.
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition: eda_units.h:125
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.
Definition: eda_units.cpp:142
T clamp(T min, T value, T max)
Definition: kicad_algo.h:204
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:742
#define REGISTER_TYPE(x)
Definition: property_mgr.h:356
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...
const double IU_PER_MM
Definition: base_units.h:77
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:588