KiCad PCB EDA Suite
eda_text.h
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) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2004-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#ifndef EDA_TEXT_H_
26#define EDA_TEXT_H_
27
28#include <memory>
29#include <vector>
30
31#include <outline_mode.h>
32#include <eda_search_data.h>
33#include <font/glyph.h>
35
36class OUTPUTFORMATTER;
37class SHAPE_COMPOUND;
38class SHAPE_POLY_SET;
39
40
41namespace KIGFX
42{
43 class RENDER_SETTINGS;
44 class COLOR4D;
45}
46
48using KIGFX::COLOR4D;
49
50// part of the kicad_plugin.h family of defines.
51// See kicad_plugin.h for the choice of the value
52// When set when calling EDA_TEXT::Format, disable writing the "hide" keyword in save file
53#define CTL_OMIT_HIDE (1 << 6)
54
55
61#define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
62#define DIM_ANCRE_TEXTE 2 // Anchor size for text
63
64
72{
73public:
74 EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText = wxEmptyString );
75
76 EDA_TEXT( const EDA_TEXT& aText );
77
78 virtual ~EDA_TEXT();
79
80 EDA_TEXT& operator=( const EDA_TEXT& aItem );
81
87 virtual const wxString& GetText() const { return m_text; }
88
98 virtual wxString GetShownText( int aDepth = 0, bool aAllowExtraText = true ) const { return m_shown_text; }
99
104
105 virtual void SetText( const wxString& aText );
106
111 void SetTextThickness( int aWidth );
113
117 int GetEffectiveTextPenWidth( int aDefaultPenWidth = 0 ) const;
118
119 virtual void SetTextAngle( const EDA_ANGLE& aAngle );
120 const EDA_ANGLE& GetTextAngle() const { return m_attributes.m_Angle; }
121
122 // For property system:
123 void SetTextAngleDegrees( double aOrientation )
124 {
125 SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
126 }
128
129 void SetItalic( bool aItalic );
130 bool IsItalic() const { return m_attributes.m_Italic; }
131
132 void SetBold( bool aBold );
133 bool IsBold() const { return m_attributes.m_Bold; }
134
135 virtual void SetVisible( bool aVisible );
136 virtual bool IsVisible() const { return m_attributes.m_Visible; }
137
138 void SetMirrored( bool isMirrored );
139 bool IsMirrored() const { return m_attributes.m_Mirrored; }
140
145 void SetMultilineAllowed( bool aAllow );
147
150
151 void SetVertJustify( GR_TEXT_V_ALIGN_T aType );
153
154 void SetKeepUpright( bool aKeepUpright );
155 bool IsKeepUpright() const { return m_attributes.m_KeepUpright; }
156
160 void SetAttributes( const EDA_TEXT& aSrc );
161
165 void SwapAttributes( EDA_TEXT& aTradingPartner );
166
167 void SwapText( EDA_TEXT& aTradingPartner );
168
169 void CopyText( const EDA_TEXT& aSrc );
170
171 void SetAttributes( const TEXT_ATTRIBUTES& aTextAttrs ) { m_attributes = aTextAttrs; }
172 const TEXT_ATTRIBUTES& GetAttributes() const { return m_attributes; }
173
183 bool Replace( const EDA_SEARCH_DATA& aSearchData );
184
185 bool IsDefaultFormatting() const;
186
187 void SetFont( KIFONT::FONT* aFont );
189
190 wxString GetFontName() const;
191
192 void SetLineSpacing( double aLineSpacing );
193 double GetLineSpacing() const { return m_attributes.m_LineSpacing; }
194
195 void SetTextSize( const VECTOR2I& aNewSize );
197
198 void SetTextWidth( int aWidth );
199 int GetTextWidth() const { return m_attributes.m_Size.x; }
200
201 void SetTextHeight( int aHeight );
202 int GetTextHeight() const { return m_attributes.m_Size.y; }
203
204 void SetTextColor( const COLOR4D& aColor ) { m_attributes.m_Color = aColor; }
206
207 void SetTextPos( const VECTOR2I& aPoint );
208 const VECTOR2I& GetTextPos() const { return m_pos; }
209
210 void SetTextX( int aX );
211 void SetTextY( int aY );
212
213 void Offset( const VECTOR2I& aOffset );
214
215 void Empty();
216
217 static GR_TEXT_H_ALIGN_T MapHorizJustify( int aHorizJustify );
218 static GR_TEXT_V_ALIGN_T MapVertJustify( int aVertJustify );
219
228 void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
229 const COLOR4D& aColor, OUTLINE_MODE aDisplay_mode = FILLED );
230
242 void TransformBoundingBoxToPolygon( SHAPE_POLY_SET* aBuffer, int aClearance ) const;
243
250 std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true,
251 bool aUseTextRotation = true ) const;
252
260 virtual bool TextHitTest( const VECTOR2I& aPoint, int aAccuracy = 0 ) const;
261
270 virtual bool TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy = 0 ) const;
271
282 BOX2I GetTextBox( int aLine = -1, bool aInvertY = false ) const;
283
291 int GetInterline() const;
292
296 wxString GetTextStyleName() const;
297
305 void GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const;
306
315 virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
316
317 virtual EDA_ANGLE GetDrawRotation() const { return GetTextAngle(); }
318 virtual VECTOR2I GetDrawPos() const { return GetTextPos(); }
319
320 virtual void ClearRenderCache();
321 virtual void ClearBoundingBoxCache();
322
323 std::vector<std::unique_ptr<KIFONT::GLYPH>>*
324 GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
325 const VECTOR2I& aOffset = { 0, 0 } ) const;
326
327 // Support for reading the cache from disk.
328 void SetupRenderCache( const wxString& aResolvedText, const EDA_ANGLE& aAngle );
329 void AddRenderCacheGlyph( const SHAPE_POLY_SET& aPoly );
330
331 int Compare( const EDA_TEXT* aOther ) const;
332
333 virtual bool HasHyperlink() const { return !m_hyperlink.IsEmpty(); }
334 wxString GetHyperlink() const { return m_hyperlink; }
335 void SetHyperlink( wxString aLink ) { m_hyperlink = aLink; }
336 void RemoveHyperlink() { m_hyperlink = wxEmptyString; }
337
344 static bool ValidateHyperlink( const wxString& aURL );
345
354 static bool IsGotoPageHref( const wxString& aHref, wxString* aDestination = nullptr );
355
362 static wxString GotoPageHref( const wxString& aDestination );
363
364protected:
365 virtual KIFONT::FONT* getDrawFont() const;
366
367 void cacheShownText();
368
378 void printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
379 const COLOR4D& aColor, OUTLINE_MODE aFillMode, const wxString& aText,
380 const VECTOR2I& aPos );
381
382protected:
386 wxString m_hyperlink;
387
388private:
389 wxString m_text;
390 wxString m_shown_text; // Cache of unescaped text for efficient access
392
393 std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
394
395 mutable wxString m_render_cache_text;
398 mutable std::vector<std::unique_ptr<KIFONT::GLYPH>> m_render_cache;
399
405
408};
409
410
411#endif // EDA_TEXT_H_
double AsDegrees() const
Definition: eda_angle.h:149
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: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:503
void SetTextColor(const COLOR4D &aColor)
Definition: eda_text.h:204
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:208
COLOR4D GetTextColor() const
Definition: eda_text.h:205
wxString GetTextStyleName() const
Definition: eda_text.cpp:736
VECTOR2I m_pos
Definition: eda_text.h:407
wxString m_text
Definition: eda_text.h:389
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:398
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:130
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:120
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:264
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:497
bool IsKeepUpright() const
Definition: eda_text.h:155
virtual bool IsVisible() const
Definition: eda_text.h:136
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:391
KIFONT::FONT * GetFont() const
Definition: eda_text.h:188
void SetAttributes(const TEXT_ATTRIBUTES &aTextAttrs)
Definition: eda_text.h:171
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: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: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:451
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:136
wxString m_hyperlink
A hyperlink URL.
Definition: eda_text.h:386
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:199
virtual bool HasHyperlink() const
Definition: eda_text.h:333
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:248
wxString GetHyperlink() const
Definition: eda_text.h:334
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:389
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:149
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:393
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition: eda_text.h:103
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: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: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:193
VECTOR2I m_render_cache_offset
Definition: eda_text.h:397
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:396
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:62
bool m_bounding_box_cache_inverted
Definition: eda_text.h:403
virtual void ClearRenderCache()
Definition: eda_text.cpp:438
void RemoveHyperlink()
Definition: eda_text.h:336
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:172
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: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: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: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:853
void SetHyperlink(wxString aLink)
Definition: eda_text.h:335
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:152
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:196
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:240
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:406
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
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.
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
Represent a set of closed polygons.
bool m_KeepUpright
If true, keep rotation angle between -90...90 degrees for readability.
KIGFX::COLOR4D m_Color
GR_TEXT_H_ALIGN_T m_Halign
GR_TEXT_V_ALIGN_T m_Valign
KIFONT::FONT * m_Font
@ DEGREES_T
Definition: eda_angle.h:31
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:266
OUTLINE_MODE
Definition: outline_mode.h:25
@ FILLED
Definition: outline_mode.h:27
GR_TEXT_H_ALIGN_T
GR_TEXT_V_ALIGN_T