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.

349 lines
11 KiB

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