From eee6c2814a84882bdd5e5e49688adbce3747844c Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Sun, 13 Jan 2019 15:00:01 +0100 Subject: [PATCH] :sparkles: Added new layout setting to bypass bin packing --- src/pattern.js | 35 +++++---- src/svg.js | 56 ++++++++------ tests/fixtures/render.js | 156 +++++++-------------------------------- tests/pattern.test.js | 4 +- tests/svg.test.js | 2 - 5 files changed, 82 insertions(+), 171 deletions(-) diff --git a/src/pattern.js b/src/pattern.js index e604c708e2c..77b870c91a4 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -11,8 +11,8 @@ import Attributes from "./attributes"; export default function Pattern(config = { options: {} }) { this.config = config; // Pattern configuration - this.width = false; // Will be set after render - this.height = false; // Will be set after render + this.width = 0; // Will be set after render + this.height = 0; // Will be set after render this.is = ""; // Will be set when drafting/sampling this.store = new Store(); // Store for sharing data across parts @@ -31,6 +31,7 @@ export default function Pattern(config = { options: {} }) { locale: "en", units: "metric", margin: 2, + layout: true, options: {} }; @@ -383,24 +384,28 @@ Pattern.prototype.pack = function() { for (let key in this.parts) { let part = this.parts[key]; // Avoid multiple render calls to cause stacking of transforms - part.attributes.set("transform", ""); + part.attributes.remove("transform"); if (part.render && this.needs(key)) { part.stack(); - bins.push({ - id: key, - width: part.bottomRight.x - part.topLeft.x, - height: part.bottomRight.y - part.topLeft.y - }); + let width = part.bottomRight.x - part.topLeft.x; + let height = part.bottomRight.y - part.topLeft.y; + if (this.settings.layout) bins.push({ id: key, width, height }); + else { + if (this.width < width) this.width = width; + if (this.height < height) this.height = height; + } } } - let size = pack(bins, { inPlace: true }); - for (let bin of bins) { - let part = this.parts[bin.id]; - if (bin.x !== 0 || bin.y !== 0) - part.attr("transform", `translate (${bin.x}, ${bin.y})`); + if (this.settings.layout) { + let size = pack(bins, { inPlace: true }); + for (let bin of bins) { + let part = this.parts[bin.id]; + if (bin.x !== 0 || bin.y !== 0) + part.attr("transform", `translate (${bin.x}, ${bin.y})`); + } + this.width = size.width; + this.height = size.height; } - this.width = size.width; - this.height = size.height; return this; }; diff --git a/src/svg.js b/src/svg.js index ff7ffe92ec7..9815bf188b7 100644 --- a/src/svg.js +++ b/src/svg.js @@ -4,12 +4,11 @@ import { version } from "../package.json"; function Svg(pattern) { this.openGroups = []; + this.layout = {}; this.freeId = 0; this.body = ""; this.style = ""; this.script = ""; - this.header = ""; - this.footer = ""; this.defs = ""; this.pattern = pattern; // Needed to expose pattern to hooks this.prefix = ''; @@ -52,39 +51,59 @@ Svg.prototype.debug = function() {}; Svg.prototype.render = function(pattern) { this.idPrefix = pattern.settings.idPrefix; this.runHooks("preRender"); - //this.preRender(); 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}`); - 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(); - this.svg += this.openGroup(this.idPrefix + "container"); + this.head = this.renderHead(); + this.tail = this.renderTail(); + this.svg = ""; + this.layout = {}; // Reset layout for (let partId in pattern.parts) { let part = pattern.parts[partId]; if (part.render && pattern.needs(partId)) { + let partSvg = this.renderPart(part); + this.layout[partId] = { + svg: partSvg, + transform: part.attributes.getAsArray("transform") + }; this.svg += this.openGroup( `${this.idPrefix}part-${partId}`, part.attributes ); - this.svg += this.renderPart(part); + this.svg += partSvg; this.svg += this.closeGroup(); } } - this.svg += this.closeGroup(); - this.svg += this.nl() + ""; - this.svg += this.renderComments(this.footer); + this.svg = + this.prefix + this.renderSvgTag() + this.head + this.svg + this.tail; this.runHooks("postRender"); + return this.svg; }; +/** Renders SVG head section */ +Svg.prototype.renderHead = function() { + let svg = this.renderStyle(); + svg += this.renderScript(); + svg += this.renderDefs(); + svg += this.openGroup(this.idPrefix + "container"); + + return svg; +}; + +/** Renders SVG closing section */ +Svg.prototype.renderTail = function() { + let svg = ""; + svg += this.closeGroup(); + svg += this.nl() + ""; + + return svg; +}; + /** Returns SVG code for the opening SVG tag */ -Svg.prototype.renderSvgTag = function(pattern) { +Svg.prototype.renderSvgTag = function() { let svg = "" - ); -}; - /** Returns SVG code for a Part object */ Svg.prototype.renderPart = function(part) { let svg = ""; diff --git a/tests/fixtures/render.js b/tests/fixtures/render.js index fb47f157d98..d39302f1ea3 100644 --- a/tests/fixtures/render.js +++ b/tests/fixtures/render.js @@ -1,11 +1,7 @@ var version = require("../../package.json").version; var render = { - boilerplate: ` - -