KiCad PCB EDA Suite
Loading...
Searching...
No Matches
stroke_params.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-2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#include <macros.h>
20#include <base_units.h>
21#include <charconv>
22#include <string_utils.h>
23#include <render_settings.h>
24#include <geometry/shape.h>
28#include <stroke_params.h>
29#include <trigo.h>
30#include <widgets/msgpanel.h>
31
32using namespace STROKEPARAMS_T;
33
34
35const std::map<PLOT_DASH_TYPE, struct lineTypeStruct> lineTypeNames = {
37 { PLOT_DASH_TYPE::DASH, { _( "Dashed" ), BITMAPS::stroke_dash } },
38 { PLOT_DASH_TYPE::DOT, { _( "Dotted" ), BITMAPS::stroke_dot } },
41};
42
43
44void STROKE_PARAMS::Stroke( const SHAPE* aShape, PLOT_DASH_TYPE aLineStyle, int aWidth,
45 const KIGFX::RENDER_SETTINGS* aRenderSettings,
46 std::function<void( const VECTOR2I& a, const VECTOR2I& b )> aStroker )
47{
48 double strokes[6] = { aWidth * 1.0, aWidth * 1.0, aWidth * 1.0, aWidth * 1.0, aWidth * 1.0,
49 aWidth * 1.0 };
50 int wrapAround = 6;
51
52 switch( aLineStyle )
53 {
54 case PLOT_DASH_TYPE::DASH:
55 strokes[0] = aRenderSettings->GetDashLength( aWidth );
56 strokes[1] = aRenderSettings->GetGapLength( aWidth );
57 wrapAround = 2;
58 break;
59 case PLOT_DASH_TYPE::DOT:
60 strokes[0] = aRenderSettings->GetDotLength( aWidth );
61 strokes[1] = aRenderSettings->GetGapLength( aWidth );
62 wrapAround = 2;
63 break;
64 case PLOT_DASH_TYPE::DASHDOT:
65 strokes[0] = aRenderSettings->GetDashLength( aWidth );
66 strokes[1] = aRenderSettings->GetGapLength( aWidth );
67 strokes[2] = aRenderSettings->GetDotLength( aWidth );
68 strokes[3] = aRenderSettings->GetGapLength( aWidth );
69 wrapAround = 4;
70 break;
71 case PLOT_DASH_TYPE::DASHDOTDOT:
72 strokes[0] = aRenderSettings->GetDashLength( aWidth );
73 strokes[1] = aRenderSettings->GetGapLength( aWidth );
74 strokes[2] = aRenderSettings->GetDotLength( aWidth );
75 strokes[3] = aRenderSettings->GetGapLength( aWidth );
76 strokes[4] = aRenderSettings->GetDotLength( aWidth );
77 strokes[5] = aRenderSettings->GetGapLength( aWidth );
78 wrapAround = 6;
79 break;
80 default:
81 UNIMPLEMENTED_FOR( lineTypeNames.at( aLineStyle ).name );
82 }
83
84 switch( aShape->Type() )
85 {
86 case SH_SIMPLE:
87 {
88 const SHAPE_SIMPLE* poly = static_cast<const SHAPE_SIMPLE*>( aShape );
89
90 for( size_t ii = 0; ii < poly->GetSegmentCount(); ++ii )
91 {
92 SEG seg = poly->GetSegment( ii );
93 SHAPE_SEGMENT line( seg.A, seg.B );
94 STROKE_PARAMS::Stroke( &line, aLineStyle, aWidth, aRenderSettings, aStroker );
95 }
96 }
97 break;
98
99 case SH_SEGMENT:
100 {
101 const SHAPE_SEGMENT* line = static_cast<const SHAPE_SEGMENT*>( aShape );
102
103 VECTOR2D start = line->GetSeg().A;
104 VECTOR2D end = line->GetSeg().B;
105 BOX2I clip( start, VECTOR2I( end.x - start.x, end.y - start.y ) );
106 clip.Normalize();
107
108 double theta = atan2( end.y - start.y, end.x - start.x );
109
110 for( size_t i = 0; i < 10000; ++i )
111 {
112 // Calculations MUST be done in doubles to keep from accumulating rounding
113 // errors as we go.
114 VECTOR2D next( start.x + strokes[ i % wrapAround ] * cos( theta ),
115 start.y + strokes[ i % wrapAround ] * sin( theta ) );
116
117 // Drawing each segment can be done rounded to ints.
118 VECTOR2I a( KiROUND( start.x ), KiROUND( start.y ) );
119 VECTOR2I b( KiROUND( next.x ), KiROUND( next.y ) );
120
121 if( ClipLine( &clip, a.x, a.y, b.x, b.y ) )
122 break;
123 else if( i % 2 == 0 )
124 aStroker( a, b );
125
126 start = next;
127 }
128 }
129 break;
130
131 case SH_ARC:
132 {
133 const SHAPE_ARC* arc = static_cast<const SHAPE_ARC*>( aShape );
134
135 double r = arc->GetRadius();
136 double C = 2.0 * M_PI * r;
137 VECTOR2I center = arc->GetCenter();
138 VECTOR2D startRadial( arc->GetP0() - center );
139 EDA_ANGLE startAngle( startRadial );
140 VECTOR2D endRadial( arc->GetP1() - center );
141 EDA_ANGLE arcEndAngle( endRadial );
142
143 if( arcEndAngle == startAngle )
144 arcEndAngle = startAngle + ANGLE_360; // ring, not null
145
146 if( startAngle > arcEndAngle )
147 {
148 if( arcEndAngle < ANGLE_0 )
149 arcEndAngle = arcEndAngle.Normalize();
150 else
151 startAngle = startAngle.Normalize() - ANGLE_360;
152 }
153
154 wxASSERT( startAngle < arcEndAngle );
155
156 for( size_t i = 0; i < 10000 && startAngle < arcEndAngle; ++i )
157 {
158 EDA_ANGLE theta = ANGLE_360 * strokes[ i % wrapAround ] / C;
159 EDA_ANGLE endAngle = std::min( startAngle + theta, arcEndAngle );
160
161 if( i % 2 == 0 )
162 {
163 VECTOR2I a( center.x + r * startAngle.Cos(), center.y + r * startAngle.Sin() );
164 VECTOR2I b( center.x + r * endAngle.Cos(), center.y + r * endAngle.Sin() );
165
166 aStroker( a, b );
167 }
168
169 startAngle = endAngle;
170 }
171 }
172 break;
173
174 case SH_CIRCLE:
175 // A circle is always filled; a ring is represented by a 360° arc.
177
178 default:
180 }
181}
182
183
185{
186 wxString token;
187
188 switch( aStyle )
189 {
190 case PLOT_DASH_TYPE::DASH: token = wxT( "dash" ); break;
191 case PLOT_DASH_TYPE::DOT: token = wxT( "dot" ); break;
192 case PLOT_DASH_TYPE::DASHDOT: token = wxT( "dash_dot" ); break;
193 case PLOT_DASH_TYPE::DASHDOTDOT: token = wxT( "dash_dot_dot" ); break;
194 case PLOT_DASH_TYPE::SOLID: token = wxT( "solid" ); break;
195 case PLOT_DASH_TYPE::DEFAULT: token = wxT( "default" ); break;
196 }
197
198 return token;
199}
200
201
203 std::vector<MSG_PANEL_ITEM>& aList,
204 bool aIncludeStyle, bool aIncludeWidth )
205{
206 if( aIncludeStyle )
207 {
208 wxString lineStyle = _( "Default" );
209
210 for( const std::pair<const PLOT_DASH_TYPE, lineTypeStruct>& typeEntry : lineTypeNames )
211 {
212 if( typeEntry.first == GetPlotStyle() )
213 {
214 lineStyle = typeEntry.second.name;
215 break;
216 }
217 }
218
219 aList.emplace_back( _( "Line Style" ), lineStyle );
220 }
221
222 if( aIncludeWidth )
223 aList.emplace_back( _( "Line Width" ), aUnitsProvider->MessageTextFromValue( GetWidth() ) );
224}
225
226
227void STROKE_PARAMS::Format( OUTPUTFORMATTER* aFormatter, const EDA_IU_SCALE& aIuScale,
228 int aNestLevel ) const
229{
230 wxASSERT( aFormatter != nullptr );
231
233 {
234 aFormatter->Print( aNestLevel, "(stroke (width %s) (type %s))",
235 EDA_UNIT_UTILS::FormatInternalUnits( aIuScale, GetWidth() ).c_str(),
237 }
238 else
239 {
240 aFormatter->Print( aNestLevel, "(stroke (width %s) (type %s) (color %d %d %d %s))",
241 EDA_UNIT_UTILS::FormatInternalUnits( aIuScale, GetWidth() ).c_str(),
243 KiROUND( GetColor().r * 255.0 ),
244 KiROUND( GetColor().g * 255.0 ),
245 KiROUND( GetColor().b * 255.0 ),
246 FormatDouble2Str( GetColor().a ).c_str() );
247 }
248}
249
250
252{
253 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
254 {
255 if( token != T_LEFT )
256 Expecting( T_LEFT );
257
258 token = NextTok();
259
260 switch( token )
261 {
262 case T_width:
263 aStroke.SetWidth( parseDouble( "stroke width" ) * m_iuPerMM );
264 NeedRIGHT();
265 break;
266
267 case T_type:
268 {
269 token = NextTok();
270
271 switch( token )
272 {
273 case T_dash: aStroke.SetPlotStyle( PLOT_DASH_TYPE::DASH ); break;
274 case T_dot: aStroke.SetPlotStyle( PLOT_DASH_TYPE::DOT ); break;
275 case T_dash_dot: aStroke.SetPlotStyle( PLOT_DASH_TYPE::DASHDOT ); break;
276 case T_dash_dot_dot: aStroke.SetPlotStyle( PLOT_DASH_TYPE::DASHDOTDOT ); break;
277 case T_solid: aStroke.SetPlotStyle( PLOT_DASH_TYPE::SOLID ); break;
278 case T_default: aStroke.SetPlotStyle( PLOT_DASH_TYPE::DEFAULT ); break;
279 default:
280 Expecting( "solid, dash, dash_dot, dash_dot_dot, dot or default" );
281 }
282
283 NeedRIGHT();
284 break;
285 }
286
287 case T_color:
288 {
290
291 color.r = parseInt( "red" ) / 255.0;
292 color.g = parseInt( "green" ) / 255.0;
293 color.b = parseInt( "blue" ) / 255.0;
294 color.a = Clamp( parseDouble( "alpha" ), 0.0, 1.0 );
295
296 aStroke.SetColor( color );
297 NeedRIGHT();
298 break;
299 }
300
301 default:
302 Expecting( "width, type, or color" );
303 }
304 }
305}
306
307
308int STROKE_PARAMS_PARSER::parseInt( const char* aText )
309{
310 T token = NextTok();
311
312 if( token != T_NUMBER )
313 Expecting( aText );
314
315 return atoi( CurText() );
316}
317
318
319double STROKE_PARAMS_PARSER::parseDouble( const char* aText )
320{
321 T token = NextTok();
322
323 if( token != T_NUMBER )
324 Expecting( aText );
325
326 return DSNLEXER::parseDouble();
327}
328
329
int color
Definition: DXF_plotter.cpp:58
@ stroke_dashdotdot
@ stroke_dashdot
@ stroke_solid
BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:120
double parseDouble()
Parse the current token as an ASCII numeric string with possible leading whitespace into a double pre...
Definition: dsnlexer.cpp:825
EDA_ANGLE Normalize()
Definition: eda_angle.h:249
double Sin() const
Definition: eda_angle.h:206
double Cos() const
Definition: eda_angle.h:221
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:382
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
double GetGapLength(int aLineWidth) const
double GetDotLength(int aLineWidth) const
double GetDashLength(int aLineWidth) const
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:475
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
const VECTOR2I & GetP1() const
Definition: shape_arc.h:113
double GetRadius() const
Definition: shape_arc.cpp:465
VECTOR2I GetCenter() const
Definition: shape_arc.cpp:434
const VECTOR2I & GetP0() const
Definition: shape_arc.h:112
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:98
const SEG & GetSeg() const
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
Definition: shape_simple.h:42
virtual const SEG GetSegment(int aIndex) const override
Definition: shape_simple.h:174
virtual size_t GetSegmentCount() const override
Definition: shape_simple.h:176
An abstract shape on 2D plane.
Definition: shape.h:126
int parseInt(const char *aText)
void ParseStroke(STROKE_PARAMS &aStroke)
double parseDouble(const char *aText)
Simple container to manage line stroke parameters.
Definition: stroke_params.h:81
int GetWidth() const
Definition: stroke_params.h:91
void SetWidth(int aWidth)
Definition: stroke_params.h:92
void SetColor(const KIGFX::COLOR4D &aColor)
Definition: stroke_params.h:98
void GetMsgPanelInfo(UNITS_PROVIDER *aUnitsProvider, std::vector< MSG_PANEL_ITEM > &aList, bool aIncludeStyle=true, bool aIncludeWidth=true)
static void Stroke(const SHAPE *aShape, PLOT_DASH_TYPE aLineStyle, int aWidth, const KIGFX::RENDER_SETTINGS *aRenderSettings, std::function< void(const VECTOR2I &a, const VECTOR2I &b)> aStroker)
KIGFX::COLOR4D GetColor() const
Definition: stroke_params.h:97
PLOT_DASH_TYPE GetPlotStyle() const
Definition: stroke_params.h:94
static wxString GetLineStyleToken(PLOT_DASH_TYPE aStyle)
void Format(OUTPUTFORMATTER *out, const EDA_IU_SCALE &aIuScale, int nestLevel) const
void SetPlotStyle(PLOT_DASH_TYPE aPlotStyle)
Definition: stroke_params.h:95
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A lower-precision version of StringFromValue().
#define _(s)
static constexpr EDA_ANGLE & ANGLE_360
Definition: eda_angle.h:443
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:437
a few functions useful in geometry calculations.
bool ClipLine(const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2)
Test if any part of a line falls within the bounds of a rectangle.
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:96
Message panel definition file.
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue)
Converts aValue from internal units to a string appropriate for writing to file.
Definition: eda_units.cpp:142
CITER next(CITER it)
Definition: ptree.cpp:126
@ SH_CIRCLE
circle
Definition: shape.h:50
@ SH_SIMPLE
simple polygon
Definition: shape.h:51
@ SH_SEGMENT
line segment
Definition: shape.h:48
@ SH_ARC
circular arc
Definition: shape.h:54
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:59
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:378
const std::map< PLOT_DASH_TYPE, struct lineTypeStruct > lineTypeNames
PLOT_DASH_TYPE
Dashed line types.
Definition: stroke_params.h:48
const std::map< PLOT_DASH_TYPE, struct lineTypeStruct > lineTypeNames
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:85
constexpr T Clamp(const T &lower, const T &value, const T &upper)
Limit value within the range lower <= value <= upper.
Definition: util.h:64
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588