2012-09-15 12:13:03 +00:00
|
|
|
/**
|
2018-01-28 18:12:26 +00:00
|
|
|
* @file SVG_plotter.cpp
|
|
|
|
* @brief Kicad: specialized plotter for SVG files format
|
2012-09-15 12:13:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-11-16 11:09:24 +00:00
|
|
|
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
|
|
|
* Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-09-15 12:13:03 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Some info on basic items SVG format, used here:
|
|
|
|
* The root element of all SVG files is the <svg> element.
|
|
|
|
*
|
|
|
|
* The <g> element is used to group SVG shapes together.
|
|
|
|
* Once grouped you can transform the whole group of shapes as if it was a single shape.
|
|
|
|
* This is an advantage compared to a nested <svg> element
|
|
|
|
* which cannot be the target of transformation by itself.
|
|
|
|
*
|
|
|
|
* The <rect> element represents a rectangle.
|
|
|
|
* Using this element you can draw rectangles of various width, height,
|
|
|
|
* with different stroke (outline) and fill colors, with sharp or rounded corners etc.
|
|
|
|
*
|
|
|
|
* <svg xmlns="http://www.w3.org/2000/svg"
|
|
|
|
* xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
|
|
*
|
|
|
|
* <rect x="10" y="10" height="100" width="100"
|
|
|
|
* style="stroke:#006600; fill: #00cc00"/>
|
|
|
|
*
|
|
|
|
* </svg>
|
|
|
|
*
|
|
|
|
* The <circle> element is used to draw circles.
|
|
|
|
* <circle cx="40" cy="40" r="24" style="stroke:#006600; fill:#00cc00"/>
|
|
|
|
*
|
|
|
|
* The <ellipse> element is used to draw ellipses.
|
|
|
|
* An ellipse is a circle that does not have equal height and width.
|
|
|
|
* Its radius in the x and y directions are different, in other words.
|
|
|
|
* <ellipse cx="40" cy="40" rx="30" ry="15"
|
|
|
|
* style="stroke:#006600; fill:#00cc00"/>
|
|
|
|
*
|
|
|
|
* The <line> element is used to draw lines.
|
|
|
|
*
|
|
|
|
* <line x1="0" y1="10" x2="0" y2="100" style="stroke:#006600;"/>
|
|
|
|
* <line x1="10" y1="10" x2="100" y2="100" style="stroke:#006600;"/>
|
|
|
|
*
|
|
|
|
* The <polyline> element is used to draw multiple connected lines
|
|
|
|
* Here is a simple example:
|
|
|
|
*
|
|
|
|
* <polyline points="0,0 30,0 15,30" style="stroke:#006600;"/>
|
|
|
|
*
|
|
|
|
* The <polygon> element is used to draw with multiple (3 or more) sides / edges.
|
|
|
|
* Here is a simple example:
|
|
|
|
*
|
|
|
|
* <polygon points="0,0 50,0 25,50" style="stroke:#660000; fill:#cc3333;"/>
|
|
|
|
*
|
2012-09-18 14:50:30 +00:00
|
|
|
* The <path> element is used to draw advanced shapes combined from lines and arcs,
|
2012-09-15 12:13:03 +00:00
|
|
|
* with or without fill.
|
|
|
|
* It is probably the most advanced and versatile SVG shape of them all.
|
|
|
|
* It is probably also the hardest element to master.
|
|
|
|
* <path d="M50,50
|
|
|
|
* A30,30 0 0,1 35,20
|
|
|
|
* L100,100
|
|
|
|
* M110,110
|
|
|
|
* L100,0"
|
|
|
|
* style="stroke:#660000; fill:none;"/>
|
2012-09-18 14:50:30 +00:00
|
|
|
*
|
|
|
|
* Draw an elliptic arc: it is one of basic path command:
|
|
|
|
* <path d="M(startx,starty) A(radiusx,radiusy)
|
|
|
|
* rotation-axe-x
|
|
|
|
* flag_arc_large,flag_sweep endx,endy">
|
|
|
|
* flag_arc_large: 0 = small arc > 180 deg, 1 = large arc > 180 deg
|
|
|
|
* flag_sweep : 0 = CCW, 1 = CW
|
|
|
|
* The center of ellipse is automatically calculated.
|
2012-09-15 12:13:03 +00:00
|
|
|
*/
|
2019-12-08 16:47:04 +00:00
|
|
|
|
|
|
|
#include <base64.h>
|
2012-09-15 12:13:03 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <trigo.h>
|
2018-01-29 15:39:40 +00:00
|
|
|
#include <eda_base_frame.h>
|
2018-01-29 10:37:29 +00:00
|
|
|
#include <eda_rect.h>
|
2012-09-15 12:13:03 +00:00
|
|
|
#include <base_struct.h>
|
|
|
|
#include <common.h>
|
2018-01-28 18:12:26 +00:00
|
|
|
#include <plotter.h>
|
2012-09-15 12:13:03 +00:00
|
|
|
#include <macros.h>
|
|
|
|
#include <kicad_string.h>
|
|
|
|
|
2019-12-08 16:47:04 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <wx/mstream.h>
|
2013-04-27 02:12:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function XmlEsc
|
|
|
|
* translates '<' to "<", '>' to ">" and so on, according to the spec:
|
|
|
|
* http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
|
|
|
|
* May be moved to a library if needed generally, but not expecting that.
|
|
|
|
*/
|
|
|
|
static wxString XmlEsc( const wxString& aStr, bool isAttribute = false )
|
|
|
|
{
|
|
|
|
wxString escaped;
|
|
|
|
|
|
|
|
escaped.reserve( aStr.length() );
|
|
|
|
|
|
|
|
for( wxString::const_iterator it = aStr.begin(); it != aStr.end(); ++it )
|
|
|
|
{
|
|
|
|
const wxChar c = *it;
|
|
|
|
|
|
|
|
switch( c )
|
|
|
|
{
|
|
|
|
case wxS( '<' ):
|
|
|
|
escaped.append( wxS( "<" ) );
|
|
|
|
break;
|
|
|
|
case wxS( '>' ):
|
|
|
|
escaped.append( wxS( ">" ) );
|
|
|
|
break;
|
|
|
|
case wxS( '&' ):
|
|
|
|
escaped.append( wxS( "&" ) );
|
|
|
|
break;
|
|
|
|
case wxS( '\r' ):
|
|
|
|
escaped.append( wxS( "
" ) );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if( isAttribute )
|
|
|
|
{
|
|
|
|
switch( c )
|
|
|
|
{
|
|
|
|
case wxS( '"' ):
|
|
|
|
escaped.append( wxS( """ ) );
|
|
|
|
break;
|
|
|
|
case wxS( '\t' ):
|
|
|
|
escaped.append( wxS( "	" ) );
|
|
|
|
break;
|
|
|
|
case wxS( '\n' ):
|
|
|
|
escaped.append( wxS( "
" ));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
escaped.append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
escaped.append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-15 12:13:03 +00:00
|
|
|
SVG_PLOTTER::SVG_PLOTTER()
|
|
|
|
{
|
|
|
|
m_graphics_changed = true;
|
|
|
|
SetTextMode( PLOTTEXTMODE_STROKE );
|
|
|
|
m_fillMode = NO_FILL; // or FILLED_SHAPE or FILLED_WITH_BG_BODYCOLOR
|
|
|
|
m_pen_rgb_color = 0; // current color value (black)
|
|
|
|
m_brush_rgb_color = 0; // current color value (black)
|
2015-03-06 17:46:04 +00:00
|
|
|
m_dashed = false;
|
2012-09-15 12:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_PLOTTER::SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
|
|
|
|
double aScale, bool aMirror )
|
|
|
|
{
|
2013-12-06 18:31:15 +00:00
|
|
|
m_plotMirror = aMirror;
|
|
|
|
m_yaxisReversed = true; // unlike other plotters, SVG has Y axis reversed
|
2012-09-15 12:13:03 +00:00
|
|
|
plotOffset = aOffset;
|
|
|
|
plotScale = aScale;
|
|
|
|
m_IUsPerDecimil = aIusPerDecimil;
|
|
|
|
iuPerDeviceUnit = 1.0 / aIusPerDecimil;
|
|
|
|
/* Compute the paper size in IUs */
|
|
|
|
paperSize = pageInfo.GetSizeMils();
|
|
|
|
paperSize.x *= 10.0 * aIusPerDecimil;
|
|
|
|
paperSize.y *= 10.0 * aIusPerDecimil;
|
|
|
|
SetDefaultLineWidth( 100 * aIusPerDecimil ); // arbitrary default
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-20 16:57:41 +00:00
|
|
|
void SVG_PLOTTER::SetColor( COLOR4D color )
|
2012-09-15 12:13:03 +00:00
|
|
|
{
|
|
|
|
PSLIKE_PLOTTER::SetColor( color );
|
2015-07-13 18:36:06 +00:00
|
|
|
|
|
|
|
if( m_graphics_changed )
|
|
|
|
setSVGPlotStyle();
|
2012-09-15 12:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_PLOTTER::setFillMode( FILL_T fill )
|
|
|
|
{
|
|
|
|
if( m_fillMode != fill )
|
|
|
|
{
|
|
|
|
m_graphics_changed = true;
|
|
|
|
m_fillMode = fill;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-30 15:25:57 +00:00
|
|
|
void SVG_PLOTTER::setSVGPlotStyle( bool aIsGroup, const std::string& aExtraStyle )
|
2012-09-15 12:13:03 +00:00
|
|
|
{
|
2019-01-30 15:25:57 +00:00
|
|
|
if( aIsGroup )
|
|
|
|
fputs( "</g>\n<g ", outputFile );
|
|
|
|
|
2012-09-15 12:13:03 +00:00
|
|
|
// output the background fill color
|
2019-01-30 15:25:57 +00:00
|
|
|
fprintf( outputFile, "style=\"fill:#%6.6lX; ", m_brush_rgb_color );
|
2012-09-15 12:13:03 +00:00
|
|
|
|
|
|
|
switch( m_fillMode )
|
|
|
|
{
|
|
|
|
case NO_FILL:
|
2012-09-20 18:58:41 +00:00
|
|
|
fputs( "fill-opacity:0.0; ", outputFile );
|
2012-09-15 12:13:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILLED_SHAPE:
|
2012-09-20 18:58:41 +00:00
|
|
|
fputs( "fill-opacity:1.0; ", outputFile );
|
2012-09-15 12:13:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILLED_WITH_BG_BODYCOLOR:
|
2012-09-20 18:58:41 +00:00
|
|
|
fputs( "fill-opacity:0.6; ", outputFile );
|
2012-09-15 12:13:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-20 18:58:41 +00:00
|
|
|
double pen_w = userToDeviceSize( GetCurrentLineWidth() );
|
|
|
|
fprintf( outputFile, "\nstroke:#%6.6lX; stroke-width:%g; stroke-opacity:1; \n",
|
|
|
|
m_pen_rgb_color, pen_w );
|
2015-03-06 17:46:04 +00:00
|
|
|
fputs( "stroke-linecap:round; stroke-linejoin:round;", outputFile );
|
|
|
|
|
2017-11-13 03:53:27 +00:00
|
|
|
switch( m_dashed )
|
|
|
|
{
|
|
|
|
case PLOTDASHTYPE_DASH:
|
2015-03-10 20:00:50 +00:00
|
|
|
fprintf( outputFile, "stroke-dasharray:%g,%g;",
|
|
|
|
GetDashMarkLenIU(), GetDashGapLenIU() );
|
2017-11-13 03:53:27 +00:00
|
|
|
break;
|
|
|
|
case PLOTDASHTYPE_DOT:
|
|
|
|
fprintf( outputFile, "stroke-dasharray:%g,%g;",
|
|
|
|
GetDotMarkLenIU(), GetDashGapLenIU() );
|
|
|
|
break;
|
|
|
|
case PLOTDASHTYPE_DASHDOT:
|
|
|
|
fprintf( outputFile, "stroke-dasharray:%g,%g,%g,%g;",
|
|
|
|
GetDashMarkLenIU(), GetDashGapLenIU(), GetDotMarkLenIU(), GetDashGapLenIU() );
|
|
|
|
break;
|
|
|
|
}
|
2015-03-06 17:46:04 +00:00
|
|
|
|
2019-01-30 15:25:57 +00:00
|
|
|
if( aExtraStyle.length() )
|
|
|
|
{
|
|
|
|
fputs( aExtraStyle.c_str(), outputFile );
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs( "\"", outputFile );
|
2012-09-15 12:13:03 +00:00
|
|
|
|
2019-01-30 15:25:57 +00:00
|
|
|
if( aIsGroup )
|
|
|
|
{
|
|
|
|
fputs( ">", outputFile );
|
|
|
|
m_graphics_changed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs( "\n", outputFile );
|
2012-09-15 12:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the current line width (in IUs) for the next plot
|
|
|
|
*/
|
2016-09-19 11:01:36 +00:00
|
|
|
void SVG_PLOTTER::SetCurrentLineWidth( int width, void* aData )
|
2012-09-15 12:13:03 +00:00
|
|
|
{
|
|
|
|
int pen_width;
|
|
|
|
|
|
|
|
if( width >= 0 )
|
|
|
|
pen_width = width;
|
|
|
|
else
|
|
|
|
pen_width = defaultPenWidth;
|
|
|
|
|
|
|
|
if( pen_width != currentPenWidth )
|
|
|
|
{
|
|
|
|
m_graphics_changed = true;
|
|
|
|
currentPenWidth = pen_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_graphics_changed )
|
|
|
|
setSVGPlotStyle();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-27 00:58:33 +00:00
|
|
|
void SVG_PLOTTER::StartBlock( void* aData )
|
|
|
|
{
|
|
|
|
std::string* idstr = reinterpret_cast<std::string*>( aData );
|
|
|
|
|
2019-02-12 09:47:06 +00:00
|
|
|
fputs( "<g ", outputFile );
|
2019-01-27 00:58:33 +00:00
|
|
|
if( idstr )
|
|
|
|
fprintf( outputFile, "id=\"%s\"", idstr->c_str() );
|
|
|
|
|
|
|
|
fprintf( outputFile, ">\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_PLOTTER::EndBlock( void* aData )
|
|
|
|
{
|
|
|
|
fprintf( outputFile, "</g>\n" );
|
2019-05-23 14:13:38 +00:00
|
|
|
|
|
|
|
m_graphics_changed = true;
|
2019-01-27 00:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-15 12:13:03 +00:00
|
|
|
/* initialize m_red, m_green, m_blue ( 0 ... 255)
|
|
|
|
* from reduced values r, g ,b ( 0.0 to 1.0 )
|
|
|
|
*/
|
|
|
|
void SVG_PLOTTER::emitSetRGBColor( double r, double g, double b )
|
|
|
|
{
|
|
|
|
int red = (int) ( 255.0 * r );
|
|
|
|
int green = (int) ( 255.0 * g );
|
|
|
|
int blue = (int) ( 255.0 * b );
|
|
|
|
long rgb_color = (red << 16) | (green << 8) | blue;
|
|
|
|
|
|
|
|
if( m_pen_rgb_color != rgb_color )
|
|
|
|
{
|
|
|
|
m_graphics_changed = true;
|
|
|
|
m_pen_rgb_color = rgb_color;
|
|
|
|
|
|
|
|
// Currently, use the same color for brush and pen
|
|
|
|
// (i.e. to draw and fill a contour)
|
|
|
|
m_brush_rgb_color = rgb_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SVG supports dashed lines
|
|
|
|
*/
|
2017-11-13 03:53:27 +00:00
|
|
|
void SVG_PLOTTER::SetDash( int dashed )
|
2012-09-15 12:13:03 +00:00
|
|
|
{
|
2015-03-06 17:46:04 +00:00
|
|
|
if( m_dashed != dashed )
|
|
|
|
{
|
|
|
|
m_graphics_changed = true;
|
|
|
|
m_dashed = dashed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_graphics_changed )
|
|
|
|
setSVGPlotStyle();
|
2012-09-15 12:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_PLOTTER::Rect( const wxPoint& p1, const wxPoint& p2, FILL_T fill, int width )
|
|
|
|
{
|
2012-09-18 14:50:30 +00:00
|
|
|
EDA_RECT rect( p1, wxSize( p2.x -p1.x, p2.y -p1.y ) );
|
|
|
|
rect.Normalize();
|
2012-09-24 06:39:59 +00:00
|
|
|
DPOINT org_dev = userToDeviceCoordinates( rect.GetOrigin() );
|
|
|
|
DPOINT end_dev = userToDeviceCoordinates( rect.GetEnd() );
|
|
|
|
DSIZE size_dev = end_dev - org_dev;
|
|
|
|
// Ensure size of rect in device coordinates is > 0
|
2015-07-13 18:36:06 +00:00
|
|
|
// I don't know if this is a SVG issue or a Inkscape issue, but
|
|
|
|
// Inkscape has problems with negative or null values for width and/or height, so avoid them
|
2012-09-24 06:39:59 +00:00
|
|
|
DBOX rect_dev( org_dev, size_dev);
|
|
|
|
rect_dev.Normalize();
|
2012-09-15 12:13:03 +00:00
|
|
|
|
|
|
|
setFillMode( fill );
|
|
|
|
SetCurrentLineWidth( width );
|
|
|
|
|
2015-07-13 18:36:06 +00:00
|
|
|
// Rectangles having a 0 size value for height or width are just not drawn on Inscape,
|
|
|
|
// so use a line when happens.
|
|
|
|
if( rect_dev.GetSize().x == 0.0 || rect_dev.GetSize().y == 0.0 ) // Draw a line
|
|
|
|
fprintf( outputFile,
|
|
|
|
"<line x1=\"%g\" y1=\"%g\" x2=\"%g\" y2=\"%g\" />\n",
|
|
|
|
rect_dev.GetPosition().x, rect_dev.GetPosition().y,
|
|
|
|
rect_dev.GetEnd().x, rect_dev.GetEnd().y
|
|
|
|
);
|
|
|
|
|
|
|
|
else
|
|
|
|
fprintf( outputFile,
|
|
|
|
"<rect x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\" rx=\"%g\" />\n",
|
|
|
|
rect_dev.GetPosition().x, rect_dev.GetPosition().y,
|
|
|
|
rect_dev.GetSize().x, rect_dev.GetSize().y,
|
|
|
|
0.0 // radius of rounded corners
|
|
|
|
);
|
2012-09-15 12:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_PLOTTER::Circle( const wxPoint& pos, int diametre, FILL_T fill, int width )
|
|
|
|
{
|
|
|
|
DPOINT pos_dev = userToDeviceCoordinates( pos );
|
|
|
|
double radius = userToDeviceSize( diametre / 2.0 );
|
|
|
|
|
|
|
|
setFillMode( fill );
|
|
|
|
SetCurrentLineWidth( width );
|
|
|
|
|
2018-03-09 03:44:26 +00:00
|
|
|
// If diameter is less than width, switch to filled mode
|
|
|
|
if( fill == NO_FILL && diametre < width )
|
|
|
|
{
|
|
|
|
setFillMode( FILLED_SHAPE );
|
|
|
|
SetCurrentLineWidth( 0 );
|
|
|
|
|
|
|
|
radius = userToDeviceSize( ( diametre / 2.0 ) + ( width / 2.0 ) );
|
|
|
|
}
|
|
|
|
|
2012-09-15 12:13:03 +00:00
|
|
|
fprintf( outputFile,
|
|
|
|
"<circle cx=\"%g\" cy=\"%g\" r=\"%g\" /> \n",
|
|
|
|
pos_dev.x, pos_dev.y, radius );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-05 07:17:48 +00:00
|
|
|
void SVG_PLOTTER::Arc( const wxPoint& centre, double StAngle, double EndAngle, int radius,
|
2012-09-15 12:13:03 +00:00
|
|
|
FILL_T fill, int width )
|
|
|
|
{
|
|
|
|
/* Draws an arc of a circle, centred on (xc,yc), with starting point
|
|
|
|
* (x1, y1) and ending at (x2, y2). The current pen is used for the outline
|
|
|
|
* and the current brush for filling the shape.
|
|
|
|
*
|
|
|
|
* The arc is drawn in an anticlockwise direction from the start point to
|
|
|
|
* the end point
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( radius <= 0 )
|
2018-03-09 03:44:26 +00:00
|
|
|
{
|
|
|
|
Circle( centre, width, FILLED_SHAPE, 0 );
|
2012-09-15 12:13:03 +00:00
|
|
|
return;
|
2018-03-09 03:44:26 +00:00
|
|
|
}
|
2012-09-15 12:13:03 +00:00
|
|
|
|
|
|
|
if( StAngle > EndAngle )
|
2015-06-26 13:41:56 +00:00
|
|
|
std::swap( StAngle, EndAngle );
|
2012-09-15 12:13:03 +00:00
|
|
|
|
|
|
|
// Calculate start point.
|
|
|
|
DPOINT centre_dev = userToDeviceCoordinates( centre );
|
|
|
|
double radius_dev = userToDeviceSize( radius );
|
|
|
|
|
2014-06-10 16:21:38 +00:00
|
|
|
if( !m_yaxisReversed ) // Should be never the case
|
2012-09-15 12:13:03 +00:00
|
|
|
{
|
2013-05-05 07:17:48 +00:00
|
|
|
double tmp = StAngle;
|
2012-09-15 12:13:03 +00:00
|
|
|
StAngle = -EndAngle;
|
|
|
|
EndAngle = -tmp;
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:31:15 +00:00
|
|
|
if( m_plotMirror )
|
|
|
|
{
|
|
|
|
if( m_mirrorIsHorizontal )
|
|
|
|
{
|
|
|
|
StAngle = 1800.0 -StAngle;
|
|
|
|
EndAngle = 1800.0 -EndAngle;
|
2015-06-26 13:41:56 +00:00
|
|
|
std::swap( StAngle, EndAngle );
|
2013-12-06 18:31:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StAngle = -StAngle;
|
|
|
|
EndAngle = -EndAngle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:50:30 +00:00
|
|
|
DPOINT start;
|
|
|
|
start.x = radius_dev;
|
2012-09-15 12:13:03 +00:00
|
|
|
RotatePoint( &start.x, &start.y, StAngle );
|
2012-09-18 14:50:30 +00:00
|
|
|
DPOINT end;
|
|
|
|
end.x = radius_dev;
|
2012-09-15 12:13:03 +00:00
|
|
|
RotatePoint( &end.x, &end.y, EndAngle );
|
2012-09-18 14:50:30 +00:00
|
|
|
start += centre_dev;
|
|
|
|
end += centre_dev;
|
2012-09-15 12:13:03 +00:00
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
double theta1 = DECIDEG2RAD( StAngle );
|
2012-09-15 12:13:03 +00:00
|
|
|
|
|
|
|
if( theta1 < 0 )
|
|
|
|
theta1 = theta1 + M_PI * 2;
|
|
|
|
|
2013-05-02 18:06:58 +00:00
|
|
|
double theta2 = DECIDEG2RAD( EndAngle );
|
2012-09-15 12:13:03 +00:00
|
|
|
|
|
|
|
if( theta2 < 0 )
|
|
|
|
theta2 = theta2 + M_PI * 2;
|
|
|
|
|
|
|
|
if( theta2 < theta1 )
|
|
|
|
theta2 = theta2 + M_PI * 2;
|
|
|
|
|
|
|
|
int flg_arc = 0; // flag for large or small arc. 0 means less than 180 degrees
|
|
|
|
|
|
|
|
if( fabs( theta2 - theta1 ) > M_PI )
|
|
|
|
flg_arc = 1;
|
|
|
|
|
|
|
|
int flg_sweep = 0; // flag for sweep always 0
|
|
|
|
|
|
|
|
// Draw a single arc: an arc is one of 3 curve commands (2 other are 2 bezier curves)
|
|
|
|
// params are start point, radius1, radius2, X axe rotation,
|
|
|
|
// flag arc size (0 = small arc > 180 deg, 1 = large arc > 180 deg),
|
|
|
|
// sweep arc ( 0 = CCW, 1 = CW),
|
2012-09-18 14:50:30 +00:00
|
|
|
// end point
|
2019-08-20 13:45:57 +00:00
|
|
|
if( fill != NO_FILL )
|
|
|
|
{
|
|
|
|
// Filled arcs (in eeschema) consist of the pie wedge and a stroke only on the arc
|
|
|
|
// This needs to be drawn in two steps.
|
|
|
|
setFillMode( fill );
|
|
|
|
SetCurrentLineWidth( 0 );
|
|
|
|
|
|
|
|
fprintf( outputFile, "<path d=\"M%g %g A%g %g 0.0 %d %d %g %g L %g %g Z\" />\n",
|
|
|
|
start.x, start.y, radius_dev, radius_dev,
|
|
|
|
flg_arc, flg_sweep,
|
|
|
|
end.x, end.y, centre_dev.x, centre_dev.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
setFillMode( NO_FILL );
|
|
|
|
SetCurrentLineWidth( width );
|
2019-12-08 16:47:04 +00:00
|
|
|
fprintf( outputFile, "<path d=\"M%g %g A%g %g 0.0 %d %d %g %g\" />\n",
|
2012-09-18 14:50:30 +00:00
|
|
|
start.x, start.y, radius_dev, radius_dev,
|
2012-09-15 12:13:03 +00:00
|
|
|
flg_arc, flg_sweep,
|
2012-09-18 14:50:30 +00:00
|
|
|
end.x, end.y );
|
2012-09-15 12:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-05 13:27:07 +00:00
|
|
|
void SVG_PLOTTER::BezierCurve( const wxPoint& aStart, const wxPoint& aControl1,
|
|
|
|
const wxPoint& aControl2, const wxPoint& aEnd,
|
|
|
|
int aTolerance, int aLineThickness )
|
|
|
|
{
|
|
|
|
#if 1
|
|
|
|
setFillMode( NO_FILL );
|
|
|
|
SetCurrentLineWidth( aLineThickness );
|
|
|
|
|
|
|
|
DPOINT start = userToDeviceCoordinates( aStart );
|
|
|
|
DPOINT ctrl1 = userToDeviceCoordinates( aControl1 );
|
|
|
|
DPOINT ctrl2 = userToDeviceCoordinates( aControl2 );
|
|
|
|
DPOINT end = userToDeviceCoordinates( aEnd );
|
|
|
|
|
|
|
|
// Generate a cubic curve: start point and 3 other control points.
|
|
|
|
fprintf( outputFile, "<path d=\"M%g,%g C%g,%g %g,%g %g,%g\" />\n",
|
|
|
|
start.x, start.y, ctrl1.x, ctrl1.y,
|
|
|
|
ctrl2.x, ctrl2.y, end.x, end.y );
|
|
|
|
#else
|
|
|
|
PLOTTER::BezierCurve( aStart, aControl1,aControl2, aEnd,aTolerance, aLineThickness );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-15 12:13:03 +00:00
|
|
|
void SVG_PLOTTER::PlotPoly( const std::vector<wxPoint>& aCornerList,
|
2016-09-19 11:01:36 +00:00
|
|
|
FILL_T aFill, int aWidth, void * aData )
|
2012-09-15 12:13:03 +00:00
|
|
|
{
|
|
|
|
if( aCornerList.size() <= 1 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
setFillMode( aFill );
|
|
|
|
SetCurrentLineWidth( aWidth );
|
2019-01-30 15:25:57 +00:00
|
|
|
fprintf( outputFile, "<path ");
|
2012-09-15 12:13:03 +00:00
|
|
|
|
2012-09-18 14:50:30 +00:00
|
|
|
switch( aFill )
|
|
|
|
{
|
2014-06-24 16:17:18 +00:00
|
|
|
case NO_FILL:
|
2019-01-30 15:25:57 +00:00
|
|
|
setSVGPlotStyle( false, "fill:none" );
|
2014-06-24 16:17:18 +00:00
|
|
|
break;
|
2012-09-18 14:50:30 +00:00
|
|
|
|
2014-06-24 16:17:18 +00:00
|
|
|
case FILLED_WITH_BG_BODYCOLOR:
|
|
|
|
case FILLED_SHAPE:
|
2019-01-30 15:25:57 +00:00
|
|
|
setSVGPlotStyle( false, "fill-rule:evenodd;" );
|
2014-06-24 16:17:18 +00:00
|
|
|
break;
|
2012-09-18 14:50:30 +00:00
|
|
|
}
|
2012-09-15 12:13:03 +00:00
|
|
|
|
|
|
|
DPOINT pos = userToDeviceCoordinates( aCornerList[0] );
|
2019-08-20 13:45:57 +00:00
|
|
|
fprintf( outputFile, "d=\"M %g,%g\n", pos.x, pos.y );
|
2012-09-15 12:13:03 +00:00
|
|
|
|
2019-08-20 13:45:57 +00:00
|
|
|
for( unsigned ii = 1; ii < aCornerList.size() - 1; ii++ )
|
2012-09-15 12:13:03 +00:00
|
|
|
{
|
|
|
|
pos = userToDeviceCoordinates( aCornerList[ii] );
|
2019-08-20 13:45:57 +00:00
|
|
|
fprintf( outputFile, "%g,%g\n", pos.x, pos.y );
|
2012-09-15 12:13:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 13:45:57 +00:00
|
|
|
// If the cornerlist ends where it begins, then close the poly
|
|
|
|
if( aCornerList.front() == aCornerList.back() )
|
|
|
|
fprintf( outputFile, "Z\" /> \n" );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = userToDeviceCoordinates( aCornerList.back() );
|
|
|
|
fprintf( outputFile, "%g,%g\n\" /> \n", pos.x, pos.y );
|
|
|
|
}
|
2012-09-15 12:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Postscript-likes at the moment are the only plot engines supporting bitmaps...
|
|
|
|
*/
|
|
|
|
void SVG_PLOTTER::PlotImage( const wxImage& aImage, const wxPoint& aPos,
|
|
|
|
double aScaleFactor )
|
|
|
|
{
|
2019-12-08 16:47:04 +00:00
|
|
|
wxSize pix_size( aImage.GetWidth(), aImage.GetHeight() );
|
|
|
|
|
|
|
|
// Requested size (in IUs)
|
|
|
|
DPOINT drawsize( aScaleFactor * pix_size.x,
|
|
|
|
aScaleFactor * pix_size.y );
|
|
|
|
|
|
|
|
// calculate the bitmap start position
|
|
|
|
wxPoint start( aPos.x - drawsize.x / 2,
|
|
|
|
aPos.y - drawsize.y / 2);
|
|
|
|
|
|
|
|
// Rectangles having a 0 size value for height or width are just not drawn on Inscape,
|
|
|
|
// so use a line when happens.
|
|
|
|
if( drawsize.x == 0.0 || drawsize.y == 0.0 ) // Draw a line
|
|
|
|
{
|
|
|
|
PLOTTER::PlotImage( aImage, aPos, aScaleFactor );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxMemoryOutputStream img_stream;
|
|
|
|
|
|
|
|
aImage.SaveFile( img_stream, wxBITMAP_TYPE_PNG );
|
|
|
|
size_t input_len = img_stream.GetOutputStreamBuffer()->GetBufferSize();
|
|
|
|
std::vector<uint8_t> buffer( input_len );
|
|
|
|
std::vector<uint8_t> encoded;
|
|
|
|
|
|
|
|
img_stream.CopyTo( buffer.data(), buffer.size() );
|
|
|
|
base64::encode( buffer, encoded );
|
|
|
|
|
|
|
|
fprintf( outputFile,
|
|
|
|
"<image x=\"%g\" y=\"%g\" xlink:href=\"data:image/png;base64,",
|
|
|
|
userToDeviceSize( start.x ), userToDeviceSize( start.y )
|
|
|
|
);
|
|
|
|
|
|
|
|
for( size_t i = 0; i < encoded.size(); i++ )
|
|
|
|
{
|
|
|
|
fprintf( outputFile, "%c", static_cast<char>( encoded[i] ) );
|
|
|
|
|
|
|
|
if( ( i % 64 ) == 63 )
|
|
|
|
fprintf( outputFile, "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( outputFile, "\"\npreserveAspectRatio=\"none\" height=\"%g\" width=\"%g\" />",
|
|
|
|
userToDeviceSize( drawsize.x ), userToDeviceSize( drawsize.y ) );
|
|
|
|
}
|
2012-09-15 12:13:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_PLOTTER::PenTo( const wxPoint& pos, char plume )
|
|
|
|
{
|
|
|
|
if( plume == 'Z' )
|
|
|
|
{
|
|
|
|
if( penState != 'Z' )
|
|
|
|
{
|
|
|
|
fputs( "\" />\n", outputFile );
|
|
|
|
penState = 'Z';
|
|
|
|
penLastpos.x = -1;
|
|
|
|
penLastpos.y = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( penState == 'Z' ) // here plume = 'D' or 'U'
|
|
|
|
{
|
|
|
|
DPOINT pos_dev = userToDeviceCoordinates( pos );
|
2014-04-28 16:13:18 +00:00
|
|
|
|
|
|
|
// Ensure we do not use a fill mode when moving tne pen,
|
|
|
|
// in SVG mode (i;e. we are plotting only basic lines, not a filled area
|
|
|
|
if( m_fillMode != NO_FILL )
|
|
|
|
{
|
|
|
|
setFillMode( NO_FILL );
|
|
|
|
setSVGPlotStyle();
|
|
|
|
}
|
|
|
|
|
2012-09-15 12:13:03 +00:00
|
|
|
fprintf( outputFile, "<path d=\"M%d %d\n",
|
|
|
|
(int) pos_dev.x, (int) pos_dev.y );
|
|
|
|
}
|
|
|
|
else if( penState != plume || pos != penLastpos )
|
|
|
|
{
|
|
|
|
DPOINT pos_dev = userToDeviceCoordinates( pos );
|
|
|
|
fprintf( outputFile, "L%d %d\n",
|
|
|
|
(int) pos_dev.x, (int) pos_dev.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
penState = plume;
|
|
|
|
penLastpos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The code within this function
|
|
|
|
* creates SVG files header
|
|
|
|
*/
|
2012-10-13 18:54:33 +00:00
|
|
|
bool SVG_PLOTTER::StartPlot()
|
2012-09-15 12:13:03 +00:00
|
|
|
{
|
2012-10-13 18:54:33 +00:00
|
|
|
wxASSERT( outputFile );
|
2012-09-15 12:13:03 +00:00
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
static const char* header[] =
|
|
|
|
{
|
|
|
|
"<?xml version=\"1.0\" standalone=\"no\"?>\n",
|
|
|
|
" <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \n",
|
|
|
|
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> \n",
|
|
|
|
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" \n",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
// Write header.
|
|
|
|
for( int ii = 0; header[ii] != NULL; ii++ )
|
|
|
|
{
|
|
|
|
fputs( header[ii], outputFile );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write viewport pos and size
|
|
|
|
wxPoint origin; // TODO set to actual value
|
|
|
|
fprintf( outputFile,
|
2019-12-08 16:47:04 +00:00
|
|
|
" width=\"%gcm\" height=\"%gcm\" viewBox=\"%d %d %d %d\">\n",
|
2012-09-15 12:13:03 +00:00
|
|
|
(double) paperSize.x / m_IUsPerDecimil * 2.54 / 10000,
|
|
|
|
(double) paperSize.y / m_IUsPerDecimil * 2.54 / 10000,
|
|
|
|
origin.x, origin.y,
|
|
|
|
(int) ( paperSize.x / m_IUsPerDecimil ),
|
|
|
|
(int) ( paperSize.y / m_IUsPerDecimil) );
|
|
|
|
|
|
|
|
// Write title
|
|
|
|
char date_buf[250];
|
|
|
|
time_t ltime = time( NULL );
|
|
|
|
strftime( date_buf, 250, "%Y/%m/%d %H:%M:%S",
|
|
|
|
localtime( <ime ) );
|
|
|
|
|
|
|
|
fprintf( outputFile,
|
|
|
|
"<title>SVG Picture created as %s date %s </title>\n",
|
2013-04-27 02:12:57 +00:00
|
|
|
TO_UTF8( XmlEsc( wxFileName( filename ).GetFullName() ) ), date_buf );
|
2012-09-15 12:13:03 +00:00
|
|
|
// End of header
|
|
|
|
fprintf( outputFile, " <desc>Picture generated by %s </desc>\n",
|
2013-04-27 02:12:57 +00:00
|
|
|
TO_UTF8( XmlEsc( creator ) ) );
|
2012-09-15 12:13:03 +00:00
|
|
|
|
2012-09-20 18:58:41 +00:00
|
|
|
// output the pen and brush color (RVB values in hex) and opacity
|
|
|
|
double opacity = 1.0; // 0.0 (transparent to 1.0 (solid)
|
|
|
|
fprintf( outputFile,
|
|
|
|
"<g style=\"fill:#%6.6lX; fill-opacity:%g;stroke:#%6.6lX; stroke-opacity:%g;\n",
|
|
|
|
m_brush_rgb_color, opacity, m_pen_rgb_color, opacity );
|
|
|
|
|
|
|
|
// output the pen cap and line joint
|
2019-12-08 16:47:04 +00:00
|
|
|
fputs( "stroke-linecap:round; stroke-linejoin:round;\"\n", outputFile );
|
2012-09-20 18:58:41 +00:00
|
|
|
fputs( " transform=\"translate(0 0) scale(1 1)\">\n", outputFile );
|
2012-09-15 12:13:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SVG_PLOTTER::EndPlot()
|
|
|
|
{
|
|
|
|
fputs( "</g> \n</svg>\n", outputFile );
|
|
|
|
fclose( outputFile );
|
|
|
|
outputFile = NULL;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_PLOTTER::Text( const wxPoint& aPos,
|
2017-02-20 16:57:41 +00:00
|
|
|
const COLOR4D aColor,
|
2012-09-15 12:13:03 +00:00
|
|
|
const wxString& aText,
|
2013-05-05 07:17:48 +00:00
|
|
|
double aOrient,
|
2012-09-15 12:13:03 +00:00
|
|
|
const wxSize& aSize,
|
|
|
|
enum EDA_TEXT_HJUSTIFY_T aH_justify,
|
|
|
|
enum EDA_TEXT_VJUSTIFY_T aV_justify,
|
|
|
|
int aWidth,
|
|
|
|
bool aItalic,
|
2014-04-28 16:13:18 +00:00
|
|
|
bool aBold,
|
2016-09-19 11:01:36 +00:00
|
|
|
bool aMultilineAllowed,
|
|
|
|
void* aData )
|
2012-09-15 12:13:03 +00:00
|
|
|
{
|
|
|
|
setFillMode( NO_FILL );
|
|
|
|
SetColor( aColor );
|
|
|
|
SetCurrentLineWidth( aWidth );
|
|
|
|
|
2015-08-16 21:52:42 +00:00
|
|
|
int width = currentPenWidth;
|
|
|
|
|
|
|
|
if( aWidth <= 0 && aBold )
|
|
|
|
width = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
|
|
|
|
|
|
|
|
if( aWidth <= 0 )
|
|
|
|
width = currentPenWidth;
|
|
|
|
|
|
|
|
wxPoint text_pos = aPos;
|
|
|
|
const char *hjust = "start";
|
|
|
|
|
|
|
|
switch( aH_justify )
|
|
|
|
{
|
|
|
|
case GR_TEXT_HJUSTIFY_CENTER:
|
|
|
|
hjust = "middle";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GR_TEXT_HJUSTIFY_RIGHT:
|
|
|
|
hjust = "end";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GR_TEXT_HJUSTIFY_LEFT:
|
|
|
|
hjust = "start";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( aV_justify )
|
|
|
|
{
|
|
|
|
case GR_TEXT_VJUSTIFY_CENTER:
|
|
|
|
text_pos.y += aSize.y / 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GR_TEXT_VJUSTIFY_TOP:
|
|
|
|
text_pos.y += aSize.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GR_TEXT_VJUSTIFY_BOTTOM:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxSize text_size;
|
|
|
|
text_size.x = GraphicTextWidth( aText, aSize, aItalic, width );
|
|
|
|
text_size.y = aSize.x * 4/3; // Hershey font height to em size conversion
|
|
|
|
DPOINT anchor_pos_dev = userToDeviceCoordinates( aPos );
|
|
|
|
DPOINT text_pos_dev = userToDeviceCoordinates( text_pos );
|
|
|
|
DPOINT sz_dev = userToDeviceSize( text_size );
|
|
|
|
|
|
|
|
if( aOrient != 0 ) {
|
|
|
|
fprintf( outputFile,
|
|
|
|
"<g transform=\"rotate(%g %g %g)\">\n",
|
|
|
|
- aOrient * 0.1, anchor_pos_dev.x, anchor_pos_dev.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( outputFile,
|
|
|
|
"<text x=\"%g\" y=\"%g\"\n"
|
|
|
|
"textLength=\"%g\" font-size=\"%g\" lengthAdjust=\"spacingAndGlyphs\"\n"
|
|
|
|
"text-anchor=\"%s\" opacity=\"0\">%s</text>\n",
|
|
|
|
text_pos_dev.x, text_pos_dev.y,
|
|
|
|
sz_dev.x, sz_dev.y,
|
|
|
|
hjust, TO_UTF8( XmlEsc( aText ) ) );
|
|
|
|
|
|
|
|
if( aOrient != 0 )
|
|
|
|
fputs( "</g>\n", outputFile );
|
2012-09-15 12:13:03 +00:00
|
|
|
|
2019-06-28 21:17:46 +00:00
|
|
|
fprintf( outputFile,
|
|
|
|
"<g class=\"stroked-text\"><desc>%s</desc>\n",
|
|
|
|
TO_UTF8( XmlEsc( aText ) ) );
|
2012-09-15 12:13:03 +00:00
|
|
|
PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify,
|
2014-04-28 16:13:18 +00:00
|
|
|
aWidth, aItalic, aBold, aMultilineAllowed );
|
2019-06-28 21:17:46 +00:00
|
|
|
fputs( "</g>", outputFile );
|
2012-09-15 12:13:03 +00:00
|
|
|
}
|