2017-02-17 21:44:49 +09:00
|
|
|
/* Copyright 2017 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 18:46:02 +09:00
|
|
|
import { NullStream, StringStream } from "../../src/core/stream.js";
|
Re-factor the `idFactory` functionality, used in the `core/`-code, and move the `fontID` generation into it
Note how the `getFontID`-method in `src/core/fonts.js` is *completely* global, rather than properly tied to the current document. This means that if you repeatedly open and parse/render, and then close, even the *same* PDF document the `fontID`s will still be incremented continuously.
For comparison the `createObjId` method, on `idFactory`, will always create a *consistent* id, assuming of course that the document and its pages are parsed/rendered in the same order.
In order to address this inconsistency, it thus seems reasonable to add a new `createFontId` method on the `idFactory` and use that when obtaining `fontID`s. (When the current `getFontID` method was added the `idFactory` didn't actually exist yet, which explains why the code looks the way it does.)
*Please note:* Since the document id is (still) part of the `loadedName`, it's thus not possible for different documents to have identical font names.
2020-07-07 23:00:05 +09:00
|
|
|
import { Page, PDFDocument } from "../../src/core/document.js";
|
2023-07-17 23:33:06 +09:00
|
|
|
import { isNodeJS } from "../../src/shared/util.js";
|
2022-02-18 20:11:45 +09:00
|
|
|
import { Ref } from "../../src/core/primitives.js";
|
2017-02-17 21:44:49 +09:00
|
|
|
|
2023-10-13 19:11:29 +09:00
|
|
|
let fs;
|
|
|
|
if (isNodeJS) {
|
|
|
|
// Native packages.
|
|
|
|
fs = await __non_webpack_import__("fs");
|
|
|
|
}
|
|
|
|
|
2021-01-09 01:12:58 +09:00
|
|
|
const TEST_PDFS_PATH = isNodeJS ? "./test/pdfs/" : "../pdfs/";
|
|
|
|
|
2023-01-30 22:12:06 +09:00
|
|
|
const CMAP_URL = isNodeJS ? "./external/bcmaps/" : "../../external/bcmaps/";
|
2021-01-09 01:12:58 +09:00
|
|
|
|
2020-12-11 10:32:18 +09:00
|
|
|
const STANDARD_FONT_DATA_URL = isNodeJS
|
|
|
|
? "./external/standard_fonts/"
|
|
|
|
: "../../external/standard_fonts/";
|
|
|
|
|
2019-02-17 20:34:37 +09:00
|
|
|
class DOMFileReaderFactory {
|
|
|
|
static async fetch(params) {
|
|
|
|
const response = await fetch(params.path);
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(response.statusText);
|
|
|
|
}
|
|
|
|
return new Uint8Array(await response.arrayBuffer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-09 00:09:54 +09:00
|
|
|
class NodeFileReaderFactory {
|
2019-02-17 20:34:37 +09:00
|
|
|
static async fetch(params) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.readFile(params.path, (error, data) => {
|
|
|
|
if (error || !data) {
|
|
|
|
reject(error || new Error(`Empty file for: ${params.path}`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(new Uint8Array(data));
|
|
|
|
});
|
|
|
|
});
|
2017-04-09 00:09:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 01:12:58 +09:00
|
|
|
const DefaultFileReaderFactory = isNodeJS
|
|
|
|
? NodeFileReaderFactory
|
|
|
|
: DOMFileReaderFactory;
|
2017-05-16 20:01:03 +09:00
|
|
|
|
|
|
|
function buildGetDocumentParams(filename, options) {
|
2020-01-24 17:48:21 +09:00
|
|
|
const params = Object.create(null);
|
2021-01-09 01:12:58 +09:00
|
|
|
params.url = isNodeJS
|
|
|
|
? TEST_PDFS_PATH + filename
|
|
|
|
: new URL(TEST_PDFS_PATH + filename, window.location).href;
|
2020-12-11 10:32:18 +09:00
|
|
|
params.standardFontDataUrl = STANDARD_FONT_DATA_URL;
|
2021-01-09 01:12:58 +09:00
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
for (const option in options) {
|
2017-05-16 20:01:03 +09:00
|
|
|
params[option] = options[option];
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2017-07-29 07:35:10 +09:00
|
|
|
class XRefMock {
|
|
|
|
constructor(array) {
|
|
|
|
this._map = Object.create(null);
|
2022-10-19 00:07:47 +09:00
|
|
|
this._newTemporaryRefNum = null;
|
|
|
|
this._newPersistentRefNum = null;
|
2022-06-22 18:46:02 +09:00
|
|
|
this.stream = new NullStream();
|
2017-07-29 07:35:10 +09:00
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
for (const key in array) {
|
|
|
|
const obj = array[key];
|
2017-07-29 07:35:10 +09:00
|
|
|
this._map[obj.ref.toString()] = obj.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 00:07:47 +09:00
|
|
|
getNewPersistentRef(obj) {
|
|
|
|
if (this._newPersistentRefNum === null) {
|
|
|
|
this._newPersistentRefNum = Object.keys(this._map).length || 1;
|
2020-08-04 02:44:04 +09:00
|
|
|
}
|
2022-10-19 00:07:47 +09:00
|
|
|
const ref = Ref.get(this._newPersistentRefNum++, 0);
|
|
|
|
this._map[ref.toString()] = obj;
|
|
|
|
return ref;
|
2020-08-04 02:44:04 +09:00
|
|
|
}
|
|
|
|
|
2022-10-19 00:07:47 +09:00
|
|
|
getNewTemporaryRef() {
|
|
|
|
if (this._newTemporaryRefNum === null) {
|
|
|
|
this._newTemporaryRefNum = Object.keys(this._map).length || 1;
|
|
|
|
}
|
|
|
|
return Ref.get(this._newTemporaryRefNum++, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
resetNewTemporaryRef() {
|
|
|
|
this._newTemporaryRefNum = null;
|
2020-08-04 02:44:04 +09:00
|
|
|
}
|
|
|
|
|
2017-07-29 07:35:10 +09:00
|
|
|
fetch(ref) {
|
|
|
|
return this._map[ref.toString()];
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:20:27 +09:00
|
|
|
async fetchAsync(ref) {
|
|
|
|
return this.fetch(ref);
|
2017-07-29 07:35:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
fetchIfRef(obj) {
|
2022-02-18 20:11:45 +09:00
|
|
|
if (obj instanceof Ref) {
|
|
|
|
return this.fetch(obj);
|
2017-07-29 07:35:10 +09:00
|
|
|
}
|
2022-02-18 20:11:45 +09:00
|
|
|
return obj;
|
2017-07-29 07:35:10 +09:00
|
|
|
}
|
|
|
|
|
2020-10-15 20:20:27 +09:00
|
|
|
async fetchIfRefAsync(obj) {
|
|
|
|
return this.fetchIfRef(obj);
|
2017-07-29 07:35:10 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 19:36:49 +09:00
|
|
|
function createIdFactory(pageIndex) {
|
Re-factor the `idFactory` functionality, used in the `core/`-code, and move the `fontID` generation into it
Note how the `getFontID`-method in `src/core/fonts.js` is *completely* global, rather than properly tied to the current document. This means that if you repeatedly open and parse/render, and then close, even the *same* PDF document the `fontID`s will still be incremented continuously.
For comparison the `createObjId` method, on `idFactory`, will always create a *consistent* id, assuming of course that the document and its pages are parsed/rendered in the same order.
In order to address this inconsistency, it thus seems reasonable to add a new `createFontId` method on the `idFactory` and use that when obtaining `fontID`s. (When the current `getFontID` method was added the `idFactory` didn't actually exist yet, which explains why the code looks the way it does.)
*Please note:* Since the document id is (still) part of the `loadedName`, it's thus not possible for different documents to have identical font names.
2020-07-07 23:00:05 +09:00
|
|
|
const pdfManager = {
|
|
|
|
get docId() {
|
|
|
|
return "d0";
|
2019-04-20 19:36:49 +09:00
|
|
|
},
|
Re-factor the `idFactory` functionality, used in the `core/`-code, and move the `fontID` generation into it
Note how the `getFontID`-method in `src/core/fonts.js` is *completely* global, rather than properly tied to the current document. This means that if you repeatedly open and parse/render, and then close, even the *same* PDF document the `fontID`s will still be incremented continuously.
For comparison the `createObjId` method, on `idFactory`, will always create a *consistent* id, assuming of course that the document and its pages are parsed/rendered in the same order.
In order to address this inconsistency, it thus seems reasonable to add a new `createFontId` method on the `idFactory` and use that when obtaining `fontID`s. (When the current `getFontID` method was added the `idFactory` didn't actually exist yet, which explains why the code looks the way it does.)
*Please note:* Since the document id is (still) part of the `loadedName`, it's thus not possible for different documents to have identical font names.
2020-07-07 23:00:05 +09:00
|
|
|
};
|
|
|
|
const stream = new StringStream("Dummy_PDF_data");
|
|
|
|
const pdfDocument = new PDFDocument(pdfManager, stream);
|
|
|
|
|
|
|
|
const page = new Page({
|
|
|
|
pdfManager: pdfDocument.pdfManager,
|
|
|
|
xref: pdfDocument.xref,
|
2019-04-20 19:36:49 +09:00
|
|
|
pageIndex,
|
Re-factor the `idFactory` functionality, used in the `core/`-code, and move the `fontID` generation into it
Note how the `getFontID`-method in `src/core/fonts.js` is *completely* global, rather than properly tied to the current document. This means that if you repeatedly open and parse/render, and then close, even the *same* PDF document the `fontID`s will still be incremented continuously.
For comparison the `createObjId` method, on `idFactory`, will always create a *consistent* id, assuming of course that the document and its pages are parsed/rendered in the same order.
In order to address this inconsistency, it thus seems reasonable to add a new `createFontId` method on the `idFactory` and use that when obtaining `fontID`s. (When the current `getFontID` method was added the `idFactory` didn't actually exist yet, which explains why the code looks the way it does.)
*Please note:* Since the document id is (still) part of the `loadedName`, it's thus not possible for different documents to have identical font names.
2020-07-07 23:00:05 +09:00
|
|
|
globalIdFactory: pdfDocument._globalIdFactory,
|
2019-04-20 19:36:49 +09:00
|
|
|
});
|
Re-factor the `idFactory` functionality, used in the `core/`-code, and move the `fontID` generation into it
Note how the `getFontID`-method in `src/core/fonts.js` is *completely* global, rather than properly tied to the current document. This means that if you repeatedly open and parse/render, and then close, even the *same* PDF document the `fontID`s will still be incremented continuously.
For comparison the `createObjId` method, on `idFactory`, will always create a *consistent* id, assuming of course that the document and its pages are parsed/rendered in the same order.
In order to address this inconsistency, it thus seems reasonable to add a new `createFontId` method on the `idFactory` and use that when obtaining `fontID`s. (When the current `getFontID` method was added the `idFactory` didn't actually exist yet, which explains why the code looks the way it does.)
*Please note:* Since the document id is (still) part of the `loadedName`, it's thus not possible for different documents to have identical font names.
2020-07-07 23:00:05 +09:00
|
|
|
return page._localIdFactory;
|
2019-04-20 19:36:49 +09:00
|
|
|
}
|
|
|
|
|
2023-10-29 22:21:13 +09:00
|
|
|
function getNodeVersion() {
|
|
|
|
if (!isNodeJS) {
|
|
|
|
throw new Error("getNodeVersion - only valid in Node.js environments.");
|
|
|
|
}
|
|
|
|
const [major, minor, patch] = process.versions.node
|
|
|
|
.split(".")
|
|
|
|
.map(parseFloat);
|
|
|
|
return { major, minor, patch };
|
|
|
|
}
|
|
|
|
|
2017-04-17 05:30:27 +09:00
|
|
|
export {
|
2017-05-16 20:01:03 +09:00
|
|
|
buildGetDocumentParams,
|
2023-01-30 22:12:06 +09:00
|
|
|
CMAP_URL,
|
2019-04-20 19:36:49 +09:00
|
|
|
createIdFactory,
|
2021-01-09 23:37:44 +09:00
|
|
|
DefaultFileReaderFactory,
|
2023-10-29 22:21:13 +09:00
|
|
|
getNodeVersion,
|
2020-12-11 10:32:18 +09:00
|
|
|
STANDARD_FONT_DATA_URL,
|
2021-01-09 23:37:44 +09:00
|
|
|
TEST_PDFS_PATH,
|
|
|
|
XRefMock,
|
2017-04-17 05:30:27 +09:00
|
|
|
};
|