2021-07-17 19:56:18 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-03-10 18:08:20 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-07-17 19:56:18 +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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <macros.h>
|
|
|
|
#include <base_units.h>
|
2022-08-15 18:55:25 +00:00
|
|
|
#include <charconv>
|
2021-07-17 19:56:18 +00:00
|
|
|
#include <string_utils.h>
|
|
|
|
#include <render_settings.h>
|
|
|
|
#include <geometry/shape.h>
|
|
|
|
#include <geometry/shape_segment.h>
|
|
|
|
#include <geometry/shape_simple.h>
|
|
|
|
#include <geometry/geometry_utils.h>
|
|
|
|
#include <stroke_params.h>
|
|
|
|
#include <trigo.h>
|
2022-03-20 15:57:18 +00:00
|
|
|
#include <widgets/msgpanel.h>
|
2021-07-17 19:56:18 +00:00
|
|
|
|
|
|
|
using namespace STROKEPARAMS_T;
|
|
|
|
|
|
|
|
|
2023-11-25 13:05:45 +00:00
|
|
|
const std::map<LINE_STYLE, struct LINE_STYLE_DESC> lineTypeNames = {
|
|
|
|
{ LINE_STYLE::SOLID, { _( "Solid" ), BITMAPS::stroke_solid } },
|
|
|
|
{ LINE_STYLE::DASH, { _( "Dashed" ), BITMAPS::stroke_dash } },
|
|
|
|
{ LINE_STYLE::DOT, { _( "Dotted" ), BITMAPS::stroke_dot } },
|
|
|
|
{ LINE_STYLE::DASHDOT, { _( "Dash-Dot" ), BITMAPS::stroke_dashdot } },
|
|
|
|
{ LINE_STYLE::DASHDOTDOT, { _( "Dash-Dot-Dot" ), BITMAPS::stroke_dashdotdot } }
|
2023-09-20 00:03:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-11-25 13:05:45 +00:00
|
|
|
void STROKE_PARAMS::Stroke( const SHAPE* aShape, LINE_STYLE aLineStyle, int aWidth,
|
2021-07-17 19:56:18 +00:00
|
|
|
const KIGFX::RENDER_SETTINGS* aRenderSettings,
|
2022-01-01 18:08:03 +00:00
|
|
|
std::function<void( const VECTOR2I& a, const VECTOR2I& b )> aStroker )
|
2021-07-17 19:56:18 +00:00
|
|
|
{
|
2022-01-02 13:20:59 +00:00
|
|
|
double strokes[6] = { aWidth * 1.0, aWidth * 1.0, aWidth * 1.0, aWidth * 1.0, aWidth * 1.0,
|
|
|
|
aWidth * 1.0 };
|
|
|
|
int wrapAround = 6;
|
2021-07-17 19:56:18 +00:00
|
|
|
|
|
|
|
switch( aLineStyle )
|
|
|
|
{
|
2023-11-25 13:05:45 +00:00
|
|
|
case LINE_STYLE::DASH:
|
2021-07-17 19:56:18 +00:00
|
|
|
strokes[0] = aRenderSettings->GetDashLength( aWidth );
|
|
|
|
strokes[1] = aRenderSettings->GetGapLength( aWidth );
|
|
|
|
wrapAround = 2;
|
|
|
|
break;
|
2023-11-25 13:05:45 +00:00
|
|
|
case LINE_STYLE::DOT:
|
2021-07-17 19:56:18 +00:00
|
|
|
strokes[0] = aRenderSettings->GetDotLength( aWidth );
|
|
|
|
strokes[1] = aRenderSettings->GetGapLength( aWidth );
|
|
|
|
wrapAround = 2;
|
|
|
|
break;
|
2023-11-25 13:05:45 +00:00
|
|
|
case LINE_STYLE::DASHDOT:
|
2021-07-17 19:56:18 +00:00
|
|
|
strokes[0] = aRenderSettings->GetDashLength( aWidth );
|
|
|
|
strokes[1] = aRenderSettings->GetGapLength( aWidth );
|
|
|
|
strokes[2] = aRenderSettings->GetDotLength( aWidth );
|
|
|
|
strokes[3] = aRenderSettings->GetGapLength( aWidth );
|
|
|
|
wrapAround = 4;
|
|
|
|
break;
|
2023-11-25 13:05:45 +00:00
|
|
|
case LINE_STYLE::DASHDOTDOT:
|
2021-07-22 23:05:01 +00:00
|
|
|
strokes[0] = aRenderSettings->GetDashLength( aWidth );
|
|
|
|
strokes[1] = aRenderSettings->GetGapLength( aWidth );
|
|
|
|
strokes[2] = aRenderSettings->GetDotLength( aWidth );
|
|
|
|
strokes[3] = aRenderSettings->GetGapLength( aWidth );
|
|
|
|
strokes[4] = aRenderSettings->GetDotLength( aWidth );
|
|
|
|
strokes[5] = aRenderSettings->GetGapLength( aWidth );
|
|
|
|
wrapAround = 6;
|
|
|
|
break;
|
2021-07-17 19:56:18 +00:00
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_FOR( lineTypeNames.at( aLineStyle ).name );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( aShape->Type() )
|
|
|
|
{
|
|
|
|
case SH_SIMPLE:
|
|
|
|
{
|
|
|
|
const SHAPE_SIMPLE* poly = static_cast<const SHAPE_SIMPLE*>( aShape );
|
|
|
|
|
|
|
|
for( size_t ii = 0; ii < poly->GetSegmentCount(); ++ii )
|
|
|
|
{
|
|
|
|
SEG seg = poly->GetSegment( ii );
|
|
|
|
SHAPE_SEGMENT line( seg.A, seg.B );
|
|
|
|
STROKE_PARAMS::Stroke( &line, aLineStyle, aWidth, aRenderSettings, aStroker );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SH_SEGMENT:
|
|
|
|
{
|
|
|
|
const SHAPE_SEGMENT* line = static_cast<const SHAPE_SEGMENT*>( aShape );
|
|
|
|
|
|
|
|
VECTOR2D start = line->GetSeg().A;
|
|
|
|
VECTOR2D end = line->GetSeg().B;
|
2022-08-31 16:17:14 +00:00
|
|
|
BOX2I clip( start, VECTOR2I( end.x - start.x, end.y - start.y ) );
|
2021-07-17 19:56:18 +00:00
|
|
|
clip.Normalize();
|
|
|
|
|
|
|
|
double theta = atan2( end.y - start.y, end.x - start.x );
|
|
|
|
|
|
|
|
for( size_t i = 0; i < 10000; ++i )
|
|
|
|
{
|
|
|
|
// Calculations MUST be done in doubles to keep from accumulating rounding
|
|
|
|
// errors as we go.
|
|
|
|
VECTOR2D next( start.x + strokes[ i % wrapAround ] * cos( theta ),
|
|
|
|
start.y + strokes[ i % wrapAround ] * sin( theta ) );
|
|
|
|
|
|
|
|
// Drawing each segment can be done rounded to ints.
|
2022-01-01 18:08:03 +00:00
|
|
|
VECTOR2I a( KiROUND( start.x ), KiROUND( start.y ) );
|
|
|
|
VECTOR2I b( KiROUND( next.x ), KiROUND( next.y ) );
|
2021-07-17 19:56:18 +00:00
|
|
|
|
|
|
|
if( ClipLine( &clip, a.x, a.y, b.x, b.y ) )
|
|
|
|
break;
|
|
|
|
else if( i % 2 == 0 )
|
|
|
|
aStroker( a, b );
|
|
|
|
|
|
|
|
start = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SH_ARC:
|
|
|
|
{
|
|
|
|
const SHAPE_ARC* arc = static_cast<const SHAPE_ARC*>( aShape );
|
|
|
|
|
2022-01-16 16:15:07 +00:00
|
|
|
double r = arc->GetRadius();
|
|
|
|
double C = 2.0 * M_PI * r;
|
|
|
|
VECTOR2I center = arc->GetCenter();
|
|
|
|
VECTOR2D startRadial( arc->GetP0() - center );
|
|
|
|
EDA_ANGLE startAngle( startRadial );
|
|
|
|
VECTOR2D endRadial( arc->GetP1() - center );
|
|
|
|
EDA_ANGLE arcEndAngle( endRadial );
|
2021-07-17 19:56:18 +00:00
|
|
|
|
|
|
|
if( arcEndAngle == startAngle )
|
2022-01-16 16:15:07 +00:00
|
|
|
arcEndAngle = startAngle + ANGLE_360; // ring, not null
|
2021-07-17 19:56:18 +00:00
|
|
|
|
|
|
|
if( startAngle > arcEndAngle )
|
|
|
|
{
|
2022-01-16 16:15:07 +00:00
|
|
|
if( arcEndAngle < ANGLE_0 )
|
|
|
|
arcEndAngle = arcEndAngle.Normalize();
|
2021-07-17 19:56:18 +00:00
|
|
|
else
|
2022-01-16 16:15:07 +00:00
|
|
|
startAngle = startAngle.Normalize() - ANGLE_360;
|
2021-07-17 19:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( startAngle < arcEndAngle );
|
|
|
|
|
|
|
|
for( size_t i = 0; i < 10000 && startAngle < arcEndAngle; ++i )
|
|
|
|
{
|
2022-01-16 16:15:07 +00:00
|
|
|
EDA_ANGLE theta = ANGLE_360 * strokes[ i % wrapAround ] / C;
|
|
|
|
EDA_ANGLE endAngle = std::min( startAngle + theta, arcEndAngle );
|
2021-07-17 19:56:18 +00:00
|
|
|
|
|
|
|
if( i % 2 == 0 )
|
|
|
|
{
|
2022-01-16 21:15:20 +00:00
|
|
|
VECTOR2I a( center.x + r * startAngle.Cos(), center.y + r * startAngle.Sin() );
|
|
|
|
VECTOR2I b( center.x + r * endAngle.Cos(), center.y + r * endAngle.Sin() );
|
2021-07-17 19:56:18 +00:00
|
|
|
|
|
|
|
aStroker( a, b );
|
|
|
|
}
|
|
|
|
|
|
|
|
startAngle = endAngle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SH_CIRCLE:
|
|
|
|
// A circle is always filled; a ring is represented by a 360° arc.
|
|
|
|
KI_FALLTHROUGH;
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_FOR( SHAPE_TYPE_asString( aShape->Type() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-25 13:05:45 +00:00
|
|
|
wxString STROKE_PARAMS::GetLineStyleToken( LINE_STYLE aStyle )
|
2021-07-17 19:56:18 +00:00
|
|
|
{
|
|
|
|
wxString token;
|
|
|
|
|
|
|
|
switch( aStyle )
|
|
|
|
{
|
2023-11-25 13:05:45 +00:00
|
|
|
case LINE_STYLE::DASH: token = wxT( "dash" ); break;
|
|
|
|
case LINE_STYLE::DOT: token = wxT( "dot" ); break;
|
|
|
|
case LINE_STYLE::DASHDOT: token = wxT( "dash_dot" ); break;
|
|
|
|
case LINE_STYLE::DASHDOTDOT: token = wxT( "dash_dot_dot" ); break;
|
|
|
|
case LINE_STYLE::SOLID: token = wxT( "solid" ); break;
|
|
|
|
case LINE_STYLE::DEFAULT: token = wxT( "default" ); break;
|
2021-07-17 19:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-19 16:09:59 +00:00
|
|
|
void STROKE_PARAMS::GetMsgPanelInfo( UNITS_PROVIDER* aUnitsProvider,
|
2022-09-16 04:38:10 +00:00
|
|
|
std::vector<MSG_PANEL_ITEM>& aList,
|
2022-03-20 15:57:18 +00:00
|
|
|
bool aIncludeStyle, bool aIncludeWidth )
|
|
|
|
{
|
|
|
|
if( aIncludeStyle )
|
|
|
|
{
|
2023-11-25 12:38:24 +00:00
|
|
|
wxString msg = _( "Default" );
|
2022-03-20 15:57:18 +00:00
|
|
|
|
2023-11-25 12:38:24 +00:00
|
|
|
for( const auto& [ lineStyle, lineStyleDesc ] : lineTypeNames )
|
2022-03-20 15:57:18 +00:00
|
|
|
{
|
2023-11-25 12:38:24 +00:00
|
|
|
if( lineStyle == GetLineStyle() )
|
2022-03-20 15:57:18 +00:00
|
|
|
{
|
2023-11-25 12:38:24 +00:00
|
|
|
msg = lineStyleDesc.name;
|
2022-03-20 15:57:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 12:38:24 +00:00
|
|
|
aList.emplace_back( _( "Line Style" ), msg );
|
2022-03-20 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( aIncludeWidth )
|
2022-09-19 16:09:59 +00:00
|
|
|
aList.emplace_back( _( "Line Width" ), aUnitsProvider->MessageTextFromValue( GetWidth() ) );
|
2022-03-20 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-16 04:38:10 +00:00
|
|
|
void STROKE_PARAMS::Format( OUTPUTFORMATTER* aFormatter, const EDA_IU_SCALE& aIuScale,
|
|
|
|
int aNestLevel ) const
|
2021-07-17 19:56:18 +00:00
|
|
|
{
|
|
|
|
wxASSERT( aFormatter != nullptr );
|
|
|
|
|
2021-07-22 23:05:01 +00:00
|
|
|
if( GetColor() == KIGFX::COLOR4D::UNSPECIFIED )
|
|
|
|
{
|
|
|
|
aFormatter->Print( aNestLevel, "(stroke (width %s) (type %s))",
|
2022-09-16 04:38:10 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( aIuScale, GetWidth() ).c_str(),
|
2023-11-25 13:05:45 +00:00
|
|
|
TO_UTF8( GetLineStyleToken( GetLineStyle() ) ) );
|
2021-07-22 23:05:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aFormatter->Print( aNestLevel, "(stroke (width %s) (type %s) (color %d %d %d %s))",
|
2022-09-16 04:38:10 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( aIuScale, GetWidth() ).c_str(),
|
2023-11-25 13:05:45 +00:00
|
|
|
TO_UTF8( GetLineStyleToken( GetLineStyle() ) ),
|
2021-07-22 23:05:01 +00:00
|
|
|
KiROUND( GetColor().r * 255.0 ),
|
|
|
|
KiROUND( GetColor().g * 255.0 ),
|
|
|
|
KiROUND( GetColor().b * 255.0 ),
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( GetColor().a ).c_str() );
|
2021-07-22 23:05:01 +00:00
|
|
|
}
|
2021-07-17 19:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void STROKE_PARAMS_PARSER::ParseStroke( STROKE_PARAMS& aStroke )
|
|
|
|
{
|
|
|
|
for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
|
|
|
|
{
|
|
|
|
if( token != T_LEFT )
|
|
|
|
Expecting( T_LEFT );
|
|
|
|
|
|
|
|
token = NextTok();
|
|
|
|
|
|
|
|
switch( token )
|
|
|
|
{
|
|
|
|
case T_width:
|
|
|
|
aStroke.SetWidth( parseDouble( "stroke width" ) * m_iuPerMM );
|
|
|
|
NeedRIGHT();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_type:
|
|
|
|
{
|
|
|
|
token = NextTok();
|
|
|
|
|
|
|
|
switch( token )
|
|
|
|
{
|
2023-11-25 13:05:45 +00:00
|
|
|
case T_dash: aStroke.SetLineStyle( LINE_STYLE::DASH ); break;
|
|
|
|
case T_dot: aStroke.SetLineStyle( LINE_STYLE::DOT ); break;
|
|
|
|
case T_dash_dot: aStroke.SetLineStyle( LINE_STYLE::DASHDOT ); break;
|
|
|
|
case T_dash_dot_dot: aStroke.SetLineStyle( LINE_STYLE::DASHDOTDOT ); break;
|
|
|
|
case T_solid: aStroke.SetLineStyle( LINE_STYLE::SOLID ); break;
|
|
|
|
case T_default: aStroke.SetLineStyle( LINE_STYLE::DEFAULT ); break;
|
2021-07-17 19:56:18 +00:00
|
|
|
default:
|
2021-07-22 23:05:01 +00:00
|
|
|
Expecting( "solid, dash, dash_dot, dash_dot_dot, dot or default" );
|
2021-07-17 19:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NeedRIGHT();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case T_color:
|
|
|
|
{
|
|
|
|
KIGFX::COLOR4D color;
|
|
|
|
|
|
|
|
color.r = parseInt( "red" ) / 255.0;
|
|
|
|
color.g = parseInt( "green" ) / 255.0;
|
|
|
|
color.b = parseInt( "blue" ) / 255.0;
|
|
|
|
color.a = Clamp( parseDouble( "alpha" ), 0.0, 1.0 );
|
|
|
|
|
|
|
|
aStroke.SetColor( color );
|
|
|
|
NeedRIGHT();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
Expecting( "width, type, or color" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int STROKE_PARAMS_PARSER::parseInt( const char* aText )
|
|
|
|
{
|
|
|
|
T token = NextTok();
|
|
|
|
|
|
|
|
if( token != T_NUMBER )
|
|
|
|
Expecting( aText );
|
|
|
|
|
|
|
|
return atoi( CurText() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double STROKE_PARAMS_PARSER::parseDouble( const char* aText )
|
|
|
|
{
|
|
|
|
T token = NextTok();
|
|
|
|
|
|
|
|
if( token != T_NUMBER )
|
|
|
|
Expecting( aText );
|
|
|
|
|
2022-08-14 22:40:54 +00:00
|
|
|
return DSNLEXER::parseDouble();
|
2021-07-17 19:56:18 +00:00
|
|
|
}
|
2022-03-20 15:57:18 +00:00
|
|
|
|
|
|
|
|