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 <trigo.h> // NORMALIZE_ANGLE_POS( angle );
34 #include <common.h> // wxStringSplit
35 #include <gr_basic.h> // EDA_DRAW_MODE_T
36 #include <base_struct.h> // EDA_RECT
37 
38 
39 // part of the kicad_plugin.h family of defines.
40 // See kicad_plugin.h for the choice of the value
41 // When set when calling EDA_TEXT::Format, disable writing the "hide" keyword in save file
42 #define CTL_OMIT_HIDE (1 << 6)
43 
44 
45 // Graphic Text justify:
46 // Values -1,0,1 are used in computations, do not change them
51 };
52 
53 
58 };
59 
60 
61 /* Options to draw items with thickness ( segments, arcs, circles, texts...) */
63  FILLED = true, // normal mode: solid segments
64  SKETCH = false // sketch mode: draw segments outlines only
65 };
66 
67 
74 #define DEFAULT_SIZE_TEXT 60 // 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  short 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 
114 class EDA_TEXT
115 {
116 public:
117  EDA_TEXT( const wxString& text = wxEmptyString );
118 
119  // Do not create a copy constructor & operator=.
120  // The ones generated by the compiler are adequate.
121 
122  virtual ~EDA_TEXT();
123 
130  const wxString& GetText() const { return m_Text; }
131 
135  virtual wxString GetShownText() const { return m_Text; }
136 
139  wxString ShortenedShownText() const;
140 
141  virtual void SetText( const wxString& aText ) { m_Text = aText; }
142 
148  void SetThickness( int aNewThickness ) { m_e.penwidth = aNewThickness; };
149 
154  int GetThickness() const { return m_e.penwidth; };
155 
156  void SetTextAngle( double aAngle )
157  {
158  // Higher level classes may be more restrictive than this by
159  // overloading SetTextAngle() (probably non-virtual) or merely
160  // calling EDA_TEXT::SetTextAngle() after clamping aAngle
161  // before calling this lowest inline accessor.
162  m_e.angle = aAngle;
163  }
164  double GetTextAngle() const { return m_e.angle; }
165 
166  double GetTextAngleDegrees() const { return GetTextAngle() / 10.0; }
167  double GetTextAngleRadians() const { return GetTextAngle() * M_PI/1800; }
168 
169  void SetItalic( bool isItalic ) { m_e.Bit( TE_ITALIC, isItalic ); }
170  bool IsItalic() const { return m_e.Bit( TE_ITALIC ); }
171 
172  void SetBold( bool aBold ) { m_e.Bit( TE_BOLD, aBold); }
173  bool IsBold() const { return m_e.Bit( TE_BOLD ); }
174 
175  void SetVisible( bool aVisible ) { m_e.Bit( TE_VISIBLE, aVisible ); }
176  bool IsVisible() const { return m_e.Bit( TE_VISIBLE ); }
177 
178  void SetMirrored( bool isMirrored ) { m_e.Bit( TE_MIRROR, isMirrored ); }
179  bool IsMirrored() const { return m_e.Bit( TE_MIRROR ); }
180 
187  void SetMultilineAllowed( bool aAllow ) { m_e.Bit( TE_MULTILINE, aAllow ); }
188  bool IsMultilineAllowed() const { return m_e.Bit( TE_MULTILINE ); }
189 
192 
193  void SetHorizJustify( EDA_TEXT_HJUSTIFY_T aType ) { m_e.hjustify = aType; };
194  void SetVertJustify( EDA_TEXT_VJUSTIFY_T aType ) { m_e.vjustify = aType; };
195 
202  void SetEffects( const EDA_TEXT& aSrc );
203 
210  void SwapEffects( EDA_TEXT& aTradingPartner );
211 
212  bool IsDefaultFormatting() const;
213 
214  void SetTextSize( const wxSize& aNewSize ) { m_e.size = aNewSize; };
215  const wxSize& GetTextSize() const { return m_e.size; };
216 
217  void SetTextWidth( int aWidth ) { m_e.size.x = aWidth; }
218  int GetTextWidth() const { return m_e.size.x; }
219 
220  void SetTextHeight( int aHeight ) { m_e.size.y = aHeight; }
221  int GetTextHeight() const { return m_e.size.y; }
222 
223  void SetTextPos( const wxPoint& aPoint ) { m_e.pos = aPoint; }
224  const wxPoint& GetTextPos() const { return m_e.pos; }
225 
226  void SetTextX( int aX ) { m_e.pos.x = aX; }
227  void SetTextY( int aY ) { m_e.pos.y = aY; }
228 
229  void Offset( const wxPoint& aOffset ) { m_e.pos += aOffset; }
230 
231  void Empty() { m_Text.Empty(); }
232 
243  void Draw( EDA_RECT* aClipBox, wxDC* aDC,
244  const wxPoint& aOffset, COLOR4D aColor,
245  GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aDisplay_mode = FILLED,
246  COLOR4D aAnchor_color = COLOR4D::UNSPECIFIED );
247 
254  void TransformTextShapeToSegmentList( std::vector<wxPoint>& aCornerBuffer ) const;
255 
263  virtual bool TextHitTest( const wxPoint& aPoint, int aAccuracy = 0 ) const;
264 
274  virtual bool TextHitTest( const EDA_RECT& aRect, bool aContains = false, int aAccuracy = 0 ) const;
275 
282  int LenSize( const wxString& aLine ) const;
283 
300  EDA_RECT GetTextBox( int aLine = -1, int aThickness = -1, bool aInvertY = false ) const;
301 
316  int GetInterline( int aTextThickness = -1 ) const;
317 
322  wxString GetTextStyleName();
323 
334  std::vector<wxPoint>& aPositions, int aLineCount ) const;
344  virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
345  throw( IO_ERROR );
346 
347 protected:
348  wxString m_Text;
349 
350 private:
364  void drawOneLineOfText( EDA_RECT* aClipBox, wxDC* aDC,
365  const wxPoint& aOffset, COLOR4D aColor,
366  GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aFillMode,
367  const wxString& aText, const wxPoint& aPos );
368 
369  // Private text effects data. API above provides accessor funcs.
371 
373  enum TE_FLAGS {
374  // start at zero, sequence is irrelevant
380  };
381 };
382 
383 
384 #endif // EDA_TEXT_H_
void SetMirrored(bool isMirrored)
Definition: eda_text.h:178
void SetTextAngle(double aAngle)
Definition: eda_text.h:156
EDA_TEXT(const wxString &text=wxEmptyString)
Definition: eda_text.cpp:54
int GetInterline(int aTextThickness=-1) const
Function GetInterline return the distance between 2 text lines has meaning only for multiline texts...
Definition: eda_text.cpp:107
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:229
void SwapEffects(EDA_TEXT &aTradingPartner)
Function SwapEffects swaps the text effects of the two involved instances.
Definition: eda_text.cpp:74
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:47
wxString m_Text
Definition: eda_text.h:348
bool IsMultilineAllowed() const
Definition: eda_text.h:188
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:92
const wxPoint & GetTextPos() const
Definition: eda_text.h:224
bool IsItalic() const
Definition: eda_text.h:170
void SetEffects(const EDA_TEXT &aSrc)
Function SetEffects sets the text effects from another instance.
Definition: eda_text.cpp:68
void SetItalic(bool isItalic)
Definition: eda_text.h:169
TEXT_EFFECTS m_e
Definition: eda_text.h:370
int GetTextWidth() const
Definition: eda_text.h:218
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:223
void SetVisible(bool aVisible)
Definition: eda_text.h:175
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:96
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
double GetTextAngleDegrees() const
Definition: eda_text.h:166
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:190
wxPoint pos
Definition: eda_text.h:100
bool IsBold() const
Definition: eda_text.h:173
double GetTextAngle() const
Definition: eda_text.h:164
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:369
wxSize size
Definition: eda_text.h:97
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:154
bool IsDefaultFormatting() const
Definition: eda_text.cpp:414
void SetTextX(int aX)
Definition: eda_text.h:226
virtual wxString GetShownText() const
Returns the string actually shown after processing of the base text.
Definition: eda_text.h:135
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:114
void Bit(int aBit, bool aValue)
Definition: eda_text.h:102
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:115
Struct TEXT_EFFECTS is a bucket for text effects.
Definition: eda_text.h:84
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:267
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:41
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:430
EDA_DRAW_MODE_T
Definition: eda_text.h:62
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:194
short bits
any set of booleans a client uses.
Definition: eda_text.h:94
TE_FLAGS
EDA_TEXT effects bools.
Definition: eda_text.h:373
void Empty()
Definition: eda_text.h:231
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:327
virtual ~EDA_TEXT()
Definition: eda_text.cpp:63
void SetMultilineAllowed(bool aAllow)
Function SetMultiLineAllowed.
Definition: eda_text.h:187
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:54
void SetTextWidth(int aWidth)
Definition: eda_text.h:217
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:191
wxString GetTextStyleName()
Function GetTextStyleName.
Definition: eda_text.cpp:393
bool Bit(int aBit) const
Definition: eda_text.h:103
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:193
bool IsMirrored() const
Definition: eda_text.h:179
bool IsVisible() const
Definition: eda_text.h:176
TEXT_EFFECTS(int aSetOfBits=0)
Definition: eda_text.h:86
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:507
Class EDA_RECT handles the component boundary box.
void SetTextHeight(int aHeight)
Definition: eda_text.h:220
The common library.
int GetTextHeight() const
Definition: eda_text.h:221
double angle
now: 0.1 degrees; future: degrees
Definition: eda_text.h:99
const wxSize & GetTextSize() const
Definition: eda_text.h:215
Basic classes for most KiCad items.
int LenSize(const wxString &aLine) const
Function LenSize.
Definition: eda_text.cpp:80
signed char hjustify
horizontal justification
Definition: eda_text.h:95
double GetTextAngleRadians() const
Definition: eda_text.h:167
void SetBold(bool aBold)
Definition: eda_text.h:172
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:148
void SetTextY(int aY)
Definition: eda_text.h:227
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:292
virtual void SetText(const wxString &aText)
Definition: eda_text.h:141
int penwidth
Definition: eda_text.h:98
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39