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-2019 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>
42 #include <lib_text.h>
43 
44 
46  LIB_ITEM( LIB_TEXT_T, aParent ),
47  EDA_TEXT()
48 {
49  SetTextSize( wxSize( Mils2iu( 50 ), Mils2iu( 50 ) ) );
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& other ) const
93 {
94  wxASSERT( other.Type() == LIB_TEXT_T );
95 
96  const LIB_TEXT* tmp = ( LIB_TEXT* ) &other;
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 bool LIB_TEXT::Inside( EDA_RECT& rect ) const
126 {
127  return rect.Intersects( GetBoundingBox() );
128 }
129 
130 
131 void LIB_TEXT::MoveTo( const wxPoint& newPosition )
132 {
133  SetTextPos( newPosition );
134 }
135 
136 
137 void LIB_TEXT::MirrorHorizontal( const wxPoint& center )
138 {
139  int x = GetTextPos().x;
140 
141  x -= center.x;
142  x *= -1;
143  x += center.x;
144 
145  SetTextX( x );
146 }
147 
148 
149 void LIB_TEXT::MirrorVertical( const wxPoint& center )
150 {
151  int y = GetTextPos().y;
152 
153  y -= center.y;
154  y *= -1;
155  y += center.y;
156 
157  SetTextY( y );
158 }
159 
160 
161 void LIB_TEXT::Rotate( const wxPoint& center, bool aRotateCCW )
162 {
163  int rot_angle = aRotateCCW ? -900 : 900;
164 
165  wxPoint pt = GetTextPos();
166  RotatePoint( &pt, center, rot_angle );
167  SetTextPos( pt );
168 
169  SetTextAngle( GetTextAngle() != 0.0 ? 0 : 900 );
170 }
171 
172 
173 void LIB_TEXT::Plot( PLOTTER* plotter, const wxPoint& offset, bool fill,
174  const TRANSFORM& aTransform )
175 {
176  wxASSERT( plotter != NULL );
177 
178  EDA_RECT bBox = GetBoundingBox();
179  // convert coordinates from draw Y axis to libedit Y axis
180  bBox.RevertYAxis();
181  wxPoint txtpos = bBox.Centre();
182 
183  /* The text orientation may need to be flipped if the
184  * transformation matrix causes xy axes to be flipped. */
185  int t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
186  wxPoint pos = aTransform.TransformCoordinate( txtpos ) + offset;
187 
188  // Get color
189  COLOR4D color;
190 
191  if( plotter->GetColorMode() ) // Used normal color or selected color
193  else
195 
196  plotter->Text( pos, color, GetText(), t1 ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT,
198  GetPenSize(), IsItalic(), IsBold() );
199 }
200 
201 
203 {
204  int pensize = GetThickness();
205 
206  if( pensize == 0 ) // Use default values for pen size
207  {
208  if( IsBold() )
209  pensize = GetPenSizeForBold( GetTextWidth() );
210  else
211  pensize = GetDefaultLineThickness();
212  }
213 
214  // Clip pen size for small texts:
215  pensize = Clamp_Text_PenSize( pensize, GetTextSize(), IsBold() );
216  return pensize;
217 }
218 
219 
220 void LIB_TEXT::print( wxDC* aDC, const wxPoint& aOffset, void* aData, const TRANSFORM& aTransform )
221 {
223 
224  /* Calculate the text orientation, according to the component
225  * orientation/mirror (needed when draw text in schematic)
226  */
227  int orient = GetTextAngle();
228 
229  if( aTransform.y1 ) // Rotate component 90 degrees.
230  {
231  if( orient == TEXT_ANGLE_HORIZ )
232  orient = TEXT_ANGLE_VERT;
233  else
234  orient = TEXT_ANGLE_HORIZ;
235  }
236 
237  /* Calculate the text justification, according to the component
238  * orientation/mirror this is a bit complicated due to cumulative
239  * calculations:
240  * - numerous cases (mirrored or not, rotation)
241  * - the DrawGraphicText function recalculate also H and H justifications
242  * according to the text orientation.
243  * - When a component is mirrored, the text is not mirrored and
244  * justifications are complicated to calculate
245  * so the more easily way is to use no justifications ( Centered text )
246  * and use GetBoundaryBox to know the text coordinate considered as centered
247  */
248  EDA_RECT bBox = GetBoundingBox();
249 
250  // convert coordinates from draw Y axis to libedit Y axis:
251  bBox.RevertYAxis();
252  wxPoint txtpos = bBox.Centre();
253 
254  // Calculate pos according to mirror/rotation.
255  txtpos = aTransform.TransformCoordinate( txtpos ) + aOffset;
256 
257  GRText( aDC, txtpos, color, GetShownText(), orient, GetTextSize(), GR_TEXT_HJUSTIFY_CENTER,
259 }
260 
261 
263 {
264  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
265 
266  wxString msg = MessageTextFromValue( aUnits, GetThickness(), true );
267  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
268 }
269 
270 
272 {
273  /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
274  * calling GetTextBox() that works using top to bottom Y axis orientation.
275  */
276  EDA_RECT rect = GetTextBox( -1, -1, true, GetTextMarkupFlags() );
277  rect.RevertYAxis();
278 
279  // We are using now a bottom to top Y axis.
280  wxPoint orig = rect.GetOrigin();
281  wxPoint end = rect.GetEnd();
282 
283  RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
284  RotatePoint( &end, GetTextPos(), -GetTextAngle() );
285 
286  rect.SetOrigin( orig );
287  rect.SetEnd( end );
288 
289  // We are using now a top to bottom Y axis:
290  rect.RevertYAxis();
291 
292  return rect;
293 }
294 
295 
296 wxString LIB_TEXT::GetSelectMenuText( EDA_UNITS aUnits ) const
297 {
298  return wxString::Format( _( "Graphic Text \"%s\"" ), ShortenedShownText() );
299 }
300 
301 
303 {
304  return text_xpm;
305 }
306 
307 
308 void LIB_TEXT::BeginEdit( const wxPoint aPosition )
309 {
310  SetTextPos( aPosition );
311 }
312 
313 
314 void LIB_TEXT::CalcEdit( const wxPoint& aPosition )
315 {
316  SetTextPos( aPosition );
317 }
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:131
EDA_UNITS
Definition: common.h:72
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:173
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
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:127
virtual COLOR4D GetDefaultColor()
Definition: lib_item.cpp:139
void CalcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_text.cpp:314
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:137
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:262
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:231
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:125
void BeginEdit(const wxPoint aStartPoint) override
Begin drawing a component library draw item at aPosition.
Definition: lib_text.cpp:308
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:53
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:61
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
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:111
#define NULL
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
Definition: lib_text.cpp:161
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
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:149
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:296
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:202
Base plotter engine class.
Definition: plotter.h:103
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_text.cpp:119
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:302
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:271
#define _(s)
Definition: 3d_actions.cpp:31
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:184
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:163
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:77
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
Message panel definition file.
void print(wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_text.cpp:220
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:45
int compare(const LIB_ITEM &aOther) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_text.cpp:92
void SetTextY(int aY)
Definition: eda_text.h:235
KICAD_T Type() const
Function Type()
Definition: base_struct.h:207
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
bool GetColorMode() const
Definition: plotter.h:137