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.

155 lines
4.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2018 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. /**
  25. * @brief Implement a very basic GAL used to draw, plot and convert texts in segments
  26. * for DRC functions, using the common GAL functions.
  27. * Draw functions use wxDC.
  28. * Plot functions use a PLOTTER
  29. * Convert texts in segments use a callback function created by the caller
  30. * @file basic_gal.cpp
  31. */
  32. #include <gr_basic.h>
  33. #include <plotter.h>
  34. #include <trigo.h>
  35. #include <basic_gal.h>
  36. using namespace KIGFX;
  37. KIGFX::GAL_DISPLAY_OPTIONS basic_displayOptions;
  38. // the basic GAL doesn't get an external display option object
  39. BASIC_GAL basic_gal( basic_displayOptions );
  40. const VECTOR2D BASIC_GAL::transform( const VECTOR2D& aPoint ) const
  41. {
  42. VECTOR2D point = aPoint + m_transform.m_moveOffset - m_transform.m_rotCenter;
  43. point = point.Rotate( m_transform.m_rotAngle ) + m_transform.m_rotCenter;
  44. return point;
  45. }
  46. // Draws a polyline given a list of points already transformed into the local coordinate
  47. // system.
  48. void BASIC_GAL::doDrawPolyline( const std::vector<wxPoint>& aLocalPointList )
  49. {
  50. if( m_DC )
  51. {
  52. if( m_isFillEnabled )
  53. {
  54. GRPoly( m_isClipped ? &m_clipBox : NULL, m_DC, aLocalPointList.size(),
  55. &aLocalPointList[0], 0, GetLineWidth(), m_Color, m_Color );
  56. }
  57. else
  58. {
  59. for( unsigned ii = 1; ii < aLocalPointList.size(); ++ii )
  60. {
  61. GRCSegm( m_isClipped ? &m_clipBox : NULL, m_DC, aLocalPointList[ ii - 1],
  62. aLocalPointList[ii], GetLineWidth(), m_Color );
  63. }
  64. }
  65. }
  66. else if( m_plotter )
  67. {
  68. m_plotter->MoveTo( aLocalPointList[0] );
  69. for( unsigned ii = 1; ii < aLocalPointList.size(); ii++ )
  70. {
  71. m_plotter->LineTo( aLocalPointList[ii] );
  72. }
  73. m_plotter->PenFinish();
  74. }
  75. else if( m_callback )
  76. {
  77. for( unsigned ii = 1; ii < aLocalPointList.size(); ii++ )
  78. {
  79. m_callback( aLocalPointList[ ii - 1].x, aLocalPointList[ ii - 1].y,
  80. aLocalPointList[ii].x, aLocalPointList[ii].y, m_callbackData );
  81. }
  82. }
  83. }
  84. void BASIC_GAL::DrawPolyline( const std::deque<VECTOR2D>& aPointList )
  85. {
  86. if( aPointList.size() < 2 )
  87. return;
  88. std::vector<wxPoint> polyline_corners;
  89. for( const VECTOR2D& pt : aPointList )
  90. polyline_corners.emplace_back( (wxPoint) transform( pt ) );
  91. doDrawPolyline( polyline_corners );
  92. }
  93. void BASIC_GAL::DrawPolyline( const VECTOR2D aPointList[], int aListSize )
  94. {
  95. if( aListSize < 2 )
  96. return;
  97. std::vector<wxPoint> polyline_corners;
  98. for( int ii = 0; ii < aListSize; ++ii )
  99. polyline_corners.emplace_back( (wxPoint) transform( aPointList[ ii ] ) );
  100. doDrawPolyline( polyline_corners );
  101. }
  102. void BASIC_GAL::DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
  103. {
  104. VECTOR2D startVector = transform( aStartPoint );
  105. VECTOR2D endVector = transform( aEndPoint );
  106. if( m_DC )
  107. {
  108. if( m_isFillEnabled )
  109. {
  110. GRLine( m_isClipped ? &m_clipBox : NULL, m_DC, startVector.x, startVector.y,
  111. endVector.x, endVector.y, GetLineWidth(), m_Color );
  112. }
  113. else
  114. {
  115. GRCSegm( m_isClipped ? &m_clipBox : NULL, m_DC, startVector.x, startVector.y,
  116. endVector.x, endVector.y, GetLineWidth(), 0, m_Color );
  117. }
  118. }
  119. else if( m_plotter )
  120. {
  121. m_plotter->MoveTo( wxPoint( startVector.x, startVector.y ) );
  122. m_plotter->LineTo( wxPoint( endVector.x, endVector.y ) );
  123. m_plotter->PenFinish();
  124. }
  125. else if( m_callback )
  126. {
  127. m_callback( startVector.x, startVector.y,
  128. endVector.x, endVector.y, m_callbackData );
  129. }
  130. }