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.

962 lines
31 KiB

3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright The 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. /* Some info on basic items SVG format, used here:
  25. * The root element of all SVG files is the <svg> element.
  26. *
  27. * The <g> element is used to group SVG shapes together.
  28. * Once grouped you can transform the whole group of shapes as if it was a single shape.
  29. * This is an advantage compared to a nested <svg> element
  30. * which cannot be the target of transformation by itself.
  31. *
  32. * The <rect> element represents a rectangle.
  33. * Using this element you can draw rectangles of various width, height,
  34. * with different stroke (outline) and fill colors, with sharp or rounded corners etc.
  35. *
  36. * <svg xmlns="http://www.w3.org/2000/svg"
  37. * xmlns:xlink="http://www.w3.org/1999/xlink">
  38. *
  39. * <rect x="10" y="10" height="100" width="100"
  40. * style="stroke:#006600; fill: #00cc00"/>
  41. *
  42. * </svg>
  43. *
  44. * The <circle> element is used to draw circles.
  45. * <circle cx="40" cy="40" r="24" style="stroke:#006600; fill:#00cc00"/>
  46. *
  47. * The <ellipse> element is used to draw ellipses.
  48. * An ellipse is a circle that does not have equal height and width.
  49. * Its radius in the x and y directions are different, in other words.
  50. * <ellipse cx="40" cy="40" rx="30" ry="15"
  51. * style="stroke:#006600; fill:#00cc00"/>
  52. *
  53. * The <line> element is used to draw lines.
  54. *
  55. * <line x1="0" y1="10" x2="0" y2="100" style="stroke:#006600;"/>
  56. * <line x1="10" y1="10" x2="100" y2="100" style="stroke:#006600;"/>
  57. *
  58. * The <polyline> element is used to draw multiple connected lines
  59. * Here is a simple example:
  60. *
  61. * <polyline points="0,0 30,0 15,30" style="stroke:#006600;"/>
  62. *
  63. * The <polygon> element is used to draw with multiple (3 or more) sides / edges.
  64. * Here is a simple example:
  65. *
  66. * <polygon points="0,0 50,0 25,50" style="stroke:#660000; fill:#cc3333;"/>
  67. *
  68. * The <path> element is used to draw advanced shapes combined from lines and arcs,
  69. * with or without fill.
  70. * It is probably the most advanced and versatile SVG shape of them all.
  71. * It is probably also the hardest element to master.
  72. * <path d="M50,50
  73. * A30,30 0 0,1 35,20
  74. * L100,100
  75. * M110,110
  76. * L100,0"
  77. * style="stroke:#660000; fill:none;"/>
  78. *
  79. * Draw an elliptic arc: it is one of basic path command:
  80. * <path d="M(startx,starty) A(radiusx,radiusy)
  81. * rotation-axe-x
  82. * flag_arc_large,flag_sweep endx,endy">
  83. * flag_arc_large: 0 = small arc > 180 deg, 1 = large arc > 180 deg
  84. * flag_sweep : 0 = CCW, 1 = CW
  85. * The center of ellipse is automatically calculated.
  86. */
  87. #include <core/base64.h>
  88. #include <eda_shape.h>
  89. #include <string_utils.h>
  90. #include <font/font.h>
  91. #include <macros.h>
  92. #include <trigo.h>
  93. #include <cstdint>
  94. #include <wx/mstream.h>
  95. #include <plotters/plotters_pslike.h>
  96. // Note:
  97. // During tests, we (JPC) found issues when the coordinates used 6 digits in mantissa
  98. // especially for stroke-width using very small (but not null) values < 0.00001 mm
  99. // So to avoid this king of issue, we are using 4 digits in mantissa
  100. // The resolution (m_precision ) is 0.1 micron, that looks enough for a SVG file
  101. /**
  102. * Translates '<' to "&lt;", '>' to "&gt;" and so on, according to the spec:
  103. * http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
  104. * May be moved to a library if needed generally, but not expecting that.
  105. */
  106. static wxString XmlEsc( const wxString& aStr, bool isAttribute = false )
  107. {
  108. wxString escaped;
  109. escaped.reserve( aStr.length() );
  110. for( wxString::const_iterator it = aStr.begin(); it != aStr.end(); ++it )
  111. {
  112. const wxChar c = *it;
  113. switch( c )
  114. {
  115. case wxS( '<' ):
  116. escaped.append( wxS( "&lt;" ) );
  117. break;
  118. case wxS( '>' ):
  119. escaped.append( wxS( "&gt;" ) );
  120. break;
  121. case wxS( '&' ):
  122. escaped.append( wxS( "&amp;" ) );
  123. break;
  124. case wxS( '\r' ):
  125. escaped.append( wxS( "&#xD;" ) );
  126. break;
  127. default:
  128. if( isAttribute )
  129. {
  130. switch( c )
  131. {
  132. case wxS( '"' ):
  133. escaped.append( wxS( "&quot;" ) );
  134. break;
  135. case wxS( '\t' ):
  136. escaped.append( wxS( "&#x9;" ) );
  137. break;
  138. case wxS( '\n' ):
  139. escaped.append( wxS( "&#xA;" ));
  140. break;
  141. default:
  142. escaped.append(c);
  143. }
  144. }
  145. else
  146. {
  147. escaped.append(c);
  148. }
  149. }
  150. }
  151. return escaped;
  152. }
  153. SVG_PLOTTER::SVG_PLOTTER( const PROJECT* aProject ) :
  154. PSLIKE_PLOTTER( aProject )
  155. {
  156. m_graphics_changed = true;
  157. SetTextMode( PLOT_TEXT_MODE::STROKE );
  158. m_fillMode = FILL_T::NO_FILL; // or FILLED_SHAPE or FILLED_WITH_BG_BODYCOLOR
  159. m_pen_rgb_color = 0; // current color value (black)
  160. m_brush_rgb_color = 0; // current color value with alpha(black)
  161. m_brush_alpha = 1.0;
  162. m_dashed = LINE_STYLE::SOLID;
  163. m_precision = 4; // default: 4 digits in mantissa.
  164. }
  165. void SVG_PLOTTER::SetViewport( const VECTOR2I& aOffset, double aIusPerDecimil,
  166. double aScale, bool aMirror )
  167. {
  168. m_plotMirror = aMirror;
  169. m_yaxisReversed = true; // unlike other plotters, SVG has Y axis reversed
  170. m_plotOffset = aOffset;
  171. m_plotScale = aScale;
  172. m_IUsPerDecimil = aIusPerDecimil;
  173. // Compute the paper size in IUs. for historical reasons the page size is in mils
  174. m_paperSize = m_pageInfo.GetSizeMils();
  175. m_paperSize.x *= 10.0 * aIusPerDecimil;
  176. m_paperSize.y *= 10.0 * aIusPerDecimil;
  177. // gives now a default value to iuPerDeviceUnit (because the units of the caller is now known)
  178. double iusPerMM = m_IUsPerDecimil / 2.54 * 1000;
  179. m_iuPerDeviceUnit = 1 / iusPerMM;
  180. SetSvgCoordinatesFormat( 4 );
  181. }
  182. void SVG_PLOTTER::SetSvgCoordinatesFormat( unsigned aPrecision )
  183. {
  184. // Only number of digits in mantissa are adjustable.
  185. // SVG units are always mm
  186. m_precision = aPrecision;
  187. }
  188. void SVG_PLOTTER::setFillMode( FILL_T fill )
  189. {
  190. if( m_fillMode != fill )
  191. {
  192. m_graphics_changed = true;
  193. m_fillMode = fill;
  194. }
  195. }
  196. void SVG_PLOTTER::setSVGPlotStyle( int aLineWidth, bool aIsGroup, const std::string& aExtraStyle )
  197. {
  198. if( aIsGroup )
  199. fputs( "</g>\n<g ", m_outputFile );
  200. fputs( "style=\"", m_outputFile );
  201. if( m_fillMode == FILL_T::NO_FILL )
  202. {
  203. fputs( "fill:none; ", m_outputFile );
  204. }
  205. else
  206. {
  207. // output the background fill color
  208. fprintf( m_outputFile, "fill:#%6.6lX; ", m_brush_rgb_color );
  209. switch( m_fillMode )
  210. {
  211. case FILL_T::FILLED_SHAPE:
  212. case FILL_T::FILLED_WITH_BG_BODYCOLOR:
  213. case FILL_T::FILLED_WITH_COLOR:
  214. fprintf( m_outputFile, "fill-opacity:%.*f; ", m_precision, m_brush_alpha );
  215. break;
  216. default: break;
  217. }
  218. }
  219. double pen_w = userToDeviceSize( aLineWidth );
  220. if( pen_w <= 0 )
  221. {
  222. fputs( "stroke:none;", m_outputFile );
  223. }
  224. else
  225. {
  226. // Fix a strange issue found in Inkscape: aWidth < 100 nm create issues on degrouping
  227. // objects.
  228. // So we use only 4 digits in mantissa for stroke-width.
  229. // TODO: perhaps used only 3 or 4 digits in mantissa for all values in mm, because some
  230. // issues were previously reported reported when using nm as integer units
  231. fprintf( m_outputFile, "\nstroke:#%6.6lX; stroke-width:%.*f; stroke-opacity:1; \n",
  232. m_pen_rgb_color, m_precision, pen_w );
  233. fputs( "stroke-linecap:round; stroke-linejoin:round;", m_outputFile );
  234. //set any extra attributes for non-solid lines
  235. switch( m_dashed )
  236. {
  237. case LINE_STYLE::DASH:
  238. fprintf( m_outputFile, "stroke-dasharray:%.*f,%.*f;", m_precision,
  239. GetDashMarkLenIU( aLineWidth ), m_precision, GetDashGapLenIU( aLineWidth ) );
  240. break;
  241. case LINE_STYLE::DOT:
  242. fprintf( m_outputFile, "stroke-dasharray:%f,%f;", GetDotMarkLenIU( aLineWidth ),
  243. GetDashGapLenIU( aLineWidth ) );
  244. break;
  245. case LINE_STYLE::DASHDOT:
  246. fprintf( m_outputFile, "stroke-dasharray:%f,%f,%f,%f;", GetDashMarkLenIU( aLineWidth ),
  247. GetDashGapLenIU( aLineWidth ), GetDotMarkLenIU( aLineWidth ),
  248. GetDashGapLenIU( aLineWidth ) );
  249. break;
  250. case LINE_STYLE::DASHDOTDOT:
  251. fprintf( m_outputFile, "stroke-dasharray:%f,%f,%f,%f,%f,%f;",
  252. GetDashMarkLenIU( aLineWidth ), GetDashGapLenIU( aLineWidth ),
  253. GetDotMarkLenIU( aLineWidth ), GetDashGapLenIU( aLineWidth ),
  254. GetDotMarkLenIU( aLineWidth ), GetDashGapLenIU( aLineWidth ) );
  255. break;
  256. case LINE_STYLE::DEFAULT:
  257. case LINE_STYLE::SOLID:
  258. default:
  259. //do nothing
  260. break;
  261. }
  262. }
  263. if( aExtraStyle.length() )
  264. fputs( aExtraStyle.c_str(), m_outputFile );
  265. fputs( "\"", m_outputFile );
  266. if( aIsGroup )
  267. {
  268. fputs( ">", m_outputFile );
  269. m_graphics_changed = false;
  270. }
  271. fputs( "\n", m_outputFile );
  272. }
  273. void SVG_PLOTTER::SetCurrentLineWidth( int aWidth, void* aData )
  274. {
  275. if( aWidth == DO_NOT_SET_LINE_WIDTH )
  276. return;
  277. else if( aWidth == USE_DEFAULT_LINE_WIDTH )
  278. aWidth = m_renderSettings->GetDefaultPenWidth();
  279. // Note: aWidth == 0 is fine: used for filled shapes with no outline thickness
  280. wxASSERT_MSG( aWidth >= 0, "Plotter called to set negative pen width" );
  281. if( aWidth != m_currentPenWidth )
  282. {
  283. m_graphics_changed = true;
  284. m_currentPenWidth = aWidth;
  285. }
  286. }
  287. void SVG_PLOTTER::StartBlock( void* aData )
  288. {
  289. // We can't use <g></g> for blocks because we're already using it for graphics context, and
  290. // our graphics context handling is lazy (ie: it leaves the last group open until the context
  291. // changes).
  292. }
  293. void SVG_PLOTTER::EndBlock( void* aData )
  294. {
  295. }
  296. void SVG_PLOTTER::emitSetRGBColor( double r, double g, double b, double a )
  297. {
  298. int red = (int) ( 255.0 * r );
  299. int green = (int) ( 255.0 * g );
  300. int blue = (int) ( 255.0 * b );
  301. long rgb_color = ( red << 16 ) | ( green << 8 ) | blue;
  302. if( m_pen_rgb_color != rgb_color || m_brush_alpha != a )
  303. {
  304. m_graphics_changed = true;
  305. m_pen_rgb_color = rgb_color;
  306. // Currently, use the same color for brush and pen (i.e. to draw and fill a contour).
  307. m_brush_rgb_color = rgb_color;
  308. m_brush_alpha = a;
  309. }
  310. }
  311. void SVG_PLOTTER::SetDash( int aLineWidth, LINE_STYLE aLineStyle )
  312. {
  313. if( m_dashed != aLineStyle )
  314. {
  315. m_graphics_changed = true;
  316. m_dashed = aLineStyle;
  317. }
  318. }
  319. void SVG_PLOTTER::Rect( const VECTOR2I& p1, const VECTOR2I& p2, FILL_T fill, int width )
  320. {
  321. BOX2I rect( p1, VECTOR2I( p2.x - p1.x, p2.y - p1.y ) );
  322. rect.Normalize();
  323. VECTOR2D org_dev = userToDeviceCoordinates( rect.GetOrigin() );
  324. VECTOR2D end_dev = userToDeviceCoordinates( rect.GetEnd() );
  325. VECTOR2D size_dev = end_dev - org_dev;
  326. // Ensure size of rect in device coordinates is > 0
  327. // I don't know if this is a SVG issue or a Inkscape issue, but
  328. // Inkscape has problems with negative or null values for width and/or height, so avoid them
  329. BOX2D rect_dev( org_dev, size_dev );
  330. rect_dev.Normalize();
  331. setFillMode( fill );
  332. SetCurrentLineWidth( width );
  333. if( m_graphics_changed )
  334. setSVGPlotStyle( GetCurrentLineWidth() );
  335. // Rectangles having a 0 size value for height or width are just not drawn on Inkscape,
  336. // so use a line when happens.
  337. if( rect_dev.GetSize().x == 0.0 || rect_dev.GetSize().y == 0.0 ) // Draw a line
  338. {
  339. fprintf( m_outputFile,
  340. "<line x1=\"%.*f\" y1=\"%.*f\" x2=\"%.*f\" y2=\"%.*f\" />\n",
  341. m_precision, rect_dev.GetPosition().x,
  342. m_precision, rect_dev.GetPosition().y,
  343. m_precision, rect_dev.GetEnd().x,
  344. m_precision, rect_dev.GetEnd().y );
  345. }
  346. else
  347. {
  348. fprintf( m_outputFile,
  349. "<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" rx=\"%f\" />\n",
  350. rect_dev.GetPosition().x,
  351. rect_dev.GetPosition().y,
  352. rect_dev.GetSize().x,
  353. rect_dev.GetSize().y,
  354. 0.0 /* radius of rounded corners */ );
  355. }
  356. }
  357. void SVG_PLOTTER::Circle( const VECTOR2I& pos, int diametre, FILL_T fill, int width )
  358. {
  359. VECTOR2D pos_dev = userToDeviceCoordinates( pos );
  360. double radius = userToDeviceSize( diametre / 2.0 );
  361. setFillMode( fill );
  362. SetCurrentLineWidth( width );
  363. if( m_graphics_changed )
  364. setSVGPlotStyle( GetCurrentLineWidth() );
  365. // If diameter is less than width, switch to filled mode
  366. if( fill == FILL_T::NO_FILL && diametre < width )
  367. {
  368. setFillMode( FILL_T::FILLED_SHAPE );
  369. SetCurrentLineWidth( 0 );
  370. radius = userToDeviceSize( ( diametre / 2.0 ) + ( width / 2.0 ) );
  371. }
  372. fprintf( m_outputFile,
  373. "<circle cx=\"%.*f\" cy=\"%.*f\" r=\"%.*f\" /> \n",
  374. m_precision, pos_dev.x,
  375. m_precision, pos_dev.y,
  376. m_precision, radius );
  377. }
  378. void SVG_PLOTTER::Arc( const VECTOR2D& aCenter, const EDA_ANGLE& aStartAngle,
  379. const EDA_ANGLE& aAngle, double aRadius, FILL_T aFill, int aWidth )
  380. {
  381. /* Draws an arc of a circle, centered on (xc,yc), with starting point (x1, y1) and ending
  382. * at (x2, y2). The current pen is used for the outline and the current brush for filling
  383. * the shape.
  384. *
  385. * The arc is drawn in an anticlockwise direction from the start point to the end point.
  386. */
  387. if( aRadius <= 0 )
  388. {
  389. Circle( aCenter, aWidth, FILL_T::FILLED_SHAPE, 0 );
  390. return;
  391. }
  392. EDA_ANGLE startAngle = -aStartAngle;
  393. EDA_ANGLE endAngle = startAngle - aAngle;
  394. if( endAngle < startAngle )
  395. std::swap( startAngle, endAngle );
  396. // Calculate start point.
  397. VECTOR2D centre_device = userToDeviceCoordinates( aCenter );
  398. double radius_device = userToDeviceSize( aRadius );
  399. if( m_plotMirror )
  400. {
  401. if( m_mirrorIsHorizontal )
  402. {
  403. std::swap( startAngle, endAngle );
  404. startAngle = ANGLE_180 - startAngle;
  405. endAngle = ANGLE_180 - endAngle;
  406. }
  407. else
  408. {
  409. startAngle = -startAngle;
  410. endAngle = -endAngle;
  411. }
  412. }
  413. VECTOR2D start;
  414. start.x = radius_device;
  415. RotatePoint( start, startAngle );
  416. VECTOR2D end;
  417. end.x = radius_device;
  418. RotatePoint( end, endAngle );
  419. start += centre_device;
  420. end += centre_device;
  421. double theta1 = startAngle.AsRadians();
  422. if( theta1 < 0 )
  423. theta1 = theta1 + M_PI * 2;
  424. double theta2 = endAngle.AsRadians();
  425. if( theta2 < 0 )
  426. theta2 = theta2 + M_PI * 2;
  427. if( theta2 < theta1 )
  428. theta2 = theta2 + M_PI * 2;
  429. int flg_arc = 0; // flag for large or small arc. 0 means less than 180 degrees
  430. if( fabs( theta2 - theta1 ) > M_PI )
  431. flg_arc = 1;
  432. int flg_sweep = 0; // flag for sweep always 0
  433. // Draw a single arc: an arc is one of 3 curve commands (2 other are 2 bezier curves)
  434. // params are start point, radius1, radius2, X axe rotation,
  435. // flag arc size (0 = small arc > 180 deg, 1 = large arc > 180 deg),
  436. // sweep arc ( 0 = CCW, 1 = CW),
  437. // end point
  438. if( aFill != FILL_T::NO_FILL )
  439. {
  440. // Filled arcs (in Eeschema) consist of the pie wedge and a stroke only on the arc
  441. // This needs to be drawn in two steps.
  442. setFillMode( aFill );
  443. SetCurrentLineWidth( 0 );
  444. if( m_graphics_changed )
  445. setSVGPlotStyle( GetCurrentLineWidth() );
  446. fprintf( m_outputFile,
  447. "<path d=\"M%.*f %.*f A%.*f %.*f 0.0 %d %d %.*f %.*f L %.*f %.*f Z\" />\n",
  448. m_precision, start.x,
  449. m_precision, start.y,
  450. m_precision, radius_device,
  451. m_precision, radius_device,
  452. flg_arc,
  453. flg_sweep,
  454. m_precision, end.x,
  455. m_precision, end.y,
  456. m_precision, centre_device.x,
  457. m_precision, centre_device.y );
  458. }
  459. setFillMode( FILL_T::NO_FILL );
  460. SetCurrentLineWidth( aWidth );
  461. if( m_graphics_changed )
  462. setSVGPlotStyle( GetCurrentLineWidth() );
  463. fprintf( m_outputFile,
  464. "<path d=\"M%.*f %.*f A%.*f %.*f 0.0 %d %d %.*f %.*f\" />\n",
  465. m_precision, start.x,
  466. m_precision, start.y,
  467. m_precision, radius_device,
  468. m_precision, radius_device,
  469. flg_arc,
  470. flg_sweep,
  471. m_precision, end.x,
  472. m_precision, end.y );
  473. }
  474. void SVG_PLOTTER::BezierCurve( const VECTOR2I& aStart, const VECTOR2I& aControl1,
  475. const VECTOR2I& aControl2, const VECTOR2I& aEnd,
  476. int aTolerance, int aLineThickness )
  477. {
  478. #if 1
  479. setFillMode( FILL_T::NO_FILL );
  480. SetCurrentLineWidth( aLineThickness );
  481. if( m_graphics_changed )
  482. setSVGPlotStyle( GetCurrentLineWidth() );
  483. VECTOR2D start = userToDeviceCoordinates( aStart );
  484. VECTOR2D ctrl1 = userToDeviceCoordinates( aControl1 );
  485. VECTOR2D ctrl2 = userToDeviceCoordinates( aControl2 );
  486. VECTOR2D end = userToDeviceCoordinates( aEnd );
  487. // Generate a cubic curve: start point and 3 other control points.
  488. fprintf( m_outputFile,
  489. "<path d=\"M%.*f,%.*f C%.*f,%.*f %.*f,%.*f %.*f,%.*f\" />\n",
  490. m_precision, start.x,
  491. m_precision, start.y,
  492. m_precision, ctrl1.x,
  493. m_precision, ctrl1.y,
  494. m_precision, ctrl2.x,
  495. m_precision, ctrl2.y,
  496. m_precision, end.x,
  497. m_precision, end.y );
  498. #else
  499. PLOTTER::BezierCurve( aStart, aControl1, aControl2, aEnd, aTolerance, aLineThickness );
  500. #endif
  501. }
  502. void SVG_PLOTTER::PlotPoly( const std::vector<VECTOR2I>& aCornerList, FILL_T aFill,
  503. int aWidth, void* aData )
  504. {
  505. if( aCornerList.size() <= 1 )
  506. return;
  507. setFillMode( aFill );
  508. SetCurrentLineWidth( aWidth );
  509. fprintf( m_outputFile, "<path ");
  510. switch( aFill )
  511. {
  512. case FILL_T::NO_FILL:
  513. setSVGPlotStyle( aWidth, false, "fill:none" );
  514. break;
  515. case FILL_T::FILLED_WITH_BG_BODYCOLOR:
  516. case FILL_T::FILLED_SHAPE:
  517. case FILL_T::FILLED_WITH_COLOR:
  518. setSVGPlotStyle( aWidth, false, "fill-rule:evenodd;" );
  519. break;
  520. }
  521. VECTOR2D pos = userToDeviceCoordinates( aCornerList[0] );
  522. fprintf( m_outputFile,
  523. "d=\"M %.*f,%.*f\n",
  524. m_precision, pos.x,
  525. m_precision, pos.y );
  526. for( unsigned ii = 1; ii < aCornerList.size() - 1; ii++ )
  527. {
  528. pos = userToDeviceCoordinates( aCornerList[ii] );
  529. fprintf( m_outputFile,
  530. "%.*f,%.*f\n",
  531. m_precision, pos.x,
  532. m_precision, pos.y );
  533. }
  534. // If the corner list ends where it begins, then close the poly
  535. if( aCornerList.front() == aCornerList.back() )
  536. {
  537. fprintf( m_outputFile, "Z\" /> \n" );
  538. }
  539. else
  540. {
  541. pos = userToDeviceCoordinates( aCornerList.back() );
  542. fprintf( m_outputFile,
  543. "%.*f,%.*f\n\" /> \n",
  544. m_precision, pos.x,
  545. m_precision, pos.y );
  546. }
  547. }
  548. void SVG_PLOTTER::PlotImage( const wxImage& aImage, const VECTOR2I& aPos, double aScaleFactor )
  549. {
  550. VECTOR2I pix_size( aImage.GetWidth(), aImage.GetHeight() );
  551. // Requested size (in IUs)
  552. VECTOR2D drawsize( aScaleFactor * pix_size.x, aScaleFactor * pix_size.y );
  553. // calculate the bitmap start position
  554. VECTOR2I start( aPos.x - drawsize.x / 2, aPos.y - drawsize.y / 2 );
  555. // Rectangles having a 0 size value for height or width are just not drawn on Inkscape,
  556. // so use a line when happens.
  557. if( drawsize.x == 0.0 || drawsize.y == 0.0 ) // Draw a line
  558. {
  559. PLOTTER::PlotImage( aImage, aPos, aScaleFactor );
  560. }
  561. else
  562. {
  563. wxMemoryOutputStream img_stream;
  564. if( m_colorMode )
  565. {
  566. aImage.SaveFile( img_stream, wxBITMAP_TYPE_PNG );
  567. }
  568. else // Plot in B&W
  569. {
  570. wxImage image = aImage.ConvertToGreyscale();
  571. image.SaveFile( img_stream, wxBITMAP_TYPE_PNG );
  572. }
  573. size_t input_len = img_stream.GetOutputStreamBuffer()->GetBufferSize();
  574. std::vector<uint8_t> buffer( input_len );
  575. std::vector<uint8_t> encoded;
  576. img_stream.CopyTo( buffer.data(), buffer.size() );
  577. base64::encode( buffer, encoded );
  578. fprintf( m_outputFile,
  579. "<image x=\"%f\" y=\"%f\" xlink:href=\"data:image/png;base64,",
  580. userToDeviceSize( start.x ),
  581. userToDeviceSize( start.y ) );
  582. for( size_t i = 0; i < encoded.size(); i++ )
  583. {
  584. fprintf( m_outputFile, "%c", static_cast<char>( encoded[i] ) );
  585. if( ( i % 64 ) == 63 )
  586. fprintf( m_outputFile, "\n" );
  587. }
  588. fprintf( m_outputFile,
  589. "\"\npreserveAspectRatio=\"none\" width=\"%.*f\" height=\"%.*f\" />",
  590. m_precision,
  591. userToDeviceSize( drawsize.x ),
  592. m_precision,
  593. userToDeviceSize( drawsize.y ) );
  594. }
  595. }
  596. void SVG_PLOTTER::PenTo( const VECTOR2I& pos, char plume )
  597. {
  598. if( plume == 'Z' )
  599. {
  600. if( m_penState != 'Z' )
  601. {
  602. fputs( "\" />\n", m_outputFile );
  603. m_penState = 'Z';
  604. m_penLastpos.x = -1;
  605. m_penLastpos.y = -1;
  606. }
  607. return;
  608. }
  609. if( m_penState == 'Z' ) // here plume = 'D' or 'U'
  610. {
  611. VECTOR2D pos_dev = userToDeviceCoordinates( pos );
  612. // Ensure we do not use a fill mode when moving the pen,
  613. // in SVG mode (i;e. we are plotting only basic lines, not a filled area
  614. if( m_fillMode != FILL_T::NO_FILL )
  615. setFillMode( FILL_T::NO_FILL );
  616. if( m_graphics_changed )
  617. setSVGPlotStyle( GetCurrentLineWidth() );
  618. fprintf( m_outputFile, "<path d=\"M%.*f %.*f\n",
  619. m_precision, pos_dev.x,
  620. m_precision, pos_dev.y );
  621. }
  622. else if( m_penState != plume || pos != m_penLastpos )
  623. {
  624. if( m_graphics_changed )
  625. setSVGPlotStyle( GetCurrentLineWidth() );
  626. VECTOR2D pos_dev = userToDeviceCoordinates( pos );
  627. fprintf( m_outputFile, "L%.*f %.*f\n",
  628. m_precision, pos_dev.x,
  629. m_precision, pos_dev.y );
  630. }
  631. m_penState = plume;
  632. m_penLastpos = pos;
  633. }
  634. bool SVG_PLOTTER::StartPlot( const wxString& aPageNumber )
  635. {
  636. wxASSERT( m_outputFile );
  637. static const char* header[] =
  638. {
  639. "<?xml version=\"1.0\" standalone=\"no\"?>\n",
  640. " <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \n",
  641. " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> \n",
  642. "<svg\n"
  643. " xmlns:svg=\"http://www.w3.org/2000/svg\"\n"
  644. " xmlns=\"http://www.w3.org/2000/svg\"\n",
  645. " xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n",
  646. " version=\"1.1\"\n",
  647. nullptr
  648. };
  649. // Write header.
  650. for( int ii = 0; header[ii] != nullptr; ii++ )
  651. {
  652. fputs( header[ii], m_outputFile );
  653. }
  654. // Write viewport pos and size
  655. VECTOR2D origin; // TODO set to actual value
  656. fprintf( m_outputFile,
  657. " width=\"%.*fmm\" height=\"%.*fmm\" viewBox=\"%.*f %.*f %.*f %.*f\">\n",
  658. m_precision, (double) m_paperSize.x / m_IUsPerDecimil * 2.54 / 1000,
  659. m_precision, (double) m_paperSize.y / m_IUsPerDecimil * 2.54 / 1000,
  660. m_precision, origin.x, m_precision, origin.y,
  661. m_precision, m_paperSize.x * m_iuPerDeviceUnit,
  662. m_precision, m_paperSize.y * m_iuPerDeviceUnit);
  663. // Write title
  664. char date_buf[250];
  665. time_t ltime = time( nullptr );
  666. strftime( date_buf, 250, "%Y/%m/%d %H:%M:%S", localtime( &ltime ) );
  667. fprintf( m_outputFile,
  668. "<title>SVG Image created as %s date %s </title>\n",
  669. TO_UTF8( XmlEsc( wxFileName( m_filename ).GetFullName() ) ),
  670. date_buf );
  671. // End of header
  672. fprintf( m_outputFile,
  673. " <desc>Image generated by %s </desc>\n",
  674. TO_UTF8( XmlEsc( m_creator ) ) );
  675. // output the pen and brush color (RVB values in hex) and opacity
  676. double opacity = 1.0; // 0.0 (transparent to 1.0 (solid)
  677. fprintf( m_outputFile,
  678. "<g style=\"fill:#%6.6lX; fill-opacity:%.*f;stroke:#%6.6lX; stroke-opacity:%.*f;\n",
  679. m_brush_rgb_color,
  680. m_precision,
  681. m_brush_alpha,
  682. m_pen_rgb_color,
  683. m_precision,
  684. opacity );
  685. // output the pen cap and line joint
  686. fputs( "stroke-linecap:round; stroke-linejoin:round;\"\n", m_outputFile );
  687. fputs( " transform=\"translate(0 0) scale(1 1)\">\n", m_outputFile );
  688. return true;
  689. }
  690. bool SVG_PLOTTER::EndPlot()
  691. {
  692. fputs( "</g> \n</svg>\n", m_outputFile );
  693. fclose( m_outputFile );
  694. m_outputFile = nullptr;
  695. return true;
  696. }
  697. void SVG_PLOTTER::Text( const VECTOR2I& aPos,
  698. const COLOR4D& aColor,
  699. const wxString& aText,
  700. const EDA_ANGLE& aOrient,
  701. const VECTOR2I& aSize,
  702. enum GR_TEXT_H_ALIGN_T aH_justify,
  703. enum GR_TEXT_V_ALIGN_T aV_justify,
  704. int aWidth,
  705. bool aItalic,
  706. bool aBold,
  707. bool aMultilineAllowed,
  708. KIFONT::FONT* aFont,
  709. const KIFONT::METRICS& aFontMetrics,
  710. void* aData )
  711. {
  712. setFillMode( FILL_T::NO_FILL );
  713. SetColor( aColor );
  714. SetCurrentLineWidth( aWidth );
  715. if( m_graphics_changed )
  716. setSVGPlotStyle( GetCurrentLineWidth() );
  717. VECTOR2I text_pos = aPos;
  718. const char* hjust = "start";
  719. switch( aH_justify )
  720. {
  721. case GR_TEXT_H_ALIGN_CENTER: hjust = "middle"; break;
  722. case GR_TEXT_H_ALIGN_RIGHT: hjust = "end"; break;
  723. case GR_TEXT_H_ALIGN_LEFT: hjust = "start"; break;
  724. case GR_TEXT_H_ALIGN_INDETERMINATE:
  725. wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
  726. break;
  727. }
  728. switch( aV_justify )
  729. {
  730. case GR_TEXT_V_ALIGN_CENTER: text_pos.y += aSize.y / 2; break;
  731. case GR_TEXT_V_ALIGN_TOP: text_pos.y += aSize.y; break;
  732. case GR_TEXT_V_ALIGN_BOTTOM: break;
  733. case GR_TEXT_V_ALIGN_INDETERMINATE:
  734. wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
  735. break;
  736. }
  737. VECTOR2I text_size;
  738. // aSize.x or aSize.y is < 0 for mirrored texts.
  739. // The actual text size value is the absolute value
  740. text_size.x = std::abs( GRTextWidth( aText, aFont, aSize, aWidth, aBold, aItalic,
  741. aFontMetrics ) );
  742. text_size.y = std::abs( aSize.x * 4/3 ); // Hershey font height to em size conversion
  743. VECTOR2D anchor_pos_dev = userToDeviceCoordinates( aPos );
  744. VECTOR2D text_pos_dev = userToDeviceCoordinates( text_pos );
  745. VECTOR2D sz_dev = userToDeviceSize( text_size );
  746. // Output the text as a hidden string (opacity = 0). This allows WYSIWYG search to highlight
  747. // a selection in approximately the right area. It also makes it easier for those that need
  748. // to edit the text (as text) in subsequent processes.
  749. {
  750. if( !aOrient.IsZero() )
  751. {
  752. fprintf( m_outputFile,
  753. "<g transform=\"rotate(%f %.*f %.*f)\">\n",
  754. m_plotMirror ? aOrient.AsDegrees() : -aOrient.AsDegrees(),
  755. m_precision,
  756. anchor_pos_dev.x,
  757. m_precision,
  758. anchor_pos_dev.y );
  759. }
  760. fprintf( m_outputFile,
  761. "<text x=\"%.*f\" y=\"%.*f\"\n",
  762. m_precision,
  763. text_pos_dev.x, m_precision,
  764. text_pos_dev.y );
  765. /// If the text is mirrored, we should also mirror the hidden text to match
  766. if( m_plotMirror != ( aSize.x < 0 ) )
  767. {
  768. fprintf( m_outputFile, "transform=\"scale(-1 1) translate(%f 0)\"\n",
  769. -2 * text_pos_dev.x );
  770. }
  771. fprintf( m_outputFile,
  772. "textLength=\"%.*f\" font-size=\"%.*f\" lengthAdjust=\"spacingAndGlyphs\"\n"
  773. "text-anchor=\"%s\" opacity=\"0\" stroke-opacity=\"0\">%s</text>\n",
  774. m_precision,
  775. sz_dev.x,
  776. m_precision,
  777. sz_dev.y,
  778. hjust,
  779. TO_UTF8( XmlEsc( aText ) ) );
  780. if( !aOrient.IsZero() )
  781. fputs( "</g>\n", m_outputFile );
  782. }
  783. // Output the text again as graphics with a <desc> tag (for non-WYSIWYG search and for
  784. // screen readers)
  785. {
  786. fprintf( m_outputFile,
  787. "<g class=\"stroked-text\"><desc>%s</desc>\n",
  788. TO_UTF8( XmlEsc( aText ) ) );
  789. PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aWidth,
  790. aItalic, aBold, aMultilineAllowed, aFont, aFontMetrics );
  791. fputs( "</g>", m_outputFile );
  792. }
  793. }
  794. void SVG_PLOTTER::PlotText( const VECTOR2I& aPos,
  795. const COLOR4D& aColor,
  796. const wxString& aText,
  797. const TEXT_ATTRIBUTES& aAttributes,
  798. KIFONT::FONT* aFont,
  799. const KIFONT::METRICS& aFontMetrics,
  800. void* aData )
  801. {
  802. VECTOR2I size = aAttributes.m_Size;
  803. if( aAttributes.m_Mirrored )
  804. size.x = -size.x;
  805. SVG_PLOTTER::Text( aPos, aColor, aText, aAttributes.m_Angle, size, aAttributes.m_Halign,
  806. aAttributes.m_Valign, aAttributes.m_StrokeWidth, aAttributes.m_Italic,
  807. aAttributes.m_Bold, aAttributes.m_Multiline, aFont, aFontMetrics, aData );
  808. }