[Pkg-javascript-commits] [pdf.js] 200/246: Fixes image and font embedding

David Prévot taffit at moszumanska.debian.org
Sun Sep 7 15:36:40 UTC 2014


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

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

commit de23d3791e53e651ab9c1a5fd79cbc85cce934cc
Author: Yury Delendik <ydelendik at mozilla.com>
Date:   Thu Aug 14 15:11:27 2014 -0500

    Fixes image and font embedding
---
 examples/node/domstubs.js |  2 +-
 examples/node/pdf2svg.js  |  1 +
 src/display/svg.js        | 27 +++++++++++++++++++++++++--
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/examples/node/domstubs.js b/examples/node/domstubs.js
index dafc758..b0e422b 100644
--- a/examples/node/domstubs.js
+++ b/examples/node/domstubs.js
@@ -99,7 +99,7 @@ DOMElement.prototype = {
       attrList.push(i + '="' + xmlEncode(this.attributes[i]) + '"');
     }
 
-    if (this.nodeName === 'svg:tspan') {
+    if (this.nodeName === 'svg:tspan' || this.nodeName === 'svg:style') {
       var encText = xmlEncode(this.textContent);
       return '<' + this.nodeName + ' ' + attrList.join(' ') + '>' +
              encText + '</' + this.nodeName + '>';
diff --git a/examples/node/pdf2svg.js b/examples/node/pdf2svg.js
index 4827149..f5979ca 100644
--- a/examples/node/pdf2svg.js
+++ b/examples/node/pdf2svg.js
@@ -65,6 +65,7 @@ PDFJS.getDocument(data).then(function (doc) {
       
       return page.getOperatorList().then(function (opList) {
         var svgGfx = new PDFJS.SVGGraphics(page.commonObjs, page.objs);
+        svgGfx.embedFonts = true;
         return svgGfx.getSVG(opList, viewport).then(function (svg) {
           var svgDump = svg.toString();
           writeToFile(svgDump, pageNum);
diff --git a/src/display/svg.js b/src/display/svg.js
index d84b6a1..09ce2d9 100644
--- a/src/display/svg.js
+++ b/src/display/svg.js
@@ -361,6 +361,10 @@ var SVGGraphics = (function SVGGraphicsClosure() {
     this.commonObjs = commonObjs;
     this.objs = objs;
     this.pendingEOFill = false;
+
+    this.embedFonts = false;
+    this.embeddedFonts = {};
+    this.cssStyle = null;
   }
 
   var NS = 'http://www.w3.org/2000/svg';
@@ -728,12 +732,31 @@ var SVGGraphics = (function SVGGraphicsClosure() {
       this.moveText(x, y);
     },
 
+    addFontStyle: function SVGGraphics_addFontStyle(fontObj) {
+      if (!this.cssStyle) {
+        this.cssStyle = document.createElementNS(NS, 'svg:style');
+        this.cssStyle.setAttributeNS(null, 'type', 'text/css');
+        this.defs.appendChild(this.cssStyle);
+      }
+
+      var url = PDFJS.createObjectURL(fontObj.data, fontObj.mimetype);
+      this.cssStyle.textContent +=
+        '@font-face { font-family: "' + fontObj.loadedName + '";' +
+        ' src: url(' + url + '); }\n';
+    },
+
     setFont: function SVGGraphics_setFont(details) {
       var current = this.current;
       var fontObj = this.commonObjs.get(details[0]);
       var size = details[1];
       this.current.font = fontObj;
 
+      if (this.embedFonts && fontObj.data &&
+          !this.embeddedFonts[fontObj.loadedName]) {
+        this.addFontStyle(fontObj);
+        this.embeddedFonts[fontObj.loadedName] = fontObj;
+      }
+
       current.fontMatrix = (fontObj.fontMatrix ?
                             fontObj.fontMatrix : FONT_IDENTITY_MATRIX);
 
@@ -1028,7 +1051,7 @@ var SVGGraphics = (function SVGGraphicsClosure() {
       var current = this.current;
       var imgObj = this.objs.get(objId);
       var imgEl = document.createElementNS(NS, 'svg:image');
-      imgEl.setAttributeNS(XLINK_NS, 'href', imgObj.src);
+      imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgObj.src);
       imgEl.setAttributeNS(null, 'width', imgObj.width + 'px');
       imgEl.setAttributeNS(null, 'height', imgObj.height + 'px');
       imgEl.setAttributeNS(null, 'x', '0');
@@ -1069,7 +1092,7 @@ var SVGGraphics = (function SVGGraphicsClosure() {
       current.element = cliprect;
       this.clip('nonzero');
       var imgEl = document.createElementNS(NS, 'svg:image');
-      imgEl.setAttributeNS(XLINK_NS, 'href', imgSrc);
+      imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgSrc);
       imgEl.setAttributeNS(null, 'x', '0');
       imgEl.setAttributeNS(null, 'y', pf(-height));
       imgEl.setAttributeNS(null, 'width', pf(width) + 'px');

-- 
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