KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_text_dims.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) 2021-2023 KiCad Developers.
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
24#include <macros.h>
25#include <pcb_field.h>
26#include <pcb_text.h>
27#include <pcb_textbox.h>
28#include <drc/drc_engine.h>
29#include <drc/drc_item.h>
30#include <drc/drc_rule.h>
32#include <font/font.h>
33
34
35/*
36 Text dimensions tests.
37 Errors generated:
38 - DRCE_TEXT_HEIGHT
39 - DRCE_TEXT_THICKNESS
40*/
41
43{
44public:
46 {
47 }
48
50 {
51 }
52
53 virtual bool Run() override;
54
55 virtual const wxString GetName() const override
56 {
57 return wxT( "text_dimensions" );
58 };
59
60 virtual const wxString GetDescription() const override
61 {
62 return wxT( "Tests text height and thickness" );
63 }
64};
65
66
68{
69 const int progressDelta = 250;
70 int count = 0;
71 int ii = 0;
72
75 {
76 reportAux( wxT( "Text dimension violations ignored. Tests not run." ) );
77 return true; // continue with other tests
78 }
79
82 {
83 reportAux( wxT( "No text height or text thickness constraints found. Tests not run." ) );
84 return true; // continue with other tests
85 }
86
87 if( !reportPhase( _( "Checking text dimensions..." ) ) )
88 return false; // DRC cancelled
89
90 auto checkTextHeight =
91 [&]( BOARD_ITEM* item, EDA_TEXT* text ) -> bool
92 {
94 return false;
95
97 nullptr, item->GetLayer() );
98
99 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
100 return true;
101
102 int actualHeight = text->GetTextSize().y;
103
104 if( constraint.Value().HasMin() && actualHeight < constraint.Value().Min() )
105 {
106 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TEXT_HEIGHT );
107 wxString msg = formatMsg( _( "(%s min height %s; actual %s)" ),
108 constraint.GetName(),
109 constraint.Value().Min(),
110 actualHeight );
111
112 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
113 drcItem->SetItems( item );
114 drcItem->SetViolatingRule( constraint.GetParentRule() );
115
116 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
117 }
118
119 if( constraint.Value().HasMax() && actualHeight > constraint.Value().Max() )
120 {
121 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TEXT_HEIGHT );
122 wxString msg = formatMsg( _( "(%s max height %s; actual %s)" ),
123 constraint.GetName(),
124 constraint.Value().Max(),
125 actualHeight );
126
127 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
128 drcItem->SetItems( item );
129 drcItem->SetViolatingRule( constraint.GetParentRule() );
130
131 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
132 }
133
134 return true;
135 };
136
137 auto checkTextThickness =
138 [&]( BOARD_ITEM* item, EDA_TEXT* text ) -> bool
139 {
141 nullptr, item->GetLayer() );
142
143 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
144 return true;
145
146 KIFONT::FONT* font = text->GetFont();
147
148 if( !font )
149 font = KIFONT::FONT::GetFont( wxEmptyString, text->IsBold(), text->IsItalic() );
150
151 if( font->IsOutline() )
152 {
153 if( !constraint.Value().HasMin() || constraint.Value().Min() <= 0 )
154 return true;
155
156 auto* glyphs = text->GetRenderCache( font, text->GetShownText( true ) );
157 bool collapsedStroke = false;
158 bool collapsedArea = false;
159
160 for( const std::unique_ptr<KIFONT::GLYPH>& glyph : *glyphs )
161 {
162 // Ensure the glyph is a OUTLINE_GLYPH (for instance, overbars in outline
163 // font text are represented as STROKE_GLYPHs).
164 if( !glyph->IsOutline() )
165 continue;
166
167 auto outlineGlyph = static_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() );
168 int outlineCount = outlineGlyph->OutlineCount();
169 int holeCount = 0;
170
171 if( outlineCount == 0 )
172 continue; // ignore spaces
173
174 for( ii = 0; ii < outlineCount; ++ii )
175 holeCount += outlineGlyph->HoleCount( ii );
176
177 SHAPE_POLY_SET poly = outlineGlyph->CloneDropTriangulation();
178 poly.Deflate( constraint.Value().Min() / 2,
179 CORNER_STRATEGY::CHAMFER_ALL_CORNERS, ARC_LOW_DEF );
181
182 int resultingOutlineCount = poly.OutlineCount();
183 int resultingHoleCount = 0;
184
185 for( ii = 0; ii < resultingOutlineCount; ++ii )
186 resultingHoleCount += poly.HoleCount( ii );
187
188 if( ( resultingOutlineCount != outlineCount )
189 || ( resultingHoleCount != holeCount ) )
190 {
191 collapsedStroke = true;
192 break;
193 }
194
195 double glyphArea = outlineGlyph->Area();
196
197 if( glyphArea == 0 )
198 continue;
199
200 poly.Inflate( constraint.Value().Min() / 2,
201 CORNER_STRATEGY::CHAMFER_ALL_CORNERS, ARC_LOW_DEF, true );
202
203 double resultingGlyphArea = poly.Area();
204
205 if( ( std::abs( resultingGlyphArea - glyphArea ) / glyphArea ) > 0.1 )
206 {
207 collapsedArea = true;
208 break;
209 }
210 }
211
212 if( collapsedStroke || collapsedArea )
213 {
214 auto drcItem = DRC_ITEM::Create( DRCE_TEXT_THICKNESS );
215 wxString msg;
216
217 msg = _( "(TrueType font characters with insufficient stroke weight)" );
218
219 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
220 drcItem->SetItems( item );
221 drcItem->SetViolatingRule( constraint.GetParentRule() );
222
223 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
224 }
225 }
226 else
227 {
228 int actualThickness = text->GetEffectiveTextPenWidth();
229
230 if( constraint.Value().HasMin() && actualThickness < constraint.Value().Min() )
231 {
232 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TEXT_THICKNESS );
233 wxString msg = formatMsg( _( "(%s min thickness %s; actual %s)" ),
234 constraint.GetName(),
235 constraint.Value().Min(),
236 actualThickness );
237
238 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
239 drcItem->SetItems( item );
240 drcItem->SetViolatingRule( constraint.GetParentRule() );
241
242 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
243 }
244
245 if( constraint.Value().HasMax() && actualThickness > constraint.Value().Max() )
246 {
247 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TEXT_THICKNESS );
248 wxString msg = formatMsg( _( "(%s max thickness %s; actual %s)" ),
249 constraint.GetName(),
250 constraint.Value().Max(),
251 actualThickness );
252
253 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
254 drcItem->SetItems( item );
255 drcItem->SetViolatingRule( constraint.GetParentRule() );
256
257 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
258 }
259 }
260
261 return true;
262 };
263
264 static const std::vector<KICAD_T> itemTypes = { PCB_FIELD_T, PCB_TEXT_T, PCB_TEXTBOX_T };
265
267 [&]( BOARD_ITEM* item ) -> bool
268 {
269 ++count;
270 return true;
271 } );
272
274 [&]( BOARD_ITEM* item ) -> bool
275 {
276 if( !reportProgress( ii++, count, progressDelta ) )
277 return false;
278
279 EDA_TEXT* text = nullptr;
280 int strikes = 0;
281
282 switch( item->Type() )
283 {
284 case PCB_FIELD_T: text = static_cast<PCB_FIELD*>( item ); break;
285 case PCB_TEXT_T: text = static_cast<PCB_TEXT*>( item ); break;
286 case PCB_TEXTBOX_T: text = static_cast<PCB_TEXTBOX*>( item ); break;
287 default: UNIMPLEMENTED_FOR( item->GetClass() ); break;
288 }
289
290 if( !text || !text->IsVisible() )
291 return true;
292
294 strikes++;
295 else
296 checkTextThickness( item, text );
297
299 strikes++;
300 else
301 checkTextHeight( item, text );
302
303 if( strikes >= 2 )
304 return false;
305
306 return true;
307 } );
308
310
311 return !m_drcEngine->IsCancelled();
312}
313
314
315namespace detail
316{
318}
constexpr int ARC_LOW_DEF
Definition: base_units.h:119
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:237
wxString GetName() const
Definition: drc_rule.h:160
SEVERITY GetSeverity() const
Definition: drc_rule.h:173
MINOPTMAX< int > & Value()
Definition: drc_rule.h:153
DRC_RULE * GetParentRule() const
Definition: drc_rule.h:156
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
bool IsErrorLimitExceeded(int error_code)
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:696
bool IsCancelled() const
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:382
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual const wxString GetName() const override
virtual const wxString GetDescription() const override
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual)
virtual bool reportPhase(const wxString &aStageName)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, LSET aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer)
DRC_ENGINE * m_drcEngine
void reportAux(const wxString &aMsg)
virtual void reportRuleStatistics()
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:243
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:80
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition: font.cpp:146
virtual bool IsOutline() const
Definition: font.h:139
static LSET AllLayersMask()
Definition: lset.cpp:711
T Min() const
Definition: minoptmax.h:33
bool HasMax() const
Definition: minoptmax.h:38
bool HasMin() const
Definition: minoptmax.h:37
T Max() const
Definition: minoptmax.h:34
Represent a set of closed polygons.
double Area()
Return the area of this poly set.
void Inflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError, bool aSimplify=false)
Perform outline inflation/deflation.
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
void Simplify(POLYGON_MODE aFastMode)
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFastMo...
void Deflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError)
int OutlineCount() const
Return the number of outlines in the set.
SHAPE_POLY_SET CloneDropTriangulation() const
@ DRCE_TEXT_HEIGHT
Definition: drc_item.h:99
@ DRCE_TEXT_THICKNESS
Definition: drc_item.h:100
@ TEXT_THICKNESS_CONSTRAINT
Definition: drc_rule.h:58
@ TEXT_HEIGHT_CONSTRAINT
Definition: drc_rule.h:57
#define _(s)
This file contains miscellaneous commonly used macros and functions.
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
@ RPT_SEVERITY_IGNORE
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90