You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
12 KiB

4 years ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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 "stroke_params.h"
  20. #include "stroke_params_parser.h"
  21. #include <base_units.h>
  22. #include <charconv>
  23. #include <string_utils.h>
  24. #include <render_settings.h>
  25. #include <geometry/geometry_utils.h>
  26. #include <geometry/shape.h>
  27. #include <geometry/shape_rect.h>
  28. #include <geometry/shape_segment.h>
  29. #include <geometry/shape_simple.h>
  30. #include <macros.h>
  31. #include <trigo.h>
  32. #include <widgets/msgpanel.h>
  33. using namespace STROKEPARAMS_T;
  34. const std::map<LINE_STYLE, struct LINE_STYLE_DESC> lineTypeNames = {
  35. { LINE_STYLE::SOLID, { _( "Solid" ), BITMAPS::stroke_solid } },
  36. { LINE_STYLE::DASH, { _( "Dashed" ), BITMAPS::stroke_dash } },
  37. { LINE_STYLE::DOT, { _( "Dotted" ), BITMAPS::stroke_dot } },
  38. { LINE_STYLE::DASHDOT, { _( "Dash-Dot" ), BITMAPS::stroke_dashdot } },
  39. { LINE_STYLE::DASHDOTDOT, { _( "Dash-Dot-Dot" ), BITMAPS::stroke_dashdotdot } }
  40. };
  41. void STROKE_PARAMS::Stroke( const SHAPE* aShape, LINE_STYLE aLineStyle, int aWidth,
  42. const KIGFX::RENDER_SETTINGS* aRenderSettings,
  43. const std::function<void( const VECTOR2I& a,
  44. const VECTOR2I& b )>& aStroker )
  45. {
  46. double strokes[6] = { aWidth * 1.0, aWidth * 1.0, aWidth * 1.0, aWidth * 1.0, aWidth * 1.0,
  47. aWidth * 1.0 };
  48. int wrapAround = 6;
  49. switch( aLineStyle )
  50. {
  51. case LINE_STYLE::DASH:
  52. strokes[0] = aRenderSettings->GetDashLength( aWidth );
  53. strokes[1] = aRenderSettings->GetGapLength( aWidth );
  54. wrapAround = 2;
  55. break;
  56. case LINE_STYLE::DOT:
  57. strokes[0] = aRenderSettings->GetDotLength( aWidth );
  58. strokes[1] = aRenderSettings->GetGapLength( aWidth );
  59. wrapAround = 2;
  60. break;
  61. case LINE_STYLE::DASHDOT:
  62. strokes[0] = aRenderSettings->GetDashLength( aWidth );
  63. strokes[1] = aRenderSettings->GetGapLength( aWidth );
  64. strokes[2] = aRenderSettings->GetDotLength( aWidth );
  65. strokes[3] = aRenderSettings->GetGapLength( aWidth );
  66. wrapAround = 4;
  67. break;
  68. case LINE_STYLE::DASHDOTDOT:
  69. strokes[0] = aRenderSettings->GetDashLength( aWidth );
  70. strokes[1] = aRenderSettings->GetGapLength( aWidth );
  71. strokes[2] = aRenderSettings->GetDotLength( aWidth );
  72. strokes[3] = aRenderSettings->GetGapLength( aWidth );
  73. strokes[4] = aRenderSettings->GetDotLength( aWidth );
  74. strokes[5] = aRenderSettings->GetGapLength( aWidth );
  75. wrapAround = 6;
  76. break;
  77. default:
  78. UNIMPLEMENTED_FOR( lineTypeNames.at( aLineStyle ).name );
  79. }
  80. switch( aShape->Type() )
  81. {
  82. case SH_RECT:
  83. {
  84. SHAPE_LINE_CHAIN outline = static_cast<const SHAPE_RECT*>( aShape )->Outline();
  85. for( int ii = 0; ii < outline.SegmentCount(); ++ii )
  86. {
  87. SEG seg = outline.GetSegment( ii );
  88. SHAPE_SEGMENT line( seg.A, seg.B );
  89. STROKE_PARAMS::Stroke( &line, aLineStyle, aWidth, aRenderSettings, aStroker );
  90. }
  91. break;
  92. }
  93. case SH_SIMPLE:
  94. {
  95. const SHAPE_SIMPLE* poly = static_cast<const SHAPE_SIMPLE*>( aShape );
  96. for( size_t ii = 0; ii < poly->GetSegmentCount(); ++ii )
  97. {
  98. SEG seg = poly->GetSegment( (int) ii );
  99. SHAPE_SEGMENT line( seg.A, seg.B );
  100. STROKE_PARAMS::Stroke( &line, aLineStyle, aWidth, aRenderSettings, aStroker );
  101. }
  102. break;
  103. }
  104. case SH_SEGMENT:
  105. {
  106. const SHAPE_SEGMENT* line = static_cast<const SHAPE_SEGMENT*>( aShape );
  107. VECTOR2D start = line->GetSeg().A;
  108. VECTOR2D end = line->GetSeg().B;
  109. BOX2I clip( start, VECTOR2I( KiROUND( end.x - start.x ), KiROUND( end.y - start.y ) ) );
  110. clip.Normalize();
  111. double theta = atan2( end.y - start.y, end.x - start.x );
  112. for( size_t i = 0; i < 10000; ++i )
  113. {
  114. // Calculations MUST be done in doubles to keep from accumulating rounding
  115. // errors as we go.
  116. VECTOR2D next( start.x + strokes[ i % wrapAround ] * cos( theta ),
  117. start.y + strokes[ i % wrapAround ] * sin( theta ) );
  118. // Drawing each segment can be done rounded to ints.
  119. VECTOR2I a( KiROUND( start.x ), KiROUND( start.y ) );
  120. VECTOR2I b( KiROUND( next.x ), KiROUND( next.y ) );
  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. start = next;
  126. }
  127. break;
  128. }
  129. case SH_ARC:
  130. {
  131. const SHAPE_ARC* arc = static_cast<const SHAPE_ARC*>( aShape );
  132. double r = arc->GetRadius();
  133. double C = 2.0 * M_PI * r;
  134. VECTOR2I center = arc->GetCenter();
  135. VECTOR2D startRadial( arc->GetP0() - center );
  136. EDA_ANGLE startAngle( startRadial );
  137. VECTOR2D endRadial( arc->GetP1() - center );
  138. EDA_ANGLE arcEndAngle( endRadial );
  139. if( arcEndAngle == startAngle )
  140. arcEndAngle = startAngle + ANGLE_360; // ring, not null
  141. if( startAngle > arcEndAngle )
  142. {
  143. if( arcEndAngle < ANGLE_0 )
  144. arcEndAngle = arcEndAngle.Normalize();
  145. else
  146. startAngle = startAngle.Normalize() - ANGLE_360;
  147. }
  148. wxASSERT( startAngle < arcEndAngle );
  149. EDA_ANGLE angleIncrement = EDA_ANGLE( 0.5, DEGREES_T );
  150. for( size_t i = 0; i < 10000 && startAngle < arcEndAngle; ++i )
  151. {
  152. EDA_ANGLE theta = ANGLE_360 * strokes[ i % wrapAround ] / C;
  153. EDA_ANGLE endAngle = std::min( startAngle + theta, arcEndAngle );
  154. if( i % 2 == 0 )
  155. {
  156. if( ( ( aLineStyle == LINE_STYLE::DASHDOT || aLineStyle == LINE_STYLE::DASHDOTDOT )
  157. && i % wrapAround == 0 )
  158. || aLineStyle == LINE_STYLE::DASH )
  159. {
  160. for( EDA_ANGLE currentAngle = startAngle; currentAngle < endAngle;
  161. currentAngle += angleIncrement )
  162. {
  163. VECTOR2I a( center.x + KiROUND( r * currentAngle.Cos() ),
  164. center.y + KiROUND( r * currentAngle.Sin() ) );
  165. // Calculate the next angle step, ensuring it doesn't exceed the endAngle
  166. EDA_ANGLE nextAngle = currentAngle + angleIncrement;
  167. if( nextAngle > endAngle )
  168. {
  169. nextAngle = endAngle; // Set nextAngle to endAngle if it exceeds
  170. }
  171. VECTOR2I b( center.x + KiROUND( r * nextAngle.Cos() ),
  172. center.y + KiROUND( r * nextAngle.Sin() ) );
  173. aStroker( a, b ); // Draw the segment as an arc
  174. }
  175. }
  176. else
  177. {
  178. VECTOR2I a( center.x + KiROUND( r * startAngle.Cos() ),
  179. center.y + KiROUND( r * startAngle.Sin() ) );
  180. VECTOR2I b( center.x + KiROUND( r * endAngle.Cos() ),
  181. center.y + KiROUND( r * endAngle.Sin() ) );
  182. aStroker( a, b );
  183. }
  184. }
  185. startAngle = endAngle;
  186. }
  187. break;
  188. }
  189. case SH_CIRCLE:
  190. // A circle is always filled; a ring is represented by a 360° arc.
  191. KI_FALLTHROUGH;
  192. default:
  193. UNIMPLEMENTED_FOR( SHAPE_TYPE_asString( aShape->Type() ) );
  194. }
  195. }
  196. wxString STROKE_PARAMS::GetLineStyleToken( LINE_STYLE aStyle )
  197. {
  198. wxString token;
  199. switch( aStyle )
  200. {
  201. case LINE_STYLE::DASH: token = wxT( "dash" ); break;
  202. case LINE_STYLE::DOT: token = wxT( "dot" ); break;
  203. case LINE_STYLE::DASHDOT: token = wxT( "dash_dot" ); break;
  204. case LINE_STYLE::DASHDOTDOT: token = wxT( "dash_dot_dot" ); break;
  205. case LINE_STYLE::SOLID: token = wxT( "solid" ); break;
  206. case LINE_STYLE::DEFAULT: token = wxT( "default" ); break;
  207. }
  208. return token;
  209. }
  210. void STROKE_PARAMS::GetMsgPanelInfo( UNITS_PROVIDER* aUnitsProvider,
  211. std::vector<MSG_PANEL_ITEM>& aList,
  212. bool aIncludeStyle, bool aIncludeWidth )
  213. {
  214. if( aIncludeStyle )
  215. {
  216. wxString msg = _( "Default" );
  217. for( const auto& [ lineStyle, lineStyleDesc ] : lineTypeNames )
  218. {
  219. if( lineStyle == GetLineStyle() )
  220. {
  221. msg = lineStyleDesc.name;
  222. break;
  223. }
  224. }
  225. aList.emplace_back( _( "Line Style" ), msg );
  226. }
  227. if( aIncludeWidth )
  228. aList.emplace_back( _( "Line Width" ), aUnitsProvider->MessageTextFromValue( GetWidth() ) );
  229. }
  230. void STROKE_PARAMS::Format( OUTPUTFORMATTER* aFormatter, const EDA_IU_SCALE& aIuScale ) const
  231. {
  232. wxASSERT( aFormatter != nullptr );
  233. if( GetColor() == KIGFX::COLOR4D::UNSPECIFIED )
  234. {
  235. aFormatter->Print( "(stroke (width %s) (type %s))",
  236. EDA_UNIT_UTILS::FormatInternalUnits( aIuScale, GetWidth() ).c_str(),
  237. TO_UTF8( GetLineStyleToken( GetLineStyle() ) ) );
  238. }
  239. else
  240. {
  241. aFormatter->Print( "(stroke (width %s) (type %s) (color %d %d %d %s))",
  242. EDA_UNIT_UTILS::FormatInternalUnits( aIuScale, GetWidth() ).c_str(),
  243. TO_UTF8( GetLineStyleToken( GetLineStyle() ) ),
  244. KiROUND( GetColor().r * 255.0 ),
  245. KiROUND( GetColor().g * 255.0 ),
  246. KiROUND( GetColor().b * 255.0 ),
  247. FormatDouble2Str( GetColor().a ).c_str() );
  248. }
  249. }
  250. void STROKE_PARAMS_PARSER::ParseStroke( STROKE_PARAMS& aStroke )
  251. {
  252. for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
  253. {
  254. if( token != T_LEFT )
  255. Expecting( T_LEFT );
  256. token = NextTok();
  257. switch( token )
  258. {
  259. case T_width:
  260. aStroke.SetWidth( KiROUND( parseDouble( "stroke width" ) * m_iuPerMM ) );
  261. NeedRIGHT();
  262. break;
  263. case T_type:
  264. {
  265. token = NextTok();
  266. switch( token )
  267. {
  268. case T_dash: aStroke.SetLineStyle( LINE_STYLE::DASH ); break;
  269. case T_dot: aStroke.SetLineStyle( LINE_STYLE::DOT ); break;
  270. case T_dash_dot: aStroke.SetLineStyle( LINE_STYLE::DASHDOT ); break;
  271. case T_dash_dot_dot: aStroke.SetLineStyle( LINE_STYLE::DASHDOTDOT ); break;
  272. case T_solid: aStroke.SetLineStyle( LINE_STYLE::SOLID ); break;
  273. case T_default: aStroke.SetLineStyle( LINE_STYLE::DEFAULT ); break;
  274. default:
  275. Expecting( "solid, dash, dash_dot, dash_dot_dot, dot or default" );
  276. }
  277. NeedRIGHT();
  278. break;
  279. }
  280. case T_color:
  281. {
  282. KIGFX::COLOR4D color;
  283. color.r = parseInt( "red" ) / 255.0;
  284. color.g = parseInt( "green" ) / 255.0;
  285. color.b = parseInt( "blue" ) / 255.0;
  286. color.a = std::clamp( parseDouble( "alpha" ), 0.0, 1.0 );
  287. aStroke.SetColor( color );
  288. NeedRIGHT();
  289. break;
  290. }
  291. default:
  292. Expecting( "width, type, or color" );
  293. }
  294. }
  295. }
  296. int STROKE_PARAMS_PARSER::parseInt( const char* aText )
  297. {
  298. T token = NextTok();
  299. if( token != T_NUMBER )
  300. Expecting( aText );
  301. return atoi( CurText() );
  302. }
  303. double STROKE_PARAMS_PARSER::parseDouble( const char* aText )
  304. {
  305. T token = NextTok();
  306. if( token != T_NUMBER )
  307. Expecting( aText );
  308. return DSNLEXER::parseDouble();
  309. }