From 355efc46d2298f48dccfe729ef65c5a17cff286a Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 25 Feb 2013 21:00:17 -0600 Subject: [PATCH] Fix text selection for oddly-spaced TJ commands This change will discard trailing space adjustments in TJ/showSpacedText() for the purposes of calculating the text width for text selection. In pathological cases, a PDF may write one character and then move the text matrix back to the beginning of the character within one TJ invocation. This would add up to a canvasWidth of 0, so the text selection
would be scaled to zero pixels wide, even though the character was drawn normally. With this change, canvasWidth will not include any adjustments made after the last character was written. Normal use of TJ will result in the same text selection behavior, whereas pathological use of TJ will result in the text selection layer matching the actual width of the characters displayed. For an example of such pathological behavior, see http://www.tycovalves-usa.com/ld/CROMC-0297-US.pdf#page=48 --- src/canvas.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index 0765049a9..f077d5fdd 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -1053,6 +1053,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { var canvasWidth = 0.0; var textSelection = textLayer ? true : false; var vertical = font.vertical; + var spacingAccumulator = 0; if (textSelection) { ctx.save(); @@ -1072,12 +1073,14 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { } if (textSelection) - canvasWidth += spacingLength; + spacingAccumulator += spacingLength; } else if (isString(e)) { var shownCanvasWidth = this.showText(e, true); - if (textSelection) - canvasWidth += shownCanvasWidth; + if (textSelection) { + canvasWidth += spacingAccumulator + shownCanvasWidth; + spacingAccumulator = 0; + } } else { error('TJ array element ' + e + ' is not string or num'); }