Do not bypass measureTextWidth

This commit is contained in:
Andreas Hocevar
2019-11-05 21:08:48 +01:00
parent 77bc6897dd
commit 48ec398037
+5 -17
View File
@@ -216,15 +216,12 @@ export const checkFont = (function() {
* @return {boolean} Font with style and weight is available * @return {boolean} Font with style and weight is available
*/ */
function isAvailable(fontStyle, fontWeight, fontFamily) { function isAvailable(fontStyle, fontWeight, fontFamily) {
const context = getMeasureContext();
let available = true; let available = true;
for (let i = 0; i < len; ++i) { for (let i = 0; i < len; ++i) {
const referenceFont = referenceFonts[i]; const referenceFont = referenceFonts[i];
context.font = fontStyle + ' ' + fontWeight + ' ' + size + referenceFont; referenceWidth = measureTextWidth(fontStyle + ' ' + fontWeight + ' ' + size + referenceFont, text);
referenceWidth = context.measureText(text).width;
if (fontFamily != referenceFont) { if (fontFamily != referenceFont) {
context.font = fontStyle + ' ' + fontWeight + ' ' + size + fontFamily + ',' + referenceFont; const width = measureTextWidth(fontStyle + ' ' + fontWeight + ' ' + size + fontFamily + ',' + referenceFont, text);
const width = context.measureText(text).width;
// If width and referenceWidth are the same, then the fallback was used // If width and referenceWidth are the same, then the fallback was used
// instead of the font we wanted, so the font is not available. // instead of the font we wanted, so the font is not available.
available = available && width != referenceWidth; available = available && width != referenceWidth;
@@ -284,17 +281,6 @@ export const checkFont = (function() {
})(); })();
/**
* @return {CanvasRenderingContext2D} Measure context.
*/
function getMeasureContext() {
if (!measureContext) {
measureContext = createCanvasContext2D(1, 1);
}
return measureContext;
}
/** /**
* @param {string} font Font to use for measuring. * @param {string} font Font to use for measuring.
* @return {import("../size.js").Size} Measurement. * @return {import("../size.js").Size} Measurement.
@@ -333,7 +319,9 @@ export const measureTextHeight = (function() {
* @return {number} Width. * @return {number} Width.
*/ */
export function measureTextWidth(font, text) { export function measureTextWidth(font, text) {
const measureContext = getMeasureContext(); if (!measureContext) {
measureContext = createCanvasContext2D(1, 1);
}
if (font != measureFont) { if (font != measureFont) {
measureContext.font = font; measureContext.font = font;
measureFont = measureContext.font; measureFont = measureContext.font;