Pass along the ability to measure and cache text width

This commit is contained in:
Tim Schaub
2018-11-19 15:34:48 -07:00
parent e058eb3164
commit 8b50c3c6cb
4 changed files with 44 additions and 35 deletions
+16
View File
@@ -324,6 +324,22 @@ export function measureTextWidth(font, text) {
}
/**
* Measure text width using a cache.
* @param {string} font The font.
* @param {string} text The text to measure.
* @param {Object<string, number>} cache A lookup of cached widths by text.
* @returns {number} The text width.
*/
export function measureAndCacheTextWidth(font, text, cache) {
if (text in cache) {
return cache[text];
}
const width = cache[text] = measureTextWidth(font, text);
return width;
}
/**
* @param {string} font Font to use for measuring.
* @param {Array<string>} lines Lines to measure.