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 <huzjakd@gmail.com>
Co-Authored-By: Jonas Jenwald <jonas.jenwald@gmail.com>
This commit is contained in:
huzjakd 2019-10-02 14:13:49 +02:00 committed by Jonas Jenwald
parent d2959fc9c9
commit 94171d9d72

View File

@ -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)) {