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#include "geometry/shape_rect.h"
32
33using namespace STROKEPARAMS_T;
34
35
36const std::map<LINE_STYLE, struct LINE_STYLE_DESC> lineTypeNames = {
37 { LINE_STYLE::SOLID, { _( "Solid" ), BITMAPS::stroke_solid } },
38 { LINE_STYLE::DASH, { _( "Dashed" ), BITMAPS::stroke_dash } },
39 { LINE_STYLE::DOT, { _( "Dotted" ), BITMAPS::stroke_dot } },
40 { LINE_STYLE::DASHDOT, { _( "Dash-Dot" ), BITMAPS::stroke_dashdot } },
41 { LINE_STYLE::DASHDOTDOT, { _( "Dash-Dot-Dot" ), BITMAPS::stroke_dashdotdot } }
42};
43
44
45void STROKE_PARAMS::Stroke( const SHAPE* aShape, LINE_STYLE aLineStyle, int aWidth,
46 const KIGFX::RENDER_SETTINGS* aRenderSettings,
47 const std::function<void( const VECTOR2I& a, const VECTOR2I& b )>& aStroker )
48{
49 double strokes[6] = { aWidth * 1.0, aWidth * 1.0, aWidth * 1.0, aWidth * 1.0, aWidth * 1.0,
50 aWidth * 1.0 };
51 int wrapAround = 6;
52
53 switch( aLineStyle )
54 {
55 case LINE_STYLE::DASH:
56 strokes[0] = aRenderSettings->GetDashLength( aWidth );
57 strokes[1] = aRenderSettings->GetGapLength( aWidth );
58 wrapAround = 2;
59 break;
60 case LINE_STYLE::DOT:
61 strokes[0] = aRenderSettings->GetDotLength( aWidth );
62 strokes[1] = aRenderSettings->GetGapLength( aWidth );
63 wrapAround = 2;
64 break;
65 case LINE_STYLE::DASHDOT:
66 strokes[0] = aRenderSettings->GetDashLength( aWidth );
67 strokes[1] = aRenderSettings->GetGapLength( aWidth );
68 strokes[2] = aRenderSettings->GetDotLength( aWidth );
69 strokes[3] = aRenderSettings->GetGapLength( aWidth );
70 wrapAround = 4;
71 break;
72 case LINE_STYLE::DASHDOTDOT:
73 strokes[0] = aRenderSettings->GetDashLength( aWidth );
74 strokes[1] = aRenderSettings->GetGapLength( aWidth );
75 strokes[2] = aRenderSettings->GetDotLength( aWidth );
76 strokes[3] = aRenderSettings->GetGapLength( aWidth );
77 strokes[4] = aRenderSettings->GetDotLength( aWidth );
78 strokes[5] = aRenderSettings->GetGapLength( aWidth );
79 wrapAround = 6;
80 break;
81 default:
82 UNIMPLEMENTED_FOR( lineTypeNames.at( aLineStyle ).name );
83 }
84
85 switch( aShape->Type() )
86 {
87 case SH_RECT:
88 {
89 SHAPE_LINE_CHAIN outline = static_cast<const SHAPE_RECT*>( aShape )->Outline();
90
91 for( int ii = 0; ii < outline.SegmentCount(); ++ii )
92 {
93 SEG seg = outline.GetSegment( ii );
94 SHAPE_SEGMENT line( seg.A, seg.B );
95 STROKE_PARAMS::Stroke( &line, aLineStyle, aWidth, aRenderSettings, aStroker );
96 }
97
98 break;
99 }
100
101 case SH_SIMPLE:
102 {
103 const SHAPE_SIMPLE* poly = static_cast<const SHAPE_SIMPLE*>( aShape );
104
105 for( size_t ii = 0; ii < poly->GetSegmentCount(); ++ii )
106 {
107 SEG seg = poly->GetSegment( (int) ii );
108 SHAPE_SEGMENT line( seg.A, seg.B );
109 STROKE_PARAMS::Stroke( &line, aLineStyle, aWidth, aRenderSettings, aStroker );
110 }
111
112 break;
113 }
114
115 case SH_SEGMENT:
116 {
117 const SHAPE_SEGMENT* line = static_cast<const SHAPE_SEGMENT*>( aShape );
118
119 VECTOR2D start = line->GetSeg().A;
120 VECTOR2D end = line->GetSeg().B;
121 BOX2I clip( start, VECTOR2I( KiROUND( end.x - start.x ), KiROUND( end.y - start.y ) ) );
122 clip.Normalize();
123
124 double theta = atan2( end.y - start.y, end.x - start.x );
125
126 for( size_t i = 0; i < 10000; ++i )
127 {
128 // Calculations MUST be done in doubles to keep from accumulating rounding
129 // errors as we go.
130 VECTOR2D next( start.x + strokes[ i % wrapAround ] * cos( theta ),
131 start.y + strokes[ i % wrapAround ] * sin( theta ) );
132
133 // Drawing each segment can be done rounded to ints.
134 VECTOR2I a( KiROUND( start.x ), KiROUND( start.y ) );
135 VECTOR2I b( KiROUND( next.x ), KiROUND( next.y ) );
136
137 if( ClipLine( &clip, a.x, a.y, b.x, b.y ) )
138 break;
139 else if( i % 2 == 0 )
140 aStroker( a, b );
141
142 start = next;
143 }
144
145 break;
146 }
147
148 case SH_ARC:
149 {
150 const SHAPE_ARC* arc = static_cast<const SHAPE_ARC*>( aShape );
151
152 double r = arc->GetRadius();
153 double C = 2.0 * M_PI * r;
154 VECTOR2I center = arc->GetCenter();
155 VECTOR2D startRadial( arc->GetP0() - center );
156 EDA_ANGLE startAngle( startRadial );
157 VECTOR2D endRadial( arc->GetP1() - center );
158 EDA_ANGLE arcEndAngle( endRadial );
159
160 if( arcEndAngle == startAngle )
161 arcEndAngle = startAngle + ANGLE_360; // ring, not null
162
163 if( startAngle > arcEndAngle )
164 {
165 if( arcEndAngle < ANGLE_0 )
166 arcEndAngle = arcEndAngle.Normalize();
167 else
168 startAngle = startAngle.Normalize() - ANGLE_360;
169 }
170
171 wxASSERT( startAngle < arcEndAngle );
172
173 for( size_t i = 0; i < 10000 && startAngle < arcEndAngle; ++i )
174 {
175 EDA_ANGLE theta = ANGLE_360 * strokes[ i % wrapAround ] / C;
176 EDA_ANGLE endAngle = std::min( startAngle + theta, arcEndAngle );
177
178 if( i % 2 == 0 )
179 {
180 VECTOR2I a( center.x + KiROUND( r * startAngle.Cos() ),
181 center.y + KiROUND( r * startAngle.Sin() ) );
182 VECTOR2I b( center.x + KiROUND( r * endAngle.Cos() ),
183 center.y + KiROUND( r * endAngle.Sin() ) );
184
185 aStroker( a, b );
186 }
187
188 startAngle = endAngle;
189 }
190
191 break;
192 }
193
194 case SH_CIRCLE:
195 // A circle is always filled; a ring is represented by a 360° arc.
197
198 default:
200 }
201}
202
203
205{
206 wxString token;
207
208 switch( aStyle )
209 {
210 case LINE_STYLE::DASH: token = wxT( "dash" ); break;
211 case LINE_STYLE::DOT: token = wxT( "dot" ); break;
212 case LINE_STYLE::DASHDOT: token = wxT( "dash_dot" ); break;
213 case LINE_STYLE::DASHDOTDOT: token = wxT( "dash_dot_dot" ); break;
214 case LINE_STYLE::SOLID: token = wxT( "solid" ); break;
215 case LINE_STYLE::DEFAULT: token = wxT( "default" ); break;
216 }
217
218 return token;
219}
220
221
223 std::vector<MSG_PANEL_ITEM>& aList,
224 bool aIncludeStyle, bool aIncludeWidth )
225{
226 if( aIncludeStyle )
227 {
228 wxString msg = _( "Default" );
229
230 for( const auto& [ lineStyle, lineStyleDesc ] : lineTypeNames )
231 {
232 if( lineStyle == GetLineStyle() )
233 {
234 msg = lineStyleDesc.name;
235 break;
236 }
237 }
238
239 aList.emplace_back( _( "Line Style" ), msg );
240 }
241
242 if( aIncludeWidth )
243 aList.emplace_back( _( "Line Width" ), aUnitsProvider->MessageTextFromValue( GetWidth() ) );
244}
245
246
247void STROKE_PARAMS::Format( OUTPUTFORMATTER* aFormatter, const EDA_IU_SCALE& aIuScale,
248 int aNestLevel ) const
249{
250 wxASSERT( aFormatter != nullptr );
251
253 {
254 aFormatter->Print( aNestLevel, "(stroke (width %s) (type %s))",
255 EDA_UNIT_UTILS::FormatInternalUnits( aIuScale, GetWidth() ).c_str(),
257 }
258 else
259 {
260 aFormatter->Print( aNestLevel, "(stroke (width %s) (type %s) (color %d %d %d %s))",
261 EDA_UNIT_UTILS::FormatInternalUnits( aIuScale, GetWidth() ).c_str(),
263 KiROUND( GetColor().r * 255.0 ),
264 KiROUND( GetColor().g * 255.0 ),
265 KiROUND( GetColor().b * 255.0 ),
266 FormatDouble2Str( GetColor().a ).c_str() );
267 }
268}
269
270
272{
273 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
274 {
275 if( token != T_LEFT )
276 Expecting( T_LEFT );
277
278 token = NextTok();
279
280 switch( token )
281 {
282 case T_width:
283 aStroke.SetWidth( KiROUND( parseDouble( "stroke width" ) * m_iuPerMM ) );
284 NeedRIGHT();
285 break;
286
287 case T_type:
288 {
289 token = NextTok();
290
291 switch( token )
292 {
293 case T_dash: aStroke.SetLineStyle( LINE_STYLE::DASH ); break;
294 case T_dot: aStroke.SetLineStyle( LINE_STYLE::DOT ); break;
295 case T_dash_dot: aStroke.SetLineStyle( LINE_STYLE::DASHDOT ); break;
296 case T_dash_dot_dot: aStroke.SetLineStyle( LINE_STYLE::DASHDOTDOT ); break;
297 case T_solid: aStroke.SetLineStyle( LINE_STYLE::SOLID ); break;
298 case T_default: aStroke.SetLineStyle( LINE_STYLE::DEFAULT ); break;
299 default:
300 Expecting( "solid, dash, dash_dot, dash_dot_dot, dot or default" );
301 }
302
303 NeedRIGHT();
304 break;
305 }
306
307 case T_color:
308 {
310
311 color.r = parseInt( "red" ) / 255.0;
312 color.g = parseInt( "green" ) / 255.0;
313 color.b = parseInt( "blue" ) / 255.0;
314 color.a = Clamp( parseDouble( "alpha" ), 0.0, 1.0 );
315
316 aStroke.SetColor( color );
317 NeedRIGHT();
318 break;
319 }
320
321 default:
322 Expecting( "width, type, or color" );
323 }
324 }
325}
326
327
328int STROKE_PARAMS_PARSER::parseInt( const char* aText )
329{
330 T token = NextTok();
331
332 if( token != T_NUMBER )
333 Expecting( aText );
334
335 return atoi( CurText() );
336}
337
338
339double STROKE_PARAMS_PARSER::parseDouble( const char* aText )
340{
341 T token = NextTok();
342
343 if( token != T_NUMBER )
344 Expecting( aText );
345
346 return DSNLEXER::parseDouble();
347}
348
349
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:824
EDA_ANGLE Normalize()
Definition: eda_angle.h:255
double Sin() const
Definition: eda_angle.h:212
double Cos() const
Definition: eda_angle.h:227
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:458
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:505
VECTOR2I GetCenter() const
Definition: shape_arc.cpp:474
const VECTOR2I & GetP0() const
Definition: shape_arc.h:112
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:98
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
virtual const SEG GetSegment(int aIndex) const override
int SegmentCount() const
Return the number of segments in this line chain.
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 SetLineStyle(LINE_STYLE aLineStyle)
Definition: stroke_params.h:95
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)
LINE_STYLE GetLineStyle() const
Definition: stroke_params.h:94
KIGFX::COLOR4D GetColor() const
Definition: stroke_params.h:97
void Format(OUTPUTFORMATTER *out, const EDA_IU_SCALE &aIuScale, int nestLevel) const
static void Stroke(const SHAPE *aShape, LINE_STYLE aLineStyle, int aWidth, const KIGFX::RENDER_SETTINGS *aRenderSettings, const std::function< void(const VECTOR2I &a, const VECTOR2I &b)> &aStroker)
static wxString GetLineStyleToken(LINE_STYLE aStyle)
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:435
static constexpr EDA_ANGLE ANGLE_360
Definition: eda_angle.h:441
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:169
CITER next(CITER it)
Definition: ptree.cpp:126
@ SH_RECT
axis-aligned rectangle
Definition: shape.h:47
@ 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:391
const std::map< LINE_STYLE, struct LINE_STYLE_DESC > lineTypeNames
const std::map< LINE_STYLE, struct LINE_STYLE_DESC > lineTypeNames
LINE_STYLE
Dashed line types.
Definition: stroke_params.h:48
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