opentype.js
Advanced tools
Comparing version 0.6.7 to 0.6.8
{ | ||
"name": "opentype.js", | ||
"version": "0.6.7", | ||
"version": "0.6.8", | ||
"main": "dist/opentype.js", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
{ | ||
"name": "opentype.js", | ||
"description": "OpenType font parser", | ||
"version": "0.6.7", | ||
"version": "0.6.8", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Frederik De Bleser", |
@@ -198,2 +198,6 @@ opentype.js | ||
##### `Glyph.getBoundingBox()` | ||
Calculate the minimum bounding box for the unscaled path of the given glyph. Returns an `opentype.BoundingBox` object that contains x1/y1/x2/y2. | ||
If the glyph has no points (e.g. a space character), all coordinates will be zero. | ||
##### `Glyph.draw(ctx, x, y, fontSize)` | ||
@@ -230,2 +234,6 @@ Draw the glyph on the given context. | ||
##### `Path.getBoundingBox()` | ||
Calculate the minimum bounding box for the given path. Returns an `opentype.BoundingBox` object that contains x1/y1/x2/y2. | ||
If the path is empty (e.g. a space character), all coordinates will be zero. | ||
##### `Path.toPathData(decimalPlaces)` | ||
@@ -232,0 +240,0 @@ Convert the Path to a string of path data instructions. |
@@ -0,1 +1,5 @@ | ||
0.6.8 (Jan 9, 2017) | ||
========================= | ||
* Add a `getBoundingBox` method to the `Path` and `Glyph` objects. | ||
0.6.7 (Jan 5, 2017) | ||
@@ -2,0 +6,0 @@ ========================= |
@@ -108,2 +108,10 @@ // The Glyph object | ||
/** | ||
* Calculate the minimum bounding box for this glyph. | ||
* @return {opentype.BoundingBox} | ||
*/ | ||
Glyph.prototype.getBoundingBox = function() { | ||
return this.path.getBoundingBox(); | ||
}; | ||
/** | ||
* Convert the glyph to a Path we can draw on a drawing context. | ||
@@ -110,0 +118,0 @@ * @param {number} [x=0] - Horizontal position of the beginning of the text. |
@@ -16,2 +16,3 @@ // opentype.js | ||
var parse = require('./parse'); | ||
var bbox = require('./bbox'); | ||
var path = require('./path'); | ||
@@ -387,4 +388,5 @@ var util = require('./util'); | ||
exports.Path = path.Path; | ||
exports.BoundingBox = bbox.BoundingBox; | ||
exports.parse = parseBuffer; | ||
exports.load = load; | ||
exports.loadSync = loadSync; |
@@ -5,2 +5,4 @@ // Geometric objects | ||
var bbox = require('./bbox'); | ||
/** | ||
@@ -132,3 +134,3 @@ * A bézier path containing a set of path commands similar to a SVG path. | ||
* Add the given path or list of commands to the commands of this path. | ||
* @param {Array} | ||
* @param {Array} pathOrCommands - another opentype.Path, an opentype.BoundingBox, or an array of commands. | ||
*/ | ||
@@ -138,2 +140,10 @@ Path.prototype.extend = function(pathOrCommands) { | ||
pathOrCommands = pathOrCommands.commands; | ||
} else if (pathOrCommands instanceof bbox.BoundingBox) { | ||
var box = pathOrCommands; | ||
this.moveTo(box.x1, box.y1); | ||
this.lineTo(box.x2, box.y1); | ||
this.lineTo(box.x2, box.y2); | ||
this.lineTo(box.x1, box.y2); | ||
this.close(); | ||
return; | ||
} | ||
@@ -145,2 +155,50 @@ | ||
/** | ||
* Calculate the bounding box of the path. | ||
* @returns {opentype.BoundingBox} | ||
*/ | ||
Path.prototype.getBoundingBox = function() { | ||
var box = new bbox.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 new Error('Unexpected path commmand ' + cmd.type); | ||
} | ||
} | ||
if (box.isEmpty()) { | ||
box.addPoint(0, 0); | ||
} | ||
return box; | ||
}; | ||
/** | ||
* Draw the path to a 2D context. | ||
@@ -253,2 +311,11 @@ * @param {CanvasRenderingContext2D} ctx - A 2D drawing context. | ||
Path.prototype.toDOMElement = function(decimalPlaces) { | ||
var temporaryPath = this.toPathData(decimalPlaces); | ||
var newPath = document.createElementNS('http://www.w3.org/2000/svg', 'path'); | ||
newPath.setAttribute('d', temporaryPath); | ||
return newPath; | ||
}; | ||
exports.Path = Path; |
@@ -38,2 +38,40 @@ /* jshint mocha: true */ | ||
}); | ||
describe('bounding box', function() { | ||
var trueTypeFont; | ||
var openTypeFont; | ||
before(function() { | ||
trueTypeFont = opentype.loadSync('./fonts/Roboto-Black.ttf'); | ||
openTypeFont = opentype.loadSync('./fonts/FiraSansMedium.woff'); | ||
}); | ||
it('calculates a box for a linear shape', function() { | ||
var glyph = trueTypeFont.charToGlyph('A'); | ||
var box = glyph.getBoundingBox(); | ||
assert.equal(box.x1, 5); | ||
assert.equal(box.y1, 0); | ||
assert.equal(box.x2, 1319); | ||
assert.equal(box.y2, 1456); | ||
}); | ||
it('calculates a box for a quadratic shape', function() { | ||
var glyph = trueTypeFont.charToGlyph('Q'); | ||
var box = glyph.getBoundingBox(); | ||
assert.equal(box.x1, 89); | ||
assert.equal(box.y1, -165); | ||
assert.equal(box.x2, 1432); | ||
assert.equal(box.y2, 1477); | ||
}); | ||
it('calculates a box for a bezier shape', function() { | ||
var glyph = openTypeFont.charToGlyph('Q'); | ||
var box = glyph.getBoundingBox(); | ||
assert.equal(box.x1, 62); | ||
assert.equal(box.y1, -103); | ||
assert.equal(box.x2, 688); | ||
assert.equal(box.y2, 701); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75
18732
269
916515