From a74e9113a4e3b6767f8c80633f642b27de0f917b Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Fri, 15 Sep 2017 21:42:36 +0200 Subject: [PATCH 1/3] Add files via upload --- plugins/standard_fonts_metrics.js | 206 +++++++----------------------- 1 file changed, 49 insertions(+), 157 deletions(-) diff --git a/plugins/standard_fonts_metrics.js b/plugins/standard_fonts_metrics.js index 38f671e9d..f7c2fd1ef 100644 --- a/plugins/standard_fonts_metrics.js +++ b/plugins/standard_fonts_metrics.js @@ -28,134 +28,6 @@ MIT license. ;(function(API) { 'use strict' -/* -# reference (Python) versions of 'compress' and 'uncompress' -# only 'uncompress' function is featured lower as JavaScript -# if you want to unit test "roundtrip", just transcribe the reference -# 'compress' function from Python into JavaScript - -def compress(data): - - keys = '0123456789abcdef' - values = 'klmnopqrstuvwxyz' - mapping = dict(zip(keys, values)) - vals = [] - for key in data.keys(): - value = data[key] - try: - keystring = hex(key)[2:] - keystring = keystring[:-1] + mapping[keystring[-1:]] - except: - keystring = key.join(["'","'"]) - #print('Keystring is %s' % keystring) - - try: - if value < 0: - valuestring = hex(value)[3:] - numberprefix = '-' - else: - valuestring = hex(value)[2:] - numberprefix = '' - valuestring = numberprefix + valuestring[:-1] + mapping[valuestring[-1:]] - except: - if type(value) == dict: - valuestring = compress(value) - else: - raise Exception("Don't know what to do with value type %s" % type(value)) - - vals.append(keystring+valuestring) - - return '{' + ''.join(vals) + '}' - -def uncompress(data): - - decoded = '0123456789abcdef' - encoded = 'klmnopqrstuvwxyz' - mapping = dict(zip(encoded, decoded)) - - sign = +1 - stringmode = False - stringparts = [] - - output = {} - - activeobject = output - parentchain = [] - - keyparts = '' - valueparts = '' - - key = None - - ending = set(encoded) - - i = 1 - l = len(data) - 1 # stripping starting, ending {} - while i != l: # stripping {} - # -, {, }, ' are special. - - ch = data[i] - i += 1 - - if ch == "'": - if stringmode: - # end of string mode - stringmode = False - key = ''.join(stringparts) - else: - # start of string mode - stringmode = True - stringparts = [] - elif stringmode == True: - #print("Adding %s to stringpart" % ch) - stringparts.append(ch) - - elif ch == '{': - # start of object - parentchain.append( [activeobject, key] ) - activeobject = {} - key = None - #DEBUG = True - elif ch == '}': - # end of object - parent, key = parentchain.pop() - parent[key] = activeobject - key = None - activeobject = parent - #DEBUG = False - - elif ch == '-': - sign = -1 - else: - # must be number - if key == None: - #debug("In Key. It is '%s', ch is '%s'" % (keyparts, ch)) - if ch in ending: - #debug("End of key") - keyparts += mapping[ch] - key = int(keyparts, 16) * sign - sign = +1 - keyparts = '' - else: - keyparts += ch - else: - #debug("In value. It is '%s', ch is '%s'" % (valueparts, ch)) - if ch in ending: - #debug("End of value") - valueparts += mapping[ch] - activeobject[key] = int(valueparts, 16) * sign - sign = +1 - key = None - valueparts = '' - else: - valueparts += ch - - #debug(activeobject) - - return output - -*/ - /** Uncompresses data compressed into custom, base16-like format. @public @@ -333,40 +205,60 @@ char codes to StandardEncoding character codes. The encoding table is to be used somewhere around "pdfEscape" call. */ -API.events.push([ - 'addFont' - ,function(font) { - var metrics - , unicode_section - , encoding = 'Unicode' - , encodingBlock; +var addStandardFontMetricsFunction = function (args) { + var postScriptName = args.postScriptName; + var fontName = args.fontName; + var fontStyle = args.fontStyle; + var encoding = args.encoding; + var metadata = args.metadata; + var mutex = args.mutex || {}; + var scope = mutex.scope; + + var metrics + , unicode_section + , encodingBlock; + + metrics = fontMetrics['Unicode'][postScriptName]; + if (metrics) { + if (metadata.Unicode === undefined) { + metadata.Unicode = metadata.Unicode || {encoding: {}, kerning: {}, widths: []}; + } + unicode_section = metadata.Unicode; - metrics = fontMetrics[encoding][font.PostScriptName]; - if (metrics) { - if (font.metadata[encoding]) { - unicode_section = font.metadata[encoding]; - } else { - unicode_section = font.metadata[encoding] = {}; - } + unicode_section.widths = metrics.widths; + unicode_section.kerning = metrics.kerning; + } - unicode_section.widths = metrics.widths; - unicode_section.kerning = metrics.kerning; + encodingBlock = encodings['Unicode'][postScriptName]; + if (encodingBlock) { + if (metadata.Unicode === undefined) { + metadata.Unicode = metadata.Unicode || {encoding: {}, kerning: {}, widths: []}; } + unicode_section = metadata.Unicode; - encodingBlock = encodings[encoding][font.PostScriptName]; - if (encodingBlock) { - if (font.metadata[encoding]) { - unicode_section = font.metadata[encoding]; - } else { - unicode_section = font.metadata[encoding] = {}; - } - - unicode_section.encoding = encodingBlock; - if (encodingBlock.codePages && encodingBlock.codePages.length) { - font.encoding = encodingBlock.codePages[0]; - } + unicode_section.encoding = encodingBlock; + if (encodingBlock.codePages && encodingBlock.codePages.length) { + encoding = encodingBlock.codePages[0]; } } -]) // end of adding event handler + return { + postScriptName: postScriptName, + fontName: fontName, + fontStyle: fontStyle, + encoding: encoding, + metadata: metadata + }; +} + +if (jsPDF.FunctionsPool === undefined) { + jsPDF.FunctionsPool = {}; +} +if (jsPDF.FunctionsPool.addFont === undefined) { + jsPDF.FunctionsPool.addFont = []; +} + +jsPDF.FunctionsPool.addFont.push( + addStandardFontMetricsFunction +); })(jsPDF.API); From d489f6ab7dff2c9d7359463ee96a74b9e5a200cc Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Wed, 20 Sep 2017 20:11:35 +0200 Subject: [PATCH 2/3] Update standard_fonts_metrics.js as long as i have pull requests, i have to maintain it this way :( --- plugins/standard_fonts_metrics.js | 206 +++++++++++++++++++++++------- 1 file changed, 157 insertions(+), 49 deletions(-) diff --git a/plugins/standard_fonts_metrics.js b/plugins/standard_fonts_metrics.js index f7c2fd1ef..38f671e9d 100644 --- a/plugins/standard_fonts_metrics.js +++ b/plugins/standard_fonts_metrics.js @@ -28,6 +28,134 @@ MIT license. ;(function(API) { 'use strict' +/* +# reference (Python) versions of 'compress' and 'uncompress' +# only 'uncompress' function is featured lower as JavaScript +# if you want to unit test "roundtrip", just transcribe the reference +# 'compress' function from Python into JavaScript + +def compress(data): + + keys = '0123456789abcdef' + values = 'klmnopqrstuvwxyz' + mapping = dict(zip(keys, values)) + vals = [] + for key in data.keys(): + value = data[key] + try: + keystring = hex(key)[2:] + keystring = keystring[:-1] + mapping[keystring[-1:]] + except: + keystring = key.join(["'","'"]) + #print('Keystring is %s' % keystring) + + try: + if value < 0: + valuestring = hex(value)[3:] + numberprefix = '-' + else: + valuestring = hex(value)[2:] + numberprefix = '' + valuestring = numberprefix + valuestring[:-1] + mapping[valuestring[-1:]] + except: + if type(value) == dict: + valuestring = compress(value) + else: + raise Exception("Don't know what to do with value type %s" % type(value)) + + vals.append(keystring+valuestring) + + return '{' + ''.join(vals) + '}' + +def uncompress(data): + + decoded = '0123456789abcdef' + encoded = 'klmnopqrstuvwxyz' + mapping = dict(zip(encoded, decoded)) + + sign = +1 + stringmode = False + stringparts = [] + + output = {} + + activeobject = output + parentchain = [] + + keyparts = '' + valueparts = '' + + key = None + + ending = set(encoded) + + i = 1 + l = len(data) - 1 # stripping starting, ending {} + while i != l: # stripping {} + # -, {, }, ' are special. + + ch = data[i] + i += 1 + + if ch == "'": + if stringmode: + # end of string mode + stringmode = False + key = ''.join(stringparts) + else: + # start of string mode + stringmode = True + stringparts = [] + elif stringmode == True: + #print("Adding %s to stringpart" % ch) + stringparts.append(ch) + + elif ch == '{': + # start of object + parentchain.append( [activeobject, key] ) + activeobject = {} + key = None + #DEBUG = True + elif ch == '}': + # end of object + parent, key = parentchain.pop() + parent[key] = activeobject + key = None + activeobject = parent + #DEBUG = False + + elif ch == '-': + sign = -1 + else: + # must be number + if key == None: + #debug("In Key. It is '%s', ch is '%s'" % (keyparts, ch)) + if ch in ending: + #debug("End of key") + keyparts += mapping[ch] + key = int(keyparts, 16) * sign + sign = +1 + keyparts = '' + else: + keyparts += ch + else: + #debug("In value. It is '%s', ch is '%s'" % (valueparts, ch)) + if ch in ending: + #debug("End of value") + valueparts += mapping[ch] + activeobject[key] = int(valueparts, 16) * sign + sign = +1 + key = None + valueparts = '' + else: + valueparts += ch + + #debug(activeobject) + + return output + +*/ + /** Uncompresses data compressed into custom, base16-like format. @public @@ -205,60 +333,40 @@ char codes to StandardEncoding character codes. The encoding table is to be used somewhere around "pdfEscape" call. */ -var addStandardFontMetricsFunction = function (args) { - var postScriptName = args.postScriptName; - var fontName = args.fontName; - var fontStyle = args.fontStyle; - var encoding = args.encoding; - var metadata = args.metadata; - var mutex = args.mutex || {}; - var scope = mutex.scope; - - var metrics - , unicode_section - , encodingBlock; - - metrics = fontMetrics['Unicode'][postScriptName]; - if (metrics) { - if (metadata.Unicode === undefined) { - metadata.Unicode = metadata.Unicode || {encoding: {}, kerning: {}, widths: []}; - } - unicode_section = metadata.Unicode; +API.events.push([ + 'addFont' + ,function(font) { + var metrics + , unicode_section + , encoding = 'Unicode' + , encodingBlock; - unicode_section.widths = metrics.widths; - unicode_section.kerning = metrics.kerning; - } + metrics = fontMetrics[encoding][font.PostScriptName]; + if (metrics) { + if (font.metadata[encoding]) { + unicode_section = font.metadata[encoding]; + } else { + unicode_section = font.metadata[encoding] = {}; + } - encodingBlock = encodings['Unicode'][postScriptName]; - if (encodingBlock) { - if (metadata.Unicode === undefined) { - metadata.Unicode = metadata.Unicode || {encoding: {}, kerning: {}, widths: []}; + unicode_section.widths = metrics.widths; + unicode_section.kerning = metrics.kerning; } - unicode_section = metadata.Unicode; - unicode_section.encoding = encodingBlock; - if (encodingBlock.codePages && encodingBlock.codePages.length) { - encoding = encodingBlock.codePages[0]; + encodingBlock = encodings[encoding][font.PostScriptName]; + if (encodingBlock) { + if (font.metadata[encoding]) { + unicode_section = font.metadata[encoding]; + } else { + unicode_section = font.metadata[encoding] = {}; + } + + unicode_section.encoding = encodingBlock; + if (encodingBlock.codePages && encodingBlock.codePages.length) { + font.encoding = encodingBlock.codePages[0]; + } } } +]) // end of adding event handler - return { - postScriptName: postScriptName, - fontName: fontName, - fontStyle: fontStyle, - encoding: encoding, - metadata: metadata - }; -} - -if (jsPDF.FunctionsPool === undefined) { - jsPDF.FunctionsPool = {}; -} -if (jsPDF.FunctionsPool.addFont === undefined) { - jsPDF.FunctionsPool.addFont = []; -} - -jsPDF.FunctionsPool.addFont.push( - addStandardFontMetricsFunction -); })(jsPDF.API); From 05dea7279a542850741644e42198eb2045a83928 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Thu, 26 Oct 2017 23:48:40 +0200 Subject: [PATCH 3/3] fix_the_fix --- plugins/from_html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/from_html.js b/plugins/from_html.js index 886ee68eb..57689f2d5 100644 --- a/plugins/from_html.js +++ b/plugins/from_html.js @@ -819,7 +819,7 @@ this.pdf.internal.write("ET", "Q"); this.pdf.addPage(); this.y = this.pdf.margins_doc.top; - this.pdf.internal.write("q", "BT 0 g", this.pdf.internal.getCoordinateString(this.x), this.pdf.internal.getVerticalCoordinateString(this.y), style.color, "Td"); + this.pdf.internal.write("q", "BT", this.getPdfColor(style.color), this.pdf.internal.getCoordinateString(this.x), this.pdf.internal.getVerticalCoordinateString(this.y), "Td"); //move cursor by one line on new page maxLineHeight = Math.max(maxLineHeight, style["line-height"], style["font-size"]); this.pdf.internal.write(0, (-1 * defaultFontSize * maxLineHeight).toFixed(2), "Td");