Addressing comments by @vingtetun

This commit is contained in:
Artur Adib 2011-12-01 14:11:17 -05:00
parent 5e1d78ef20
commit 705356fa41

View File

@ -275,37 +275,38 @@ var CanvasGraphics = (function canvasGraphics() {
}, },
endDrawing: function canvasGraphicsEndDrawing() { endDrawing: function canvasGraphicsEndDrawing() {
var self = this;
this.ctx.restore(); this.ctx.restore();
var textLayer = this.textLayer; var textLayer = this.textLayer;
if (textLayer) { if (!textLayer)
var renderTextLayer = function canvasRenderTextLayer() { return;
var textDivs = self.textDivs;
for (var i = 0, length = textDivs.length; i < length; ++i) { var self = this;
if (textDivs[i].dataset.textLength > 1) { // avoid div by zero var renderTextLayer = function canvasRenderTextLayer() {
textLayer.appendChild(textDivs[i]); var textDivs = self.textDivs;
// Adjust div width (via letterSpacing) to match canvas text for (var i = 0, length = textDivs.length; i < length; ++i) {
// Due to the .offsetWidth calls, this is slow if (textDivs[i].dataset.textLength > 1) { // avoid div by zero
textDivs[i].style.letterSpacing = textLayer.appendChild(textDivs[i]);
((textDivs[i].dataset.canvasWidth - textDivs[i].offsetWidth) / // Adjust div width (via letterSpacing) to match canvas text
(textDivs[i].dataset.textLength - 1)) + 'px'; // Due to the .offsetWidth calls, this is slow
} textDivs[i].style.letterSpacing =
((textDivs[i].dataset.canvasWidth - textDivs[i].offsetWidth) /
(textDivs[i].dataset.textLength - 1)) + 'px';
} }
} }
var textLayerQueue = self.textLayerQueue;
textLayerQueue.push(renderTextLayer);
// Lazy textLayer rendering (to prevent UI hangs)
// Only render queue if activity has stopped, where "no activity" ==
// "no beginDrawing() calls in the last N ms"
self.textLayerTimer = setTimeout(function renderTextLayerQueue() {
// Render most recent (==most relevant) layers first
for (var i = textLayerQueue.length - 1; i >= 0; i--) {
textLayerQueue.pop().call();
}
}, 500);
} }
var textLayerQueue = this.textLayerQueue;
textLayerQueue.push(renderTextLayer);
// Lazy textLayer rendering (to prevent UI hangs)
// Only render queue if activity has stopped, where "no activity" ==
// "no beginDrawing() calls in the last N ms"
this.textLayerTimer = setTimeout(function renderTextLayerQueue() {
// Render most recent (==most relevant) layers first
for (var i = textLayerQueue.length - 1; i >= 0; i--) {
textLayerQueue.pop().call();
}
}, 500);
}, },
// Graphics state // Graphics state
@ -577,28 +578,32 @@ var CanvasGraphics = (function canvasGraphics() {
ctx.scale(1 / textHScale, 1); ctx.scale(1 / textHScale, 1);
}, },
getTextGeometry: function canvasGetTextGeometry() { getTextGeometry: function canvasGetTextGeometry() {
var geom = {}; var geometry = {};
var ctx = this.ctx; var ctx = this.ctx;
var font = this.current.font; var font = this.current.font;
var ctxMatrix = ctx.mozCurrentTransform; var ctxMatrix = ctx.mozCurrentTransform;
if (ctxMatrix) { if (ctxMatrix) {
var bl = Util.applyTransform([0, 0], ctxMatrix); var bl = Util.applyTransform([0, 0], ctxMatrix);
var tr = Util.applyTransform([1, 1], ctxMatrix); var tr = Util.applyTransform([1, 1], ctxMatrix);
geom.x = bl[0]; geometry.x = bl[0];
geom.y = bl[1]; geometry.y = bl[1];
geom.hScale = tr[0] - bl[0]; geometry.hScale = tr[0] - bl[0];
geom.vScale = tr[1] - bl[1]; geometry.vScale = tr[1] - bl[1];
} }
var spaceGlyph = font.charsToGlyphs(' '); var spaceGlyph = font.charsToGlyphs(' ');
// Hack (sometimes space is not encoded) // Hack (sometimes space is not encoded)
if (spaceGlyph.length === 0 || spaceGlyph[0].width === 0) if (spaceGlyph.length === 0 || spaceGlyph[0].width === 0)
spaceGlyph = font.charsToGlyphs('i'); spaceGlyph = font.charsToGlyphs('i');
// Fallback // Fallback
if (spaceGlyph.length === 0 || spaceGlyph[0].width === 0) if (spaceGlyph.length === 0 || spaceGlyph[0].width === 0)
spaceGlyph = [{width: 0}]; spaceGlyph = [{width: 0}];
geom.spaceWidth = spaceGlyph[0].width;
return geom; geometry.spaceWidth = spaceGlyph[0].width;
return geometry;
}, },
pushTextDivs: function canvasGraphicsPushTextDivs(text) { pushTextDivs: function canvasGraphicsPushTextDivs(text) {
var div = document.createElement('div'); var div = document.createElement('div');
var fontSize = this.current.fontSize; var fontSize = this.current.fontSize;