Skip to content
This repository was archived by the owner on Feb 6, 2022. It is now read-only.

Commit 1ac4fb2

Browse files
author
Arnavion
committed
Handle negative positions in SVG drawings.
1 parent 1aef4ef commit 1ac4fb2

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

src/renderers/web/drawing-styles.ts

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,38 +54,69 @@ export class DrawingStyles {
5454
* @return {!SVGSVGElement}
5555
*/
5656
toSVG(drawingInstructions: parts.DrawingInstructions, fillColor: parts.Color): SVGSVGElement {
57+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
58+
svg.setAttribute("version", "1.1");
59+
60+
if (drawingInstructions.instructions.length === 0) {
61+
return svg;
62+
}
63+
5764
const scaleFactor = Math.pow(2, this._scale - 1);
5865
const scaleX = this._outputScaleX / scaleFactor;
5966
const scaleY = this._outputScaleY / scaleFactor;
6067

6168
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
6269
let pathString = "";
6370

64-
let bboxWidth = 0;
65-
let bboxHeight = 0;
71+
let bboxMinX = Infinity;
72+
let bboxMaxX = -Infinity;
73+
let bboxMinY = Infinity;
74+
let bboxMaxY = -Infinity;
6675

6776
for (const instruction of drawingInstructions.instructions) {
6877
if (instruction instanceof parts.drawing.MoveInstruction) {
6978
pathString += ` M ${ instruction.x.toFixed(3) } ${ (instruction.y + this._baselineOffset).toFixed(3) }`;
70-
bboxWidth = Math.max(bboxWidth, instruction.x);
71-
bboxHeight = Math.max(bboxHeight, instruction.y + this._baselineOffset);
79+
80+
bboxMinX = Math.min(bboxMinX, instruction.x);
81+
bboxMaxX = Math.max(bboxMaxX, instruction.x);
82+
bboxMinY = Math.min(bboxMinY, instruction.y + this._baselineOffset);
83+
bboxMaxY = Math.max(bboxMaxY, instruction.y + this._baselineOffset);
7284
}
7385
else if (instruction instanceof parts.drawing.LineInstruction) {
7486
pathString += ` L ${ instruction.x.toFixed(3) } ${ (instruction.y + this._baselineOffset).toFixed(3) }`;
75-
bboxWidth = Math.max(bboxWidth, instruction.x);
76-
bboxHeight = Math.max(bboxHeight, instruction.y + this._baselineOffset);
87+
88+
bboxMinX = Math.min(bboxMinX, instruction.x);
89+
bboxMaxX = Math.max(bboxMaxX, instruction.x);
90+
bboxMinY = Math.min(bboxMinY, instruction.y + this._baselineOffset);
91+
bboxMaxY = Math.max(bboxMaxY, instruction.y + this._baselineOffset);
7792
}
7893
else if (instruction instanceof parts.drawing.CubicBezierCurveInstruction) {
7994
pathString += ` C ${ instruction.x1.toFixed(3) } ${ (instruction.y1 + this._baselineOffset).toFixed(3) } ${ instruction.x2.toFixed(3) } ${ (instruction.y2 + this._baselineOffset).toFixed(3) } ${ instruction.x3.toFixed(3) } ${ (instruction.y3 + this._baselineOffset).toFixed(3) }`;
80-
bboxWidth = Math.max(bboxWidth, instruction.x1, instruction.x2, instruction.x3);
81-
bboxHeight = Math.max(bboxHeight, instruction.y1 + this._baselineOffset, instruction.y2 + this._baselineOffset, instruction.y3 + this._baselineOffset);
95+
96+
bboxMinX = Math.min(bboxMinX, instruction.x1, instruction.x2, instruction.x3);
97+
bboxMaxX = Math.max(bboxMaxX, instruction.x1, instruction.x2, instruction.x3);
98+
bboxMinY = Math.min(bboxMinY, instruction.y1 + this._baselineOffset, instruction.y2 + this._baselineOffset, instruction.y3 + this._baselineOffset);
99+
bboxMaxY = Math.max(bboxMaxY, instruction.y1 + this._baselineOffset, instruction.y2 + this._baselineOffset, instruction.y3 + this._baselineOffset);
82100
}
83101
}
84102

85-
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
86-
svg.setAttribute("version", "1.1");
87-
svg.width.baseVal.valueAsString = `${ (bboxWidth * scaleX).toFixed(3) }px`;
88-
svg.height.baseVal.valueAsString = `${ (bboxHeight * scaleY).toFixed(3) }px`;
103+
bboxMinX *= scaleX;
104+
bboxMaxX *= scaleX;
105+
bboxMinY *= scaleY;
106+
bboxMaxY *= scaleY;
107+
108+
const bboxWidth = bboxMaxX - bboxMinX;
109+
const bboxHeight = bboxMaxY - bboxMinY;
110+
111+
svg.width.baseVal.valueAsString = `${ bboxWidth.toFixed(3) }px`;
112+
svg.height.baseVal.valueAsString = `${ bboxHeight.toFixed(3) }px`;
113+
svg.viewBox.baseVal.x = bboxMinX;
114+
svg.viewBox.baseVal.y = bboxMinY;
115+
svg.viewBox.baseVal.width = bboxWidth;
116+
svg.viewBox.baseVal.height = bboxHeight;
117+
svg.style.position = "relative";
118+
svg.style.left = `${ bboxMinX.toFixed(3) }px`;
119+
svg.style.top = `${ bboxMinY.toFixed(3) }px`;
89120

90121
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
91122
svg.appendChild(g);

0 commit comments

Comments
 (0)