[api-minor] Change Font.exportData
to, by default, stop exporting properties which are completely unused on the main-thread and/or in the API (PR 11773 follow-up)
For years now, the `Font.exportData` method has (because of its previous implementation) been exporting many properties despite them being completely unused on the main-thread and/or in the API. This is unfortunate, since among those properties there's a number of potentially very large data-structures, containing e.g. Arrays and Objects, which thus have to be first structured cloned and then stored on the main-thread. With the changes in this patch, we'll thus by default save memory for *every* `Font` instance created (there can be a lot in longer documents). The memory savings obviously depends a lot on the actual font data, but some approximate figures are: For non-embedded fonts it can save a couple of kilobytes, for simple embedded fonts a handful of kilobytes, and for composite fonts the size of this auxiliary can even be larger than the actual font program itself. All-in-all, there's no good reason to keep exporting these properties by default when they're unused. However, since we cannot be sure that every property is unused in custom implementations of the PDF.js library, this patch adds a new `getDocument` option (named `fontExtraProperties`) that still allows access to the following properties: - "cMap": An internal data structure, only used with composite fonts and never really intended to be exposed on the main-thread and/or in the API. Note also that the `CMap`/`IdentityCMap` classes are a lot more complex than simple Objects, but only their "internal" properties survive the structured cloning used to send data to the main-thread. Given that CMaps can often be *very* large, not exporting them can also save a fair bit of memory. - "defaultEncoding": An internal property used with simple fonts, and used when building the glyph mapping on the worker-thread. Considering how complex that topic is, and given that not all font types are handled identically, exposing this on the main-thread and/or in the API most likely isn't useful. - "differences": An internal property used with simple fonts, and used when building the glyph mapping on the worker-thread. Considering how complex that topic is, and given that not all font types are handled identically, exposing this on the main-thread and/or in the API most likely isn't useful. - "isSymbolicFont": An internal property, used during font parsing and building of the glyph mapping on the worker-thread. - "seacMap": An internal map, only potentially used with *some* Type1/CFF fonts and never intended to be exposed in the API. The existing `Font.{charToGlyph, charToGlyphs}` functionality already takes this data into account when handling text. - "toFontChar": The glyph map, necessary for mapping characters to glyphs in the font, which is built upon the various encoding information contained in the font dictionary and/or font program. This is not directly used on the main-thread and/or in the API. - "toUnicode": The unicode map, necessary for text-extraction to work correctly, which is built upon the ToUnicode/CMap information contained in the font dictionary, but not directly used on the main-thread and/or in the API. - "vmetrics": An array of width data used with fonts which are composite *and* vertical, but not directly used on the main-thread and/or in the API. - "widths": An array of width data used with most fonts, but not directly used on the main-thread and/or in the API.
This commit is contained in:
parent
2619272d73
commit
2d46230d23
@ -94,6 +94,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||||||
nativeImageDecoderSupport: NativeImageDecoding.DECODE,
|
nativeImageDecoderSupport: NativeImageDecoding.DECODE,
|
||||||
ignoreErrors: false,
|
ignoreErrors: false,
|
||||||
isEvalSupported: true,
|
isEvalSupported: true,
|
||||||
|
fontExtraProperties: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
// eslint-disable-next-line no-shadow
|
// eslint-disable-next-line no-shadow
|
||||||
@ -807,6 +808,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||||||
loadedName: "g_font_error",
|
loadedName: "g_font_error",
|
||||||
font: new ErrorFont(`Type3 font load error: ${reason}`),
|
font: new ErrorFont(`Type3 font load error: ${reason}`),
|
||||||
dict: translated.font,
|
dict: translated.font,
|
||||||
|
extraProperties: this.options.fontExtraProperties,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@ -956,15 +958,16 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
loadFont: function PartialEvaluator_loadFont(fontName, font, resources) {
|
loadFont: function PartialEvaluator_loadFont(fontName, font, resources) {
|
||||||
function errorFont() {
|
const errorFont = () => {
|
||||||
return Promise.resolve(
|
return Promise.resolve(
|
||||||
new TranslatedFont({
|
new TranslatedFont({
|
||||||
loadedName: "g_font_error",
|
loadedName: "g_font_error",
|
||||||
font: new ErrorFont(`Font "${fontName}" is not available.`),
|
font: new ErrorFont(`Font "${fontName}" is not available.`),
|
||||||
dict: font,
|
dict: font,
|
||||||
|
extraProperties: this.options.fontExtraProperties,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
var fontRef,
|
var fontRef,
|
||||||
xref = this.xref;
|
xref = this.xref;
|
||||||
@ -1096,7 +1099,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
translatedPromise
|
translatedPromise
|
||||||
.then(function(translatedFont) {
|
.then(translatedFont => {
|
||||||
if (translatedFont.fontType !== undefined) {
|
if (translatedFont.fontType !== undefined) {
|
||||||
var xrefFontStats = xref.stats.fontTypes;
|
var xrefFontStats = xref.stats.fontTypes;
|
||||||
xrefFontStats[translatedFont.fontType] = true;
|
xrefFontStats[translatedFont.fontType] = true;
|
||||||
@ -1107,6 +1110,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||||||
loadedName: font.loadedName,
|
loadedName: font.loadedName,
|
||||||
font: translatedFont,
|
font: translatedFont,
|
||||||
dict: font,
|
dict: font,
|
||||||
|
extraProperties: this.options.fontExtraProperties,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
@ -1136,6 +1140,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||||||
reason instanceof Error ? reason.message : reason
|
reason instanceof Error ? reason.message : reason
|
||||||
),
|
),
|
||||||
dict: font,
|
dict: font,
|
||||||
|
extraProperties: this.options.fontExtraProperties,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -3273,10 +3278,11 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
class TranslatedFont {
|
class TranslatedFont {
|
||||||
constructor({ loadedName, font, dict }) {
|
constructor({ loadedName, font, dict, extraProperties = false }) {
|
||||||
this.loadedName = loadedName;
|
this.loadedName = loadedName;
|
||||||
this.font = font;
|
this.font = font;
|
||||||
this.dict = dict;
|
this.dict = dict;
|
||||||
|
this._extraProperties = extraProperties;
|
||||||
this.type3Loaded = null;
|
this.type3Loaded = null;
|
||||||
this.sent = false;
|
this.sent = false;
|
||||||
}
|
}
|
||||||
@ -3290,7 +3296,7 @@ class TranslatedFont {
|
|||||||
handler.send("commonobj", [
|
handler.send("commonobj", [
|
||||||
this.loadedName,
|
this.loadedName,
|
||||||
"Font",
|
"Font",
|
||||||
this.font.exportData(),
|
this.font.exportData(this._extraProperties),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,21 +92,17 @@ const EXPORT_DATA_PROPERTIES = [
|
|||||||
"bbox",
|
"bbox",
|
||||||
"black",
|
"black",
|
||||||
"bold",
|
"bold",
|
||||||
"cMap",
|
|
||||||
"charProcOperatorList",
|
"charProcOperatorList",
|
||||||
"composite",
|
"composite",
|
||||||
"data",
|
"data",
|
||||||
"defaultEncoding",
|
|
||||||
"defaultVMetrics",
|
"defaultVMetrics",
|
||||||
"defaultWidth",
|
"defaultWidth",
|
||||||
"descent",
|
"descent",
|
||||||
"differences",
|
|
||||||
"fallbackName",
|
"fallbackName",
|
||||||
"fontMatrix",
|
"fontMatrix",
|
||||||
"fontType",
|
"fontType",
|
||||||
"isMonospace",
|
"isMonospace",
|
||||||
"isSerifFont",
|
"isSerifFont",
|
||||||
"isSymbolicFont",
|
|
||||||
"isType3Font",
|
"isType3Font",
|
||||||
"italic",
|
"italic",
|
||||||
"loadedName",
|
"loadedName",
|
||||||
@ -114,12 +110,19 @@ const EXPORT_DATA_PROPERTIES = [
|
|||||||
"missingFile",
|
"missingFile",
|
||||||
"name",
|
"name",
|
||||||
"remeasure",
|
"remeasure",
|
||||||
"seacMap",
|
|
||||||
"subtype",
|
"subtype",
|
||||||
"toFontChar",
|
|
||||||
"toUnicode",
|
|
||||||
"type",
|
"type",
|
||||||
"vertical",
|
"vertical",
|
||||||
|
];
|
||||||
|
|
||||||
|
const EXPORT_DATA_EXTRA_PROPERTIES = [
|
||||||
|
"cMap",
|
||||||
|
"defaultEncoding",
|
||||||
|
"differences",
|
||||||
|
"isSymbolicFont",
|
||||||
|
"seacMap",
|
||||||
|
"toFontChar",
|
||||||
|
"toUnicode",
|
||||||
"vmetrics",
|
"vmetrics",
|
||||||
"widths",
|
"widths",
|
||||||
];
|
];
|
||||||
@ -1295,10 +1298,14 @@ var Font = (function FontClosure() {
|
|||||||
return shadow(this, "renderer", renderer);
|
return shadow(this, "renderer", renderer);
|
||||||
},
|
},
|
||||||
|
|
||||||
exportData() {
|
exportData(extraProperties = false) {
|
||||||
|
const exportDataProperties = extraProperties
|
||||||
|
? [...EXPORT_DATA_PROPERTIES, ...EXPORT_DATA_EXTRA_PROPERTIES]
|
||||||
|
: EXPORT_DATA_PROPERTIES;
|
||||||
|
|
||||||
const data = Object.create(null);
|
const data = Object.create(null);
|
||||||
let property, value;
|
let property, value;
|
||||||
for (property of EXPORT_DATA_PROPERTIES) {
|
for (property of exportDataProperties) {
|
||||||
value = this[property];
|
value = this[property];
|
||||||
// Ignore properties that haven't been explicitly set.
|
// Ignore properties that haven't been explicitly set.
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
@ -3352,7 +3359,7 @@ var ErrorFont = (function ErrorFontClosure() {
|
|||||||
charsToGlyphs: function ErrorFont_charsToGlyphs() {
|
charsToGlyphs: function ErrorFont_charsToGlyphs() {
|
||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
exportData: function ErrorFont_exportData() {
|
exportData(extraProperties = false) {
|
||||||
return { error: this.error };
|
return { error: this.error };
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -403,6 +403,7 @@ var WorkerMessageHandler = {
|
|||||||
nativeImageDecoderSupport: data.nativeImageDecoderSupport,
|
nativeImageDecoderSupport: data.nativeImageDecoderSupport,
|
||||||
ignoreErrors: data.ignoreErrors,
|
ignoreErrors: data.ignoreErrors,
|
||||||
isEvalSupported: data.isEvalSupported,
|
isEvalSupported: data.isEvalSupported,
|
||||||
|
fontExtraProperties: data.fontExtraProperties,
|
||||||
};
|
};
|
||||||
|
|
||||||
getPdfManager(data, evaluatorOptions)
|
getPdfManager(data, evaluatorOptions)
|
||||||
|
@ -145,6 +145,11 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
|
|||||||
* converted to OpenType fonts and loaded via font face rules. If disabled,
|
* converted to OpenType fonts and loaded via font face rules. If disabled,
|
||||||
* fonts will be rendered using a built-in font renderer that constructs the
|
* fonts will be rendered using a built-in font renderer that constructs the
|
||||||
* glyphs with primitive path commands. The default value is `false`.
|
* glyphs with primitive path commands. The default value is `false`.
|
||||||
|
* @property {boolean} [fontExtraProperties] - Include additional properties,
|
||||||
|
* which are unused during rendering of PDF documents, when exporting the
|
||||||
|
* parsed font data from the worker-thread. This may be useful for debugging
|
||||||
|
* purposes (and backwards compatibility), but note that it will lead to
|
||||||
|
* increased memory usage. The default value is `false`.
|
||||||
* @property {boolean} [disableRange] - Disable range request loading
|
* @property {boolean} [disableRange] - Disable range request loading
|
||||||
* of PDF files. When enabled, and if the server supports partial content
|
* of PDF files. When enabled, and if the server supports partial content
|
||||||
* requests, then the PDF will be fetched in chunks.
|
* requests, then the PDF will be fetched in chunks.
|
||||||
@ -251,6 +256,7 @@ function getDocument(src) {
|
|||||||
params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE;
|
params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE;
|
||||||
params.CMapReaderFactory = params.CMapReaderFactory || DOMCMapReaderFactory;
|
params.CMapReaderFactory = params.CMapReaderFactory || DOMCMapReaderFactory;
|
||||||
params.ignoreErrors = params.stopAtErrors !== true;
|
params.ignoreErrors = params.stopAtErrors !== true;
|
||||||
|
params.fontExtraProperties = params.fontExtraProperties === true;
|
||||||
params.pdfBug = params.pdfBug === true;
|
params.pdfBug = params.pdfBug === true;
|
||||||
|
|
||||||
const NativeImageDecoderValues = Object.values(NativeImageDecoding);
|
const NativeImageDecoderValues = Object.values(NativeImageDecoding);
|
||||||
@ -403,6 +409,7 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
|
|||||||
nativeImageDecoderSupport: source.nativeImageDecoderSupport,
|
nativeImageDecoderSupport: source.nativeImageDecoderSupport,
|
||||||
ignoreErrors: source.ignoreErrors,
|
ignoreErrors: source.ignoreErrors,
|
||||||
isEvalSupported: source.isEvalSupported,
|
isEvalSupported: source.isEvalSupported,
|
||||||
|
fontExtraProperties: source.fontExtraProperties,
|
||||||
})
|
})
|
||||||
.then(function(workerId) {
|
.then(function(workerId) {
|
||||||
if (worker.destroyed) {
|
if (worker.destroyed) {
|
||||||
|
@ -309,6 +309,8 @@ const PDFViewerApplication = {
|
|||||||
}
|
}
|
||||||
if ("pdfbug" in hashParams) {
|
if ("pdfbug" in hashParams) {
|
||||||
AppOptions.set("pdfBug", true);
|
AppOptions.set("pdfBug", true);
|
||||||
|
AppOptions.set("fontExtraProperties", true);
|
||||||
|
|
||||||
const enabled = hashParams["pdfbug"].split(",");
|
const enabled = hashParams["pdfbug"].split(",");
|
||||||
waitOn.push(loadAndEnablePDFBug(enabled));
|
waitOn.push(loadAndEnablePDFBug(enabled));
|
||||||
}
|
}
|
||||||
|
@ -193,6 +193,11 @@ const defaultOptions = {
|
|||||||
value: "",
|
value: "",
|
||||||
kind: OptionKind.API,
|
kind: OptionKind.API,
|
||||||
},
|
},
|
||||||
|
fontExtraProperties: {
|
||||||
|
/** @type {boolean} */
|
||||||
|
value: false,
|
||||||
|
kind: OptionKind.API,
|
||||||
|
},
|
||||||
isEvalSupported: {
|
isEvalSupported: {
|
||||||
/** @type {boolean} */
|
/** @type {boolean} */
|
||||||
value: true,
|
value: true,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user