Measure height only once per font

This commit is contained in:
Andreas Hocevar
2017-10-31 08:48:02 +01:00
parent 4a73754b93
commit b29e74d1ab

View File

@@ -135,7 +135,10 @@ ol.render.canvas.TextReplay.labelCache_ = new ol.structs.LRUCache();
*/
ol.render.canvas.TextReplay.measureTextHeight = (function() {
var span;
return function(font, lines, widths) {
var heights = {};
return function(font) {
var height = heights[font];
if (height == undefined) {
if (!span) {
span = document.createElement('span');
span.textContent = 'M';
@@ -145,8 +148,9 @@ ol.render.canvas.TextReplay.measureTextHeight = (function() {
}
span.style.font = font;
document.body.appendChild(span);
var height = span.offsetHeight;
height = heights[font] = span.offsetHeight;
document.body.removeChild(span);
}
return height;
};
})();