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_unit = m_unit;
77  newitem->m_convert = m_convert;
78  newitem->m_flags = m_flags;
79 
80  newitem->SetText( GetText() );
81  newitem->SetEffects( *this );
82 
83  return newitem;
84 }
85 
86 
87 int LIB_TEXT::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
88 {
89  wxASSERT( aOther.Type() == LIB_TEXT_T );
90 
91  int retv = LIB_ITEM::compare( aOther, aCompareFlags );
92 
93  if( retv )
94  return retv;
95 
96  const LIB_TEXT* tmp = ( LIB_TEXT* ) &aOther;
97 
98  int result = GetText().CmpNoCase( tmp->GetText() );
99 
100  if( result != 0 )
101  return result;
102 
103  if( GetTextPos().x != tmp->GetTextPos().x )
104  return GetTextPos().x - tmp->GetTextPos().x;
105 
106  if( GetTextPos().y != tmp->GetTextPos().y )
107  return GetTextPos().y - tmp->GetTextPos().y;
108 
109  if( GetTextWidth() != tmp->GetTextWidth() )
110  return GetTextWidth() - tmp->GetTextWidth();
111 
112  if( GetTextHeight() != tmp->GetTextHeight() )
113  return GetTextHeight() - tmp->GetTextHeight();
114 
115  return 0;
116 }
117 
118 
119 void LIB_TEXT::Offset( const wxPoint& aOffset )
120 {
121  EDA_TEXT::Offset( aOffset );
122 }
123 
124 
125 void LIB_TEXT::MoveTo( const wxPoint& newPosition )
126 {
127  SetTextPos( newPosition );
128 }
129 
130 
132 {
133  wxPoint delta( 0, 0 );
134  EDA_RECT bbox = GetTextBox();
135 
136  if( GetTextAngle() == 0.0 )
137  {
139  delta.x = bbox.GetWidth() / 2;
141  delta.x = - bbox.GetWidth() / 2;
142 
144  delta.y = - bbox.GetHeight() / 2;
146  delta.y = bbox.GetHeight() / 2;
147  }
148  else
149  {
151  delta.y = bbox.GetWidth() / 2;
153  delta.y = - bbox.GetWidth() / 2;
154 
156  delta.x = + bbox.GetHeight() / 2;
158  delta.x = - bbox.GetHeight() / 2;
159  }
160 
161  if( inverse )
162  SetTextPos( GetTextPos() - delta );
163  else
164  SetTextPos( GetTextPos() + delta );
165 }
166 
167 
168 void LIB_TEXT::MirrorHorizontal( const wxPoint& center )
169 {
170  NormalizeJustification( false );
171  int x = GetTextPos().x;
172 
173  x -= center.x;
174  x *= -1;
175  x += center.x;
176 
177  if( GetTextAngle() == 0.0 )
178  {
183  }
184  else
185  {
190  }
191 
192  SetTextX( x );
193  NormalizeJustification( true );
194 }
195 
196 
197 void LIB_TEXT::MirrorVertical( const wxPoint& center )
198 {
199  NormalizeJustification( false );
200  int y = GetTextPos().y;
201 
202  y -= center.y;
203  y *= -1;
204  y += center.y;
205 
206  if( GetTextAngle() == 0.0 )
207  {
212  }
213  else
214  {
219  }
220 
221  SetTextY( y );
222  NormalizeJustification( true );
223 }
224 
225 
226 void LIB_TEXT::Rotate( const wxPoint& center, bool aRotateCCW )
227 {
228  NormalizeJustification( false );
229  int rot_angle = aRotateCCW ? -900 : 900;
230 
231  wxPoint pt = GetTextPos();
232  RotatePoint( &pt, center, rot_angle );
233  SetTextPos( pt );
234 
235  if( GetTextAngle() == 0.0 )
236  {
237  SetTextAngle( 900 );
238  }
239  else
240  {
241  // 180ยบ of rotation is a mirror
242 
247 
252 
253  SetTextAngle( 0 );
254  }
255 
256  NormalizeJustification( true );
257 }
258 
259 
260 void LIB_TEXT::Plot( PLOTTER* plotter, const wxPoint& offset, bool fill,
261  const TRANSFORM& aTransform ) const
262 {
263  wxASSERT( plotter != nullptr );
264 
265  EDA_RECT bBox = GetBoundingBox();
266  // convert coordinates from draw Y axis to symbol_editor Y axis
267  bBox.RevertYAxis();
268  wxPoint txtpos = bBox.Centre();
269 
270  // The text orientation may need to be flipped if the transformation matrix causes xy
271  // axes to be flipped.
272  int t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
273  wxPoint pos = aTransform.TransformCoordinate( txtpos ) + offset;
274 
275  // Get color
276  COLOR4D color;
277 
278  if( plotter->GetColorMode() ) // Used normal color or selected color
280  else
282 
283  RENDER_SETTINGS* settings = plotter->RenderSettings();
284 
285  int penWidth = std::max( GetEffectiveTextPenWidth(), settings->GetMinPenWidth() );
286 
287  plotter->Text( pos, color, GetText(), t1 ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT, GetTextSize(),
289  IsBold() );
290 }
291 
292 
294 {
295  return GetEffectiveTextPenWidth();
296 }
297 
298 
299 void LIB_TEXT::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
300  const TRANSFORM& aTransform )
301 {
302  wxDC* DC = aSettings->GetPrintDC();
303  COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
304  int penWidth = std::max( GetEffectiveTextPenWidth(), aSettings->GetDefaultPenWidth() );
305 
306  // Calculate the text orientation, according to the symbol orientation/mirror (needed when
307  // draw text in schematic)
308  int orient = (int) GetTextAngle();
309 
310  if( aTransform.y1 ) // Rotate symbol 90 degrees.
311  {
312  if( orient == TEXT_ANGLE_HORIZ )
313  orient = TEXT_ANGLE_VERT;
314  else
315  orient = TEXT_ANGLE_HORIZ;
316  }
317 
318  /*
319  * Calculate the text justification, according to the symbol orientation/mirror.
320  * This is a bit complicated due to cumulative calculations:
321  * - numerous cases (mirrored or not, rotation)
322  * - the GRText function will also recalculate H and V justifications according to the text
323  * orientation.
324  * - When a symbol is mirrored, the text is not mirrored and justifications are complicated
325  * to calculate so the more easily way is to use no justifications (centered text) and
326  * use GetBoundingBox to know the text coordinate considered as centered
327  */
328  EDA_RECT bBox = GetBoundingBox();
329 
330  // convert coordinates from draw Y axis to symbol_editor Y axis:
331  bBox.RevertYAxis();
332  wxPoint txtpos = bBox.Centre();
333 
334  // Calculate pos according to mirror/rotation.
335  txtpos = aTransform.TransformCoordinate( txtpos ) + aOffset;
336 
337  GRText( DC, txtpos, color, GetShownText(), orient, GetTextSize(), GR_TEXT_HJUSTIFY_CENTER,
338  GR_TEXT_VJUSTIFY_CENTER, penWidth, IsItalic(), IsBold() );
339 }
340 
341 
342 void LIB_TEXT::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
343 {
344  wxString msg;
345 
346  LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
347 
348  // Don't use GetShownText() here; we want to show the user the variable references
349  aList.emplace_back( _( "Text" ), UnescapeString( GetText() ) );
350 
351  aList.emplace_back( _( "Style" ), GetTextStyleName() );
352 
353  aList.emplace_back( _( "Text Size" ), MessageTextFromValue( aFrame->GetUserUnits(),
354  GetTextWidth() ) );
355 
356  switch ( GetHorizJustify() )
357  {
358  case GR_TEXT_HJUSTIFY_LEFT: msg = _( "Left" ); break;
359  case GR_TEXT_HJUSTIFY_CENTER: msg = _( "Center" ); break;
360  case GR_TEXT_HJUSTIFY_RIGHT: msg = _( "Right" ); break;
361  }
362 
363  aList.emplace_back( _( "H Justification" ), msg );
364 
365  switch ( GetVertJustify() )
366  {
367  case GR_TEXT_VJUSTIFY_TOP: msg = _( "Top" ); break;
368  case GR_TEXT_VJUSTIFY_CENTER: msg = _( "Center" ); break;
369  case GR_TEXT_VJUSTIFY_BOTTOM: msg = _( "Bottom" ); break;
370  }
371 
372  aList.emplace_back( _( "V Justification" ), msg );
373 }
374 
375 
377 {
378  /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
379  * calling GetTextBox() that works using top to bottom Y axis orientation.
380  */
381  EDA_RECT rect = GetTextBox( -1, true );
382  rect.RevertYAxis();
383 
384  // We are using now a bottom to top Y axis.
385  wxPoint orig = rect.GetOrigin();
386  wxPoint end = rect.GetEnd();
387 
388  RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
389  RotatePoint( &end, GetTextPos(), -GetTextAngle() );
390 
391  rect.SetOrigin( orig );
392  rect.SetEnd( end );
393 
394  // We are using now a top to bottom Y axis:
395  rect.RevertYAxis();
396 
397  return rect;
398 }
399 
400 
401 wxString LIB_TEXT::GetSelectMenuText( EDA_UNITS aUnits ) const
402 {
403  return wxString::Format( _( "Graphic Text '%s'" ), ShortenedShownText() );
404 }
405 
406 
408 {
409  return BITMAPS::text;
410 }
411 
412 
413 void LIB_TEXT::BeginEdit( const wxPoint& aPosition )
414 {
415  SetTextPos( aPosition );
416 }
417 
418 
419 void LIB_TEXT::CalcEdit( const wxPoint& aPosition )
420 {
421  SetTextPos( aPosition );
422 }
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_text.cpp:125
#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:50
bool IsBold() const
Definition: eda_text.h:183
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:252
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:199
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:342
void NormalizeJustification(bool inverse)
Definition: lib_text.cpp:131
void CalcEdit(const wxPoint &aPosition) override
Calculate the attributes of an item at aPosition when it is being edited.
Definition: lib_text.cpp:419
void SetEffects(const EDA_TEXT &aSrc)
Set the text effects from another instance.
Definition: eda_text.cpp:129
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_text.cpp:168
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:229
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:246
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:87
int GetWidth() const
Definition: eda_rect.h:109
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:260
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
double GetTextAngle() const
Definition: eda_text.h:174
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:121
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:237
Definition: color4d.h:44
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:481
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:198
void print(const RENDER_SETTINGS *aSettings, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_text.cpp:299
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:149
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 contained within or on the bounding box of an item.
Definition: lib_text.cpp:56
int GetTextHeight() const
Definition: eda_text.h:244
void SetTextX(int aX)
Definition: eda_text.h:249
bool IsItalic() const
Definition: eda_text.h:180
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:119
int y1
Definition: transform.h:49
const wxPoint GetEnd() const
Definition: eda_rect.h:103
wxString GetTextStyleName() const
Definition: eda_text.cpp:491
int m_unit
Unit identification for multiple parts per package.
Definition: lib_item.h:310
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:198
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:114
void BeginEdit(const wxPoint &aStartPoint) override
Begin drawing a symbol library draw item at aPosition.
Definition: lib_text.cpp:413
COMPARE_FLAGS
The list of flags used by the compare function.
Definition: lib_item.h:80
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
Definition: lib_text.cpp:226
const wxPoint GetOrigin() const
Definition: eda_rect.h:101
void SetEnd(int x, int y)
Definition: eda_rect.h:182
const wxSize & GetTextSize() const
Definition: eda_text.h:238
int m_convert
Shape identification for alternate body styles.
Definition: lib_item.h:316
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:202
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:208
#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:197
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:401
int GetHeight() const
Definition: eda_rect.h:110
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:119
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:156
int GetTextWidth() const
Definition: eda_text.h:241
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:376
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:201
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:382
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
LIB_TEXT(LIB_SYMBOL *aParent)
Definition: lib_text.cpp:40
int GetPenWidth() const override
Definition: lib_text.cpp:293
The common library.
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: lib_text.cpp:407
wxPoint Centre() const
Definition: eda_rect.h:55
int GetDefaultPenWidth() const
const wxPoint & GetTextPos() const
Definition: eda_text.h:247
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:51
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:167
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 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:67
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:141
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetTextY(int aY)
Definition: eda_text.h:250
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103
bool GetColorMode() const
Definition: plotter.h:153