You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.3 KiB
JavaScript
34 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const memoizee = require("memoizee");
|
|
|
|
const setTextStyles = require("./set-text-styles");
|
|
//const measureFont = memoizee(require("./measure-font"));
|
|
const measureFont = memoizee(require("measure-font"));
|
|
|
|
module.exports = function measureText(text, options) {
|
|
let offscreenCanvas = document.createElement("canvas");
|
|
let context = offscreenCanvas.getContext("2d");
|
|
|
|
setTextStyles(context, options);
|
|
|
|
let fontMeasurements = measureFont(options.fontFamily);
|
|
|
|
return Object.assign(context.measureText(text), {
|
|
/* FIXME: The following is a dirty hack until the Canvas v5 API is
|
|
* widely supported in major browsers. The 1.13 multiplier comes
|
|
* from the fabric.js source code - don't ask me why it's 1.13.
|
|
* See also:
|
|
* https://kangax.github.io/jstests/canvas-v5/
|
|
* https://lists.w3.org/Archives/Public/public-whatwg-archive/2012Mar/0269.htm
|
|
*/
|
|
//height: options.fontSize * 1.13,
|
|
height: options.fontSize * (fontMeasurements.descender - fontMeasurements.topBounding),
|
|
ascender: options.fontSize * fontMeasurements.ascender,
|
|
descender: options.fontSize * fontMeasurements.descender,
|
|
capHeight: options.fontSize * fontMeasurements.capHeight,
|
|
median: options.fontSize * fontMeasurements.median,
|
|
topBounding: options.fontSize * fontMeasurements.topBounding,
|
|
});
|
|
}
|