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.

152 lines
4.7 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-2021 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. * 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 <plotters/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. void BASIC_GAL::doDrawPolyline( const std::vector<wxPoint>& aLocalPointList )
  47. {
  48. if( m_DC )
  49. {
  50. if( m_isFillEnabled )
  51. {
  52. GRPoly( m_isClipped ? &m_clipBox : nullptr, m_DC, aLocalPointList.size(),
  53. &aLocalPointList[0], 0, GetLineWidth(), m_Color, m_Color );
  54. }
  55. else
  56. {
  57. for( unsigned ii = 1; ii < aLocalPointList.size(); ++ii )
  58. {
  59. GRCSegm( m_isClipped ? &m_clipBox : nullptr, m_DC, aLocalPointList[ ii - 1],
  60. aLocalPointList[ii], GetLineWidth(), m_Color );
  61. }
  62. }
  63. }
  64. else if( m_plotter )
  65. {
  66. m_plotter->MoveTo( aLocalPointList[0] );
  67. for( unsigned ii = 1; ii < aLocalPointList.size(); ii++ )
  68. {
  69. m_plotter->LineTo( aLocalPointList[ii] );
  70. }
  71. m_plotter->PenFinish();
  72. }
  73. else if( m_callback )
  74. {
  75. for( unsigned ii = 1; ii < aLocalPointList.size(); ii++ )
  76. {
  77. m_callback( aLocalPointList[ ii - 1].x, aLocalPointList[ ii - 1].y,
  78. aLocalPointList[ii].x, aLocalPointList[ii].y, m_callbackData );
  79. }
  80. }
  81. }
  82. void BASIC_GAL::DrawPolyline( const std::deque<VECTOR2D>& aPointList )
  83. {
  84. if( aPointList.size() < 2 )
  85. return;
  86. std::vector<wxPoint> polyline_corners;
  87. for( const VECTOR2D& pt : aPointList )
  88. polyline_corners.emplace_back( (wxPoint) transform( pt ) );
  89. doDrawPolyline( polyline_corners );
  90. }
  91. void BASIC_GAL::DrawPolyline( const VECTOR2D aPointList[], int aListSize )
  92. {
  93. if( aListSize < 2 )
  94. return;
  95. std::vector<wxPoint> polyline_corners;
  96. for( int ii = 0; ii < aListSize; ++ii )
  97. polyline_corners.emplace_back( (wxPoint) transform( aPointList[ ii ] ) );
  98. doDrawPolyline( polyline_corners );
  99. }
  100. void BASIC_GAL::DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
  101. {
  102. VECTOR2D startVector = transform( aStartPoint );
  103. VECTOR2D endVector = transform( aEndPoint );
  104. if( m_DC )
  105. {
  106. if( m_isFillEnabled )
  107. {
  108. GRLine( m_isClipped ? &m_clipBox : nullptr, m_DC, startVector.x, startVector.y,
  109. endVector.x, endVector.y, GetLineWidth(), m_Color );
  110. }
  111. else
  112. {
  113. GRCSegm( m_isClipped ? &m_clipBox : nullptr, m_DC, startVector.x, startVector.y,
  114. endVector.x, endVector.y, GetLineWidth(), 0, m_Color );
  115. }
  116. }
  117. else if( m_plotter )
  118. {
  119. m_plotter->MoveTo( wxPoint( startVector.x, startVector.y ) );
  120. m_plotter->LineTo( wxPoint( endVector.x, endVector.y ) );
  121. m_plotter->PenFinish();
  122. }
  123. else if( m_callback )
  124. {
  125. m_callback( startVector.x, startVector.y, endVector.x, endVector.y, m_callbackData );
  126. }
  127. }