Don't compute the string for the TextLayer in the canvas backend anymore and change the syntax of appendText

This commit is contained in:
Julian Viereck 2012-09-19 22:17:01 +02:00
parent 049b19264c
commit a33ba145bf
2 changed files with 31 additions and 40 deletions

View File

@ -677,9 +677,10 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var textHScale2 = textHScale * fontMatrix[0]; var textHScale2 = textHScale * fontMatrix[0];
var glyphsLength = glyphs.length; var glyphsLength = glyphs.length;
var textLayer = this.textLayer; var textLayer = this.textLayer;
var text = {str: '', length: 0, canvasWidth: 0, geom: {}}; var geom;
var textSelection = textLayer && !skipTextSelection ? true : false; var textSelection = textLayer && !skipTextSelection ? true : false;
var textRenderingMode = current.textRenderingMode; var textRenderingMode = current.textRenderingMode;
var canvasWidth = 0.0;
// Type3 fonts - each glyph is a "mini-PDF" // Type3 fonts - each glyph is a "mini-PDF"
if (font.coded) { if (font.coded) {
@ -692,7 +693,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
if (textSelection) { if (textSelection) {
this.save(); this.save();
ctx.scale(1, -1); ctx.scale(1, -1);
text.geom = this.getTextGeometry(); geom = this.getTextGeometry();
this.restore(); this.restore();
} }
for (var i = 0; i < glyphsLength; ++i) { for (var i = 0; i < glyphsLength; ++i) {
@ -718,9 +719,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
ctx.translate(width, 0); ctx.translate(width, 0);
current.x += width * textHScale; current.x += width * textHScale;
text.str += glyph.unicode; canvasWidth += width;
text.length++;
text.canvasWidth += width;
} }
ctx.restore(); ctx.restore();
} else { } else {
@ -735,7 +734,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
lineWidth /= scale; lineWidth /= scale;
if (textSelection) if (textSelection)
text.geom = this.getTextGeometry(); geom = this.getTextGeometry();
if (fontSizeScale != 1.0) { if (fontSizeScale != 1.0) {
ctx.scale(fontSizeScale, fontSizeScale); ctx.scale(fontSizeScale, fontSizeScale);
@ -784,17 +783,19 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var glyphUnicode = glyph.unicode === ' ' ? '\u00A0' : glyph.unicode; var glyphUnicode = glyph.unicode === ' ' ? '\u00A0' : glyph.unicode;
if (glyphUnicode in NormalizedUnicodes) if (glyphUnicode in NormalizedUnicodes)
glyphUnicode = NormalizedUnicodes[glyphUnicode]; glyphUnicode = NormalizedUnicodes[glyphUnicode];
text.str += reverseIfRtl(glyphUnicode);
text.canvasWidth += charWidth; canvasWidth += charWidth;
} }
current.x += x * textHScale2; current.x += x * textHScale2;
ctx.restore(); ctx.restore();
} }
if (textSelection) if (textSelection) {
this.textLayer.appendText(text, font.fallbackName, fontSize); geom.canvasWidth = canvasWidth;
this.textLayer.appendText(font.fallbackName, fontSize, geom);
}`
return text; return canvasWidth;
}, },
showSpacedText: function CanvasGraphics_showSpacedText(arr) { showSpacedText: function CanvasGraphics_showSpacedText(arr) {
var ctx = this.ctx; var ctx = this.ctx;
@ -806,7 +807,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
textHScale *= (current.fontMatrix || IDENTITY_MATRIX)[0]; textHScale *= (current.fontMatrix || IDENTITY_MATRIX)[0];
var arrLength = arr.length; var arrLength = arr.length;
var textLayer = this.textLayer; var textLayer = this.textLayer;
var text = {str: '', length: 0, canvasWidth: 0, geom: {}}; var geom;
var canvasWidth = 0.0;
var textSelection = textLayer ? true : false; var textSelection = textLayer ? true : false;
if (textSelection) { if (textSelection) {
@ -819,7 +821,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
ctx.scale(textHScale, 1); ctx.scale(textHScale, 1);
} else } else
this.applyTextTransforms(); this.applyTextTransforms();
text.geom = this.getTextGeometry(); geom = this.getTextGeometry();
ctx.restore(); ctx.restore();
} }
@ -829,34 +831,22 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var spacingLength = -e * 0.001 * fontSize * textHScale; var spacingLength = -e * 0.001 * fontSize * textHScale;
current.x += spacingLength; current.x += spacingLength;
if (textSelection) { if (textSelection)
// Emulate precise spacing via HTML spaces canvasWidth += spacingLength;
text.canvasWidth += spacingLength;
if (e < 0 && text.geom.spaceWidth > 0) { // avoid div by zero
var numFakeSpaces = Math.round(-e / text.geom.spaceWidth);
if (numFakeSpaces > 0) {
text.str += '\u00A0';
}
}
}
} else if (isString(e)) { } else if (isString(e)) {
var shownText = this.showText(e, true); var shownCanvasWidth = this.showText(e, true);
if (textSelection) { if (textSelection)
if (shownText.str === ' ') { canvasWidth += shownCanvasWidth;
text.str += '\u00A0';
} else {
text.str += shownText.str;
}
text.canvasWidth += shownText.canvasWidth;
}
} else { } else {
error('TJ array element ' + e + ' is not string or num'); error('TJ array element ' + e + ' is not string or num');
} }
} }
if (textSelection) if (textSelection) {
this.textLayer.appendText(text, font.fallbackName, fontSize); geom.canvasWidth = canvasWidth;
this.textLayer.appendText(font.fallbackName, fontSize, geom);
}
}, },
nextLineShowText: function CanvasGraphics_nextLineShowText(text) { nextLineShowText: function CanvasGraphics_nextLineShowText(text) {
this.nextLine(); this.nextLine();

View File

@ -1902,18 +1902,19 @@ var TextLayerBuilder = function textLayerBuilder(textLayerDiv) {
} }
}; };
this.appendText = function textLayerBuilderAppendText(text, this.appendText = function textLayerBuilderAppendText(fontName, fontSize,
fontName, fontSize) { geom) {
var textDiv = document.createElement('div'); var textDiv = document.createElement('div');
// vScale and hScale already contain the scaling to pixel units // vScale and hScale already contain the scaling to pixel units
var fontHeight = fontSize * text.geom.vScale; var fontHeight = fontSize * geom.vScale;
textDiv.dataset.canvasWidth = text.canvasWidth * text.geom.hScale; textDiv.dataset.canvasWidth = geom.canvasWidth * geom.hScale;
textDiv.dataset.fontName = fontName;
textDiv.style.fontSize = fontHeight + 'px'; textDiv.style.fontSize = fontHeight + 'px';
textDiv.style.fontFamily = fontName; textDiv.style.fontFamily = fontName;
textDiv.style.left = text.geom.x + 'px'; textDiv.style.left = geom.x + 'px';
textDiv.style.top = (text.geom.y - fontHeight) + 'px'; textDiv.style.top = (geom.y - fontHeight) + 'px';
// The content of the div is set in the `setTextContent` function. // The content of the div is set in the `setTextContent` function.