KiCad PCB EDA Suite
SVG_plotter.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
10  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 /* Some info on basic items SVG format, used here:
31  * The root element of all SVG files is the <svg> element.
32  *
33  * The <g> element is used to group SVG shapes together.
34  * Once grouped you can transform the whole group of shapes as if it was a single shape.
35  * This is an advantage compared to a nested <svg> element
36  * which cannot be the target of transformation by itself.
37  *
38  * The <rect> element represents a rectangle.
39  * Using this element you can draw rectangles of various width, height,
40  * with different stroke (outline) and fill colors, with sharp or rounded corners etc.
41  *
42  * <svg xmlns="http://www.w3.org/2000/svg"
43  * xmlns:xlink="http://www.w3.org/1999/xlink">
44  *
45  * <rect x="10" y="10" height="100" width="100"
46  * style="stroke:#006600; fill: #00cc00"/>
47  *
48  * </svg>
49  *
50  * The <circle> element is used to draw circles.
51  * <circle cx="40" cy="40" r="24" style="stroke:#006600; fill:#00cc00"/>
52  *
53  * The <ellipse> element is used to draw ellipses.
54  * An ellipse is a circle that does not have equal height and width.
55  * Its radius in the x and y directions are different, in other words.
56  * <ellipse cx="40" cy="40" rx="30" ry="15"
57  * style="stroke:#006600; fill:#00cc00"/>
58  *
59  * The <line> element is used to draw lines.
60  *
61  * <line x1="0" y1="10" x2="0" y2="100" style="stroke:#006600;"/>
62  * <line x1="10" y1="10" x2="100" y2="100" style="stroke:#006600;"/>
63  *
64  * The <polyline> element is used to draw multiple connected lines
65  * Here is a simple example:
66  *
67  * <polyline points="0,0 30,0 15,30" style="stroke:#006600;"/>
68  *
69  * The <polygon> element is used to draw with multiple (3 or more) sides / edges.
70  * Here is a simple example:
71  *
72  * <polygon points="0,0 50,0 25,50" style="stroke:#660000; fill:#cc3333;"/>
73  *
74  * The <path> element is used to draw advanced shapes combined from lines and arcs,
75  * with or without fill.
76  * It is probably the most advanced and versatile SVG shape of them all.
77  * It is probably also the hardest element to master.
78  * <path d="M50,50
79  * A30,30 0 0,1 35,20
80  * L100,100
81  * M110,110
82  * L100,0"
83  * style="stroke:#660000; fill:none;"/>
84  *
85  * Draw an elliptic arc: it is one of basic path command:
86  * <path d="M(startx,starty) A(radiusx,radiusy)
87  * rotation-axe-x
88  * flag_arc_large,flag_sweep endx,endy">
89  * flag_arc_large: 0 = small arc > 180 deg, 1 = large arc > 180 deg
90  * flag_sweep : 0 = CCW, 1 = CW
91  * The center of ellipse is automatically calculated.
92  */
93 
94 #include <base64.h>
95 #include <fctsys.h>
96 #include <trigo.h>
97 #include <eda_base_frame.h>
98 #include <eda_rect.h>
99 #include <base_struct.h>
100 #include <common.h>
101 #include <plotter.h>
102 #include <macros.h>
103 #include <kicad_string.h>
104 
105 #include <cstdint>
106 #include <wx/mstream.h>
107 
108 
115 static wxString XmlEsc( const wxString& aStr, bool isAttribute = false )
116 {
117  wxString escaped;
118 
119  escaped.reserve( aStr.length() );
120 
121  for( wxString::const_iterator it = aStr.begin(); it != aStr.end(); ++it )
122  {
123  const wxChar c = *it;
124 
125  switch( c )
126  {
127  case wxS( '<' ):
128  escaped.append( wxS( "&lt;" ) );
129  break;
130  case wxS( '>' ):
131  escaped.append( wxS( "&gt;" ) );
132  break;
133  case wxS( '&' ):
134  escaped.append( wxS( "&amp;" ) );
135  break;
136  case wxS( '\r' ):
137  escaped.append( wxS( "&#xD;" ) );
138  break;
139  default:
140  if( isAttribute )
141  {
142  switch( c )
143  {
144  case wxS( '"' ):
145  escaped.append( wxS( "&quot;" ) );
146  break;
147  case wxS( '\t' ):
148  escaped.append( wxS( "&#x9;" ) );
149  break;
150  case wxS( '\n' ):
151  escaped.append( wxS( "&#xA;" ));
152  break;
153  default:
154  escaped.append(c);
155  }
156  }
157  else
158  escaped.append(c);
159  }
160  }
161 
162  return escaped;
163 }
164 
165 
167 {
168  m_graphics_changed = true;
170  m_fillMode = NO_FILL; // or FILLED_SHAPE or FILLED_WITH_BG_BODYCOLOR
171  m_pen_rgb_color = 0; // current color value (black)
172  m_brush_rgb_color = 0; // current color value (black)
174 }
175 
176 
177 void SVG_PLOTTER::SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
178  double aScale, bool aMirror )
179 {
180  m_plotMirror = aMirror;
181  m_yaxisReversed = true; // unlike other plotters, SVG has Y axis reversed
182  plotOffset = aOffset;
183  plotScale = aScale;
184  m_IUsPerDecimil = aIusPerDecimil;
185  iuPerDeviceUnit = 1.0 / aIusPerDecimil;
186  /* Compute the paper size in IUs */
188  paperSize.x *= 10.0 * aIusPerDecimil;
189  paperSize.y *= 10.0 * aIusPerDecimil;
190  SetDefaultLineWidth( 100 * aIusPerDecimil ); // arbitrary default
191 }
192 
193 
195 {
197 
198  if( m_graphics_changed )
199  setSVGPlotStyle();
200 }
201 
202 
204 {
205  if( m_fillMode != fill )
206  {
207  m_graphics_changed = true;
208  m_fillMode = fill;
209  }
210 }
211 
212 
213 void SVG_PLOTTER::setSVGPlotStyle( bool aIsGroup, const std::string& aExtraStyle )
214 {
215  if( aIsGroup )
216  fputs( "</g>\n<g ", outputFile );
217 
218  // output the background fill color
219  fprintf( outputFile, "style=\"fill:#%6.6lX; ", m_brush_rgb_color );
220 
221  switch( m_fillMode )
222  {
223  case NO_FILL:
224  fputs( "fill-opacity:0.0; ", outputFile );
225  break;
226 
227  case FILLED_SHAPE:
228  fputs( "fill-opacity:1.0; ", outputFile );
229  break;
230 
232  fputs( "fill-opacity:0.6; ", outputFile );
233  break;
234  }
235 
236  double pen_w = userToDeviceSize( GetCurrentLineWidth() );
237  fprintf( outputFile, "\nstroke:#%6.6lX; stroke-width:%g; stroke-opacity:1; \n",
238  m_pen_rgb_color, pen_w );
239  fputs( "stroke-linecap:round; stroke-linejoin:round;", outputFile );
240 
241  //set any extra attributes for non-solid lines
242  switch( m_dashed )
243  {
245  fprintf( outputFile, "stroke-dasharray:%g,%g;", GetDashMarkLenIU(), GetDashGapLenIU() );
246  break;
247  case PLOT_DASH_TYPE::DOT:
248  fprintf( outputFile, "stroke-dasharray:%g,%g;", GetDotMarkLenIU(), GetDashGapLenIU() );
249  break;
251  fprintf( outputFile, "stroke-dasharray:%g,%g,%g,%g;", GetDashMarkLenIU(), GetDashGapLenIU(),
253  break;
256  default:
257  //do nothing
258  break;
259  }
260 
261  if( aExtraStyle.length() )
262  {
263  fputs( aExtraStyle.c_str(), outputFile );
264  }
265 
266  fputs( "\"", outputFile );
267 
268  if( aIsGroup )
269  {
270  fputs( ">", outputFile );
271  m_graphics_changed = false;
272  }
273 
274  fputs( "\n", outputFile );
275 }
276 
277 /* Set the current line width (in IUs) for the next plot
278  */
279 void SVG_PLOTTER::SetCurrentLineWidth( int width, void* aData )
280 {
281  int pen_width;
282 
283  if( width >= 0 )
284  pen_width = width;
285  else
286  pen_width = defaultPenWidth;
287 
288  if( pen_width != currentPenWidth )
289  {
290  m_graphics_changed = true;
291  currentPenWidth = pen_width;
292  }
293 
294  if( m_graphics_changed )
295  setSVGPlotStyle();
296 }
297 
298 
299 void SVG_PLOTTER::StartBlock( void* aData )
300 {
301  std::string* idstr = reinterpret_cast<std::string*>( aData );
302 
303  fputs( "<g ", outputFile );
304  if( idstr )
305  fprintf( outputFile, "id=\"%s\"", idstr->c_str() );
306 
307  fprintf( outputFile, ">\n" );
308 }
309 
310 
311 void SVG_PLOTTER::EndBlock( void* aData )
312 {
313  fprintf( outputFile, "</g>\n" );
314 
315  m_graphics_changed = true;
316 }
317 
318 
319 /* initialize m_red, m_green, m_blue ( 0 ... 255)
320  * from reduced values r, g ,b ( 0.0 to 1.0 )
321  */
322 void SVG_PLOTTER::emitSetRGBColor( double r, double g, double b )
323 {
324  int red = (int) ( 255.0 * r );
325  int green = (int) ( 255.0 * g );
326  int blue = (int) ( 255.0 * b );
327  long rgb_color = (red << 16) | (green << 8) | blue;
328 
329  if( m_pen_rgb_color != rgb_color )
330  {
331  m_graphics_changed = true;
332  m_pen_rgb_color = rgb_color;
333 
334  // Currently, use the same color for brush and pen
335  // (i.e. to draw and fill a contour)
336  m_brush_rgb_color = rgb_color;
337  }
338 }
339 
340 
345 {
346  if( m_dashed != dashed )
347  {
348  m_graphics_changed = true;
349  m_dashed = dashed;
350  }
351 
352  if( m_graphics_changed )
353  setSVGPlotStyle();
354 }
355 
356 
357 void SVG_PLOTTER::Rect( const wxPoint& p1, const wxPoint& p2, FILL_T fill, int width )
358 {
359  EDA_RECT rect( p1, wxSize( p2.x -p1.x, p2.y -p1.y ) );
360  rect.Normalize();
361  DPOINT org_dev = userToDeviceCoordinates( rect.GetOrigin() );
362  DPOINT end_dev = userToDeviceCoordinates( rect.GetEnd() );
363  DSIZE size_dev = end_dev - org_dev;
364  // Ensure size of rect in device coordinates is > 0
365  // I don't know if this is a SVG issue or a Inkscape issue, but
366  // Inkscape has problems with negative or null values for width and/or height, so avoid them
367  DBOX rect_dev( org_dev, size_dev);
368  rect_dev.Normalize();
369 
370  setFillMode( fill );
371  SetCurrentLineWidth( width );
372 
373  // Rectangles having a 0 size value for height or width are just not drawn on Inscape,
374  // so use a line when happens.
375  if( rect_dev.GetSize().x == 0.0 || rect_dev.GetSize().y == 0.0 ) // Draw a line
376  fprintf( outputFile,
377  "<line x1=\"%g\" y1=\"%g\" x2=\"%g\" y2=\"%g\" />\n",
378  rect_dev.GetPosition().x, rect_dev.GetPosition().y,
379  rect_dev.GetEnd().x, rect_dev.GetEnd().y
380  );
381 
382  else
383  fprintf( outputFile,
384  "<rect x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\" rx=\"%g\" />\n",
385  rect_dev.GetPosition().x, rect_dev.GetPosition().y,
386  rect_dev.GetSize().x, rect_dev.GetSize().y,
387  0.0 // radius of rounded corners
388  );
389 }
390 
391 
392 void SVG_PLOTTER::Circle( const wxPoint& pos, int diametre, FILL_T fill, int width )
393 {
394  DPOINT pos_dev = userToDeviceCoordinates( pos );
395  double radius = userToDeviceSize( diametre / 2.0 );
396 
397  setFillMode( fill );
398  SetCurrentLineWidth( width );
399 
400  // If diameter is less than width, switch to filled mode
401  if( fill == NO_FILL && diametre < width )
402  {
404  SetCurrentLineWidth( 0 );
405 
406  radius = userToDeviceSize( ( diametre / 2.0 ) + ( width / 2.0 ) );
407  }
408 
409  fprintf( outputFile,
410  "<circle cx=\"%g\" cy=\"%g\" r=\"%g\" /> \n",
411  pos_dev.x, pos_dev.y, radius );
412 }
413 
414 
415 void SVG_PLOTTER::Arc( const wxPoint& centre, double StAngle, double EndAngle, int radius,
416  FILL_T fill, int width )
417 {
418  /* Draws an arc of a circle, centred on (xc,yc), with starting point
419  * (x1, y1) and ending at (x2, y2). The current pen is used for the outline
420  * and the current brush for filling the shape.
421  *
422  * The arc is drawn in an anticlockwise direction from the start point to
423  * the end point
424  */
425 
426  if( radius <= 0 )
427  {
428  Circle( centre, width, FILLED_SHAPE, 0 );
429  return;
430  }
431 
432  if( StAngle > EndAngle )
433  std::swap( StAngle, EndAngle );
434 
435  // Calculate start point.
436  DPOINT centre_dev = userToDeviceCoordinates( centre );
437  double radius_dev = userToDeviceSize( radius );
438 
439  if( !m_yaxisReversed ) // Should be never the case
440  {
441  double tmp = StAngle;
442  StAngle = -EndAngle;
443  EndAngle = -tmp;
444  }
445 
446  if( m_plotMirror )
447  {
449  {
450  StAngle = 1800.0 -StAngle;
451  EndAngle = 1800.0 -EndAngle;
452  std::swap( StAngle, EndAngle );
453  }
454  else
455  {
456  StAngle = -StAngle;
457  EndAngle = -EndAngle;
458  }
459  }
460 
461  DPOINT start;
462  start.x = radius_dev;
463  RotatePoint( &start.x, &start.y, StAngle );
464  DPOINT end;
465  end.x = radius_dev;
466  RotatePoint( &end.x, &end.y, EndAngle );
467  start += centre_dev;
468  end += centre_dev;
469 
470  double theta1 = DECIDEG2RAD( StAngle );
471 
472  if( theta1 < 0 )
473  theta1 = theta1 + M_PI * 2;
474 
475  double theta2 = DECIDEG2RAD( EndAngle );
476 
477  if( theta2 < 0 )
478  theta2 = theta2 + M_PI * 2;
479 
480  if( theta2 < theta1 )
481  theta2 = theta2 + M_PI * 2;
482 
483  int flg_arc = 0; // flag for large or small arc. 0 means less than 180 degrees
484 
485  if( fabs( theta2 - theta1 ) > M_PI )
486  flg_arc = 1;
487 
488  int flg_sweep = 0; // flag for sweep always 0
489 
490  // Draw a single arc: an arc is one of 3 curve commands (2 other are 2 bezier curves)
491  // params are start point, radius1, radius2, X axe rotation,
492  // flag arc size (0 = small arc > 180 deg, 1 = large arc > 180 deg),
493  // sweep arc ( 0 = CCW, 1 = CW),
494  // end point
495  if( fill != NO_FILL )
496  {
497  // Filled arcs (in eeschema) consist of the pie wedge and a stroke only on the arc
498  // This needs to be drawn in two steps.
499  setFillMode( fill );
500  SetCurrentLineWidth( 0 );
501 
502  fprintf( outputFile, "<path d=\"M%g %g A%g %g 0.0 %d %d %g %g L %g %g Z\" />\n",
503  start.x, start.y, radius_dev, radius_dev,
504  flg_arc, flg_sweep,
505  end.x, end.y, centre_dev.x, centre_dev.y );
506  }
507 
508  setFillMode( NO_FILL );
509  SetCurrentLineWidth( width );
510  fprintf( outputFile, "<path d=\"M%g %g A%g %g 0.0 %d %d %g %g\" />\n",
511  start.x, start.y, radius_dev, radius_dev,
512  flg_arc, flg_sweep,
513  end.x, end.y );
514 }
515 
516 
517 void SVG_PLOTTER::BezierCurve( const wxPoint& aStart, const wxPoint& aControl1,
518  const wxPoint& aControl2, const wxPoint& aEnd,
519  int aTolerance, int aLineThickness )
520 {
521 #if 1
522  setFillMode( NO_FILL );
523  SetCurrentLineWidth( aLineThickness );
524 
525  DPOINT start = userToDeviceCoordinates( aStart );
526  DPOINT ctrl1 = userToDeviceCoordinates( aControl1 );
527  DPOINT ctrl2 = userToDeviceCoordinates( aControl2 );
528  DPOINT end = userToDeviceCoordinates( aEnd );
529 
530  // Generate a cubic curve: start point and 3 other control points.
531  fprintf( outputFile, "<path d=\"M%g,%g C%g,%g %g,%g %g,%g\" />\n",
532  start.x, start.y, ctrl1.x, ctrl1.y,
533  ctrl2.x, ctrl2.y, end.x, end.y );
534 #else
535  PLOTTER::BezierCurve( aStart, aControl1,aControl2, aEnd,aTolerance, aLineThickness );
536 #endif
537 }
538 
539 
540 void SVG_PLOTTER::PlotPoly( const std::vector<wxPoint>& aCornerList,
541  FILL_T aFill, int aWidth, void * aData )
542 {
543  if( aCornerList.size() <= 1 )
544  return;
545 
546  setFillMode( aFill );
547  SetCurrentLineWidth( aWidth );
548  fprintf( outputFile, "<path ");
549 
550  switch( aFill )
551  {
552  case NO_FILL:
553  setSVGPlotStyle( false, "fill:none" );
554  break;
555 
557  case FILLED_SHAPE:
558  setSVGPlotStyle( false, "fill-rule:evenodd;" );
559  break;
560  }
561 
562  DPOINT pos = userToDeviceCoordinates( aCornerList[0] );
563  fprintf( outputFile, "d=\"M %g,%g\n", pos.x, pos.y );
564 
565  for( unsigned ii = 1; ii < aCornerList.size() - 1; ii++ )
566  {
567  pos = userToDeviceCoordinates( aCornerList[ii] );
568  fprintf( outputFile, "%g,%g\n", pos.x, pos.y );
569  }
570 
571  // If the cornerlist ends where it begins, then close the poly
572  if( aCornerList.front() == aCornerList.back() )
573  fprintf( outputFile, "Z\" /> \n" );
574  else
575  {
576  pos = userToDeviceCoordinates( aCornerList.back() );
577  fprintf( outputFile, "%g,%g\n\" /> \n", pos.x, pos.y );
578  }
579 }
580 
581 
585 void SVG_PLOTTER::PlotImage( const wxImage& aImage, const wxPoint& aPos,
586  double aScaleFactor )
587 {
588  wxSize pix_size( aImage.GetWidth(), aImage.GetHeight() );
589 
590  // Requested size (in IUs)
591  DPOINT drawsize( aScaleFactor * pix_size.x,
592  aScaleFactor * pix_size.y );
593 
594  // calculate the bitmap start position
595  wxPoint start( aPos.x - drawsize.x / 2,
596  aPos.y - drawsize.y / 2);
597 
598  // Rectangles having a 0 size value for height or width are just not drawn on Inscape,
599  // so use a line when happens.
600  if( drawsize.x == 0.0 || drawsize.y == 0.0 ) // Draw a line
601  {
602  PLOTTER::PlotImage( aImage, aPos, aScaleFactor );
603  }
604  else
605  {
606  wxMemoryOutputStream img_stream;
607 
608  aImage.SaveFile( img_stream, wxBITMAP_TYPE_PNG );
609  size_t input_len = img_stream.GetOutputStreamBuffer()->GetBufferSize();
610  std::vector<uint8_t> buffer( input_len );
611  std::vector<uint8_t> encoded;
612 
613  img_stream.CopyTo( buffer.data(), buffer.size() );
614  base64::encode( buffer, encoded );
615 
616  fprintf( outputFile,
617  "<image x=\"%g\" y=\"%g\" xlink:href=\"data:image/png;base64,",
618  userToDeviceSize( start.x ), userToDeviceSize( start.y )
619  );
620 
621  for( size_t i = 0; i < encoded.size(); i++ )
622  {
623  fprintf( outputFile, "%c", static_cast<char>( encoded[i] ) );
624 
625  if( ( i % 64 ) == 63 )
626  fprintf( outputFile, "\n" );
627  }
628 
629  fprintf( outputFile, "\"\npreserveAspectRatio=\"none\" height=\"%g\" width=\"%g\" />",
630  userToDeviceSize( drawsize.x ), userToDeviceSize( drawsize.y ) );
631  }
632 
633 
634 }
635 
636 
637 void SVG_PLOTTER::PenTo( const wxPoint& pos, char plume )
638 {
639  if( plume == 'Z' )
640  {
641  if( penState != 'Z' )
642  {
643  fputs( "\" />\n", outputFile );
644  penState = 'Z';
645  penLastpos.x = -1;
646  penLastpos.y = -1;
647  }
648 
649  return;
650  }
651 
652  if( penState == 'Z' ) // here plume = 'D' or 'U'
653  {
654  DPOINT pos_dev = userToDeviceCoordinates( pos );
655 
656  // Ensure we do not use a fill mode when moving tne pen,
657  // in SVG mode (i;e. we are plotting only basic lines, not a filled area
658  if( m_fillMode != NO_FILL )
659  {
660  setFillMode( NO_FILL );
661  setSVGPlotStyle();
662  }
663 
664  fprintf( outputFile, "<path d=\"M%d %d\n",
665  (int) pos_dev.x, (int) pos_dev.y );
666  }
667  else if( penState != plume || pos != penLastpos )
668  {
669  DPOINT pos_dev = userToDeviceCoordinates( pos );
670  fprintf( outputFile, "L%d %d\n",
671  (int) pos_dev.x, (int) pos_dev.y );
672  }
673 
674  penState = plume;
675  penLastpos = pos;
676 }
677 
678 
684 {
685  wxASSERT( outputFile );
686  wxString msg;
687 
688  static const char* header[] =
689  {
690  "<?xml version=\"1.0\" standalone=\"no\"?>\n",
691  " <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \n",
692  " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> \n",
693  "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" \n",
694  NULL
695  };
696 
697  // Write header.
698  for( int ii = 0; header[ii] != NULL; ii++ )
699  {
700  fputs( header[ii], outputFile );
701  }
702 
703  // Write viewport pos and size
704  wxPoint origin; // TODO set to actual value
705  fprintf( outputFile,
706  " width=\"%gcm\" height=\"%gcm\" viewBox=\"%d %d %d %d\">\n",
707  (double) paperSize.x / m_IUsPerDecimil * 2.54 / 10000,
708  (double) paperSize.y / m_IUsPerDecimil * 2.54 / 10000,
709  origin.x, origin.y,
710  (int) ( paperSize.x / m_IUsPerDecimil ),
711  (int) ( paperSize.y / m_IUsPerDecimil) );
712 
713  // Write title
714  char date_buf[250];
715  time_t ltime = time( NULL );
716  strftime( date_buf, 250, "%Y/%m/%d %H:%M:%S",
717  localtime( &ltime ) );
718 
719  fprintf( outputFile,
720  "<title>SVG Picture created as %s date %s </title>\n",
721  TO_UTF8( XmlEsc( wxFileName( filename ).GetFullName() ) ), date_buf );
722  // End of header
723  fprintf( outputFile, " <desc>Picture generated by %s </desc>\n",
724  TO_UTF8( XmlEsc( creator ) ) );
725 
726  // output the pen and brush color (RVB values in hex) and opacity
727  double opacity = 1.0; // 0.0 (transparent to 1.0 (solid)
728  fprintf( outputFile,
729  "<g style=\"fill:#%6.6lX; fill-opacity:%g;stroke:#%6.6lX; stroke-opacity:%g;\n",
730  m_brush_rgb_color, opacity, m_pen_rgb_color, opacity );
731 
732  // output the pen cap and line joint
733  fputs( "stroke-linecap:round; stroke-linejoin:round;\"\n", outputFile );
734  fputs( " transform=\"translate(0 0) scale(1 1)\">\n", outputFile );
735  return true;
736 }
737 
738 
740 {
741  fputs( "</g> \n</svg>\n", outputFile );
742  fclose( outputFile );
743  outputFile = NULL;
744 
745  return true;
746 }
747 
748 
749 void SVG_PLOTTER::Text( const wxPoint& aPos,
750  const COLOR4D aColor,
751  const wxString& aText,
752  double aOrient,
753  const wxSize& aSize,
754  enum EDA_TEXT_HJUSTIFY_T aH_justify,
755  enum EDA_TEXT_VJUSTIFY_T aV_justify,
756  int aWidth,
757  bool aItalic,
758  bool aBold,
759  bool aMultilineAllowed,
760  void* aData )
761 {
762  setFillMode( NO_FILL );
763  SetColor( aColor );
764  SetCurrentLineWidth( aWidth );
765 
766  int width = currentPenWidth;
767 
768  if( aWidth <= 0 && aBold )
769  width = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
770 
771  if( aWidth <= 0 )
772  width = currentPenWidth;
773 
774  wxPoint text_pos = aPos;
775  const char *hjust = "start";
776 
777  switch( aH_justify )
778  {
780  hjust = "middle";
781  break;
782 
784  hjust = "end";
785  break;
786 
788  hjust = "start";
789  break;
790  }
791 
792  switch( aV_justify )
793  {
795  text_pos.y += aSize.y / 2;
796  break;
797 
799  text_pos.y += aSize.y;
800  break;
801 
803  break;
804  }
805 
806  wxSize text_size;
807  // aSize.x or aSize.y is < 0 for mirrored texts.
808  // The actual text size value is the absolue value
809  text_size.x = std::abs( GraphicTextWidth( aText, aSize, aItalic, width ) );
810  text_size.y = std::abs( aSize.x * 4/3 ); // Hershey font height to em size conversion
811  DPOINT anchor_pos_dev = userToDeviceCoordinates( aPos );
812  DPOINT text_pos_dev = userToDeviceCoordinates( text_pos );
813  DPOINT sz_dev = userToDeviceSize( text_size );
814 
815  if( aOrient != 0 ) {
816  fprintf( outputFile,
817  "<g transform=\"rotate(%g %g %g)\">\n",
818  - aOrient * 0.1, anchor_pos_dev.x, anchor_pos_dev.y );
819  }
820 
821  fprintf( outputFile,
822  "<text x=\"%g\" y=\"%g\"\n"
823  "textLength=\"%g\" font-size=\"%g\" lengthAdjust=\"spacingAndGlyphs\"\n"
824  "text-anchor=\"%s\" opacity=\"0\">%s</text>\n",
825  text_pos_dev.x, text_pos_dev.y,
826  sz_dev.x, sz_dev.y,
827  hjust, TO_UTF8( XmlEsc( aText ) ) );
828 
829  if( aOrient != 0 )
830  fputs( "</g>\n", outputFile );
831 
832  fprintf( outputFile,
833  "<g class=\"stroked-text\"><desc>%s</desc>\n",
834  TO_UTF8( XmlEsc( aText ) ) );
835  PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify,
836  aWidth, aItalic, aBold, aMultilineAllowed );
837  fputs( "</g>", outputFile );
838 }
double GetDotMarkLenIU() const
Definition: plotter.cpp:139
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:44
virtual void SetDash(PLOT_DASH_TYPE dashed) override
SVG supports dashed lines.
virtual void PlotImage(const wxImage &aImage, const wxPoint &aPos, double aScaleFactor) override
Postscript-likes at the moment are the only plot engines supporting bitmaps...
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
Definition: gr_text.cpp:66
const Vec GetEnd() const
Definition: box2.h:194
FILL_T m_fillMode
Definition: plotter.h:1007
void setFillMode(FILL_T fill)
function setFillMode() prepare parameters for setSVGPlotStyle()
int color
Definition: DXF_plotter.cpp:61
virtual void PenTo(const wxPoint &pos, char plume) override
moveto/lineto primitive, moves the 'pen' to the specified direction
void setSVGPlotStyle(bool aIsGroup=true, const std::string &aExtraStyle={})
function setSVGPlotStyle() output the string which define pen and brush color, shape,...
virtual void BezierCurve(const wxPoint &aStart, const wxPoint &aControl1, const wxPoint &aControl2, const wxPoint &aEnd, int aTolerance, int aLineThickness=USE_DEFAULT_LINE_WIDTH)
Generic fallback: Cubic Bezier curve rendered as a polyline In Kicad the bezier curves have 4 control...
Definition: plotter.cpp:203
int GraphicTextWidth(const wxString &aText, const wxSize &aSize, bool aItalic, bool aBold)
Function GraphicTextWidth.
Definition: gr_text.cpp:107
char penState
Current pen state: 'U', 'D' or 'Z' (see PenTo)
Definition: plotter.h:592
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
double m_IUsPerDecimil
Definition: plotter.h:569
wxPoint plotOffset
Plot offset (in IUs)
Definition: plotter.h:575
wxPoint penLastpos
Last pen positions; set to -1,-1 when the pen is at rest.
Definition: plotter.h:594
static wxString XmlEsc(const wxString &aStr, bool isAttribute=false)
Function XmlEsc translates '<' to "<", '>' to ">" and so on, according to the spec: http://www....
PAGE_INFO pageInfo
Definition: plotter.h:598
PLOT_DASH_TYPE m_dashed
Definition: plotter.h:1018
This file contains miscellaneous commonly used macros and functions.
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL)
Draws text with the plotter.
Definition: gr_text.cpp:232
void encode(const std::vector< uint8_t > &aInput, std::vector< uint8_t > &aOutput)
Definition: base64.cpp:76
double GetDashGapLenIU() const
Definition: plotter.cpp:151
virtual int GetCurrentLineWidth() const
Definition: plotter.h:158
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL) override
Draws text with the plotter.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
virtual bool EndPlot() override
bool m_yaxisReversed
true to mirror horizontally (else vertically)
Definition: plotter.h:581
const wxPoint GetEnd() const
Definition: eda_rect.h:116
virtual void SetColor(COLOR4D color) override
The SetColor implementation is split with the subclasses: The PSLIKE computes the rgb values,...
bool m_plotMirror
X axis orientation (SVG) and plot mirrored (only for PS, PDF HPGL and SVG)
Definition: plotter.h:579
#define NULL
virtual void SetDefaultLineWidth(int width) override
Set the default line width.
Definition: PS_plotter.cpp:53
virtual void SetTextMode(PLOT_TEXT_MODE mode) override
PS and PDF fully implement native text (for the Latin-1 subset)
Definition: plotter.h:704
long m_pen_rgb_color
Definition: plotter.h:1009
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
virtual void Arc(const wxPoint &centre, double StAngle, double EndAngle, int rayon, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
Generic fallback: arc rendered as a polyline.
BOX2< Vec > & Normalize()
Function Normalize ensures that the height ant width are positive.
Definition: box2.h:128
virtual void SetColor(COLOR4D color) override
The SetColor implementation is split with the subclasses: The PSLIKE computes the rgb values,...
Definition: PS_plotter.cpp:60
Base window classes and related definitions.
double plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plotter.h:563
virtual bool StartPlot() override
The code within this function creates SVG files header.
const wxSize & GetSizeMils() const
Definition: page_info.h:142
virtual DPOINT userToDeviceSize(const wxSize &size)
Modifies size according to the plotter scale factors (wxSize version, returns a DPOINT)
Definition: plotter.cpp:124
virtual void SetCurrentLineWidth(int width, void *aData=NULL) override
Set the line width for the next drawing.
virtual void Circle(const wxPoint &pos, int diametre, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
PLOT_DASH_TYPE
Enum for choosing dashed line type.
Definition: plotter.h:86
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:51
const Vec & GetPosition() const
Definition: box2.h:193
virtual DPOINT userToDeviceCoordinates(const wxPoint &aCoordinate)
Modifies coordinates according to the orientation, scale factor, and offsets trace.
Definition: plotter.cpp:99
void Normalize()
Function Normalize ensures that the height ant width are positive.
virtual void PlotImage(const wxImage &aImage, const wxPoint &aPos, double aScaleFactor)
Function PlotImage Only Postscript plotters can plot bitmaps for plotters that cannot plot a bitmap,...
Definition: plotter.cpp:231
int currentPenWidth
Definition: plotter.h:590
int defaultPenWidth
true to generate a negative image (PS mode mainly)
Definition: plotter.h:589
double GetDashMarkLenIU() const
Definition: plotter.cpp:145
bool m_graphics_changed
Definition: plotter.h:1015
long m_brush_rgb_color
Definition: plotter.h:1013
virtual void EndBlock(void *aData) override
calling this function allows one to define the end of a group of drawing items the group is started b...
FILE * outputFile
true if the Y axis is top to bottom (SVG)
Definition: plotter.h:584
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
double DECIDEG2RAD(double deg)
Definition: trigo.h:214
virtual void emitSetRGBColor(double r, double g, double b) override
function emitSetRGBColor() initialize m_pen_rgb_color from reduced values r, g ,b ( reduced values ar...
The common library.
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:42
virtual void StartBlock(void *aData) override
calling this function allows one to define the beginning of a group of drawing items (used in SVG for...
const Vec & GetSize() const
Definition: box2.h:188
double iuPerDeviceUnit
Device scale (from IUs to plotter device units - usually decimils)
Definition: plotter.h:572
wxString filename
Definition: plotter.h:596
virtual void BezierCurve(const wxPoint &aStart, const wxPoint &aControl1, const wxPoint &aControl2, const wxPoint &aEnd, int aTolerance, int aLineThickness=USE_DEFAULT_LINE_WIDTH) override
Generic fallback: Cubic Bezier curve rendered as a polyline In Kicad the bezier curves have 4 control...
wxString creator
Definition: plotter.h:595
wxSize paperSize
Paper size in IU - not in mils.
Definition: plotter.h:600
bool m_mirrorIsHorizontal
Definition: plotter.h:580
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=NULL) override
Function PlotPoly.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39