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#include <api/serializable.h>
36
37
38class OUTPUTFORMATTER;
39class SHAPE_COMPOUND;
40class SHAPE_POLY_SET;
41
42
43// These are only here for algorithmic safety, not to tell the user what to do.
44// PL_EDITOR has the least resolution (its internal units are microns), so the min size is chosen
45// to yield 1 in PL_EDITOR.
46// The max size chosen is somewhat arbitrary, but no one has complained yet.
47#define TEXT_MIN_SIZE_MM 0.001
48#define TEXT_MAX_SIZE_MM 250.0
49
50
51namespace KIGFX
52{
53 class RENDER_SETTINGS;
54 class COLOR4D;
55}
56
57namespace KIFONT
58{
59 class METRICS;
60 }
61
63using KIGFX::COLOR4D;
64
65
70#define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
71
72
79class EDA_TEXT : public SERIALIZABLE
80{
81public:
82 EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText = wxEmptyString );
83
84 EDA_TEXT( const EDA_TEXT& aText );
85
86 virtual ~EDA_TEXT();
87
88 EDA_TEXT& operator=( const EDA_TEXT& aItem );
89
90 void Serialize( google::protobuf::Any &aContainer ) const override;
91 bool Deserialize( const google::protobuf::Any &aContainer ) override;
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
148 void SetItalic( bool aItalic );
149
155 void SetItalicFlag( bool aItalic );
156 bool IsItalic() const { return m_attributes.m_Italic; }
157
163 void SetBold( bool aBold );
164
170 void SetBoldFlag( bool aBold );
171 bool IsBold() const { return m_attributes.m_Bold; }
172
173 virtual void SetVisible( bool aVisible );
174 virtual bool IsVisible() const { return m_attributes.m_Visible; }
175
176 void SetMirrored( bool isMirrored );
177 bool IsMirrored() const { return m_attributes.m_Mirrored; }
178
183 void SetMultilineAllowed( bool aAllow );
185
188
189 void SetVertJustify( GR_TEXT_V_ALIGN_T aType );
191
192 void SetKeepUpright( bool aKeepUpright );
193 bool IsKeepUpright() const { return m_attributes.m_KeepUpright; }
194
196 {
201 }
202
206 void SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition = true );
207
211 void SwapAttributes( EDA_TEXT& aTradingPartner );
212
213 void SwapText( EDA_TEXT& aTradingPartner );
214
215 void CopyText( const EDA_TEXT& aSrc );
216
217 void SetAttributes( const TEXT_ATTRIBUTES& aTextAttrs ) { m_attributes = aTextAttrs; }
218 const TEXT_ATTRIBUTES& GetAttributes() const { return m_attributes; }
219
229 bool Replace( const EDA_SEARCH_DATA& aSearchData );
230
231 bool IsDefaultFormatting() const;
232
233 void SetFont( KIFONT::FONT* aFont );
235
236 wxString GetFontName() const;
237
238 void SetFontIndex( int aIdx );
239 int GetFontIndex() const;
240
241 void SetLineSpacing( double aLineSpacing );
242 double GetLineSpacing() const { return m_attributes.m_LineSpacing; }
243
244 void SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize = true );
246
247 void SetTextWidth( int aWidth );
248 int GetTextWidth() const { return m_attributes.m_Size.x; }
249
250 void SetTextHeight( int aHeight );
251 int GetTextHeight() const { return m_attributes.m_Size.y; }
252
253 void SetTextColor( const COLOR4D& aColor ) { m_attributes.m_Color = aColor; }
255
256 void SetTextPos( const VECTOR2I& aPoint );
257 const VECTOR2I& GetTextPos() const { return m_pos; }
258
259 void SetTextX( int aX );
260 void SetTextY( int aY );
261
262 void Offset( const VECTOR2I& aOffset );
263
264 void Empty();
265
266 static GR_TEXT_H_ALIGN_T MapHorizJustify( int aHorizJustify );
267 static GR_TEXT_V_ALIGN_T MapVertJustify( int aVertJustify );
268
277 void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor,
278 OUTLINE_MODE aDisplay_mode = FILLED );
279
286 std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true,
287 const BOX2I& aBBox = BOX2I(),
288 const EDA_ANGLE& aAngle = ANGLE_0 ) const;
289
297 virtual bool TextHitTest( const VECTOR2I& aPoint, int aAccuracy = 0 ) const;
298
307 virtual bool TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy = 0 ) const;
308
318 BOX2I GetTextBox( int aLine = -1 ) const;
319
327 int GetInterline() const;
328
332 wxString GetTextStyleName() const;
333
341 void GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const;
342
348 double Levenshtein( const EDA_TEXT& aOther ) const;
349
350
351 double Similarity( const EDA_TEXT& aOther ) const;
352
360 virtual void Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const;
361
362 virtual EDA_ANGLE GetDrawRotation() const { return GetTextAngle(); }
363 virtual VECTOR2I GetDrawPos() const { return GetTextPos(); }
364
365 virtual void ClearRenderCache();
366 virtual void ClearBoundingBoxCache();
367
368 std::vector<std::unique_ptr<KIFONT::GLYPH>>*
369 GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
370 const VECTOR2I& aOffset = { 0, 0 } ) const;
371
372 // Support for reading the cache from disk.
373 void SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont,
374 const EDA_ANGLE& aAngle, const VECTOR2I& aOffset );
375 void AddRenderCacheGlyph( const SHAPE_POLY_SET& aPoly );
376
377 int Compare( const EDA_TEXT* aOther ) const;
378
379 bool operator==( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) == 0; }
380 bool operator<( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) < 0; }
381 bool operator>( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) > 0; }
382
383 virtual bool HasHyperlink() const { return !m_hyperlink.IsEmpty(); }
384 wxString GetHyperlink() const { return m_hyperlink; }
385 void SetHyperlink( wxString aLink ) { m_hyperlink = aLink; }
386 void RemoveHyperlink() { m_hyperlink = wxEmptyString; }
387
394 static bool ValidateHyperlink( const wxString& aURL );
395
404 static bool IsGotoPageHref( const wxString& aHref, wxString* aDestination = nullptr );
405
412 static wxString GotoPageHref( const wxString& aDestination );
413
414protected:
415 virtual KIFONT::FONT* getDrawFont() const;
416
417 virtual const KIFONT::METRICS& getFontMetrics() const;
418
419 virtual void cacheShownText();
420
430 void printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
431 const COLOR4D& aColor, OUTLINE_MODE aFillMode, const wxString& aText,
432 const VECTOR2I& aPos );
433
434protected:
438 wxString m_hyperlink;
439
440private:
441 wxString m_text;
442 wxString m_shown_text; // Cache of unescaped text for efficient access
444
445 std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
446
447 mutable wxString m_render_cache_text;
451 mutable std::vector<std::unique_ptr<KIFONT::GLYPH>> m_render_cache;
452
457
460};
461
462
463extern std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aAttributes );
464
465
466template<>
467struct std::hash<EDA_TEXT>
468{
469 std::size_t operator()( const EDA_TEXT& aText ) const
470 {
471 return hash_val( aText.GetText(), aText.GetAttributes(), aText.GetTextPos().x,
472 aText.GetTextPos().y );
473 }
474};
475
476#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:80
int GetTextHeight() const
Definition: eda_text.h:251
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: eda_text.cpp:185
void SetTextColor(const COLOR4D &aColor)
Definition: eda_text.h:253
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:257
COLOR4D GetTextColor() const
Definition: eda_text.h:254
wxString GetTextStyleName() const
Definition: eda_text.cpp:949
VECTOR2I m_pos
Definition: eda_text.h:459
wxString m_text
Definition: eda_text.h:441
bool IsDefaultFormatting() const
Definition: eda_text.cpp:1023
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
Definition: eda_text.cpp:1285
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_render_cache
Definition: eda_text.h:451
wxString GetFontName() const
Definition: eda_text.cpp:970
virtual ~EDA_TEXT()
Definition: eda_text.cpp:149
bool IsItalic() const
Definition: eda_text.h:156
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:134
BOX2I m_bounding_box_cache
Definition: eda_text.h:456
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:506
bool IsMultilineAllowed() const
Definition: eda_text.h:184
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:697
bool IsKeepUpright() const
Definition: eda_text.h:193
virtual bool IsVisible() const
Definition: eda_text.h:174
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:551
void SetTextX(int aX)
Definition: eda_text.cpp:557
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:443
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: eda_text.cpp:221
KIFONT::FONT * GetFont() const
Definition: eda_text.h:234
void SetAttributes(const TEXT_ATTRIBUTES &aTextAttrs)
Definition: eda_text.h:217
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition: eda_text.cpp:424
wxString m_shown_text
Definition: eda_text.h:442
int GetFontIndex() const
Definition: eda_text.cpp:979
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:703
void SetMirrored(bool isMirrored)
Definition: eda_text.cpp:384
void SetTextY(int aY)
Definition: eda_text.cpp:563
int m_bounding_box_cache_line
Definition: eda_text.h:455
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:644
const KIFONT::FONT * m_render_cache_font
Definition: eda_text.h:448
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:362
virtual VECTOR2I GetDrawPos() const
Definition: eda_text.h:363
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition: eda_text.cpp:154
wxString m_hyperlink
A hyperlink URL.
Definition: eda_text.h:438
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:924
int GetTextWidth() const
Definition: eda_text.h:248
virtual bool HasHyperlink() const
Definition: eda_text.h:383
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:408
wxString GetHyperlink() const
Definition: eda_text.h:384
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:569
virtual void Format(OUTPUTFORMATTER *aFormatter, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:1038
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:187
void SetTextWidth(int aWidth)
Definition: eda_text.cpp:529
void SetBoldFlag(bool aBold)
Set only the italic flag, without changing the font.
Definition: eda_text.cpp:369
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:477
void SetupRenderCache(const wxString &aResolvedText, const KIFONT::FONT *aFont, const EDA_ANGLE &aAngle, const VECTOR2I &aOffset)
Definition: eda_text.cpp:679
int Compare(const EDA_TEXT *aOther) const
Definition: eda_text.cpp:1189
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition: eda_text.h:445
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:381
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:377
bool m_bounding_box_cache_valid
Definition: eda_text.h:453
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:880
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
Definition: eda_text.cpp:1291
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:637
virtual KIFONT::FONT * getDrawFont() const
Definition: eda_text.cpp:614
double GetLineSpacing() const
Definition: eda_text.h:242
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1269
VECTOR2I m_render_cache_offset
Definition: eda_text.h:450
void SetLineSpacing(double aLineSpacing)
Definition: eda_text.cpp:498
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition: eda_text.cpp:690
void Empty()
Definition: eda_text.cpp:588
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.cpp:283
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:837
void FlipHJustify()
Definition: eda_text.h:195
void SetTextHeight(int aHeight)
Definition: eda_text.cpp:540
virtual void cacheShownText()
Definition: eda_text.cpp:596
EDA_ANGLE m_render_cache_angle
Definition: eda_text.h:449
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:67
virtual void ClearRenderCache()
Definition: eda_text.cpp:631
void RemoveHyperlink()
Definition: eda_text.h:386
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:218
void SetBold(bool aBold)
Set the text to be bold - this will also update the font if needed.
Definition: eda_text.cpp:327
static bool ValidateHyperlink(const wxString &aURL)
Check if aURL is a valid hyperlink.
Definition: eda_text.cpp:1213
VECTOR2I m_bounding_box_cache_pos
Definition: eda_text.h:454
void SetItalicFlag(bool aItalic)
Set only the italic flag, without changing the font.
Definition: eda_text.cpp:319
bool IsMirrored() const
Definition: eda_text.h:177
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:456
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition: eda_text.cpp:443
wxString m_render_cache_text
Definition: eda_text.h:447
double GetTextAngleDegrees() const
Definition: eda_text.h:141
virtual const KIFONT::METRICS & getFontMetrics() const
Definition: eda_text.cpp:625
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:1110
bool operator<(const EDA_TEXT &aRhs) const
Definition: eda_text.h:380
bool IsBold() const
Definition: eda_text.h:171
void SetTextAngleDegrees(double aOrientation)
Definition: eda_text.h:137
void SetHyperlink(wxString aLink)
Definition: eda_text.h:385
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:81
void SetKeepUpright(bool aKeepUpright)
Definition: eda_text.cpp:416
void CopyText(const EDA_TEXT &aSrc)
Definition: eda_text.cpp:276
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:190
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:269
double Levenshtein(const EDA_TEXT &aOther) const
Return the levenstein distance between two texts.
Definition: eda_text.cpp:1223
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:291
void SetFontIndex(int aIdx)
Definition: eda_text.cpp:1000
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:856
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition: eda_text.cpp:299
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:436
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:379
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.cpp:392
void SetFont(KIFONT::FONT *aFont)
Definition: eda_text.cpp:490
VECTOR2I GetTextSize() const
Definition: eda_text.h:245
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:400
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:458
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
Interface for objects that can be serialized to Protobuf messages.
Definition: serializable.h:37
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:1297
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:469
GR_TEXT_H_ALIGN_T
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
GR_TEXT_V_ALIGN_T