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::emitSetRGBColor( double r, double g, double b, double a )
343{
344 uint32_t red = (uint32_t) ( 255.0 * r );
345 uint32_t green = (uint32_t) ( 255.0 * g );
346 uint32_t blue = (uint32_t) ( 255.0 * b );
347 uint32_t rgb_color = ( red << 16 ) | ( green << 8 ) | blue;
348
349 if( m_pen_rgb_color != rgb_color || m_brush_alpha != a )
350 {
351 m_graphics_changed = true;
352 m_pen_rgb_color = rgb_color;
353
354 // Currently, use the same color for brush and pen (i.e. to draw and fill a contour).
355 m_brush_rgb_color = rgb_color;
356 m_brush_alpha = a;
357 }
358}
359
360
361void SVG_PLOTTER::SetDash( int aLineWidth, LINE_STYLE aLineStyle )
362{
363 if( m_dashed != aLineStyle )
364 {
365 m_graphics_changed = true;
366 m_dashed = aLineStyle;
367 }
368}
369
370
371void SVG_PLOTTER::Rect( const VECTOR2I& p1, const VECTOR2I& p2, FILL_T fill, int width,
372 int aCornerRadius )
373{
374 BOX2I rect( p1, VECTOR2I( p2.x - p1.x, p2.y - p1.y ) );
375 rect.Normalize();
376
377 VECTOR2D org_dev = userToDeviceCoordinates( rect.GetOrigin() );
378 VECTOR2D end_dev = userToDeviceCoordinates( rect.GetEnd() );
379 VECTOR2D size_dev = end_dev - org_dev;
380
381 // Ensure size of rect in device coordinates is > 0
382 // I don't know if this is a SVG issue or a Inkscape issue, but
383 // Inkscape has problems with negative or null values for width and/or height, so avoid them
384 BOX2D rect_dev( org_dev, size_dev );
385 rect_dev.Normalize();
386
387 setFillMode( fill );
388 SetCurrentLineWidth( width );
389
392
393 // Rectangles having a 0 size value for height or width are just not drawn on Inkscape,
394 // so use a line when happens.
395 if( rect_dev.GetSize().x == 0.0 || rect_dev.GetSize().y == 0.0 ) // Draw a line
396 {
397 fmt::print( m_outputFile,
398 "<line x1=\"{:.{}f}\" y1=\"{:.{}f}\" x2=\"{:.{}f}\" y2=\"{:.{}f}\" />\n",
399 rect_dev.GetPosition().x, m_precision,
400 rect_dev.GetPosition().y, m_precision,
401 rect_dev.GetEnd().x, m_precision,
402 rect_dev.GetEnd().y, m_precision );
403 }
404 else
405 {
406 fmt::print( m_outputFile,
407 "<rect x=\"{:f}\" y=\"{:f}\" width=\"{:f}\" height=\"{:f}\" rx=\"{:f}\" />\n",
408 rect_dev.GetPosition().x,
409 rect_dev.GetPosition().y,
410 rect_dev.GetSize().x,
411 rect_dev.GetSize().y,
412 userToDeviceSize( aCornerRadius ) );
413 }
414}
415
416
417void SVG_PLOTTER::Circle( const VECTOR2I& pos, int diametre, FILL_T fill, int width )
418{
419 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
420 double radius = userToDeviceSize( diametre / 2.0 );
421
422 setFillMode( fill );
423 SetCurrentLineWidth( width );
424
427
428 // If diameter is less than width, switch to filled mode
429 if( fill == FILL_T::NO_FILL && diametre < GetCurrentLineWidth() )
430 {
432 width = GetCurrentLineWidth();
434
435 radius = userToDeviceSize( ( diametre / 2.0 ) + ( width / 2.0 ) );
436 }
437
438 fmt::print( m_outputFile,
439 "<circle cx=\"{:.{}f}\" cy=\"{:.{}f}\" r=\"{:.{}f}\" /> \n",
440 pos_dev.x, m_precision,
441 pos_dev.y, m_precision,
443}
444
445
446void SVG_PLOTTER::Arc( const VECTOR2D& aCenter, const EDA_ANGLE& aStartAngle,
447 const EDA_ANGLE& aAngle, double aRadius, FILL_T aFill, int aWidth )
448{
449 /* Draws an arc of a circle, centered on (xc,yc), with starting point (x1, y1) and ending
450 * at (x2, y2). The current pen is used for the outline and the current brush for filling
451 * the shape.
452 *
453 * The arc is drawn in an anticlockwise direction from the start point to the end point.
454 */
455 if( aRadius <= 0 )
456 {
457 Circle( aCenter, aWidth, FILL_T::FILLED_SHAPE, 0 );
458 return;
459 }
460
461 EDA_ANGLE startAngle = -aStartAngle;
462 EDA_ANGLE endAngle = startAngle - aAngle;
463
464 if( endAngle < startAngle )
465 std::swap( startAngle, endAngle );
466
467 // Calculate start point.
468 VECTOR2D centre_device = userToDeviceCoordinates( aCenter );
469 double radius_device = userToDeviceSize( aRadius );
470
471 if( m_plotMirror )
472 {
474 {
475 std::swap( startAngle, endAngle );
476 startAngle = ANGLE_180 - startAngle;
477 endAngle = ANGLE_180 - endAngle;
478 }
479 else
480 {
481 startAngle = -startAngle;
482 endAngle = -endAngle;
483 }
484 }
485
486 VECTOR2D start;
487 start.x = radius_device;
488 RotatePoint( start, startAngle );
490 end.x = radius_device;
491 RotatePoint( end, endAngle );
492 start += centre_device;
493 end += centre_device;
494
495 double theta1 = startAngle.AsRadians();
496
497 if( theta1 < 0 )
498 theta1 = theta1 + M_PI * 2;
499
500 double theta2 = endAngle.AsRadians();
501
502 if( theta2 < 0 )
503 theta2 = theta2 + M_PI * 2;
504
505 if( theta2 < theta1 )
506 theta2 = theta2 + M_PI * 2;
507
508 int flg_arc = 0; // flag for large or small arc. 0 means less than 180 degrees
509
510 if( fabs( theta2 - theta1 ) > M_PI )
511 flg_arc = 1;
512
513 int flg_sweep = 0; // flag for sweep always 0
514
515 // Draw a single arc: an arc is one of 3 curve commands (2 other are 2 bezier curves)
516 // params are start point, radius1, radius2, X axe rotation,
517 // flag arc size (0 = small arc > 180 deg, 1 = large arc > 180 deg),
518 // sweep arc ( 0 = CCW, 1 = CW),
519 // end point
520 if( aFill != FILL_T::NO_FILL )
521 {
522 // Filled arcs (in Eeschema) consist of the pie wedge and a stroke only on the arc
523 // This needs to be drawn in two steps.
524 setFillMode( aFill );
526
529
530 fmt::print( m_outputFile,
531 "<path d=\"M{:.{}f} {:.{}f} A{:.{}f} {:.{}f} 0.0 {:d} {:d} {:.{}f} {:.{}f} L {:.{}f} {:.{}f} Z\" />\n",
532 start.x, m_precision,
533 start.y, m_precision,
534 radius_device, m_precision,
535 radius_device, m_precision,
536 flg_arc,
537 flg_sweep,
538 end.x, m_precision,
539 end.y, m_precision,
540 centre_device.x, m_precision,
541 centre_device.y, m_precision );
542 }
543
545 SetCurrentLineWidth( aWidth );
546
549
550 fmt::print( m_outputFile,
551 "<path d=\"M{:.{}f} {:.{}f} A{:.{}f} {:.{}f} 0.0 {:d} {:d} {:.{}f} {:.{}f}\" />\n",
552 start.x, m_precision,
553 start.y, m_precision,
554 radius_device, m_precision,
555 radius_device, m_precision,
556 flg_arc,
557 flg_sweep,
558 end.x, m_precision,
559 end.y, m_precision );
560}
561
562
563void SVG_PLOTTER::BezierCurve( const VECTOR2I& aStart, const VECTOR2I& aControl1,
564 const VECTOR2I& aControl2, const VECTOR2I& aEnd,
565 int aTolerance, int aLineThickness )
566{
567#if 1
569 SetCurrentLineWidth( aLineThickness );
570
573
574 VECTOR2D start = userToDeviceCoordinates( aStart );
575 VECTOR2D ctrl1 = userToDeviceCoordinates( aControl1 );
576 VECTOR2D ctrl2 = userToDeviceCoordinates( aControl2 );
578
579 // Generate a cubic curve: start point and 3 other control points.
580 fmt::print( m_outputFile,
581 "<path d=\"M{:.{}f},{:.{}f} C{:.{}f},{:.{}f} {:.{}f},{:.{}f} {:.{}f},{:.{}f}\" />\n",
582 start.x, m_precision,
583 start.y, m_precision,
584 ctrl1.x,m_precision,
585 ctrl1.y, m_precision,
586 ctrl2.x, m_precision,
587 ctrl2.y, m_precision,
588 end.x, m_precision,
589 end.y, m_precision );
590#else
591 PLOTTER::BezierCurve( aStart, aControl1, aControl2, aEnd, aTolerance, aLineThickness );
592#endif
593}
594
595
596void SVG_PLOTTER::PlotPoly( const std::vector<VECTOR2I>& aCornerList, FILL_T aFill,
597 int aWidth, void* aData )
598{
599 if( aCornerList.size() <= 1 )
600 return;
601
602 setFillMode( aFill );
603 SetCurrentLineWidth( aWidth );
604 fmt::print( m_outputFile, "<path " );
605
606 switch( aFill )
607 {
608 case FILL_T::NO_FILL:
609 case FILL_T::HATCH:
612 setSVGPlotStyle( aWidth, false, "fill:none" );
613 break;
614
618 setSVGPlotStyle( aWidth, false, "fill-rule:evenodd;" );
619 break;
620 }
621
622 VECTOR2D pos = userToDeviceCoordinates( aCornerList[0] );
623 fmt::print( m_outputFile, "d=\"M {:.{}f},{:.{}f}\n", pos.x, m_precision, pos.y, m_precision );
624
625 for( unsigned ii = 1; ii < aCornerList.size() - 1; ii++ )
626 {
627 pos = userToDeviceCoordinates( aCornerList[ii] );
628 fmt::print( m_outputFile, "{:.{}f},{:.{}f}\n", pos.x, m_precision, pos.y, m_precision );
629 }
630
631 // If the corner list ends where it begins, then close the poly
632 if( aCornerList.front() == aCornerList.back() )
633 {
634 fmt::print( m_outputFile, "Z\" /> \n" );
635 }
636 else
637 {
638 pos = userToDeviceCoordinates( aCornerList.back() );
639 fmt::print( m_outputFile,
640 "{:.{}f},{:.{}f}\n\" /> \n",
641 pos.x, m_precision,
642 pos.y, m_precision );
643 }
644}
645
646
647void SVG_PLOTTER::PlotImage( const wxImage& aImage, const VECTOR2I& aPos, double aScaleFactor )
648{
649 VECTOR2I pix_size( aImage.GetWidth(), aImage.GetHeight() );
650
651 // Requested size (in IUs)
652 VECTOR2D drawsize( aScaleFactor * pix_size.x, aScaleFactor * pix_size.y );
653
654 // calculate the bitmap start position
655 VECTOR2I start( aPos.x - drawsize.x / 2, aPos.y - drawsize.y / 2 );
656
657 // Rectangles having a 0 size value for height or width are just not drawn on Inkscape,
658 // so use a line when happens.
659 if( drawsize.x == 0.0 || drawsize.y == 0.0 ) // Draw a line
660 {
661 PLOTTER::PlotImage( aImage, aPos, aScaleFactor );
662 }
663 else
664 {
665 wxMemoryOutputStream img_stream;
666
667 if( m_colorMode )
668 {
669 aImage.SaveFile( img_stream, wxBITMAP_TYPE_PNG );
670 }
671 else // Plot in B&W
672 {
673 wxImage image = aImage.ConvertToGreyscale();
674 image.SaveFile( img_stream, wxBITMAP_TYPE_PNG );
675 }
676
677 size_t input_len = img_stream.GetOutputStreamBuffer()->GetBufferSize();
678 std::vector<uint8_t> buffer( input_len );
679 std::vector<uint8_t> encoded;
680
681 img_stream.CopyTo( buffer.data(), buffer.size() );
682 base64::encode( buffer, encoded );
683
684 VECTOR2D pos = userToDeviceCoordinates( start );
685 fmt::print( m_outputFile,
686 "<image x=\"{:f}\" y=\"{:f}\" xlink:href=\"data:image/png;base64,", pos.x, pos.y );
687
688 for( size_t i = 0; i < encoded.size(); i++ )
689 {
690 fmt::print( m_outputFile, "{}", static_cast<char>( encoded[i] ) );
691
692 if( ( i % 64 ) == 63 )
693 fmt::print( m_outputFile, "\n" );
694 }
695
696 fmt::print( m_outputFile,
697 "\"\npreserveAspectRatio=\"none\" width=\"{:.{}f}\" height=\"{:.{}f}\" />",
698 userToDeviceSize( drawsize.x ), m_precision,
699 userToDeviceSize( drawsize.y ), m_precision );
700 }
701}
702
703
704void SVG_PLOTTER::PenTo( const VECTOR2I& pos, char plume )
705{
706 if( plume == 'Z' )
707 {
708 if( m_penState != 'Z' )
709 {
710 fmt::print( m_outputFile, "\" />\n" );
711 m_penState = 'Z';
712 m_penLastpos.x = -1;
713 m_penLastpos.y = -1;
714 }
715
716 return;
717 }
718
719 if( m_penState == 'Z' ) // here plume = 'D' or 'U'
720 {
721 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
722
723 // Ensure we do not use a fill mode when moving the pen,
724 // in SVG mode (i;e. we are plotting only basic lines, not a filled area
727
730
731 fmt::print( m_outputFile, "<path d=\"M{:.{}f} {:.{}f}\n",
732 pos_dev.x, m_precision,
733 pos_dev.y, m_precision );
734 }
735 else if( m_penState != plume || pos != m_penLastpos )
736 {
739
740 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
741
742 fmt::print( m_outputFile, "L{:.{}f} {:.{}f}\n",
743 pos_dev.x, m_precision,
744 pos_dev.y, m_precision );
745 }
746
747 m_penState = plume;
748 m_penLastpos = pos;
749}
750
751
752bool SVG_PLOTTER::StartPlot( const wxString& aPageNumber )
753{
754 wxASSERT( m_outputFile );
755
756 std::string header = "<?xml version=\"1.0\" standalone=\"no\"?>\n"
757 " <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \n"
758 " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> \n"
759 "<svg\n"
760 " xmlns:svg=\"http://www.w3.org/2000/svg\"\n"
761 " xmlns=\"http://www.w3.org/2000/svg\"\n"
762 " xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
763 " xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"\n"
764 " version=\"1.1\"\n";
765
766 // Write header.
767 fmt::print( m_outputFile, "{}", header );
768
769 // Write viewport pos and size
770 VECTOR2D origin; // TODO set to actual value
771 fmt::print( m_outputFile,
772 " width=\"{:.{}f}mm\" height=\"{:.{}f}mm\" viewBox=\"{:.{}f} {:.{}f} {:.{}f} {:.{}f}\">\n",
773 (double) m_paperSize.x / m_IUsPerDecimil * 2.54 / 1000, m_precision,
774 (double) m_paperSize.y / m_IUsPerDecimil * 2.54 / 1000, m_precision,
775 origin.x, m_precision, origin.y, m_precision,
778
779 // Write title
780 wxString date = GetISO8601CurrentDateTime();
781
782 fmt::print( m_outputFile,
783 "<title>SVG Image created as {} date {} </title>\n",
784 TO_UTF8( XmlEsc( wxFileName( m_filename ).GetFullName() ) ),
785 TO_UTF8( date ) );
786
787 // End of header
788 fmt::print( m_outputFile, " <desc>Image generated by {} </desc>\n",
789 TO_UTF8( XmlEsc( m_creator ) ) );
790
791 // output the pen and brush color (RVB values in hex) and opacity
792 double opacity = 1.0; // 0.0 (transparent to 1.0 (solid)
793 fmt::print( m_outputFile,
794 "<g style=\"fill:#{:06X}; fill-opacity:{:.{}f};stroke:#{:06X}; stroke-opacity:{:.{}f};\n",
799 opacity,
800 m_precision );
801
802 // output the pen cap and line joint
803 fmt::print( m_outputFile, "stroke-linecap:round; stroke-linejoin:round;\"\n" );
804 fmt::print( m_outputFile, " transform=\"translate(0 0) scale(1 1)\">\n" );
805 return true;
806}
807
808
810{
811 fmt::print( m_outputFile, "</g> \n</svg>\n" );
812 fclose( m_outputFile );
813 m_outputFile = nullptr;
814
815 return true;
816}
817
818
819void SVG_PLOTTER::Text( const VECTOR2I& aPos,
820 const COLOR4D& aColor,
821 const wxString& aText,
822 const EDA_ANGLE& aOrient,
823 const VECTOR2I& aSize,
824 enum GR_TEXT_H_ALIGN_T aH_justify,
825 enum GR_TEXT_V_ALIGN_T aV_justify,
826 int aWidth,
827 bool aItalic,
828 bool aBold,
829 bool aMultilineAllowed,
830 KIFONT::FONT* aFont,
831 const KIFONT::METRICS& aFontMetrics,
832 void* aData )
833{
835 SetColor( aColor );
836 SetCurrentLineWidth( aWidth );
837
840
841 VECTOR2I text_pos = aPos;
842 const char* hjust = "start";
843
844 switch( aH_justify )
845 {
846 case GR_TEXT_H_ALIGN_CENTER: hjust = "middle"; break;
847 case GR_TEXT_H_ALIGN_RIGHT: hjust = "end"; break;
848 case GR_TEXT_H_ALIGN_LEFT: hjust = "start"; break;
850 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
851 break;
852 }
853
854 switch( aV_justify )
855 {
856 case GR_TEXT_V_ALIGN_CENTER: text_pos.y += aSize.y / 2; break;
857 case GR_TEXT_V_ALIGN_TOP: text_pos.y += aSize.y; break;
858 case GR_TEXT_V_ALIGN_BOTTOM: break;
860 wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
861 break;
862 }
863
864 VECTOR2I text_size;
865
866 // aSize.x or aSize.y is < 0 for mirrored texts.
867 // The actual text size value is the absolute value
868 text_size.x = std::abs( GRTextWidth( aText, aFont, aSize, GetCurrentLineWidth(), aBold, aItalic,
869 aFontMetrics ) );
870 text_size.y = std::abs( aSize.x * 4/3 ); // Hershey font height to em size conversion
871 VECTOR2D anchor_pos_dev = userToDeviceCoordinates( aPos );
872 VECTOR2D text_pos_dev = userToDeviceCoordinates( text_pos );
873 VECTOR2D sz_dev = userToDeviceSize( text_size );
874
875 // Output the text as a hidden string (opacity = 0). This allows WYSIWYG search to highlight
876 // a selection in approximately the right area. It also makes it easier for those that need
877 // to edit the text (as text) in subsequent processes.
878 {
879 if( !aOrient.IsZero() )
880 {
881 fmt::print( m_outputFile,
882 "<g transform=\"rotate({:f} {:.{}f} {:.{}f})\">\n",
883 m_plotMirror ? aOrient.AsDegrees() : -aOrient.AsDegrees(),
884 anchor_pos_dev.x,
886 anchor_pos_dev.y,
887 m_precision );
888 }
889
890 fmt::print( m_outputFile,
891 "<text x=\"{:.{}f}\" y=\"{:.{}f}\"\n",
892 text_pos_dev.x, m_precision,
893 text_pos_dev.y, m_precision );
894
896 if( m_plotMirror != ( aSize.x < 0 ) )
897 {
898 fmt::print( m_outputFile, "transform=\"scale(-1 1) translate({:f} 0)\"\n",
899 -2 * text_pos_dev.x );
900 }
901
902 fmt::print( m_outputFile,
903 "textLength=\"{:.{}f}\" font-size=\"{:.{}f}\" lengthAdjust=\"spacingAndGlyphs\"\n"
904 "text-anchor=\"{}\" opacity=\"0\" stroke-opacity=\"0\">{}</text>\n",
905 sz_dev.x,
907 sz_dev.y,
909 hjust,
910 TO_UTF8( XmlEsc( aText ) ) );
911
912 if( !aOrient.IsZero() )
913 fmt::print( m_outputFile, "</g>\n" );
914 }
915
916 // Output the text again as graphics with a <desc> tag (for non-WYSIWYG search and for
917 // screen readers)
918 {
919 fmt::print( m_outputFile,
920 "<g class=\"stroked-text\"><desc>{}</desc>\n",
921 TO_UTF8( XmlEsc( aText ) ) );
922
923 PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, GetCurrentLineWidth(),
924 aItalic, aBold, aMultilineAllowed, aFont, aFontMetrics );
925
926 fmt::print( m_outputFile, "</g>" );
927 }
928}
929
930
932 const COLOR4D& aColor,
933 const wxString& aText,
934 const TEXT_ATTRIBUTES& aAttributes,
935 KIFONT::FONT* aFont,
936 const KIFONT::METRICS& aFontMetrics,
937 void* aData )
938{
939 VECTOR2I size = aAttributes.m_Size;
940
941 if( aAttributes.m_Mirrored )
942 size.x = -size.x;
943
944 SVG_PLOTTER::Text( aPos, aColor, aText, aAttributes.m_Angle, size, aAttributes.m_Halign,
945 aAttributes.m_Valign, aAttributes.m_StrokeWidth, aAttributes.m_Italic,
946 aAttributes.m_Bold, aAttributes.m_Multiline, aFont, aFontMetrics, aData );
947}
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:63
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)
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
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.
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