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-2021 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 
24 #include <common.h>
25 #include <sch_draw_panel.h>
26 #include <plotters/plotter.h>
27 #include <trigo.h>
28 #include <base_units.h>
29 #include <widgets/msgpanel.h>
30 #include <bitmaps.h>
31 #include <eda_draw_frame.h>
32 #include <lib_item.h>
33 #include <general.h>
34 #include <transform.h>
36 #include <lib_text.h>
37 #include <default_values.h> // For some default values
38 #include <string_utils.h>
39 
41  LIB_ITEM( LIB_TEXT_T, aParent ),
42  EDA_TEXT( wxEmptyString )
43 {
44  SetTextSize( wxSize( Mils2iu( DEFAULT_TEXT_SIZE ), Mils2iu( DEFAULT_TEXT_SIZE ) ) );
45 }
46 
47 
48 void LIB_TEXT::ViewGetLayers( int aLayers[], int& aCount ) const
49 {
50  aCount = 2;
51  aLayers[0] = LAYER_DEVICE;
52  aLayers[1] = LAYER_SELECTION_SHADOWS;
53 }
54 
55 
56 bool LIB_TEXT::HitTest( const wxPoint& aPosition, int aAccuracy ) const
57 {
58  EDA_TEXT tmp_text( *this );
60 
61  /* The text orientation may need to be flipped if the
62  * transformation matrix causes xy axes to be flipped.
63  * this simple algo works only for schematic matrix (rot 90 or/and mirror)
64  */
65  bool t1 = ( DefaultTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
66 
68  return tmp_text.TextHitTest( aPosition, aAccuracy );
69 }
70 
71 
73 {
74  LIB_TEXT* newitem = new LIB_TEXT( nullptr );
75 
76  newitem->m_parent = m_parent;
77  newitem->m_unit = m_unit;
78  newitem->m_convert = m_convert;
79  newitem->m_flags = m_flags;
80 
81  newitem->SetText( GetText() );
82  newitem->SetEffects( *this );
83 
84  return newitem;
85 }
86 
87 
88 int LIB_TEXT::compare( const LIB_ITEM& aOther, int aCompareFlags ) const
89 {
90  wxASSERT( aOther.Type() == LIB_TEXT_T );
91 
92  int retv = LIB_ITEM::compare( aOther, aCompareFlags );
93 
94  if( retv )
95  return retv;
96 
97  const LIB_TEXT* tmp = ( LIB_TEXT* ) &aOther;
98 
99  int result = GetText().CmpNoCase( tmp->GetText() );
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 void LIB_TEXT::MoveTo( const wxPoint& newPosition )
127 {
128  SetTextPos( newPosition );
129 }
130 
131 
133 {
134  wxPoint delta( 0, 0 );
135  EDA_RECT bbox = GetTextBox();
136 
137  if( GetTextAngle() == 0.0 )
138  {
140  delta.x = bbox.GetWidth() / 2;
142  delta.x = - bbox.GetWidth() / 2;
143 
145  delta.y = - bbox.GetHeight() / 2;
147  delta.y = bbox.GetHeight() / 2;
148  }
149  else
150  {
152  delta.y = bbox.GetWidth() / 2;
154  delta.y = - bbox.GetWidth() / 2;
155 
157  delta.x = + bbox.GetHeight() / 2;
159  delta.x = - bbox.GetHeight() / 2;
160  }
161 
162  if( inverse )
163  SetTextPos( GetTextPos() - delta );
164  else
165  SetTextPos( GetTextPos() + delta );
166 }
167 
168 
169 void LIB_TEXT::MirrorHorizontal( const wxPoint& center )
170 {
171  NormalizeJustification( false );
172  int x = GetTextPos().x;
173 
174  x -= center.x;
175  x *= -1;
176  x += center.x;
177 
178  if( GetTextAngle() == 0.0 )
179  {
184  }
185  else
186  {
191  }
192 
193  SetTextX( x );
194  NormalizeJustification( true );
195 }
196 
197 
198 void LIB_TEXT::MirrorVertical( const wxPoint& center )
199 {
200  NormalizeJustification( false );
201  int y = GetTextPos().y;
202 
203  y -= center.y;
204  y *= -1;
205  y += center.y;
206 
207  if( GetTextAngle() == 0.0 )
208  {
213  }
214  else
215  {
220  }
221 
222  SetTextY( y );
223  NormalizeJustification( true );
224 }
225 
226 
227 void LIB_TEXT::Rotate( const wxPoint& center, bool aRotateCCW )
228 {
229  NormalizeJustification( false );
230  int rot_angle = aRotateCCW ? -900 : 900;
231 
232  wxPoint pt = GetTextPos();
233  RotatePoint( &pt, center, rot_angle );
234  SetTextPos( pt );
235 
236  if( GetTextAngle() == 0.0 )
237  {
238  SetTextAngle( 900 );
239  }
240  else
241  {
242  // 180ยบ of rotation is a mirror
243 
248 
253 
254  SetTextAngle( 0 );
255  }
256 
257  NormalizeJustification( true );
258 }
259 
260 
261 void LIB_TEXT::Plot( PLOTTER* plotter, const wxPoint& offset, bool fill,
262  const TRANSFORM& aTransform ) const
263 {
264  wxASSERT( plotter != nullptr );
265 
266  EDA_RECT bBox = GetBoundingBox();
267  // convert coordinates from draw Y axis to symbol_editor Y axis
268  bBox.RevertYAxis();
269  wxPoint txtpos = bBox.Centre();
270 
271  // The text orientation may need to be flipped if the transformation matrix causes xy
272  // axes to be flipped.
273  int t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
274  wxPoint pos = aTransform.TransformCoordinate( txtpos ) + offset;
275 
276  // Get color
277  COLOR4D color;
278 
279  if( plotter->GetColorMode() ) // Used normal color or selected color
281  else
283 
284  RENDER_SETTINGS* settings = plotter->RenderSettings();
285 
286  int penWidth = std::max( GetEffectiveTextPenWidth(), settings->GetMinPenWidth() );
287 
288  plotter->Text( pos, color, GetText(), t1 ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT, GetTextSize(),
290  IsBold() );
291 }
292 
293 
295 {
296  return GetEffectiveTextPenWidth();
297 }
298 
299 
300 void LIB_TEXT::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
301  const TRANSFORM& aTransform )
302 {
303  wxDC* DC = aSettings->GetPrintDC();
304  COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
305  int penWidth = std::max( GetEffectiveTextPenWidth(), aSettings->GetDefaultPenWidth() );
306 
307  // Calculate the text orientation, according to the symbol orientation/mirror (needed when
308  // draw text in schematic)
309  int orient = (int) GetTextAngle();
310 
311  if( aTransform.y1 ) // Rotate symbol 90 degrees.
312  {
313  if( orient == TEXT_ANGLE_HORIZ )
314  orient = TEXT_ANGLE_VERT;
315  else
316  orient = TEXT_ANGLE_HORIZ;
317  }
318 
319  /*
320  * Calculate the text justification, according to the symbol orientation/mirror.
321  * This is a bit complicated due to cumulative calculations:
322  * - numerous cases (mirrored or not, rotation)
323  * - the GRText function will also recalculate H and V justifications according to the text
324  * orientation.
325  * - When a symbol is mirrored, the text is not mirrored and justifications are complicated
326  * to calculate so the more easily way is to use no justifications (centered text) and
327  * use GetBoundingBox to know the text coordinate considered as centered
328  */
329  EDA_RECT bBox = GetBoundingBox();
330 
331  // convert coordinates from draw Y axis to symbol_editor Y axis:
332  bBox.RevertYAxis();
333  wxPoint txtpos = bBox.Centre();
334 
335  // Calculate pos according to mirror/rotation.
336  txtpos = aTransform.TransformCoordinate( txtpos ) + aOffset;
337 
338  GRText( DC, txtpos, color, GetShownText(), orient, GetTextSize(), GR_TEXT_HJUSTIFY_CENTER,
339  GR_TEXT_VJUSTIFY_CENTER, penWidth, IsItalic(), IsBold() );
340 }
341 
342 
343 void LIB_TEXT::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
344 {
345  wxString msg;
346 
347  LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
348 
349  // Don't use GetShownText() here; we want to show the user the variable references
350  aList.emplace_back( _( "Text" ), UnescapeString( GetText() ) );
351 
352  aList.emplace_back( _( "Style" ), GetTextStyleName() );
353 
354  aList.emplace_back( _( "Text Size" ), MessageTextFromValue( aFrame->GetUserUnits(),
355  GetTextWidth() ) );
356 
357  switch ( GetHorizJustify() )
358  {
359  case GR_TEXT_HJUSTIFY_LEFT: msg = _( "Left" ); break;
360  case GR_TEXT_HJUSTIFY_CENTER: msg = _( "Center" ); break;
361  case GR_TEXT_HJUSTIFY_RIGHT: msg = _( "Right" ); break;
362  }
363 
364  aList.emplace_back( _( "H Justification" ), msg );
365 
366  switch ( GetVertJustify() )
367  {
368  case GR_TEXT_VJUSTIFY_TOP: msg = _( "Top" ); break;
369  case GR_TEXT_VJUSTIFY_CENTER: msg = _( "Center" ); break;
370  case GR_TEXT_VJUSTIFY_BOTTOM: msg = _( "Bottom" ); break;
371  }
372 
373  aList.emplace_back( _( "V Justification" ), msg );
374 }
375 
376 
378 {
379  /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
380  * calling GetTextBox() that works using top to bottom Y axis orientation.
381  */
382  EDA_RECT rect = GetTextBox( -1, true );
383  rect.RevertYAxis();
384 
385  // We are using now a bottom to top Y axis.
386  wxPoint orig = rect.GetOrigin();
387  wxPoint end = rect.GetEnd();
388 
389  RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
390  RotatePoint( &end, GetTextPos(), -GetTextAngle() );
391 
392  rect.SetOrigin( orig );
393  rect.SetEnd( end );
394 
395  // We are using now a top to bottom Y axis:
396  rect.RevertYAxis();
397 
398  return rect;
399 }
400 
401 
402 wxString LIB_TEXT::GetSelectMenuText( EDA_UNITS aUnits ) const
403 {
404  return wxString::Format( _( "Graphic Text '%s'" ), ShortenedShownText() );
405 }
406 
407 
409 {
410  return BITMAPS::text;
411 }
412 
413 
414 void LIB_TEXT::BeginEdit( const wxPoint& aPosition )
415 {
416  SetTextPos( aPosition );
417 }
418 
419 
420 void LIB_TEXT::CalcEdit( const wxPoint& aPosition )
421 {
422  SetTextPos( aPosition );
423 }
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_text.cpp:126
#define TEXT_ANGLE_HORIZ
Frequent text rotations, used with {Set,Get}TextAngle(), in 0.1 degrees for now, hoping to migrate to...
Definition: eda_text.h:71
bool IsBold() const
Definition: eda_text.h:204
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:273
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:478
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:104
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:220
int compare(const LIB_ITEM &aOther, int aCompareFlags=0) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_text.cpp:88
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
Implementation of conversion functions that require both schematic and board internal units.
Define a symbol library graphical text item.
Definition: lib_text.h:39
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, 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:343
void NormalizeJustification(bool inverse)
Definition: lib_text.cpp:132
void CalcEdit(const wxPoint &aPosition) override
Calculate the attributes of an item at aPosition when it is being edited.
Definition: lib_text.cpp:420
void SetEffects(const EDA_TEXT &aSrc)
Set the text effects from another instance.
Definition: eda_text.cpp:139
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_text.cpp:169
int color
Definition: DXF_plotter.cpp:57
EDA_RECT GetTextBox(int aLine=-1, bool aInvertY=false) const
Useful in multiline texts to calculate the full text or a line area (for zones filling,...
Definition: eda_text.cpp:239
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:267
int GetWidth() const
Definition: eda_rect.h:118
void Plot(PLOTTER *aPlotter, const wxPoint &aOffset, bool aFill, const TRANSFORM &aTransform) const override
Plot the draw item using the plot object.
Definition: lib_text.cpp:261
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
double GetTextAngle() const
Definition: eda_text.h:195
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:130
int x1
Definition: transform.h:48
void GRText(wxDC *aDC, 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, void(*aCallback)(int x0, int y0, int xf, int yf, void *aData), void *aCallbackData, PLOTTER *aPlotter)
Draw a graphic text (like footprint texts).
Definition: gr_text.cpp:129
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:258
Definition: color4d.h:44
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:480
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
The base class for create windows for drawing purpose.
Define a library symbol object.
Definition: lib_symbol.h:96
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
Definition: eda_rect.h:207
void print(const RENDER_SETTINGS *aSettings, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_text.cpp:300
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 symbols.
Definition: lib_item.h:61
int GetEffectiveTextPenWidth(int aDefaultWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultWidth.
Definition: eda_text.cpp:159
void ViewGetLayers(int aLayers[], int &aCount) const override
Return the all the layers within the VIEW the object is painted on.
Definition: lib_text.cpp:48
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition: lib_text.cpp:56
int GetTextHeight() const
Definition: eda_text.h:265
void SetTextX(int aX)
Definition: eda_text.h:270
bool IsItalic() const
Definition: eda_text.h:201
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:140
int y1
Definition: transform.h:49
const wxPoint GetEnd() const
Definition: eda_rect.h:112
wxString GetTextStyleName() const
Definition: eda_text.cpp:504
virtual int compare(const LIB_ITEM &aOther, int aCompareFlags=0) const
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_item.cpp:67
int m_unit
Unit identification for multiple parts per package.
Definition: lib_item.h:317
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:219
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:124
void BeginEdit(const wxPoint &aStartPoint) override
Begin drawing a symbol library draw item at aPosition.
Definition: lib_text.cpp:414
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
Definition: lib_text.cpp:227
const wxPoint GetOrigin() const
Definition: eda_rect.h:110
void SetEnd(int x, int y)
Definition: eda_rect.h:191
const wxSize & GetTextSize() const
Definition: eda_text.h:259
int m_convert
Shape identification for alternate body styles.
Definition: lib_item.h:323
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:223
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:218
#define _(s)
wxDC * GetPrintDC() const
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Definition: lib_text.cpp:198
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: lib_text.cpp:402
int GetHeight() const
Definition: eda_rect.h:119
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, 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:43
wxString UnescapeString(const wxString &aSource)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
EDA_UNITS
Definition: eda_units.h:38
Base plotter engine class.
Definition: plotter.h:121
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_text.cpp:120
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:156
int GetTextWidth() const
Definition: eda_text.h:262
TRANSFORM DefaultTransform
Definition: eeschema.cpp:56
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
const EDA_RECT GetBoundingBox() const override
Definition: lib_text.cpp:377
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:222
Handle the component boundary box.
Definition: eda_rect.h:42
virtual bool TextHitTest(const wxPoint &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:395
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
LIB_TEXT(LIB_SYMBOL *aParent)
Definition: lib_text.cpp:40
int GetPenWidth() const override
Definition: lib_text.cpp:294
The common library.
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: lib_text.cpp:408
wxPoint Centre() const
Definition: eda_rect.h:64
int GetDefaultPenWidth() const
const wxPoint & GetTextPos() const
Definition: eda_text.h:268
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=nullptr)
Draw text with the plotter.
Definition: gr_text.cpp:219
constexpr int delta
#define DEFAULT_TEXT_SIZE
Ratio of the font height to the baseline of the text above the wire.
#define TEXT_ANGLE_VERT
Definition: eda_text.h:72
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:188
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: lib_text.cpp:72
Message panel definition file.
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:154
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:162
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetTextY(int aY)
Definition: eda_text.h:271
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103
bool GetColorMode() const
Definition: plotter.h:153