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.

170 lines
5.0 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-2017 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. * BASIC_GAL is a minimal GAL implementation to draw, plot and convert
  33. * stroke texts to a set of segments 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. wxDC* m_DC;
  55. COLOR4D m_Color;
  56. private:
  57. TRANSFORM_PRM m_transform;
  58. std::stack <TRANSFORM_PRM> m_transformHistory;
  59. public:
  60. BASIC_GAL( KIGFX::GAL_DISPLAY_OPTIONS& aDisplayOptions ) :
  61. GAL( aDisplayOptions )
  62. {
  63. m_DC = NULL;
  64. m_Color = RED;
  65. m_plotter = NULL;
  66. m_callback = NULL;
  67. m_callbackData = nullptr;
  68. m_isClipped = false;
  69. }
  70. void SetPlotter( PLOTTER* aPlotter )
  71. {
  72. m_plotter = aPlotter;
  73. }
  74. void SetCallback( void (* aCallback)( int x0, int y0, int xf, int yf, void* aData ), void* aData )
  75. {
  76. m_callback = aCallback;
  77. m_callbackData = aData;
  78. }
  79. /// Set a clip box for drawings
  80. /// If NULL, no clip will be made
  81. void SetClipBox( EDA_RECT* aClipBox )
  82. {
  83. m_isClipped = aClipBox != NULL;
  84. if( aClipBox )
  85. m_clipBox = *aClipBox;
  86. }
  87. /// @brief Save the context.
  88. virtual void Save() override
  89. {
  90. m_transformHistory.push( m_transform );
  91. }
  92. virtual void Restore() override
  93. {
  94. m_transform = m_transformHistory.top();
  95. m_transformHistory.pop();
  96. }
  97. /**
  98. * @brief Draw a polyline
  99. * @param aPointList is a list of 2D-Vectors containing the polyline points.
  100. */
  101. virtual void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
  102. /** Start and end points are defined as 2D-Vectors.
  103. * @param aStartPoint is the start point of the line.
  104. * @param aEndPoint is the end point of the line.
  105. */
  106. virtual void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
  107. /**
  108. * @brief Translate the context.
  109. *
  110. * @param aTranslation is the translation vector.
  111. */
  112. virtual void Translate( const VECTOR2D& aTranslation ) override
  113. {
  114. m_transform.m_moveOffset += aTranslation;
  115. }
  116. /**
  117. * @brief Rotate the context.
  118. *
  119. * @param aAngle is the rotation angle in radians.
  120. */
  121. virtual void Rotate( double aAngle ) override
  122. {
  123. m_transform.m_rotAngle = aAngle;
  124. m_transform.m_rotCenter = m_transform.m_moveOffset;
  125. }
  126. private:
  127. // Apply the roation/translation transform to aPoint
  128. const VECTOR2D transform( const VECTOR2D& aPoint ) const;
  129. // A clip box, to clip drawings in a wxDC (mandatory to avoid draw issues)
  130. EDA_RECT m_clipBox; // The clip box
  131. bool m_isClipped; // Allows/disallows clipping
  132. // When calling the draw functions outside a wxDC, to get the basic drawings
  133. // lines / polylines ..., a callback function (used in DRC) to store
  134. // coordinates of each segment:
  135. void (* m_callback)( int x0, int y0, int xf, int yf, void* aData );
  136. void* m_callbackData; // a optional parameter for m_callback
  137. // When calling the draw functions for plot, the plotter acts as a wxDC
  138. // to plot basic items
  139. PLOTTER* m_plotter;
  140. };
  141. extern BASIC_GAL basic_gal;
  142. #endif // define BASIC_GAL_H