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
95 virtual wxString GetShownText( int aDepth = 0 ) const { return m_shown_text; }
96
101
102 virtual void SetText( const wxString& aText );
103
108 void SetTextThickness( int aWidth );
110
114 int GetEffectiveTextPenWidth( int aDefaultPenWidth = 0 ) const;
115
116 virtual void SetTextAngle( const EDA_ANGLE& aAngle );
117 const EDA_ANGLE& GetTextAngle() const { return m_attributes.m_Angle; }
118
119 void SetItalic( bool aItalic );
120 bool IsItalic() const { return m_attributes.m_Italic; }
121
122 void SetBold( bool aBold );
123 bool IsBold() const { return m_attributes.m_Bold; }
124
125 virtual void SetVisible( bool aVisible );
126 virtual bool IsVisible() const { return m_attributes.m_Visible; }
127
128 void SetMirrored( bool isMirrored );
129 bool IsMirrored() const { return m_attributes.m_Mirrored; }
130
135 void SetMultilineAllowed( bool aAllow );
137
140
141 void SetVertJustify( GR_TEXT_V_ALIGN_T aType );
143
144 void SetKeepUpright( bool aKeepUpright );
145 bool IsKeepUpright() const { return m_attributes.m_KeepUpright; }
146
150 void SetAttributes( const EDA_TEXT& aSrc );
151
155 void SwapAttributes( EDA_TEXT& aTradingPartner );
156
157 void SwapText( EDA_TEXT& aTradingPartner );
158
159 void CopyText( const EDA_TEXT& aSrc );
160
161 void SetAttributes( const TEXT_ATTRIBUTES& aTextAttrs ) { m_attributes = aTextAttrs; }
162 const TEXT_ATTRIBUTES& GetAttributes() const { return m_attributes; }
163
173 bool Replace( const EDA_SEARCH_DATA& aSearchData );
174
175 bool IsDefaultFormatting() const;
176
177 void SetFont( KIFONT::FONT* aFont );
179
180 wxString GetFontName() const;
181
182 void SetLineSpacing( double aLineSpacing );
183 double GetLineSpacing() const { return m_attributes.m_LineSpacing; }
184
185 void SetTextSize( const VECTOR2I& aNewSize );
187
188 void SetTextWidth( int aWidth );
189 int GetTextWidth() const { return m_attributes.m_Size.x; }
190
191 void SetTextHeight( int aHeight );
192 int GetTextHeight() const { return m_attributes.m_Size.y; }
193
194 void SetTextColor( const COLOR4D& aColor ) { m_attributes.m_Color = aColor; }
196
197 void SetTextPos( const VECTOR2I& aPoint );
198 const VECTOR2I& GetTextPos() const { return m_pos; }
199
200 void SetTextX( int aX );
201 void SetTextY( int aY );
202
203 void Offset( const VECTOR2I& aOffset );
204
205 void Empty();
206
207 static GR_TEXT_H_ALIGN_T MapHorizJustify( int aHorizJustify );
208 static GR_TEXT_V_ALIGN_T MapVertJustify( int aVertJustify );
209
218 void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
219 const COLOR4D& aColor, OUTLINE_MODE aDisplay_mode = FILLED );
220
233 int aClearanceValue ) const;
234
241 std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true,
242 bool aUseTextRotation = true ) const;
243
251 virtual bool TextHitTest( const VECTOR2I& aPoint, int aAccuracy = 0 ) const;
252
261 virtual bool TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy = 0 ) const;
262
273 BOX2I GetTextBox( int aLine = -1, bool aInvertY = false ) const;
274
282 int GetInterline() const;
283
287 wxString GetTextStyleName() const;
288
296 void GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const;
297
306 virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
307
308 virtual KIFONT::FONT* GetDrawFont() const;
309 virtual EDA_ANGLE GetDrawRotation() const { return GetTextAngle(); }
310 virtual VECTOR2I GetDrawPos() const { return GetTextPos(); }
311
312 virtual void ClearRenderCache();
313 virtual void ClearBoundingBoxCache();
314
315 std::vector<std::unique_ptr<KIFONT::GLYPH>>*
316 GetRenderCache( const wxString& forResolvedText, const VECTOR2I& aOffset = { 0, 0 } ) const;
317
318 // Support for reading the cache from disk.
319 void SetupRenderCache( const wxString& aResolvedText, const EDA_ANGLE& aAngle );
320 void AddRenderCacheGlyph( const SHAPE_POLY_SET& aPoly );
321
322 int Compare( const EDA_TEXT* aOther ) const;
323
324 virtual bool HasHyperlink() const { return !m_hyperlink.IsEmpty(); }
325 wxString GetHyperlink() const { return m_hyperlink; }
326 void SetHyperlink( wxString aLink ) { m_hyperlink = aLink; }
327 void RemoveHyperlink() { m_hyperlink = wxEmptyString; }
328
335 static bool ValidateHyperlink( const wxString& aURL );
336
345 static bool IsGotoPageHref( const wxString& aHref, wxString* aDestination = nullptr );
346
353 static wxString GotoPageHref( const wxString& aDestination );
354
355protected:
359 wxString m_hyperlink;
360
361private:
362 void cacheShownText();
363
373 void printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
374 const COLOR4D& aColor, OUTLINE_MODE aFillMode, const wxString& aText,
375 const VECTOR2I& aPos );
376
377 wxString m_text;
378 wxString m_shown_text; // Cache of unescaped text for efficient access
380
381 std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
382
383 mutable wxString m_render_cache_text;
386 mutable std::vector<std::unique_ptr<KIFONT::GLYPH>> m_render_cache;
387
393
396};
397
398
399#endif // EDA_TEXT_H_
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:72
int GetTextHeight() const
Definition: eda_text.h:192
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:502
void SetTextColor(const COLOR4D &aColor)
Definition: eda_text.h:194
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:198
COLOR4D GetTextColor() const
Definition: eda_text.h:195
wxString GetTextStyleName() const
Definition: eda_text.cpp:730
VECTOR2I m_pos
Definition: eda_text.h:395
wxString m_text
Definition: eda_text.h:377
bool IsDefaultFormatting() const
Definition: eda_text.cpp:760
virtual KIFONT::FONT * GetDrawFont() const
Definition: eda_text.cpp:426
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
Definition: eda_text.cpp:994
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_render_cache
Definition: eda_text.h:386
wxString GetFontName() const
Definition: eda_text.cpp:751
void SetupRenderCache(const wxString &aResolvedText, const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:482
virtual ~EDA_TEXT()
Definition: eda_text.cpp:131
bool IsItalic() const
Definition: eda_text.h:120
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:117
BOX2I m_bounding_box_cache
Definition: eda_text.h:392
bool IsMultilineAllowed() const
Definition: eda_text.h:136
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:496
bool IsKeepUpright() const
Definition: eda_text.h:145
virtual bool IsVisible() const
Definition: eda_text.h:126
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:379
KIFONT::FONT * GetFont() const
Definition: eda_text.h:178
void SetAttributes(const TEXT_ATTRIBUTES &aTextAttrs)
Definition: eda_text.h:161
wxString m_shown_text
Definition: eda_text.h:378
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:775
std::vector< std::unique_ptr< KIFONT::GLYPH > > * GetRenderCache(const wxString &forResolvedText, const VECTOR2I &aOffset={ 0, 0 }) const
Definition: eda_text.cpp:451
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:390
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:309
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:95
virtual VECTOR2I GetDrawPos() const
Definition: eda_text.h:310
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition: eda_text.cpp:136
wxString m_hyperlink
A hyperlink URL.
Definition: eda_text.h:359
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:710
int GetTextWidth() const
Definition: eda_text.h:189
virtual bool HasHyperlink() const
Definition: eda_text.h:324
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:248
wxString GetHyperlink() const
Definition: eda_text.h:325
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:389
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:139
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:905
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition: eda_text.h:381
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition: eda_text.h:100
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:388
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:670
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
Definition: eda_text.cpp:1000
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:444
double GetLineSpacing() const
Definition: eda_text.h:183
VECTOR2I m_render_cache_offset
Definition: eda_text.h:385
void SetLineSpacing(double aLineSpacing)
Definition: eda_text.cpp:339
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition: eda_text.cpp:490
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:622
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:384
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:62
bool m_bounding_box_cache_inverted
Definition: eda_text.h:391
virtual void ClearRenderCache()
Definition: eda_text.cpp:438
void RemoveHyperlink()
Definition: eda_text.h:327
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:162
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:976
VECTOR2I m_bounding_box_cache_pos
Definition: eda_text.h:389
bool IsMirrored() const
Definition: eda_text.h:129
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:383
void TransformBoundingBoxWithClearanceToPolygon(SHAPE_POLY_SET *aCornerBuffer, int aClearanceValue) const
Convert the text bounding box to a rectangular polygon depending on the text orientation,...
Definition: eda_text.cpp:939
bool IsBold() const
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:847
void SetHyperlink(wxString aLink)
Definition: eda_text.h:326
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:142
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:109
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:647
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:186
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:240
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:394
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
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:243
OUTLINE_MODE
Definition: outline_mode.h:25
@ FILLED
Definition: outline_mode.h:27
GR_TEXT_H_ALIGN_T
GR_TEXT_V_ALIGN_T