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 <common.h>
29 #include <sch_draw_panel.h>
30 #include <plotter.h>
31 #include <gr_text.h>
32 #include <trigo.h>
33 #include <base_units.h>
34 #include <widgets/msgpanel.h>
35 #include <bitmaps.h>
36 #include <eda_draw_frame.h>
37 #include <lib_item.h>
38 #include <general.h>
39 #include <transform.h>
41 #include <lib_text.h>
42 #include <default_values.h> // For some default values
43 
44 
46  LIB_ITEM( LIB_TEXT_T, aParent ),
47  EDA_TEXT( wxEmptyString )
48 {
49  SetTextSize( wxSize( Mils2iu( DEFAULT_TEXT_SIZE ), Mils2iu( DEFAULT_TEXT_SIZE ) ) );
50 }
51 
52 
53 void LIB_TEXT::ViewGetLayers( int aLayers[], int& aCount ) const
54 {
55  aCount = 2;
56  aLayers[0] = LAYER_DEVICE;
57  aLayers[1] = LAYER_SELECTION_SHADOWS;
58 }
59 
60 
61 bool LIB_TEXT::HitTest( const wxPoint& aPosition, int aAccuracy ) const
62 {
63  EDA_TEXT tmp_text( *this );
65 
66  /* The text orientation may need to be flipped if the
67  * transformation matrix causes xy axes to be flipped.
68  * this simple algo works only for schematic matrix (rot 90 or/and mirror)
69  */
70  bool t1 = ( DefaultTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
71 
73  return tmp_text.TextHitTest( aPosition, aAccuracy );
74 }
75 
76 
78 {
79  LIB_TEXT* newitem = new LIB_TEXT( nullptr );
80 
81  newitem->m_unit = m_unit;
82  newitem->m_convert = m_convert;
83  newitem->m_flags = m_flags;
84 
85  newitem->SetText( GetText() );
86  newitem->SetEffects( *this );
87 
88  return newitem;
89 }
90 
91 
92 int LIB_TEXT::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
93 {
94  wxASSERT( aOther.Type() == LIB_TEXT_T );
95 
96  int retv = LIB_ITEM::compare( aOther, aCompareFlags );
97 
98  if( retv )
99  return retv;
100 
101  const LIB_TEXT* tmp = ( LIB_TEXT* ) &aOther;
102 
103  int result = GetText().CmpNoCase( tmp->GetText() );
104 
105  if( result != 0 )
106  return result;
107 
108  if( GetTextPos().x != tmp->GetTextPos().x )
109  return GetTextPos().x - tmp->GetTextPos().x;
110 
111  if( GetTextPos().y != tmp->GetTextPos().y )
112  return GetTextPos().y - tmp->GetTextPos().y;
113 
114  if( GetTextWidth() != tmp->GetTextWidth() )
115  return GetTextWidth() - tmp->GetTextWidth();
116 
117  if( GetTextHeight() != tmp->GetTextHeight() )
118  return GetTextHeight() - tmp->GetTextHeight();
119 
120  return 0;
121 }
122 
123 
124 void LIB_TEXT::Offset( const wxPoint& aOffset )
125 {
126  EDA_TEXT::Offset( aOffset );
127 }
128 
129 
130 void LIB_TEXT::MoveTo( const wxPoint& newPosition )
131 {
132  SetTextPos( newPosition );
133 }
134 
135 
137 {
138  wxPoint delta( 0, 0 );
139  EDA_RECT bbox = GetTextBox();
140 
141  if( GetTextAngle() == 0.0 )
142  {
144  delta.x = bbox.GetWidth() / 2;
146  delta.x = - bbox.GetWidth() / 2;
147 
149  delta.y = - bbox.GetHeight() / 2;
151  delta.y = bbox.GetHeight() / 2;
152  }
153  else
154  {
156  delta.y = bbox.GetWidth() / 2;
158  delta.y = - bbox.GetWidth() / 2;
159 
161  delta.x = + bbox.GetHeight() / 2;
163  delta.x = - bbox.GetHeight() / 2;
164  }
165 
166  if( inverse )
167  SetTextPos( GetTextPos() - delta );
168  else
169  SetTextPos( GetTextPos() + delta );
170 }
171 
172 
173 void LIB_TEXT::MirrorHorizontal( const wxPoint& center )
174 {
175  NormalizeJustification( false );
176  int x = GetTextPos().x;
177 
178  x -= center.x;
179  x *= -1;
180  x += center.x;
181 
182  if( GetTextAngle() == 0.0 )
183  {
188  }
189  else
190  {
195  }
196 
197  SetTextX( x );
198  NormalizeJustification( true );
199 }
200 
201 
202 void LIB_TEXT::MirrorVertical( const wxPoint& center )
203 {
204  NormalizeJustification( false );
205  int y = GetTextPos().y;
206 
207  y -= center.y;
208  y *= -1;
209  y += center.y;
210 
211  if( GetTextAngle() == 0.0 )
212  {
217  }
218  else
219  {
224  }
225 
226  SetTextY( y );
227  NormalizeJustification( true );
228 }
229 
230 
231 void LIB_TEXT::Rotate( const wxPoint& center, bool aRotateCCW )
232 {
233  NormalizeJustification( false );
234  int rot_angle = aRotateCCW ? -900 : 900;
235 
236  wxPoint pt = GetTextPos();
237  RotatePoint( &pt, center, rot_angle );
238  SetTextPos( pt );
239 
240  if( GetTextAngle() == 0.0 )
241  {
242  SetTextAngle( 900 );
243  }
244  else
245  {
246  // 180ยบ of rotation is a mirror
247 
252 
257 
258  SetTextAngle( 0 );
259  }
260 
261  NormalizeJustification( true );
262 }
263 
264 
265 void LIB_TEXT::Plot( PLOTTER* plotter, const wxPoint& offset, bool fill,
266  const TRANSFORM& aTransform ) const
267 {
268  wxASSERT( plotter != NULL );
269 
270  EDA_RECT bBox = GetBoundingBox();
271  // convert coordinates from draw Y axis to symbol_editor Y axis
272  bBox.RevertYAxis();
273  wxPoint txtpos = bBox.Centre();
274 
275  // The text orientation may need to be flipped if the transformation matrix causes xy
276  // axes to be flipped.
277  int t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
278  wxPoint pos = aTransform.TransformCoordinate( txtpos ) + offset;
279 
280  // Get color
281  COLOR4D color;
282 
283  if( plotter->GetColorMode() ) // Used normal color or selected color
285  else
287 
288  RENDER_SETTINGS* settings = plotter->RenderSettings();
289 
290  int penWidth = std::max( GetEffectiveTextPenWidth(), settings->GetMinPenWidth() );
291 
292  plotter->Text( pos, color, GetText(), t1 ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT, GetTextSize(),
294  IsBold() );
295 }
296 
297 
299 {
300  return GetEffectiveTextPenWidth();
301 }
302 
303 
304 void LIB_TEXT::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
305  const TRANSFORM& aTransform )
306 {
307  wxDC* DC = aSettings->GetPrintDC();
308  COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
309  int penWidth = std::max( GetEffectiveTextPenWidth(), aSettings->GetDefaultPenWidth() );
310 
311  // Calculate the text orientation, according to the symbol orientation/mirror (needed when
312  // draw text in schematic)
313  int orient = (int) GetTextAngle();
314 
315  if( aTransform.y1 ) // Rotate symbol 90 degrees.
316  {
317  if( orient == TEXT_ANGLE_HORIZ )
318  orient = TEXT_ANGLE_VERT;
319  else
320  orient = TEXT_ANGLE_HORIZ;
321  }
322 
323  /*
324  * Calculate the text justification, according to the symbol orientation/mirror.
325  * This is a bit complicated due to cumulative calculations:
326  * - numerous cases (mirrored or not, rotation)
327  * - the GRText function will also recalculate H and V justifications according to the text
328  * orientation.
329  * - When a symbol is mirrored, the text is not mirrored and justifications are complicated
330  * to calculate so the more easily way is to use no justifications (centered text) and
331  * use GetBoundingBox to know the text coordinate considered as centered
332  */
333  EDA_RECT bBox = GetBoundingBox();
334 
335  // convert coordinates from draw Y axis to symbol_editor Y axis:
336  bBox.RevertYAxis();
337  wxPoint txtpos = bBox.Centre();
338 
339  // Calculate pos according to mirror/rotation.
340  txtpos = aTransform.TransformCoordinate( txtpos ) + aOffset;
341 
342  GRText( DC, txtpos, color, GetShownText(), orient, GetTextSize(), GR_TEXT_HJUSTIFY_CENTER,
343  GR_TEXT_VJUSTIFY_CENTER, penWidth, IsItalic(), IsBold() );
344 }
345 
346 
348 {
349  LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
350 
351  wxString msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextThickness() );
352  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg ) );
353 }
354 
355 
357 {
358  /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
359  * calling GetTextBox() that works using top to bottom Y axis orientation.
360  */
361  EDA_RECT rect = GetTextBox( -1, true );
362  rect.RevertYAxis();
363 
364  // We are using now a bottom to top Y axis.
365  wxPoint orig = rect.GetOrigin();
366  wxPoint end = rect.GetEnd();
367 
368  RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
369  RotatePoint( &end, GetTextPos(), -GetTextAngle() );
370 
371  rect.SetOrigin( orig );
372  rect.SetEnd( end );
373 
374  // We are using now a top to bottom Y axis:
375  rect.RevertYAxis();
376 
377  return rect;
378 }
379 
380 
381 wxString LIB_TEXT::GetSelectMenuText( EDA_UNITS aUnits ) const
382 {
383  return wxString::Format( _( "Graphic Text '%s'" ), ShortenedShownText() );
384 }
385 
386 
388 {
389  return BITMAPS::text;
390 }
391 
392 
393 void LIB_TEXT::BeginEdit( const wxPoint aPosition )
394 {
395  SetTextPos( aPosition );
396 }
397 
398 
399 void LIB_TEXT::CalcEdit( const wxPoint& aPosition )
400 {
401  SetTextPos( aPosition );
402 }
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_text.cpp:130
#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:190
void Offset(const wxPoint &aOffset)
Definition: eda_text.h:259
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:90
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:206
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:347
void NormalizeJustification(bool inverse)
Definition: lib_text.cpp:136
void CalcEdit(const wxPoint &aPosition) override
Calculate the attributes of an item at aPosition when it is being edited.
Definition: lib_text.cpp:399
void SetEffects(const EDA_TEXT &aSrc)
Set the text effects from another instance.
Definition: eda_text.cpp:137
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_text.cpp:173
int color
Definition: DXF_plotter.cpp:60
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:223
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:253
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:92
int GetWidth() const
Definition: eda_rect.h:114
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:265
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
double GetTextAngle() const
Definition: eda_text.h:181
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:126
int x1
Definition: transform.h:48
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:244
Definition: color4d.h:44
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:228
int GetTextThickness() const
Definition: eda_text.h:167
The base class for create windows for drawing purpose.
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
Definition: eda_rect.h:203
void BeginEdit(const wxPoint aStartPoint) override
Begin drawing a component library draw item at aPosition.
Definition: lib_text.cpp:393
void print(const RENDER_SETTINGS *aSettings, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_text.cpp:304
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:62
int GetEffectiveTextPenWidth(int aDefaultWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultWidth.
Definition: eda_text.cpp:157
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:53
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:61
int GetTextHeight() const
Definition: eda_text.h:251
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:219
void SetTextX(int aX)
Definition: eda_text.h:256
bool IsItalic() const
Definition: eda_text.h:187
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:108
int m_unit
Unit identification for multiple parts per package.
Definition: lib_item.h:315
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:205
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:121
#define NULL
COMPARE_FLAGS
The list of flags used by the compare function.
Definition: lib_item.h:82
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
Definition: lib_text.cpp:231
const wxPoint GetOrigin() const
Definition: eda_rect.h:106
void SetEnd(int x, int y)
Definition: eda_rect.h:187
const wxSize & GetTextSize() const
Definition: eda_text.h:245
int m_convert
Shape identification for alternate body styles.
Definition: lib_item.h:321
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:209
Define a library symbol object.
Definition: lib_symbol.h:93
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:202
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:202
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:381
int GetHeight() const
Definition: eda_rect.h:115
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:50
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 footprint texts)
Definition: gr_text.cpp:131
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:124
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:155
int GetTextWidth() const
Definition: eda_text.h:248
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:356
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:208
#define _(s)
Definition: 3d_actions.cpp:33
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:372
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:150
int GetPenWidth() const override
Definition: lib_text.cpp:298
The common library.
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: lib_text.cpp:387
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:97
wxPoint Centre() const
Definition: eda_rect.h:60
int GetDefaultPenWidth() const
const wxPoint & GetTextPos() const
Definition: eda_text.h:254
#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:174
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: lib_text.cpp:77
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:54
STATUS_FLAGS m_flags
Definition: eda_item.h:531
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:74
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
LIB_TEXT(LIB_PART *aParent)
Definition: lib_text.cpp:45
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetTextY(int aY)
Definition: eda_text.h:257
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:163
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98
bool GetColorMode() const
Definition: plotter.h:152