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-2020 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 
149  wxString GetShownText( bool* processTextVars ) const
150  {
151  *processTextVars = m_shown_text_has_text_var_refs;
152  return m_shown_text;
153  }
154 
158  wxString ShortenedShownText() const;
159 
160  virtual void SetText( const wxString& aText );
161 
166  void SetTextThickness( int aWidth ) { m_e.penwidth = aWidth; };
167  int GetTextThickness() const { return m_e.penwidth; };
168 
172  int GetEffectiveTextPenWidth( int aDefaultWidth = 0 ) const;
173 
174  virtual void SetTextAngle( double aAngle )
175  {
176  // Higher level classes may be more restrictive than this by overloading
177  // SetTextAngle() or merely calling EDA_TEXT::SetTextAngle() after clamping
178  // aAngle before calling this lowest inline accessor.
179  m_e.angle = aAngle;
180  }
181  double GetTextAngle() const { return m_e.angle; }
182 
183  double GetTextAngleDegrees() const { return GetTextAngle() / 10.0; }
184  double GetTextAngleRadians() const { return GetTextAngle() * M_PI/1800; }
185 
186  void SetItalic( bool isItalic ) { m_e.Bit( TE_ITALIC, isItalic ); }
187  bool IsItalic() const { return m_e.Bit( TE_ITALIC ); }
188 
189  void SetBold( bool aBold ) { m_e.Bit( TE_BOLD, aBold); }
190  bool IsBold() const { return m_e.Bit( TE_BOLD ); }
191 
192  void SetVisible( bool aVisible ) { m_e.Bit( TE_VISIBLE, aVisible ); }
193  bool IsVisible() const { return m_e.Bit( TE_VISIBLE ); }
194 
195  void SetMirrored( bool isMirrored ) { m_e.Bit( TE_MIRROR, isMirrored ); }
196  bool IsMirrored() const { return m_e.Bit( TE_MIRROR ); }
197 
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( const 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( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
276  COLOR4D aColor, OUTLINE_MODE aDisplay_mode = FILLED );
277 
284  std::vector<wxPoint> TransformToSegmentList() const;
285 
298  int aClearanceValue ) const;
299 
300  std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape() const;
301 
309  virtual bool TextHitTest( const wxPoint& aPoint, int aAccuracy = 0 ) const;
310 
319  virtual bool TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy = 0 ) const;
320 
327  int LenSize( const wxString& aLine, int aThickness ) const;
328 
339  EDA_RECT GetTextBox( int aLine = -1, bool aInvertY = false ) const;
340 
348  int GetInterline() const;
349 
353  wxString GetTextStyleName() const;
354 
362  void GetLinePositions( std::vector<wxPoint>& aPositions, int aLineCount ) const;
363 
372  virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
373 
374  virtual double GetDrawRotation() const;
375 
376 private:
386  void printOneLineOfText( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset, COLOR4D aColor,
387  OUTLINE_MODE aFillMode, const wxString& aText, const wxPoint& aPos );
388 
389  wxString m_text;
390  wxString m_shown_text; // Cache of unescaped text for efficient access
392 
393  TEXT_EFFECTS m_e; // Private bitflags for text styling. API above
394  // provides accessor funcs.
395  enum TE_FLAGS {
401  };
402 };
403 
404 
405 #endif // EDA_TEXT_H_
void SetMirrored(bool isMirrored)
Definition: eda_text.h:195
EDA_TEXT(const wxString &text=wxEmptyString)
Definition: eda_text.cpp:91
bool IsBold() const
Definition: eda_text.h:190
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:217
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: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:196
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
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:129
wxString m_shown_text
Definition: eda_text.h:390
std::vector< wxPoint > TransformToSegmentList() const
Convert the text shape to a list of segment.
Definition: eda_text.cpp:584
bool IsVisible() const
Definition: eda_text.h:193
void SetEffects(const EDA_TEXT &aSrc)
Set the text effects from another instance.
Definition: eda_text.cpp:137
bool Replace(const wxFindReplaceData &aSearchData)
Helper function used in search and replace dialog.
Definition: eda_text.cpp:178
void SetItalic(bool isItalic)
Definition: eda_text.h:186
TEXT_EFFECTS m_e
Definition: eda_text.h:393
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:223
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:253
void SetVisible(bool aVisible)
Definition: eda_text.h:192
double GetTextAngle() const
Definition: eda_text.h:181
bool m_shown_text_has_text_var_refs
Definition: eda_text.h:391
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:306
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:637
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:167
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:157
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:187
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:485
void Bit(int aBit, bool aValue)
Definition: eda_text.h:108
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:149
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:205
static EDA_TEXT_HJUSTIFY_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:63
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:121
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 Empty()
Definition: eda_text.h:261
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:202
virtual ~EDA_TEXT()
Definition: eda_text.cpp:116
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:520
bool IsDefaultFormatting() const
Definition: eda_text.cpp:506
wxString m_text
Definition: eda_text.h:389
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.h:202
static EDA_TEXT_VJUSTIFY_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:77
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
void printOneLineOfText(const 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:465
double GetTextAngleRadians() const
Definition: eda_text.h:184
void SwapText(EDA_TEXT &aTradingPartner)
Definition: eda_text.cpp:143
void Print(const 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:401
double GetTextAngleDegrees() const
Definition: eda_text.h:183
int GetTextWidth() const
Definition: eda_text.h:248
std::shared_ptr< SHAPE_COMPOUND > GetEffectiveTextShape() const
Definition: eda_text.cpp:624
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:208
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:376
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:424
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:166
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:174
signed char hjustify
horizontal justification
Definition: eda_text.h:101
void SetBold(bool aBold)
Definition: eda_text.h:189
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:257
int penwidth
Definition: eda_text.h:104
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98