1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Allow loading pdf fonts into another document.

This commit is contained in:
Jonathan Grimes 2020-07-27 17:22:45 +00:00
parent fea40442f7
commit ac723a1760
5 changed files with 166 additions and 19 deletions

View file

@ -107,3 +107,127 @@ describe("custom canvas rendering", function () {
.catch(done.fail);
});
});
describe("custom ownerDocument", function () {
const FontFace = globalThis.FontFace;
const checkFont = font => /g_d\d+_f1/.test(font.family);
const checkFontFaceRule = rule =>
/^@font-face {font-family:"g_d\d+_f1";src:/.test(rule);
beforeEach(() => {
globalThis.FontFace = function MockFontFace(name) {
this.family = name;
};
});
afterEach(() => {
globalThis.FontFace = FontFace;
});
function getMocks() {
const elements = [];
const createElement = name => {
let element =
typeof document !== "undefined" && document.createElement(name);
if (name === "style") {
element = {
tagName: name,
sheet: {
cssRules: [],
insertRule(rule) {
this.cssRules.push(rule);
},
},
};
Object.assign(element, {
remove() {
this.remove.called = true;
},
});
}
elements.push(element);
return element;
};
const ownerDocument = {
fonts: new Set(),
createElement,
documentElement: {
getElementsByTagName: () => [{ appendChild: () => {} }],
},
};
const CanvasFactory = isNodeJS
? new NodeCanvasFactory()
: new DOMCanvasFactory({ ownerDocument });
return {
elements,
ownerDocument,
CanvasFactory,
};
}
it("should use given document for loading fonts (with Font Loading API)", async function () {
const { ownerDocument, elements, CanvasFactory } = getMocks();
const getDocumentParams = buildGetDocumentParams(
"TrueType_without_cmap.pdf",
{
disableFontFace: false,
ownerDocument,
}
);
const loadingTask = getDocument(getDocumentParams);
const doc = await loadingTask.promise;
const page = await doc.getPage(1);
const viewport = page.getViewport({ scale: 1 });
const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
await page.render({
canvasContext: canvasAndCtx.context,
viewport,
}).promise;
const style = elements.find(element => element.tagName === "style");
expect(style).toBeFalsy();
expect(ownerDocument.fonts.size).toBeGreaterThanOrEqual(1);
expect(Array.from(ownerDocument.fonts).find(checkFont)).toBeTruthy();
await doc.destroy();
await loadingTask.destroy();
CanvasFactory.destroy(canvasAndCtx);
expect(ownerDocument.fonts.size).toBe(0);
});
it("should use given document for loading fonts (with CSS rules)", async function () {
const { ownerDocument, elements, CanvasFactory } = getMocks();
ownerDocument.fonts = null;
const getDocumentParams = buildGetDocumentParams(
"TrueType_without_cmap.pdf",
{
disableFontFace: false,
ownerDocument,
}
);
const loadingTask = getDocument(getDocumentParams);
const doc = await loadingTask.promise;
const page = await doc.getPage(1);
const viewport = page.getViewport({ scale: 1 });
const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
await page.render({
canvasContext: canvasAndCtx.context,
viewport,
}).promise;
const style = elements.find(element => element.tagName === "style");
expect(style.sheet.cssRules.length).toBeGreaterThanOrEqual(1);
expect(style.sheet.cssRules.find(checkFontFaceRule)).toBeTruthy();
await doc.destroy();
await loadingTask.destroy();
CanvasFactory.destroy(canvasAndCtx);
expect(style.remove.called).toBe(true);
});
});