1
0
Fork 0
freesewing/src/svg.js

289 lines
7.9 KiB
JavaScript
Raw Normal View History

import Attributes from "./attributes";
import * as hooklib from "hooks";
import { version } from "../package.json";
function Svg(pattern) {
this.openGroups = [];
this.freeId = 0;
this.body = "";
this.style = "";
this.script = "";
this.header = "";
this.footer = "";
this.defs = "";
2018-07-23 11:12:06 +00:00
this.pattern = pattern; // Needed to expose pattern to hooks
this.prefix = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
this.attributes = new Attributes();
2018-07-23 11:12:06 +00:00
this.attributes.add("xmlns", "http://www.w3.org/2000/svg");
this.attributes.add("xmlns:svg", "http://www.w3.org/2000/svg");
this.attributes.add("xmlns:xlink", "http://www.w3.org/1999/xlink");
this.attributes.add("xml:lang", pattern.settings.locale);
this.attributes.add(
"xmlns:freesewing",
"http://freesewing.org/namespaces/freesewing"
);
this.attributes.add("freesewing", version);
for (let k in hooklib) this[k] = hooklib[k];
2018-08-07 15:23:37 +02:00
//this.hooks was injected into the prototype by pattern
let self = this;
2018-08-10 15:36:39 +02:00
this.hooks.attach("preRender", self);
this.hooks.attach("postRender", self);
2018-08-07 15:23:37 +02:00
this.hooks.attach("insertText", self);
this.hooks.attach("debug", self);
2018-07-23 20:14:32 +02:00
}
2018-07-23 11:12:06 +00:00
2018-08-10 15:36:39 +02:00
/** Method to attach preRender hooks on */
Svg.prototype.preRender = function() {};
2018-07-23 20:14:32 +02:00
2018-08-10 15:36:39 +02:00
/** Method to attach postRender hooks on */
Svg.prototype.postRender = function() {};
2018-07-23 20:14:32 +02:00
/** Method to attach insertText hooks on */
2018-08-07 15:23:37 +02:00
Svg.prototype.insertText = function() {};
/** Debug method, exposes debug hook */
Svg.prototype.debug = function() {};
2018-07-23 20:14:32 +02:00
/** Renders a draft object as SVG */
Svg.prototype.render = function(pattern) {
2018-08-16 15:44:01 +02:00
this.idPrefix = pattern.settings.idPrefix;
2018-08-10 15:36:39 +02:00
this.preRender();
2018-08-16 13:59:11 +02:00
if (!pattern.settings.embed) {
this.attributes.add("width", pattern.width + "mm");
this.attributes.add("height", pattern.height + "mm");
}
this.attributes.add("viewBox", `0 0 ${pattern.width} ${pattern.height}`);
2018-07-23 20:14:32 +02:00
this.svg = this.prefix;
this.svg += this.renderComments(this.header);
this.svg += this.renderSvgTag(pattern);
this.svg += this.renderStyle();
this.svg += this.renderScript();
this.svg += this.renderDefs();
2018-08-16 15:44:01 +02:00
this.svg += this.openGroup(this.idPrefix + "container");
2018-07-23 20:14:32 +02:00
for (let partId in pattern.parts) {
let part = pattern.parts[partId];
if (part.render && pattern.needs(partId)) {
2018-08-16 15:44:01 +02:00
this.svg += this.openGroup(
`${this.idPrefix}part-${partId}`,
part.attributes
);
2018-07-23 20:14:32 +02:00
this.svg += this.renderPart(part);
this.svg += this.closeGroup();
2018-07-14 16:04:39 +00:00
}
}
2018-07-23 20:14:32 +02:00
this.svg += this.closeGroup();
this.svg += this.nl() + "</svg>";
this.svg += this.renderComments(this.footer);
2018-08-10 15:36:39 +02:00
this.postRender();
2018-07-23 20:14:32 +02:00
return this.svg;
};
/** Returns SVG code for the opening SVG tag */
Svg.prototype.renderSvgTag = function(pattern) {
2018-07-23 20:14:32 +02:00
let svg = "<svg";
this.indent();
svg += this.nl() + this.attributes.render();
this.outdent();
svg += this.nl() + ">" + this.nl();
return svg;
};
/** Returns SVG code for the style block */
Svg.prototype.renderStyle = function() {
2018-07-23 20:14:32 +02:00
let svg = '<style type="text/css"> <![CDATA[ ';
this.indent();
svg += this.nl() + this.style;
this.outdent();
svg += this.nl() + "]]>" + this.nl() + "</style>" + this.nl();
return svg;
};
/** Returns SVG code for the script block */
Svg.prototype.renderScript = function() {
2018-07-23 20:14:32 +02:00
let svg = '<script type="text/javascript"> <![CDATA[';
this.indent();
svg += this.nl() + this.script;
this.outdent();
svg += this.nl() + "]]>" + this.nl() + "</script>" + this.nl();
return svg;
};
/** Returns SVG code for the defs block */
Svg.prototype.renderDefs = function() {
2018-08-16 15:44:01 +02:00
let svg = "<defs>";
2018-07-23 20:14:32 +02:00
this.indent();
svg += this.nl() + this.defs;
this.outdent();
svg += this.nl() + "</defs>" + this.nl();
return svg;
};
/** Returns SVG code for a comment block */
Svg.prototype.renderComments = function(comments) {
2018-07-23 20:14:32 +02:00
return (
this.nl() + this.nl() + "<!--" + this.nl() + comments + this.nl() + "-->"
);
};
/** Returns SVG code for a Part object */
Svg.prototype.renderPart = function(part) {
2018-07-23 20:14:32 +02:00
let svg = "";
for (let key in part.paths) {
let path = part.paths[key];
if (path.render) svg += this.renderPath(path);
}
for (let key in part.points) {
if (part.points[key].attributes.get("data-text")) {
svg += this.renderText(part.points[key]);
}
if (part.points[key].attributes.get("data-circle")) {
svg += this.renderCircle(part.points[key]);
}
2018-07-23 20:14:32 +02:00
}
for (let key in part.snippets) {
let snippet = part.snippets[key];
svg += this.renderSnippet(snippet);
}
2018-07-23 20:14:32 +02:00
return svg;
};
/** Returns SVG code for a Path object */
Svg.prototype.renderPath = function(path) {
2018-08-16 15:44:01 +02:00
if (!path.attributes.get("id"))
path.attributes.add("id", this.idPrefix + this.getId());
2018-08-16 15:44:01 +02:00
path.attributes.set("d", path.asPathstring());
2018-07-23 20:14:32 +02:00
return `${this.nl()}<path ${path.attributes.render()} />${this.renderPathText(
path
)}`;
};
Svg.prototype.renderPathText = function(path) {
this.text = path.attributes.get("data-text");
if (!this.text) return "";
else this.insertText();
2018-07-23 20:14:32 +02:00
let attributes = path.attributes.renderIfPrefixIs("data-text-");
2018-07-25 14:53:10 +00:00
// Sadly aligning text along a patch can't be done in CSS only
let offset = "";
let align = path.attributes.get("data-text-class");
if (align && align.indexOf("center") > -1) offset = ' startOffset="50%" ';
else if (align && align.indexOf("right") > -1)
offset = ' startOffset="100%" ';
2018-07-23 20:14:32 +02:00
let svg = this.nl() + "<text>";
this.indent();
svg += `<textPath xlink:href="#${path.attributes.get(
"id"
)}" ${offset}><tspan ${attributes}>${this.text}</tspan></textPath>`;
2018-07-23 20:14:32 +02:00
this.outdent();
svg += this.nl() + "</text>";
return svg;
};
Svg.prototype.renderText = function(point) {
this.text = point.attributes.get("data-text");
this.insertText();
point.attributes.set("data-text-x", point.x);
point.attributes.set("data-text-y", point.y);
let lineHeight = point.attributes.get("data-text-lineheight") || 12;
point.attributes.remove("data-text-lineheight");
2018-07-23 20:14:32 +02:00
let svg = `${this.nl()}<text ${point.attributes.renderIfPrefixIs(
"data-text-"
)}>`;
this.indent();
2018-08-14 12:56:25 +02:00
// Multi-line text?
if (this.text.indexOf("\n") !== -1) {
2018-08-14 12:56:25 +02:00
let lines = this.text.split("\n");
svg += `<tspan>${lines.shift()}</tspan>`;
for (let line of lines) {
svg += `<tspan x="${point.x}" dy="${lineHeight}">${line}</tspan>`;
}
} else {
svg += `<tspan>${this.text}</tspan>`;
}
2018-07-23 20:14:32 +02:00
this.outdent();
svg += this.nl() + "</text>";
return svg;
};
Svg.prototype.renderCircle = function(point) {
return `<circle
cx="${point.x}"
cy="${point.y}"
r="${point.attributes.get("data-circle")}"
${point.attributes.renderIfPrefixIs("data-circle-")}
></circle>`;
};
2018-07-23 20:14:32 +02:00
/** Returns SVG code for a snippet */
Svg.prototype.renderSnippet = function(snippet) {
2018-07-23 20:14:32 +02:00
let svg = this.nl();
svg += `<use x="${snippet.anchor.x}" y="${snippet.anchor.y}" `;
svg += `xlink:href="#${snippet.def}" ${snippet.attributes.render()}>`;
svg += "</use>";
return svg;
};
/** Returns SVG code to open a group */
Svg.prototype.openGroup = function(id, attributes = false) {
2018-07-23 20:14:32 +02:00
let svg = this.nl() + this.nl();
svg += `<!-- Start of group #${id} -->`;
svg += this.nl();
2018-08-01 18:18:29 +02:00
svg += `<g id="${id}"`;
if (attributes) svg += ` ${attributes.render()}`;
svg += ">";
2018-07-23 20:14:32 +02:00
this.indent();
this.openGroups.push(id);
return svg;
};
/** Returns SVG code to close a group */
Svg.prototype.closeGroup = function() {
2018-07-23 20:14:32 +02:00
this.outdent();
return `${this.nl()}</g>${this.nl()}<!-- end of group #${this.openGroups.pop()} -->`;
};
/** Returns a linebreak + identation */
Svg.prototype.nl = function() {
2018-07-23 20:14:32 +02:00
return "\n" + this.tab();
};
/** Returns indentation */
Svg.prototype.tab = function() {
2018-07-23 20:14:32 +02:00
let space = "";
for (let i = 0; i < this.tabs; i++) {
space += " ";
}
2018-07-14 16:04:39 +00:00
2018-07-23 20:14:32 +02:00
return space;
};
2018-07-14 16:04:39 +00:00
2018-07-23 20:14:32 +02:00
/** Increases indentation by 1 */
Svg.prototype.indent = function() {
2018-07-23 20:14:32 +02:00
this.tabs += 1;
};
2018-07-14 16:04:39 +00:00
2018-07-23 20:14:32 +02:00
/** Decreases indentation by 1 */
Svg.prototype.outdent = function() {
2018-07-23 20:14:32 +02:00
this.tabs -= 1;
};
2018-07-14 16:04:39 +00:00
2018-07-23 20:14:32 +02:00
/** Returns an unused ID */
Svg.prototype.getId = function() {
2018-07-23 20:14:32 +02:00
this.freeId += 1;
2018-07-23 20:14:32 +02:00
return "" + this.freeId;
};
2018-07-23 11:12:06 +00:00
export default Svg;