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-2021 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 
141  virtual wxString GetShownText( int aDepth = 0 ) const { return m_shown_text; }
142 
146  wxString ShortenedShownText() const;
147 
152 
153  virtual void SetText( const wxString& aText );
154 
159  void SetTextThickness( int aWidth ) { m_e.penwidth = aWidth; };
160  int GetTextThickness() const { return m_e.penwidth; };
161 
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  virtual void SetVisible( bool aVisible ) { m_e.Bit( TE_VISIBLE, aVisible ); }
186  virtual 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 
195  void SetMultilineAllowed( bool aAllow ) { m_e.Bit( TE_MULTILINE, aAllow ); }
196  bool IsMultilineAllowed() const { return m_e.Bit( TE_MULTILINE ); }
197 
200 
201  void SetHorizJustify( EDA_TEXT_HJUSTIFY_T aType ) { m_e.hjustify = aType; };
202  void SetVertJustify( EDA_TEXT_VJUSTIFY_T aType ) { m_e.vjustify = aType; };
203 
210  void SetEffects( const EDA_TEXT& aSrc );
211 
218  void SwapEffects( EDA_TEXT& aTradingPartner );
219 
220  void SwapText( EDA_TEXT& aTradingPartner );
221 
222  void CopyText( const EDA_TEXT& aSrc );
223 
233  bool Replace( const wxFindReplaceData& aSearchData );
234 
235  bool IsDefaultFormatting() const;
236 
237  void SetTextSize( const wxSize& aNewSize ) { m_e.size = aNewSize; }
238  const wxSize& GetTextSize() const { return m_e.size; }
239 
240  void SetTextWidth( int aWidth ) { m_e.size.x = aWidth; }
241  int GetTextWidth() const { return m_e.size.x; }
242 
243  void SetTextHeight( int aHeight ) { m_e.size.y = aHeight; }
244  int GetTextHeight() const { return m_e.size.y; }
245 
246  void SetTextPos( const wxPoint& aPoint ) { m_e.pos = aPoint; }
247  const wxPoint& GetTextPos() const { return m_e.pos; }
248 
249  void SetTextX( int aX ) { m_e.pos.x = aX; }
250  void SetTextY( int aY ) { m_e.pos.y = aY; }
251 
252  void Offset( const wxPoint& aOffset ) { m_e.pos += aOffset; }
253 
254  void Empty() { m_text.Empty(); }
255 
256  static EDA_TEXT_HJUSTIFY_T MapHorizJustify( int aHorizJustify );
257 
258  static EDA_TEXT_VJUSTIFY_T MapVertJustify( int aVertJustify );
259 
268  void Print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
269  const COLOR4D& aColor, OUTLINE_MODE aDisplay_mode = FILLED );
270 
277  std::vector<wxPoint> TransformToSegmentList() const;
278 
291  int aClearanceValue ) const;
292 
293  std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape() const;
294 
302  virtual bool TextHitTest( const wxPoint& aPoint, int aAccuracy = 0 ) const;
303 
312  virtual bool TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy = 0 ) const;
313 
320  int LenSize( const wxString& aLine, int aThickness ) const;
321 
332  EDA_RECT GetTextBox( int aLine = -1, bool aInvertY = false ) const;
333 
341  int GetInterline() const;
342 
346  wxString GetTextStyleName() const;
347 
355  void GetLinePositions( std::vector<wxPoint>& aPositions, int aLineCount ) const;
356 
365  virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
366 
367  virtual double GetDrawRotation() const;
368 
369 private:
370  void cacheShownText();
371 
381  void printOneLineOfText( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
382  const COLOR4D& aColor, OUTLINE_MODE aFillMode, const wxString& aText,
383  const wxPoint& aPos );
384 
385  wxString m_text;
386  wxString m_shown_text; // Cache of unescaped text for efficient access
388 
389  TEXT_EFFECTS m_e; // Private bitflags for text styling. API above
390  // provides accessor funcs.
391  enum TE_FLAGS {
397  };
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:252
OUTLINE_MODE
Definition: outline_mode.h:24
int GetInterline() const
Return the distance between two lines of text.
Definition: eda_text.cpp:223
void SwapEffects(EDA_TEXT &aTradingPartner)
Swap the text effects of the two involved instances.
Definition: eda_text.cpp:143
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:199
bool IsMirrored() const
Definition: eda_text.h:189
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:191
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:121
wxString m_shown_text
Definition: eda_text.h:386
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:471
std::vector< wxPoint > TransformToSegmentList() const
Convert the text shape to a list of segment.
Definition: eda_text.cpp:593
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:407
void SetEffects(const EDA_TEXT &aSrc)
Set the text effects from another instance.
Definition: eda_text.cpp:129
bool Replace(const wxFindReplaceData &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:170
void SetItalic(bool isItalic)
Definition: eda_text.h:179
TEXT_EFFECTS m_e
Definition: eda_text.h:389
void cacheShownText()
Definition: eda_text.cpp:179
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:229
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:246
double GetTextAngle() const
Definition: eda_text.h:174
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:387
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:102
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:237
virtual double GetDrawRotation() const
Definition: eda_text.cpp:646
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:160
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:149
wxSize size
Definition: eda_text.h:103
int GetTextHeight() const
Definition: eda_text.h:244
void SetTextX(int aX)
Definition: eda_text.h:249
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:119
virtual void SetVisible(bool aVisible)
Definition: eda_text.h:185
wxString GetTextStyleName() const
Definition: eda_text.cpp:491
void Bit(int aBit, bool aValue)
Definition: eda_text.h:108
int LenSize(const wxString &aLine, int aThickness) const
Definition: eda_text.cpp:194
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:198
static EDA_TEXT_HJUSTIFY_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:63
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:114
A container for text effects.
Definition: eda_text.h:90
Represent a set of closed polygons.
virtual bool IsVisible() const
Definition: eda_text.h:186
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition: eda_text.h:151
const wxSize & GetTextSize() const
Definition: eda_text.h:238
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:202
void Empty()
Definition: eda_text.h:254
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:208
virtual ~EDA_TEXT()
Definition: eda_text.cpp:109
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:526
bool IsDefaultFormatting() const
Definition: eda_text.cpp:512
wxString m_text
Definition: eda_text.h:385
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.h:195
static EDA_TEXT_VJUSTIFY_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:77
bool IsMultilineAllowed() const
Definition: eda_text.h:196
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:240
double GetTextAngleRadians() const
Definition: eda_text.h:177
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:135
double GetTextAngleDegrees() const
Definition: eda_text.h:176
int GetTextWidth() const
Definition: eda_text.h:241
std::shared_ptr< SHAPE_COMPOUND > GetEffectiveTextShape() const
Definition: eda_text.cpp:633
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:201
TEXT_EFFECTS(int aSetOfBits=0)
Definition: eda_text.h:92
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:382
void SetTextHeight(int aHeight)
Definition: eda_text.h:243
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:430
double angle
now: 0.1 degrees; future: degrees
Definition: eda_text.h:105
const wxPoint & GetTextPos() const
Definition: eda_text.h:247
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:159
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:167
signed char hjustify
horizontal justification
Definition: eda_text.h:101
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:133
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:141
void SetTextY(int aY)
Definition: eda_text.h:250
int penwidth
Definition: eda_text.h:104
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103