From 00d120470566843c88f8a9dc70b0122c7104e19f Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Fri, 2 Dec 2011 16:52:31 -0600 Subject: [PATCH 1/2] Suppress invisible text (#799) --- src/canvas.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index 0913b582a..6e33475d7 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -23,6 +23,7 @@ var CanvasExtraState = (function canvasExtraState() { this.charSpacing = 0; this.wordSpacing = 0; this.textHScale = 1; + this.textRenderingMode = 0; // Color spaces this.fillColorSpace = new DeviceGrayCS(); this.fillColorSpaceObj = null; @@ -543,7 +544,9 @@ var CanvasGraphics = (function canvasGraphics() { this.ctx.font = rule; }, setTextRenderingMode: function canvasGraphicsSetTextRenderingMode(mode) { - TODO('text rendering mode: ' + mode); + if (mode != 0 && mode != 3) + TODO('unsupported text rendering mode: ' + mode); + this.current.textRenderingMode = mode; }, setTextRise: function canvasGraphicsSetTextRise(rise) { TODO('text rise: ' + rise); @@ -637,6 +640,7 @@ var CanvasGraphics = (function canvasGraphics() { var textLayer = this.textLayer; var text = {str: '', length: 0, canvasWidth: 0, geom: {}}; var textSelection = textLayer && !skipTextSelection ? true : false; + var textRenderingMode = current.textRenderingMode; if (textSelection) { ctx.save(); @@ -693,7 +697,15 @@ var CanvasGraphics = (function canvasGraphics() { var char = glyph.fontChar; var charWidth = glyph.width * fontSize * 0.001 + charSpacing; - ctx.fillText(char, width, 0); + + switch (textRenderingMode) { + default: // unsupported rendering mode + case 0: // fill + ctx.fillText(char, width, 0); + break; + case 3: // invisible + } + width += charWidth; text.str += glyph.unicode === ' ' ? ' ' : glyph.unicode; From 2719e8eadc007cdd5d6eb2b90a9db52a85741e57 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Fri, 2 Dec 2011 22:30:31 -0600 Subject: [PATCH 2/2] add stroke and other text rendering modes --- src/canvas.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index 6e33475d7..6eb066dba 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -6,6 +6,17 @@ // contexts store most of the state we need natively. // However, PDF needs a bit more state, which we store here. +var TextRenderingMode = { + FILL: 0, + STROKE: 1, + FILL_STROKE: 2, + INVISIBLE: 3, + FILL_ADD_TO_PATH: 4, + STROKE_ADD_TO_PATH: 5, + FILL_STROKE_ADD_TO_PATH: 6, + ADD_TO_PATH: 7 +}; + var CanvasExtraState = (function canvasExtraState() { function constructor(old) { // Are soft masks and alpha values shapes or opacities? @@ -23,7 +34,7 @@ var CanvasExtraState = (function canvasExtraState() { this.charSpacing = 0; this.wordSpacing = 0; this.textHScale = 1; - this.textRenderingMode = 0; + this.textRenderingMode = TextRenderingMode.FILL; // Color spaces this.fillColorSpace = new DeviceGrayCS(); this.fillColorSpaceObj = null; @@ -544,7 +555,7 @@ var CanvasGraphics = (function canvasGraphics() { this.ctx.font = rule; }, setTextRenderingMode: function canvasGraphicsSetTextRenderingMode(mode) { - if (mode != 0 && mode != 3) + if (mode >= TextRenderingMode.FILL_ADD_TO_PATH) TODO('unsupported text rendering mode: ' + mode); this.current.textRenderingMode = mode; }, @@ -699,11 +710,22 @@ var CanvasGraphics = (function canvasGraphics() { var charWidth = glyph.width * fontSize * 0.001 + charSpacing; switch (textRenderingMode) { - default: // unsupported rendering mode - case 0: // fill + default: // other unsupported rendering modes + case TextRenderingMode.FILL: + case TextRenderingMode.FILL_ADD_TO_PATH: ctx.fillText(char, width, 0); break; - case 3: // invisible + case TextRenderingMode.STROKE: + case TextRenderingMode.STROKE_ADD_TO_PATH: + ctx.strokeText(char, width, 0); + break; + case TextRenderingMode.FILL_STROKE: + case TextRenderingMode.FILL_STROKE_ADD_TO_PATH: + ctx.fillText(char, width, 0); + ctx.strokeText(char, width, 0); + break; + case TextRenderingMode.INVISIBLE: + break; } width += charWidth;