Skip to content

webcanvas: added FontProvider for pluggable font backends#256

Merged
tinyjin merged 2 commits intomainfrom
jinny/font-provider
Mar 30, 2026
Merged

webcanvas: added FontProvider for pluggable font backends#256
tinyjin merged 2 commits intomainfrom
jinny/font-provider

Conversation

@tinyjin
Copy link
Copy Markdown
Member

@tinyjin tinyjin commented Mar 18, 2026

CleanShot 2026-03-30 at 12 38 47@2x
  • Remove FontRegistry to ensure ThorVG manages font loading status
  • Added new beta API (TVG.setFontProvider) to set user own backend (default: fontsource)

API addtion & modification

// addition
+ Font.setProvider(provider: FontProvider): void

// modification (option parameter is decoupled from specific vendor)
- Font.load(name: string, options?: FontsourceOptions): Promise<void>
+ Font.load(name: string, options?: Record<string, unknown>): Promise<void>

Example

import { FontsourceProvider } from '@thorvg/webcanvas';

// Connect to own backend
TVG.Font.setProvider({
    fetch: async (name, options) => {
      const res = await fetch(`https://my-cdn.com/fonts/${name}.ttf`);
      return { data: new Uint8Array(await res.arrayBuffer()), type: 'ttf' };
    }
});

// Load from font provider
await TVG.Font.load('my-font');

// Reset to default provider
TVG.Font.setProvider(new FontsourceProvider());

issue: #242

@tinyjin tinyjin self-assigned this Mar 18, 2026
@tinyjin tinyjin added APIs Update / Revise APIs webcanvas WebCanvas labels Mar 18, 2026
@tinyjin tinyjin force-pushed the jinny/font-provider branch from ef694fe to e7abb9d Compare March 18, 2026 05:40
@tinyjin tinyjin force-pushed the jinny/font-provider branch from e7abb9d to 2493a24 Compare March 18, 2026 06:09
@github-actions
Copy link
Copy Markdown

🚀 Playground preview deployment ready!

🎮 Playground: https://thorvg-playground-hw4nonix9-thorvg-web.vercel.app

@tinyjin tinyjin marked this pull request as ready for review March 19, 2026 10:07
@tinyjin tinyjin requested a review from hermet as a code owner March 19, 2026 10:07
Copilot AI review requested due to automatic review settings March 19, 2026 10:07
@tinyjin
Copy link
Copy Markdown
Member Author

tinyjin commented Mar 19, 2026

TVG.Font.load API is necessarily called, not to make Text API (font) async call(Promise)

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a pluggable FontProvider abstraction for @thorvg/webcanvas so Font.load() can auto-fetch font binaries via a configurable backend (defaulting to a new FontsourceProvider), and removes the old FontRegistry-based tracking.

Changes:

  • Added FontProvider / FontProviderResult interfaces and a default FontsourceProvider implementation.
  • Added Font.setProvider() and updated Font.load(name, options?) to use the configured provider (options now vendor-agnostic).
  • Updated package exports/types accordingly and removed the old FontRegistry utility.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/webcanvas/src/utils/FontRegistry.ts Removed previous fontsource-fetch dedup/registry helper.
packages/webcanvas/src/core/FontProvider.ts Added provider interface for pluggable font fetching backends.
packages/webcanvas/src/providers/FontsourceProvider.ts Added default provider that fetches fonts from the fontsource CDN.
packages/webcanvas/src/core/Font.ts Switched Font.load() to provider-based fetching and added Font.setProvider().
packages/webcanvas/src/index.ts Exported the new provider/types and re-exported FontsourceOptions from the provider module.
packages/webcanvas/src/core/Text.ts Minor JSDoc update for Text.font().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Copy Markdown

🚀 Playground preview deployment ready!

🎮 Playground: https://thorvg-playground-riha4e015-thorvg-web.vercel.app

@hermet
Copy link
Copy Markdown
Member

hermet commented Mar 26, 2026

@tinyjin Please arrange the apis and then let's push this.

Copilot AI review requested due to automatic review settings March 26, 2026 10:13
@tinyjin tinyjin force-pushed the jinny/font-provider branch from 1028316 to ea16259 Compare March 26, 2026 10:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a pluggable FontProvider abstraction for @thorvg/webcanvas so Font.load(name, options?) can auto-fetch font binaries via a configurable backend (defaulting to a new FontsourceProvider), and removes the old FontRegistry tracking.

Changes:

  • Added FontProvider / FontProviderResult types and a default FontsourceProvider implementation.
  • Updated Font.load(name, options?) to delegate font fetching to the configured provider and decoupled options from the fontsource-specific type.
  • Updated public exports to expose FontsourceProvider, FontsourceOptions, and the provider interfaces.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/webcanvas/src/utils/FontRegistry.ts Removed prior fetch-state dedup helper used for fontsource auto-loading.
packages/webcanvas/src/providers/FontsourceProvider.ts Added default provider that fetches .ttf files from the fontsource CDN.
packages/webcanvas/src/core/FontProvider.ts Added provider interface + result type for pluggable font sources.
packages/webcanvas/src/core/Font.ts Switched auto-load logic to use the provider abstraction and added caching/dedup state.
packages/webcanvas/src/core/Text.ts Updated Text.font() JSDoc wording.
packages/webcanvas/src/index.ts Exported the provider class and related public types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Copy Markdown

🚀 Playground preview deployment ready!

🎮 Playground: https://thorvg-playground-ms8jnwx8k-thorvg-web.vercel.app

@tinyjin tinyjin requested a review from hermet March 30, 2026 03:39
@tinyjin tinyjin merged commit 8756053 into main Mar 30, 2026
6 checks passed
@tinyjin tinyjin deleted the jinny/font-provider branch March 30, 2026 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

APIs Update / Revise APIs webcanvas WebCanvas

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants