Skip to content
Open
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
18 changes: 13 additions & 5 deletions packages/font/src/font-source.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import isUrl from 'is-url';
import * as fontkit from 'fontkit';

import { Font, FontSourceOptions, FontStyle, RemoteOptions } from './types';
import {
BufferFontSource,
Font,
FontSourceOptions,
FontStyle,
RemoteOptions,
} from './types';
import StandardFont, { STANDARD_FONTS } from './standard-font';

const fetchFont = async (src: string, options: RemoteOptions) => {
Expand Down Expand Up @@ -30,7 +36,7 @@ const isDataUrl = (dataUrl: string) => {
};

class FontSource {
src: string;
src: string | BufferFontSource;
fontFamily: string;
fontStyle: FontStyle;
fontWeight: number;
Expand All @@ -39,7 +45,7 @@ class FontSource {
loadResultPromise: Promise<void> | null;

constructor(
src: string,
src: string | BufferFontSource,
fontFamily: string,
fontStyle?: FontStyle,
fontWeight?: number,
Expand All @@ -60,7 +66,9 @@ class FontSource {

let data = null;

if (STANDARD_FONTS.includes(this.src)) {
if (typeof this.src !== 'string') {
data = fontkit.create(this.src.buffer, postscriptName);
} else if (STANDARD_FONTS.includes(this.src)) {
data = new StandardFont(this.src);
} else if (isDataUrl(this.src)) {
const raw = this.src.split(',')[1];
Expand All @@ -70,7 +78,7 @@ class FontSource {
.map((c) => c.charCodeAt(0)),
);
data = fontkit.create(uint8Array, postscriptName);
} else if (BROWSER || isUrl(this.src)) {
} else if (BROWSER || isUrl(this.src) || this.src.startsWith('blob:')) {
const { headers, body, method = 'GET' } = this.options;
const buffer = await fetchFont(this.src, { method, body, headers });
data = fontkit.create(buffer, postscriptName);
Expand Down
6 changes: 5 additions & 1 deletion packages/font/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export type FontSourceOptions = {
postscriptName?: string;
} & RemoteOptions;

export type BufferFontSource = {
buffer: Buffer;
};

export type FontSource = {
src: string;
src: string | BufferFontSource;
fontStyle?: FontStyle;
fontWeight?: FontWeight;
} & FontSourceOptions;
Expand Down
43 changes: 43 additions & 0 deletions packages/font/tests/font-source.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { readFile } from 'node:fs/promises';

import { beforeEach, describe, expect, it, vi } from 'vitest';

import FontStore from '../src/index';
import FontSource from '../src/font-source';

const fontPath = new URL('../../layout/tests/assets/font.ttf', import.meta.url);

describe('FontSource', () => {
beforeEach(() => {
vi.stubGlobal('BROWSER', false);
});

it('loads blob URLs', async () => {
const font = await readFile(fontPath);
const src = URL.createObjectURL(new Blob([font], { type: 'font/ttf' }));

try {
const source = new FontSource(src, 'BlobFont');

await source.load();

expect(source.data).not.toBeNull();
} finally {
URL.revokeObjectURL(src);
}
});

it('loads buffer sources registered through FontStore', async () => {
const buffer = await readFile(fontPath);
const fontStore = new FontStore();

fontStore.register({
family: 'BufferFont',
src: { buffer },
});

await fontStore.load({ fontFamily: 'BufferFont' });

expect(fontStore.getFont({ fontFamily: 'BufferFont' }).data).not.toBeNull();
});
});