From 2248690d110b48fd3d30ed235b405020351d27e3 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Wed, 21 Sep 2011 19:56:09 -0500 Subject: [PATCH 1/2] Guess default width for FileFont3 using BaseFont --- pdf.js | 82 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/pdf.js b/pdf.js index 9a0b0977a..7e56c309f 100644 --- a/pdf.js +++ b/pdf.js @@ -4483,6 +4483,32 @@ var PartialEvaluator = (function() { return glyphs; }, + getBaseFontMetrics: function(baseFontName) { + var map = {}; + if (/^Symbol(-?(Bold|Italic))*$/.test(baseFontName)) { + // special case for symbols + var encoding = Encodings.symbolsEncoding; + for (var i = 0, n = encoding.length, j; i < n; i++) { + if (!(j = encoding[i])) + continue; + map[i] = GlyphsUnicode[j] || 0; + } + } + + var defaultWidth = 0; + var widths = Metrics[stdFontMap[baseFontName] || baseFontName]; + if (IsNum(widths)) { + defaultWidth = widths; + widths = null; + } + + return { + defaultWidth: defaultWidth, + widths: widths || [], + map: map + }; + }, + translateFont: function(dict, xref, resources) { var baseDict = dict; var type = dict.get('Subtype'); @@ -4518,30 +4544,15 @@ var PartialEvaluator = (function() { return null; // Using base font name as a font name. - baseFontName = baseFontName.name; - var map = {}; - if (/^Symbol(-?(Bold|Italic))*$/.test(baseFontName)) { - // special case for symbols - var encoding = Encodings.symbolsEncoding; - for (var i = 0, n = encoding.length, j; i < n; i++) { - if (!(j = encoding[i])) - continue; - map[i] = GlyphsUnicode[j] || 0; - } - } + baseFontName = baseFontName.name.replace(/,/g, '_'); + var metrics = this.getBaseFontMetrics(baseFontName); - var defaultWidth = 0; - var widths = Metrics[stdFontMap[baseFontName] || baseFontName]; - if (IsNum(widths)) { - defaultWidth = widths; - widths = null; - } var properties = { type: type.name, - encoding: map, + encoding: metrics.map, differences: [], - widths: widths || {}, - defaultWidth: defaultWidth, + widths: metrics.widths, + defaultWidth: metrics.defaultWidth, firstChar: 0, lastChar: 256 }; @@ -4561,7 +4572,25 @@ var PartialEvaluator = (function() { // a variant. var firstChar = xref.fetchIfRef(dict.get('FirstChar')) || 0; var lastChar = xref.fetchIfRef(dict.get('LastChar')) || 256; - var widths = xref.fetchIfRef(dict.get('Widths')) || []; + var defaultWidth = 0; + var glyphWidths = {}; + var encoding = {}; + var widths = xref.fetchIfRef(dict.get('Widths')); + if (widths) { + for (var i = 0, j = firstChar; i < widths.length; i++, j++) + glyphWidths[j] = widths[i]; + defaultWidth = parseFloat(descriptor.get('MissingWidth')) || 0; + } else { + // Trying get the BaseFont metrics (see comment above). + var baseFontName = dict.get('BaseFont'); + if (IsName(baseFontName)) { + var metrics = this.getBaseFontMetrics(baseFontName.name); + + glyphWidths = metrics.widths; + defaultWidth = metrics.defaultWidth; + encoding = metrics.map; + } + } var fontName = xref.fetchIfRef(descriptor.get('FontName')); assertWellFormed(IsName(fontName), 'invalid font name'); @@ -4600,17 +4629,12 @@ var PartialEvaluator = (function() { descent: descriptor.get('Descent'), xHeight: descriptor.get('XHeight'), capHeight: descriptor.get('CapHeight'), - defaultWidth: parseFloat(descriptor.get('MissingWidth')) || 0, + defaultWidth: defaultWidth, flags: descriptor.get('Flags'), italicAngle: descriptor.get('ItalicAngle'), differences: [], - widths: (function() { - var glyphWidths = {}; - for (var i = 0; i < widths.length; i++) - glyphWidths[firstChar++] = widths[i]; - return glyphWidths; - })(), - encoding: {} + widths: glyphWidths, + encoding: encoding }; properties.glyphs = this.extractEncoding(dict, xref, properties); From fc5ae0f87548cb2d137519c3b78d23b9248a5392 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Wed, 21 Sep 2011 21:53:36 -0500 Subject: [PATCH 2/2] Fixing getBaseFontMetricsAndMap function name --- pdf.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pdf.js b/pdf.js index f6f4a3b52..1a5bf4854 100644 --- a/pdf.js +++ b/pdf.js @@ -4490,9 +4490,9 @@ var PartialEvaluator = (function partialEvaluator() { return glyphs; }, - getBaseFontMetrics: function(baseFontName) { + getBaseFontMetricsAndMap: function getBaseFontMetricsAndMap(name) { var map = {}; - if (/^Symbol(-?(Bold|Italic))*$/.test(baseFontName)) { + if (/^Symbol(-?(Bold|Italic))*$/.test(name)) { // special case for symbols var encoding = Encodings.symbolsEncoding; for (var i = 0, n = encoding.length, j; i < n; i++) { @@ -4503,7 +4503,7 @@ var PartialEvaluator = (function partialEvaluator() { } var defaultWidth = 0; - var widths = Metrics[stdFontMap[baseFontName] || baseFontName]; + var widths = Metrics[stdFontMap[name] || name]; if (IsNum(widths)) { defaultWidth = widths; widths = null; @@ -4553,14 +4553,14 @@ var PartialEvaluator = (function partialEvaluator() { // Using base font name as a font name. baseFontName = baseFontName.name.replace(/,/g, '_'); - var metrics = this.getBaseFontMetrics(baseFontName); + var metricsAndMap = this.getBaseFontMetricsAndMap(baseFontName); var properties = { type: type.name, - encoding: metrics.map, + encoding: metricsAndMap.map, differences: [], - widths: metrics.widths, - defaultWidth: metrics.defaultWidth, + widths: metricsAndMap.widths, + defaultWidth: metricsAndMap.defaultWidth, firstChar: 0, lastChar: 256 }; @@ -4592,11 +4592,11 @@ var PartialEvaluator = (function partialEvaluator() { // Trying get the BaseFont metrics (see comment above). var baseFontName = dict.get('BaseFont'); if (IsName(baseFontName)) { - var metrics = this.getBaseFontMetrics(baseFontName.name); + var metricsAndMap = this.getBaseFontMetricsAndMap(baseFontName.name); - glyphWidths = metrics.widths; - defaultWidth = metrics.defaultWidth; - encoding = metrics.map; + glyphWidths = metricsAndMap.widths; + defaultWidth = metricsAndMap.defaultWidth; + encoding = metricsAndMap.map; } }