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.

179 lines
5.3 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef BASIC_GAL_H
  25. #define BASIC_GAL_H
  26. #include <eda_rect.h>
  27. #include <gal/stroke_font.h>
  28. #include <gal/graphics_abstraction_layer.h>
  29. #include <newstroke_font.h>
  30. class PLOTTER;
  31. /*
  32. * A minimal GAL implementation to draw, plot and convert stroke texts to a set of segments
  33. * for DRC tests, and to calculate text sizes.
  34. *
  35. * Currently it allows one to use GAL and STROKE_FONT methods in legacy draw mode
  36. * (using wxDC functions) in plot functions only for texts.
  37. * It is used also to calculate the text bounding boxes
  38. *
  39. * The main purpose is to avoid duplicate code to do the same thing in GAL canvas,
  40. * print & plotter canvasses and DRC.
  41. *
  42. * It will be certainly removed when a full GAL canvas using wxDC is implemented
  43. * (or at least restricted to plotter and DRC "canvas")
  44. */
  45. struct TRANSFORM_PRM // A helper class to transform coordinates in BASIC_GAL canvas
  46. {
  47. VECTOR2D m_rotCenter;
  48. VECTOR2D m_moveOffset;
  49. double m_rotAngle;
  50. };
  51. class BASIC_GAL: public KIGFX::GAL
  52. {
  53. public:
  54. BASIC_GAL( KIGFX::GAL_DISPLAY_OPTIONS& aDisplayOptions ) :
  55. GAL( aDisplayOptions ),
  56. m_DC( nullptr ),
  57. m_Color( RED ),
  58. m_transform(),
  59. m_clipBox(),
  60. m_isClipped( false ),
  61. m_callback( nullptr ),
  62. m_callbackData( nullptr ),
  63. m_plotter( nullptr )
  64. {
  65. }
  66. void SetPlotter( PLOTTER* aPlotter )
  67. {
  68. m_plotter = aPlotter;
  69. }
  70. void SetCallback( void (* aCallback)( int x0, int y0, int xf, int yf, void* aData ),
  71. void* aData )
  72. {
  73. m_callback = aCallback;
  74. m_callbackData = aData;
  75. }
  76. /// Set a clip box for drawings
  77. /// If NULL, no clip will be made
  78. void SetClipBox( EDA_RECT* aClipBox )
  79. {
  80. m_isClipped = aClipBox != nullptr;
  81. if( aClipBox )
  82. m_clipBox = *aClipBox;
  83. }
  84. /// Save the context.
  85. virtual void Save() override
  86. {
  87. m_transformHistory.push( m_transform );
  88. }
  89. virtual void Restore() override
  90. {
  91. m_transform = m_transformHistory.top();
  92. m_transformHistory.pop();
  93. }
  94. /**
  95. * Draw a polyline
  96. *
  97. * @param aPointList is a list of 2D-Vectors containing the polyline points.
  98. */
  99. virtual void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
  100. virtual void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
  101. /**
  102. * Start and end points are defined as 2D-Vectors.
  103. *
  104. * @param aStartPoint is the start point of the line.
  105. * @param aEndPoint is the end point of the line.
  106. */
  107. virtual void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
  108. /**
  109. * Translate the context.
  110. *
  111. * @param aTranslation is the translation vector.
  112. */
  113. virtual void Translate( const VECTOR2D& aTranslation ) override
  114. {
  115. m_transform.m_moveOffset += aTranslation;
  116. }
  117. /**
  118. * Rotate the context.
  119. *
  120. * @param aAngle is the rotation angle in radians.
  121. */
  122. virtual void Rotate( double aAngle ) override
  123. {
  124. m_transform.m_rotAngle = aAngle;
  125. m_transform.m_rotCenter = m_transform.m_moveOffset;
  126. }
  127. private:
  128. void doDrawPolyline( const std::vector<wxPoint>& aLocalPointList );
  129. // Apply the rotation/translation transform to aPoint
  130. const VECTOR2D transform( const VECTOR2D& aPoint ) const;
  131. public:
  132. wxDC* m_DC;
  133. COLOR4D m_Color;
  134. private:
  135. TRANSFORM_PRM m_transform;
  136. std::stack <TRANSFORM_PRM> m_transformHistory;
  137. // A clip box, to clip drawings in a wxDC (mandatory to avoid draw issues)
  138. EDA_RECT m_clipBox; // The clip box
  139. bool m_isClipped; // Allows/disallows clipping
  140. // When calling the draw functions outside a wxDC, to get the basic drawings
  141. // lines / polylines ..., a callback function (used in DRC) to store
  142. // coordinates of each segment:
  143. void (* m_callback)( int x0, int y0, int xf, int yf, void* aData );
  144. void* m_callbackData; // a optional parameter for m_callback
  145. // When calling the draw functions for plot, the plotter acts as a wxDC to plot basic items.
  146. PLOTTER* m_plotter;
  147. };
  148. extern BASIC_GAL basic_gal;
  149. #endif // define BASIC_GAL_H