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