[Pkg-javascript-commits] [pdf.js] 102/109: Fix text spacing with vertical fonts (#6387)

David Prévot taffit at moszumanska.debian.org
Fri Sep 25 03:04:23 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository pdf.js.

commit 256482750388f6360354318f78d16ceffaa47f0e
Author: Fabian Lange <lange.fabian at gmail.com>
Date:   Fri Aug 28 13:42:01 2015 +0200

    Fix text spacing with vertical fonts (#6387)
    
    According to the PDF spec 5.3.2, a positive value means in horizontal,
    that the next glyph is further to the left (so narrower), and in
    vertical that it is further down (so wider).
    This change fixes the way PDF.js has interpreted the value.
---
 src/core/evaluator.js   |  30 ++++++++++++++++++++----------
 src/display/canvas.js   |   8 +++++---
 test/pdfs/.gitignore    |   1 +
 test/pdfs/issue6387.pdf | Bin 0 -> 66117 bytes
 test/test_manifest.json |  13 +++++++++++++
 5 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index 9eb9c63..3e0a98d 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -731,11 +731,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
               var arr = args[0];
               var combinedGlyphs = [];
               var arrLength = arr.length;
+              var state = stateManager.state;
               for (i = 0; i < arrLength; ++i) {
                 var arrItem = arr[i];
                 if (isString(arrItem)) {
                   Array.prototype.push.apply(combinedGlyphs,
-                    self.handleText(arrItem, stateManager.state));
+                    self.handleText(arrItem, state));
                 } else if (isNum(arrItem)) {
                   combinedGlyphs.push(arrItem);
                 }
@@ -1131,17 +1132,26 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
                 if (typeof items[j] === 'string') {
                   buildTextGeometry(items[j], textChunk);
                 } else {
-                  var val = items[j] / 1000;
-                  if (!textState.font.vertical) {
-                    offset = -val * textState.fontSize * textState.textHScale *
-                      textState.textMatrix[0];
-                    textState.translateTextMatrix(offset, 0);
-                    textChunk.width += offset;
-                  } else {
-                    offset = -val * textState.fontSize *
-                      textState.textMatrix[3];
+                  // PDF Specification 5.3.2 states:
+                  // The number is expressed in thousandths of a unit of text
+                  // space.
+                  // This amount is subtracted from the current horizontal or
+                  // vertical coordinate, depending on the writing mode.
+                  // In the default coordinate system, a positive adjustment
+                  // has the effect of moving the next glyph painted either to
+                  // the left or down by the given amount.
+                  var val = items[j] * textState.fontSize / 1000;
+                  if (textState.font.vertical) {
+                    offset = val * textState.textMatrix[3];
                     textState.translateTextMatrix(0, offset);
+                    // Value needs to be added to height to paint down.
                     textChunk.height += offset;
+                  } else {
+                    offset = val * textState.textHScale *
+                                   textState.textMatrix[0];
+                    textState.translateTextMatrix(offset, 0);
+                    // Value needs to be subtracted from width to paint left.
+                    textChunk.width -= offset;
                   }
                   if (items[j] < 0 && textState.font.spaceWidth > 0) {
                     var fakeSpaces = -items[j] / textState.font.spaceWidth;
diff --git a/src/display/canvas.js b/src/display/canvas.js
index 0330687..4c6754a 100644
--- a/src/display/canvas.js
+++ b/src/display/canvas.js
@@ -1360,6 +1360,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
       var textHScale = current.textHScale * fontDirection;
       var glyphsLength = glyphs.length;
       var vertical = font.vertical;
+      var spacingDir = vertical ? 1 : -1;
       var defaultVMetrics = font.defaultVMetrics;
       var widthAdvanceScale = fontSize * current.fontMatrix[0];
 
@@ -1406,7 +1407,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
           x += fontDirection * wordSpacing;
           continue;
         } else if (isNum(glyph)) {
-          x += -glyph * fontSize * 0.001;
+          x += spacingDir * glyph * fontSize / 1000;
           continue;
         }
 
@@ -1476,6 +1477,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
       var font = current.font;
       var fontSize = current.fontSize;
       var fontDirection = current.fontDirection;
+      var spacingDir = font.vertical ? 1 : -1;
       var charSpacing = current.charSpacing;
       var wordSpacing = current.wordSpacing;
       var textHScale = current.textHScale * fontDirection;
@@ -1483,7 +1485,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
       var glyphsLength = glyphs.length;
       var isTextInvisible =
         current.textRenderingMode === TextRenderingMode.INVISIBLE;
-      var i, glyph, width;
+      var i, glyph, width, spacingLength;
 
       if (isTextInvisible || fontSize === 0) {
         return;
@@ -1504,7 +1506,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
           current.x += wordSpacing * textHScale;
           continue;
         } else if (isNum(glyph)) {
-          var spacingLength = -glyph * 0.001 * fontSize;
+          spacingLength = spacingDir * glyph * fontSize / 1000;
           this.ctx.translate(spacingLength, 0);
           current.x += spacingLength * textHScale;
           continue;
diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore
index 5e4ab38..1e3ccaf 100644
--- a/test/pdfs/.gitignore
+++ b/test/pdfs/.gitignore
@@ -100,6 +100,7 @@
 !issue5747.pdf
 !issue6099.pdf
 !issue6336.pdf
+!issue6387.pdf
 !issue6410.pdf
 !gradientfill.pdf
 !bug903856.pdf
diff --git a/test/pdfs/issue6387.pdf b/test/pdfs/issue6387.pdf
new file mode 100644
index 0000000..daf0561
Binary files /dev/null and b/test/pdfs/issue6387.pdf differ
diff --git a/test/test_manifest.json b/test/test_manifest.json
index 61ed242..0f05639 100644
--- a/test/test_manifest.json
+++ b/test/test_manifest.json
@@ -1574,6 +1574,19 @@
        "lastPage": 1,
        "type": "eq"
     },
+    {  "id": "issue6387-canvas",
+       "file": "pdfs/issue6387.pdf",
+       "md5": "08c39ac6d0aab1596e6e59793eaf3ee4",
+       "rounds": 1,
+       "type": "eq"
+    },
+    {  "id": "issue6387-text",
+       "file": "pdfs/issue6387.pdf",
+       "md5": "08c39ac6d0aab1596e6e59793eaf3ee4",
+       "rounds": 1,
+       "type": "text",
+       "about": "Note that the text layer seems to be off to the right."
+    },
     {  "id": "issue4890",
        "file": "pdfs/issue4890.pdf",
        "md5": "1666feb4cd26318c2bdbea6a175dce87",

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git



More information about the Pkg-javascript-commits mailing list