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-2020 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 <gr_text.h>
34 #include <trigo.h>
35 #include <base_units.h>
36 #include <msgpanel.h>
37 #include <bitmaps.h>
38 
39 #include <lib_item.h>
40 #include <general.h>
41 #include <transform.h>
43 #include <lib_text.h>
44 
45 
47  LIB_ITEM( LIB_TEXT_T, aParent ),
48  EDA_TEXT()
49 {
50  SetTextSize( wxSize( Mils2iu( 50 ), Mils2iu( 50 ) ) );
51 }
52 
53 
54 void LIB_TEXT::ViewGetLayers( int aLayers[], int& aCount ) const
55 {
56  aCount = 2;
57  aLayers[0] = LAYER_DEVICE;
58  aLayers[1] = LAYER_SELECTION_SHADOWS;
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 
86  newitem->SetText( GetText() );
87  newitem->SetEffects( *this );
88 
89  return newitem;
90 }
91 
92 
93 int LIB_TEXT::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
94 {
95  wxASSERT( aOther.Type() == LIB_TEXT_T );
96 
97  int retv = LIB_ITEM::compare( aOther, aCompareFlags );
98 
99  if( retv )
100  return retv;
101 
102  const LIB_TEXT* tmp = ( LIB_TEXT* ) &aOther;
103 
104  int result = GetText().CmpNoCase( tmp->GetText() );
105 
106  if( result != 0 )
107  return result;
108 
109  if( GetTextPos().x != tmp->GetTextPos().x )
110  return GetTextPos().x - tmp->GetTextPos().x;
111 
112  if( GetTextPos().y != tmp->GetTextPos().y )
113  return GetTextPos().y - tmp->GetTextPos().y;
114 
115  if( GetTextWidth() != tmp->GetTextWidth() )
116  return GetTextWidth() - tmp->GetTextWidth();
117 
118  if( GetTextHeight() != tmp->GetTextHeight() )
119  return GetTextHeight() - tmp->GetTextHeight();
120 
121  return 0;
122 }
123 
124 
125 void LIB_TEXT::Offset( const wxPoint& aOffset )
126 {
127  EDA_TEXT::Offset( aOffset );
128 }
129 
130 
131 bool LIB_TEXT::Inside( EDA_RECT& rect ) const
132 {
133  return rect.Intersects( GetBoundingBox() );
134 }
135 
136 
137 void LIB_TEXT::MoveTo( const wxPoint& newPosition )
138 {
139  SetTextPos( newPosition );
140 }
141 
142 
144 {
145  wxPoint delta( 0, 0 );
146  EDA_RECT bbox = GetTextBox( -1 );
147 
148  if( GetTextAngle() == 0.0 )
149  {
151  delta.x = bbox.GetWidth() / 2;
153  delta.x = - bbox.GetWidth() / 2;
154 
156  delta.y = - bbox.GetHeight() / 2;
158  delta.y = bbox.GetHeight() / 2;
159  }
160  else
161  {
163  delta.y = bbox.GetWidth() / 2;
165  delta.y = - bbox.GetWidth() / 2;
166 
168  delta.x = + bbox.GetHeight() / 2;
170  delta.x = - bbox.GetHeight() / 2;
171  }
172 
173  if( inverse )
174  SetTextPos( GetTextPos() - delta );
175  else
176  SetTextPos( GetTextPos() + delta );
177 }
178 
179 
180 void LIB_TEXT::MirrorHorizontal( const wxPoint& center )
181 {
182  NormalizeJustification( false );
183  int x = GetTextPos().x;
184 
185  x -= center.x;
186  x *= -1;
187  x += center.x;
188 
189  if( GetTextAngle() == 0.0 )
190  {
195  }
196  else
197  {
202  }
203 
204  SetTextX( x );
205  NormalizeJustification( true );
206 }
207 
208 
209 void LIB_TEXT::MirrorVertical( const wxPoint& center )
210 {
211  NormalizeJustification( false );
212  int y = GetTextPos().y;
213 
214  y -= center.y;
215  y *= -1;
216  y += center.y;
217 
218  if( GetTextAngle() == 0.0 )
219  {
224  }
225  else
226  {
231  }
232 
233  SetTextY( y );
234  NormalizeJustification( true );
235 }
236 
237 
238 void LIB_TEXT::Rotate( const wxPoint& center, bool aRotateCCW )
239 {
240  NormalizeJustification( false );
241  int rot_angle = aRotateCCW ? -900 : 900;
242 
243  wxPoint pt = GetTextPos();
244  RotatePoint( &pt, center, rot_angle );
245  SetTextPos( pt );
246 
247  if( GetTextAngle() == 0.0 )
248  {
249  SetTextAngle( 900 );
250  }
251  else
252  {
253  // 180ยบ of rotation is a mirror
254 
259 
264 
265  SetTextAngle( 0 );
266  }
267 
268  NormalizeJustification( true );
269 }
270 
271 
272 void LIB_TEXT::Plot( PLOTTER* plotter, const wxPoint& offset, bool fill,
273  const TRANSFORM& aTransform )
274 {
275  wxASSERT( plotter != NULL );
276 
277  EDA_RECT bBox = GetBoundingBox();
278  // convert coordinates from draw Y axis to libedit Y axis
279  bBox.RevertYAxis();
280  wxPoint txtpos = bBox.Centre();
281 
282  /* The text orientation may need to be flipped if the
283  * transformation matrix causes xy axes to be flipped. */
284  int t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
285  wxPoint pos = aTransform.TransformCoordinate( txtpos ) + offset;
286 
287  // Get color
288  COLOR4D color;
289 
290  if( plotter->GetColorMode() ) // Used normal color or selected color
291  color = plotter->ColorSettings()->GetColor( LAYER_DEVICE );
292  else
294 
295  plotter->Text( pos, color, GetText(), t1 ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT,
297  GetPenSize(), IsItalic(), IsBold() );
298 }
299 
300 
302 {
303  int pensize = GetThickness();
304 
305  if( pensize == 0 ) // Use default values for pen size
306  {
307  if( IsBold() )
308  pensize = GetPenSizeForBold( GetTextWidth() );
309  else
310  pensize = GetDefaultLineThickness();
311  }
312 
313  // Clip pen size for small texts:
314  pensize = Clamp_Text_PenSize( pensize, GetTextSize(), IsBold() );
315  return pensize;
316 }
317 
318 
319 void LIB_TEXT::print( wxDC* aDC, const wxPoint& aOffset, void* aData, const TRANSFORM& aTransform )
320 {
322 
323  /* Calculate the text orientation, according to the component
324  * orientation/mirror (needed when draw text in schematic)
325  */
326  int orient = GetTextAngle();
327 
328  if( aTransform.y1 ) // Rotate component 90 degrees.
329  {
330  if( orient == TEXT_ANGLE_HORIZ )
331  orient = TEXT_ANGLE_VERT;
332  else
333  orient = TEXT_ANGLE_HORIZ;
334  }
335 
336  /* Calculate the text justification, according to the component
337  * orientation/mirror this is a bit complicated due to cumulative
338  * calculations:
339  * - numerous cases (mirrored or not, rotation)
340  * - the DrawGraphicText function recalculate also H and H justifications
341  * according to the text orientation.
342  * - When a component is mirrored, the text is not mirrored and
343  * justifications are complicated to calculate
344  * so the more easily way is to use no justifications ( Centered text )
345  * and use GetBoundaryBox to know the text coordinate considered as centered
346  */
347  EDA_RECT bBox = GetBoundingBox();
348 
349  // convert coordinates from draw Y axis to libedit Y axis:
350  bBox.RevertYAxis();
351  wxPoint txtpos = bBox.Centre();
352 
353  // Calculate pos according to mirror/rotation.
354  txtpos = aTransform.TransformCoordinate( txtpos ) + aOffset;
355 
356  GRText( aDC, txtpos, color, GetShownText(), orient, GetTextSize(), GR_TEXT_HJUSTIFY_CENTER,
358 }
359 
360 
362 {
363  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
364 
365  wxString msg = MessageTextFromValue( aUnits, GetThickness(), true );
366  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
367 }
368 
369 
371 {
372  /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
373  * calling GetTextBox() that works using top to bottom Y axis orientation.
374  */
375  EDA_RECT rect = GetTextBox( -1, -1, true, GetTextMarkupFlags() );
376  rect.RevertYAxis();
377 
378  // We are using now a bottom to top Y axis.
379  wxPoint orig = rect.GetOrigin();
380  wxPoint end = rect.GetEnd();
381 
382  RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
383  RotatePoint( &end, GetTextPos(), -GetTextAngle() );
384 
385  rect.SetOrigin( orig );
386  rect.SetEnd( end );
387 
388  // We are using now a top to bottom Y axis:
389  rect.RevertYAxis();
390 
391  return rect;
392 }
393 
394 
395 wxString LIB_TEXT::GetSelectMenuText( EDA_UNITS aUnits ) const
396 {
397  return wxString::Format( _( "Graphic Text \"%s\"" ), ShortenedShownText() );
398 }
399 
400 
402 {
403  return text_xpm;
404 }
405 
406 
407 void LIB_TEXT::BeginEdit( const wxPoint aPosition )
408 {
409  SetTextPos( aPosition );
410 }
411 
412 
413 void LIB_TEXT::CalcEdit( const wxPoint& aPosition )
414 {
415  SetTextPos( aPosition );
416 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:150
Definition: colors.h:57
#define TEXT_ANGLE_HORIZ
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_text.cpp:137
EDA_UNITS
Definition: common.h:184
bool IsBold() const
Definition: eda_text.h:167
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:237
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:272
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:184
PNG memory record (file in memory).
Definition: bitmap_def.h:29
void GetMsgPanelInfo(EDA_UNITS aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
Definition: lib_item.cpp:52
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
Definition: gr_text.cpp:66
#define TEXT_ANGLE_VERT
Implementation of conversion functions that require both schematic and board internal units.
Define a symbol library graphical text item.
Definition: lib_text.h:40
void NormalizeJustification(bool inverse)
Definition: lib_text.cpp:143
virtual COLOR4D GetDefaultColor()
Definition: lib_item.cpp:152
void CalcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_text.cpp:413
void SetEffects(const EDA_TEXT &aSrc)
Set the text effects from another instance.
Definition: eda_text.cpp:125
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_text.cpp:180
int color
Definition: DXF_plotter.cpp:61
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i....
void GetMsgPanelInfo(EDA_UNITS 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:361
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:231
int compare(const LIB_ITEM &aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags=LIB_ITEM::COMPARE_FLAGS::NORMAL) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_text.cpp:93
int GetWidth() const
Definition: eda_rect.h:119
const BITMAP_OPAQUE text_xpm[1]
Definition: text.cpp:27
double GetTextAngle() const
Definition: eda_text.h:158
TRANSFORM DefaultTransform
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
int m_Unit
Unit identification for multiple parts per package.
Definition: lib_item.h:81
int GetThickness() const
Return the pen width.
Definition: eda_text.h:148
int x1
Definition: transform.h:48
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:222
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size)
Definition: eda_rect.h:209
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:131
void BeginEdit(const wxPoint aStartPoint) override
Begin drawing a component library draw item at aPosition.
Definition: lib_text.cpp:407
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:42
The base class for drawable items used by schematic library components.
Definition: lib_item.h:60
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:54
This file contains miscellaneous commonly used macros and functions.
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:229
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.
Definition: gr_text.cpp:232
void SetTextX(int aX)
Definition: eda_text.h:234
bool IsItalic() const
Definition: eda_text.h:164
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:109
int y1
Definition: transform.h:49
const wxPoint GetEnd() const
Definition: eda_rect.h:116
int GetTextMarkupFlags()
Definition: gr_text.cpp:55
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aBold)
Function Clamp_Text_PenSize Don't allow text to become cluttered up in its own fatness.
Definition: gr_text.cpp:81
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:183
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:111
#define NULL
COMPARE_FLAGS
The list of flags used by the compare function.
Definition: lib_item.h:116
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
Definition: lib_text.cpp:238
COLOR_SETTINGS * ColorSettings()
Definition: plotter.h:145
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
EDA_RECT GetTextBox(int aLine=-1, int aThickness=-1, bool aInvertY=false, int aMarkupFlags=0) const
Useful in multiline texts to calculate the full text or a line area (for zones filling,...
Definition: eda_text.cpp:184
void SetEnd(int x, int y)
Definition: eda_rect.h:192
const wxSize & GetTextSize() const
Definition: eda_text.h:223
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:187
Define a library symbol object.
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:163
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Definition: lib_text.cpp:209
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Definition: lib_text.cpp:395
int GetHeight() const
Definition: eda_rect.h:120
void GRText(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 GRText Draw a graphic text (like module texts)
Definition: gr_text.cpp:143
int GetPenSize() const override
Definition: lib_text.cpp:301
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:127
Base plotter engine class.
Definition: plotter.h:104
COLOR4D GetColor(int aLayer) const
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_text.cpp:125
int GetTextWidth() const
Definition: eda_text.h:226
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: lib_text.cpp:401
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:370
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:186
#define _(s)
Definition: 3d_actions.cpp:33
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:189
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
virtual bool TextHitTest(const wxPoint &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:336
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
wxPoint Centre() const
Definition: eda_rect.h:62
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_item.h:87
const wxPoint & GetTextPos() const
Definition: eda_text.h:232
virtual wxString GetShownText() const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:129
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
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
Message panel definition file.
virtual int compare(const LIB_ITEM &aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags=LIB_ITEM::COMPARE_FLAGS::NORMAL) const
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_item.cpp:76
void print(wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_text.cpp:319
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:123
LIB_TEXT(LIB_PART *aParent)
Definition: lib_text.cpp:46
void SetTextY(int aY)
Definition: eda_text.h:235
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40
bool GetColorMode() const
Definition: plotter.h:138