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.

1098 lines
29 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
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 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, wxPoint Points[],
  64. int n );
  65. /* These functions are used by corresponding functions
  66. * ( GRSCircle is called by GRCircle for instance) after mapping coordinates
  67. * from user units to screen units(pixels coordinates)
  68. */
  69. static void GRSRect( EDA_RECT* aClipBox, wxDC* aDC, int x1, int y1,
  70. int x2, int y2, int aWidth, COLOR4D aColor,
  71. wxPenStyle aStyle = wxPENSTYLE_SOLID );
  72. /**/
  73. static int GRLastMoveToX, GRLastMoveToY;
  74. static bool s_ForceBlackPen; /* if true: draws in black instead of
  75. * color for printing. */
  76. static int xcliplo = 0,
  77. ycliplo = 0,
  78. xcliphi = 2000,
  79. ycliphi = 2000;
  80. static COLOR4D s_DC_lastcolor( 0, 0, 0, 0 );
  81. static COLOR4D s_DC_lastbrushcolor( 0, 0, 0, 0 );
  82. static bool s_DC_lastbrushfill = false;
  83. static wxDC* s_DC_lastDC = NULL;
  84. static void WinClipAndDrawLine( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2, int width )
  85. {
  86. GRLastMoveToX = x2;
  87. GRLastMoveToY = y2;
  88. if( ClipBox )
  89. {
  90. EDA_RECT clipbox(*ClipBox);
  91. clipbox.Inflate(width/2);
  92. if( ClipLine( &clipbox, x1, y1, x2, y2 ) )
  93. return;
  94. }
  95. DC->DrawLine( x1, y1, x2, y2 );
  96. }
  97. /* Forcing a reset of the current pen.
  98. * Must be called after changing the graphical device before any trace.
  99. */
  100. void GRResetPenAndBrush( wxDC* DC )
  101. {
  102. GRSetBrush( DC, BLACK ); // Force no fill
  103. s_DC_lastbrushcolor = COLOR4D::UNSPECIFIED;
  104. s_DC_lastcolor = COLOR4D::UNSPECIFIED;
  105. s_DC_lastDC = NULL;
  106. }
  107. /**
  108. * Function GRSetColorPen
  109. * sets a pen style, width, color, and alpha into the given device context.
  110. */
  111. void GRSetColorPen( wxDC* DC, COLOR4D Color, int width, wxPenStyle style )
  112. {
  113. wxDash dots[2] = { 1, 3 };
  114. // Under OSX and while printing when wxPen is set to 0, renderer follows the request drawing
  115. // nothing & in the bitmap world the minimum is enough to light a pixel, in vectorial one not
  116. if( width <= 1 )
  117. width = DC->DeviceToLogicalXRel( 1 );
  118. if( s_ForceBlackPen )
  119. Color = COLOR4D::BLACK;
  120. const wxPen& curr_pen = DC->GetPen();
  121. if( !curr_pen.IsOk() || curr_pen.GetColour() != Color.ToColour()
  122. || curr_pen.GetWidth() != width
  123. || curr_pen.GetStyle() != style )
  124. {
  125. wxPen pen;
  126. pen.SetColour( Color.ToColour() );
  127. if( style == wxPENSTYLE_DOT )
  128. {
  129. style = wxPENSTYLE_USER_DASH;
  130. pen.SetDashes( 2, dots );
  131. }
  132. pen.SetWidth( width );
  133. pen.SetStyle( style );
  134. DC->SetPen( pen );
  135. }
  136. else
  137. // Should be not needed, but on Linux, in printing process
  138. // the curr pen settings needs to be sometimes re-initialized
  139. // Clearly, this is due to a bug, related to SetBrush(),
  140. // but we have to live with it, at least on wxWidgets 3.0
  141. DC->SetPen( curr_pen );
  142. }
  143. void GRSetBrush( wxDC* DC, COLOR4D Color, bool fill )
  144. {
  145. if( s_ForceBlackPen )
  146. Color = COLOR4D::BLACK;
  147. if( s_DC_lastbrushcolor != Color
  148. || s_DC_lastbrushfill != fill
  149. || s_DC_lastDC != DC )
  150. {
  151. wxBrush brush;
  152. brush.SetColour( Color.ToColour() );
  153. if( fill )
  154. brush.SetStyle( wxBRUSHSTYLE_SOLID );
  155. else
  156. brush.SetStyle( wxBRUSHSTYLE_TRANSPARENT );
  157. DC->SetBrush( brush );
  158. s_DC_lastbrushcolor = Color;
  159. s_DC_lastbrushfill = fill;
  160. s_DC_lastDC = DC;
  161. }
  162. }
  163. /**
  164. * Function GRForceBlackPen
  165. * @param flagforce True to force a black pen whenever the asked color
  166. */
  167. void GRForceBlackPen( bool flagforce )
  168. {
  169. s_ForceBlackPen = flagforce;
  170. }
  171. /**
  172. * Function GetGRForceBlackPenState
  173. * @return s_ForceBlackPen (True if a black pen was forced)
  174. */
  175. bool GetGRForceBlackPenState( void )
  176. {
  177. return s_ForceBlackPen;
  178. }
  179. void GRPutPixel( EDA_RECT* ClipBox, wxDC* DC, int x, int y, COLOR4D Color )
  180. {
  181. if( ClipBox && !ClipBox->Contains( x, y ) )
  182. return;
  183. GRSetColorPen( DC, Color );
  184. DC->DrawPoint( x, y );
  185. }
  186. /*
  187. * Draw a line, in object space.
  188. */
  189. void GRLine( EDA_RECT* ClipBox,
  190. wxDC* DC,
  191. int x1,
  192. int y1,
  193. int x2,
  194. int y2,
  195. int width,
  196. COLOR4D Color,
  197. wxPenStyle aStyle)
  198. {
  199. GRSetColorPen( DC, Color, width, aStyle );
  200. WinClipAndDrawLine( ClipBox, DC, x1, y1, x2, y2, width );
  201. GRLastMoveToX = x2;
  202. GRLastMoveToY = y2;
  203. }
  204. void GRLine( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aStart, wxPoint aEnd, int aWidth, COLOR4D aColor, wxPenStyle aStyle )
  205. {
  206. GRLine( aClipBox, aDC, aStart.x, aStart.y, aEnd.x, aEnd.y, aWidth, aColor, aStyle );
  207. }
  208. /*
  209. * Move to a new position, in object space.
  210. */
  211. void GRMoveTo( int x, int y )
  212. {
  213. GRLastMoveToX = x;
  214. GRLastMoveToY = y;
  215. }
  216. /*
  217. * Draw line to a new position, in object space.
  218. */
  219. void GRLineTo( EDA_RECT* ClipBox, wxDC* DC, int x, int y, int width, COLOR4D Color )
  220. {
  221. GRLine( ClipBox, DC, GRLastMoveToX, GRLastMoveToY, x, y, width, Color );
  222. }
  223. /**
  224. * Function GRLineArray
  225. * draws an array of lines (not a polygon).
  226. * @param aClipBox = the clip box
  227. * @param aDC = the device context into which drawing should occur.
  228. * @param aLines = a list of pair of coordinate in user space: a pair for each line.
  229. * @param aWidth = the width of each line.
  230. * @param aColor = color to draw the lines
  231. * @see COLOR4D
  232. */
  233. void GRLineArray( EDA_RECT* aClipBox, wxDC* aDC, std::vector<wxPoint>& aLines,
  234. int aWidth, COLOR4D aColor )
  235. {
  236. if( aLines.empty() )
  237. return;
  238. GRSetColorPen( aDC, aColor, aWidth );
  239. if( aClipBox )
  240. aClipBox->Inflate( aWidth / 2 );
  241. for( unsigned i = 0; i < aLines.size(); i += 2 )
  242. {
  243. int x1 = aLines[i].x;
  244. int y1 = aLines[i].y;
  245. int x2 = aLines[i + 1].x;
  246. int y2 = aLines[i + 1].y;
  247. if( ( aClipBox == NULL ) || !ClipLine( aClipBox, x1, y1, x2, y2 ) )
  248. aDC->DrawLine( x1, y1, x2, y2 );
  249. }
  250. GRMoveTo( aLines[aLines.size() - 1].x, aLines[aLines.size() - 1].y );
  251. if( aClipBox )
  252. aClipBox->Inflate(-aWidth/2);
  253. }
  254. // Draw the outline of a thick segment wih rounded ends
  255. void GRCSegm( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  256. int width, int aPenSize, COLOR4D Color )
  257. {
  258. GRLastMoveToX = x2;
  259. GRLastMoveToY = y2;
  260. if( ClipBox )
  261. {
  262. EDA_RECT clipbox(*ClipBox);
  263. clipbox.Inflate(width/2);
  264. if( ClipLine( &clipbox, x1, y1, x2, y2 ) )
  265. return;
  266. }
  267. if( width <= 2 ) /* single line or 2 pixels */
  268. {
  269. GRSetColorPen( DC, Color, width );
  270. DC->DrawLine( x1, y1, x2, y2 );
  271. return;
  272. }
  273. GRSetBrush( DC, Color, NOT_FILLED );
  274. GRSetColorPen( DC, Color, aPenSize );
  275. int radius = (width + 1) >> 1;
  276. int dx = x2 - x1;
  277. int dy = y2 - y1;
  278. double angle = -ArcTangente( dy, dx );
  279. wxPoint start;
  280. wxPoint end;
  281. wxPoint org( x1, y1);
  282. int len = (int) hypot( dx, dy );
  283. // We know if the DC is mirrored, to draw arcs
  284. int slx = DC->DeviceToLogicalX( 1 ) - DC->DeviceToLogicalX( 0 );
  285. int sly = DC->DeviceToLogicalY( 1 ) - DC->DeviceToLogicalY( 0 );
  286. bool mirrored = (slx > 0 && sly < 0) || (slx < 0 && sly > 0);
  287. // first edge
  288. start.x = 0;
  289. start.y = radius;
  290. end.x = len;
  291. end.y = radius;
  292. RotatePoint( &start, angle);
  293. RotatePoint( &end, angle);
  294. start += org;
  295. end += org;
  296. DC->DrawLine( start, end );
  297. // first rounded end
  298. end.x = 0;
  299. end.y = -radius;
  300. RotatePoint( &end, angle);
  301. end += org;
  302. if( !mirrored )
  303. DC->DrawArc( end, start, org );
  304. else
  305. DC->DrawArc( start, end, org );
  306. // second edge
  307. start.x = len;
  308. start.y = -radius;
  309. RotatePoint( &start, angle);
  310. start += org;
  311. DC->DrawLine( start, end );
  312. // second rounded end
  313. end.x = len;
  314. end.y = radius;
  315. RotatePoint( &end, angle);
  316. end += org;
  317. if( !mirrored )
  318. DC->DrawArc( end.x, end.y, start.x, start.y, x2, y2 );
  319. else
  320. DC->DrawArc( start.x, start.y, end.x, end.y, x2, y2 );
  321. }
  322. void GRCSegm( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  323. int width, COLOR4D Color )
  324. {
  325. GRCSegm( ClipBox, DC, x1, y1, x2, y2, width, 0, Color );
  326. }
  327. void GRCSegm( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aStart, wxPoint aEnd,
  328. int aWidth, COLOR4D aColor )
  329. {
  330. GRCSegm( aClipBox, aDC, aStart.x, aStart.y, aEnd.x, aEnd.y, aWidth, 0, aColor );
  331. }
  332. /*
  333. * Draw segment (full) with rounded ends in object space (real coords.).
  334. */
  335. void GRFillCSegm( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  336. int width, COLOR4D Color )
  337. {
  338. GRSetColorPen( DC, Color, width );
  339. WinClipAndDrawLine( ClipBox, DC, x1, y1, x2, y2, width );
  340. }
  341. void GRFilledSegment( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aStart, wxPoint aEnd,
  342. int aWidth, COLOR4D aColor )
  343. {
  344. GRSetColorPen( aDC, aColor, aWidth );
  345. WinClipAndDrawLine( aClipBox, aDC, aStart.x, aStart.y, aEnd.x, aEnd.y, aWidth );
  346. }
  347. static bool IsGRSPolyDrawable( EDA_RECT* ClipBox, int n, wxPoint Points[] )
  348. {
  349. if( !ClipBox )
  350. return true;
  351. if( n <= 0 )
  352. return false;
  353. int Xmin, Xmax, Ymin, Ymax;
  354. Xmin = Xmax = Points[0].x;
  355. Ymin = Ymax = Points[0].y;
  356. for( int ii = 1; ii < n; ii++ ) // calculate rectangle
  357. {
  358. Xmin = std::min( Xmin, Points[ii].x );
  359. Xmax = std::max( Xmax, Points[ii].x );
  360. Ymin = std::min( Ymin, Points[ii].y );
  361. Ymax = std::max( Ymax, Points[ii].y );
  362. }
  363. xcliplo = ClipBox->GetX();
  364. ycliplo = ClipBox->GetY();
  365. xcliphi = ClipBox->GetRight();
  366. ycliphi = ClipBox->GetBottom();
  367. if( Xmax < xcliplo )
  368. return false;
  369. if( Xmin > xcliphi )
  370. return false;
  371. if( Ymax < ycliplo )
  372. return false;
  373. if( Ymin > ycliphi )
  374. return false;
  375. return true;
  376. }
  377. /*
  378. * Draw a new polyline and fill it if Fill, in screen space.
  379. */
  380. static void GRSPoly( EDA_RECT* ClipBox, wxDC* DC, int n, wxPoint Points[],
  381. bool Fill, int width,
  382. COLOR4D Color, COLOR4D BgColor )
  383. {
  384. if( !IsGRSPolyDrawable( ClipBox, n, Points ) )
  385. return;
  386. if( Fill && ( n > 2 ) )
  387. {
  388. GRSetBrush( DC, BgColor, FILLED );
  389. GRSetColorPen( DC, Color, width );
  390. /* clip before send the filled polygon to wxDC, because under linux
  391. * (GTK?) polygons having large coordinates are incorrectly drawn
  392. * (integer overflow in coordinates, I am guessing)
  393. */
  394. ClipAndDrawPoly( ClipBox, DC, Points, n );
  395. }
  396. else
  397. {
  398. GRMoveTo( Points[0].x, Points[0].y );
  399. for( int i = 1; i < n; ++i )
  400. {
  401. GRLineTo( ClipBox, DC, Points[i].x, Points[i].y, width, Color );
  402. }
  403. }
  404. }
  405. /*
  406. * Draw a new closed polyline and fill it if Fill, in screen space.
  407. */
  408. static void GRSClosedPoly( EDA_RECT* aClipBox, wxDC* aDC,
  409. int aPointCount, wxPoint aPoints[],
  410. bool aFill, int aWidth,
  411. COLOR4D aColor,
  412. COLOR4D aBgColor )
  413. {
  414. if( !IsGRSPolyDrawable( aClipBox, aPointCount, aPoints ) )
  415. return;
  416. if( aFill && ( aPointCount > 2 ) )
  417. {
  418. GRLastMoveToX = aPoints[aPointCount - 1].x;
  419. GRLastMoveToY = aPoints[aPointCount - 1].y;
  420. GRSetBrush( aDC, aBgColor, FILLED );
  421. GRSetColorPen( aDC, aColor, aWidth );
  422. ClipAndDrawPoly( aClipBox, aDC, aPoints, aPointCount );
  423. }
  424. else
  425. {
  426. GRMoveTo( aPoints[0].x, aPoints[0].y );
  427. for( int i = 1; i < aPointCount; ++i )
  428. {
  429. GRLineTo( aClipBox, aDC, aPoints[i].x, aPoints[i].y, aWidth, aColor );
  430. }
  431. int lastpt = aPointCount - 1;
  432. // Close the polygon
  433. if( aPoints[lastpt] != aPoints[0] )
  434. {
  435. GRLineTo( aClipBox, aDC, aPoints[0].x, aPoints[0].y, aWidth, aColor );
  436. }
  437. }
  438. }
  439. /*
  440. * Draw a new polyline and fill it if Fill, in drawing space.
  441. */
  442. void GRPoly( EDA_RECT* ClipBox, wxDC* DC, int n, wxPoint Points[],
  443. bool Fill, int width, COLOR4D Color, COLOR4D BgColor )
  444. {
  445. GRSPoly( ClipBox, DC, n, Points, Fill, width, Color, BgColor );
  446. }
  447. /*
  448. * Draw a closed polyline and fill it if Fill, in object space.
  449. */
  450. void GRClosedPoly( EDA_RECT* ClipBox, wxDC* DC, int n, wxPoint Points[],
  451. bool Fill, COLOR4D Color, COLOR4D BgColor )
  452. {
  453. GRClosedPoly( ClipBox, DC, n, Points, Fill, 0, Color, BgColor );
  454. }
  455. void GRClosedPoly( EDA_RECT* ClipBox, wxDC* DC, int n, wxPoint Points[],
  456. bool Fill, int width, COLOR4D Color, COLOR4D BgColor )
  457. {
  458. GRSClosedPoly( ClipBox, DC, n, Points, Fill, width, Color, BgColor );
  459. }
  460. static bool clipCircle( EDA_RECT* aClipBox, int xc, int yc, int r, int aWidth )
  461. {
  462. // Clip circles that are outside the ClipBox.
  463. if( aClipBox )
  464. {
  465. int x0, y0, xm, ym;
  466. x0 = aClipBox->GetX();
  467. y0 = aClipBox->GetY();
  468. xm = aClipBox->GetRight();
  469. ym = aClipBox->GetBottom();
  470. r += aWidth;
  471. if( xc < ( x0 - r ) )
  472. return true;
  473. if( yc < ( y0 - r ) )
  474. return true;
  475. if( xc > ( r + xm ) )
  476. return true;
  477. if( yc > ( r + ym ) )
  478. return true;
  479. }
  480. return false;
  481. }
  482. void GRCircle( EDA_RECT* ClipBox, wxDC* DC, int xc, int yc, int r, int width, COLOR4D Color )
  483. {
  484. if( clipCircle( ClipBox, xc, yc, r, width ) || r <= 0 )
  485. return;
  486. GRSetBrush( DC, Color, NOT_FILLED );
  487. GRSetColorPen( DC, Color, width );
  488. DC->DrawEllipse( xc - r, yc - r, r + r, r + r );
  489. }
  490. void GRCircle( EDA_RECT* ClipBox, wxDC* DC, int x, int y, int r, COLOR4D Color )
  491. {
  492. GRCircle( ClipBox, DC, x, y, r, 0, Color );
  493. }
  494. void GRCircle( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aPos, int aRadius, int aWidth, COLOR4D aColor )
  495. {
  496. GRCircle( aClipBox, aDC, aPos.x, aPos.y, aRadius, aWidth, aColor );
  497. }
  498. void GRFilledCircle( EDA_RECT* ClipBox, wxDC* DC, int x, int y, int r,
  499. int width, COLOR4D Color, COLOR4D BgColor )
  500. {
  501. if( clipCircle( ClipBox, x, y, r, width ) || r <= 0 )
  502. return;
  503. GRSetBrush( DC, BgColor, FILLED );
  504. GRSetColorPen( DC, Color, width );
  505. DC->DrawEllipse( x - r, y - r, r + r, r + r );
  506. }
  507. void GRFilledCircle( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aPos, int aRadius, COLOR4D aColor )
  508. {
  509. GRFilledCircle( aClipBox, aDC, aPos.x, aPos.y, aRadius, 0, aColor, aColor );
  510. }
  511. /*
  512. * Draw an arc in user space.
  513. */
  514. void GRArc1( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  515. int xc, int yc, COLOR4D Color )
  516. {
  517. GRArc1( ClipBox, DC, x1, y1, x2, y2, xc, yc, 0, Color );
  518. }
  519. /*
  520. * Draw an arc, width = width in user space.
  521. */
  522. void GRArc1( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  523. int xc, int yc, int width, COLOR4D Color )
  524. {
  525. /* Clip arcs off screen. */
  526. if( ClipBox )
  527. {
  528. int x0, y0, xm, ym, r;
  529. x0 = ClipBox->GetX();
  530. y0 = ClipBox->GetY();
  531. xm = ClipBox->GetRight();
  532. ym = ClipBox->GetBottom();
  533. r = KiROUND( Distance( x1, y1, xc, yc ) );
  534. if( xc < ( x0 - r ) )
  535. return;
  536. if( yc < ( y0 - r ) )
  537. return;
  538. if( xc > ( r + xm ) )
  539. return;
  540. if( yc > ( r + ym ) )
  541. return;
  542. }
  543. GRSetBrush( DC, Color );
  544. GRSetColorPen( DC, Color, width );
  545. DC->DrawArc( x1, y1, x2, y2, xc, yc );
  546. }
  547. void GRArc1( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aStart, wxPoint aEnd,
  548. wxPoint aCenter, int aWidth, COLOR4D aColor )
  549. {
  550. GRArc1( aClipBox, aDC, aStart.x, aStart.y, aEnd.x, aEnd.y, aCenter.x, aCenter.y,
  551. aWidth, aColor );
  552. }
  553. /*
  554. * Draw a filled arc in drawing space.
  555. */
  556. void GRFilledArc( EDA_RECT* ClipBox,
  557. wxDC* DC,
  558. int x,
  559. int y,
  560. double StAngle,
  561. double EndAngle,
  562. int r,
  563. int width,
  564. COLOR4D Color,
  565. COLOR4D BgColor )
  566. {
  567. int x1, y1, x2, y2;
  568. /* Clip arcs off screen */
  569. if( ClipBox )
  570. {
  571. int x0, y0, xm, ym;
  572. x0 = ClipBox->GetX();
  573. y0 = ClipBox->GetY();
  574. xm = ClipBox->GetRight();
  575. ym = ClipBox->GetBottom();
  576. if( x < ( x0 - r - 1 ) )
  577. return;
  578. if( y < ( y0 - r - 1 ) )
  579. return;
  580. if( x > ( r + xm + 1 ) )
  581. return;
  582. if( y > ( r + ym + 1 ) )
  583. return;
  584. }
  585. x1 = r;
  586. y1 = 0;
  587. RotatePoint( &x1, &y1, EndAngle );
  588. x2 = r;
  589. y2 = 0;
  590. RotatePoint( &x2, &y2, StAngle );
  591. GRSetBrush( DC, BgColor, FILLED );
  592. GRSetColorPen( DC, Color, width );
  593. DC->DrawArc( x + x1, y - y1, x + x2, y - y2, x, y );
  594. }
  595. void GRFilledArc( EDA_RECT* ClipBox, wxDC* DC, int x, int y,
  596. double StAngle, double EndAngle, int r,
  597. COLOR4D Color, COLOR4D BgColor )
  598. {
  599. GRFilledArc( ClipBox, DC, x, y, StAngle, EndAngle, r, 0, Color, BgColor );
  600. }
  601. /*
  602. * Draw an arc in drawing space.
  603. */
  604. void GRArc( EDA_RECT* ClipBox, wxDC* DC, int xc, int yc, double StAngle,
  605. double EndAngle, int r, COLOR4D Color )
  606. {
  607. int x1, y1, x2, y2;
  608. /* Clip arcs off screen */
  609. if( ClipBox )
  610. {
  611. int radius = r + 1;
  612. int x0, y0, xm, ym, x, y;
  613. x0 = ClipBox->GetX();
  614. y0 = ClipBox->GetY();
  615. xm = ClipBox->GetRight();
  616. ym = ClipBox->GetBottom();
  617. x = xc;
  618. y = yc;
  619. if( x < ( x0 - radius ) )
  620. return;
  621. if( y < ( y0 - radius ) )
  622. return;
  623. if( x > ( xm + radius ) )
  624. return;
  625. if( y > ( ym + radius ) )
  626. return;
  627. }
  628. x1 = r;
  629. y1 = 0;
  630. RotatePoint( &x1, &y1, EndAngle );
  631. x2 = r;
  632. y2 = 0;
  633. RotatePoint( &x2, &y2, StAngle );
  634. GRSetBrush( DC, Color, NOT_FILLED );
  635. GRSetColorPen( DC, Color );
  636. DC->DrawArc( xc + x1, yc - y1, xc + x2, yc - y2, xc, yc );
  637. }
  638. /*
  639. * Draw an arc with width = width in drawing space.
  640. */
  641. void GRArc( EDA_RECT* ClipBox,
  642. wxDC* DC,
  643. int x,
  644. int y,
  645. double StAngle,
  646. double EndAngle,
  647. int r,
  648. int width,
  649. COLOR4D Color )
  650. {
  651. int x1, y1, x2, y2;
  652. /* Clip arcs off screen. */
  653. if( ClipBox )
  654. {
  655. int x0, y0, xm, ym;
  656. x0 = ClipBox->GetX();
  657. y0 = ClipBox->GetY();
  658. xm = ClipBox->GetRight();
  659. ym = ClipBox->GetBottom();
  660. if( x < ( x0 - r - width ) )
  661. return;
  662. if( y < ( y0 - r - width ) )
  663. return;
  664. if( x > ( r + xm + width ) )
  665. return;
  666. if( y > ( r + ym + width ) )
  667. return;
  668. }
  669. x1 = r;
  670. y1 = 0;
  671. RotatePoint( &x1, &y1, EndAngle );
  672. x2 = r;
  673. y2 = 0;
  674. RotatePoint( &x2, &y2, StAngle );
  675. GRSetBrush( DC, Color );
  676. GRSetColorPen( DC, Color, width );
  677. DC->DrawArc( x + x1, y - y1, x + x2, y - y2, x, y );
  678. }
  679. /*
  680. * Draw a rectangle in drawing space.
  681. */
  682. void GRRect( EDA_RECT* aClipBox, wxDC* aDC, int x1, int y1, int x2, int y2, COLOR4D aColor )
  683. {
  684. GRSRect( aClipBox, aDC, x1, y1, x2, y2, 0, aColor );
  685. }
  686. void GRRectPs( EDA_RECT* aClipBox, wxDC* aDC, const EDA_RECT& aRect, COLOR4D aColor, wxPenStyle aStyle )
  687. {
  688. int x1 = aRect.GetX();
  689. int y1 = aRect.GetY();
  690. int x2 = aRect.GetRight();
  691. int y2 = aRect.GetBottom();
  692. GRSRect( aClipBox, aDC, x1, y1, x2, y2, 0, aColor, aStyle );
  693. }
  694. /*
  695. * Draw a rectangle (thick lines) in drawing space.
  696. */
  697. void GRRect( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2, int width, COLOR4D Color )
  698. {
  699. GRSRect( ClipBox, DC, x1, y1, x2, y2, width, Color );
  700. }
  701. void GRRect( EDA_RECT* aClipBox, wxDC* aDC, const EDA_RECT& aRect, int aWidth, COLOR4D aColor )
  702. {
  703. int x1 = aRect.GetX();
  704. int y1 = aRect.GetY();
  705. int x2 = aRect.GetRight();
  706. int y2 = aRect.GetBottom();
  707. GRSRect( aClipBox, aDC, x1, y1, x2, y2, aWidth, aColor );
  708. }
  709. /*
  710. * Draw a rectangle (filled with AreaColor) in drawing space.
  711. */
  712. void GRFilledRect( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  713. COLOR4D Color, COLOR4D BgColor )
  714. {
  715. GRSFilledRect( ClipBox, DC, x1, y1, x2, y2, 0, Color, BgColor );
  716. }
  717. /*
  718. * Draw a rectangle (filled with AreaColor) in drawing space.
  719. */
  720. void GRFilledRect( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1, int x2, int y2,
  721. int width, COLOR4D Color, COLOR4D BgColor )
  722. {
  723. GRSFilledRect( ClipBox, DC, x1, y1, x2, y2, width, Color, BgColor );
  724. }
  725. /*
  726. * Draw a rectangle in screen space.
  727. */
  728. void GRSRect( EDA_RECT* aClipBox, wxDC* aDC, int x1, int y1, int x2, int y2,
  729. int aWidth, COLOR4D aColor, wxPenStyle aStyle )
  730. {
  731. wxPoint points[5];
  732. points[0] = wxPoint(x1, y1);
  733. points[1] = wxPoint(x1, y2);
  734. points[2] = wxPoint(x2, y2);
  735. points[3] = wxPoint(x2, y1);
  736. points[4] = points[0];
  737. GRSClosedPoly( aClipBox, aDC, 5, points, NOT_FILLED, aWidth,
  738. aColor, aColor );
  739. }
  740. void GRSFilledRect( EDA_RECT* aClipBox, wxDC* aDC, int x1, int y1, int x2, int y2,
  741. int aWidth, COLOR4D aColor, COLOR4D aBgColor )
  742. {
  743. wxPoint points[5];
  744. points[0] = wxPoint(x1, y1);
  745. points[1] = wxPoint(x1, y2);
  746. points[2] = wxPoint(x2, y2);
  747. points[3] = wxPoint(x2, y1);
  748. points[4] = points[0];
  749. GRSetBrush( aDC, aBgColor, FILLED );
  750. GRSetColorPen( aDC, aBgColor, aWidth );
  751. if( aClipBox && (aWidth > 0) )
  752. {
  753. EDA_RECT clipbox(*aClipBox);
  754. clipbox.Inflate(aWidth);
  755. ClipAndDrawPoly(&clipbox, aDC, points, 5); // polygon approach is more accurate
  756. }
  757. else
  758. ClipAndDrawPoly(aClipBox, aDC, points, 5 );
  759. }
  760. /**
  761. * Function ClipAndDrawPoly
  762. * Used to clip a polygon and draw it as Filled Polygon
  763. * uses the Sutherland and Hodgman algo to clip the given poly against a
  764. * rectangle. This rectangle is the drawing area this is useful under
  765. * Linux (2009) because filled polygons are incorrectly drawn if they have
  766. * too large coordinates (seems due to integer overflows in calculations)
  767. * Could be removed in some years, if become unnecessary.
  768. */
  769. /* Note: aClipBox == NULL is legal, so if aClipBox == NULL,
  770. * the polygon is drawn, but not clipped
  771. */
  772. #include <SutherlandHodgmanClipPoly.h>
  773. void ClipAndDrawPoly( EDA_RECT* aClipBox, wxDC* aDC, wxPoint aPoints[], int n )
  774. {
  775. if( aClipBox == NULL )
  776. {
  777. aDC->DrawPolygon( n, aPoints );
  778. return;
  779. }
  780. // A clip box exists: clip and draw the polygon.
  781. static std::vector<wxPoint> clippedPolygon;
  782. static pointVector inputPolygon, outputPolygon;
  783. inputPolygon.clear();
  784. outputPolygon.clear();
  785. clippedPolygon.clear();
  786. for( int ii = 0; ii < n; ii++ )
  787. inputPolygon.push_back( PointF( (REAL) aPoints[ii].x, (REAL) aPoints[ii].y ) );
  788. RectF window( (REAL) aClipBox->GetX(), (REAL) aClipBox->GetY(),
  789. (REAL) aClipBox->GetWidth(), (REAL) aClipBox->GetHeight() );
  790. SutherlandHodgman sh( window );
  791. sh.Clip( inputPolygon, outputPolygon );
  792. for( cpointIterator cit = outputPolygon.begin(); cit != outputPolygon.end(); ++cit )
  793. {
  794. clippedPolygon.push_back( wxPoint( KiROUND( cit->X ), KiROUND( cit->Y ) ) );
  795. }
  796. if( clippedPolygon.size() )
  797. aDC->DrawPolygon( clippedPolygon.size(), &clippedPolygon[0] );
  798. }
  799. void GRBezier( EDA_RECT* ClipBox,
  800. wxDC* DC,
  801. int x1,
  802. int y1,
  803. int x2,
  804. int y2,
  805. int x3,
  806. int y3,
  807. int width,
  808. COLOR4D Color )
  809. {
  810. std::vector<wxPoint> points;
  811. BEZIER_POLY converter( x1, y1, x2, y2, x3, y3 );
  812. converter.GetPoly( points );
  813. GRPoly( ClipBox, DC, points.size(), &points[0], false, width, Color, Color );
  814. }
  815. void GRBezier( EDA_RECT* ClipBox,
  816. wxDC* DC,
  817. int x1,
  818. int y1,
  819. int x2,
  820. int y2,
  821. int x3,
  822. int y3,
  823. int x4,
  824. int y4,
  825. int width,
  826. COLOR4D Color )
  827. {
  828. std::vector<wxPoint> points;
  829. BEZIER_POLY converter( x1, y1, x2, y2, x3, y3, x4, y4 );
  830. converter.GetPoly( points );
  831. GRPoly( ClipBox, DC, points.size(), &points[0], false, width, Color, Color );
  832. }
  833. void GRDrawAnchor( EDA_RECT *aClipBox, wxDC *aDC, int x, int y,
  834. int aSize, COLOR4D aColor )
  835. {
  836. int anchor_size = aDC->DeviceToLogicalXRel( aSize );
  837. GRLine( aClipBox, aDC,
  838. x - anchor_size, y,
  839. x + anchor_size, y, 0, aColor );
  840. GRLine( aClipBox, aDC,
  841. x, y - anchor_size,
  842. x, y + anchor_size, 0, aColor );
  843. }
  844. void GRDrawWrappedText( wxDC& aDC, wxString const& aText )
  845. {
  846. wxStringTokenizer tokenizer( aText, " " );
  847. wxSize const dc_size = aDC.GetSize();
  848. wxSize const margin = aDC.GetTextExtent( " " );
  849. std::vector<wxString> lines;
  850. wxString line_accumulator;
  851. int total_height = 0;
  852. while( tokenizer.HasMoreTokens() )
  853. {
  854. wxString word = tokenizer.GetNextToken();
  855. wxSize linesize = aDC.GetTextExtent( line_accumulator + " " + word );
  856. if( linesize.x >= dc_size.x - margin.x && !line_accumulator.IsEmpty() )
  857. {
  858. lines.push_back( line_accumulator );
  859. line_accumulator = word;
  860. }
  861. else
  862. {
  863. line_accumulator += " ";
  864. line_accumulator += word;
  865. }
  866. }
  867. if( !line_accumulator.IsEmpty() )
  868. {
  869. lines.push_back( line_accumulator );
  870. }
  871. for( auto const& line: lines )
  872. {
  873. wxSize linesize = aDC.GetTextExtent( line );
  874. total_height += linesize.y;
  875. }
  876. int top = ( dc_size.y - total_height ) / 2;
  877. int pos = top;
  878. for( auto const& line: lines )
  879. {
  880. wxSize linesize = aDC.GetTextExtent( line );
  881. aDC.DrawText( line, ( dc_size.x - linesize.x ) / 2, pos );
  882. pos += linesize.y;
  883. }
  884. }