KiCad PCB EDA Suite
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, [email protected]
5 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 1992-2022 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 <pcb_edit_frame.h>
27#include <base_units.h>
28#include <bitmaps.h>
29#include <board.h>
31#include <core/mirror.h>
32#include <footprint.h>
33#include <pcb_text.h>
34#include <pcb_painter.h>
35#include <trigo.h>
36#include <string_utils.h>
38#include <callback_gal.h>
40
41
43 BOARD_ITEM( parent, PCB_TEXT_T ),
45{
46 SetMultilineAllowed( true );
47}
48
49
51{
52}
53
54
55wxString PCB_TEXT::GetShownText( int aDepth, bool aAllowExtraText ) const
56{
57 BOARD* board = dynamic_cast<BOARD*>( GetParent() );
58
59 std::function<bool( wxString* )> pcbTextResolver =
60 [&]( wxString* token ) -> bool
61 {
62 if( token->IsSameAs( wxT( "LAYER" ) ) )
63 {
64 *token = GetLayerName();
65 return true;
66 }
67
68 if( token->Contains( ':' ) )
69 {
70 wxString remainder;
71 wxString ref = token->BeforeFirst( ':', &remainder );
72 BOARD_ITEM* refItem = board->GetItem( KIID( ref ) );
73
74 if( refItem && refItem->Type() == PCB_FOOTPRINT_T )
75 {
76 FOOTPRINT* refFP = static_cast<FOOTPRINT*>( refItem );
77
78 if( refFP->ResolveTextVar( &remainder, aDepth + 1 ) )
79 {
80 *token = remainder;
81 return true;
82 }
83 }
84 }
85 return false;
86 };
87
88 std::function<bool( wxString* )> boardTextResolver =
89 [&]( wxString* token ) -> bool
90 {
91 return board->ResolveTextVar( token, aDepth + 1 );
92 };
93
94 wxString text = EDA_TEXT::GetShownText();
95
96 if( board && HasTextVars() && aDepth < 10 )
97 text = ExpandTextVars( text, &pcbTextResolver, &boardTextResolver, board->GetProject() );
98
99 return text;
100}
101
102
103double PCB_TEXT::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
104{
105 constexpr double HIDE = std::numeric_limits<double>::max();
106
107 KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
108 KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
109
110 if( aLayer == LAYER_LOCKED_ITEM_SHADOW )
111 {
112 // Hide shadow if the main layer is not shown
113 if( !aView->IsLayerVisible( m_layer ) )
114 return HIDE;
115
116 // Hide shadow on dimmed tracks
117 if( renderSettings->GetHighContrast() )
118 {
119 if( m_layer != renderSettings->GetPrimaryHighContrastLayer() )
120 return HIDE;
121 }
122 }
123
124 return 0.0;
125}
126
127
128void PCB_TEXT::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
129{
130 // Don't use GetShownText() here; we want to show the user the variable references
131 aList.emplace_back( _( "PCB Text" ), KIUI::EllipsizeStatusText( aFrame, GetText() ) );
132
133 if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
134 aList.emplace_back( _( "Status" ), _( "Locked" ) );
135
136 aList.emplace_back( _( "Layer" ), GetLayerName() );
137
138 aList.emplace_back( _( "Mirror" ), IsMirrored() ? _( "Yes" ) : _( "No" ) );
139
140 aList.emplace_back( _( "Angle" ), wxString::Format( wxT( "%g" ), GetTextAngle().AsDegrees() ) );
141
142 aList.emplace_back( _( "Font" ), GetFont() ? GetFont()->GetName() : _( "Default" ) );
143 aList.emplace_back( _( "Thickness" ), aFrame->MessageTextFromValue( GetTextThickness() ) );
144 aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetTextWidth() ) );
145 aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetTextHeight() ) );
146}
147
148
150{
151 VECTOR2I textSize( GetTextWidth(), GetTextHeight() );
152 int thickness = GetTextThickness();
153
154 return thickness * 1.5 + GetKnockoutTextMargin( textSize, thickness );
155}
156
157
159{
160 BOX2I rect = GetTextBox();
161
162 if( IsKnockout() )
163 rect.Inflate( getKnockoutMargin() );
164
165 if( !GetTextAngle().IsZero() )
167
168 return rect;
169}
170
171
172bool PCB_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
173{
174 if( IsKnockout() )
175 {
176 SHAPE_POLY_SET poly;
177
179
180 return poly.Collide( aPoint, aAccuracy );
181 }
182 else
183 {
184 return EDA_TEXT::TextHitTest( aPoint, aAccuracy );
185 }
186}
187
188
189bool PCB_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
190{
191 BOX2I rect = aRect;
192
193 rect.Inflate( aAccuracy );
194
195 if( aContains )
196 return rect.Contains( GetBoundingBox() );
197
198 return rect.Intersects( GetBoundingBox() );
199}
200
201
202void PCB_TEXT::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
203{
204 VECTOR2I pt = GetTextPos();
205 RotatePoint( pt, aRotCentre, aAngle );
206 SetTextPos( pt );
207
208 EDA_ANGLE new_angle = GetTextAngle() + aAngle;
209 new_angle.Normalize180();
210 SetTextAngle( new_angle );
211}
212
213
214void PCB_TEXT::Mirror( const VECTOR2I& aCentre, bool aMirrorAroundXAxis )
215{
216 // the position and justification are mirrored, but not the text itself
217
218 if( aMirrorAroundXAxis )
219 {
222
223 SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
224 }
225 else
226 {
229
230 SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
231 }
232}
233
234
235void PCB_TEXT::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
236{
237 if( aFlipLeftRight )
238 {
239 SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
241 }
242 else
243 {
244 SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
246 }
247
248 SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
249
250 if( ( GetLayerSet() & LSET::SideSpecificMask() ).any() )
252}
253
254
255wxString PCB_TEXT::GetSelectMenuText( UNITS_PROVIDER* aUnitsProvider ) const
256{
257 return wxString::Format( _( "PCB Text '%s' on %s"),
259 GetLayerName() );
260}
261
262
264{
265 return BITMAPS::text;
266}
267
268
270{
271 return new PCB_TEXT( *this );
272}
273
274
276{
277 assert( aImage->Type() == PCB_TEXT_T );
278
279 std::swap( *((PCB_TEXT*) this), *((PCB_TEXT*) aImage) );
280}
281
282
283std::shared_ptr<SHAPE> PCB_TEXT::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
284{
285 return GetEffectiveTextShape();
286}
287
288
290 int aClearance, int aError, ERROR_LOC aErrorLoc ) const
291{
293 KIFONT::FONT* font = getDrawFont();
294 int penWidth = GetEffectiveTextPenWidth();
295
296 // Note: this function is mainly used in 3D viewer.
297 // the polygonal shape of a text can have many basic shapes,
298 // so combining these shapes can be very useful to create a final shape
299 // swith a lot less vertices to speedup calculations using this final shape
300 // Simplify shapes is not usually always efficient, but in this case it is.
301 SHAPE_POLY_SET buffer;
302
303 CALLBACK_GAL callback_gal( empty_opts,
304 // Stroke callback
305 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
306 {
307 TransformOvalToPolygon( aBuffer, aPt1, aPt2, penWidth + ( 2 * aClearance ),
308 aError, ERROR_INSIDE );
309 },
310 // Triangulation callback
311 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
312 {
313 buffer.NewOutline();
314
315 for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
316 buffer.Append( point.x, point.y );
317 } );
318
319 font->Draw( &callback_gal, GetShownText(), GetTextPos(), GetAttributes() );
320
322 aBuffer.Append( buffer );
323}
324
325
327 int aClearance, int aError, ERROR_LOC aErrorLoc,
328 bool aIgnoreLineWidth ) const
329{
330 EDA_TEXT::TransformBoundingBoxToPolygon( &aBuffer, aClearance );
331}
332
333
334static struct PCB_TEXT_DESC
335{
337 {
344 }
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:180
PCB_LAYER_ID m_layer
Definition: board_item.h:329
virtual bool IsKnockout() const
Definition: board_item.h:250
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:214
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:43
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:185
virtual bool IsLocked() const
Definition: board_item.cpp:71
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:163
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:94
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
BOARD_ITEM * GetItem(const KIID &aID) const
Definition: board.cpp:958
bool ResolveTextVar(wxString *token, int aDepth) const
Definition: board.cpp:310
PROJECT * GetProject() const
Definition: board.h:440
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:269
bool Contains(const Vec &aPoint) const
Definition: box2.h:141
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
const BOX2< Vec > GetBoundingBoxRotated(const VECTOR2I &aRotCenter, const EDA_ANGLE &aAngle) const
Useful to calculate bounding box of rotated items, when rotation is not cardinal.
Definition: box2.h:650
EDA_ANGLE Normalize180()
Definition: eda_angle.h:271
The base class for create windows for drawing purpose.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:72
void TransformBoundingBoxToPolygon(SHAPE_POLY_SET *aBuffer, int aClearance) const
Convert the text bounding box to a rectangular polygon depending on the text orientation,...
Definition: eda_text.cpp:945
int GetTextHeight() const
Definition: eda_text.h:195
BOX2I 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:503
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:201
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:120
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:87
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:371
void SetTextX(int aX)
Definition: eda_text.cpp:377
KIFONT::FONT * GetFont() const
Definition: eda_text.h:181
void SetMirrored(bool isMirrored)
Definition: eda_text.cpp:224
void SetTextY(int aY)
Definition: eda_text.cpp:383
int GetTextWidth() const
Definition: eda_text.h:192
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:142
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition: eda_text.h:103
virtual KIFONT::FONT * getDrawFont() const
Definition: eda_text.cpp:426
virtual bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:623
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:165
bool IsMirrored() const
Definition: eda_text.h:132
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:297
std::shared_ptr< SHAPE_COMPOUND > GetEffectiveTextShape(bool aTriangulate=true, bool aUseTextRotation=true) const
build a list of segments (SHAPE_SEGMENT) to describe a text shape.
Definition: eda_text.cpp:853
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:193
int GetTextThickness() const
Definition: eda_text.h:112
virtual wxString GetShownText(int aDepth=0, bool aAllowExtraText=true) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:98
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.cpp:232
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:240
bool ResolveTextVar(wxString *token, int aDepth=0) const
Resolve any references to system tokens supported by the component.
Definition: footprint.cpp:490
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttrs) const
Draw a string.
Definition: font.cpp:159
Contains methods for drawing PCB-specific items.
Definition: pcb_painter.h:157
virtual PCB_RENDER_SETTINGS * GetSettings() override
Return a pointer to current settings that are going to be used when drawing items.
Definition: pcb_painter.h:162
PCB specific render settings.
Definition: pcb_painter.h:72
PCB_LAYER_ID GetPrimaryHighContrastLayer() const
Return the board layer which is in high-contrast mode.
bool GetHighContrast() const
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:69
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:410
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:213
Definition: kiid.h:48
static LSET SideSpecificMask()
Definition: lset.cpp:908
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: pcb_text.cpp:128
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool aIgnoreLineWidth=false) const override
Convert the item shape to a closed polygon.
Definition: pcb_text.cpp:326
virtual void swapData(BOARD_ITEM *aImage) override
Definition: pcb_text.cpp:275
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: pcb_text.cpp:283
void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_text.cpp:235
~PCB_TEXT()
Definition: pcb_text.cpp:50
wxString GetShownText(int aDepth=0, bool aAllowExtraText=true) const override
Return the string actually shown after processing of the base text.
Definition: pcb_text.cpp:55
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_text.cpp:103
int getKnockoutMargin() const
Definition: pcb_text.cpp:149
void Mirror(const VECTOR2I &aCentre, bool aMirrorAroundXAxis)
Definition: pcb_text.cpp:214
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_text.cpp:158
bool TextHitTest(const VECTOR2I &aPoint, int aAccuracy=0) const override
Test if aPoint is within the bounds of this object.
Definition: pcb_text.cpp:172
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_text.cpp:269
void TransformTextToPolySet(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc) const
Function TransformTextToPolySet Convert the text to a polygonSet describing the actual character stro...
Definition: pcb_text.cpp:289
PCB_TEXT(BOARD_ITEM *parent)
Definition: pcb_text.cpp:42
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
Definition: pcb_text.cpp:202
wxString GetSelectMenuText(UNITS_PROVIDER *aUnitsProvider) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: pcb_text.cpp:255
BITMAPS GetMenuImage() const override
Definition: pcb_text.cpp:263
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:67
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:69
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
Represent a set of closed polygons.
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Add a new vertex to the contour indexed by aOutline and aHole (defaults to the outline of the last po...
void Simplify(POLYGON_MODE aFastMode)
int NewOutline()
Creates a new hole in a given outline.
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A lower-precision version of StringFromValue().
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:57
void TransformOvalToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aWidth, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a oblong shape to a polygon, using multiple segments.
#define _(s)
static constexpr EDA_ANGLE & ANGLE_180
Definition: eda_angle.h:416
static constexpr EDA_ANGLE & ANGLE_HORIZONTAL
Definition: eda_angle.h:408
static constexpr EDA_ANGLE & ANGLE_VERTICAL
Definition: eda_angle.h:409
#define PCB_EDIT_FRAME_NAME
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
@ ERROR_INSIDE
int GetKnockoutTextMargin(const VECTOR2I &aSize, int aThickness)
Returns the margin for knockout text.
Definition: gr_text.h:97
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition: layer_ids.h:147
@ LAYER_LOCKED_ITEM_SHADOW
shadow layer for locked items
Definition: layer_ids.h:239
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:544
T MIRRORVAL(T aPoint, T aMirrorRef)
Returns the mirror of aPoint relative to the aMirrorRef.
Definition: mirror.h:31
wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
Definition: ui_common.cpp:203
wxString EllipsizeStatusText(wxWindow *aWindow, const wxString &aString)
Ellipsize text (at the end) to be no more than 1/3 of the window width.
Definition: ui_common.cpp:185
static struct PCB_TEXT_DESC _PCB_TEXT_DESC
#define TYPE_HASH(x)
Definition: property.h:61
#define REGISTER_TYPE(x)
Definition: property_mgr.h:292
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
GR_TEXT_H_ALIGN_T
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:90
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86