KiCad PCB EDA Suite
lib_text.cpp
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) 2004-2019 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
28 #include <fctsys.h>
29 #include <gr_basic.h>
30 #include <macros.h>
31 #include <sch_draw_panel.h>
32 #include <plotter.h>
33 #include <draw_graphic_text.h>
34 #include <trigo.h>
35 #include <base_units.h>
36 #include <msgpanel.h>
37 #include <bitmaps.h>
38 
39 #include <lib_draw_item.h>
40 #include <general.h>
41 #include <transform.h>
42 #include <lib_text.h>
43 
44 
46  LIB_ITEM( LIB_TEXT_T, aParent ),
47  EDA_TEXT()
48 {
49  SetTextSize( wxSize( 50, 50 ) );
50  m_rotate = false;
51  m_updateText = false;
52 }
53 
54 
55 void LIB_TEXT::ViewGetLayers( int aLayers[], int& aCount ) const
56 {
57  aCount = 1;
58  aLayers[0] = LAYER_DEVICE;
59 }
60 
61 
62 bool LIB_TEXT::HitTest( const wxPoint& aPosition, int aAccuracy ) const
63 {
64  EDA_TEXT tmp_text( *this );
66 
67  /* The text orientation may need to be flipped if the
68  * transformation matrix causes xy axes to be flipped.
69  * this simple algo works only for schematic matrix (rot 90 or/and mirror)
70  */
71  bool t1 = ( DefaultTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
72 
74  return tmp_text.TextHitTest( aPosition, aAccuracy );
75 }
76 
77 
79 {
80  LIB_TEXT* newitem = new LIB_TEXT( nullptr );
81 
82  newitem->m_Unit = m_Unit;
83  newitem->m_Convert = m_Convert;
84  newitem->m_Flags = m_Flags;
85  newitem->m_Text = m_Text;
86 
87  newitem->SetEffects( *this );
88 
89  return newitem;
90 }
91 
92 
93 int LIB_TEXT::compare( const LIB_ITEM& other ) const
94 {
95  wxASSERT( other.Type() == LIB_TEXT_T );
96 
97  const LIB_TEXT* tmp = ( LIB_TEXT* ) &other;
98 
99  int result = m_Text.CmpNoCase( tmp->m_Text );
100 
101  if( result != 0 )
102  return result;
103 
104  if( GetTextPos().x != tmp->GetTextPos().x )
105  return GetTextPos().x - tmp->GetTextPos().x;
106 
107  if( GetTextPos().y != tmp->GetTextPos().y )
108  return GetTextPos().y - tmp->GetTextPos().y;
109 
110  if( GetTextWidth() != tmp->GetTextWidth() )
111  return GetTextWidth() - tmp->GetTextWidth();
112 
113  if( GetTextHeight() != tmp->GetTextHeight() )
114  return GetTextHeight() - tmp->GetTextHeight();
115 
116  return 0;
117 }
118 
119 
120 void LIB_TEXT::Offset( const wxPoint& aOffset )
121 {
122  EDA_TEXT::Offset( aOffset );
123 }
124 
125 
126 bool LIB_TEXT::Inside( EDA_RECT& rect ) const
127 {
128  return rect.Intersects( GetBoundingBox() );
129 }
130 
131 
132 void LIB_TEXT::MoveTo( const wxPoint& newPosition )
133 {
134  SetTextPos( newPosition );
135 }
136 
137 
138 void LIB_TEXT::MirrorHorizontal( const wxPoint& center )
139 {
140  int x = GetTextPos().x;
141 
142  x -= center.x;
143  x *= -1;
144  x += center.x;
145 
146  SetTextX( x );
147 }
148 
149 
150 void LIB_TEXT::MirrorVertical( const wxPoint& center )
151 {
152  int y = GetTextPos().y;
153 
154  y -= center.y;
155  y *= -1;
156  y += center.y;
157 
158  SetTextY( y );
159 }
160 
161 
162 void LIB_TEXT::Rotate( const wxPoint& center, bool aRotateCCW )
163 {
164  int rot_angle = aRotateCCW ? -900 : 900;
165 
166  wxPoint pt = GetTextPos();
167  RotatePoint( &pt, center, rot_angle );
168  SetTextPos( pt );
169 
170  SetTextAngle( GetTextAngle() != 0.0 ? 0 : 900 );
171 }
172 
173 
174 void LIB_TEXT::Plot( PLOTTER* plotter, const wxPoint& offset, bool fill,
175  const TRANSFORM& aTransform )
176 {
177  wxASSERT( plotter != NULL );
178 
179  EDA_RECT bBox = GetBoundingBox();
180  // convert coordinates from draw Y axis to libedit Y axis
181  bBox.RevertYAxis();
182  wxPoint txtpos = bBox.Centre();
183 
184  /* The text orientation may need to be flipped if the
185  * transformation matrix causes xy axes to be flipped. */
186  int t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
187  wxPoint pos = aTransform.TransformCoordinate( txtpos ) + offset;
188 
189  // Get color
190  COLOR4D color;
191 
192  if( plotter->GetColorMode() ) // Used normal color or selected color
194  else
196 
197  plotter->Text( pos, color, GetShownText(),
200  GetPenSize(), IsItalic(), IsBold() );
201 }
202 
203 
205 {
206  int pensize = GetThickness();
207 
208  if( pensize == 0 ) // Use default values for pen size
209  {
210  if( IsBold() )
211  pensize = GetPenSizeForBold( GetTextWidth() );
212  else
213  pensize = GetDefaultLineThickness();
214  }
215 
216  // Clip pen size for small texts:
217  pensize = Clamp_Text_PenSize( pensize, GetTextSize(), IsBold() );
218  return pensize;
219 }
220 
221 
222 void LIB_TEXT::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
223  void* aData, const TRANSFORM& aTransform )
224 {
226 
227  /* Calculate the text orientation, according to the component
228  * orientation/mirror (needed when draw text in schematic)
229  */
230  int orient = GetTextAngle();
231 
232  if( aTransform.y1 ) // Rotate component 90 degrees.
233  {
234  if( orient == TEXT_ANGLE_HORIZ )
235  orient = TEXT_ANGLE_VERT;
236  else
237  orient = TEXT_ANGLE_HORIZ;
238  }
239 
240  /* Calculate the text justification, according to the component
241  * orientation/mirror this is a bit complicated due to cumulative
242  * calculations:
243  * - numerous cases (mirrored or not, rotation)
244  * - the DrawGraphicText function recalculate also H and H justifications
245  * according to the text orientation.
246  * - When a component is mirrored, the text is not mirrored and
247  * justifications are complicated to calculate
248  * so the more easily way is to use no justifications ( Centered text )
249  * and use GetBoundaryBox to know the text coordinate considered as centered
250  */
251  EDA_RECT bBox = GetBoundingBox();
252 
253  // convert coordinates from draw Y axis to libedit Y axis:
254  bBox.RevertYAxis();
255  wxPoint txtpos = bBox.Centre();
256 
257  // Calculate pos according to mirror/rotation.
258  txtpos = aTransform.TransformCoordinate( txtpos ) + aOffset;
259 
260  EDA_RECT* clipbox = aPanel? aPanel->GetClipBox() : NULL;
261  DrawGraphicText( clipbox, aDC, txtpos, color, GetShownText(), orient, GetTextSize(),
263  IsItalic(), IsBold() );
264 }
265 
266 
268 {
269  wxString msg;
270 
271  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
272 
273  msg = MessageTextFromValue( aUnits, GetThickness(), true );
274 
275  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
276 }
277 
278 
280 {
281  /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
282  * calling GetTextBox() that works using top to bottom Y axis orientation.
283  */
284  EDA_RECT rect = GetTextBox( -1, -1, true );
285  rect.RevertYAxis();
286 
287  // We are using now a bottom to top Y axis.
288  wxPoint orig = rect.GetOrigin();
289  wxPoint end = rect.GetEnd();
290 
291  RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
292  RotatePoint( &end, GetTextPos(), -GetTextAngle() );
293 
294  rect.SetOrigin( orig );
295  rect.SetEnd( end );
296 
297  // We are using now a top to bottom Y axis:
298  rect.RevertYAxis();
299 
300  return rect;
301 }
302 
303 
305 {
306  if( InEditMode() )
307  m_rotate = true;
308  else
310 }
311 
312 
313 void LIB_TEXT::SetText( const wxString& aText )
314 {
315  if( aText == m_Text )
316  return;
317 
318  if( InEditMode() )
319  {
320  m_savedText = aText;
321  m_updateText = true;
322  }
323  else
324  {
325  m_Text = aText;
326  }
327 }
328 
329 
330 wxString LIB_TEXT::GetSelectMenuText( EDA_UNITS_T aUnits ) const
331 {
332  return wxString::Format( _( "Graphic Text \"%s\"" ), ShortenedShownText() );
333 }
334 
335 
336 BITMAP_DEF LIB_TEXT::GetMenuImage() const
337 {
338  return text_xpm;
339 }
340 
341 
342 void LIB_TEXT::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
343 {
344  LIB_ITEM::BeginEdit( aEditMode, aPosition );
345 
346  if( aEditMode == IS_MOVED )
347  {
349  m_initialCursorPos = aPosition;
350  }
351  else
352  {
353  SetTextPos( aPosition );
354  }
355 }
356 
357 
358 void LIB_TEXT::EndEdit( const wxPoint& aPosition )
359 {
360  LIB_ITEM::EndEdit( aPosition );
361 
362  m_rotate = false;
363  m_updateText = false;
364 }
365 
366 
367 void LIB_TEXT::CalcEdit( const wxPoint& aPosition )
368 {
369  DBG(printf("textCalcEdit %d %d\n", aPosition.x, aPosition.y );)
370 
371  if( m_rotate )
372  {
374  m_rotate = false;
375  }
376 
377  if( m_updateText )
378  {
379  std::swap( m_Text, m_savedText );
380  m_updateText = false;
381  }
382 
383  if( IsNew() )
384  {
385  SetTextPos( aPosition );
386  }
387  else if( IsMoving() )
388  {
389  MoveTo( m_initialPos + aPosition - m_initialCursorPos );
390  DBG(printf("%p: move %d %d\n", this, GetPosition().x, GetPosition().y );)
391  }
392 
393  DBG(printf("%p: move2 %d %d\n", this, GetPosition().x, GetPosition().y );)
394 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:173
Definition: colors.h:57
#define TEXT_ANGLE_HORIZ
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_text.cpp:132
void GetMsgPanelInfo(EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
bool IsBold() const
Definition: eda_text.h:190
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:246
void Plot(PLOTTER *aPlotter, const wxPoint &aOffset, bool aFill, const TRANSFORM &aTransform) override
Plot the draw item using the plot object.
Definition: lib_text.cpp:174
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aBold)
Function Clamp_Text_PenSize As a rule, pen width should not be >1/4em, otherwise the character will b...
virtual void EndEdit(const wxPoint &aPosition)
End an object editing action.
wxString m_Text
Definition: eda_text.h:378
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
bool IsSelected() const
Definition: base_struct.h:227
#define TEXT_ANGLE_VERT
Implementation of conversion functions that require both schematic and board internal units.
void EndEdit(const wxPoint &aPosition) override
End an object editing action.
Definition: lib_text.cpp:358
Define a symbol library graphical text item.
Definition: lib_text.h:44
wxString GetSelectMenuText(EDA_UNITS_T aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Definition: lib_text.cpp:330
virtual COLOR4D GetDefaultColor()
void GetMsgPanelInfo(EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
Definition: lib_text.cpp:267
void CalcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_text.cpp:367
void SetEffects(const EDA_TEXT &aSrc)
Function SetEffects sets the text effects from another instance.
Definition: eda_text.cpp:71
bool IsMoving() const
Definition: base_struct.h:224
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_text.cpp:138
wxPoint GetPosition() const override
Return the current draw object position.
Definition: lib_text.h:110
int color
Definition: DXF_plotter.cpp:62
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i....
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:240
double GetTextAngle() const
Definition: eda_text.h:181
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:124
int m_Unit
Unit identification for multiple parts per package.
Definition: lib_draw_item.h:89
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:171
int x1
Definition: transform.h:48
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:231
virtual EDA_RECT * GetClipBox()
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
void Rotate() override
Rotate the draw item.
Definition: lib_text.cpp:304
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size)
Definition: eda_rect.h:144
bool Inside(EDA_RECT &aRect) const override
Test if any part of the draw object is inside rectangle bounds of aRect.
Definition: lib_text.cpp:126
void DrawGraphicText(EDA_RECT *aClipBox, wxDC *aDC, const wxPoint &aPos, COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, void(*aCallback)(int x0, int y0, int xf, int yf, void *aData), void *aCallbackData, PLOTTER *aPlotter)
Function DrawGraphicText Draw a graphic text (like module texts)
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
The base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:67
void ViewGetLayers(int aLayers[], int &aCount) const override
Function ViewGetLayers() Returns the all the layers within the VIEW the object is painted on.
Definition: lib_text.cpp:55
This file contains miscellaneous commonly used macros and functions.
bool IsNew() const
Definition: base_struct.h:222
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item.
Definition: lib_text.cpp:62
int GetTextHeight() const
Definition: eda_text.h:238
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL)
Draws text with the plotter.
void SetTextX(int aX)
Definition: eda_text.h:243
bool IsItalic() const
Definition: eda_text.h:187
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels,...
Definition: eda_text.h:128
int y1
Definition: transform.h:49
const wxPoint GetEnd() const
Definition: eda_rect.h:114
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
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
bool InEditMode() const
Return the draw item editing mode status.
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
void SetEnd(int x, int y)
Definition: eda_rect.h:134
const wxSize & GetTextSize() const
Definition: eda_text.h:232
void drawGraphic(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Draw the item on aPanel.
Definition: lib_text.cpp:222
Define a library symbol object.
unsigned STATUS_FLAGS
Definition: base_struct.h:150
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:96
bool m_rotate
Flag to indicate a rotation occurred while editing.
Definition: lib_text.h:47
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Definition: lib_text.cpp:150
virtual void BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aPosition)
Begin an editing a component library draw item in aEditMode at aPosition.
int GetPenSize() const override
Definition: lib_text.cpp:204
wxString m_savedText
Temporary storage for the string when editing.
Definition: lib_text.h:46
Base plotter engine class.
Definition: plotter.h:97
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_text.cpp:120
int GetTextWidth() const
Definition: eda_text.h:235
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: lib_text.cpp:336
TRANSFORM DefaultTransform
Definition: eeschema.cpp:59
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
const EDA_RECT GetBoundingBox() const override
Definition: lib_text.cpp:279
wxPoint m_initialPos
Temporary position when moving an existing item.
void SetText(const wxString &aText) override
Sets the text item string to aText.
Definition: lib_text.cpp:313
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:181
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
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:157
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
wxPoint Centre() const
Definition: eda_rect.h:60
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
int m_Convert
Shape identification for alternate body styles.
Definition: lib_draw_item.h:96
const wxPoint & GetTextPos() const
Definition: eda_text.h:241
COLOR4D GetItemSelectedColor()
#define DBG(x)
Definition: fctsys.h:33
static const char * text_xpm[]
virtual wxString GetShownText() const
Returns the string actually shown after processing of the base text.
Definition: eda_text.h:152
Definition: colors.h:45
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_text.cpp:78
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
Message panel definition file.
void BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aStartPoint) override
Begin an editing a component library draw item in aEditMode at aPosition.
Definition: lib_text.cpp:342
bool m_updateText
Flag to indicate text change occurred while editing.
Definition: lib_text.h:48
LIB_TEXT(LIB_PART *aParent)
Definition: lib_text.cpp:45
EDA_UNITS_T
Definition: common.h:157
int compare(const LIB_ITEM &aOther) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_text.cpp:93
void SetTextY(int aY)
Definition: eda_text.h:244
wxPoint m_initialCursorPos
Initial cursor position at the beginning of a move.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:204
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
#define IS_MOVED
Item being moved.
Definition: base_struct.h:113
bool GetColorMode() const
Definition: plotter.h:131