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