Path: blob/master/thirdparty/msdfgen/core/export-svg.cpp
9902 views
1#include "export-svg.h"23#include <cstdio>4#include "edge-segments.h"56namespace msdfgen {78static void writeSvgCoord(FILE *f, Point2 coord) {9fprintf(f, "%.17g %.17g", coord.x, coord.y);10}1112static void writeSvgPathDef(FILE *f, const Shape &shape) {13bool beginning = true;14for (const Contour &c : shape.contours) {15if (c.edges.empty())16continue;17if (beginning)18beginning = false;19else20fputc(' ', f);21fputs("M ", f);22writeSvgCoord(f, c.edges[0]->controlPoints()[0]);23for (const EdgeHolder &e : c.edges) {24const Point2 *cp = e->controlPoints();25switch (e->type()) {26case (int) LinearSegment::EDGE_TYPE:27fputs(" L ", f);28writeSvgCoord(f, cp[1]);29break;30case (int) QuadraticSegment::EDGE_TYPE:31fputs(" Q ", f);32writeSvgCoord(f, cp[1]);33fputc(' ', f);34writeSvgCoord(f, cp[2]);35break;36case (int) CubicSegment::EDGE_TYPE:37fputs(" C ", f);38writeSvgCoord(f, cp[1]);39fputc(' ', f);40writeSvgCoord(f, cp[2]);41fputc(' ', f);42writeSvgCoord(f, cp[3]);43break;44}45}46fputs(" Z", f);47}48}4950bool saveSvgShape(const Shape &shape, const char *filename) {51if (FILE *f = fopen(filename, "w")) {52fputs("<svg xmlns=\"http://www.w3.org/2000/svg\"><path", f);53if (!shape.inverseYAxis)54fputs(" transform=\"scale(1 -1)\"", f);55fputs(" d=\"", f);56writeSvgPathDef(f, shape);57fputs("\"/></svg>\n", f);58fclose(f);59return true;60}61return false;62}6364bool saveSvgShape(const Shape &shape, const Shape::Bounds &bounds, const char *filename) {65if (FILE *f = fopen(filename, "w")) {66fprintf(f, "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"%.17g %.17g %.17g %.17g\"><path", bounds.l, bounds.b, bounds.r-bounds.l, bounds.t-bounds.b);67if (!shape.inverseYAxis)68fprintf(f, " transform=\"translate(0 %.17g) scale(1 -1)\"", bounds.b+bounds.t);69fputs(" d=\"", f);70writeSvgPathDef(f, shape);71fputs("\"/></svg>\n", f);72fclose(f);73return true;74}75return false;76}7778}798081