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