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 <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 void SetUnresolvedFontName( const wxString& aFontName ) { m_unresolvedFontName = aFontName; }
237 bool ResolveFont( const std::vector<wxString>* aEmbeddedFonts );
238
239 wxString GetFontName() const;
240
241 void SetFontIndex( int aIdx );
242 int GetFontIndex() const;
243
244 void SetLineSpacing( double aLineSpacing );
245 double GetLineSpacing() const { return m_attributes.m_LineSpacing; }
246
247 void SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize = true );
249
250 void SetTextWidth( int aWidth );
251 int GetTextWidth() const { return m_attributes.m_Size.x; }
252
253 void SetTextHeight( int aHeight );
254 int GetTextHeight() const { return m_attributes.m_Size.y; }
255
256 void SetTextColor( const COLOR4D& aColor ) { m_attributes.m_Color = aColor; }
258
259 void SetTextPos( const VECTOR2I& aPoint );
260 const VECTOR2I& GetTextPos() const { return m_pos; }
261
262 void SetTextX( int aX );
263 void SetTextY( int aY );
264
265 void Offset( const VECTOR2I& aOffset );
266
267 void Empty();
268
269 static GR_TEXT_H_ALIGN_T MapHorizJustify( int aHorizJustify );
270 static GR_TEXT_V_ALIGN_T MapVertJustify( int aVertJustify );
271
280 void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, const COLOR4D& aColor,
281 OUTLINE_MODE aDisplay_mode = FILLED );
282
289 std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true,
290 const BOX2I& aBBox = BOX2I(),
291 const EDA_ANGLE& aAngle = ANGLE_0 ) const;
292
300 virtual bool TextHitTest( const VECTOR2I& aPoint, int aAccuracy = 0 ) const;
301
310 virtual bool TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy = 0 ) const;
311
321 BOX2I GetTextBox( int aLine = -1 ) const;
322
330 int GetInterline() const;
331
335 wxString GetTextStyleName() const;
336
344 void GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const;
345
351 double Levenshtein( const EDA_TEXT& aOther ) const;
352
353
354 double Similarity( const EDA_TEXT& aOther ) const;
355
363 virtual void Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const;
364
365 virtual EDA_ANGLE GetDrawRotation() const { return GetTextAngle(); }
366 virtual VECTOR2I GetDrawPos() const { return GetTextPos(); }
367
368 virtual void ClearRenderCache();
369 virtual void ClearBoundingBoxCache();
370
371 std::vector<std::unique_ptr<KIFONT::GLYPH>>*
372 GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
373 const VECTOR2I& aOffset = { 0, 0 } ) const;
374
375 // Support for reading the cache from disk.
376 void SetupRenderCache( const wxString& aResolvedText, const KIFONT::FONT* aFont,
377 const EDA_ANGLE& aAngle, const VECTOR2I& aOffset );
378 void AddRenderCacheGlyph( const SHAPE_POLY_SET& aPoly );
379
380 int Compare( const EDA_TEXT* aOther ) const;
381
382 bool operator==( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) == 0; }
383 bool operator<( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) < 0; }
384 bool operator>( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) > 0; }
385
386 virtual bool HasHyperlink() const { return !m_hyperlink.IsEmpty(); }
387 wxString GetHyperlink() const { return m_hyperlink; }
388 void SetHyperlink( wxString aLink ) { m_hyperlink = aLink; }
389 void RemoveHyperlink() { m_hyperlink = wxEmptyString; }
390
397 static bool ValidateHyperlink( const wxString& aURL );
398
407 static bool IsGotoPageHref( const wxString& aHref, wxString* aDestination = nullptr );
408
415 static wxString GotoPageHref( const wxString& aDestination );
416
417protected:
418 virtual KIFONT::FONT* getDrawFont() const;
419
420 virtual const KIFONT::METRICS& getFontMetrics() const;
421
422 virtual void cacheShownText();
423
433 void printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
434 const COLOR4D& aColor, OUTLINE_MODE aFillMode, const wxString& aText,
435 const VECTOR2I& aPos );
436
437protected:
441 wxString m_hyperlink;
442
443private:
444 wxString m_text;
445 wxString m_shown_text; // Cache of unescaped text for efficient access
447
448 std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
449
450 mutable wxString m_render_cache_text;
454 mutable std::vector<std::unique_ptr<KIFONT::GLYPH>> m_render_cache;
455
457 {
460 };
461
462 mutable std::map<int, BBOX_CACHE_ENTRY> m_bbox_cache;
463
467};
468
469
470extern std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aAttributes );
471
472
473template<>
474struct std::hash<EDA_TEXT>
475{
476 std::size_t operator()( const EDA_TEXT& aText ) const
477 {
478 return hash_val( aText.GetText(), aText.GetAttributes(), aText.GetTextPos().x,
479 aText.GetTextPos().y );
480 }
481};
482
483#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:254
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: eda_text.cpp:184
void SetTextColor(const COLOR4D &aColor)
Definition: eda_text.h:256
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:260
COLOR4D GetTextColor() const
Definition: eda_text.h:257
wxString GetTextStyleName() const
Definition: eda_text.cpp:963
VECTOR2I m_pos
Definition: eda_text.h:466
wxString m_text
Definition: eda_text.h:444
std::map< int, BBOX_CACHE_ENTRY > m_bbox_cache
Definition: eda_text.h:462
bool IsDefaultFormatting() const
Definition: eda_text.cpp:1037
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
Definition: eda_text.cpp:1299
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_render_cache
Definition: eda_text.h:454
wxString GetFontName() const
Definition: eda_text.cpp:984
virtual ~EDA_TEXT()
Definition: eda_text.cpp:147
bool IsItalic() const
Definition: eda_text.h:156
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:134
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:524
void SetUnresolvedFontName(const wxString &aFontName)
Definition: eda_text.h:236
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:715
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:569
void SetTextX(int aX)
Definition: eda_text.cpp:575
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:446
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: eda_text.cpp:220
KIFONT::FONT * GetFont() const
Definition: eda_text.h:234
void SetAttributes(const TEXT_ATTRIBUTES &aTextAttrs)
Definition: eda_text.h:217
bool ResolveFont(const std::vector< wxString > *aEmbeddedFonts)
Definition: eda_text.cpp:498
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:445
int GetFontIndex() const
Definition: eda_text.cpp:993
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:721
void SetMirrored(bool isMirrored)
Definition: eda_text.cpp:384
void SetTextY(int aY)
Definition: eda_text.cpp:581
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:662
const KIFONT::FONT * m_render_cache_font
Definition: eda_text.h:451
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:365
wxString m_unresolvedFontName
Definition: eda_text.h:465
virtual VECTOR2I GetDrawPos() const
Definition: eda_text.h:366
EDA_TEXT & operator=(const EDA_TEXT &aItem)
Definition: eda_text.cpp:152
wxString m_hyperlink
A hyperlink URL.
Definition: eda_text.h:441
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:938
int GetTextWidth() const
Definition: eda_text.h:251
virtual bool HasHyperlink() const
Definition: eda_text.h:386
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:408
wxString GetHyperlink() const
Definition: eda_text.h:387
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:587
virtual void Format(OUTPUTFORMATTER *aFormatter, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:1052
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:187
void SetTextWidth(int aWidth)
Definition: eda_text.cpp:547
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:697
int Compare(const EDA_TEXT *aOther) const
Definition: eda_text.cpp:1203
std::reference_wrapper< const EDA_IU_SCALE > m_IuScale
Definition: eda_text.h:448
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:384
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:377
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:894
static wxString GotoPageHref(const wxString &aDestination)
Generate a href to a page in the current schematic.
Definition: eda_text.cpp:1305
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:655
virtual KIFONT::FONT * getDrawFont() const
Definition: eda_text.cpp:632
double GetLineSpacing() const
Definition: eda_text.h:245
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1283
VECTOR2I m_render_cache_offset
Definition: eda_text.h:453
void SetLineSpacing(double aLineSpacing)
Definition: eda_text.cpp:516
void AddRenderCacheGlyph(const SHAPE_POLY_SET &aPoly)
Definition: eda_text.cpp:708
void Empty()
Definition: eda_text.cpp:606
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.cpp:282
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:851
void FlipHJustify()
Definition: eda_text.h:195
void SetTextHeight(int aHeight)
Definition: eda_text.cpp:558
virtual void cacheShownText()
Definition: eda_text.cpp:614
EDA_ANGLE m_render_cache_angle
Definition: eda_text.h:452
static GR_TEXT_H_ALIGN_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:67
virtual void ClearRenderCache()
Definition: eda_text.cpp:649
void RemoveHyperlink()
Definition: eda_text.h:389
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:326
static bool ValidateHyperlink(const wxString &aURL)
Check if aURL is a valid hyperlink.
Definition: eda_text.cpp:1227
void SetItalicFlag(bool aItalic)
Set only the italic flag, without changing the font.
Definition: eda_text.cpp:318
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:450
double GetTextAngleDegrees() const
Definition: eda_text.h:141
virtual const KIFONT::METRICS & getFontMetrics() const
Definition: eda_text.cpp:643
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:1124
bool operator<(const EDA_TEXT &aRhs) const
Definition: eda_text.h:383
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:388
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:275
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:268
double Levenshtein(const EDA_TEXT &aOther) const
Return the levenstein distance between two texts.
Definition: eda_text.cpp:1237
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:290
void SetFontIndex(int aIdx)
Definition: eda_text.cpp:1014
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:870
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition: eda_text.cpp:298
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:436
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:382
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:248
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:400
TEXT_ATTRIBUTES m_attributes
Definition: eda_text.h:464
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:1311
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
Definition: eda_text.h:457
VECTOR2I m_pos
Definition: eda_text.h:458
BOX2I m_bbox
Definition: eda_text.h:459
std::size_t operator()(const EDA_TEXT &aText) const
Definition: eda_text.h:476
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.