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
36
37class OUTPUTFORMATTER;
38class SHAPE_COMPOUND;
39class SHAPE_POLY_SET;
40
41
42// These are only here for algorithmic safety, not to tell the user what to do.
43// PL_EDITOR has the least resolution (its internal units are microns), so the min size is chosen
44// to yield 1 in PL_EDITOR.
45// The max size chosen is somewhat arbitrary, but no one has complained yet.
46#define TEXT_MIN_SIZE_MM 0.001
47#define TEXT_MAX_SIZE_MM 250.0
48
49
50namespace KIGFX
51{
52 class RENDER_SETTINGS;
53 class COLOR4D;
54}
55
56namespace KIFONT
57{
58 class METRICS;
59 }
60
62using KIGFX::COLOR4D;
63
64
69#define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
70
71
79{
80public:
81 EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText = wxEmptyString );
82
83 EDA_TEXT( const EDA_TEXT& aText );
84
85 virtual ~EDA_TEXT();
86
87 EDA_TEXT& operator=( const EDA_TEXT& aItem );
88
94 virtual const wxString& GetText() const { return m_text; }
95
105 virtual wxString GetShownText( bool aAllowExtraText, int aDepth = 0 ) const
106 {
107 return m_shown_text;
108 }
109
114
115 virtual void SetText( const wxString& aText );
116
121 void SetTextThickness( int aWidth );
123
127 int GetEffectiveTextPenWidth( int aDefaultPenWidth = 0 ) const;
128
129 virtual void SetTextAngle( const EDA_ANGLE& aAngle );
130 const EDA_ANGLE& GetTextAngle() const { return m_attributes.m_Angle; }
131
132 // For property system:
133 void SetTextAngleDegrees( double aOrientation )
134 {
135 SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
136 }
138
144 void SetItalic( bool aItalic );
145
151 void SetItalicFlag( bool aItalic );
152 bool IsItalic() const { return m_attributes.m_Italic; }
153
159 void SetBold( bool aBold );
160
166 void SetBoldFlag( bool aBold );
167 bool IsBold() const { return m_attributes.m_Bold; }
168
169 virtual void SetVisible( bool aVisible );
170 virtual bool IsVisible() const { return m_attributes.m_Visible; }
171
172 void SetMirrored( bool isMirrored );
173 bool IsMirrored() const { return m_attributes.m_Mirrored; }
174
179 void SetMultilineAllowed( bool aAllow );
181
184
185 void SetVertJustify( GR_TEXT_V_ALIGN_T aType );
187
188 void SetKeepUpright( bool aKeepUpright );
189 bool IsKeepUpright() const { return m_attributes.m_KeepUpright; }
190
192 {
197 }
198
202 void SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition = true );
203
207 void SwapAttributes( EDA_TEXT& aTradingPartner );
208
209 void SwapText( EDA_TEXT& aTradingPartner );
210
211 void CopyText( const EDA_TEXT& aSrc );
212
213 void SetAttributes( const TEXT_ATTRIBUTES& aTextAttrs ) { m_attributes = aTextAttrs; }
214 const TEXT_ATTRIBUTES& GetAttributes() const { return m_attributes; }
215
225 bool Replace( const EDA_SEARCH_DATA& aSearchData );
226
227 bool IsDefaultFormatting() const;
228
229 void SetFont( KIFONT::FONT* aFont );
231
232 wxString GetFontName() const;
233
234 void SetFontIndex( int aIdx );
235 int GetFontIndex() const;
236
237 void SetLineSpacing( double aLineSpacing );
238 double GetLineSpacing() const { return m_attributes.m_LineSpacing; }
239
240 void SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize = true );
242
243 void SetTextWidth( int aWidth );
244 int GetTextWidth() const { return m_attributes.m_Size.x; }
245
246 void SetTextHeight( int aHeight );
247 int GetTextHeight() const { return m_attributes.m_Size.y; }
248
249 void SetTextColor( const COLOR4D& aColor ) { m_attributes.m_Color = aColor; }
251
252 void SetTextPos( const VECTOR2I& aPoint );
253 const VECTOR2I& GetTextPos() const { return m_pos; }
254
255 void SetTextX( int aX );
256 void SetTextY( int aY );
257
258 void Offset( const VECTOR2I& aOffset );
259
260 void Empty();
261
262 static GR_TEXT_H_ALIGN_T MapHorizJustify( int aHorizJustify );
263 static GR_TEXT_V_ALIGN_T MapVertJustify( int aVertJustify );
264
273 void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor,
274 OUTLINE_MODE aDisplay_mode = FILLED );
275
282 std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true,
283 const BOX2I& aBBox = BOX2I(),
284 const EDA_ANGLE& aAngle = ANGLE_0 ) const;
285
293 virtual bool TextHitTest( const VECTOR2I& aPoint, int aAccuracy = 0 ) const;
294
303 virtual bool TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy = 0 ) const;
304
314 BOX2I GetTextBox( int aLine = -1 ) const;
315
323 int GetInterline() const;
324
328 wxString GetTextStyleName() const;
329
337 void GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const;
338
344 double Levenshtein( const EDA_TEXT& aOther ) const;
345
346
347 double Similarity( const EDA_TEXT& aOther ) const;
348
357 virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
358
359 virtual EDA_ANGLE GetDrawRotation() const { return GetTextAngle(); }
360 virtual VECTOR2I GetDrawPos() const { return GetTextPos(); }
361
362 virtual void ClearRenderCache();
363 virtual void ClearBoundingBoxCache();
364
365 std::vector<std::unique_ptr<KIFONT::GLYPH>>*
366 GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
367 const VECTOR2I& aOffset = { 0, 0 } ) const;
368
369 // Support for reading the cache from disk.
370 void SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont,
371 const EDA_ANGLE& aAngle, const VECTOR2I& aOffset );
372 void AddRenderCacheGlyph( const SHAPE_POLY_SET& aPoly );
373
374 int Compare( const EDA_TEXT* aOther ) const;
375
376 bool operator==( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) == 0; }
377 bool operator<( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) < 0; }
378 bool operator>( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) > 0; }
379
380 virtual bool HasHyperlink() const { return !m_hyperlink.IsEmpty(); }
381 wxString GetHyperlink() const { return m_hyperlink; }
382 void SetHyperlink( wxString aLink ) { m_hyperlink = aLink; }
383 void RemoveHyperlink() { m_hyperlink = wxEmptyString; }
384
391 static bool ValidateHyperlink( const wxString& aURL );
392
401 static bool IsGotoPageHref( const wxString& aHref, wxString* aDestination = nullptr );
402
409 static wxString GotoPageHref( const wxString& aDestination );
410
411protected:
412 virtual KIFONT::FONT* getDrawFont() const;
413
414 virtual const KIFONT::METRICS& getFontMetrics() const;
415
416 virtual void cacheShownText();
417
427 void printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
428 const COLOR4D& aColor, OUTLINE_MODE aFillMode, const wxString& aText,
429 const VECTOR2I& aPos );
430
431protected:
435 wxString m_hyperlink;
436
437private:
438 wxString m_text;
439 wxString m_shown_text; // Cache of unescaped text for efficient access
441
442 std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
443
444 mutable wxString m_render_cache_text;
448 mutable std::vector<std::unique_ptr<KIFONT::GLYPH>> m_render_cache;
449
454
457};
458
459
460extern std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aAttributes );
461
462
463template<>
464struct std::hash<EDA_TEXT>
465{
466 std::size_t operator()( const EDA_TEXT& aText ) const
467 {
468 return hash_val( aText.GetText(), aText.GetAttributes(), aText.GetTextPos().x,
469 aText.GetTextPos().y );
470 }
471};
472
473#endif // EDA_TEXT_H_
BOX2< VECTOR2I > BOX2I
Definition: box2.h:922
double AsDegrees() const
Definition: eda_angle.h:113
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:79
int GetTextHeight() const
Definition: eda_text.h:247
void SetTextColor(const COLOR4D &aColor)
Definition: eda_text.h:249
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:253
COLOR4D GetTextColor() const
Definition: eda_text.h:250
wxString GetTextStyleName() const
Definition: eda_text.cpp:862
VECTOR2I m_pos
Definition: eda_text.h:456
wxString m_text
Definition: eda_text.h:438
bool IsDefaultFormatting() const
Definition: eda_text.cpp:936
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
Definition: eda_text.cpp:1198
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_render_cache
Definition: eda_text.h:448
wxString GetFontName() const
Definition: eda_text.cpp:883
virtual ~EDA_TEXT()
Definition: eda_text.cpp:146
bool IsItalic() const
Definition: eda_text.h:152
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:130
BOX2I m_bounding_box_cache
Definition: eda_text.h:453
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:419
bool IsMultilineAllowed() const
Definition: eda_text.h:180
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:94
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:610
bool IsKeepUpright() const
Definition: eda_text.h:189
virtual bool IsVisible() const
Definition: eda_text.h:170
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:464
void SetTextX(int aX)
Definition: eda_text.cpp:470
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:440
KIFONT::FONT * GetFont() const
Definition: eda_text.h:230
void SetAttributes(const TEXT_ATTRIBUTES &aTextAttrs)
Definition: eda_text.h:213
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition: eda_text.cpp:337
wxString m_shown_text
Definition: eda_text.h:439
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:951
int GetFontIndex() const
Definition: eda_text.cpp:892
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:616
void SetMirrored(bool isMirrored)
Definition: eda_text.cpp:297
void SetTextY(int aY)
Definition: eda_text.cpp:476
int m_bounding_box_cache_line
Definition: eda_text.h:452
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:557
const KIFONT::FONT * m_render_cache_font
Definition: eda_text.h:445
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:359
virtual VECTOR2I GetDrawPos() const
Definition: eda_text.h:360
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition: eda_text.cpp:151
wxString m_hyperlink
A hyperlink URL.
Definition: eda_text.h:435
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:837
int GetTextWidth() const
Definition: eda_text.h:244
virtual bool HasHyperlink() const
Definition: eda_text.h:380
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:321
wxString GetHyperlink() const
Definition: eda_text.h:381
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:482
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:183
void SetTextWidth(int aWidth)
Definition: eda_text.cpp:442
void SetBoldFlag(bool aBold)
Set only the italic flag, without changing the font.
Definition: eda_text.cpp:282
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:390
void SetupRenderCache(const wxString &aResolvedText, const KIFONT::FONT *aFont, const EDA_ANGLE &aAngle, const VECTOR2I &aOffset)
Definition: eda_text.cpp:592
int Compare(const EDA_TEXT *aOther) const
Definition: eda_text.cpp:1102
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition: eda_text.h:442
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition: eda_text.h:113
bool operator>(const EDA_TEXT &aRhs) const
Definition: eda_text.h:378
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:290
bool m_bounding_box_cache_valid
Definition: eda_text.h:450
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:793
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
Definition: eda_text.cpp:1204
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:550
virtual KIFONT::FONT * getDrawFont() const
Definition: eda_text.cpp:527
double GetLineSpacing() const
Definition: eda_text.h:238
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1182
VECTOR2I m_render_cache_offset
Definition: eda_text.h:447
void SetLineSpacing(double aLineSpacing)
Definition: eda_text.cpp:411
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition: eda_text.cpp:603
void Empty()
Definition: eda_text.cpp:501
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.cpp:196
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:750
void FlipHJustify()
Definition: eda_text.h:191
void SetTextHeight(int aHeight)
Definition: eda_text.cpp:453
virtual void cacheShownText()
Definition: eda_text.cpp:509
EDA_ANGLE m_render_cache_angle
Definition: eda_text.h:446
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:64
virtual void ClearRenderCache()
Definition: eda_text.cpp:544
void RemoveHyperlink()
Definition: eda_text.h:383
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:214
void SetBold(bool aBold)
Set the text to be bold - this will also update the font if needed.
Definition: eda_text.cpp:240
static bool ValidateHyperlink(const wxString &aURL)
Check if aURL is a valid hyperlink.
Definition: eda_text.cpp:1126
VECTOR2I m_bounding_box_cache_pos
Definition: eda_text.h:451
void SetItalicFlag(bool aItalic)
Set only the italic flag, without changing the font.
Definition: eda_text.cpp:232
bool IsMirrored() const
Definition: eda_text.h:173
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:369
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition: eda_text.cpp:356
wxString m_render_cache_text
Definition: eda_text.h:444
double GetTextAngleDegrees() const
Definition: eda_text.h:137
virtual const KIFONT::METRICS & getFontMetrics() const
Definition: eda_text.cpp:538
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:1023
bool operator<(const EDA_TEXT &aRhs) const
Definition: eda_text.h:377
bool IsBold() const
Definition: eda_text.h:167
void SetTextAngleDegrees(double aOrientation)
Definition: eda_text.h:133
void SetHyperlink(wxString aLink)
Definition: eda_text.h:382
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:78
void SetKeepUpright(bool aKeepUpright)
Definition: eda_text.cpp:329
void CopyText(const EDA_TEXT &aSrc)
Definition: eda_text.cpp:189
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:186
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:105
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:182
double Levenshtein(const EDA_TEXT &aOther) const
Return the levenstein distance between two texts.
Definition: eda_text.cpp:1136
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:204
void SetFontIndex(int aIdx)
Definition: eda_text.cpp:913
int GetTextThickness() const
Definition: eda_text.h:122
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:769
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition: eda_text.cpp:212
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:349
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:376
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.cpp:305
void SetFont(KIFONT::FONT *aFont)
Definition: eda_text.cpp:403
VECTOR2I GetTextSize() const
Definition: eda_text.h:241
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:313
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:455
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:401
@ DEGREES_T
Definition: eda_angle.h:31
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1210
static const bool FILLED
Definition: gr_basic.cpp:30
static constexpr 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:466
GR_TEXT_H_ALIGN_T
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
GR_TEXT_V_ALIGN_T