KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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
41// These are only here for algorithmic safety, not to tell the user what to do.
42// PL_EDITOR has the least resolution (its internal units are microns), so the min size is chosen
43// to yield 1 in PL_EDITOR.
44// The max size chosen is somewhat arbitrary, but no one has complained yet.
45#define TEXT_MIN_SIZE_MM 0.001
46#define TEXT_MAX_SIZE_MM 250.0
47
48
49namespace KIGFX
50{
51 class RENDER_SETTINGS;
52 class COLOR4D;
53}
54
55namespace KIFONT
56{
57 class METRICS;
58 }
59
61using KIGFX::COLOR4D;
62
63// part of the kicad_plugin.h family of defines.
64// See kicad_plugin.h for the choice of the value
65// When set when calling EDA_TEXT::Format, disable writing the "hide" keyword in save file
66#define CTL_OMIT_HIDE (1 << 6)
67
68
73#define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
74
75
83{
84public:
85 EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText = wxEmptyString );
86
87 EDA_TEXT( const EDA_TEXT& aText );
88
89 virtual ~EDA_TEXT();
90
91 EDA_TEXT& operator=( const EDA_TEXT& aItem );
92
98 virtual const wxString& GetText() const { return m_text; }
99
109 virtual wxString GetShownText( bool aAllowExtraText, int aDepth = 0 ) const
110 {
111 return m_shown_text;
112 }
113
118
119 virtual void SetText( const wxString& aText );
120
125 void SetTextThickness( int aWidth );
127
131 int GetEffectiveTextPenWidth( int aDefaultPenWidth = 0 ) const;
132
133 virtual void SetTextAngle( const EDA_ANGLE& aAngle );
134 const EDA_ANGLE& GetTextAngle() const { return m_attributes.m_Angle; }
135
136 // For property system:
137 void SetTextAngleDegrees( double aOrientation )
138 {
139 SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
140 }
142
143 void SetItalic( bool aItalic );
144 bool IsItalic() const { return m_attributes.m_Italic; }
145
146 void SetBold( bool aBold );
147 void SetBoldFlag( bool aBold );
148 bool IsBold() const { return m_attributes.m_Bold; }
149
150 virtual void SetVisible( bool aVisible );
151 virtual bool IsVisible() const { return m_attributes.m_Visible; }
152
153 void SetMirrored( bool isMirrored );
154 bool IsMirrored() const { return m_attributes.m_Mirrored; }
155
160 void SetMultilineAllowed( bool aAllow );
162
165
166 void SetVertJustify( GR_TEXT_V_ALIGN_T aType );
168
169 void SetKeepUpright( bool aKeepUpright );
170 bool IsKeepUpright() const { return m_attributes.m_KeepUpright; }
171
173 {
178 }
179
183 void SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition = true );
184
188 void SwapAttributes( EDA_TEXT& aTradingPartner );
189
190 void SwapText( EDA_TEXT& aTradingPartner );
191
192 void CopyText( const EDA_TEXT& aSrc );
193
194 void SetAttributes( const TEXT_ATTRIBUTES& aTextAttrs ) { m_attributes = aTextAttrs; }
195 const TEXT_ATTRIBUTES& GetAttributes() const { return m_attributes; }
196
206 bool Replace( const EDA_SEARCH_DATA& aSearchData );
207
208 bool IsDefaultFormatting() const;
209
210 void SetFont( KIFONT::FONT* aFont );
212
213 wxString GetFontName() const;
214
215 void SetFontIndex( int aIdx );
216 int GetFontIndex() const;
217
218 void SetLineSpacing( double aLineSpacing );
219 double GetLineSpacing() const { return m_attributes.m_LineSpacing; }
220
221 void SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize = true );
223
224 void SetTextWidth( int aWidth );
225 int GetTextWidth() const { return m_attributes.m_Size.x; }
226
227 void SetTextHeight( int aHeight );
228 int GetTextHeight() const { return m_attributes.m_Size.y; }
229
230 void SetTextColor( const COLOR4D& aColor ) { m_attributes.m_Color = aColor; }
232
233 void SetTextPos( const VECTOR2I& aPoint );
234 const VECTOR2I& GetTextPos() const { return m_pos; }
235
236 void SetTextX( int aX );
237 void SetTextY( int aY );
238
239 void Offset( const VECTOR2I& aOffset );
240
241 void Empty();
242
243 static GR_TEXT_H_ALIGN_T MapHorizJustify( int aHorizJustify );
244 static GR_TEXT_V_ALIGN_T MapVertJustify( int aVertJustify );
245
254 void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor,
255 OUTLINE_MODE aDisplay_mode = FILLED );
256
263 std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true,
264 const BOX2I& aBBox = BOX2I(),
265 const EDA_ANGLE& aAngle = ANGLE_0 ) const;
266
274 virtual bool TextHitTest( const VECTOR2I& aPoint, int aAccuracy = 0 ) const;
275
284 virtual bool TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy = 0 ) const;
285
295 BOX2I GetTextBox( int aLine = -1 ) const;
296
304 int GetInterline() const;
305
309 wxString GetTextStyleName() const;
310
318 void GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const;
319
325 double Levenshtein( const EDA_TEXT& aOther ) const;
326
327
328 double Similarity( const EDA_TEXT& aOther ) const;
329
338 virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
339
340 virtual EDA_ANGLE GetDrawRotation() const { return GetTextAngle(); }
341 virtual VECTOR2I GetDrawPos() const { return GetTextPos(); }
342
343 virtual void ClearRenderCache();
344 virtual void ClearBoundingBoxCache();
345
346 std::vector<std::unique_ptr<KIFONT::GLYPH>>*
347 GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
348 const VECTOR2I& aOffset = { 0, 0 } ) const;
349
350 // Support for reading the cache from disk.
351 void SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont,
352 const EDA_ANGLE& aAngle, const VECTOR2I& aOffset );
353 void AddRenderCacheGlyph( const SHAPE_POLY_SET& aPoly );
354
355 int Compare( const EDA_TEXT* aOther ) const;
356
357 bool operator==( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) == 0; }
358 bool operator<( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) < 0; }
359 bool operator>( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) > 0; }
360
361 virtual bool HasHyperlink() const { return !m_hyperlink.IsEmpty(); }
362 wxString GetHyperlink() const { return m_hyperlink; }
363 void SetHyperlink( wxString aLink ) { m_hyperlink = aLink; }
364 void RemoveHyperlink() { m_hyperlink = wxEmptyString; }
365
372 static bool ValidateHyperlink( const wxString& aURL );
373
382 static bool IsGotoPageHref( const wxString& aHref, wxString* aDestination = nullptr );
383
390 static wxString GotoPageHref( const wxString& aDestination );
391
392protected:
393 virtual KIFONT::FONT* getDrawFont() const;
394
395 virtual const KIFONT::METRICS& getFontMetrics() const;
396
397 virtual void cacheShownText();
398
408 void printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
409 const COLOR4D& aColor, OUTLINE_MODE aFillMode, const wxString& aText,
410 const VECTOR2I& aPos );
411
412protected:
416 wxString m_hyperlink;
417
418private:
419 wxString m_text;
420 wxString m_shown_text; // Cache of unescaped text for efficient access
422
423 std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
424
425 mutable wxString m_render_cache_text;
429 mutable std::vector<std::unique_ptr<KIFONT::GLYPH>> m_render_cache;
430
435
438};
439
440
441extern std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aAttributes );
442
443
444template<>
445struct std::hash<EDA_TEXT>
446{
447 std::size_t operator()( const EDA_TEXT& aText ) const
448 {
449 return hash_val( aText.GetText(), aText.GetAttributes(), aText.GetTextPos().x,
450 aText.GetTextPos().y );
451 }
452};
453
454#endif // EDA_TEXT_H_
BOX2< VECTOR2I > BOX2I
Definition: box2.h:877
double AsDegrees() const
Definition: eda_angle.h:155
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:83
int GetTextHeight() const
Definition: eda_text.h:228
void SetTextColor(const COLOR4D &aColor)
Definition: eda_text.h:230
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:234
COLOR4D GetTextColor() const
Definition: eda_text.h:231
wxString GetTextStyleName() const
Definition: eda_text.cpp:821
VECTOR2I m_pos
Definition: eda_text.h:437
wxString m_text
Definition: eda_text.h:419
bool IsDefaultFormatting() const
Definition: eda_text.cpp:895
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
Definition: eda_text.cpp:1157
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_render_cache
Definition: eda_text.h:429
wxString GetFontName() const
Definition: eda_text.cpp:842
virtual ~EDA_TEXT()
Definition: eda_text.cpp:145
bool IsItalic() const
Definition: eda_text.h:144
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:134
BOX2I m_bounding_box_cache
Definition: eda_text.h:434
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:372
bool IsMultilineAllowed() const
Definition: eda_text.h:161
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:563
bool IsKeepUpright() const
Definition: eda_text.h:170
virtual bool IsVisible() const
Definition: eda_text.h:151
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:417
void SetTextX(int aX)
Definition: eda_text.cpp:423
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:421
KIFONT::FONT * GetFont() const
Definition: eda_text.h:211
void SetAttributes(const TEXT_ATTRIBUTES &aTextAttrs)
Definition: eda_text.h:194
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition: eda_text.cpp:290
wxString m_shown_text
Definition: eda_text.h:420
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:910
int GetFontIndex() const
Definition: eda_text.cpp:851
BOX2I GetTextBox(int aLine=-1) const
Useful in multiline texts to calculate the full text or a line area (for zones filling,...
Definition: eda_text.cpp:569
void SetMirrored(bool isMirrored)
Definition: eda_text.cpp:250
void SetTextY(int aY)
Definition: eda_text.cpp:429
int m_bounding_box_cache_line
Definition: eda_text.h:433
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:510
const KIFONT::FONT * m_render_cache_font
Definition: eda_text.h:426
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:340
virtual VECTOR2I GetDrawPos() const
Definition: eda_text.h:341
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition: eda_text.cpp:150
wxString m_hyperlink
A hyperlink URL.
Definition: eda_text.h:416
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:796
int GetTextWidth() const
Definition: eda_text.h:225
virtual bool HasHyperlink() const
Definition: eda_text.h:361
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:274
wxString GetHyperlink() const
Definition: eda_text.h:362
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:435
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:164
void SetTextWidth(int aWidth)
Definition: eda_text.cpp:395
void SetBoldFlag(bool aBold)
Definition: eda_text.cpp:235
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:343
void SetupRenderCache(const wxString &aResolvedText, const KIFONT::FONT *aFont, const EDA_ANGLE &aAngle, const VECTOR2I &aOffset)
Definition: eda_text.cpp:545
int Compare(const EDA_TEXT *aOther) const
Definition: eda_text.cpp:1061
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition: eda_text.h:423
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition: eda_text.h:117
bool operator>(const EDA_TEXT &aRhs) const
Definition: eda_text.h:359
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:243
bool m_bounding_box_cache_valid
Definition: eda_text.h:431
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:752
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
Definition: eda_text.cpp:1163
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:503
virtual KIFONT::FONT * getDrawFont() const
Definition: eda_text.cpp:480
double GetLineSpacing() const
Definition: eda_text.h:219
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1141
VECTOR2I m_render_cache_offset
Definition: eda_text.h:428
void SetLineSpacing(double aLineSpacing)
Definition: eda_text.cpp:364
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition: eda_text.cpp:556
void Empty()
Definition: eda_text.cpp:454
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.cpp:195
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:703
void FlipHJustify()
Definition: eda_text.h:172
void SetTextHeight(int aHeight)
Definition: eda_text.cpp:406
virtual void cacheShownText()
Definition: eda_text.cpp:462
EDA_ANGLE m_render_cache_angle
Definition: eda_text.h:427
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:63
virtual void ClearRenderCache()
Definition: eda_text.cpp:497
void RemoveHyperlink()
Definition: eda_text.h:364
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:195
void SetBold(bool aBold)
Definition: eda_text.cpp:219
static bool ValidateHyperlink(const wxString &aURL)
Check if aURL is a valid hyperlink.
Definition: eda_text.cpp:1085
VECTOR2I m_bounding_box_cache_pos
Definition: eda_text.h:432
bool IsMirrored() const
Definition: eda_text.h:154
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:322
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition: eda_text.cpp:309
wxString m_render_cache_text
Definition: eda_text.h:425
double GetTextAngleDegrees() const
Definition: eda_text.h:141
virtual const KIFONT::METRICS & getFontMetrics() const
Definition: eda_text.cpp:491
std::shared_ptr< SHAPE_COMPOUND > GetEffectiveTextShape(bool aTriangulate=true, const BOX2I &aBBox=BOX2I(), const EDA_ANGLE &aAngle=ANGLE_0) const
build a list of segments (SHAPE_SEGMENT) to describe a text shape.
Definition: eda_text.cpp:982
bool operator<(const EDA_TEXT &aRhs) const
Definition: eda_text.h:358
bool IsBold() const
Definition: eda_text.h:148
void SetTextAngleDegrees(double aOrientation)
Definition: eda_text.h:137
void SetHyperlink(wxString aLink)
Definition: eda_text.h:363
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:77
void SetKeepUpright(bool aKeepUpright)
Definition: eda_text.cpp:282
void CopyText(const EDA_TEXT &aSrc)
Definition: eda_text.cpp:188
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:167
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:109
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:181
double Levenshtein(const EDA_TEXT &aOther) const
Return the levenstein distance between two texts.
Definition: eda_text.cpp:1095
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:203
void SetFontIndex(int aIdx)
Definition: eda_text.cpp:872
int GetTextThickness() const
Definition: eda_text.h:126
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:728
void SetItalic(bool aItalic)
Definition: eda_text.cpp:211
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:302
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:357
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.cpp:258
void SetFont(KIFONT::FONT *aFont)
Definition: eda_text.cpp:356
VECTOR2I GetTextSize() const
Definition: eda_text.h:222
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:266
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:436
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
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:322
Represent a set of closed polygons.
KIGFX::COLOR4D m_Color
GR_TEXT_H_ALIGN_T m_Halign
GR_TEXT_V_ALIGN_T m_Valign
KIFONT::FONT * m_Font
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:435
@ DEGREES_T
Definition: eda_angle.h:31
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aAttributes)
Definition: eda_text.cpp:1169
static const bool FILLED
Definition: gr_basic.cpp:30
static std::size_t hash_val(const Types &... args)
Definition: hash.h:51
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
OUTLINE_MODE
Definition: outline_mode.h:25
std::size_t operator()(const EDA_TEXT &aText) const
Definition: eda_text.h:447
GR_TEXT_H_ALIGN_T
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
GR_TEXT_V_ALIGN_T