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.

1064 lines
28 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software: you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation, either version 3 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /********************************/
  22. /* Low level graphics routines */
  23. /********************************/
  24. #include <fctsys.h>
  25. #include <gr_basic.h>
  26. #include <common.h>
  27. #include <trigo.h>
  28. #include <macros.h>
  29. #include <base_struct.h>
  30. #include <base_screen.h>
  31. #include <bezier_curves.h>
  32. #include <math_for_graphics.h>
  33. #include <wx/graphics.h>
  34. #include <wx/tokenzr.h>
  35. #include <geometry/geometry_utils.h>
  36. static const bool FILLED = true;
  37. static const bool NOT_FILLED = false;
  38. /* Important Note:
  39. * These drawing functions clip draw item before send these items to wxDC draw
  40. * functions. For guy who asks why i did it, see a sample of problems encountered
  41. * when pixels
  42. * coordinates overflow 16 bits values:
  43. * http://trac.wxwidgets.org/ticket/10446
  44. * Problems can be found under Windows **and** Linux (mainly when drawing arcs)
  45. * (mainly at low zoom values (2, 1 or 0.5), in Pcbnew)
  46. * some of these problems could be now fixed in recent distributions.
  47. *
  48. * Currently (feb 2009) there are overflow problems when drawing solid (filled)
  49. * polygons under linux without clipping
  50. *
  51. * So before removing clipping functions, be aware these bug (they are not in
  52. * KiCad or wxWidgets) are fixed by testing how are drawn complex lines arcs
  53. * and solid polygons under Windows and Linux and remember users can have old
  54. * versions with bugs
  55. */
  56. /* Definitions for enabling and disabling debugging features in gr_basic.cpp.
  57. * Please remember to set these back to 0 before making LAUNCHPAD commits.
  58. */
  59. #define DEBUG_DUMP_CLIP_ERROR_COORDS 0 // Set to 1 to dump clip algorithm errors.
  60. #define DEBUG_DUMP_CLIP_COORDS 0 // Set to 1 to dump clipped coordinates.
  61. // For draw mode = XOR GR_XOR or GR_NXOR by background color
  62. GR_DRAWMODE g_XorMode = GR_NXOR;
  63. static void ClipAndDrawPoly( EDA_RECT* ClipBox, wxDC* DC, const wxPoint* Points, int n );
  64. /* These functions are used by corresponding functions
  65. * ( GRSCircle is called by GRCircle for instance) after mapping coordinates
  66. * from user units to screen units(pixels coordinates)
  67. */
  68. static void GRSRect( EDA_RECT* aClipBox, wxDC* aDC, int x1, int y1,
  69. int x2, int y2, int aWidth, COLOR4D aColor,
  70. wxPenStyle aStyle = wxPENSTYLE_SOLID );
  71. /**/
  72. static int GRLastMoveToX, GRLastMoveToY;
  73. static bool s_ForceBlackPen; /* if true: draws in black instead of
  74. * color for printing. */
  75. static int xcliplo = 0,
  76. ycliplo = 0,
  77. xcliphi = 2000,
  78. ycliphi = 2000;
  79. static COLOR4D s_DC_lastcolor( 0, 0, 0, 0 );
  80. static COLOR4D s_DC_lastbrushcolor( 0, 0, 0, 0 );
  81. static bool s_DC_lastbrushfill = false;
  82. static wxDC* s_DC_lastDC = NULL;
  83. static void WinClipAndDrawLine( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2, int width )
  84. {
  85. GRLastMoveToX = x2;
  86. GRLastMoveToY = y2;
  87. if( ClipBox )
  88. {
  89. EDA_RECT clipbox(*ClipBox);
  90. clipbox.Inflate(width/2);
  91. if( ClipLine( &clipbox, x1, y1, x2, y2 ) )
  92. return;
  93. }
  94. DC->DrawLine( x1, y1, x2, y2 );
  95. }
  96. /* Forcing a reset of the current pen.
  97. * Must be called after changing the graphical device before any trace.
  98. */
  99. void GRResetPenAndBrush( wxDC* DC )
  100. {
  101. GRSetBrush( DC, BLACK ); // Force no fill
  102. s_DC_lastbrushcolor = COLOR4D::UNSPECIFIED;
  103. s_DC_lastcolor = COLOR4D::UNSPECIFIED;
  104. s_DC_lastDC = NULL;
  105. }
  106. /**
  107. * Function GRSetColorPen
  108. * sets a pen style, width, color, and alpha into the given device context.
  109. */
  110. void GRSetColorPen( wxDC* DC, COLOR4D Color, int width, wxPenStyle style )
  111. {
  112. wxDash dots[2] = { 1, 3 };
  113. // Under OSX and while printing when wxPen is set to 0, renderer follows the request drawing
  114. // nothing & in the bitmap world the minimum is enough to light a pixel, in vectorial one not
  115. if( width <= 1 )
  116. width = DC->DeviceToLogicalXRel( 1 );
  117. if( s_ForceBlackPen )
  118. Color = COLOR4D::BLACK;
  119. const wxPen& curr_pen = DC->GetPen();
  120. if( !curr_pen.IsOk() || curr_pen.GetColour() != Color.ToColour()
  121. || curr_pen.GetWidth() != width
  122. || curr_pen.GetStyle() != style )
  123. {
  124. wxPen pen;
  125. pen.SetColour( Color.ToColour() );
  126. if( style == wxPENSTYLE_DOT )
  127. {
  128. style = wxPENSTYLE_USER_DASH;
  129. pen.SetDashes( 2, dots );
  130. }
  131. pen.SetWidth( width );
  132. pen.SetStyle( style );
  133. DC->SetPen( pen );
  134. }
  135. else
  136. // Should be not needed, but on Linux, in printing process
  137. // the curr pen settings needs to be sometimes re-initialized
  138. // Clearly, this is due to a bug, related to SetBrush(),
  139. // but we have to live with it, at least on wxWidgets 3.0
  140. DC->SetPen( curr_pen );
  141. }
  142. void GRSetBrush( wxDC* DC, COLOR4D Color, bool fill )
  143. {
  144. if( s_ForceBlackPen )
  145. Color = COLOR4D::BLACK;
  146. if( s_DC_lastbrushcolor != Color
  147. || s_DC_lastbrushfill != fill
  148. || s_DC_lastDC != DC )
  149. {
  150. wxBrush brush;
  151. brush.SetColour( Color.ToColour() );
  152. if( fill )
  153. brush.SetStyle( wxBRUSHSTYLE_SOLID );
  154. else
  155. brush.SetStyle( wxBRUSHSTYLE_TRANSPARENT );
  156. DC->SetBrush( brush );
  157. s_DC_lastbrushcolor = Color;
  158. s_DC_lastbrushfill = fill;
  159. s_DC_lastDC = DC;
  160. }
  161. }
  162. /**
  163. * Function GRForceBlackPen
  164. * @param flagforce True to force a black pen whenever the asked color
  165. */
  166. void GRForceBlackPen( bool flagforce )
  167. {
  168. s_ForceBlackPen = flagforce;
  169. }
  170. /**
  171. * Function GetGRForceBlackPenState
  172. * @return s_ForceBlackPen (True if a black pen was forced)
  173. */
  174. bool GetGRForceBlackPenState( void )
  175. {
  176. return s_ForceBlackPen;
  177. }
  178. void GRPutPixel( EDA_RECT* ClipBox, wxDC* DC, int x, int y, COLOR4D Color )
  179. {
  180. if( ClipBox && !ClipBox->Contains( x, y ) )
  181. return;
  182. GRSetColorPen( DC, Color );
  183. DC->DrawPoint( x, y );
  184. }
  185. /*
  186. * Draw a line, in object space.
  187. */
  188. void GRLine( EDA_RECT* ClipBox,
  189. wxDC* DC,
  190. int x1,
  191. int y1,
  192. int x2,
  193. int y2,
  194. int width,
  195. COLOR4D Color,
  196. wxPenStyle aStyle)
  197. {
  198. GRSetColorPen( DC, Color, width, aStyle );
  199. WinClipAndDrawLine( ClipBox, DC, x1, y1, x2, y2, width );
  200. GRLastMoveToX = x2;
  201. GRLastMoveToY = y2;
  202. }
  203. void GRLine( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aStart, wxPoint aEnd, int aWidth, COLOR4D aColor, wxPenStyle aStyle )
  204. {
  205. GRLine( aClipBox, aDC, aStart.x, aStart.y, aEnd.x, aEnd.y, aWidth, aColor, aStyle );
  206. }
  207. /*
  208. * Move to a new position, in object space.
  209. */
  210. void GRMoveTo( int x, int y )
  211. {
  212. GRLastMoveToX = x;
  213. GRLastMoveToY = y;
  214. }
  215. /*
  216. * Draw line to a new position, in object space.
  217. */
  218. void GRLineTo( EDA_RECT* ClipBox, wxDC* DC, int x, int y, int width, COLOR4D Color )
  219. {
  220. GRLine( ClipBox, DC, GRLastMoveToX, GRLastMoveToY, x, y, width, Color );
  221. }
  222. /**
  223. * Function GRLineArray
  224. * draws an array of lines (not a polygon).
  225. * @param aClipBox = the clip box
  226. * @param aDC = the device context into which drawing should occur.
  227. * @param aLines = a list of pair of coordinate in user space: a pair for each line.
  228. * @param aWidth = the width of each line.
  229. * @param aColor = color to draw the lines
  230. * @see COLOR4D
  231. */
  232. void GRLineArray( EDA_RECT* aClipBox, wxDC* aDC, std::vector<wxPoint>& aLines,
  233. int aWidth, COLOR4D aColor )
  234. {
  235. if( aLines.empty() )
  236. return;
  237. GRSetColorPen( aDC, aColor, aWidth );
  238. if( aClipBox )
  239. aClipBox->Inflate( aWidth / 2 );
  240. for( unsigned i = 0; i < aLines.size(); i += 2 )
  241. {
  242. int x1 = aLines[i].x;
  243. int y1 = aLines[i].y;
  244. int x2 = aLines[i + 1].x;
  245. int y2 = aLines[i + 1].y;
  246. if( ( aClipBox == NULL ) || !ClipLine( aClipBox, x1, y1, x2, y2 ) )
  247. aDC->DrawLine( x1, y1, x2, y2 );
  248. }
  249. GRMoveTo( aLines[aLines.size() - 1].x, aLines[aLines.size() - 1].y );
  250. if( aClipBox )
  251. aClipBox->Inflate(-aWidth/2);
  252. }
  253. // Draw the outline of a thick segment wih rounded ends
  254. void GRCSegm( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  255. int width, int aPenSize, COLOR4D Color )
  256. {
  257. GRLastMoveToX = x2;
  258. GRLastMoveToY = y2;
  259. if( ClipBox )
  260. {
  261. EDA_RECT clipbox(*ClipBox);
  262. clipbox.Inflate(width/2);
  263. if( ClipLine( &clipbox, x1, y1, x2, y2 ) )
  264. return;
  265. }
  266. if( width <= 2 ) /* single line or 2 pixels */
  267. {
  268. GRSetColorPen( DC, Color, width );
  269. DC->DrawLine( x1, y1, x2, y2 );
  270. return;
  271. }
  272. GRSetBrush( DC, Color, NOT_FILLED );
  273. GRSetColorPen( DC, Color, aPenSize );
  274. int radius = (width + 1) >> 1;
  275. int dx = x2 - x1;
  276. int dy = y2 - y1;
  277. double angle = -ArcTangente( dy, dx );
  278. wxPoint start;
  279. wxPoint end;
  280. wxPoint org( x1, y1);
  281. int len = (int) hypot( dx, dy );
  282. // We know if the DC is mirrored, to draw arcs
  283. int slx = DC->DeviceToLogicalX( 1 ) - DC->DeviceToLogicalX( 0 );
  284. int sly = DC->DeviceToLogicalY( 1 ) - DC->DeviceToLogicalY( 0 );
  285. bool mirrored = (slx > 0 && sly < 0) || (slx < 0 && sly > 0);
  286. // first edge
  287. start.x = 0;
  288. start.y = radius;
  289. end.x = len;
  290. end.y = radius;
  291. RotatePoint( &start, angle);
  292. RotatePoint( &end, angle);
  293. start += org;
  294. end += org;
  295. DC->DrawLine( start, end );
  296. // first rounded end
  297. end.x = 0;
  298. end.y = -radius;
  299. RotatePoint( &end, angle);
  300. end += org;
  301. if( !mirrored )
  302. DC->DrawArc( end, start, org );
  303. else
  304. DC->DrawArc( start, end, org );
  305. // second edge
  306. start.x = len;
  307. start.y = -radius;
  308. RotatePoint( &start, angle);
  309. start += org;
  310. DC->DrawLine( start, end );
  311. // second rounded end
  312. end.x = len;
  313. end.y = radius;
  314. RotatePoint( &end, angle);
  315. end += org;
  316. if( !mirrored )
  317. DC->DrawArc( end.x, end.y, start.x, start.y, x2, y2 );
  318. else
  319. DC->DrawArc( start.x, start.y, end.x, end.y, x2, y2 );
  320. }
  321. void GRCSegm( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  322. int width, COLOR4D Color )
  323. {
  324. GRCSegm( ClipBox, DC, x1, y1, x2, y2, width, 0, Color );
  325. }
  326. void GRCSegm( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aStart, wxPoint aEnd,
  327. int aWidth, COLOR4D aColor )
  328. {
  329. GRCSegm( aClipBox, aDC, aStart.x, aStart.y, aEnd.x, aEnd.y, aWidth, 0, aColor );
  330. }
  331. /*
  332. * Draw segment (full) with rounded ends in object space (real coords.).
  333. */
  334. void GRFillCSegm( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  335. int width, COLOR4D Color )
  336. {
  337. GRSetColorPen( DC, Color, width );
  338. WinClipAndDrawLine( ClipBox, DC, x1, y1, x2, y2, width );
  339. }
  340. void GRFilledSegment( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aStart, wxPoint aEnd,
  341. int aWidth, COLOR4D aColor )
  342. {
  343. GRSetColorPen( aDC, aColor, aWidth );
  344. WinClipAndDrawLine( aClipBox, aDC, aStart.x, aStart.y, aEnd.x, aEnd.y, aWidth );
  345. }
  346. static bool IsGRSPolyDrawable( EDA_RECT* ClipBox, int n, const wxPoint* Points, )
  347. {
  348. if( !ClipBox )
  349. return true;
  350. if( n <= 0 )
  351. return false;
  352. int Xmin, Xmax, Ymin, Ymax;
  353. Xmin = Xmax = Points[0].x;
  354. Ymin = Ymax = Points[0].y;
  355. for( int ii = 1; ii < n; ii++ ) // calculate rectangle
  356. {
  357. Xmin = std::min( Xmin, Points[ii].x );
  358. Xmax = std::max( Xmax, Points[ii].x );
  359. Ymin = std::min( Ymin, Points[ii].y );
  360. Ymax = std::max( Ymax, Points[ii].y );
  361. }
  362. xcliplo = ClipBox->GetX();
  363. ycliplo = ClipBox->GetY();
  364. xcliphi = ClipBox->GetRight();
  365. ycliphi = ClipBox->GetBottom();
  366. if( Xmax < xcliplo )
  367. return false;
  368. if( Xmin > xcliphi )
  369. return false;
  370. if( Ymax < ycliplo )
  371. return false;
  372. if( Ymin > ycliphi )
  373. return false;
  374. return true;
  375. }
  376. /*
  377. * Draw a new polyline and fill it if Fill, in screen space.
  378. */
  379. static void GRSPoly( EDA_RECT* ClipBox, wxDC* DC, int n, const wxPoint* Points, bool Fill,
  380. int width, COLOR4D Color, COLOR4D BgColor )
  381. {
  382. if( !IsGRSPolyDrawable( ClipBox, n, Points ) )
  383. return;
  384. if( Fill && ( n > 2 ) )
  385. {
  386. GRSetBrush( DC, BgColor, FILLED );
  387. GRSetColorPen( DC, Color, width );
  388. /* clip before send the filled polygon to wxDC, because under linux
  389. * (GTK?) polygons having large coordinates are incorrectly drawn
  390. * (integer overflow in coordinates, I am guessing)
  391. */
  392. ClipAndDrawPoly( ClipBox, DC, Points, n );
  393. }
  394. else
  395. {
  396. GRMoveTo( Points[0].x, Points[0].y );
  397. for( int i = 1; i < n; ++i )
  398. {
  399. GRLineTo( ClipBox, DC, Points[i].x, Points[i].y, width, Color );
  400. }
  401. }
  402. }
  403. /*
  404. * Draw a new closed polyline and fill it if Fill, in screen space.
  405. */
  406. static void GRSClosedPoly( EDA_RECT* aClipBox, wxDC* aDC, int aPointCount, const wxPoint* aPoints,
  407. bool aFill, int aWidth, COLOR4D aColor, COLOR4D aBgColor )
  408. {
  409. if( !IsGRSPolyDrawable( aClipBox, aPointCount, aPoints ) )
  410. return;
  411. if( aFill && ( aPointCount > 2 ) )
  412. {
  413. GRLastMoveToX = aPoints[aPointCount - 1].x;
  414. GRLastMoveToY = aPoints[aPointCount - 1].y;
  415. GRSetBrush( aDC, aBgColor, FILLED );
  416. GRSetColorPen( aDC, aColor, aWidth );
  417. ClipAndDrawPoly( aClipBox, aDC, aPoints, aPointCount );
  418. }
  419. else
  420. {
  421. GRMoveTo( aPoints[0].x, aPoints[0].y );
  422. for( int i = 1; i < aPointCount; ++i )
  423. {
  424. GRLineTo( aClipBox, aDC, aPoints[i].x, aPoints[i].y, aWidth, aColor );
  425. }
  426. int lastpt = aPointCount - 1;
  427. // Close the polygon
  428. if( aPoints[lastpt] != aPoints[0] )
  429. {
  430. GRLineTo( aClipBox, aDC, aPoints[0].x, aPoints[0].y, aWidth, aColor );
  431. }
  432. }
  433. }
  434. /*
  435. * Draw a new polyline and fill it if Fill, in drawing space.
  436. */
  437. void GRPoly( EDA_RECT* ClipBox, wxDC* DC, int n, const wxPoint* Points, bool Fill, int width,
  438. COLOR4D Color, COLOR4D BgColor )
  439. {
  440. GRSPoly( ClipBox, DC, n, Points, Fill, width, Color, BgColor );
  441. }
  442. /*
  443. * Draw a closed polyline and fill it if Fill, in object space.
  444. */
  445. void GRClosedPoly( EDA_RECT* ClipBox, wxDC* DC, int n, const wxPoint* Points, bool Fill,
  446. COLOR4D Color, COLOR4D BgColor )
  447. {
  448. GRClosedPoly( ClipBox, DC, n, Points, Fill, 0, Color, BgColor );
  449. }
  450. void GRClosedPoly( EDA_RECT* ClipBox, wxDC* DC, int n, const wxPoint* Points, bool Fill, int width,
  451. COLOR4D Color, COLOR4D BgColor )
  452. {
  453. GRSClosedPoly( ClipBox, DC, n, Points, Fill, width, Color, BgColor );
  454. }
  455. static bool clipCircle( EDA_RECT* aClipBox, int xc, int yc, int r, int aWidth )
  456. {
  457. // Clip circles that are outside the ClipBox.
  458. if( aClipBox )
  459. {
  460. int x0, y0, xm, ym;
  461. x0 = aClipBox->GetX();
  462. y0 = aClipBox->GetY();
  463. xm = aClipBox->GetRight();
  464. ym = aClipBox->GetBottom();
  465. r += aWidth;
  466. if( xc < ( x0 - r ) )
  467. return true;
  468. if( yc < ( y0 - r ) )
  469. return true;
  470. if( xc > ( r + xm ) )
  471. return true;
  472. if( yc > ( r + ym ) )
  473. return true;
  474. }
  475. return false;
  476. }
  477. void GRCircle( EDA_RECT* ClipBox, wxDC* DC, int xc, int yc, int r, int width, COLOR4D Color )
  478. {
  479. if( clipCircle( ClipBox, xc, yc, r, width ) || r <= 0 )
  480. return;
  481. GRSetBrush( DC, Color, NOT_FILLED );
  482. GRSetColorPen( DC, Color, width );
  483. DC->DrawEllipse( xc - r, yc - r, r + r, r + r );
  484. }
  485. void GRCircle( EDA_RECT* ClipBox, wxDC* DC, int x, int y, int r, COLOR4D Color )
  486. {
  487. GRCircle( ClipBox, DC, x, y, r, 0, Color );
  488. }
  489. void GRCircle( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aPos, int aRadius, int aWidth, COLOR4D aColor )
  490. {
  491. GRCircle( aClipBox, aDC, aPos.x, aPos.y, aRadius, aWidth, aColor );
  492. }
  493. void GRFilledCircle( EDA_RECT* ClipBox, wxDC* DC, int x, int y, int r,
  494. int width, COLOR4D Color, COLOR4D BgColor )
  495. {
  496. if( clipCircle( ClipBox, x, y, r, width ) || r <= 0 )
  497. return;
  498. GRSetBrush( DC, BgColor, FILLED );
  499. GRSetColorPen( DC, Color, width );
  500. DC->DrawEllipse( x - r, y - r, r + r, r + r );
  501. }
  502. void GRFilledCircle( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aPos, int aRadius, COLOR4D aColor )
  503. {
  504. GRFilledCircle( aClipBox, aDC, aPos.x, aPos.y, aRadius, 0, aColor, aColor );
  505. }
  506. /*
  507. * Draw an arc in user space.
  508. */
  509. void GRArc1( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  510. int xc, int yc, COLOR4D Color )
  511. {
  512. GRArc1( ClipBox, DC, x1, y1, x2, y2, xc, yc, 0, Color );
  513. }
  514. /*
  515. * Draw an arc, width = width in user space.
  516. */
  517. void GRArc1( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  518. int xc, int yc, int width, COLOR4D Color )
  519. {
  520. /* Clip arcs off screen. */
  521. if( ClipBox )
  522. {
  523. int x0, y0, xm, ym, r;
  524. x0 = ClipBox->GetX();
  525. y0 = ClipBox->GetY();
  526. xm = ClipBox->GetRight();
  527. ym = ClipBox->GetBottom();
  528. r = KiROUND( Distance( x1, y1, xc, yc ) );
  529. if( xc < ( x0 - r ) )
  530. return;
  531. if( yc < ( y0 - r ) )
  532. return;
  533. if( xc > ( r + xm ) )
  534. return;
  535. if( yc > ( r + ym ) )
  536. return;
  537. }
  538. GRSetBrush( DC, Color );
  539. GRSetColorPen( DC, Color, width );
  540. DC->DrawArc( x1, y1, x2, y2, xc, yc );
  541. }
  542. void GRArc1( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aStart, wxPoint aEnd,
  543. wxPoint aCenter, int aWidth, COLOR4D aColor )
  544. {
  545. GRArc1( aClipBox, aDC, aStart.x, aStart.y, aEnd.x, aEnd.y, aCenter.x, aCenter.y,
  546. aWidth, aColor );
  547. }
  548. /*
  549. * Draw a filled arc in drawing space.
  550. */
  551. void GRFilledArc( EDA_RECT* ClipBox,
  552. wxDC* DC,
  553. int x,
  554. int y,
  555. double StAngle,
  556. double EndAngle,
  557. int r,
  558. int width,
  559. COLOR4D Color,
  560. COLOR4D BgColor )
  561. {
  562. int x1, y1, x2, y2;
  563. /* Clip arcs off screen */
  564. if( ClipBox )
  565. {
  566. int x0, y0, xm, ym;
  567. x0 = ClipBox->GetX();
  568. y0 = ClipBox->GetY();
  569. xm = ClipBox->GetRight();
  570. ym = ClipBox->GetBottom();
  571. if( x < ( x0 - r - 1 ) )
  572. return;
  573. if( y < ( y0 - r - 1 ) )
  574. return;
  575. if( x > ( r + xm + 1 ) )
  576. return;
  577. if( y > ( r + ym + 1 ) )
  578. return;
  579. }
  580. x1 = r;
  581. y1 = 0;
  582. RotatePoint( &x1, &y1, EndAngle );
  583. x2 = r;
  584. y2 = 0;
  585. RotatePoint( &x2, &y2, StAngle );
  586. GRSetBrush( DC, BgColor, FILLED );
  587. GRSetColorPen( DC, Color, width );
  588. DC->DrawArc( x + x1, y - y1, x + x2, y - y2, x, y );
  589. }
  590. void GRFilledArc( EDA_RECT* ClipBox, wxDC* DC, int x, int y,
  591. double StAngle, double EndAngle, int r,
  592. COLOR4D Color, COLOR4D BgColor )
  593. {
  594. GRFilledArc( ClipBox, DC, x, y, StAngle, EndAngle, r, 0, Color, BgColor );
  595. }
  596. /*
  597. * Draw an arc in drawing space.
  598. */
  599. void GRArc( EDA_RECT* ClipBox, wxDC* DC, int xc, int yc, double StAngle,
  600. double EndAngle, int r, COLOR4D Color )
  601. {
  602. int x1, y1, x2, y2;
  603. /* Clip arcs off screen */
  604. if( ClipBox )
  605. {
  606. int radius = r + 1;
  607. int x0, y0, xm, ym, x, y;
  608. x0 = ClipBox->GetX();
  609. y0 = ClipBox->GetY();
  610. xm = ClipBox->GetRight();
  611. ym = ClipBox->GetBottom();
  612. x = xc;
  613. y = yc;
  614. if( x < ( x0 - radius ) )
  615. return;
  616. if( y < ( y0 - radius ) )
  617. return;
  618. if( x > ( xm + radius ) )
  619. return;
  620. if( y > ( ym + radius ) )
  621. return;
  622. }
  623. x1 = r;
  624. y1 = 0;
  625. RotatePoint( &x1, &y1, EndAngle );
  626. x2 = r;
  627. y2 = 0;
  628. RotatePoint( &x2, &y2, StAngle );
  629. GRSetBrush( DC, Color, NOT_FILLED );
  630. GRSetColorPen( DC, Color );
  631. DC->DrawArc( xc + x1, yc - y1, xc + x2, yc - y2, xc, yc );
  632. }
  633. /*
  634. * Draw an arc with width = width in drawing space.
  635. */
  636. void GRArc( EDA_RECT* ClipBox,
  637. wxDC* DC,
  638. int x,
  639. int y,
  640. double StAngle,
  641. double EndAngle,
  642. int r,
  643. int width,
  644. COLOR4D Color )
  645. {
  646. int x1, y1, x2, y2;
  647. /* Clip arcs off screen. */
  648. if( ClipBox )
  649. {
  650. int x0, y0, xm, ym;
  651. x0 = ClipBox->GetX();
  652. y0 = ClipBox->GetY();
  653. xm = ClipBox->GetRight();
  654. ym = ClipBox->GetBottom();
  655. if( x < ( x0 - r - width ) )
  656. return;
  657. if( y < ( y0 - r - width ) )
  658. return;
  659. if( x > ( r + xm + width ) )
  660. return;
  661. if( y > ( r + ym + width ) )
  662. return;
  663. }
  664. x1 = r;
  665. y1 = 0;
  666. RotatePoint( &x1, &y1, EndAngle );
  667. x2 = r;
  668. y2 = 0;
  669. RotatePoint( &x2, &y2, StAngle );
  670. GRSetBrush( DC, Color );
  671. GRSetColorPen( DC, Color, width );
  672. DC->DrawArc( x + x1, y - y1, x + x2, y - y2, x, y );
  673. }
  674. /*
  675. * Draw a rectangle in drawing space.
  676. */
  677. void GRRect( EDA_RECT* aClipBox, wxDC* aDC, int x1, int y1, int x2, int y2, COLOR4D aColor )
  678. {
  679. GRSRect( aClipBox, aDC, x1, y1, x2, y2, 0, aColor );
  680. }
  681. void GRRectPs( EDA_RECT* aClipBox, wxDC* aDC, const EDA_RECT& aRect, COLOR4D aColor, wxPenStyle aStyle )
  682. {
  683. int x1 = aRect.GetX();
  684. int y1 = aRect.GetY();
  685. int x2 = aRect.GetRight();
  686. int y2 = aRect.GetBottom();
  687. GRSRect( aClipBox, aDC, x1, y1, x2, y2, 0, aColor, aStyle );
  688. }
  689. /*
  690. * Draw a rectangle (thick lines) in drawing space.
  691. */
  692. void GRRect( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2, int width, COLOR4D Color )
  693. {
  694. GRSRect( ClipBox, DC, x1, y1, x2, y2, width, Color );
  695. }
  696. void GRRect( EDA_RECT* aClipBox, wxDC* aDC, const EDA_RECT& aRect, int aWidth, COLOR4D aColor )
  697. {
  698. int x1 = aRect.GetX();
  699. int y1 = aRect.GetY();
  700. int x2 = aRect.GetRight();
  701. int y2 = aRect.GetBottom();
  702. GRSRect( aClipBox, aDC, x1, y1, x2, y2, aWidth, aColor );
  703. }
  704. /*
  705. * Draw a rectangle (filled with AreaColor) in drawing space.
  706. */
  707. void GRFilledRect( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  708. COLOR4D Color, COLOR4D BgColor )
  709. {
  710. GRSFilledRect( ClipBox, DC, x1, y1, x2, y2, 0, Color, BgColor );
  711. }
  712. /*
  713. * Draw a rectangle (filled with AreaColor) in drawing space.
  714. */
  715. void GRFilledRect( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  716. int width, COLOR4D Color, COLOR4D BgColor )
  717. {
  718. GRSFilledRect( ClipBox, DC, x1, y1, x2, y2, width, Color, BgColor );
  719. }
  720. /*
  721. * Draw a rectangle in screen space.
  722. */
  723. void GRSRect( EDA_RECT* aClipBox, wxDC* aDC, int x1, int y1, int x2, int y2,
  724. int aWidth, COLOR4D aColor, wxPenStyle aStyle )
  725. {
  726. wxPoint points[5];
  727. points[0] = wxPoint(x1, y1);
  728. points[1] = wxPoint(x1, y2);
  729. points[2] = wxPoint(x2, y2);
  730. points[3] = wxPoint(x2, y1);
  731. points[4] = points[0];
  732. GRSClosedPoly( aClipBox, aDC, 5, points, NOT_FILLED, aWidth,
  733. aColor, aColor );
  734. }
  735. void GRSFilledRect( EDA_RECT* aClipBox, wxDC* aDC, int x1, int y1, int x2, int y2,
  736. int aWidth, COLOR4D aColor, COLOR4D aBgColor )
  737. {
  738. wxPoint points[5];
  739. points[0] = wxPoint(x1, y1);
  740. points[1] = wxPoint(x1, y2);
  741. points[2] = wxPoint(x2, y2);
  742. points[3] = wxPoint(x2, y1);
  743. points[4] = points[0];
  744. GRSetBrush( aDC, aBgColor, FILLED );
  745. GRSetColorPen( aDC, aBgColor, aWidth );
  746. if( aClipBox && (aWidth > 0) )
  747. {
  748. EDA_RECT clipbox(*aClipBox);
  749. clipbox.Inflate(aWidth);
  750. ClipAndDrawPoly(&clipbox, aDC, points, 5); // polygon approach is more accurate
  751. }
  752. else
  753. ClipAndDrawPoly(aClipBox, aDC, points, 5 );
  754. }
  755. /**
  756. * Function ClipAndDrawPoly
  757. * Used to clip a polygon and draw it as Filled Polygon
  758. * uses the Sutherland and Hodgman algo to clip the given poly against a
  759. * rectangle. This rectangle is the drawing area this is useful under
  760. * Linux (2009) because filled polygons are incorrectly drawn if they have
  761. * too large coordinates (seems due to integer overflows in calculations)
  762. * Could be removed in some years, if become unnecessary.
  763. */
  764. /* Note: aClipBox == NULL is legal, so if aClipBox == NULL,
  765. * the polygon is drawn, but not clipped
  766. */
  767. #include <SutherlandHodgmanClipPoly.h>
  768. void ClipAndDrawPoly( EDA_RECT* aClipBox, wxDC* aDC, const wxPoint* Points, int n )
  769. {
  770. if( aClipBox == NULL )
  771. {
  772. aDC->DrawPolygon( n, aPoints );
  773. return;
  774. }
  775. // A clip box exists: clip and draw the polygon.
  776. static std::vector<wxPoint> clippedPolygon;
  777. static pointVector inputPolygon, outputPolygon;
  778. inputPolygon.clear();
  779. outputPolygon.clear();
  780. clippedPolygon.clear();
  781. for( int ii = 0; ii < n; ii++ )
  782. inputPolygon.push_back( PointF( (REAL) aPoints[ii].x, (REAL) aPoints[ii].y ) );
  783. RectF window( (REAL) aClipBox->GetX(), (REAL) aClipBox->GetY(),
  784. (REAL) aClipBox->GetWidth(), (REAL) aClipBox->GetHeight() );
  785. SutherlandHodgman sh( window );
  786. sh.Clip( inputPolygon, outputPolygon );
  787. for( cpointIterator cit = outputPolygon.begin(); cit != outputPolygon.end(); ++cit )
  788. {
  789. clippedPolygon.emplace_back( KiROUND( cit->X ), KiROUND( cit->Y ) );
  790. }
  791. if( clippedPolygon.size() )
  792. aDC->DrawPolygon( clippedPolygon.size(), &clippedPolygon[0] );
  793. }
  794. void GRBezier( EDA_RECT* aClipBox, wxDC* aDC,
  795. std::vector<wxPoint>& aPoint,
  796. int aWidth, COLOR4D aColor )
  797. {
  798. std::vector<wxPoint> output;
  799. BEZIER_POLY converter( aPoint );
  800. converter.GetPoly( output, aWidth );
  801. GRPoly( aClipBox, aDC, output.size(), &output[0], false, aWidth, aColor, aColor );
  802. }
  803. void GRDrawAnchor( EDA_RECT *aClipBox, wxDC *aDC, int x, int y,
  804. int aSize, COLOR4D aColor )
  805. {
  806. int anchor_size = aDC->DeviceToLogicalXRel( aSize );
  807. GRLine( aClipBox, aDC,
  808. x - anchor_size, y,
  809. x + anchor_size, y, 0, aColor );
  810. GRLine( aClipBox, aDC,
  811. x, y - anchor_size,
  812. x, y + anchor_size, 0, aColor );
  813. }
  814. void GRDrawWrappedText( wxDC& aDC, wxString const& aText )
  815. {
  816. wxStringTokenizer tokenizer( aText, " " );
  817. wxSize const dc_size = aDC.GetSize();
  818. wxSize const margin = aDC.GetTextExtent( " " );
  819. std::vector<wxString> lines;
  820. wxString line_accumulator;
  821. int total_height = 0;
  822. while( tokenizer.HasMoreTokens() )
  823. {
  824. wxString word = tokenizer.GetNextToken();
  825. wxSize linesize = aDC.GetTextExtent( line_accumulator + " " + word );
  826. if( linesize.x >= dc_size.x - margin.x && !line_accumulator.IsEmpty() )
  827. {
  828. lines.push_back( line_accumulator );
  829. line_accumulator = word;
  830. }
  831. else
  832. {
  833. line_accumulator += " ";
  834. line_accumulator += word;
  835. }
  836. }
  837. if( !line_accumulator.IsEmpty() )
  838. {
  839. lines.push_back( line_accumulator );
  840. }
  841. for( auto const& line: lines )
  842. {
  843. wxSize linesize = aDC.GetTextExtent( line );
  844. total_height += linesize.y;
  845. }
  846. int top = ( dc_size.y - total_height ) / 2;
  847. int pos = top;
  848. for( auto const& line: lines )
  849. {
  850. wxSize linesize = aDC.GetTextExtent( line );
  851. aDC.DrawText( line, ( dc_size.x - linesize.x ) / 2, pos );
  852. pos += linesize.y;
  853. }
  854. }