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 The 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 <eda_search_data.h>
32#include <font/glyph.h>
34#include <api/serializable.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
78class EDA_TEXT : public SERIALIZABLE
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
89 void Serialize( google::protobuf::Any &aContainer ) const override;
90 bool Deserialize( const google::protobuf::Any &aContainer ) override;
91
97 virtual const wxString& GetText() const { return m_text; }
98
108 virtual wxString GetShownText( bool aAllowExtraText, int aDepth = 0 ) const
109 {
110 return m_shown_text;
111 }
112
117
118 virtual void SetText( const wxString& aText );
119
124 void SetTextThickness( int aWidth );
126
128 {
129 if( GetAutoThickness() )
131 else
132 return GetTextThickness();
133 }
134
135 void SetAutoThickness( bool aAuto );
136 bool GetAutoThickness() const { return GetTextThickness() == 0; };
137
141 int GetEffectiveTextPenWidth( int aDefaultPenWidth = 0 ) const;
142
143 virtual void SetTextAngle( const EDA_ANGLE& aAngle );
144 const EDA_ANGLE& GetTextAngle() const { return m_attributes.m_Angle; }
145
146 // For property system:
147 void SetTextAngleDegrees( double aOrientation )
148 {
149 SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
150 }
152
158 void SetItalic( bool aItalic );
159
165 void SetItalicFlag( bool aItalic );
166 bool IsItalic() const { return m_attributes.m_Italic; }
167
173 void SetBold( bool aBold );
174
180 void SetBoldFlag( bool aBold );
181 bool IsBold() const { return m_attributes.m_Bold; }
182
183 virtual void SetVisible( bool aVisible );
184 virtual bool IsVisible() const { return m_visible; }
185
186 void SetMirrored( bool isMirrored );
187 bool IsMirrored() const { return m_attributes.m_Mirrored; }
188
193 void SetMultilineAllowed( bool aAllow );
195
198
199 void SetVertJustify( GR_TEXT_V_ALIGN_T aType );
201
202 void SetKeepUpright( bool aKeepUpright );
203 bool IsKeepUpright() const { return m_attributes.m_KeepUpright; }
204
206 {
211 }
212
216 void SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition = true );
217
221 void SwapAttributes( EDA_TEXT& aTradingPartner );
222
223 void SwapText( EDA_TEXT& aTradingPartner );
224
225 void CopyText( const EDA_TEXT& aSrc );
226
227 void SetAttributes( const TEXT_ATTRIBUTES& aTextAttrs ) { m_attributes = aTextAttrs; }
228 const TEXT_ATTRIBUTES& GetAttributes() const { return m_attributes; }
229
239 bool Replace( const EDA_SEARCH_DATA& aSearchData );
240
241 bool IsDefaultFormatting() const;
242
243 void SetFont( KIFONT::FONT* aFont );
245
246 void SetUnresolvedFontName( const wxString& aFontName ) { m_unresolvedFontName = aFontName; }
247 bool ResolveFont( const std::vector<wxString>* aEmbeddedFonts );
248
249 wxString GetFontName() const;
250
251 void SetFontIndex( int aIdx );
252 int GetFontIndex() const;
253
254 void SetLineSpacing( double aLineSpacing );
255 double GetLineSpacing() const { return m_attributes.m_LineSpacing; }
256
257 void SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize = true );
259
260 void SetTextWidth( int aWidth );
261 int GetTextWidth() const { return m_attributes.m_Size.x; }
262
263 void SetTextHeight( int aHeight );
264 int GetTextHeight() const { return m_attributes.m_Size.y; }
265
266 void SetTextColor( const COLOR4D& aColor ) { m_attributes.m_Color = aColor; }
268
269 void SetTextPos( const VECTOR2I& aPoint );
270 const VECTOR2I& GetTextPos() const { return m_pos; }
271
272 void SetTextX( int aX );
273 void SetTextY( int aY );
274
275 void Offset( const VECTOR2I& aOffset );
276
277 void Empty();
278
279 static GR_TEXT_H_ALIGN_T MapHorizJustify( int aHorizJustify );
280 static GR_TEXT_V_ALIGN_T MapVertJustify( int aVertJustify );
281
289 void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor );
290
297 std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true,
298 const BOX2I& aBBox = BOX2I(),
299 const EDA_ANGLE& aAngle = ANGLE_0 ) const;
300
308 virtual bool TextHitTest( const VECTOR2I& aPoint, int aAccuracy = 0 ) const;
309
318 virtual bool TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy = 0 ) const;
319
329 BOX2I GetTextBox( int aLine = -1 ) const;
330
338 int GetInterline() const;
339
343 wxString GetTextStyleName() const;
344
352 void GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const;
353
359 double Levenshtein( const EDA_TEXT& aOther ) const;
360
361
362 double Similarity( const EDA_TEXT& aOther ) const;
363
371 virtual void Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const;
372
373 virtual EDA_ANGLE GetDrawRotation() const { return GetTextAngle(); }
374 virtual VECTOR2I GetDrawPos() const { return GetTextPos(); }
375
376 virtual void ClearRenderCache();
377 virtual void ClearBoundingBoxCache();
378
379 std::vector<std::unique_ptr<KIFONT::GLYPH>>*
380 GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
381 const VECTOR2I& aOffset = { 0, 0 } ) const;
382
383 // Support for reading the cache from disk.
384 void SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont,
385 const EDA_ANGLE& aAngle, const VECTOR2I& aOffset );
386 void AddRenderCacheGlyph( const SHAPE_POLY_SET& aPoly );
387
388 int Compare( const EDA_TEXT* aOther ) const;
389
390 bool operator==( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) == 0; }
391 bool operator<( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) < 0; }
392 bool operator>( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) > 0; }
393
394 virtual bool HasHyperlink() const { return !m_hyperlink.IsEmpty(); }
395 wxString GetHyperlink() const { return m_hyperlink; }
396 void SetHyperlink( wxString aLink ) { m_hyperlink = aLink; }
397 void RemoveHyperlink() { m_hyperlink = wxEmptyString; }
398
405 static bool ValidateHyperlink( const wxString& aURL );
406
415 static bool IsGotoPageHref( const wxString& aHref, wxString* aDestination = nullptr );
416
423 static wxString GotoPageHref( const wxString& aDestination );
424
425protected:
426 virtual KIFONT::FONT* getDrawFont() const;
427
428 virtual const KIFONT::METRICS& getFontMetrics() const;
429
430 virtual void cacheShownText();
431
440 void printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
441 const COLOR4D& aColor, const wxString& aText, const VECTOR2I& aPos );
442
443protected:
447 wxString m_hyperlink;
448
449private:
450 wxString m_text;
451 wxString m_shown_text; // Cache of unescaped text for efficient access
453
454 std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
455
456 mutable wxString m_render_cache_text;
460 mutable std::vector<std::unique_ptr<KIFONT::GLYPH>> m_render_cache;
461
463 {
466 };
467
468 mutable std::map<int, BBOX_CACHE_ENTRY> m_bbox_cache;
469
473 bool m_visible; // For SCH_FIELDs and PCB_FIELDs
474};
475
476
477extern std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aAttributes );
478
479
480template<>
481struct std::hash<EDA_TEXT>
482{
483 std::size_t operator()( const EDA_TEXT& aText ) const
484 {
485 return hash_val( aText.GetText(), aText.GetAttributes(), aText.GetTextPos().x,
486 aText.GetTextPos().y );
487 }
488};
489
490#endif // EDA_TEXT_H_
BOX2< VECTOR2I > BOX2I
Definition: box2.h:922
double AsDegrees() const
Definition: eda_angle.h:116
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:264
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: eda_text.cpp:187
void SetTextColor(const COLOR4D &aColor)
Definition: eda_text.h:266
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:270
COLOR4D GetTextColor() const
Definition: eda_text.h:267
wxString GetTextStyleName() const
Definition: eda_text.cpp:966
VECTOR2I m_pos
Definition: eda_text.h:472
wxString m_text
Definition: eda_text.h:450
std::map< int, BBOX_CACHE_ENTRY > m_bbox_cache
Definition: eda_text.h:468
bool IsDefaultFormatting() const
Definition: eda_text.cpp:1040
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
Definition: eda_text.cpp:1296
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_render_cache
Definition: eda_text.h:460
wxString GetFontName() const
Definition: eda_text.cpp:987
virtual ~EDA_TEXT()
Definition: eda_text.cpp:149
bool IsItalic() const
Definition: eda_text.h:166
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:144
bool m_visible
Definition: eda_text.h:473
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:533
void SetUnresolvedFontName(const wxString &aFontName)
Definition: eda_text.h:246
bool IsMultilineAllowed() const
Definition: eda_text.h:194
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:97
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:724
bool IsKeepUpright() const
Definition: eda_text.h:203
virtual bool IsVisible() const
Definition: eda_text.h:184
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:578
void SetTextX(int aX)
Definition: eda_text.cpp:584
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:452
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: eda_text.cpp:223
KIFONT::FONT * GetFont() const
Definition: eda_text.h:244
void SetAttributes(const TEXT_ATTRIBUTES &aTextAttrs)
Definition: eda_text.h:227
bool ResolveFont(const std::vector< wxString > *aEmbeddedFonts)
Definition: eda_text.cpp:507
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition: eda_text.cpp:433
wxString m_shown_text
Definition: eda_text.h:451
int GetFontIndex() const
Definition: eda_text.cpp:996
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:730
void SetMirrored(bool isMirrored)
Definition: eda_text.cpp:393
void SetTextY(int aY)
Definition: eda_text.cpp:590
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:671
const KIFONT::FONT * m_render_cache_font
Definition: eda_text.h:457
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:373
wxString m_unresolvedFontName
Definition: eda_text.h:471
virtual VECTOR2I GetDrawPos() const
Definition: eda_text.h:374
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition: eda_text.cpp:154
wxString m_hyperlink
A hyperlink URL.
Definition: eda_text.h:447
int GetTextWidth() const
Definition: eda_text.h:261
virtual bool HasHyperlink() const
Definition: eda_text.h:394
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:417
wxString GetHyperlink() const
Definition: eda_text.h:395
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:596
virtual void Format(OUTPUTFORMATTER *aFormatter, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:1054
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:197
void SetTextWidth(int aWidth)
Definition: eda_text.cpp:556
void SetBoldFlag(bool aBold)
Set only the bold flag, without changing the font.
Definition: eda_text.cpp:378
bool Replace(const EDA_SEARCH_DATA &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:486
void SetupRenderCache(const wxString &aResolvedText, const KIFONT::FONT *aFont, const EDA_ANGLE &aAngle, const VECTOR2I &aOffset)
Definition: eda_text.cpp:706
int Compare(const EDA_TEXT *aOther) const
Definition: eda_text.cpp:1200
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition: eda_text.h:454
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition: eda_text.h:116
bool operator>(const EDA_TEXT &aRhs) const
Definition: eda_text.h:392
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:386
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:901
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
Definition: eda_text.cpp:1302
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:664
virtual KIFONT::FONT * getDrawFont() const
Definition: eda_text.cpp:641
bool GetAutoThickness() const
Definition: eda_text.h:136
double GetLineSpacing() const
Definition: eda_text.h:255
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1280
VECTOR2I m_render_cache_offset
Definition: eda_text.h:459
void SetLineSpacing(double aLineSpacing)
Definition: eda_text.cpp:525
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition: eda_text.cpp:717
void Empty()
Definition: eda_text.cpp:615
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.cpp:284
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:860
void FlipHJustify()
Definition: eda_text.h:205
void SetTextHeight(int aHeight)
Definition: eda_text.cpp:567
virtual void cacheShownText()
Definition: eda_text.cpp:623
EDA_ANGLE m_render_cache_angle
Definition: eda_text.h:458
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:67
virtual void ClearRenderCache()
Definition: eda_text.cpp:658
void RemoveHyperlink()
Definition: eda_text.h:397
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:228
void SetBold(bool aBold)
Set the text to be bold - this will also update the font if needed.
Definition: eda_text.cpp:335
static bool ValidateHyperlink(const wxString &aURL)
Check if aURL is a valid hyperlink.
Definition: eda_text.cpp:1224
void SetItalicFlag(bool aItalic)
Set only the italic flag, without changing the font.
Definition: eda_text.cpp:327
void SetAutoThickness(bool aAuto)
Definition: eda_text.cpp:292
bool IsMirrored() const
Definition: eda_text.h:187
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:465
void SwapAttributes(EDA_TEXT &aTradingPartner)
Swap the text attributes of the two involved instances.
Definition: eda_text.cpp:452
wxString m_render_cache_text
Definition: eda_text.h:456
void Print(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset, const COLOR4D &aColor)
Print this text object to the device context aDC.
Definition: eda_text.cpp:879
double GetTextAngleDegrees() const
Definition: eda_text.h:151
virtual const KIFONT::METRICS & getFontMetrics() const
Definition: eda_text.cpp:652
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:1121
bool operator<(const EDA_TEXT &aRhs) const
Definition: eda_text.h:391
bool IsBold() const
Definition: eda_text.h:181
void SetTextAngleDegrees(double aOrientation)
Definition: eda_text.h:147
void SetHyperlink(wxString aLink)
Definition: eda_text.h:396
static GR_TEXT_V_ALIGN_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:81
void SetKeepUpright(bool aKeepUpright)
Definition: eda_text.cpp:425
void CopyText(const EDA_TEXT &aSrc)
Definition: eda_text.cpp:277
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:200
int GetTextThicknessProperty() const
Definition: eda_text.h:127
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:108
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:270
double Levenshtein(const EDA_TEXT &aOther) const
Return the levenstein distance between two texts.
Definition: eda_text.cpp:1234
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:299
void SetFontIndex(int aIdx)
Definition: eda_text.cpp:1017
int GetTextThickness() const
Definition: eda_text.h:125
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition: eda_text.cpp:307
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:445
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:390
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.cpp:401
void SetFont(KIFONT::FONT *aFont)
Definition: eda_text.cpp:499
void printOneLineOfText(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset, const COLOR4D &aColor, const wxString &aText, const VECTOR2I &aPos)
Print each line of this EDA_TEXT.
Definition: eda_text.cpp:945
VECTOR2I GetTextSize() const
Definition: eda_text.h:258
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:409
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:470
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:404
@ DEGREES_T
Definition: eda_angle.h:31
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1308
static constexpr std::size_t hash_val(const Types &... args)
Definition: hash.h:51
The Cairo implementation of the graphics abstraction layer.
Definition: eda_group.h:33
Definition: eda_text.h:463
VECTOR2I m_pos
Definition: eda_text.h:464
BOX2I m_bbox
Definition: eda_text.h:465
std::size_t operator()(const EDA_TEXT &aText) const
Definition: eda_text.h:483
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.