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-2017 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 <class_drawpanel.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 bool LIB_TEXT::HitTest( const wxPoint& aPosition ) const
56 {
57  return HitTest( aPosition, 0, DefaultTransform );
58 }
59 
60 
61 bool LIB_TEXT::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
62 {
63  if( aThreshold < 0 )
64  aThreshold = 0;
65 
66  EDA_TEXT tmp_text( *this );
67  tmp_text.SetTextPos( aTransform.TransformCoordinate( GetTextPos() ) );
68 
69  /* The text orientation may need to be flipped if the
70  * transformation matrix causes xy axes to be flipped.
71  * this simple algo works only for schematic matrix (rot 90 or/and mirror)
72  */
73  bool t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
74 
76  return tmp_text.TextHitTest( aPosition );
77 }
78 
79 
81 {
82  LIB_TEXT* newitem = new LIB_TEXT(NULL);
83 
84  newitem->m_Unit = m_Unit;
85  newitem->m_Convert = m_Convert;
86  newitem->m_Flags = m_Flags;
87  newitem->m_Text = m_Text;
88 
89  newitem->SetEffects( *this );
90 
91  return newitem;
92 }
93 
94 
95 int LIB_TEXT::compare( const LIB_ITEM& other ) const
96 {
97  wxASSERT( other.Type() == LIB_TEXT_T );
98 
99  const LIB_TEXT* tmp = ( LIB_TEXT* ) &other;
100 
101  int result = m_Text.CmpNoCase( tmp->m_Text );
102 
103  if( result != 0 )
104  return result;
105 
106  if( GetTextPos().x != tmp->GetTextPos().x )
107  return GetTextPos().x - tmp->GetTextPos().x;
108 
109  if( GetTextPos().y != tmp->GetTextPos().y )
110  return GetTextPos().y - tmp->GetTextPos().y;
111 
112  if( GetTextWidth() != tmp->GetTextWidth() )
113  return GetTextWidth() - tmp->GetTextWidth();
114 
115  if( GetTextHeight() != tmp->GetTextHeight() )
116  return GetTextHeight() - tmp->GetTextHeight();
117 
118  return 0;
119 }
120 
121 
122 void LIB_TEXT::SetOffset( const wxPoint& aOffset )
123 {
124  EDA_TEXT::Offset( aOffset );
125 }
126 
127 
128 bool LIB_TEXT::Inside( EDA_RECT& rect ) const
129 {
130  return rect.Intersects( GetBoundingBox() );
131 }
132 
133 
134 void LIB_TEXT::Move( const wxPoint& newPosition )
135 {
136  SetTextPos( newPosition );
137 }
138 
139 
140 void LIB_TEXT::MirrorHorizontal( const wxPoint& center )
141 {
142  int x = GetTextPos().x;
143 
144  x -= center.x;
145  x *= -1;
146  x += center.x;
147 
148  SetTextX( x );
149 }
150 
151 
152 void LIB_TEXT::MirrorVertical( const wxPoint& center )
153 {
154  int y = GetTextPos().y;
155 
156  y -= center.y;
157  y *= -1;
158  y += center.y;
159 
160  SetTextY( y );
161 }
162 
163 
164 void LIB_TEXT::Rotate( const wxPoint& center, bool aRotateCCW )
165 {
166  int rot_angle = aRotateCCW ? -900 : 900;
167 
168  wxPoint pt = GetTextPos();
169  RotatePoint( &pt, center, rot_angle );
170  SetTextPos( pt );
171 
172  SetTextAngle( GetTextAngle() != 0.0 ? 0 : 900 );
173 }
174 
175 
176 void LIB_TEXT::Plot( PLOTTER* plotter, const wxPoint& offset, bool fill,
177  const TRANSFORM& aTransform )
178 {
179  wxASSERT( plotter != NULL );
180 
181  EDA_RECT bBox = GetBoundingBox();
182  // convert coordinates from draw Y axis to libedit Y axis
183  bBox.RevertYAxis();
184  wxPoint txtpos = bBox.Centre();
185 
186  /* The text orientation may need to be flipped if the
187  * transformation matrix causes xy axes to be flipped. */
188  int t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
189  wxPoint pos = aTransform.TransformCoordinate( txtpos ) + offset;
190 
191  // Get color
192  COLOR4D color;
193 
194  if( plotter->GetColorMode() ) // Used normal color or selected color
196  else
197  color = COLOR4D::BLACK;
198 
199  plotter->Text( pos, color, GetShownText(),
202  GetPenSize(), IsItalic(), IsBold() );
203 }
204 
205 
207 {
208  int pensize = GetThickness();
209 
210  if( pensize == 0 ) // Use default values for pen size
211  {
212  if( IsBold() )
213  pensize = GetPenSizeForBold( GetTextWidth() );
214  else
215  pensize = GetDefaultLineThickness();
216  }
217 
218  // Clip pen size for small texts:
219  pensize = Clamp_Text_PenSize( pensize, GetTextSize(), IsBold() );
220  return pensize;
221 }
222 
223 
224 void LIB_TEXT::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
225  COLOR4D aColor, GR_DRAWMODE aDrawMode, void* aData,
226  const TRANSFORM& aTransform )
227 {
229 
230  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
231  {
232  if( IsSelected() )
233  color = GetItemSelectedColor();
234  }
235  else
236  {
237  color = aColor;
238  }
239 
240  GRSetDrawMode( aDC, aDrawMode );
241 
242  /* Calculate the text orientation, according to the component
243  * orientation/mirror (needed when draw text in schematic)
244  */
245  int orient = GetTextAngle();
246 
247  if( aTransform.y1 ) // Rotate component 90 degrees.
248  {
249  if( orient == TEXT_ANGLE_HORIZ )
250  orient = TEXT_ANGLE_VERT;
251  else
252  orient = TEXT_ANGLE_HORIZ;
253  }
254 
255  /* Calculate the text justification, according to the component
256  * orientation/mirror this is a bit complicated due to cumulative
257  * calculations:
258  * - numerous cases (mirrored or not, rotation)
259  * - the DrawGraphicText function recalculate also H and H justifications
260  * according to the text orientation.
261  * - When a component is mirrored, the text is not mirrored and
262  * justifications are complicated to calculate
263  * so the more easily way is to use no justifications ( Centered text )
264  * and use GetBoundaryBox to know the text coordinate considered as centered
265  */
266  EDA_RECT bBox = GetBoundingBox();
267 
268  // convert coordinates from draw Y axis to libedit Y axis:
269  bBox.RevertYAxis();
270  wxPoint txtpos = bBox.Centre();
271 
272  // Calculate pos according to mirror/rotation.
273  txtpos = aTransform.TransformCoordinate( txtpos ) + aOffset;
274 
275  EDA_RECT* clipbox = aPanel? aPanel->GetClipBox() : NULL;
276  DrawGraphicText( clipbox, aDC, txtpos, color, GetShownText(), orient, GetTextSize(),
278  IsItalic(), IsBold() );
279 
280 
281  /* Enable this to draw the bounding box around the text field to validate
282  * the bounding box calculations.
283  */
284 #if 0
285  // bBox already uses libedit Y axis.
286  bBox = aTransform.TransformCoordinate( bBox );
287  bBox.Move( aOffset );
288  GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
289 #endif
290 }
291 
292 
294 {
295  wxString msg;
296 
297  LIB_ITEM::GetMsgPanelInfo( aList );
298 
299  msg = StringFromValue( g_UserUnit, GetThickness(), true );
300 
301  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
302 }
303 
304 
306 {
307  /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
308  * calling GetTextBox() that works using top to bottom Y axis orientation.
309  */
310  EDA_RECT rect = GetTextBox( -1, -1, true );
311  rect.RevertYAxis();
312 
313  // We are using now a bottom to top Y axis.
314  wxPoint orig = rect.GetOrigin();
315  wxPoint end = rect.GetEnd();
316 
317  RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
318  RotatePoint( &end, GetTextPos(), -GetTextAngle() );
319 
320  rect.SetOrigin( orig );
321  rect.SetEnd( end );
322 
323  // We are using now a top to bottom Y axis:
324  rect.RevertYAxis();
325 
326  return rect;
327 }
328 
329 
331 {
332  if( InEditMode() )
333  {
334  m_rotate = true;
335  }
336  else
337  {
339  }
340 }
341 
342 
343 void LIB_TEXT::SetText( const wxString& aText )
344 {
345  if( aText == m_Text )
346  return;
347 
348  if( InEditMode() )
349  {
350  m_savedText = aText;
351  m_updateText = true;
352  }
353  else
354  {
355  m_Text = aText;
356  }
357 }
358 
359 
361 {
362  wxString msg;
363  msg.Printf( _( "Graphic Text %s" ), GetChars( ShortenedShownText() ) );
364  return msg;
365 }
366 
367 
369 {
370  return text_xpm;
371 }
372 
373 
374 void LIB_TEXT::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
375 {
376  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED ) ) != 0,
377  wxT( "Invalid edit mode for LIB_TEXT object." ) );
378 
379  if( aEditMode == IS_MOVED )
380  {
382  m_initialCursorPos = aPosition;
384  }
385  else
386  {
387  SetTextPos( aPosition );
388  }
389 
390  m_Flags = aEditMode;
391 }
392 
393 
394 bool LIB_TEXT::ContinueEdit( const wxPoint aPosition )
395 {
396  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED ) ) != 0, false,
397  wxT( "Bad call to ContinueEdit(). Text is not being edited." ) );
398 
399  return false;
400 }
401 
402 
403 void LIB_TEXT::EndEdit( const wxPoint& aPosition, bool aAbort )
404 {
405  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED ) ) != 0,
406  wxT( "Bad call to EndEdit(). Text is not being edited." ) );
407 
408  m_Flags = 0;
409  m_rotate = false;
410  m_updateText = false;
411  SetEraseLastDrawItem( false );
412 }
413 
414 
415 void LIB_TEXT::calcEdit( const wxPoint& aPosition )
416 {
417  if( m_rotate )
418  {
420  m_rotate = false;
421  }
422 
423  if( m_updateText )
424  {
425  std::swap( m_Text, m_savedText );
426  m_updateText = false;
427  }
428 
429  if( m_Flags == IS_NEW )
430  {
432  SetTextPos( aPosition );
433  }
434  else if( m_Flags == IS_MOVED )
435  {
436  Move( m_initialPos + aPosition - m_initialCursorPos );
437  }
438 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:154
Definition: colors.h:57
KICAD_T Type() const
Function Type()
Definition: base_struct.h:227
#define TEXT_ANGLE_HORIZ
Frequent text rotations, used with {Set,Get}TextAngle(), in 0.1 degrees for now, hoping to migrate to...
Definition: common.h:91
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:227
void BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aStartPoint=wxPoint(0, 0)) override
Begin an editing a component library draw item in aEditMode at aPosition.
Definition: lib_text.cpp:374
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:176
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...
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
wxString m_Text
Definition: eda_text.h:344
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:79
PNG memory record (file in memory).
Definition: bitmap_types.h:41
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:318
const wxPoint & GetTextPos() const
Definition: eda_text.h:222
bool IsItalic() const
Definition: eda_text.h:168
void SetEraseLastDrawItem(bool aErase=true)
void Move(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_text.cpp:134
Implementation of conversion functions that require both schematic and board internal units...
Define a symbol library graphical text item.
Definition: lib_text.h:44
bool HitTest(const wxPoint &aPosition) const override
Function HitTest tests if aPosition is contained within or on the bounding area of an item...
Definition: lib_text.cpp:55
void EndEdit(const wxPoint &aPosition, bool aAbort=false) override
End an object editing action.
Definition: lib_text.cpp:403
virtual COLOR4D GetDefaultColor()
bool ContinueEdit(const wxPoint aNextPoint) override
Continue an edit in progress at aPosition.
Definition: lib_text.cpp:394
void SetEffects(const EDA_TEXT &aSrc)
Function SetEffects sets the text effects from another instance.
Definition: eda_text.cpp:55
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_text.cpp:140
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:204
int color
Definition: DXF_plotter.cpp:62
int GetTextWidth() const
Definition: eda_text.h:216
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:221
bool IsSelected() const
Definition: base_struct.h:250
bool InEditMode() const
Return the draw item editing mode status.
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:124
int m_Unit
Unit identification for multiple parts per package.
int x1
Definition: transform.h:48
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:212
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
void Rotate() override
Rotate the draw item.
Definition: lib_text.cpp:330
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:128
#define TEXT_ANGLE_VERT
Definition: common.h:92
bool IsBold() const
Definition: eda_text.h:171
double GetTextAngle() const
Definition: eda_text.h:162
The base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:66
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:152
This file contains miscellaneous commonly used macros and functions.
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:224
virtual wxString GetShownText() const
Returns the string actually shown after processing of the base text.
Definition: eda_text.h:133
#define IS_NEW
New item, just created.
Definition: base_struct.h:127
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:112
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
int y1
Definition: transform.h:49
void GetMsgPanelInfo(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:293
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1098
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:102
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:254
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:37
void SetEnd(int x, int y)
Definition: eda_rect.h:134
void SetOffset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_text.cpp:122
Define a library symbol object.
unsigned STATUS_FLAGS
Definition: base_struct.h:160
EDA_RECT * GetClipBox()
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:152
wxPoint Centre() const
Definition: eda_rect.h:60
bool GetColorMode() const
Definition: plotter.h:130
const wxPoint GetEnd() const
Definition: eda_rect.h:114
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
int GetPenSize() const override
Definition: lib_text.cpp:206
wxString m_savedText
Temporary storage for the string when edition.
Definition: lib_text.h:46
void calcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_text.cpp:415
Base plotter engine class.
Definition: plotter.h:96
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
virtual void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: lib_text.cpp:368
TRANSFORM DefaultTransform
Definition: eeschema.cpp:58
void drawGraphic(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, void *aData, const TRANSFORM &aTransform) override
Draw the item on aPanel.
Definition: lib_text.cpp:224
const EDA_RECT GetBoundingBox() const override
Definition: lib_text.cpp:305
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:343
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:204
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:180
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
int m_Convert
Shape identification for alternate body styles.
int GetTextHeight() const
Definition: eda_text.h:219
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), PLOTTER *aPlotter)
Function DrawGraphicText Draw a graphic text (like module texts)
COLOR4D GetItemSelectedColor()
static const char * text_xpm[]
Definition: colors.h:45
const wxSize & GetTextSize() const
Definition: eda_text.h:213
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_text.cpp:80
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
Message panel definition file.
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
int compare(const LIB_ITEM &aOther) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_text.cpp:95
void SetTextY(int aY)
Definition: eda_text.h:225
wxPoint m_initialCursorPos
Initial cursor position at the beginning of a move.
wxString GetSelectMenuText() const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Definition: lib_text.cpp:360
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:126