PrusaSlicer-NonPlainar/xs/src/libslic3r/SVG.cpp

114 lines
3.1 KiB
C++
Raw Normal View History

#include "SVG.hpp"
2015-01-06 15:26:15 +00:00
#include <iostream>
#define COORD(x) ((float)unscale(x)*10)
namespace Slic3r {
SVG::SVG(const char* filename)
2015-01-06 15:26:15 +00:00
: arrows(true), filename(filename), fill("grey"), stroke("black")
{
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"
);
}
void
SVG::draw(const Line &line, std::string stroke)
{
fprintf(this->f,
" <line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: %s; stroke-width: 1\"",
COORD(line.a.x), COORD(line.a.y), COORD(line.b.x), COORD(line.b.y), stroke.c_str()
);
2013-10-26 12:41:37 +00:00
if (this->arrows)
fprintf(this->f, " marker-end=\"url(#endArrow)\"");
fprintf(this->f, "/>\n");
}
void
SVG::AddLine(const IntersectionLine &line)
{
this->draw(Line(line.a, line.b));
}
2015-01-06 15:26:15 +00:00
void
SVG::draw(const ExPolygon &expolygon, std::string fill)
2015-01-06 15:26:15 +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) {
d += this->get_path_d(*p, true) + " ";
2015-01-06 15:26:15 +00:00
}
this->path(d, true);
}
void
SVG::draw(const Polygon &polygon, std::string fill)
2015-01-06 15:26:15 +00:00
{
this->fill = fill;
this->path(this->get_path_d(polygon, true), true);
2015-01-06 15:26:15 +00:00
}
void
SVG::draw(const Polyline &polyline, std::string stroke)
2015-01-06 15:26:15 +00:00
{
this->stroke = stroke;
this->path(this->get_path_d(polyline, false), false);
}
void
SVG::draw(const Point &point, std::string fill, unsigned int radius)
{
std::ostringstream svg;
svg << " <circle cx=\"" << COORD(point.x) << "\" cy=\"" << COORD(point.y)
<< "\" r=\"" << radius << "\" "
<< "style=\"stroke: none; fill: " << fill << "\" />";
fprintf(this->f, "%s\n", svg.str().c_str());
2015-01-06 15:26:15 +00:00
}
void
SVG::path(const std::string &d, bool fill)
{
fprintf(
this->f,
" <path d=\"%s\" style=\"fill: %s; stroke: %s; stroke-width: %s; 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(),
fill ? "0" : "2",
(this->arrows && !fill) ? " marker-end=\"url(#endArrow)\"" : ""
2015-01-06 15:26:15 +00:00
);
}
std::string
SVG::get_path_d(const MultiPoint &mp, bool closed) const
2015-01-06 15:26:15 +00:00
{
std::ostringstream d;
d << "M ";
2015-01-06 15:26:15 +00:00
for (Points::const_iterator p = mp.points.begin(); p != mp.points.end(); ++p) {
d << COORD(p->x) << " ";
d << COORD(p->y) << " ";
2015-01-06 15:26:15 +00:00
}
if (closed) d << "z";
2015-01-06 15:26:15 +00:00
return d.str();
}
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());
}
}