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 <trigo.h> // NORMALIZE_ANGLE_POS( angle );
29 #include <common.h> // wxStringSplit
30 #include <gr_basic.h> // EDA_DRAW_MODE_T
31 #include <base_struct.h> // EDA_RECT
32 #include "kicad_string.h"
33 #include "painter.h"
34 
35 class SHAPE;
36 class SHAPE_POLY_SET;
37 
39 
40 // part of the kicad_plugin.h family of defines.
41 // See kicad_plugin.h for the choice of the value
42 // When set when calling EDA_TEXT::Format, disable writing the "hide" keyword in save file
43 #define CTL_OMIT_HIDE (1 << 6)
44 
45 
46 // Graphic Text justify:
47 // Values -1,0,1 are used in computations, do not change them
52 };
53 
54 
59 };
60 
61 
62 /* Options to draw items with thickness ( segments, arcs, circles, texts...) */
64  FILLED = true, // normal mode: solid segments
65  SKETCH = false // sketch mode: draw segments outlines only
66 };
67 
68 
74 #define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
75 #define DIM_ANCRE_TEXTE 2 // Anchor size for text
76 
77 
85 {
86  TEXT_EFFECTS( int aSetOfBits = 0 ) :
87  bits( aSetOfBits ),
90  penwidth( 0 ),
91  angle( 0.0 )
92  {}
93 
94  int bits;
95  signed char hjustify;
96  signed char vjustify;
97  wxSize size;
98  int penwidth;
99  double angle;
101 
102  void Bit( int aBit, bool aValue ) { aValue ? bits |= (1<<aBit) : bits &= ~(1<<aBit); }
103  bool Bit( int aBit ) const { return bits & (1<<aBit); }
104 };
105 
106 
113 class EDA_TEXT
114 {
115 public:
116  EDA_TEXT( const wxString& text = wxEmptyString );
117 
118  EDA_TEXT( const EDA_TEXT& aText );
119 
120  virtual ~EDA_TEXT();
121 
127  virtual const wxString& GetText() const { return m_text; }
128 
134  virtual wxString GetShownText( int aDepth = 0 ) const { return m_shown_text; }
135 
141  wxString GetShownText( bool* processTextVars ) const
142  {
143  *processTextVars = m_shown_text_has_text_var_refs;
144  return m_shown_text;
145  }
146 
150  wxString ShortenedShownText() const;
151 
152  virtual void SetText( const wxString& aText );
153 
158  void SetTextThickness( int aWidth ) { m_e.penwidth = aWidth; };
159  int GetTextThickness() const { return m_e.penwidth; };
160 
165  int GetEffectiveTextPenWidth( int aDefaultWidth = 0 ) const;
166 
167  virtual void SetTextAngle( double aAngle )
168  {
169  // Higher level classes may be more restrictive than this by overloading
170  // SetTextAngle() or merely calling EDA_TEXT::SetTextAngle() after clamping
171  // aAngle before calling this lowest inline accessor.
172  m_e.angle = aAngle;
173  }
174  double GetTextAngle() const { return m_e.angle; }
175 
176  double GetTextAngleDegrees() const { return GetTextAngle() / 10.0; }
177  double GetTextAngleRadians() const { return GetTextAngle() * M_PI/1800; }
178 
179  void SetItalic( bool isItalic ) { m_e.Bit( TE_ITALIC, isItalic ); }
180  bool IsItalic() const { return m_e.Bit( TE_ITALIC ); }
181 
182  void SetBold( bool aBold ) { m_e.Bit( TE_BOLD, aBold); }
183  bool IsBold() const { return m_e.Bit( TE_BOLD ); }
184 
185  void SetVisible( bool aVisible ) { m_e.Bit( TE_VISIBLE, aVisible ); }
186  bool IsVisible() const { return m_e.Bit( TE_VISIBLE ); }
187 
188  void SetMirrored( bool isMirrored ) { m_e.Bit( TE_MIRROR, isMirrored ); }
189  bool IsMirrored() const { return m_e.Bit( TE_MIRROR ); }
190 
196  void SetMultilineAllowed( bool aAllow ) { m_e.Bit( TE_MULTILINE, aAllow ); }
197  bool IsMultilineAllowed() const { return m_e.Bit( TE_MULTILINE ); }
198 
201 
202  void SetHorizJustify( EDA_TEXT_HJUSTIFY_T aType ) { m_e.hjustify = aType; };
203  void SetVertJustify( EDA_TEXT_VJUSTIFY_T aType ) { m_e.vjustify = aType; };
204 
211  void SetEffects( const EDA_TEXT& aSrc );
212 
219  void SwapEffects( EDA_TEXT& aTradingPartner );
220 
221  void SwapText( EDA_TEXT& aTradingPartner );
222 
223  void CopyText( const EDA_TEXT& aSrc );
224 
234  bool Replace( wxFindReplaceData& aSearchData );
235 
236  bool IsDefaultFormatting() const;
237 
238  void SetTextSize( const wxSize& aNewSize ) { m_e.size = aNewSize; }
239  const wxSize& GetTextSize() const { return m_e.size; }
240 
241  void SetTextWidth( int aWidth ) { m_e.size.x = aWidth; }
242  int GetTextWidth() const { return m_e.size.x; }
243 
244  void SetTextHeight( int aHeight ) { m_e.size.y = aHeight; }
245  int GetTextHeight() const { return m_e.size.y; }
246 
247  void SetTextPos( const wxPoint& aPoint ) { m_e.pos = aPoint; }
248  const wxPoint& GetTextPos() const { return m_e.pos; }
249 
250  void SetTextX( int aX ) { m_e.pos.x = aX; }
251  void SetTextY( int aY ) { m_e.pos.y = aY; }
252 
253  void Offset( const wxPoint& aOffset ) { m_e.pos += aOffset; }
254 
255  void Empty() { m_text.Empty(); }
256 
257  static EDA_TEXT_HJUSTIFY_T MapHorizJustify( int aHorizJustify );
258 
259  static EDA_TEXT_VJUSTIFY_T MapVertJustify( int aVertJustify );
260 
269  void Print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, COLOR4D aColor,
270  EDA_DRAW_MODE_T aDisplay_mode = FILLED );
271 
280  void TransformTextShapeToSegmentList( std::vector<wxPoint>& aCornerBuffer ) const;
281 
294  int aClearanceValue ) const;
295 
296  std::shared_ptr<SHAPE> GetEffectiveShape( ) const;
297 
305  virtual bool TextHitTest( const wxPoint& aPoint, int aAccuracy = 0 ) const;
306 
315  virtual bool TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy = 0 ) const;
316 
323  int LenSize( const wxString& aLine, int aThickness ) const;
324 
334  EDA_RECT GetTextBox( int aLine = -1, bool aInvertY = false ) const;
335 
343  int GetInterline() const;
344 
348  wxString GetTextStyleName();
349 
359  void GetLinePositions( std::vector<wxPoint>& aPositions, int aLineCount ) const;
368  virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
369 
370 private:
371  wxString m_text;
372  wxString m_shown_text; // Cache of unescaped text for efficient access
374 
375  TEXT_EFFECTS m_e; // Private bitflags for text styling. API above
376  // provides accessor funcs.
377  enum TE_FLAGS {
383  };
384 
385 private:
395  void printOneLineOfText( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, COLOR4D aColor,
396  EDA_DRAW_MODE_T aFillMode, const wxString& aText,
397  const wxPoint& aPos );
398 };
399 
400 
401 #endif // EDA_TEXT_H_
void SetMirrored(bool isMirrored)
Definition: eda_text.h:188
EDA_TEXT(const wxString &text=wxEmptyString)
Definition: eda_text.cpp:91
bool IsBold() const
Definition: eda_text.h:183
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:253
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:151
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:48
bool Bit(int aBit) const
Definition: eda_text.h:103
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:200
bool IsMirrored() const
Definition: eda_text.h:189
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:129
wxString m_shown_text
Definition: eda_text.h:372
bool IsVisible() const
Definition: eda_text.h:186
void SetEffects(const EDA_TEXT &aSrc)
Set the text effects from another instance.
Definition: eda_text.cpp:137
void SetItalic(bool isItalic)
Definition: eda_text.h:179
TEXT_EFFECTS m_e
Definition: eda_text.h:375
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:247
void SetVisible(bool aVisible)
Definition: eda_text.h:185
double GetTextAngle() const
Definition: eda_text.h:174
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:373
OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a convenient ...
Definition: richio.h:327
signed char vjustify
vertical justification
Definition: eda_text.h:96
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:238
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:159
wxPoint pos
Definition: eda_text.h:100
int GetEffectiveTextPenWidth(int aDefaultWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultWidth.
Definition: eda_text.cpp:157
wxSize size
Definition: eda_text.h:97
void Print(RENDER_SETTINGS *aSettings, const wxPoint &aOffset, COLOR4D aColor, EDA_DRAW_MODE_T aDisplay_mode=FILLED)
Print this text object to the device context aDC.
Definition: eda_text.cpp:400
int GetTextHeight() const
Definition: eda_text.h:245
void SetTextX(int aX)
Definition: eda_text.h:250
bool IsItalic() const
Definition: eda_text.h:180
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:113
void Bit(int aBit, bool aValue)
Definition: eda_text.h:102
int LenSize(const wxString &aLine, int aThickness) const
Definition: eda_text.cpp:188
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:141
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:199
static EDA_TEXT_HJUSTIFY_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:63
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:121
std::shared_ptr< SHAPE > GetEffectiveShape() const
Definition: eda_text.cpp:620
A container for text effects.
Definition: eda_text.h:84
SHAPE_POLY_SET.
EDA_DRAW_MODE_T
Definition: eda_text.h:63
const wxSize & GetTextSize() const
Definition: eda_text.h:239
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:203
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:255
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:116
void printOneLineOfText(RENDER_SETTINGS *aSettings, const wxPoint &aOffset, COLOR4D aColor, EDA_DRAW_MODE_T aFillMode, const wxString &aText, const wxPoint &aPos)
Print each line of this EDA_TEXT.
Definition: eda_text.cpp:464
SHAPE.
Definition: shape.h:74
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:371
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.h:196
static EDA_TEXT_VJUSTIFY_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:77
bool IsMultilineAllowed() const
Definition: eda_text.h:197
int bits
any set of booleans a client uses.
Definition: eda_text.h:94
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:55
void SetTextWidth(int aWidth)
Definition: eda_text.h:241
double GetTextAngleRadians() const
Definition: eda_text.h:177
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:143
double GetTextAngleDegrees() const
Definition: eda_text.h:176
int GetTextWidth() const
Definition: eda_text.h:242
wxString GetTextStyleName()
Definition: eda_text.cpp:484
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:202
bool Replace(wxFindReplaceData &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:178
TEXT_EFFECTS(int aSetOfBits=0)
Definition: eda_text.h:86
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:244
The common library.
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:99
const wxPoint & GetTextPos() const
Definition: eda_text.h:248
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:158
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:167
signed char hjustify
horizontal justification
Definition: eda_text.h:95
void SetBold(bool aBold)
Definition: eda_text.h:182
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:127
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:134
void SetTextY(int aY)
Definition: eda_text.h:251
int penwidth
Definition: eda_text.h:98
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99