From 858e3864abb2886a5bdc3576fdbc572b03313100 Mon Sep 17 00:00:00 2001 From: Helmuth Breitenfellner Date: Sat, 18 Jan 2014 21:21:56 +0100 Subject: [PATCH 1/2] Add WinAnsiEncoding for standard fonts When using a PDF standard font, the encoding is set to WinAnsiEncoding. In addition, a encoding function is used to translate from UTF-8 to WinAnsiEncoding. --- lib/font.coffee | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/font.coffee b/lib/font.coffee index 92af2df..d28b0cf 100644 --- a/lib/font.coffee +++ b/lib/font.coffee @@ -33,6 +33,44 @@ class PDFFont return fn() if @isAFM @embedTTF fn + _WinAnsiMap: { + 402: 131, + 8211: 150, + 8212: 151, + 8216: 145, + 8217: 146, + 8218: 130, + 8220: 147, + 8221: 148, + 8222: 132, + 8224: 134, + 8225: 135, + 8226: 149, + 8230: 133, + 8364: 128, + 8240:137, + 8249: 139, + 8250: 155, + 710: 136, + 8482: 153, + 338: 140, + 339: 156, + 732: 152, + 352: 138, + 353: 154, + 376: 159, + 381: 142, + 382: 158 + } + + _encodeWinAnsi: (text) -> + string = '' + for i in [0...text.length] + char = @_WinAnsiMap[text.charCodeAt(i)] || text.charCodeAt(i) + string += String.fromCharCode(char) + + return string + encode: (text) -> @subset?.encodeText(text) or text @@ -173,6 +211,8 @@ class PDFFont Type: 'Font' BaseFont: @filename Subtype: 'Type1' + Encoding: 'WinAnsiEncoding' + @encode = @_encodeWinAnsi _standardFonts: [ "Courier" @@ -205,4 +245,4 @@ class PDFFont gap = if includeGap then @lineGap else 0 (@ascender + gap - @decender) / 1000 * size -module.exports = PDFFont \ No newline at end of file +module.exports = PDFFont From 0d56d85a5dc2fc817d23c1c01d7b5bf73d9ae14d Mon Sep 17 00:00:00 2001 From: Helmuth Breitenfellner Date: Sat, 18 Jan 2014 22:10:57 +0100 Subject: [PATCH 2/2] incorporated comments from @devongovett --- lib/font.coffee | 61 ++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/lib/font.coffee b/lib/font.coffee index d28b0cf..c36f762 100644 --- a/lib/font.coffee +++ b/lib/font.coffee @@ -34,45 +34,49 @@ class PDFFont @embedTTF fn _WinAnsiMap: { - 402: 131, - 8211: 150, - 8212: 151, - 8216: 145, - 8217: 146, - 8218: 130, - 8220: 147, - 8221: 148, - 8222: 132, - 8224: 134, - 8225: 135, - 8226: 149, - 8230: 133, - 8364: 128, - 8240:137, - 8249: 139, - 8250: 155, - 710: 136, - 8482: 153, - 338: 140, - 339: 156, - 732: 152, - 352: 138, - 353: 154, - 376: 159, - 381: 142, + 402: 131 + 8211: 150 + 8212: 151 + 8216: 145 + 8217: 146 + 8218: 130 + 8220: 147 + 8221: 148 + 8222: 132 + 8224: 134 + 8225: 135 + 8226: 149 + 8230: 133 + 8364: 128 + 8240: 137 + 8249: 139 + 8250: 155 + 710: 136 + 8482: 153 + 338: 140 + 339: 156 + 732: 152 + 352: 138 + 353: 154 + 376: 159 + 381: 142 382: 158 } _encodeWinAnsi: (text) -> string = '' for i in [0...text.length] - char = @_WinAnsiMap[text.charCodeAt(i)] || text.charCodeAt(i) + char = text.charCodeAt(i) + char = @_WinAnsiMap[char] || char string += String.fromCharCode(char) return string encode: (text) -> - @subset?.encodeText(text) or text + if @isAFM + @_encodeWinAnsi text + else + @subset?.encodeText(text) or text registerTTF: -> @scaleFactor = 1000.0 / @ttf.head.unitsPerEm @@ -212,7 +216,6 @@ class PDFFont BaseFont: @filename Subtype: 'Type1' Encoding: 'WinAnsiEncoding' - @encode = @_encodeWinAnsi _standardFonts: [ "Courier"