From 94171d9d720f4bfe2cf1737c0aa7ccfad04c7c81 Mon Sep 17 00:00:00 2001 From: huzjakd Date: Wed, 2 Oct 2019 14:13:49 +0200 Subject: [PATCH] Attempt to fallback to a default font, for non-available ones, in `PartialEvaluator.loadFont` This handles the two different ways that fonts can be loaded, either by Name (which is the common case) or by Reference. Furthermore, this also takes the `ignoreErrors` option into account when deciding whether to fallback or Error. Finally, by creating a minimal but valid Font dictionary, there's no special-cases necessary in any of the font parsing code. Co-authored-by: huzjakd Co-Authored-By: Jonas Jenwald --- src/core/evaluator.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 9bc1e75ef..46c45fd58 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -711,21 +711,35 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { var fontRef, xref = this.xref; if (font) { // Loading by ref. if (!isRef(font)) { - throw new Error('The "font" object should be a reference.'); + throw new FormatError('The "font" object should be a reference.'); } fontRef = font; } else { // Loading by name. var fontRes = resources.get('Font'); if (fontRes) { fontRef = fontRes.getRaw(fontName); - } else { - warn('fontRes not available'); - return errorFont(); } } if (!fontRef) { - warn('fontRef not available'); - return errorFont(); + const partialMsg = + `Font "${fontName || (font && font.toString())}" is not available`; + + if (!this.options.ignoreErrors && !this.parsingType3Font) { + warn(`${partialMsg}.`); + return errorFont(); + } + // Font not found -- sending unsupported feature notification. + this.handler.send('UnsupportedFeature', + { featureId: UNSUPPORTED_FEATURES.font, }); + warn(`${partialMsg} -- attempting to fallback to a default font.`); + + // Falling back to a default font to avoid completely broken rendering, + // but note that there're no guarantees that things will look "correct". + fontRef = new Dict(); + fontRef.set('BaseFont', Name.get('PDFJS-FallbackFont')); + fontRef.set('Type', Name.get('FallbackType')); + fontRef.set('Subtype', Name.get('FallbackType')); + fontRef.set('Encoding', Name.get('WinAnsiEncoding')); } if (this.fontCache.has(fontRef)) {