'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, }); }