1
0
Fork 0
freesewing/lib/svg.ts

166 lines
3.9 KiB
TypeScript
Raw Normal View History

2018-07-14 16:04:39 +00:00
import { Part } from './part'
import { Path } from './path'
import { Pattern } from './pattern'
import { Attributes } from './attributes'
2018-07-15 08:36:19 +00:00
export class Svg {
prefix: string;
body: string = '';
style: string = '';
header: string = '';
footer: string = '';
defs: string = '';
attributes: Attributes = new Attributes();
2018-07-14 16:04:39 +00:00
tabs: number = 0;
freeId: number = 1;
openGroups: string[] = [];
constructor() {
2018-07-15 08:36:19 +00:00
this.prefix = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
this.attributes.add
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("xmlns:freesewing", "http://freesewing.org/namespaces/freesewing");
this.attributes.add("freesewing:foo", "bar");
2018-07-15 08:36:19 +00:00
2018-07-14 16:04:39 +00:00
return this;
}
/** Renders a draft object as SVG */
render(pattern: Pattern): string {
let svg = this.prefix;
svg += this.renderComments(this.header);
svg += this.renderSvgTag(pattern);
svg += this.renderStyle();
svg += this.renderDefs();
svg += this.openGroup('draftContainer');
2018-07-14 16:04:39 +00:00
for (let partId in pattern.parts) {
let part = pattern.parts[partId];
if (part.render) {
svg += this.openGroup(part.id, part.attributes);
svg += this.renderPart(part);
svg += this.closeGroup();
}
}
svg += this.closeGroup();
svg += this.nl()+'</svg>';
svg += this.renderComments(this.footer);
2018-07-14 16:04:39 +00:00
return svg;
}
/** Returns SVG code for the opening SVG tag */
renderSvgTag(pattern: Pattern) {
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 */
renderStyle() {
let svg = '<style type="text/css">';
this.indent();
svg += this.nl()+this.style;
this.outdent();
svg += this.nl()+']]> >'+this.nl();
return svg;
}
/** Returns SVG code for the defs block */
renderDefs() {
let svg = '<defs id="defs">';
this.indent();
svg += this.nl()+this.defs;
this.outdent();
svg += this.nl()+'</defs>'+this.nl();
return svg;
}
/** Returns SVG code for a comment block */
renderComments(comments: string) {
return this.nl()+this.nl()+'<!--'+this.nl()+comments+this.nl()+'-->';
}
2018-07-14 16:04:39 +00:00
/** Returns SVG code for a Part object */
renderPart(part: Part): string {
let svg = '';
for (let pathId in part.paths) {
let path = part.paths[pathId];
if(path.render) svg += this.renderPath(path);
}
// includes
// text on path
// notes
// dimensions
// texts
// snippets
return svg;
}
/** Returns SVG code for a Path object */
renderPath(path: Path): string {
2018-07-15 08:36:19 +00:00
path.attributes.add('d', path.asPathstring());
return `${this.nl()}<path ${path.attributes.render()} />`;
2018-07-14 16:04:39 +00:00
}
/** Returns SVG code to open a group */
openGroup(id: string, attributes?: Attributes): string {
2018-07-14 16:04:39 +00:00
let svg = this.nl()+this.nl();
svg += `<!-- Start of group #${id} -->`;
svg += this.nl();
svg += `<g id="${id}">`;
this.indent();
this.openGroups.push(id);
return svg;
}
/** Returns SVG code to close a group */
closeGroup(): string {
2018-07-14 16:04:39 +00:00
this.outdent();
return `${this.nl()}</g>${this.nl()}<!-- end of group #${this.openGroups.pop()} -->`;
}
/** Returns a linebreak + identation */
nl(): string {
return "\n"+this.tab();
}
/** Returns indentation */
tab(): string {
let space = '';
for (let i = 0; i < this.tabs; i++) {
space += ' ';
}
return space;
2018-07-14 16:04:39 +00:00
}
/** Increases indentation by 1 */
indent(): void {
2018-07-14 16:04:39 +00:00
this.tabs += 1;
}
/** Decreases indentation by 1 */
outdent(): void {
2018-07-14 16:04:39 +00:00
this.tabs -= 1;
}
/** Returns an unused ID */
getUid() {
2018-07-14 16:04:39 +00:00
this.freeId += 1;
return this.freeId;
}
}