KiCad PCB EDA Suite
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-2021 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_rect.h>
33 
34 class OUTPUTFORMATTER;
35 class SHAPE_COMPOUND;
36 class SHAPE_POLY_SET;
37 class wxFindReplaceData;
38 
39 
47 {
49  int m_error;
51 };
52 
53 
57 extern void addTextSegmToPoly( int x0, int y0, int xf, int yf, void* aData );
58 
59 
60 namespace KIGFX
61 {
62  class RENDER_SETTINGS;
63  class COLOR4D;
64 }
65 
67 using KIGFX::COLOR4D;
68 
71 #define TEXT_ANGLE_HORIZ 0
72 #define TEXT_ANGLE_VERT 900
73 
74 // part of the kicad_plugin.h family of defines.
75 // See kicad_plugin.h for the choice of the value
76 // When set when calling EDA_TEXT::Format, disable writing the "hide" keyword in save file
77 #define CTL_OMIT_HIDE (1 << 6)
78 
79 
80 // Graphic Text justify:
81 // Values -1,0,1 are used in computations, do not change them
86 };
87 
88 
93 };
94 
95 
101 #define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
102 #define DIM_ANCRE_TEXTE 2 // Anchor size for text
103 
104 
112 {
113  TEXT_EFFECTS( int aSetOfBits = 0 ) :
114  bits( aSetOfBits ),
117  penwidth( 0 ),
118  angle( 0.0 )
119  {}
120 
121  int bits;
122  signed char hjustify;
123  signed char vjustify;
124  wxSize size;
125  int penwidth;
126  double angle;
127  wxPoint pos;
128 
129  void Bit( int aBit, bool aValue ) { aValue ? bits |= (1<<aBit) : bits &= ~(1<<aBit); }
130  bool Bit( int aBit ) const { return bits & (1<<aBit); }
131 };
132 
133 
140 class EDA_TEXT
141 {
142 public:
143  EDA_TEXT( const wxString& text = wxEmptyString );
144 
145  EDA_TEXT( const EDA_TEXT& aText );
146 
147  virtual ~EDA_TEXT();
148 
154  virtual const wxString& GetText() const { return m_text; }
155 
162  virtual wxString GetShownText( int aDepth = 0 ) const { return m_shown_text; }
163 
167  wxString ShortenedShownText() const;
168 
173 
174  virtual void SetText( const wxString& aText );
175 
180  void SetTextThickness( int aWidth ) { m_e.penwidth = aWidth; };
181  int GetTextThickness() const { return m_e.penwidth; };
182 
186  int GetEffectiveTextPenWidth( int aDefaultWidth = 0 ) const;
187 
188  virtual void SetTextAngle( double aAngle )
189  {
190  // Higher level classes may be more restrictive than this by overloading
191  // SetTextAngle() or merely calling EDA_TEXT::SetTextAngle() after clamping
192  // aAngle before calling this lowest inline accessor.
193  m_e.angle = aAngle;
194  }
195  double GetTextAngle() const { return m_e.angle; }
196 
197  double GetTextAngleDegrees() const { return GetTextAngle() / 10.0; }
198  double GetTextAngleRadians() const { return GetTextAngle() * M_PI/1800; }
199 
200  void SetItalic( bool isItalic ) { m_e.Bit( TE_ITALIC, isItalic ); }
201  bool IsItalic() const { return m_e.Bit( TE_ITALIC ); }
202 
203  void SetBold( bool aBold ) { m_e.Bit( TE_BOLD, aBold); }
204  bool IsBold() const { return m_e.Bit( TE_BOLD ); }
205 
206  virtual void SetVisible( bool aVisible ) { m_e.Bit( TE_VISIBLE, aVisible ); }
207  virtual bool IsVisible() const { return m_e.Bit( TE_VISIBLE ); }
208 
209  void SetMirrored( bool isMirrored ) { m_e.Bit( TE_MIRROR, isMirrored ); }
210  bool IsMirrored() const { return m_e.Bit( TE_MIRROR ); }
211 
216  void SetMultilineAllowed( bool aAllow ) { m_e.Bit( TE_MULTILINE, aAllow ); }
217  bool IsMultilineAllowed() const { return m_e.Bit( TE_MULTILINE ); }
218 
221 
222  void SetHorizJustify( EDA_TEXT_HJUSTIFY_T aType ) { m_e.hjustify = aType; };
223  void SetVertJustify( EDA_TEXT_VJUSTIFY_T aType ) { m_e.vjustify = aType; };
224 
231  void SetEffects( const EDA_TEXT& aSrc );
232 
239  void SwapEffects( EDA_TEXT& aTradingPartner );
240 
241  void SwapText( EDA_TEXT& aTradingPartner );
242 
243  void CopyText( const EDA_TEXT& aSrc );
244 
254  bool Replace( const wxFindReplaceData& aSearchData );
255 
256  bool IsDefaultFormatting() const;
257 
258  void SetTextSize( const wxSize& aNewSize ) { m_e.size = aNewSize; }
259  const wxSize& GetTextSize() const { return m_e.size; }
260 
261  void SetTextWidth( int aWidth ) { m_e.size.x = aWidth; }
262  int GetTextWidth() const { return m_e.size.x; }
263 
264  void SetTextHeight( int aHeight ) { m_e.size.y = aHeight; }
265  int GetTextHeight() const { return m_e.size.y; }
266 
267  void SetTextPos( const wxPoint& aPoint ) { m_e.pos = aPoint; }
268  const wxPoint& GetTextPos() const { return m_e.pos; }
269 
270  void SetTextX( int aX ) { m_e.pos.x = aX; }
271  void SetTextY( int aY ) { m_e.pos.y = aY; }
272 
273  void Offset( const wxPoint& aOffset ) { m_e.pos += aOffset; }
274 
275  void Empty() { m_text.Empty(); }
276 
277  static EDA_TEXT_HJUSTIFY_T MapHorizJustify( int aHorizJustify );
278 
279  static EDA_TEXT_VJUSTIFY_T MapVertJustify( int aVertJustify );
280 
289  void Print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
290  const COLOR4D& aColor, OUTLINE_MODE aDisplay_mode = FILLED );
291 
298  std::vector<wxPoint> TransformToSegmentList() const;
299 
312  int aClearanceValue ) const;
313 
314  std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape() const;
315 
323  virtual bool TextHitTest( const wxPoint& aPoint, int aAccuracy = 0 ) const;
324 
333  virtual bool TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy = 0 ) const;
334 
341  int LenSize( const wxString& aLine, int aThickness ) const;
342 
353  EDA_RECT GetTextBox( int aLine = -1, bool aInvertY = false ) const;
354 
362  int GetInterline() const;
363 
367  wxString GetTextStyleName() const;
368 
376  void GetLinePositions( std::vector<wxPoint>& aPositions, int aLineCount ) const;
377 
386  virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
387 
388  virtual double GetDrawRotation() const { return GetTextAngle(); }
389  virtual wxPoint GetDrawPos() const { return GetTextPos(); }
392 
393  int Compare( const EDA_TEXT* aOther ) const;
394 
395 private:
396  void cacheShownText();
397 
407  void printOneLineOfText( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
408  const COLOR4D& aColor, OUTLINE_MODE aFillMode, const wxString& aText,
409  const wxPoint& aPos );
410 
411  wxString m_text;
412  wxString m_shown_text; // Cache of unescaped text for efficient access
414 
415  TEXT_EFFECTS m_e; // Private bitflags for text styling. API above
416  // provides accessor funcs.
417  enum TE_FLAGS {
423  };
424 };
425 
426 
427 #endif // EDA_TEXT_H_
void SetMirrored(bool isMirrored)
Definition: eda_text.h:209
EDA_TEXT(const wxString &text=wxEmptyString)
Definition: eda_text.cpp:101
bool IsBold() const
Definition: eda_text.h:204
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:273
OUTLINE_MODE
Definition: outline_mode.h:24
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:233
void SwapEffects(EDA_TEXT &aTradingPartner)
Swap the text effects of the two involved instances.
Definition: eda_text.cpp:153
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:82
bool Bit(int aBit) const
Definition: eda_text.h:130
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:220
bool IsMirrored() const
Definition: eda_text.h:210
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:243
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
void CopyText(const EDA_TEXT &aSrc)
Definition: eda_text.cpp:131
virtual double GetDrawRotation() const
Definition: eda_text.h:388
wxString m_shown_text
Definition: eda_text.h:412
void printOneLineOfText(const RENDER_SETTINGS *aSettings, const wxPoint &aOffset, const COLOR4D &aColor, OUTLINE_MODE aFillMode, const wxString &aText, const wxPoint &aPos)
Print each line of this EDA_TEXT.
Definition: eda_text.cpp:484
std::vector< wxPoint > TransformToSegmentList() const
Convert the text shape to a list of segment.
Definition: eda_text.cpp:606
void Print(const RENDER_SETTINGS *aSettings, const wxPoint &aOffset, const COLOR4D &aColor, OUTLINE_MODE aDisplay_mode=FILLED)
Print this text object to the device context aDC.
Definition: eda_text.cpp:420
void SetEffects(const EDA_TEXT &aSrc)
Set the text effects from another instance.
Definition: eda_text.cpp:139
bool Replace(const wxFindReplaceData &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:180
void SetItalic(bool isItalic)
Definition: eda_text.h:200
TEXT_EFFECTS m_e
Definition: eda_text.h:415
void cacheShownText()
Definition: eda_text.cpp:189
EDA_RECT GetTextBox(int aLine=-1, bool aInvertY=false) const
Useful in multiline texts to calculate the full text or a line area (for zones filling,...
Definition: eda_text.cpp:239
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:267
SHAPE_POLY_SET * m_cornerBuffer
Definition: eda_text.h:50
double GetTextAngle() const
Definition: eda_text.h:195
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:413
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:309
signed char vjustify
vertical justification
Definition: eda_text.h:123
A helper for the text to polygon callback function.
Definition: eda_text.h:46
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:258
void TransformBoundingBoxWithClearanceToPolygon(SHAPE_POLY_SET *aCornerBuffer, int aClearanceValue) const
Convert the text bounding box to a rectangular polygon depending on the text orientation,...
Definition: eda_text.cpp:681
int GetTextThickness() const
Definition: eda_text.h:181
wxPoint pos
Definition: eda_text.h:127
int GetEffectiveTextPenWidth(int aDefaultWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultWidth.
Definition: eda_text.cpp:159
wxSize size
Definition: eda_text.h:124
int GetTextHeight() const
Definition: eda_text.h:265
void SetTextX(int aX)
Definition: eda_text.h:270
bool IsItalic() const
Definition: eda_text.h:201
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:140
virtual void SetVisible(bool aVisible)
Definition: eda_text.h:206
wxString GetTextStyleName() const
Definition: eda_text.cpp:504
void Bit(int aBit, bool aValue)
Definition: eda_text.h:129
int LenSize(const wxString &aLine, int aThickness) const
Definition: eda_text.cpp:204
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:219
static EDA_TEXT_HJUSTIFY_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:73
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:124
A container for text effects.
Definition: eda_text.h:111
Represent a set of closed polygons.
virtual bool IsVisible() const
Definition: eda_text.h:207
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition: eda_text.h:172
const wxSize & GetTextSize() const
Definition: eda_text.h:259
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:223
void Empty()
Definition: eda_text.h:275
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:218
virtual ~EDA_TEXT()
Definition: eda_text.cpp:119
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:539
bool IsDefaultFormatting() const
Definition: eda_text.cpp:525
wxString m_text
Definition: eda_text.h:411
virtual EDA_TEXT_VJUSTIFY_T GetDrawVertJustify() const
Definition: eda_text.h:391
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.h:216
static EDA_TEXT_VJUSTIFY_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:87
bool IsMultilineAllowed() const
Definition: eda_text.h:217
int bits
any set of booleans a client uses.
Definition: eda_text.h:121
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:89
void SetTextWidth(int aWidth)
Definition: eda_text.h:261
double GetTextAngleRadians() const
Definition: eda_text.h:198
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:145
double GetTextAngleDegrees() const
Definition: eda_text.h:197
int GetTextWidth() const
Definition: eda_text.h:262
std::shared_ptr< SHAPE_COMPOUND > GetEffectiveTextShape() const
Definition: eda_text.cpp:646
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:222
TEXT_EFFECTS(int aSetOfBits=0)
Definition: eda_text.h:113
Handle the component boundary box.
Definition: eda_rect.h:42
virtual bool TextHitTest(const wxPoint &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:395
void SetTextHeight(int aHeight)
Definition: eda_text.h:264
void GetLinePositions(std::vector< wxPoint > &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:443
double angle
now: 0.1 degrees; future: degrees
Definition: eda_text.h:126
const wxPoint & GetTextPos() const
Definition: eda_text.h:268
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:180
int Compare(const EDA_TEXT *aOther) const
Definition: eda_text.cpp:659
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:188
virtual EDA_TEXT_HJUSTIFY_T GetDrawHorizJustify() const
Definition: eda_text.h:390
signed char hjustify
horizontal justification
Definition: eda_text.h:122
void SetBold(bool aBold)
Definition: eda_text.h:203
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:154
virtual wxPoint GetDrawPos() const
Definition: eda_text.h:389
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:162
void SetTextY(int aY)
Definition: eda_text.h:271
void addTextSegmToPoly(int x0, int y0, int xf, int yf, void *aData)
Callback function used to convert text segments to polygons.
Definition: eda_text.cpp:65
int penwidth
Definition: eda_text.h:125
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103