mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Include and use the 14 standard fonts files.
This commit is contained in:
parent
3456ed271b
commit
4c1dd47e65
36 changed files with 463 additions and 80 deletions
|
@ -74,7 +74,10 @@ class CFFFont {
|
|||
return charCodeToGlyphId;
|
||||
}
|
||||
|
||||
const encoding = cff.encoding ? cff.encoding.encoding : null;
|
||||
let encoding = cff.encoding ? cff.encoding.encoding : null;
|
||||
if (properties.isInternalFont) {
|
||||
encoding = properties.defaultEncoding;
|
||||
}
|
||||
charCodeToGlyphId = type1FontGlyphMapping(properties, encoding, charsets);
|
||||
return charCodeToGlyphId;
|
||||
}
|
||||
|
|
|
@ -64,7 +64,9 @@ import {
|
|||
} from "./unicode.js";
|
||||
import {
|
||||
getSerifFonts,
|
||||
getStandardFontName,
|
||||
getStdFontMap,
|
||||
getStdFontNameToFileMap,
|
||||
getSymbolsFonts,
|
||||
} from "./standard_fonts.js";
|
||||
import { getTilingPatternIR, Pattern } from "./pattern.js";
|
||||
|
@ -77,6 +79,7 @@ import {
|
|||
LocalImageCache,
|
||||
LocalTilingPatternCache,
|
||||
} from "./image_utils.js";
|
||||
import { NullStream, Stream } from "./stream.js";
|
||||
import { bidi } from "./bidi.js";
|
||||
import { ColorSpace } from "./colorspace.js";
|
||||
import { DecodeStream } from "./decode_stream.js";
|
||||
|
@ -84,7 +87,6 @@ import { getGlyphsUnicode } from "./glyphlist.js";
|
|||
import { getLookupTableFactory } from "./core_utils.js";
|
||||
import { getMetrics } from "./metrics.js";
|
||||
import { MurmurHash3_64 } from "./murmurhash3.js";
|
||||
import { NullStream } from "./stream.js";
|
||||
import { OperatorList } from "./operator_list.js";
|
||||
import { PDFImage } from "./image.js";
|
||||
|
||||
|
@ -94,6 +96,8 @@ const DefaultPartialEvaluatorOptions = Object.freeze({
|
|||
ignoreErrors: false,
|
||||
isEvalSupported: true,
|
||||
fontExtraProperties: false,
|
||||
standardFontDataUrl: null,
|
||||
useSystemFonts: true,
|
||||
});
|
||||
|
||||
const PatternType = {
|
||||
|
@ -381,6 +385,43 @@ class PartialEvaluator {
|
|||
return data;
|
||||
}
|
||||
|
||||
async fetchStandardFontData(name) {
|
||||
// The symbol fonts are not consistent across platforms, always load the
|
||||
// font data for them.
|
||||
if (
|
||||
this.options.useSystemFonts &&
|
||||
name !== "Symbol" &&
|
||||
name !== "ZapfDingbats"
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const standardFontNameToFileName = getStdFontNameToFileMap();
|
||||
const filename = standardFontNameToFileName[name];
|
||||
if (this.options.standardFontDataUrl !== null) {
|
||||
const url = `${this.options.standardFontDataUrl}${filename}.pfb`;
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
warn(
|
||||
`fetchStandardFontData failed to fetch file "${url}" with "${response.statusText}".`
|
||||
);
|
||||
return null;
|
||||
}
|
||||
return new Stream(await response.arrayBuffer());
|
||||
}
|
||||
// Get the data on the main thread instead.
|
||||
try {
|
||||
const data = await this.handler.sendWithPromise("FetchStandardFontData", {
|
||||
filename,
|
||||
});
|
||||
return new Stream(data);
|
||||
} catch (e) {
|
||||
warn(
|
||||
`fetchStandardFontData failed to fetch file "${filename}" with "${e}".`
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async buildFormXObject(
|
||||
resources,
|
||||
xobj,
|
||||
|
@ -3711,6 +3752,7 @@ class PartialEvaluator {
|
|||
properties = {
|
||||
type,
|
||||
name: baseFontName,
|
||||
loadedName: baseDict.loadedName,
|
||||
widths: metrics.widths,
|
||||
defaultWidth: metrics.defaultWidth,
|
||||
flags,
|
||||
|
@ -3720,6 +3762,13 @@ class PartialEvaluator {
|
|||
isType3Font,
|
||||
};
|
||||
const widths = dict.get("Widths");
|
||||
const standardFontName = getStandardFontName(baseFontName);
|
||||
let file = null;
|
||||
if (standardFontName) {
|
||||
properties.isStandardFont = true;
|
||||
file = await this.fetchStandardFontData(standardFontName);
|
||||
properties.isInternalFont = !!file;
|
||||
}
|
||||
return this.extractDataStructures(dict, dict, properties).then(
|
||||
newProperties => {
|
||||
if (widths) {
|
||||
|
@ -3735,7 +3784,7 @@ class PartialEvaluator {
|
|||
newProperties
|
||||
);
|
||||
}
|
||||
return new Font(baseFontName, null, newProperties);
|
||||
return new Font(baseFontName, file, newProperties);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -3788,6 +3837,8 @@ class PartialEvaluator {
|
|||
warn(`translateFont - fetching "${fontName.name}" font file: "${ex}".`);
|
||||
fontFile = new NullStream();
|
||||
}
|
||||
let isStandardFont = false;
|
||||
let isInternalFont = false;
|
||||
if (fontFile) {
|
||||
if (fontFile.dict) {
|
||||
const subtypeEntry = fontFile.dict.get("Subtype");
|
||||
|
@ -3798,6 +3849,13 @@ class PartialEvaluator {
|
|||
length2 = fontFile.dict.get("Length2");
|
||||
length3 = fontFile.dict.get("Length3");
|
||||
}
|
||||
} else if (type === "Type1") {
|
||||
const standardFontName = getStandardFontName(fontName.name);
|
||||
if (standardFontName) {
|
||||
isStandardFont = true;
|
||||
fontFile = await this.fetchStandardFontData(standardFontName);
|
||||
isInternalFont = !!fontFile;
|
||||
}
|
||||
}
|
||||
|
||||
properties = {
|
||||
|
@ -3808,6 +3866,8 @@ class PartialEvaluator {
|
|||
length1,
|
||||
length2,
|
||||
length3,
|
||||
isStandardFont,
|
||||
isInternalFont,
|
||||
loadedName: baseDict.loadedName,
|
||||
composite,
|
||||
fixedPitch: false,
|
||||
|
|
|
@ -30,6 +30,7 @@ import {
|
|||
FontFlags,
|
||||
getFontType,
|
||||
MacStandardGlyphOrdering,
|
||||
normalizeFontName,
|
||||
recoverGlyphName,
|
||||
SEAC_ANALYSIS_ENABLED,
|
||||
} from "./fonts_utils.js";
|
||||
|
@ -130,6 +131,9 @@ function adjustWidths(properties) {
|
|||
}
|
||||
|
||||
function adjustToUnicode(properties, builtInEncoding) {
|
||||
if (properties.isInternalFont) {
|
||||
return;
|
||||
}
|
||||
if (properties.hasIncludedToUnicodeMap) {
|
||||
return; // The font dictionary has a `ToUnicode` entry.
|
||||
}
|
||||
|
@ -928,7 +932,7 @@ class Font {
|
|||
}
|
||||
|
||||
this.data = data;
|
||||
this.fontType = getFontType(type, subtype);
|
||||
this.fontType = getFontType(type, subtype, properties.isStandardFont);
|
||||
|
||||
// Transfer some properties again that could change during font conversion
|
||||
this.fontMatrix = properties.fontMatrix;
|
||||
|
@ -967,7 +971,7 @@ class Font {
|
|||
const name = this.name;
|
||||
const type = this.type;
|
||||
const subtype = this.subtype;
|
||||
let fontName = name.replace(/[,_]/g, "-").replace(/\s/g, "");
|
||||
let fontName = normalizeFontName(name);
|
||||
const stdFontMap = getStdFontMap(),
|
||||
nonStdFontMap = getNonStdFontMap();
|
||||
const isStandardFont = !!stdFontMap[fontName];
|
||||
|
@ -1086,7 +1090,7 @@ class Font {
|
|||
this.toFontChar = map;
|
||||
}
|
||||
this.loadedName = fontName.split("-")[0];
|
||||
this.fontType = getFontType(type, subtype);
|
||||
this.fontType = getFontType(type, subtype, properties.isStandardFont);
|
||||
}
|
||||
|
||||
checkAndRepair(name, font, properties) {
|
||||
|
|
|
@ -78,9 +78,12 @@ const MacStandardGlyphOrdering = [
|
|||
"threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla",
|
||||
"scedilla", "Cacute", "cacute", "Ccaron", "ccaron", "dcroat"];
|
||||
|
||||
function getFontType(type, subtype) {
|
||||
function getFontType(type, subtype, isStandardFont = false) {
|
||||
switch (type) {
|
||||
case "Type1":
|
||||
if (isStandardFont) {
|
||||
return FontType.TYPE1STANDARD;
|
||||
}
|
||||
return subtype === "Type1C" ? FontType.TYPE1C : FontType.TYPE1;
|
||||
case "CIDFontType0":
|
||||
return subtype === "CIDFontType0C"
|
||||
|
@ -135,7 +138,17 @@ function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
|
|||
let glyphId, charCode, baseEncoding;
|
||||
const isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
|
||||
|
||||
if (properties.baseEncodingName) {
|
||||
if (properties.isInternalFont) {
|
||||
baseEncoding = builtInEncoding;
|
||||
for (charCode = 0; charCode < baseEncoding.length; charCode++) {
|
||||
glyphId = glyphNames.indexOf(baseEncoding[charCode]);
|
||||
if (glyphId >= 0) {
|
||||
charCodeToGlyphId[charCode] = glyphId;
|
||||
} else {
|
||||
charCodeToGlyphId[charCode] = 0; // notdef
|
||||
}
|
||||
}
|
||||
} else if (properties.baseEncodingName) {
|
||||
// If a valid base encoding name was used, the mapping is initialized with
|
||||
// that.
|
||||
baseEncoding = getEncoding(properties.baseEncodingName);
|
||||
|
@ -193,10 +206,15 @@ function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
|
|||
return charCodeToGlyphId;
|
||||
}
|
||||
|
||||
function normalizeFontName(name) {
|
||||
return name.replace(/[,_]/g, "-").replace(/\s/g, "");
|
||||
}
|
||||
|
||||
export {
|
||||
FontFlags,
|
||||
getFontType,
|
||||
MacStandardGlyphOrdering,
|
||||
normalizeFontName,
|
||||
recoverGlyphName,
|
||||
SEAC_ANALYSIS_ENABLED,
|
||||
type1FontGlyphMapping,
|
||||
|
|
|
@ -14,12 +14,30 @@
|
|||
*/
|
||||
|
||||
import { getLookupTableFactory } from "./core_utils.js";
|
||||
import { normalizeFontName } from "./fonts_utils.js";
|
||||
|
||||
/**
|
||||
* Hold a map of decoded fonts and of the standard fourteen Type1
|
||||
* fonts and their acronyms.
|
||||
*/
|
||||
const getStdFontMap = getLookupTableFactory(function (t) {
|
||||
// The standard 14 fonts:
|
||||
t["Times-Roman"] = "Times-Roman";
|
||||
t.Helvetica = "Helvetica";
|
||||
t.Courier = "Courier";
|
||||
t.Symbol = "Symbol";
|
||||
t["Times-Bold"] = "Times-Bold";
|
||||
t["Helvetica-Bold"] = "Helvetica-Bold";
|
||||
t["Courier-Bold"] = "Courier-Bold";
|
||||
t.ZapfDingbats = "ZapfDingbats";
|
||||
t["Times-Italic"] = "Times-Italic";
|
||||
t["Helvetica-Oblique"] = "Helvetica-Oblique";
|
||||
t["Courier-Oblique"] = "Courier-Oblique";
|
||||
t["Times-BoldItalic"] = "Times-BoldItalic";
|
||||
t["Helvetica-BoldOblique"] = "Helvetica-BoldOblique";
|
||||
t["Courier-BoldOblique"] = "Courier-BoldOblique";
|
||||
|
||||
// Extra mappings
|
||||
t.ArialNarrow = "Helvetica";
|
||||
t["ArialNarrow-Bold"] = "Helvetica-Bold";
|
||||
t["ArialNarrow-BoldItalic"] = "Helvetica-BoldOblique";
|
||||
|
@ -40,7 +58,6 @@ const getStdFontMap = getLookupTableFactory(function (t) {
|
|||
t["Arial-BoldMT"] = "Helvetica-Bold";
|
||||
t["Arial-ItalicMT"] = "Helvetica-Oblique";
|
||||
t.ArialMT = "Helvetica";
|
||||
t["Courier-Bold"] = "Courier-Bold";
|
||||
t["Courier-BoldItalic"] = "Courier-BoldOblique";
|
||||
t["Courier-Italic"] = "Courier-Oblique";
|
||||
t.CourierNew = "Courier";
|
||||
|
@ -51,12 +68,8 @@ const getStdFontMap = getLookupTableFactory(function (t) {
|
|||
t["CourierNewPS-BoldMT"] = "Courier-Bold";
|
||||
t["CourierNewPS-ItalicMT"] = "Courier-Oblique";
|
||||
t.CourierNewPSMT = "Courier";
|
||||
t.Helvetica = "Helvetica";
|
||||
t["Helvetica-Bold"] = "Helvetica-Bold";
|
||||
t["Helvetica-BoldItalic"] = "Helvetica-BoldOblique";
|
||||
t["Helvetica-BoldOblique"] = "Helvetica-BoldOblique";
|
||||
t["Helvetica-Italic"] = "Helvetica-Oblique";
|
||||
t["Helvetica-Oblique"] = "Helvetica-Oblique";
|
||||
t["Symbol-Bold"] = "Symbol";
|
||||
t["Symbol-BoldItalic"] = "Symbol";
|
||||
t["Symbol-Italic"] = "Symbol";
|
||||
|
@ -77,6 +90,23 @@ const getStdFontMap = getLookupTableFactory(function (t) {
|
|||
t["TimesNewRomanPSMT-Italic"] = "Times-Italic";
|
||||
});
|
||||
|
||||
const getStdFontNameToFileMap = getLookupTableFactory(function (t) {
|
||||
t.Courier = "FoxitFixed";
|
||||
t["Courier-Bold"] = "FoxitFixedBold";
|
||||
t["Courier-BoldOblique"] = "FoxitFixedBoldItalic";
|
||||
t["Courier-Oblique"] = "FoxitFixedItalic";
|
||||
t.Helvetica = "FoxitSans";
|
||||
t["Helvetica-Bold"] = "FoxitSansBold";
|
||||
t["Helvetica-BoldOblique"] = "FoxitSansBoldItalic";
|
||||
t["Helvetica-Oblique"] = "FoxitSansItalic";
|
||||
t["Times-Roman"] = "FoxitSerif";
|
||||
t["Times-Bold"] = "FoxitSerifBold";
|
||||
t["Times-BoldItalic"] = "FoxitSerifBoldItalic";
|
||||
t["Times-Italic"] = "FoxitSerifItalic";
|
||||
t.Symbol = "FoxitSymbol";
|
||||
t.ZapfDingbats = "FoxitDingbats";
|
||||
});
|
||||
|
||||
/**
|
||||
* Holds the map of the non-standard fonts that might be included as
|
||||
* a standard fonts without glyph data.
|
||||
|
@ -763,11 +793,19 @@ const getSupplementalGlyphMapForCalibri = getLookupTableFactory(function (t) {
|
|||
t[1086] = 45;
|
||||
});
|
||||
|
||||
function getStandardFontName(name) {
|
||||
const fontName = normalizeFontName(name);
|
||||
const stdFontMap = getStdFontMap();
|
||||
return stdFontMap[fontName];
|
||||
}
|
||||
|
||||
export {
|
||||
getGlyphMapForStandardFonts,
|
||||
getNonStdFontMap,
|
||||
getSerifFonts,
|
||||
getStandardFontName,
|
||||
getStdFontMap,
|
||||
getStdFontNameToFileMap,
|
||||
getSupplementalGlyphMapForArialBlack,
|
||||
getSupplementalGlyphMapForCalibri,
|
||||
getSymbolsFonts,
|
||||
|
|
|
@ -411,6 +411,8 @@ class WorkerMessageHandler {
|
|||
ignoreErrors: data.ignoreErrors,
|
||||
isEvalSupported: data.isEvalSupported,
|
||||
fontExtraProperties: data.fontExtraProperties,
|
||||
useSystemFonts: data.useSystemFonts,
|
||||
standardFontDataUrl: data.standardFontDataUrl,
|
||||
};
|
||||
|
||||
getPdfManager(data, evaluatorOptions, data.enableXfa)
|
||||
|
|
|
@ -40,6 +40,7 @@ import {
|
|||
deprecated,
|
||||
DOMCanvasFactory,
|
||||
DOMCMapReaderFactory,
|
||||
DOMStandardFontDataFactory,
|
||||
isDataScheme,
|
||||
loadScript,
|
||||
PageViewport,
|
||||
|
@ -47,7 +48,11 @@ import {
|
|||
StatTimer,
|
||||
} from "./display_utils.js";
|
||||
import { FontFaceObject, FontLoader } from "./font_loader.js";
|
||||
import { NodeCanvasFactory, NodeCMapReaderFactory } from "./node_utils.js";
|
||||
import {
|
||||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
NodeStandardFontDataFactory,
|
||||
} from "./node_utils.js";
|
||||
import { AnnotationStorage } from "./annotation_storage.js";
|
||||
import { apiCompatibilityParams } from "./api_compatibility.js";
|
||||
import { CanvasGraphics } from "./canvas.js";
|
||||
|
@ -69,6 +74,10 @@ const DefaultCMapReaderFactory =
|
|||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS
|
||||
? NodeCMapReaderFactory
|
||||
: DOMCMapReaderFactory;
|
||||
const DefaultStandardFontDataFactory =
|
||||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS
|
||||
? NodeStandardFontDataFactory
|
||||
: DOMStandardFontDataFactory;
|
||||
|
||||
/**
|
||||
* @typedef {function} IPDFStreamFactory
|
||||
|
@ -143,6 +152,19 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
|
|||
* reading built-in CMap files. Providing a custom factory is useful for
|
||||
* environments without Fetch API or `XMLHttpRequest` support, such as
|
||||
* Node.js. The default value is {DOMCMapReaderFactory}.
|
||||
* @property {boolean} [useSystemFonts] - When true, fonts that aren't embedded
|
||||
* in the PDF will fallback to a system font. Defaults to true for web
|
||||
* environments and false for node.
|
||||
* @property {string} [standardFontDataUrl] - The URL where the standard font
|
||||
* files are located. Include the trailing slash.
|
||||
* @property {boolean} [useWorkerFetch] - Enable using fetch in the worker for
|
||||
* resources. This currently only used for fetching the font data from the
|
||||
* worker thread. When `true`, StandardFontDataFactory will be ignored. The
|
||||
* default value is `true` in web environment and `false` for Node.
|
||||
* @property {Object} [StandardFontDataFactory] - The factory that will be used
|
||||
* when reading the standard font files. Providing a custom factory is useful
|
||||
* for environments without Fetch API or `XMLHttpRequest` support, such as
|
||||
* Node.js. The default value is {DOMStandardFontDataFactory}.
|
||||
* @property {boolean} [stopAtErrors] - Reject certain promises, e.g.
|
||||
* `getOperatorList`, `getTextContent`, and `RenderTask`, when the associated
|
||||
* PDF data cannot be successfully parsed, instead of attempting to recover
|
||||
|
@ -287,6 +309,8 @@ function getDocument(src) {
|
|||
params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE;
|
||||
params.CMapReaderFactory =
|
||||
params.CMapReaderFactory || DefaultCMapReaderFactory;
|
||||
params.StandardFontDataFactory =
|
||||
params.StandardFontDataFactory || DefaultStandardFontDataFactory;
|
||||
params.ignoreErrors = params.stopAtErrors !== true;
|
||||
params.fontExtraProperties = params.fontExtraProperties === true;
|
||||
params.pdfBug = params.pdfBug === true;
|
||||
|
@ -304,6 +328,13 @@ function getDocument(src) {
|
|||
if (!Number.isInteger(params.maxImageSize)) {
|
||||
params.maxImageSize = -1;
|
||||
}
|
||||
if (typeof params.useSystemFonts !== "boolean") {
|
||||
params.useSystemFonts = !isNodeJS;
|
||||
}
|
||||
if (typeof params.useWorkerFetch !== "boolean") {
|
||||
params.useWorkerFetch =
|
||||
params.StandardFontDataFactory === DOMStandardFontDataFactory;
|
||||
}
|
||||
if (typeof params.isEvalSupported !== "boolean") {
|
||||
params.isEvalSupported = true;
|
||||
}
|
||||
|
@ -455,6 +486,10 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
|
|||
isEvalSupported: source.isEvalSupported,
|
||||
fontExtraProperties: source.fontExtraProperties,
|
||||
enableXfa: source.enableXfa,
|
||||
useSystemFonts: source.useSystemFonts,
|
||||
standardFontDataUrl: source.useWorkerFetch
|
||||
? source.standardFontDataUrl
|
||||
: null,
|
||||
})
|
||||
.then(function (workerId) {
|
||||
if (worker.destroyed) {
|
||||
|
@ -2242,6 +2277,9 @@ class WorkerTransport {
|
|||
baseUrl: params.cMapUrl,
|
||||
isCompressed: params.cMapPacked,
|
||||
});
|
||||
this.StandardFontDataFactory = new params.StandardFontDataFactory({
|
||||
baseUrl: params.standardFontDataUrl,
|
||||
});
|
||||
|
||||
this.destroyed = false;
|
||||
this.destroyCapability = null;
|
||||
|
@ -2640,6 +2678,13 @@ class WorkerTransport {
|
|||
this._onUnsupportedFeature.bind(this)
|
||||
);
|
||||
|
||||
messageHandler.on("FetchStandardFontData", data => {
|
||||
if (this.destroyed) {
|
||||
return Promise.reject(new Error("Worker was destroyed"));
|
||||
}
|
||||
return this.StandardFontDataFactory.fetch(data);
|
||||
});
|
||||
|
||||
messageHandler.on("FetchBuiltInCMap", (data, sink) => {
|
||||
if (this.destroyed) {
|
||||
sink.error(new Error("Worker was destroyed"));
|
||||
|
@ -3182,6 +3227,7 @@ export {
|
|||
build,
|
||||
DefaultCanvasFactory,
|
||||
DefaultCMapReaderFactory,
|
||||
DefaultStandardFontDataFactory,
|
||||
getDocument,
|
||||
LoopbackPort,
|
||||
PDFDataRangeTransport,
|
||||
|
|
|
@ -84,6 +84,56 @@ class DOMCanvasFactory extends BaseCanvasFactory {
|
|||
}
|
||||
}
|
||||
|
||||
function fetchData(url, asTypedArray) {
|
||||
if (
|
||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||
(isFetchSupported() && isValidFetchUrl(url, document.baseURI))
|
||||
) {
|
||||
return fetch(url).then(async response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
let data;
|
||||
if (asTypedArray) {
|
||||
data = new Uint8Array(await response.arrayBuffer());
|
||||
} else {
|
||||
data = stringToBytes(await response.text());
|
||||
}
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
// The Fetch API is not supported.
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = new XMLHttpRequest();
|
||||
request.open("GET", url, /* asTypedArray = */ true);
|
||||
|
||||
if (asTypedArray) {
|
||||
request.responseType = "arraybuffer";
|
||||
}
|
||||
request.onreadystatechange = () => {
|
||||
if (request.readyState !== XMLHttpRequest.DONE) {
|
||||
return;
|
||||
}
|
||||
if (request.status === 200 || request.status === 0) {
|
||||
let data;
|
||||
if (asTypedArray && request.response) {
|
||||
data = new Uint8Array(request.response);
|
||||
} else if (!asTypedArray && request.responseText) {
|
||||
data = stringToBytes(request.responseText);
|
||||
}
|
||||
if (data) {
|
||||
resolve(data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
reject(new Error(request.statusText));
|
||||
};
|
||||
|
||||
request.send(null);
|
||||
});
|
||||
}
|
||||
|
||||
class BaseCMapReaderFactory {
|
||||
constructor({ baseUrl = null, isCompressed = false }) {
|
||||
if (this.constructor === BaseCMapReaderFactory) {
|
||||
|
@ -125,56 +175,44 @@ class BaseCMapReaderFactory {
|
|||
|
||||
class DOMCMapReaderFactory extends BaseCMapReaderFactory {
|
||||
_fetchData(url, compressionType) {
|
||||
if (
|
||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||
(isFetchSupported() && isValidFetchUrl(url, document.baseURI))
|
||||
) {
|
||||
return fetch(url).then(async response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
let cMapData;
|
||||
if (this.isCompressed) {
|
||||
cMapData = new Uint8Array(await response.arrayBuffer());
|
||||
} else {
|
||||
cMapData = stringToBytes(await response.text());
|
||||
}
|
||||
return { cMapData, compressionType };
|
||||
});
|
||||
}
|
||||
|
||||
// The Fetch API is not supported.
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = new XMLHttpRequest();
|
||||
request.open("GET", url, true);
|
||||
|
||||
if (this.isCompressed) {
|
||||
request.responseType = "arraybuffer";
|
||||
}
|
||||
request.onreadystatechange = () => {
|
||||
if (request.readyState !== XMLHttpRequest.DONE) {
|
||||
return;
|
||||
}
|
||||
if (request.status === 200 || request.status === 0) {
|
||||
let cMapData;
|
||||
if (this.isCompressed && request.response) {
|
||||
cMapData = new Uint8Array(request.response);
|
||||
} else if (!this.isCompressed && request.responseText) {
|
||||
cMapData = stringToBytes(request.responseText);
|
||||
}
|
||||
if (cMapData) {
|
||||
resolve({ cMapData, compressionType });
|
||||
return;
|
||||
}
|
||||
}
|
||||
reject(new Error(request.statusText));
|
||||
};
|
||||
|
||||
request.send(null);
|
||||
return fetchData(url, /* asTypedArray = */ this.isCompressed).then(data => {
|
||||
return { cMapData: data, compressionType };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class BaseStandardFontDataFactory {
|
||||
constructor({ baseUrl = null }) {
|
||||
if (this.constructor === BaseStandardFontDataFactory) {
|
||||
unreachable("Cannot initialize BaseStandardFontDataFactory.");
|
||||
}
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
async fetch({ filename }) {
|
||||
if (!this.baseUrl) {
|
||||
throw new Error(
|
||||
'The standard font "baseUrl" parameter must be specified, ensure that ' +
|
||||
'the "standardFontDataUrl" API parameter is provided.'
|
||||
);
|
||||
}
|
||||
if (!filename) {
|
||||
throw new Error("Font filename must be specified.");
|
||||
}
|
||||
const url = this.baseUrl + filename + ".pfb";
|
||||
|
||||
return this._fetchData(url).catch(reason => {
|
||||
throw new Error(`Unable to load font data at: ${url}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
|
||||
_fetchData(url) {
|
||||
return fetchData(url, /* asTypedArray = */ true);
|
||||
}
|
||||
}
|
||||
|
||||
class DOMSVGFactory {
|
||||
create(width, height) {
|
||||
assert(width > 0 && height > 0, "Invalid SVG dimensions");
|
||||
|
@ -704,10 +742,12 @@ export {
|
|||
addLinkAttributes,
|
||||
BaseCanvasFactory,
|
||||
BaseCMapReaderFactory,
|
||||
BaseStandardFontDataFactory,
|
||||
DEFAULT_LINK_REL,
|
||||
deprecated,
|
||||
DOMCanvasFactory,
|
||||
DOMCMapReaderFactory,
|
||||
DOMStandardFontDataFactory,
|
||||
DOMSVGFactory,
|
||||
getFilenameFromUrl,
|
||||
getPdfFilenameFromUrl,
|
||||
|
|
|
@ -14,10 +14,27 @@
|
|||
*/
|
||||
/* globals __non_webpack_require__ */
|
||||
|
||||
import { BaseCanvasFactory, BaseCMapReaderFactory } from "./display_utils.js";
|
||||
import {
|
||||
BaseCanvasFactory,
|
||||
BaseCMapReaderFactory,
|
||||
BaseStandardFontDataFactory,
|
||||
} from "./display_utils.js";
|
||||
import { isNodeJS } from "../shared/is_node.js";
|
||||
import { unreachable } from "../shared/util.js";
|
||||
|
||||
function fetchData(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fs = __non_webpack_require__("fs");
|
||||
fs.readFile(url, (error, data) => {
|
||||
if (error || !data) {
|
||||
reject(new Error(error));
|
||||
return;
|
||||
}
|
||||
resolve(new Uint8Array(data));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
let NodeCanvasFactory = class {
|
||||
constructor() {
|
||||
unreachable("Not implemented: NodeCanvasFactory");
|
||||
|
@ -30,6 +47,12 @@ let NodeCMapReaderFactory = class {
|
|||
}
|
||||
};
|
||||
|
||||
let NodeStandardFontDataFactory = class {
|
||||
constructor() {
|
||||
unreachable("Not implemented: NodeStandardFontDataFactory");
|
||||
}
|
||||
};
|
||||
|
||||
if ((typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS) {
|
||||
NodeCanvasFactory = class extends BaseCanvasFactory {
|
||||
create(width, height) {
|
||||
|
@ -47,18 +70,21 @@ if ((typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS) {
|
|||
|
||||
NodeCMapReaderFactory = class extends BaseCMapReaderFactory {
|
||||
_fetchData(url, compressionType) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fs = __non_webpack_require__("fs");
|
||||
fs.readFile(url, (error, data) => {
|
||||
if (error || !data) {
|
||||
reject(new Error(error));
|
||||
return;
|
||||
}
|
||||
resolve({ cMapData: new Uint8Array(data), compressionType });
|
||||
});
|
||||
return fetchData(url).then(data => {
|
||||
return { cMapData: data, compressionType };
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
NodeStandardFontDataFactory = class extends BaseStandardFontDataFactory {
|
||||
_fetchData(url) {
|
||||
return fetchData(url);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { NodeCanvasFactory, NodeCMapReaderFactory };
|
||||
export {
|
||||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
NodeStandardFontDataFactory,
|
||||
};
|
||||
|
|
|
@ -190,6 +190,7 @@ const StreamType = {
|
|||
const FontType = {
|
||||
UNKNOWN: "UNKNOWN",
|
||||
TYPE1: "TYPE1",
|
||||
TYPE1STANDARD: "TYPE1STANDARD",
|
||||
TYPE1C: "TYPE1C",
|
||||
CIDFONTTYPE0: "CIDFONTTYPE0",
|
||||
CIDFONTTYPE0C: "CIDFONTTYPE0C",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue