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-2017 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 
30 #ifndef EDA_TEXT_H_
31 #define EDA_TEXT_H_
32 
33 #include <mutex>
34 #include <trigo.h> // NORMALIZE_ANGLE_POS( angle );
35 #include <common.h> // wxStringSplit
36 #include <gr_basic.h> // EDA_DRAW_MODE_T
37 #include <base_struct.h> // EDA_RECT
38 
39 #include <mutex>
40 #include "kicad_string.h"
41 
42 class SHAPE_POLY_SET;
43 
44 // A mutex which is unique to each instance it appears in (ie: a new std::mutex is allocated
45 // on copy or assignment).
46 class UNIQUE_MUTEX : public std::mutex
47 {
48 public:
49  UNIQUE_MUTEX() : std::mutex() {}
50  UNIQUE_MUTEX( const UNIQUE_MUTEX& ) : std::mutex() {}
51  UNIQUE_MUTEX& operator= (const UNIQUE_MUTEX& ) { return *this; }
52 };
53 
54 
55 // part of the kicad_plugin.h family of defines.
56 // See kicad_plugin.h for the choice of the value
57 // When set when calling EDA_TEXT::Format, disable writing the "hide" keyword in save file
58 #define CTL_OMIT_HIDE (1 << 6)
59 
60 
61 // Graphic Text justify:
62 // Values -1,0,1 are used in computations, do not change them
67 };
68 
69 
74 };
75 
76 
77 /* Options to draw items with thickness ( segments, arcs, circles, texts...) */
79  FILLED = true, // normal mode: solid segments
80  SKETCH = false // sketch mode: draw segments outlines only
81 };
82 
83 
88 #define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
89 #define DIM_ANCRE_TEXTE 2 // Anchor size for text
90 
91 
99 {
100  TEXT_EFFECTS( int aSetOfBits = 0 ) :
101  bits( aSetOfBits ),
104  penwidth( 0 ),
105  angle( 0.0 )
106  {}
107 
108  short bits;
109  signed char hjustify;
110  signed char vjustify;
111  wxSize size;
112  int penwidth;
113  double angle;
114  wxPoint pos;
115 
116  void Bit( int aBit, bool aValue ) { aValue ? bits |= (1<<aBit) : bits &= ~(1<<aBit); }
117  bool Bit( int aBit ) const { return bits & (1<<aBit); }
118 };
119 
120 
128 class EDA_TEXT
129 {
130 public:
131  EDA_TEXT( const wxString& text = wxEmptyString );
132 
133  EDA_TEXT( const EDA_TEXT& aText );
134 
135  virtual ~EDA_TEXT();
136 
147  virtual const wxString/* & forbidden */ GetText() const { return m_Text; }
148 
152  virtual wxString GetShownText() const { return m_shown_text; }
153 
156  wxString ShortenedShownText() const;
157 
158  virtual void SetText( const wxString& aText );
159 
165  void SetThickness( int aNewThickness ) { m_e.penwidth = aNewThickness; };
166 
171  int GetThickness() const { return m_e.penwidth; };
172 
173  void SetTextAngle( double aAngle )
174  {
175  // Higher level classes may be more restrictive than this by
176  // overloading SetTextAngle() (probably non-virtual) or merely
177  // calling EDA_TEXT::SetTextAngle() after clamping aAngle
178  // 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 
204  void SetMultilineAllowed( bool aAllow ) { m_e.Bit( TE_MULTILINE, aAllow ); }
205  bool IsMultilineAllowed() const { return m_e.Bit( TE_MULTILINE ); }
206 
209 
210  void SetHorizJustify( EDA_TEXT_HJUSTIFY_T aType ) { m_e.hjustify = aType; };
211  void SetVertJustify( EDA_TEXT_VJUSTIFY_T aType ) { m_e.vjustify = aType; };
212 
219  void SetEffects( const EDA_TEXT& aSrc );
220 
227  void SwapEffects( EDA_TEXT& aTradingPartner );
228 
229  bool IsDefaultFormatting() const;
230 
231  void SetTextSize( const wxSize& aNewSize ) { m_e.size = aNewSize; };
232  const wxSize& GetTextSize() const { return m_e.size; };
233 
234  void SetTextWidth( int aWidth ) { m_e.size.x = aWidth; }
235  int GetTextWidth() const { return m_e.size.x; }
236 
237  void SetTextHeight( int aHeight ) { m_e.size.y = aHeight; }
238  int GetTextHeight() const { return m_e.size.y; }
239 
240  void SetTextPos( const wxPoint& aPoint ) { m_e.pos = aPoint; }
241  const wxPoint& GetTextPos() const { return m_e.pos; }
242 
243  void SetTextX( int aX ) { m_e.pos.x = aX; }
244  void SetTextY( int aY ) { m_e.pos.y = aY; }
245 
246  void Offset( const wxPoint& aOffset ) { m_e.pos += aOffset; }
247 
248  void Empty() { m_Text.Empty(); }
249 
260  void Draw( EDA_RECT* aClipBox, wxDC* aDC,
261  const wxPoint& aOffset, COLOR4D aColor,
262  GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aDisplay_mode = FILLED,
263  COLOR4D aAnchor_color = COLOR4D::UNSPECIFIED );
264 
271  void TransformTextShapeToSegmentList( std::vector<wxPoint>& aCornerBuffer ) const;
272 
285  int aClearanceValue ) const;
286 
294  virtual bool TextHitTest( const wxPoint& aPoint, int aAccuracy = 0 ) const;
295 
305  virtual bool TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy = 0 ) const;
306 
314  int LenSize( const wxString& aLine, int aThickness ) const;
315 
332  EDA_RECT GetTextBox( int aLine = -1, int aThickness = -1, bool aInvertY = false ) const;
333 
347  int GetInterline( int aTextThickness = -1 ) const;
348 
353  wxString GetTextStyleName();
354 
365  std::vector<wxPoint>& aPositions, int aLineCount ) const;
375  virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
376 
377 protected:
378  wxString m_Text;
379 
381  wxString m_shown_text;
382 
383  // wxString isn't thread-safe, so make use of this in multi-threaded situations
385 
386 private:
400  void drawOneLineOfText( EDA_RECT* aClipBox, wxDC* aDC,
401  const wxPoint& aOffset, COLOR4D aColor,
402  GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aFillMode,
403  const wxString& aText, const wxPoint& aPos );
404 
405  // Private text effects data. API above provides accessor funcs.
407 
409  enum TE_FLAGS {
410  // start at zero, sequence is irrelevant
416  };
417 };
418 
419 
420 #endif // EDA_TEXT_H_
void SetMirrored(bool isMirrored)
Definition: eda_text.h:195
void SetTextAngle(double aAngle)
Definition: eda_text.h:173
EDA_TEXT(const wxString &text=wxEmptyString)
Definition: eda_text.cpp:41
bool IsBold() const
Definition: eda_text.h:190
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:246
void SwapEffects(EDA_TEXT &aTradingPartner)
Function SwapEffects swaps the text effects of the two involved instances.
Definition: eda_text.cpp:77
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:63
bool Bit(int aBit) const
Definition: eda_text.h:117
wxString m_Text
Definition: eda_text.h:378
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:208
bool IsMirrored() const
Definition: eda_text.h:196
UNIQUE_MUTEX & operator=(const UNIQUE_MUTEX &)
Definition: eda_text.h:51
wxString m_shown_text
Cache of unescaped text for efficient access.
Definition: eda_text.h:381
bool IsVisible() const
Definition: eda_text.h:193
void SetEffects(const EDA_TEXT &aSrc)
Function SetEffects sets the text effects from another instance.
Definition: eda_text.cpp:71
void SetItalic(bool isItalic)
Definition: eda_text.h:186
TEXT_EFFECTS m_e
Definition: eda_text.h:406
int GetInterline(int aTextThickness=-1) const
Return the distance between two lines of text.
Definition: eda_text.cpp:111
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:240
void SetVisible(bool aVisible)
Definition: eda_text.h:192
double GetTextAngle() const
Definition: eda_text.h:181
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:171
signed char vjustify
vertical justification
Definition: eda_text.h:110
virtual const wxString GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:147
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:231
void TransformBoundingBoxWithClearanceToPolygon(SHAPE_POLY_SET *aCornerBuffer, int aClearanceValue) const
Function TransformBoundingBoxWithClearanceToPolygon Convert the text bounding box to a rectangular po...
wxPoint pos
Definition: eda_text.h:114
void drawOneLineOfText(EDA_RECT *aClipBox, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aFillMode, const wxString &aText, const wxPoint &aPos)
Function drawOneLineOfText draws a single text line.
Definition: eda_text.cpp:373
wxSize size
Definition: eda_text.h:111
int GetTextHeight() const
Definition: eda_text.h:238
void SetTextX(int aX)
Definition: eda_text.h:243
bool IsItalic() const
Definition: eda_text.h:187
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels,...
Definition: eda_text.h:128
void Bit(int aBit, bool aValue)
Definition: eda_text.h:116
int LenSize(const wxString &aLine, int aThickness) const
Function LenSize.
Definition: eda_text.cpp:83
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:207
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:64
EDA_RECT GetTextBox(int aLine=-1, int aThickness=-1, bool aInvertY=false) const
Function GetTextBox useful in multiline texts to calculate the full text or a line area (for zones fi...
Definition: eda_text.cpp:119
Struct TEXT_EFFECTS is a bucket for text effects.
Definition: eda_text.h:98
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:37
Class SHAPE_POLY_SET.
EDA_DRAW_MODE_T
Definition: eda_text.h:78
const wxSize & GetTextSize() const
Definition: eda_text.h:232
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:211
short bits
any set of booleans a client uses.
Definition: eda_text.h:108
TE_FLAGS
EDA_TEXT effects bools.
Definition: eda_text.h:409
void TransformTextShapeToSegmentList(std::vector< wxPoint > &aCornerBuffer) const
Convert the text shape to a list of segment each segment is stored as 2 wxPoints: the starting point ...
Definition: eda_text.cpp:496
void Empty()
Definition: eda_text.h:248
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:96
virtual ~EDA_TEXT()
Definition: eda_text.cpp:59
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Function Format outputs the object to aFormatter in s-expression form.
Definition: eda_text.cpp:432
bool IsDefaultFormatting() const
Definition: eda_text.cpp:418
void SetMultilineAllowed(bool aAllow)
Function SetMultiLineAllowed.
Definition: eda_text.h:204
bool IsMultilineAllowed() const
Definition: eda_text.h:205
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:70
void SetTextWidth(int aWidth)
Definition: eda_text.h:234
double GetTextAngleRadians() const
Definition: eda_text.h:184
double GetTextAngleDegrees() const
Definition: eda_text.h:183
int GetTextWidth() const
Definition: eda_text.h:235
wxString GetTextStyleName()
Function GetTextStyleName.
Definition: eda_text.cpp:397
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:210
TEXT_EFFECTS(int aSetOfBits=0)
Definition: eda_text.h:100
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
virtual bool TextHitTest(const wxPoint &aPoint, int aAccuracy=0) const
Function TextHitTest Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:271
void SetTextHeight(int aHeight)
Definition: eda_text.h:237
The common library.
UNIQUE_MUTEX(const UNIQUE_MUTEX &)
Definition: eda_text.h:50
double angle
now: 0.1 degrees; future: degrees
Definition: eda_text.h:113
const wxPoint & GetTextPos() const
Definition: eda_text.h:241
void GetPositionsOfLinesOfMultilineText(std::vector< wxPoint > &aPositions, int aLineCount) const
Function GetPositionsOfLinesOfMultilineText Populates aPositions with the position of each line of a ...
Definition: eda_text.cpp:331
virtual wxString GetShownText() const
Returns the string actually shown after processing of the base text.
Definition: eda_text.h:152
Basic classes for most KiCad items.
UNIQUE_MUTEX()
Definition: eda_text.h:49
signed char hjustify
horizontal justification
Definition: eda_text.h:109
void SetBold(bool aBold)
Definition: eda_text.h:189
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:165
void SetTextY(int aY)
Definition: eda_text.h:244
void Draw(EDA_RECT *aClipBox, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aDisplay_mode=FILLED, COLOR4D aAnchor_color=COLOR4D::UNSPECIFIED)
Function Draw.
Definition: eda_text.cpp:296
int penwidth
Definition: eda_text.h:112
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
UNIQUE_MUTEX m_mutex
Definition: eda_text.h:384