2013-10-13 13:59:38 +00:00
|
|
|
#include "SVG.hpp"
|
2015-01-06 15:26:15 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#define COORD(x) ((float)unscale(x)*10)
|
2013-10-13 13:59:38 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
SVG::SVG(const char* filename)
|
2016-03-19 14:33:58 +00:00
|
|
|
: arrows(false), fill("grey"), stroke("black"), filename(filename)
|
2013-10-13 13:59:38 +00:00
|
|
|
{
|
|
|
|
this->f = fopen(filename, "w");
|
|
|
|
fprintf(this->f,
|
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
|
|
|
|
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
|
|
|
|
"<svg height=\"2000\" width=\"2000\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"
|
|
|
|
" <marker id=\"endArrow\" markerHeight=\"8\" markerUnits=\"strokeWidth\" markerWidth=\"10\" orient=\"auto\" refX=\"1\" refY=\"5\" viewBox=\"0 0 10 10\">\n"
|
|
|
|
" <polyline fill=\"darkblue\" points=\"0,0 10,5 0,10 1,5\" />\n"
|
|
|
|
" </marker>\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-27 08:53:59 +00:00
|
|
|
SVG::SVG(const char* filename, const BoundingBox &bbox)
|
|
|
|
: arrows(false), fill("grey"), stroke("black"), filename(filename), origin(bbox.min)
|
|
|
|
{
|
|
|
|
this->f = fopen(filename, "w");
|
|
|
|
float w = COORD(bbox.max.x - bbox.min.x);
|
|
|
|
float h = COORD(bbox.max.y - bbox.min.y);
|
|
|
|
fprintf(this->f,
|
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
|
|
|
|
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
|
|
|
|
"<svg height=\"%f\" width=\"%f\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"
|
|
|
|
" <marker id=\"endArrow\" markerHeight=\"8\" markerUnits=\"strokeWidth\" markerWidth=\"10\" orient=\"auto\" refX=\"1\" refY=\"5\" viewBox=\"0 0 10 10\">\n"
|
|
|
|
" <polyline fill=\"darkblue\" points=\"0,0 10,5 0,10 1,5\" />\n"
|
|
|
|
" </marker>\n",
|
|
|
|
h, w);
|
|
|
|
}
|
|
|
|
|
2013-10-13 13:59:38 +00:00
|
|
|
void
|
2016-03-27 08:53:59 +00:00
|
|
|
SVG::draw(const Line &line, std::string stroke, coord_t stroke_width)
|
2013-10-13 13:59:38 +00:00
|
|
|
{
|
|
|
|
fprintf(this->f,
|
2016-03-27 08:53:59 +00:00
|
|
|
" <line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: %s; stroke-width: %f\"",
|
|
|
|
COORD(line.a.x - origin.x), COORD(line.a.y - origin.y), COORD(line.b.x - origin.x), COORD(line.b.y - origin.y), stroke.c_str(), (stroke_width == 0) ? 1.f : COORD(stroke_width));
|
2013-10-26 12:41:37 +00:00
|
|
|
if (this->arrows)
|
|
|
|
fprintf(this->f, " marker-end=\"url(#endArrow)\"");
|
|
|
|
fprintf(this->f, "/>\n");
|
|
|
|
}
|
|
|
|
|
2016-03-27 08:53:59 +00:00
|
|
|
void SVG::draw(const ThickLine &line, const std::string &fill, const std::string &stroke, coord_t stroke_width)
|
|
|
|
{
|
|
|
|
Pointf dir(line.b.x-line.a.x, line.b.y-line.a.y);
|
|
|
|
Pointf perp(-dir.y, dir.x);
|
|
|
|
coordf_t len = sqrt(perp.x*perp.x + perp.y*perp.y);
|
|
|
|
coordf_t da = coordf_t(0.5)*line.a_width/len;
|
|
|
|
coordf_t db = coordf_t(0.5)*line.b_width/len;
|
|
|
|
fprintf(this->f,
|
|
|
|
" <polygon points=\"%f,%f %f,%f %f,%f %f,%f\" style=\"fill:%s; stroke: %s; stroke-width: %f\"/>\n",
|
|
|
|
COORD(line.a.x-da*perp.x-origin.x),
|
|
|
|
COORD(line.a.y-da*perp.y-origin.y),
|
|
|
|
COORD(line.b.x-db*perp.x-origin.x),
|
|
|
|
COORD(line.b.y-db*perp.y-origin.y),
|
|
|
|
COORD(line.b.x+db*perp.x-origin.x),
|
|
|
|
COORD(line.b.y+db*perp.y-origin.y),
|
|
|
|
COORD(line.a.x+da*perp.x-origin.x),
|
|
|
|
COORD(line.a.y+da*perp.y-origin.y),
|
|
|
|
fill.c_str(), stroke.c_str(),
|
|
|
|
(stroke_width == 0) ? 1.f : COORD(stroke_width));
|
|
|
|
}
|
|
|
|
|
2013-10-26 12:41:37 +00:00
|
|
|
void
|
2015-01-28 12:00:38 +00:00
|
|
|
SVG::draw(const Lines &lines, std::string stroke)
|
2013-10-26 12:41:37 +00:00
|
|
|
{
|
2015-01-28 12:00:38 +00:00
|
|
|
for (Lines::const_iterator it = lines.begin(); it != lines.end(); ++it)
|
|
|
|
this->draw(*it, stroke);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SVG::draw(const IntersectionLines &lines, std::string stroke)
|
|
|
|
{
|
|
|
|
for (IntersectionLines::const_iterator it = lines.begin(); it != lines.end(); ++it)
|
|
|
|
this->draw((Line)*it, stroke);
|
2013-10-13 13:59:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 15:26:15 +00:00
|
|
|
void
|
2015-01-06 19:51:48 +00:00
|
|
|
SVG::draw(const ExPolygon &expolygon, std::string fill)
|
2015-01-06 15:26:15 +00:00
|
|
|
{
|
2015-01-06 19:51:48 +00:00
|
|
|
this->fill = fill;
|
|
|
|
|
2015-01-06 15:26:15 +00:00
|
|
|
std::string d;
|
|
|
|
Polygons pp = expolygon;
|
|
|
|
for (Polygons::const_iterator p = pp.begin(); p != pp.end(); ++p) {
|
2015-01-06 19:51:48 +00:00
|
|
|
d += this->get_path_d(*p, true) + " ";
|
2015-01-06 15:26:15 +00:00
|
|
|
}
|
|
|
|
this->path(d, true);
|
|
|
|
}
|
|
|
|
|
2015-01-28 12:00:38 +00:00
|
|
|
void
|
|
|
|
SVG::draw(const ExPolygons &expolygons, std::string fill)
|
|
|
|
{
|
|
|
|
for (ExPolygons::const_iterator it = expolygons.begin(); it != expolygons.end(); ++it)
|
|
|
|
this->draw(*it, fill);
|
|
|
|
}
|
|
|
|
|
2015-01-06 15:26:15 +00:00
|
|
|
void
|
2015-01-06 19:51:48 +00:00
|
|
|
SVG::draw(const Polygon &polygon, std::string fill)
|
2015-01-06 15:26:15 +00:00
|
|
|
{
|
2015-01-06 19:51:48 +00:00
|
|
|
this->fill = fill;
|
2015-01-28 12:00:38 +00:00
|
|
|
this->path(this->get_path_d(polygon, true), !fill.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SVG::draw(const Polygons &polygons, std::string fill)
|
|
|
|
{
|
|
|
|
for (Polygons::const_iterator it = polygons.begin(); it != polygons.end(); ++it)
|
|
|
|
this->draw(*it, fill);
|
2015-01-06 15:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-27 08:53:59 +00:00
|
|
|
SVG::draw(const Polyline &polyline, std::string stroke, coord_t stroke_width)
|
2015-01-06 15:26:15 +00:00
|
|
|
{
|
2015-01-06 19:51:48 +00:00
|
|
|
this->stroke = stroke;
|
2016-03-27 08:53:59 +00:00
|
|
|
this->path(this->get_path_d(polyline, false), false, stroke_width);
|
2015-01-06 19:51:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 12:00:38 +00:00
|
|
|
void
|
2016-03-27 08:53:59 +00:00
|
|
|
SVG::draw(const Polylines &polylines, std::string stroke, coord_t stroke_width)
|
2015-01-28 12:00:38 +00:00
|
|
|
{
|
|
|
|
for (Polylines::const_iterator it = polylines.begin(); it != polylines.end(); ++it)
|
2016-03-27 08:53:59 +00:00
|
|
|
this->draw(*it, fill, stroke_width);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SVG::draw(const ThickLines &thicklines, const std::string &fill, const std::string &stroke, coord_t stroke_width)
|
|
|
|
{
|
|
|
|
for (ThickLines::const_iterator it = thicklines.begin(); it != thicklines.end(); ++it)
|
|
|
|
this->draw(*it, fill, stroke, stroke_width);
|
2016-03-19 14:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-27 08:53:59 +00:00
|
|
|
SVG::draw(const ThickPolylines &polylines, const std::string &stroke, coord_t stroke_width)
|
2016-03-19 14:33:58 +00:00
|
|
|
{
|
|
|
|
for (ThickPolylines::const_iterator it = polylines.begin(); it != polylines.end(); ++it)
|
2016-03-27 08:53:59 +00:00
|
|
|
this->draw((Polyline)*it, stroke, stroke_width);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SVG::draw(const ThickPolylines &thickpolylines, const std::string &fill, const std::string &stroke, coord_t stroke_width)
|
|
|
|
{
|
|
|
|
for (ThickPolylines::const_iterator it = thickpolylines.begin(); it != thickpolylines.end(); ++ it)
|
|
|
|
draw(it->thicklines(), fill, stroke, stroke_width);
|
2015-01-28 12:00:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 19:51:48 +00:00
|
|
|
void
|
2016-03-27 08:53:59 +00:00
|
|
|
SVG::draw(const Point &point, std::string fill, coord_t iradius)
|
2015-01-06 19:51:48 +00:00
|
|
|
{
|
2016-03-27 08:53:59 +00:00
|
|
|
float radius = (iradius == 0) ? 3.f : COORD(iradius);
|
2015-01-06 19:51:48 +00:00
|
|
|
std::ostringstream svg;
|
2016-03-27 08:53:59 +00:00
|
|
|
svg << " <circle cx=\"" << COORD(point.x - origin.x) << "\" cy=\"" << COORD(point.y - origin.y)
|
2015-01-06 19:51:48 +00:00
|
|
|
<< "\" r=\"" << radius << "\" "
|
|
|
|
<< "style=\"stroke: none; fill: " << fill << "\" />";
|
|
|
|
|
|
|
|
fprintf(this->f, "%s\n", svg.str().c_str());
|
2015-01-06 15:26:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 12:00:38 +00:00
|
|
|
void
|
2016-03-27 08:53:59 +00:00
|
|
|
SVG::draw(const Points &points, std::string fill, coord_t radius)
|
2015-01-28 12:00:38 +00:00
|
|
|
{
|
|
|
|
for (Points::const_iterator it = points.begin(); it != points.end(); ++it)
|
|
|
|
this->draw(*it, fill, radius);
|
|
|
|
}
|
|
|
|
|
2015-01-06 15:26:15 +00:00
|
|
|
void
|
2016-03-27 08:53:59 +00:00
|
|
|
SVG::path(const std::string &d, bool fill, coord_t stroke_width)
|
2015-01-06 15:26:15 +00:00
|
|
|
{
|
2016-03-27 08:53:59 +00:00
|
|
|
float lineWidth = 0.f;
|
|
|
|
if (! fill)
|
|
|
|
lineWidth = (stroke_width == 0) ? 2.f : COORD(stroke_width);
|
|
|
|
|
2015-01-06 15:26:15 +00:00
|
|
|
fprintf(
|
|
|
|
this->f,
|
2016-03-27 08:53:59 +00:00
|
|
|
" <path d=\"%s\" style=\"fill: %s; stroke: %s; stroke-width: %f; fill-type: evenodd\" %s />\n",
|
2015-01-06 15:26:15 +00:00
|
|
|
d.c_str(),
|
|
|
|
fill ? this->fill.c_str() : "none",
|
|
|
|
this->stroke.c_str(),
|
2016-03-27 08:53:59 +00:00
|
|
|
lineWidth,
|
2015-01-06 19:51:48 +00:00
|
|
|
(this->arrows && !fill) ? " marker-end=\"url(#endArrow)\"" : ""
|
2015-01-06 15:26:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2015-01-06 19:51:48 +00:00
|
|
|
SVG::get_path_d(const MultiPoint &mp, bool closed) const
|
2015-01-06 15:26:15 +00:00
|
|
|
{
|
|
|
|
std::ostringstream d;
|
2015-01-06 19:51:48 +00:00
|
|
|
d << "M ";
|
2015-01-06 15:26:15 +00:00
|
|
|
for (Points::const_iterator p = mp.points.begin(); p != mp.points.end(); ++p) {
|
2016-03-27 08:53:59 +00:00
|
|
|
d << COORD(p->x - origin.x) << " ";
|
|
|
|
d << COORD(p->y - origin.y) << " ";
|
2015-01-06 15:26:15 +00:00
|
|
|
}
|
2015-01-06 19:51:48 +00:00
|
|
|
if (closed) d << "z";
|
2015-01-06 15:26:15 +00:00
|
|
|
return d.str();
|
|
|
|
}
|
|
|
|
|
2013-10-13 13:59:38 +00:00
|
|
|
void
|
|
|
|
SVG::Close()
|
|
|
|
{
|
|
|
|
fprintf(this->f, "</svg>\n");
|
|
|
|
fclose(this->f);
|
2015-01-06 15:26:15 +00:00
|
|
|
printf("SVG written to %s\n", this->filename.c_str());
|
2013-10-13 13:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|