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.

324 lines
10 KiB

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