-
-
Notifications
You must be signed in to change notification settings - Fork 17
webcanvas: added FontProvider for pluggable font backends #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * Font provider abstraction for pluggable font sources. | ||
| * @category Font | ||
| */ | ||
|
|
||
| import type { FontType } from './Font'; | ||
|
|
||
| /** | ||
| * Result returned by a {@link FontProvider} after fetching font data. | ||
| * @category Font | ||
| */ | ||
| export interface FontProviderResult { | ||
| /** Raw font binary data */ | ||
| data: Uint8Array; | ||
| /** Font format */ | ||
| type: FontType; | ||
| } | ||
|
|
||
| /** | ||
| * Interface for pluggable font sources. | ||
| * | ||
| * A font provider resolves a font name into binary font data. | ||
| * Implement this interface to load fonts from any source — a self-hosted | ||
| * CDN, a local server, or any custom storage. | ||
| * | ||
| * @category Font | ||
| * @beta | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * TVG.Font.provider({ | ||
| * fetch: async (name) => { | ||
| * const res = await fetch(`/my-fonts/${name}.ttf`); | ||
| * return { data: new Uint8Array(await res.arrayBuffer()), type: 'ttf' }; | ||
| * } | ||
| * }); | ||
| * | ||
| * await TVG.Font.load('my-font'); | ||
| * ``` | ||
| */ | ||
| export interface FontProvider { | ||
| /** | ||
| * Fetch font data by name. | ||
| * @param name - Font name as provided by the caller | ||
| * @param options - Provider-specific options | ||
| * @returns Font binary data and format | ||
| */ | ||
| fetch(name: string, options?: Record<string, unknown>): Promise<FontProviderResult>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * Fontsource CDN font provider. | ||
| * @category Font | ||
| */ | ||
|
|
||
| import type { FontProvider, FontProviderResult } from '../core/FontProvider'; | ||
|
|
||
| const FONTSOURCE_CDN = 'https://cdn.jsdelivr.net/fontsource/fonts'; | ||
|
|
||
| /** | ||
| * Options for loading a font from the fontsource CDN. | ||
| * @category Font | ||
| */ | ||
| export interface FontsourceOptions { | ||
| /** | ||
| * Font weight to load. | ||
| * @default 400 | ||
| */ | ||
| weight?: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900; | ||
| /** | ||
| * Font style to load. | ||
| * @default 'normal' | ||
| */ | ||
| style?: 'normal' | 'italic'; | ||
| /** | ||
| * Unicode subset to load. | ||
| * @default 'latin' | ||
| */ | ||
| subset?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Font provider that fetches fonts from the [fontsource](https://fontsource.org) CDN. | ||
| * | ||
| * This is the default provider used by {@link Font.load} when no raw data is supplied. | ||
| * | ||
| * @category Font | ||
| * @beta | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // Restore to default (if you previously swapped it out) | ||
| * TVG.Font.provider(new FontsourceProvider()); | ||
| * ``` | ||
| */ | ||
| export class FontsourceProvider implements FontProvider { | ||
| async fetch(name: string, options?: FontsourceOptions): Promise<FontProviderResult> { | ||
| const opts = { | ||
| weight: 400 as const, | ||
| style: 'normal' as const, | ||
| subset: 'latin', | ||
| ...options, | ||
| }; | ||
|
|
||
| const slug = name.toLowerCase().replace(/\s+/g, '-'); | ||
| const url = `${FONTSOURCE_CDN}/${slug}@latest/${opts.subset}-${opts.weight}-${opts.style}.ttf`; | ||
|
|
||
| const res = await fetch(url); | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `Font "${name}" could not be loaded from fontsource (HTTP ${res.status}). ` + | ||
| `Check that the font exists at https://fontsource.org/fonts/${slug}`, | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| data: new Uint8Array(await res.arrayBuffer()), | ||
| type: 'ttf', | ||
| }; | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.