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, jpe.charras at wanadoo.fr
5  * Copyright (C) 2004-2019 KiCad Developers, see change_log.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 namespace KIGFX
40 {
41  class RENDER_SETTINGS;
42  class COLOR4D;
43 }
44 
46 using KIGFX::COLOR4D;
47 
50 #define TEXT_ANGLE_HORIZ 0
51 #define TEXT_ANGLE_VERT 900
52 
53 // part of the kicad_plugin.h family of defines.
54 // See kicad_plugin.h for the choice of the value
55 // When set when calling EDA_TEXT::Format, disable writing the "hide" keyword in save file
56 #define CTL_OMIT_HIDE (1 << 6)
57 
58 
59 // Graphic Text justify:
60 // Values -1,0,1 are used in computations, do not change them
65 };
66 
67 
72 };
73 
74 
80 #define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
81 #define DIM_ANCRE_TEXTE 2 // Anchor size for text
82 
83 
91 {
92  TEXT_EFFECTS( int aSetOfBits = 0 ) :
93  bits( aSetOfBits ),
96  penwidth( 0 ),
97  angle( 0.0 )
98  {}
99 
100  int bits;
101  signed char hjustify;
102  signed char vjustify;
103  wxSize size;
104  int penwidth;
105  double angle;
106  wxPoint pos;
107 
108  void Bit( int aBit, bool aValue ) { aValue ? bits |= (1<<aBit) : bits &= ~(1<<aBit); }
109  bool Bit( int aBit ) const { return bits & (1<<aBit); }
110 };
111 
112 
119 class EDA_TEXT
120 {
121 public:
122  EDA_TEXT( const wxString& text = wxEmptyString );
123 
124  EDA_TEXT( const EDA_TEXT& aText );
125 
126  virtual ~EDA_TEXT();
127 
133  virtual const wxString& GetText() const { return m_text; }
134 
140  virtual wxString GetShownText( int aDepth = 0 ) const { return m_shown_text; }
141 
147  wxString GetShownText( bool* processTextVars ) const
148  {
149  *processTextVars = m_shown_text_has_text_var_refs;
150  return m_shown_text;
151  }
152 
156  wxString ShortenedShownText() const;
157 
158  virtual void SetText( const wxString& aText );
159 
164  void SetTextThickness( int aWidth ) { m_e.penwidth = aWidth; };
165  int GetTextThickness() const { return m_e.penwidth; };
166 
171  int GetEffectiveTextPenWidth( int aDefaultWidth = 0 ) const;
172 
173  virtual void SetTextAngle( double aAngle )
174  {
175  // Higher level classes may be more restrictive than this by overloading
176  // SetTextAngle() or merely calling EDA_TEXT::SetTextAngle() after clamping
177  // aAngle before calling this lowest inline accessor.
178  m_e.angle = aAngle;
179  }
180  double GetTextAngle() const { return m_e.angle; }
181 
182  double GetTextAngleDegrees() const { return GetTextAngle() / 10.0; }
183  double GetTextAngleRadians() const { return GetTextAngle() * M_PI/1800; }
184 
185  void SetItalic( bool isItalic ) { m_e.Bit( TE_ITALIC, isItalic ); }
186  bool IsItalic() const { return m_e.Bit( TE_ITALIC ); }
187 
188  void SetBold( bool aBold ) { m_e.Bit( TE_BOLD, aBold); }
189  bool IsBold() const { return m_e.Bit( TE_BOLD ); }
190 
191  void SetVisible( bool aVisible ) { m_e.Bit( TE_VISIBLE, aVisible ); }
192  bool IsVisible() const { return m_e.Bit( TE_VISIBLE ); }
193 
194  void SetMirrored( bool isMirrored ) { m_e.Bit( TE_MIRROR, isMirrored ); }
195  bool IsMirrored() const { return m_e.Bit( TE_MIRROR ); }
196 
202  void SetMultilineAllowed( bool aAllow ) { m_e.Bit( TE_MULTILINE, aAllow ); }
203  bool IsMultilineAllowed() const { return m_e.Bit( TE_MULTILINE ); }
204 
207 
208  void SetHorizJustify( EDA_TEXT_HJUSTIFY_T aType ) { m_e.hjustify = aType; };
209  void SetVertJustify( EDA_TEXT_VJUSTIFY_T aType ) { m_e.vjustify = aType; };
210 
217  void SetEffects( const EDA_TEXT& aSrc );
218 
225  void SwapEffects( EDA_TEXT& aTradingPartner );
226 
227  void SwapText( EDA_TEXT& aTradingPartner );
228 
229  void CopyText( const EDA_TEXT& aSrc );
230 
240  bool Replace( wxFindReplaceData& aSearchData );
241 
242  bool IsDefaultFormatting() const;
243 
244  void SetTextSize( const wxSize& aNewSize ) { m_e.size = aNewSize; }
245  const wxSize& GetTextSize() const { return m_e.size; }
246 
247  void SetTextWidth( int aWidth ) { m_e.size.x = aWidth; }
248  int GetTextWidth() const { return m_e.size.x; }
249 
250  void SetTextHeight( int aHeight ) { m_e.size.y = aHeight; }
251  int GetTextHeight() const { return m_e.size.y; }
252 
253  void SetTextPos( const wxPoint& aPoint ) { m_e.pos = aPoint; }
254  const wxPoint& GetTextPos() const { return m_e.pos; }
255 
256  void SetTextX( int aX ) { m_e.pos.x = aX; }
257  void SetTextY( int aY ) { m_e.pos.y = aY; }
258 
259  void Offset( const wxPoint& aOffset ) { m_e.pos += aOffset; }
260 
261  void Empty() { m_text.Empty(); }
262 
263  static EDA_TEXT_HJUSTIFY_T MapHorizJustify( int aHorizJustify );
264 
265  static EDA_TEXT_VJUSTIFY_T MapVertJustify( int aVertJustify );
266 
275  void Print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, COLOR4D aColor,
276  OUTLINE_MODE aDisplay_mode = FILLED );
277 
286  void TransformTextShapeToSegmentList( std::vector<wxPoint>& aCornerBuffer ) const;
287 
300  int aClearanceValue ) const;
301 
302  std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( ) const;
303 
311  virtual bool TextHitTest( const wxPoint& aPoint, int aAccuracy = 0 ) const;
312 
321  virtual bool TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy = 0 ) const;
322 
329  int LenSize( const wxString& aLine, int aThickness ) const;
330 
340  EDA_RECT GetTextBox( int aLine = -1, bool aInvertY = false ) const;
341 
349  int GetInterline() const;
350 
354  wxString GetTextStyleName() const;
355 
365  void GetLinePositions( std::vector<wxPoint>& aPositions, int aLineCount ) const;
374  virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
375 
376  virtual double GetDrawRotation() const;
377 
378 private:
379  wxString m_text;
380  wxString m_shown_text; // Cache of unescaped text for efficient access
382 
383  TEXT_EFFECTS m_e; // Private bitflags for text styling. API above
384  // provides accessor funcs.
385  enum TE_FLAGS {
391  };
392 
393 private:
403  void printOneLineOfText( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, COLOR4D aColor,
404  OUTLINE_MODE aFillMode, const wxString& aText,
405  const wxPoint& aPos );
406 };
407 
408 
409 #endif // EDA_TEXT_H_
void SetMirrored(bool isMirrored)
Definition: eda_text.h:194
EDA_TEXT(const wxString &text=wxEmptyString)
Definition: eda_text.cpp:90
bool IsBold() const
Definition: eda_text.h:189
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:259
OUTLINE_MODE
Definition: outline_mode.h:24
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:216
void SwapEffects(EDA_TEXT &aTradingPartner)
Swap the text effects of the two involved instances.
Definition: eda_text.cpp:150
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:61
bool Bit(int aBit) const
Definition: eda_text.h:109
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:206
bool IsMirrored() const
Definition: eda_text.h:195
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
void printOneLineOfText(RENDER_SETTINGS *aSettings, const wxPoint &aOffset, COLOR4D aColor, OUTLINE_MODE aFillMode, const wxString &aText, const wxPoint &aPos)
Print each line of this EDA_TEXT.
Definition: eda_text.cpp:464
RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output surfac...
void CopyText(const EDA_TEXT &aSrc)
Definition: eda_text.cpp:128
wxString m_shown_text
Definition: eda_text.h:380
bool IsVisible() const
Definition: eda_text.h:192
void SetEffects(const EDA_TEXT &aSrc)
Set the text effects from another instance.
Definition: eda_text.cpp:136
void SetItalic(bool isItalic)
Definition: eda_text.h:185
TEXT_EFFECTS m_e
Definition: eda_text.h:383
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:222
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:253
void SetVisible(bool aVisible)
Definition: eda_text.h:191
double GetTextAngle() const
Definition: eda_text.h:180
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:381
OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a convenient ...
Definition: richio.h:328
signed char vjustify
vertical justification
Definition: eda_text.h:102
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:244
virtual double GetDrawRotation() const
Definition: eda_text.cpp:634
void TransformBoundingBoxWithClearanceToPolygon(SHAPE_POLY_SET *aCornerBuffer, int aClearanceValue) const
Convert the text bounding box to a rectangular polygon depending on the text orientation,...
int GetTextThickness() const
Definition: eda_text.h:165
wxPoint pos
Definition: eda_text.h:106
int GetEffectiveTextPenWidth(int aDefaultWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultWidth.
Definition: eda_text.cpp:156
wxSize size
Definition: eda_text.h:103
int GetTextHeight() const
Definition: eda_text.h:251
void SetTextX(int aX)
Definition: eda_text.h:256
bool IsItalic() const
Definition: eda_text.h:186
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:119
wxString GetTextStyleName() const
Definition: eda_text.cpp:484
void Bit(int aBit, bool aValue)
Definition: eda_text.h:108
int LenSize(const wxString &aLine, int aThickness) const
Definition: eda_text.cpp:187
wxString GetShownText(bool *processTextVars) const
A version of GetShownText() which also indicates whether or not the text needs to be processed for te...
Definition: eda_text.h:147
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:205
static EDA_TEXT_HJUSTIFY_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:62
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:120
A container for text effects.
Definition: eda_text.h:90
SHAPE_POLY_SET.
const wxSize & GetTextSize() const
Definition: eda_text.h:245
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:209
void TransformTextShapeToSegmentList(std::vector< wxPoint > &aCornerBuffer) const
Convert the text shape to a list of segment.
Definition: eda_text.cpp:583
void Empty()
Definition: eda_text.h:261
void Print(RENDER_SETTINGS *aSettings, const wxPoint &aOffset, COLOR4D aColor, OUTLINE_MODE aDisplay_mode=FILLED)
Print this text object to the device context aDC.
Definition: eda_text.cpp:400
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:201
virtual ~EDA_TEXT()
Definition: eda_text.cpp:115
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:519
bool IsDefaultFormatting() const
Definition: eda_text.cpp:505
wxString m_text
Definition: eda_text.h:379
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.h:202
static EDA_TEXT_VJUSTIFY_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:76
bool IsMultilineAllowed() const
Definition: eda_text.h:203
int bits
any set of booleans a client uses.
Definition: eda_text.h:100
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:68
void SetTextWidth(int aWidth)
Definition: eda_text.h:247
double GetTextAngleRadians() const
Definition: eda_text.h:183
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:142
double GetTextAngleDegrees() const
Definition: eda_text.h:182
int GetTextWidth() const
Definition: eda_text.h:248
std::shared_ptr< SHAPE_COMPOUND > GetEffectiveTextShape() const
Definition: eda_text.cpp:620
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:208
bool Replace(wxFindReplaceData &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:177
TEXT_EFFECTS(int aSetOfBits=0)
Definition: eda_text.h:92
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
virtual bool TextHitTest(const wxPoint &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:375
void SetTextHeight(int aHeight)
Definition: eda_text.h:250
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:423
double angle
now: 0.1 degrees; future: degrees
Definition: eda_text.h:105
const wxPoint & GetTextPos() const
Definition: eda_text.h:254
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:164
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:173
signed char hjustify
horizontal justification
Definition: eda_text.h:101
void SetBold(bool aBold)
Definition: eda_text.h:188
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:140
void SetTextY(int aY)
Definition: eda_text.h:257
int penwidth
Definition: eda_text.h:104
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:100