FFFF Load font paths from disk in Bun by hipstersmoothie · Pull Request #1576 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions plugins/plugin-print/src/load-bitmap-font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import png from "@jimp/js-png";
import { createJimp } from "@jimp/core";
import path from "path";
import xmlPackage from "simple-xml-to-json";
import { isWebWorker } from "./runtime.js";

const { convertXML } = xmlPackage;

export const isWebWorker =
typeof self !== "undefined" && self.document === undefined;

const CharacterJimp = createJimp({ formats: [png] });
const HEADER = Buffer.from([66, 77, 70, 3]);

Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin-print/src/load-font.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
isWebWorker,
loadBitmapFontData,
processBitmapFont,
resolvePagePath,
} from "./load-bitmap-font.js";
import { isWebWorker } from "./runtime.js";

// Strings Node can only load with fetch
const URL_LIKE = /^(https?|data|blob):/i;
Expand Down
3 changes: 1 addition & 2 deletions plugins/plugin-print/src/load-ttf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { gposKerning, readTable } from "./kerning.js";
import type { OpenTypeFont } from "./opentype.js";
import { isWebWorker } from "./runtime.js";

/**
* A TrueType, OpenType or WOFF font loaded with {@link loadTTF}, for
Expand All @@ -22,8 +23,6 @@ export interface VectorFont {
/** What {@link loadTTF} can load a font from */
export type FontSource = string | URL | ArrayBuffer | ArrayBufferView;

const isWebWorker = typeof self !== "undefined" && self.document === undefined;

// Strings Node can only load with fetch
const URL_LIKE = /^(https?|data|blob):/i;

Expand Down
48 changes: 48 additions & 0 deletions plugins/plugin-print/src/runtime.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { afterEach, describe, expect, test, vi } from "vitest";
import path from "path";
import { fileURLToPath } from "url";

const fixture = (name: string) =>
path.join(path.dirname(fileURLToPath(import.meta.url)), "../fixtures", name);

// Bun and Deno define `self` on their main thread, without a `document`,
// which used to look like a web worker: the loaders fetched paths instead of
// reading them, and fetch can't load a path.
describe("in a runtime with a global self, like Bun and Deno", () => {
async function load() {
vi.stubGlobal("self", globalThis);
vi.resetModules();

return {
runtime: await import("./runtime.js"),
ttf: await import("./load-ttf.js"),
bitmap: await import("./load-font.js"),
fonts: await import("./fonts.js"),
};
}

afterEach(() => {
vi.unstubAllGlobals();
vi.resetModules();
});

test("isn't a web worker", async () => {
const { runtime } = await load();

expect(runtime.isWebWorker).toBe(false);
});

test("loadTTF reads a path from disk", async () => {
const { ttf } = await load();
const font = await ttf.loadTTF(fixture("Inter-Regular.otf"));

expect(font.opentype.unitsPerEm).toBeGreaterThan(0);
});

test("loadFont reads a path from disk", async () => {
const { bitmap, fonts } = await load();
const font = await bitmap.loadFont(fonts.SANS_16_BLACK);

expect(font.chars.A).toBeDefined();
});
});
14 changes: 14 additions & 0 deletions plugins/plugin-print/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { WorkerGlobalScope } = globalThis as {
WorkerGlobalScope?: abstract new () => unknown;
};

/**
* Whether this is a browser's web worker, which loads fonts with `fetch`.
*
* It checks for `WorkerGlobalScope` rather than a `self` without a
* `document`: Bun and Deno define `self` on their main thread too, and they
* read font files from disk like Node.js.
*/
export const isWebWorker =
typeof WorkerGlobalScope === "function" &&
globalThis instanceof WorkerGlobalScope;
Loading
0