2021-07-11 15:12:28 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2023-05-05 13:21:56 +00:00
|
|
|
* Copyright (C) 2021-2023 KiCad Developers.
|
2021-07-11 15:12:28 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2022-01-02 13:20:59 +00:00
|
|
|
#include <macros.h>
|
2023-06-06 15:09:34 +00:00
|
|
|
#include <pcb_field.h>
|
2021-07-11 15:12:28 +00:00
|
|
|
#include <pcb_text.h>
|
2022-01-30 10:52:52 +00:00
|
|
|
#include <pcb_textbox.h>
|
2021-07-11 15:12:28 +00:00
|
|
|
#include <drc/drc_engine.h>
|
|
|
|
#include <drc/drc_item.h>
|
|
|
|
#include <drc/drc_rule.h>
|
|
|
|
#include <drc/drc_test_provider.h>
|
2022-02-01 11:19:09 +00:00
|
|
|
#include <font/font.h>
|
2021-07-11 15:12:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Text dimensions tests.
|
|
|
|
Errors generated:
|
|
|
|
- DRCE_TEXT_HEIGHT
|
|
|
|
- DRCE_TEXT_THICKNESS
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DRC_TEST_PROVIDER_TEXT_DIMS : public DRC_TEST_PROVIDER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DRC_TEST_PROVIDER_TEXT_DIMS()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~DRC_TEST_PROVIDER_TEXT_DIMS()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Run() override;
|
|
|
|
|
|
|
|
virtual const wxString GetName() const override
|
|
|
|
{
|
2022-03-11 21:16:52 +00:00
|
|
|
return wxT( "text_dimensions" );
|
2021-07-11 15:12:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual const wxString GetDescription() const override
|
|
|
|
{
|
2022-03-11 21:16:52 +00:00
|
|
|
return wxT( "Tests text height and thickness" );
|
2021-07-11 15:12:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool DRC_TEST_PROVIDER_TEXT_DIMS::Run()
|
|
|
|
{
|
2022-08-03 09:10:23 +00:00
|
|
|
const int progressDelta = 250;
|
2021-07-11 15:12:28 +00:00
|
|
|
int count = 0;
|
|
|
|
int ii = 0;
|
|
|
|
|
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_TEXT_HEIGHT )
|
|
|
|
&& m_drcEngine->IsErrorLimitExceeded( DRCE_TEXT_THICKNESS ) )
|
|
|
|
{
|
2022-03-11 21:16:52 +00:00
|
|
|
reportAux( wxT( "Text dimension violations ignored. Tests not run." ) );
|
2021-07-11 15:12:28 +00:00
|
|
|
return true; // continue with other tests
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !m_drcEngine->HasRulesForConstraintType( TEXT_HEIGHT_CONSTRAINT )
|
|
|
|
&& !m_drcEngine->HasRulesForConstraintType( TEXT_THICKNESS_CONSTRAINT ) )
|
|
|
|
{
|
2022-03-11 21:16:52 +00:00
|
|
|
reportAux( wxT( "No text height or text thickness constraints found. Tests not run." ) );
|
2021-07-11 15:12:28 +00:00
|
|
|
return true; // continue with other tests
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !reportPhase( _( "Checking text dimensions..." ) ) )
|
|
|
|
return false; // DRC cancelled
|
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
auto checkTextHeight =
|
|
|
|
[&]( BOARD_ITEM* item, EDA_TEXT* text ) -> bool
|
2021-07-11 15:12:28 +00:00
|
|
|
{
|
2022-02-08 14:02:55 +00:00
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_TEXT_HEIGHT ) )
|
2021-07-11 15:12:28 +00:00
|
|
|
return false;
|
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
DRC_CONSTRAINT constraint = m_drcEngine->EvalRules( TEXT_HEIGHT_CONSTRAINT, item,
|
|
|
|
nullptr, item->GetLayer() );
|
2021-07-11 15:12:28 +00:00
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
|
|
|
|
return true;
|
2022-01-30 10:52:52 +00:00
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
int actualHeight = text->GetTextSize().y;
|
|
|
|
|
|
|
|
if( constraint.Value().HasMin() && actualHeight < constraint.Value().Min() )
|
2022-01-02 13:20:59 +00:00
|
|
|
{
|
2022-02-08 14:02:55 +00:00
|
|
|
std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TEXT_HEIGHT );
|
2022-10-06 20:52:17 +00:00
|
|
|
wxString msg = formatMsg( _( "(%s min height %s; actual %s)" ),
|
|
|
|
constraint.GetName(),
|
|
|
|
constraint.Value().Min(),
|
|
|
|
actualHeight );
|
2022-02-08 14:02:55 +00:00
|
|
|
|
2022-06-15 23:42:34 +00:00
|
|
|
drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
|
2022-02-08 14:02:55 +00:00
|
|
|
drcItem->SetItems( item );
|
|
|
|
drcItem->SetViolatingRule( constraint.GetParentRule() );
|
|
|
|
|
|
|
|
reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
|
2022-01-02 13:20:59 +00:00
|
|
|
}
|
2022-01-01 21:35:29 +00:00
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
if( constraint.Value().HasMax() && actualHeight > constraint.Value().Max() )
|
|
|
|
{
|
|
|
|
std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TEXT_HEIGHT );
|
2022-10-06 20:52:17 +00:00
|
|
|
wxString msg = formatMsg( _( "(%s max height %s; actual %s)" ),
|
|
|
|
constraint.GetName(),
|
|
|
|
constraint.Value().Max(),
|
|
|
|
actualHeight );
|
2022-02-08 14:02:55 +00:00
|
|
|
|
2022-06-15 23:42:34 +00:00
|
|
|
drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
|
2022-02-08 14:02:55 +00:00
|
|
|
drcItem->SetItems( item );
|
|
|
|
drcItem->SetViolatingRule( constraint.GetParentRule() );
|
|
|
|
|
|
|
|
reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto checkTextThickness =
|
|
|
|
[&]( BOARD_ITEM* item, EDA_TEXT* text ) -> bool
|
|
|
|
{
|
|
|
|
DRC_CONSTRAINT constraint = m_drcEngine->EvalRules( TEXT_THICKNESS_CONSTRAINT, item,
|
|
|
|
nullptr, item->GetLayer() );
|
|
|
|
|
|
|
|
if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
|
2021-07-11 15:12:28 +00:00
|
|
|
return true;
|
|
|
|
|
2022-10-22 20:32:10 +00:00
|
|
|
KIFONT::FONT* font = text->GetFont();
|
|
|
|
|
|
|
|
if( !font )
|
|
|
|
font = KIFONT::FONT::GetFont( wxEmptyString, text->IsBold(), text->IsItalic() );
|
2022-02-01 11:19:09 +00:00
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
if( font->IsOutline() )
|
2021-07-11 15:12:28 +00:00
|
|
|
{
|
2024-01-17 00:23:35 +00:00
|
|
|
if( !constraint.Value().HasMin() || constraint.Value().Min() <= 0 )
|
2022-02-08 14:02:55 +00:00
|
|
|
return true;
|
2021-07-11 15:12:28 +00:00
|
|
|
|
2023-05-05 13:21:56 +00:00
|
|
|
auto* glyphs = text->GetRenderCache( font, text->GetShownText( true ) );
|
2022-02-08 14:02:55 +00:00
|
|
|
bool collapsedStroke = false;
|
|
|
|
bool collapsedArea = false;
|
|
|
|
|
|
|
|
for( const std::unique_ptr<KIFONT::GLYPH>& glyph : *glyphs )
|
2021-07-11 15:12:28 +00:00
|
|
|
{
|
2024-02-05 15:45:45 +00:00
|
|
|
// Ensure the glyph is a OUTLINE_GLYPH (for instance, overbars in outline
|
|
|
|
// font text are represented as STROKE_GLYPHs).
|
2023-09-09 08:25:22 +00:00
|
|
|
if( !glyph->IsOutline() )
|
|
|
|
continue;
|
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
auto outlineGlyph = static_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() );
|
|
|
|
int outlineCount = outlineGlyph->OutlineCount();
|
|
|
|
int holeCount = 0;
|
|
|
|
|
|
|
|
if( outlineCount == 0 )
|
|
|
|
continue; // ignore spaces
|
|
|
|
|
2022-02-08 14:26:12 +00:00
|
|
|
for( ii = 0; ii < outlineCount; ++ii )
|
2022-02-08 14:02:55 +00:00
|
|
|
holeCount += outlineGlyph->HoleCount( ii );
|
|
|
|
|
2022-03-14 15:52:12 +00:00
|
|
|
SHAPE_POLY_SET poly = outlineGlyph->CloneDropTriangulation();
|
2023-05-29 15:22:06 +00:00
|
|
|
poly.Deflate( constraint.Value().Min() / 2,
|
2023-10-05 07:34:24 +00:00
|
|
|
CORNER_STRATEGY::CHAMFER_ALL_CORNERS, ARC_LOW_DEF );
|
2022-02-08 14:02:55 +00:00
|
|
|
poly.Simplify( SHAPE_POLY_SET::PM_FAST );
|
|
|
|
|
|
|
|
int resultingOutlineCount = poly.OutlineCount();
|
|
|
|
int resultingHoleCount = 0;
|
|
|
|
|
2022-02-08 14:26:12 +00:00
|
|
|
for( ii = 0; ii < resultingOutlineCount; ++ii )
|
2022-02-08 14:02:55 +00:00
|
|
|
resultingHoleCount += poly.HoleCount( ii );
|
|
|
|
|
|
|
|
if( ( resultingOutlineCount != outlineCount )
|
|
|
|
|| ( resultingHoleCount != holeCount ) )
|
2021-09-05 15:06:12 +00:00
|
|
|
{
|
2022-02-08 14:02:55 +00:00
|
|
|
collapsedStroke = true;
|
|
|
|
break;
|
2021-09-05 15:06:12 +00:00
|
|
|
}
|
2021-07-11 15:12:28 +00:00
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
double glyphArea = outlineGlyph->Area();
|
|
|
|
|
|
|
|
if( glyphArea == 0 )
|
|
|
|
continue;
|
|
|
|
|
2023-05-29 14:28:48 +00:00
|
|
|
poly.Inflate( constraint.Value().Min() / 2,
|
2023-10-05 07:34:24 +00:00
|
|
|
CORNER_STRATEGY::CHAMFER_ALL_CORNERS, ARC_LOW_DEF, true );
|
2023-05-29 14:28:48 +00:00
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
double resultingGlyphArea = poly.Area();
|
|
|
|
|
|
|
|
if( ( std::abs( resultingGlyphArea - glyphArea ) / glyphArea ) > 0.1 )
|
2021-09-05 15:06:12 +00:00
|
|
|
{
|
2022-02-08 14:02:55 +00:00
|
|
|
collapsedArea = true;
|
|
|
|
break;
|
2021-09-05 15:06:12 +00:00
|
|
|
}
|
2021-07-11 15:12:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
if( collapsedStroke || collapsedArea )
|
2021-07-11 15:12:28 +00:00
|
|
|
{
|
2022-02-08 14:02:55 +00:00
|
|
|
auto drcItem = DRC_ITEM::Create( DRCE_TEXT_THICKNESS );
|
2022-06-15 23:42:34 +00:00
|
|
|
wxString msg;
|
2021-07-11 15:12:28 +00:00
|
|
|
|
2022-06-15 23:42:34 +00:00
|
|
|
msg = _( "(TrueType font characters with insufficient stroke weight)" );
|
2021-07-11 15:12:28 +00:00
|
|
|
|
2022-06-15 23:42:34 +00:00
|
|
|
drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
|
2021-07-11 15:12:28 +00:00
|
|
|
drcItem->SetItems( item );
|
|
|
|
drcItem->SetViolatingRule( constraint.GetParentRule() );
|
|
|
|
|
2021-12-26 13:47:00 +00:00
|
|
|
reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
|
2021-07-11 15:12:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-08 14:02:55 +00:00
|
|
|
else
|
2021-07-11 15:12:28 +00:00
|
|
|
{
|
2022-06-03 21:30:58 +00:00
|
|
|
int actualThickness = text->GetEffectiveTextPenWidth();
|
2022-02-08 14:02:55 +00:00
|
|
|
|
|
|
|
if( constraint.Value().HasMin() && actualThickness < constraint.Value().Min() )
|
2022-02-07 20:56:19 +00:00
|
|
|
{
|
|
|
|
std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TEXT_THICKNESS );
|
2022-10-06 20:52:17 +00:00
|
|
|
wxString msg = formatMsg( _( "(%s min thickness %s; actual %s)" ),
|
|
|
|
constraint.GetName(),
|
|
|
|
constraint.Value().Min(),
|
|
|
|
actualThickness );
|
2022-02-07 20:56:19 +00:00
|
|
|
|
2022-06-15 23:42:34 +00:00
|
|
|
drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
|
2022-02-07 20:56:19 +00:00
|
|
|
drcItem->SetItems( item );
|
|
|
|
drcItem->SetViolatingRule( constraint.GetParentRule() );
|
|
|
|
|
|
|
|
reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
|
2021-07-11 15:12:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 14:02:55 +00:00
|
|
|
if( constraint.Value().HasMax() && actualThickness > constraint.Value().Max() )
|
2021-07-11 15:12:28 +00:00
|
|
|
{
|
|
|
|
std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TEXT_THICKNESS );
|
2022-10-06 20:52:17 +00:00
|
|
|
wxString msg = formatMsg( _( "(%s max thickness %s; actual %s)" ),
|
|
|
|
constraint.GetName(),
|
|
|
|
constraint.Value().Max(),
|
|
|
|
actualThickness );
|
2021-07-11 15:12:28 +00:00
|
|
|
|
2022-06-15 23:42:34 +00:00
|
|
|
drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
|
2021-07-11 15:12:28 +00:00
|
|
|
drcItem->SetItems( item );
|
|
|
|
drcItem->SetViolatingRule( constraint.GetParentRule() );
|
|
|
|
|
2021-12-26 13:47:00 +00:00
|
|
|
reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
|
2021-07-11 15:12:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2023-06-06 15:09:34 +00:00
|
|
|
static const std::vector<KICAD_T> itemTypes = { PCB_FIELD_T, PCB_TEXT_T, PCB_TEXTBOX_T };
|
2022-03-09 00:47:37 +00:00
|
|
|
|
|
|
|
forEachGeometryItem( itemTypes, LSET::AllLayersMask(),
|
|
|
|
[&]( BOARD_ITEM* item ) -> bool
|
|
|
|
{
|
|
|
|
++count;
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
|
|
|
|
forEachGeometryItem( itemTypes, LSET::AllLayersMask(),
|
2022-02-08 14:02:55 +00:00
|
|
|
[&]( BOARD_ITEM* item ) -> bool
|
|
|
|
{
|
2022-08-03 09:10:23 +00:00
|
|
|
if( !reportProgress( ii++, count, progressDelta ) )
|
2022-02-08 14:02:55 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
EDA_TEXT* text = nullptr;
|
|
|
|
int strikes = 0;
|
|
|
|
|
|
|
|
switch( item->Type() )
|
|
|
|
{
|
2023-06-06 15:09:34 +00:00
|
|
|
case PCB_FIELD_T: text = static_cast<PCB_FIELD*>( item ); break;
|
2023-03-30 11:49:23 +00:00
|
|
|
case PCB_TEXT_T: text = static_cast<PCB_TEXT*>( item ); break;
|
|
|
|
case PCB_TEXTBOX_T: text = static_cast<PCB_TEXTBOX*>( item ); break;
|
|
|
|
default: UNIMPLEMENTED_FOR( item->GetClass() ); break;
|
2022-02-08 14:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !text || !text->IsVisible() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_TEXT_THICKNESS ) )
|
|
|
|
strikes++;
|
|
|
|
else
|
|
|
|
checkTextThickness( item, text );
|
|
|
|
|
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_TEXT_HEIGHT ) )
|
|
|
|
strikes++;
|
|
|
|
else
|
|
|
|
checkTextHeight( item, text );
|
|
|
|
|
|
|
|
if( strikes >= 2 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2022-03-09 00:47:37 +00:00
|
|
|
} );
|
2021-07-11 15:12:28 +00:00
|
|
|
|
|
|
|
reportRuleStatistics();
|
|
|
|
|
2022-03-11 20:13:47 +00:00
|
|
|
return !m_drcEngine->IsCancelled();
|
2021-07-11 15:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
static DRC_REGISTER_TEST_PROVIDER<DRC_TEST_PROVIDER_TEXT_DIMS> dummy;
|
|
|
|
}
|