KiCad PCB EDA Suite
class_pcb_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) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <fctsys.h>
27 #include <base_struct.h>
28 #include <pcb_edit_frame.h>
29 #include <base_units.h>
30 #include <bitmaps.h>
31 #include <class_board.h>
32 #include <class_pcb_text.h>
33 #include <pcb_painter.h>
34 
36 
37 
39  BOARD_ITEM( parent, PCB_TEXT_T ),
40  EDA_TEXT()
41 {
42  SetMultilineAllowed( true );
43 }
44 
45 
47 {
48 }
49 
50 
51 wxString TEXTE_PCB::GetShownText( int aDepth ) const
52 {
53  BOARD* board = dynamic_cast<BOARD*>( GetParent() );
54 
55  std::function<bool( wxString* )> pcbTextResolver =
56  [&]( wxString* token ) -> bool
57  {
58  if( token->IsSameAs( wxT( "LAYER" ) ) )
59  {
60  *token = GetLayerName();
61  return true;
62  }
63 
64  if( token->Contains( ':' ) )
65  {
66  wxString remainder;
67  wxString ref = token->BeforeFirst( ':', &remainder );
68  BOARD_ITEM* refItem = board->GetItem( KIID( ref ) );
69 
70  if( refItem && refItem->Type() == PCB_MODULE_T )
71  {
72  MODULE* refModule = static_cast<MODULE*>( refItem );
73 
74  if( refModule->ResolveTextVar( &remainder, aDepth + 1 ) )
75  {
76  *token = remainder;
77  return true;
78  }
79  }
80  }
81  return false;
82  };
83 
84  std::function<bool( wxString* )> boardTextResolver =
85  [&]( wxString* token ) -> bool
86  {
87  return board->ResolveTextVar( token, aDepth + 1 );
88  };
89 
90  bool processTextVars = false;
91  wxString text = EDA_TEXT::GetShownText( &processTextVars );
92 
93  if( board && processTextVars && aDepth < 10 )
94  text = ExpandTextVars( text, &pcbTextResolver, board->GetProject(), &boardTextResolver );
95 
96  return text;
97 }
98 
99 
100 void TEXTE_PCB::SetTextAngle( double aAngle )
101 {
103 }
104 
105 
106 void TEXTE_PCB::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
107 {
108  wxString msg;
109 
110  wxCHECK_RET( m_Parent != NULL, wxT( "TEXTE_PCB::GetMsgPanelInfo() m_Parent is NULL." ) );
111 
112  if( BaseType( m_Parent->Type() ) == PCB_DIMENSION_T )
113  aList.emplace_back( _( "Dimension" ), GetShownText(), DARKGREEN );
114  else
115  aList.emplace_back( _( "PCB Text" ), GetShownText(), DARKGREEN );
116 
117  aList.emplace_back( _( "Layer" ), GetLayerName(), BLUE );
118 
119  if( !IsMirrored() )
120  aList.emplace_back( _( "Mirror" ), _( "No" ), DARKGREEN );
121  else
122  aList.emplace_back( _( "Mirror" ), _( "Yes" ), DARKGREEN );
123 
124  msg.Printf( wxT( "%.1f" ), GetTextAngle() / 10.0 );
125  aList.emplace_back( _( "Angle" ), msg, DARKGREEN );
126 
127  msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextThickness() );
128  aList.emplace_back( _( "Thickness" ), msg, MAGENTA );
129 
130  msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextWidth() );
131  aList.emplace_back( _( "Width" ), msg, RED );
132 
133  msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextHeight() );
134  aList.emplace_back( _( "Height" ), msg, RED );
135 }
136 
137 
139 {
140  EDA_RECT rect = GetTextBox();
141 
142  if( GetTextAngle() )
143  rect = rect.GetBoundingBoxRotated( GetTextPos(), GetTextAngle() );
144 
145  return rect;
146 }
147 
148 
149 void TEXTE_PCB::Rotate( const wxPoint& aRotCentre, double aAngle )
150 {
151  wxPoint pt = GetTextPos();
152  RotatePoint( &pt, aRotCentre, aAngle );
153  SetTextPos( pt );
154 
155  SetTextAngle( GetTextAngle() + aAngle );
156 }
157 
158 
159 void TEXTE_PCB::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
160 {
161  double angle = GetTextAngle();
162  bool vertical = KiROUND( angle ) % 1800 == 900;
163 
164  if( KiROUND( angle ) != 0 )
165  {
166  Rotate( aCentre, -angle );
167 
168  if( vertical )
169  aFlipLeftRight = !aFlipLeftRight;
170  }
171 
172  // Flip the bounding box
173  EDA_RECT box = GetTextBox();
174  int left = box.GetLeft();
175  int right = box.GetRight();
176  int top = box.GetTop();
177  int bottom = box.GetBottom();
178 
179  if( aFlipLeftRight )
180  {
181  MIRROR( left, aCentre.x );
182  MIRROR( right, aCentre.x );
183  std::swap( left, right );
184  }
185  else
186  {
187  MIRROR( top, aCentre.y );
188  MIRROR( bottom, aCentre.y );
189  std::swap( top, bottom );
190  }
191 
192  // Now put the text back in it (these look backwards but remember that out text will
193  // be mirrored when all is said and done)
194  switch( GetHorizJustify() )
195  {
196  case GR_TEXT_HJUSTIFY_LEFT: SetTextX( right ); break;
197  case GR_TEXT_HJUSTIFY_CENTER: SetTextX( ( left + right ) / 2 ); break;
198  case GR_TEXT_HJUSTIFY_RIGHT: SetTextX( left ); break;
199  }
200 
201  switch( GetVertJustify() )
202  {
203  case GR_TEXT_VJUSTIFY_TOP: SetTextY( bottom ); break;
204  case GR_TEXT_VJUSTIFY_CENTER: SetTextY( ( top + bottom ) / 2 ); break;
205  case GR_TEXT_VJUSTIFY_BOTTOM: SetTextY( top ); break;
206  }
207 
208  // And restore orientation
209  if( KiROUND( angle ) != 0 )
210  Rotate( aCentre, angle );
211 
212  SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
213  SetMirrored( !IsMirrored() );
214 }
215 
216 
217 wxString TEXTE_PCB::GetSelectMenuText( EDA_UNITS aUnits ) const
218 {
219  return wxString::Format( _( "Pcb Text \"%s\" on %s"), ShortenedShownText(), GetLayerName() );
220 }
221 
222 
224 {
225  return text_xpm;
226 }
227 
228 
230 {
231  return new TEXTE_PCB( *this );
232 }
233 
234 
236 {
237  assert( aImage->Type() == PCB_TEXT_T );
238 
239  std::swap( *((TEXTE_PCB*) this), *((TEXTE_PCB*) aImage) );
240 }
241 
242 
243 std::shared_ptr<SHAPE> TEXTE_PCB::GetEffectiveShape( PCB_LAYER_ID aLayer ) const
244 {
245  return GetEffectiveTextShape();
246 }
247 
248 
249 static struct TEXTE_PCB_DESC
250 {
252  {
259  }
void SetMirrored(bool isMirrored)
Definition: eda_text.h:188
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER) const override
Function GetEffectiveShape Some pad shapes can be complex (rounded/chamfered rectangle),...
EDA_UNITS
Definition: common.h:198
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
void SetTextAngle(double aAngle) override
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:61
EDA_ITEM * m_Parent
Linked list: Link (parent struct)
Definition: base_struct.h:174
#define TYPE_HASH(x)
Macro to generate unique identifier for a type
Definition: property.h:53
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:200
bool IsMirrored() const
Definition: eda_text.h:189
PNG memory record (file in memory).
Definition: bitmap_def.h:29
TEXTE_PCB class definition.
bool ResolveTextVar(wxString *token, int aDepth=0) const
Resolve any references to system tokens supported by the component.
const EDA_RECT GetBoundingBoxRotated(wxPoint aRotCenter, double aAngle) const
Function GetBoundingBoxRotated.
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Function Rotate Rotate this object.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
Implementation of conversion functions that require both schematic and board internal units.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
int GetTop() const
Definition: eda_rect.h:123
constexpr KICAD_T BaseType(const KICAD_T aType)
Returns the underlying type of the given type.
Definition: typeinfo.h:233
void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Function Flip Flip this object, i.e.
int GetLeft() const
Definition: eda_rect.h:122
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Function FlippedLayerNumber.
Definition: lset.cpp:490
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
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
PROJECT * GetProject() const
Definition: class_board.h:356
const BITMAP_OPAQUE text_xpm[1]
Definition: text.cpp:27
double GetTextAngle() const
Definition: eda_text.h:174
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:92
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.
BOARD_ITEM * GetItem(const KIID &aID)
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names
Definition: property_mgr.h:244
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
int GetBottom() const
Definition: eda_rect.h:124
int GetTextHeight() const
Definition: eda_text.h:245
void SetTextX(int aX)
Definition: eda_text.h:250
static struct TEXTE_PCB_DESC _TEXTE_PCB_DESC
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:113
PCB_RENDER_SETTINGS Stores PCB specific render settings.
Definition: pcb_painter.h:64
void MIRROR(T &aPoint, const T &aMirrorRef)
Definition: macros.h:175
Definition: common.h:68
class MODULE, a footprint
Definition: typeinfo.h:89
PCB_LAYER_ID
A quick note on layer IDs:
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:199
#define NULL
virtual BOARD * GetBoard() const
Function GetBoard returns the BOARD in which this BOARD_ITEM resides, or NULL if none.
int GetRight() const
Definition: eda_rect.h:121
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declares an inheritance relationship between types.
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:201
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
class DIMENSION: abstract dimension meta-type
Definition: typeinfo.h:100
Definition: color4d.h:59
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.h:196
wxString ExpandTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aLocalResolver, const PROJECT *aProject, const std::function< bool(wxString *)> *aFallbackResolver)
Expand '${var-name}' templates in text.
Definition: common.cpp:384
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:124
Definition: color4d.h:56
int GetTextWidth() const
Definition: eda_text.h:242
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
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:178
#define _(s)
Definition: 3d_actions.cpp:33
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
void AddTypeCast(TYPE_CAST_BASE *aCast)
Registers a type converter.
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
const wxPoint & GetTextPos() const
Definition: eda_text.h:248
Provides class metadata.
Definition: property_mgr.h:58
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo populates aList of MSG_PANEL_ITEM objects with it's internal state for displ...
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.
T NormalizeAngle360Min(T Angle)
Normalize angle to be > -360.0 and < 360.0 Angle equal to -360 or +360 are set to 0.
Definition: trigo.h:242
std::shared_ptr< SHAPE > GetEffectiveTextShape() const
Definition: eda_text.cpp:620
bool ResolveTextVar(wxString *token, int aDepth) const
BOARD_ITEM_CONTAINER * GetParent() const
wxString GetLayerName() const
Function GetLayerName returns the name of the PCB layer on which the item resides.
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:134
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetTextY(int aY)
Definition: eda_text.h:251
TEXTE_PCB(BOARD_ITEM *parent)
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193