fontpath-util
Version:
Some simple utils for handling font sizes
68 lines (60 loc) • 2.7 kB
JavaScript
// module.exports.pointsToPixels = function(pointSize, resolution) {
// resolution = typeof resolution === "number" ? resolution : 72;
// return pointSize * resolution / 72;
// };
// module.exports.coordToPixel = function(coord, pixelSize, emSize) {
// emSize = typeof emSize === "number" ? emSize : 2048;
// return coord * pixelSize / emSize;
// };
/**
* Converts a pt size to px size, namely useful for matching
* size with CSS styles. If no DPI is specified, 96 is assumed
* (as it leads to correct rendering in all browsers).
*
* @param {Number} fontSize the desired font size in points
* @param {Number} dpi the expected DPI, generally 96 for browsers
* @return {Number} the rounded pixel font size
*/
module.exports.pointToPixel = function(fontSize, dpi) {
dpi = dpi||dpi===0 ? dpi : 96;
fontSize = fontSize * dpi / 72;
return Math.round(fontSize);
};
/**
* For the given font and (pixel) font size, this method returns the
* scale that will need to be applied to EM units (i.e. font paths)
* to have the font render at the expected size (i.e. to match the browser).
*
* If no font size is specified, we will use the default font size (which is in points)
* and convert it to pixels.
*
* @param {Font} font a font object from the fontpath tool
* @param {Number} fontSize the desired font size, defaults to the font's default size
* @return {Number} returns the scale for this font size
*/
module.exports.getPxScale = function(font, fontSize) {
if (font.bitmap)
return 1.0;
//If no fontSize is specified, it will just fall back to using the font's own size with 96 DPI.
fontSize = typeof fontSize === "number" ? fontSize : this.pointToPixel(font.size);
//Takes in a font size in PIXELS and gives us the expected scaling factor
var sz = font.units_per_EM/64;
sz = (sz/font.size * fontSize);
return ((font.resolution * 1/72 * sz) / font.units_per_EM);
};
/**
* For the given font and (point) font size, this method returns the
* scale that will need to be applied to EM units (i.e. font paths)
* to have the font render at the expected size (i.e. to match the browser).
*
* If no font size is specified, we will use the default font size.
*
* @param {Font} font a font object from the fontpath tool
* @param {Number} fontSize the desired font size, defaults to the font's default size
* @return {Number} the scale for this font size
*/
module.exports.getPtScale = function(font, fontSize) {
fontSize = typeof fontSize === "number" ? fontSize : font.size;
fontSize = this.pointToPixel(fontSize);
return this.getPxScale(font, fontSize);
};