Reset text measurements when available fonts change
This commit is contained in:
+74
-11
@@ -3,6 +3,7 @@ goog.provide('ol.render.canvas');
|
|||||||
|
|
||||||
goog.require('ol.css');
|
goog.require('ol.css');
|
||||||
goog.require('ol.dom');
|
goog.require('ol.dom');
|
||||||
|
goog.require('ol.obj');
|
||||||
goog.require('ol.structs.LRUCache');
|
goog.require('ol.structs.LRUCache');
|
||||||
goog.require('ol.transform');
|
goog.require('ol.transform');
|
||||||
|
|
||||||
@@ -98,11 +99,23 @@ ol.render.canvas.labelCache = new ol.structs.LRUCache();
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {!Object.<string, (number)>}
|
* @type {!Object.<string, number>}
|
||||||
*/
|
*/
|
||||||
ol.render.canvas.checkedFonts_ = {};
|
ol.render.canvas.checkedFonts_ = {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {CanvasRenderingContext2D}
|
||||||
|
*/
|
||||||
|
ol.render.canvas.measureContext_ = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {!Object.<string, number>}
|
||||||
|
*/
|
||||||
|
ol.render.canvas.textHeights_ = {};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears the label cache when a font becomes available.
|
* Clears the label cache when a font becomes available.
|
||||||
* @param {string} fontSpec CSS font spec.
|
* @param {string} fontSpec CSS font spec.
|
||||||
@@ -112,14 +125,12 @@ ol.render.canvas.checkFont = (function() {
|
|||||||
var labelCache = ol.render.canvas.labelCache;
|
var labelCache = ol.render.canvas.labelCache;
|
||||||
var font = '32px monospace';
|
var font = '32px monospace';
|
||||||
var text = 'wmytzilWMYTZIL@#/&?$%10';
|
var text = 'wmytzilWMYTZIL@#/&?$%10';
|
||||||
var context, interval, referenceWidth;
|
var interval, referenceWidth;
|
||||||
|
|
||||||
function isAvailable(fontFamily) {
|
function isAvailable(fontFamily) {
|
||||||
if (!context) {
|
var context = ol.render.canvas.getMeasureContext();
|
||||||
context = ol.dom.createCanvasContext2D(1, 1);
|
context.font = font;
|
||||||
context.font = font;
|
referenceWidth = context.measureText(text).width;
|
||||||
referenceWidth = context.measureText(text).width;
|
|
||||||
}
|
|
||||||
var available = true;
|
var available = true;
|
||||||
if (fontFamily != 'monospace') {
|
if (fontFamily != 'monospace') {
|
||||||
context.font = '32px ' + fontFamily + ',monospace';
|
context.font = '32px ' + fontFamily + ',monospace';
|
||||||
@@ -128,10 +139,6 @@ ol.render.canvas.checkFont = (function() {
|
|||||||
// fallback was used instead of the font we wanted, so the font is not
|
// fallback was used instead of the font we wanted, so the font is not
|
||||||
// available.
|
// available.
|
||||||
available = width != referenceWidth;
|
available = width != referenceWidth;
|
||||||
// Setting the font back to a different one works around an issue in
|
|
||||||
// Safari where subsequent `context.font` assignments with the same font
|
|
||||||
// will not re-attempt to use a font that is currently loading.
|
|
||||||
context.font = font;
|
|
||||||
}
|
}
|
||||||
return available;
|
return available;
|
||||||
}
|
}
|
||||||
@@ -143,6 +150,9 @@ ol.render.canvas.checkFont = (function() {
|
|||||||
if (isAvailable(font)) {
|
if (isAvailable(font)) {
|
||||||
checked[font] = 60;
|
checked[font] = 60;
|
||||||
labelCache.clear();
|
labelCache.clear();
|
||||||
|
ol.obj.clear(ol.render.canvas.textHeights_);
|
||||||
|
// Make sure that loaded fonts are picked up by Safari
|
||||||
|
ol.render.canvas.measureContext_ = null;
|
||||||
} else {
|
} else {
|
||||||
++checked[font];
|
++checked[font];
|
||||||
done = false;
|
done = false;
|
||||||
@@ -176,6 +186,59 @@ ol.render.canvas.checkFont = (function() {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {CanvasRenderingContext2D} Measure context.
|
||||||
|
*/
|
||||||
|
ol.render.canvas.getMeasureContext = function() {
|
||||||
|
var context = ol.render.canvas.measureContext_;
|
||||||
|
if (!context) {
|
||||||
|
context = ol.render.canvas.measureContext_ = ol.dom.createCanvasContext2D(1, 1);
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} font Font to use for measuring.
|
||||||
|
* @return {ol.Size} Measurement.
|
||||||
|
*/
|
||||||
|
ol.render.canvas.measureTextHeight = (function() {
|
||||||
|
var span;
|
||||||
|
var heights = ol.render.canvas.textHeights_;
|
||||||
|
return function(font) {
|
||||||
|
var height = heights[font];
|
||||||
|
if (height == undefined) {
|
||||||
|
if (!span) {
|
||||||
|
span = document.createElement('span');
|
||||||
|
span.textContent = 'M';
|
||||||
|
span.style.margin = span.style.padding = '0 !important';
|
||||||
|
span.style.position = 'absolute !important';
|
||||||
|
span.style.left = '-99999px !important';
|
||||||
|
}
|
||||||
|
span.style.font = font;
|
||||||
|
document.body.appendChild(span);
|
||||||
|
height = heights[font] = span.offsetHeight;
|
||||||
|
document.body.removeChild(span);
|
||||||
|
}
|
||||||
|
return height;
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} font Font.
|
||||||
|
* @param {string} text Text.
|
||||||
|
* @return {number} Width.
|
||||||
|
*/
|
||||||
|
ol.render.canvas.measureTextWidth = function(font, text) {
|
||||||
|
var measureContext = ol.render.canvas.getMeasureContext();
|
||||||
|
if (font != measureContext.font) {
|
||||||
|
measureContext.font = font;
|
||||||
|
}
|
||||||
|
return measureContext.measureText(text).width;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {CanvasRenderingContext2D} context Context.
|
* @param {CanvasRenderingContext2D} context Context.
|
||||||
* @param {number} rotation Rotation.
|
* @param {number} rotation Rotation.
|
||||||
|
|||||||
@@ -118,53 +118,6 @@ ol.render.canvas.TextReplay = function(
|
|||||||
ol.inherits(ol.render.canvas.TextReplay, ol.render.canvas.Replay);
|
ol.inherits(ol.render.canvas.TextReplay, ol.render.canvas.Replay);
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} font Font to use for measuring.
|
|
||||||
* @return {ol.Size} Measurement.
|
|
||||||
*/
|
|
||||||
ol.render.canvas.TextReplay.measureTextHeight = (function() {
|
|
||||||
var span;
|
|
||||||
var heights = {};
|
|
||||||
return function(font) {
|
|
||||||
var height = heights[font];
|
|
||||||
if (height == undefined) {
|
|
||||||
if (!span) {
|
|
||||||
span = document.createElement('span');
|
|
||||||
span.textContent = 'M';
|
|
||||||
span.style.margin = span.style.padding = '0 !important';
|
|
||||||
span.style.position = 'absolute !important';
|
|
||||||
span.style.left = '-99999px !important';
|
|
||||||
}
|
|
||||||
span.style.font = font;
|
|
||||||
document.body.appendChild(span);
|
|
||||||
height = heights[font] = span.offsetHeight;
|
|
||||||
document.body.removeChild(span);
|
|
||||||
}
|
|
||||||
return height;
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} font Font.
|
|
||||||
* @param {string} text Text.
|
|
||||||
* @return {number} Width.
|
|
||||||
*/
|
|
||||||
ol.render.canvas.TextReplay.measureTextWidth = (function() {
|
|
||||||
var measureContext;
|
|
||||||
var currentFont;
|
|
||||||
return function(font, text) {
|
|
||||||
if (!measureContext) {
|
|
||||||
measureContext = ol.dom.createCanvasContext2D(1, 1);
|
|
||||||
}
|
|
||||||
if (font != currentFont) {
|
|
||||||
currentFont = measureContext.font = font;
|
|
||||||
}
|
|
||||||
return measureContext.measureText(text).width;
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} font Font to use for measuring.
|
* @param {string} font Font to use for measuring.
|
||||||
* @param {Array.<string>} lines Lines to measure.
|
* @param {Array.<string>} lines Lines to measure.
|
||||||
@@ -177,7 +130,7 @@ ol.render.canvas.TextReplay.measureTextWidths = function(font, lines, widths) {
|
|||||||
var width = 0;
|
var width = 0;
|
||||||
var currentWidth, i;
|
var currentWidth, i;
|
||||||
for (i = 0; i < numLines; ++i) {
|
for (i = 0; i < numLines; ++i) {
|
||||||
currentWidth = ol.render.canvas.TextReplay.measureTextWidth(font, lines[i]);
|
currentWidth = ol.render.canvas.measureTextWidth(font, lines[i]);
|
||||||
width = Math.max(width, currentWidth);
|
width = Math.max(width, currentWidth);
|
||||||
widths.push(currentWidth);
|
widths.push(currentWidth);
|
||||||
}
|
}
|
||||||
@@ -325,7 +278,7 @@ ol.render.canvas.TextReplay.prototype.getImage = function(text, fill, stroke) {
|
|||||||
var numLines = lines.length;
|
var numLines = lines.length;
|
||||||
var widths = [];
|
var widths = [];
|
||||||
var width = ol.render.canvas.TextReplay.measureTextWidths(textState.font, lines, widths);
|
var width = ol.render.canvas.TextReplay.measureTextWidths(textState.font, lines, widths);
|
||||||
var lineHeight = ol.render.canvas.TextReplay.measureTextHeight(textState.font);
|
var lineHeight = ol.render.canvas.measureTextHeight(textState.font);
|
||||||
var height = lineHeight * numLines;
|
var height = lineHeight * numLines;
|
||||||
var renderWidth = (width + strokeWidth);
|
var renderWidth = (width + strokeWidth);
|
||||||
var context = ol.dom.createCanvasContext2D(
|
var context = ol.dom.createCanvasContext2D(
|
||||||
@@ -433,7 +386,7 @@ ol.render.canvas.TextReplay.prototype.drawChars_ = function(begin, end, declutte
|
|||||||
function(text) {
|
function(text) {
|
||||||
var width = widths[text];
|
var width = widths[text];
|
||||||
if (!width) {
|
if (!width) {
|
||||||
width = widths[text] = ol.render.canvas.TextReplay.measureTextWidth(font, text);
|
width = widths[text] = ol.render.canvas.measureTextWidth(font, text);
|
||||||
}
|
}
|
||||||
return width * textScale * pixelRatio;
|
return width * textScale * pixelRatio;
|
||||||
},
|
},
|
||||||
@@ -445,7 +398,7 @@ ol.render.canvas.TextReplay.prototype.drawChars_ = function(begin, end, declutte
|
|||||||
function(text) {
|
function(text) {
|
||||||
var width = widths[text];
|
var width = widths[text];
|
||||||
if (!width) {
|
if (!width) {
|
||||||
width = widths[text] = ol.render.canvas.TextReplay.measureTextWidth(font, text);
|
width = widths[text] = ol.render.canvas.measureTextWidth(font, text);
|
||||||
}
|
}
|
||||||
return width * textScale;
|
return width * textScale;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user