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.

322 lines
10 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 CERN
  5. * Copyright (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #ifndef GRAPHICS_IMPORTER_BUFFER_H
  27. #define GRAPHICS_IMPORTER_BUFFER_H
  28. #include "graphics_importer.h"
  29. #include <math/matrix3x3.h>
  30. #include <list>
  31. class IMPORTED_SHAPE
  32. {
  33. public:
  34. virtual ~IMPORTED_SHAPE() {}
  35. virtual void ImportTo( GRAPHICS_IMPORTER& aImporter ) const = 0;
  36. virtual std::unique_ptr<IMPORTED_SHAPE> clone() const = 0;
  37. virtual void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) = 0;
  38. void SetParentShapeIndex( int aIndex ) { m_parentShapeIndex = aIndex; }
  39. int GetParentShapeIndex() const { return m_parentShapeIndex; }
  40. protected:
  41. int m_parentShapeIndex = -1;
  42. };
  43. class IMPORTED_LINE : public IMPORTED_SHAPE
  44. {
  45. public:
  46. IMPORTED_LINE( const VECTOR2D& aStart, const VECTOR2D& aEnd, const IMPORTED_STROKE& aStroke ) :
  47. m_start( aStart ), m_end( aEnd ), m_stroke( aStroke )
  48. {
  49. }
  50. void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
  51. {
  52. aImporter.AddLine( m_start, m_end, m_stroke );
  53. }
  54. virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
  55. {
  56. return std::make_unique<IMPORTED_LINE>( *this );
  57. }
  58. void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
  59. {
  60. m_start = aTransform * m_start + aTranslation;
  61. m_end = aTransform * m_end + aTranslation;
  62. }
  63. private:
  64. VECTOR2D m_start;
  65. VECTOR2D m_end;
  66. IMPORTED_STROKE m_stroke;
  67. };
  68. class IMPORTED_CIRCLE : public IMPORTED_SHAPE
  69. {
  70. public:
  71. IMPORTED_CIRCLE( const VECTOR2D& aCenter, double aRadius, const IMPORTED_STROKE& aStroke,
  72. bool aFilled, const COLOR4D& aFillColor ) :
  73. m_center( aCenter ),
  74. m_radius( aRadius ), m_stroke( aStroke ), m_filled( aFilled ), m_fillColor( aFillColor )
  75. {
  76. }
  77. void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
  78. {
  79. aImporter.AddCircle( m_center, m_radius, m_stroke, m_filled, m_fillColor );
  80. }
  81. virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
  82. {
  83. return std::make_unique<IMPORTED_CIRCLE>( *this );
  84. }
  85. void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
  86. {
  87. VECTOR2D newCenter = ( aTransform * m_center ) + aTranslation;
  88. VECTOR2D newRadius = VECTOR2D( m_radius, 0 );
  89. newRadius = aTransform * newRadius;
  90. m_center = newCenter;
  91. m_radius = newRadius.EuclideanNorm();
  92. }
  93. private:
  94. VECTOR2D m_center;
  95. double m_radius;
  96. IMPORTED_STROKE m_stroke;
  97. bool m_filled;
  98. COLOR4D m_fillColor;
  99. };
  100. class IMPORTED_ARC : public IMPORTED_SHAPE
  101. {
  102. public:
  103. IMPORTED_ARC( const VECTOR2D& aCenter, const VECTOR2D& aStart, const EDA_ANGLE& aAngle,
  104. const IMPORTED_STROKE& aStroke ) :
  105. m_center( aCenter ),
  106. m_start( aStart ), m_angle( aAngle ), m_stroke( aStroke )
  107. {
  108. }
  109. void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
  110. {
  111. aImporter.AddArc( m_center, m_start, m_angle, m_stroke );
  112. }
  113. virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
  114. {
  115. return std::make_unique<IMPORTED_ARC>( *this );
  116. }
  117. void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
  118. {
  119. m_start = aTransform * m_start + aTranslation;
  120. m_center = aTransform * m_center + aTranslation;
  121. }
  122. private:
  123. VECTOR2D m_center;
  124. VECTOR2D m_start;
  125. EDA_ANGLE m_angle;
  126. IMPORTED_STROKE m_stroke;
  127. };
  128. class IMPORTED_POLYGON : public IMPORTED_SHAPE
  129. {
  130. public:
  131. IMPORTED_POLYGON( const std::vector<VECTOR2D>& aVertices, const IMPORTED_STROKE& aStroke,
  132. bool aFilled, const COLOR4D& aFillColor ) :
  133. m_vertices( aVertices ),
  134. m_stroke( aStroke ), m_filled( aFilled ), m_fillColor( aFillColor )
  135. {
  136. }
  137. void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
  138. {
  139. aImporter.AddPolygon( m_vertices, m_stroke, m_filled, m_fillColor );
  140. }
  141. virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
  142. {
  143. return std::make_unique<IMPORTED_POLYGON>( *this );
  144. }
  145. void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
  146. {
  147. for( VECTOR2D& vert : m_vertices )
  148. {
  149. vert = aTransform * vert + aTranslation;
  150. }
  151. }
  152. std::vector<VECTOR2D>& Vertices() { return m_vertices; }
  153. bool IsFilled() const { return m_filled; }
  154. const COLOR4D& GetFillColor() const { return m_fillColor; }
  155. const IMPORTED_STROKE& GetStroke() const { return m_stroke; }
  156. private:
  157. std::vector<VECTOR2D> m_vertices;
  158. IMPORTED_STROKE m_stroke;
  159. bool m_filled;
  160. COLOR4D m_fillColor;
  161. };
  162. class IMPORTED_TEXT : public IMPORTED_SHAPE
  163. {
  164. public:
  165. IMPORTED_TEXT( const VECTOR2D& aOrigin, const wxString& aText, double aHeight, double aWidth,
  166. double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
  167. GR_TEXT_V_ALIGN_T aVJustify, const COLOR4D& aColor ) :
  168. m_origin( aOrigin ),
  169. m_text( aText ), m_height( aHeight ), m_width( aWidth ), m_thickness( aThickness ),
  170. m_orientation( aOrientation ), m_hJustify( aHJustify ), m_vJustify( aVJustify ),
  171. m_color( aColor )
  172. {
  173. }
  174. void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
  175. {
  176. aImporter.AddText( m_origin, m_text, m_height, m_width, m_thickness, m_orientation,
  177. m_hJustify, m_vJustify, m_color );
  178. }
  179. virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
  180. {
  181. return std::make_unique<IMPORTED_TEXT>( *this );
  182. }
  183. void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
  184. {
  185. m_origin = aTransform * m_origin + aTranslation;
  186. }
  187. private:
  188. VECTOR2D m_origin;
  189. const wxString m_text;
  190. double m_height;
  191. double m_width;
  192. double m_thickness;
  193. double m_orientation;
  194. GR_TEXT_H_ALIGN_T m_hJustify;
  195. GR_TEXT_V_ALIGN_T m_vJustify;
  196. COLOR4D m_color;
  197. };
  198. class IMPORTED_SPLINE : public IMPORTED_SHAPE
  199. {
  200. public:
  201. IMPORTED_SPLINE( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
  202. const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
  203. const IMPORTED_STROKE& aStroke ) :
  204. m_start( aStart ),
  205. m_bezierControl1( aBezierControl1 ), m_bezierControl2( aBezierControl2 ), m_end( aEnd ),
  206. m_stroke( aStroke )
  207. {
  208. }
  209. void ImportTo( GRAPHICS_IMPORTER& aImporter ) const override
  210. {
  211. aImporter.AddSpline( m_start, m_bezierControl1, m_bezierControl2, m_end, m_stroke );
  212. }
  213. virtual std::unique_ptr<IMPORTED_SHAPE> clone() const override
  214. {
  215. return std::make_unique<IMPORTED_SPLINE>( *this );
  216. }
  217. void Transform( const MATRIX3x3D& aTransform, const VECTOR2D& aTranslation ) override
  218. {
  219. m_start = aTransform * m_start + aTranslation;
  220. m_bezierControl1 = aTransform * m_bezierControl1 + aTranslation;
  221. m_bezierControl2 = aTransform * m_bezierControl2 + aTranslation;
  222. m_end = aTransform * m_end + aTranslation;
  223. }
  224. private:
  225. VECTOR2D m_start;
  226. VECTOR2D m_bezierControl1;
  227. VECTOR2D m_bezierControl2;
  228. VECTOR2D m_end;
  229. IMPORTED_STROKE m_stroke;
  230. };
  231. class GRAPHICS_IMPORTER_BUFFER : public GRAPHICS_IMPORTER
  232. {
  233. public:
  234. void AddLine( const VECTOR2D& aStart, const VECTOR2D& aEnd,
  235. const IMPORTED_STROKE& aStroke ) override;
  236. void AddCircle( const VECTOR2D& aCenter, double aRadius, const IMPORTED_STROKE& aStroke,
  237. bool aFilled, const COLOR4D& aFillColor = COLOR4D::UNSPECIFIED ) override;
  238. void AddArc( const VECTOR2D& aCenter, const VECTOR2D& aStart, const EDA_ANGLE& aAngle,
  239. const IMPORTED_STROKE& aStroke ) override;
  240. void AddPolygon( const std::vector<VECTOR2D>& aVertices, const IMPORTED_STROKE& aStroke,
  241. bool aFilled, const COLOR4D& aFillColor = COLOR4D::UNSPECIFIED ) override;
  242. void AddText( const VECTOR2D& aOrigin, const wxString& aText, double aHeight, double aWidth,
  243. double aThickness, double aOrientation, GR_TEXT_H_ALIGN_T aHJustify,
  244. GR_TEXT_V_ALIGN_T aVJustify,
  245. const COLOR4D& aColor = COLOR4D::UNSPECIFIED ) override;
  246. void AddSpline( const VECTOR2D& aStart, const VECTOR2D& aBezierControl1,
  247. const VECTOR2D& aBezierControl2, const VECTOR2D& aEnd,
  248. const IMPORTED_STROKE& aStroke ) override;
  249. void ImportTo( GRAPHICS_IMPORTER& aImporter );
  250. void AddShape( std::unique_ptr<IMPORTED_SHAPE>& aShape );
  251. std::list<std::unique_ptr<IMPORTED_SHAPE>>& GetShapes() { return m_shapes; }
  252. void ClearShapes() { m_shapes.clear(); }
  253. void PostprocessNestedPolygons();
  254. protected:
  255. ///< List of imported shapes
  256. std::list<std::unique_ptr<IMPORTED_SHAPE>> m_shapes;
  257. };
  258. #endif /* GRAPHICS_IMPORTER_BUFFER */