getBoundingBox method
dynamic
getBoundingBox()
Calculate the bounding box of the path. @returns {opentype.BoundingBox}
Implementation
getBoundingBox() {
var box = new BoundingBox();
var startX = 0;
var startY = 0;
var prevX = 0;
var prevY = 0;
for (var i = 0; i < this.commands.length; i++) {
var cmd = this.commands[i];
switch (cmd["type"]) {
case 'M':
box.addPoint(cmd["x"], cmd["y"]);
startX = prevX = cmd["x"];
startY = prevY = cmd["y"];
break;
case 'L':
box.addPoint(cmd["x"], cmd["y"]);
prevX = cmd["x"];
prevY = cmd["y"];
break;
case 'Q':
box.addQuad(prevX, prevY, cmd["x1"], cmd["y1"], cmd["x"], cmd["y"]);
prevX = cmd["x"];
prevY = cmd["y"];
break;
case 'C':
box.addBezier(prevX, prevY, cmd["x1"], cmd["y1"], cmd["x2"], cmd["y2"], cmd["x"], cmd["y"]);
prevX = cmd["x"];
prevY = cmd["y"];
break;
case 'Z':
prevX = startX;
prevY = startY;
break;
default:
throw('Unexpected path command ' + cmd["type"]);
}
}
if (box.isEmpty()) {
box.addPoint(0, 0);
}
return box;
}