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 
41 class SHAPE_POLY_SET;
42 
43 // A mutex which is unique to each instance it appears in (ie: a new std::mutex is allocated
44 // on copy or assignment).
45 class UNIQUE_MUTEX : public std::mutex
46 {
47 public:
48  UNIQUE_MUTEX() : std::mutex() {}
49  UNIQUE_MUTEX( const UNIQUE_MUTEX& ) : std::mutex() {}
50  UNIQUE_MUTEX& operator= (const UNIQUE_MUTEX& ) { return *this; }
51 };
52 
53 
54 // part of the kicad_plugin.h family of defines.
55 // See kicad_plugin.h for the choice of the value
56 // When set when calling EDA_TEXT::Format, disable writing the "hide" keyword in save file
57 #define CTL_OMIT_HIDE (1 << 6)
58 
59 
60 // Graphic Text justify:
61 // Values -1,0,1 are used in computations, do not change them
66 };
67 
68 
73 };
74 
75 
76 /* Options to draw items with thickness ( segments, arcs, circles, texts...) */
78  FILLED = true, // normal mode: solid segments
79  SKETCH = false // sketch mode: draw segments outlines only
80 };
81 
82 
87 #define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
88 #define DIM_ANCRE_TEXTE 2 // Anchor size for text
89 
90 
98 {
99  TEXT_EFFECTS( int aSetOfBits = 0 ) :
100  bits( aSetOfBits ),
101  hjustify( GR_TEXT_HJUSTIFY_CENTER ),
102  vjustify( GR_TEXT_VJUSTIFY_CENTER ),
103  penwidth( 0 ),
104  angle( 0.0 )
105  {}
106 
107  short bits;
108  signed char hjustify;
109  signed char vjustify;
110  wxSize size;
111  int penwidth;
112  double angle;
114 
115  void Bit( int aBit, bool aValue ) { aValue ? bits |= (1<<aBit) : bits &= ~(1<<aBit); }
116  bool Bit( int aBit ) const { return bits & (1<<aBit); }
117 };
118 
119 
127 class EDA_TEXT
128 {
129 public:
130  EDA_TEXT( const wxString& text = wxEmptyString );
131 
132  // Do not create a copy constructor & operator=.
133  // The ones generated by the compiler are adequate.
134 
135  virtual ~EDA_TEXT();
136 
143  const wxString& GetText() const { return m_Text; }
144 
148  virtual wxString GetShownText() const { return m_Text; }
149 
152  wxString ShortenedShownText() const;
153 
154  virtual void SetText( const wxString& aText ) { m_Text = aText; }
155 
161  void SetThickness( int aNewThickness ) { m_e.penwidth = aNewThickness; };
162 
167  int GetThickness() const { return m_e.penwidth; };
168 
169  void SetTextAngle( double aAngle )
170  {
171  // Higher level classes may be more restrictive than this by
172  // overloading SetTextAngle() (probably non-virtual) or merely
173  // calling EDA_TEXT::SetTextAngle() after clamping aAngle
174  // before calling this lowest inline accessor.
175  m_e.angle = aAngle;
176  }
177  double GetTextAngle() const { return m_e.angle; }
178 
179  double GetTextAngleDegrees() const { return GetTextAngle() / 10.0; }
180  double GetTextAngleRadians() const { return GetTextAngle() * M_PI/1800; }
181 
182  void SetItalic( bool isItalic ) { m_e.Bit( TE_ITALIC, isItalic ); }
183  bool IsItalic() const { return m_e.Bit( TE_ITALIC ); }
184 
185  void SetBold( bool aBold ) { m_e.Bit( TE_BOLD, aBold); }
186  bool IsBold() const { return m_e.Bit( TE_BOLD ); }
187 
188  void SetVisible( bool aVisible ) { m_e.Bit( TE_VISIBLE, aVisible ); }
189  bool IsVisible() const { return m_e.Bit( TE_VISIBLE ); }
190 
191  void SetMirrored( bool isMirrored ) { m_e.Bit( TE_MIRROR, isMirrored ); }
192  bool IsMirrored() const { return m_e.Bit( TE_MIRROR ); }
193 
200  void SetMultilineAllowed( bool aAllow ) { m_e.Bit( TE_MULTILINE, aAllow ); }
201  bool IsMultilineAllowed() const { return m_e.Bit( TE_MULTILINE ); }
202 
203  EDA_TEXT_HJUSTIFY_T GetHorizJustify() const { return EDA_TEXT_HJUSTIFY_T( m_e.hjustify ); };
204  EDA_TEXT_VJUSTIFY_T GetVertJustify() const { return EDA_TEXT_VJUSTIFY_T( m_e.vjustify ); };
205 
206  void SetHorizJustify( EDA_TEXT_HJUSTIFY_T aType ) { m_e.hjustify = aType; };
207  void SetVertJustify( EDA_TEXT_VJUSTIFY_T aType ) { m_e.vjustify = aType; };
208 
215  void SetEffects( const EDA_TEXT& aSrc );
216 
223  void SwapEffects( EDA_TEXT& aTradingPartner );
224 
225  bool IsDefaultFormatting() const;
226 
227  void SetTextSize( const wxSize& aNewSize ) { m_e.size = aNewSize; };
228  const wxSize& GetTextSize() const { return m_e.size; };
229 
230  void SetTextWidth( int aWidth ) { m_e.size.x = aWidth; }
231  int GetTextWidth() const { return m_e.size.x; }
232 
233  void SetTextHeight( int aHeight ) { m_e.size.y = aHeight; }
234  int GetTextHeight() const { return m_e.size.y; }
235 
236  void SetTextPos( const wxPoint& aPoint ) { m_e.pos = aPoint; }
237  const wxPoint& GetTextPos() const { return m_e.pos; }
238 
239  void SetTextX( int aX ) { m_e.pos.x = aX; }
240  void SetTextY( int aY ) { m_e.pos.y = aY; }
241 
242  void Offset( const wxPoint& aOffset ) { m_e.pos += aOffset; }
243 
244  void Empty() { m_Text.Empty(); }
245 
256  void Draw( EDA_RECT* aClipBox, wxDC* aDC,
257  const wxPoint& aOffset, COLOR4D aColor,
258  GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aDisplay_mode = FILLED,
259  COLOR4D aAnchor_color = COLOR4D::UNSPECIFIED );
260 
267  void TransformTextShapeToSegmentList( std::vector<wxPoint>& aCornerBuffer ) const;
268 
280  void TransformBoundingBoxWithClearanceToPolygon( SHAPE_POLY_SET* aCornerBuffer,
281  int aClearanceValue ) const;
282 
290  virtual bool TextHitTest( const wxPoint& aPoint, int aAccuracy = 0 ) const;
291 
301  virtual bool TextHitTest( const EDA_RECT& aRect, bool aContains = false, int aAccuracy = 0 ) const;
302 
310  int LenSize( const wxString& aLine, int aThickness ) const;
311 
328  EDA_RECT GetTextBox( int aLine = -1, int aThickness = -1, bool aInvertY = false ) const;
329 
343  int GetInterline( int aTextThickness = -1 ) const;
344 
349  wxString GetTextStyleName();
350 
360  void GetPositionsOfLinesOfMultilineText(
361  std::vector<wxPoint>& aPositions, int aLineCount ) const;
371  virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
372 
373 protected:
374  wxString m_Text;
375 
376  // wxString isn't thread-safe, so make use of this in multi-threaded situations
378 
379 private:
393  void drawOneLineOfText( EDA_RECT* aClipBox, wxDC* aDC,
394  const wxPoint& aOffset, COLOR4D aColor,
395  GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aFillMode,
396  const wxString& aText, const wxPoint& aPos );
397 
398  // Private text effects data. API above provides accessor funcs.
400 
402  enum TE_FLAGS {
403  // start at zero, sequence is irrelevant
409  };
410 };
411 
412 
413 #endif // EDA_TEXT_H_
void SetMirrored(bool isMirrored)
Definition: eda_text.h:191
void SetTextAngle(double aAngle)
Definition: eda_text.h:169
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:242
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:62
wxString m_Text
Definition: eda_text.h:374
bool IsMultilineAllowed() const
Definition: eda_text.h:201
const wxPoint & GetTextPos() const
Definition: eda_text.h:237
UNIQUE_MUTEX & operator=(const UNIQUE_MUTEX &)
Definition: eda_text.h:50
bool IsItalic() const
Definition: eda_text.h:183
void SetItalic(bool isItalic)
Definition: eda_text.h:182
TEXT_EFFECTS m_e
Definition: eda_text.h:399
int GetTextWidth() const
Definition: eda_text.h:231
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:236
void SetVisible(bool aVisible)
Definition: eda_text.h:188
Template specialization to enable wxStrings for certain containers (e.g. unordered_map) ...
Definition: bitmap.cpp:55
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
signed char vjustify
vertical justification
Definition: eda_text.h:109
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:227
double GetTextAngleDegrees() const
Definition: eda_text.h:179
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:203
wxPoint pos
Definition: eda_text.h:113
bool IsBold() const
Definition: eda_text.h:186
double GetTextAngle() const
Definition: eda_text.h:177
wxSize size
Definition: eda_text.h:110
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:167
void SetTextX(int aX)
Definition: eda_text.h:239
virtual wxString GetShownText() const
Returns the string actually shown after processing of the base text.
Definition: eda_text.h:148
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:127
void Bit(int aBit, bool aValue)
Definition: eda_text.h:115
Struct TEXT_EFFECTS is a bucket for text effects.
Definition: eda_text.h:97
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:77
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:143
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:207
short bits
any set of booleans a client uses.
Definition: eda_text.h:107
TE_FLAGS
EDA_TEXT effects bools.
Definition: eda_text.h:402
void Empty()
Definition: eda_text.h:244
void SetMultilineAllowed(bool aAllow)
Function SetMultiLineAllowed.
Definition: eda_text.h:200
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:69
void SetTextWidth(int aWidth)
Definition: eda_text.h:230
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:204
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool Bit(int aBit) const
Definition: eda_text.h:116
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:206
bool IsMirrored() const
Definition: eda_text.h:192
bool IsVisible() const
Definition: eda_text.h:189
TEXT_EFFECTS(int aSetOfBits=0)
Definition: eda_text.h:99
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
void SetTextHeight(int aHeight)
Definition: eda_text.h:233
The common library.
UNIQUE_MUTEX(const UNIQUE_MUTEX &)
Definition: eda_text.h:49
int GetTextHeight() const
Definition: eda_text.h:234
double angle
now: 0.1 degrees; future: degrees
Definition: eda_text.h:112
const wxSize & GetTextSize() const
Definition: eda_text.h:228
Basic classes for most KiCad items.
UNIQUE_MUTEX()
Definition: eda_text.h:48
signed char hjustify
horizontal justification
Definition: eda_text.h:108
double GetTextAngleRadians() const
Definition: eda_text.h:180
void SetBold(bool aBold)
Definition: eda_text.h:185
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:161
void SetTextY(int aY)
Definition: eda_text.h:240
virtual void SetText(const wxString &aText)
Definition: eda_text.h:154
int penwidth
Definition: eda_text.h:111
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:377