Skip to content

webcanvas: added FontProvider for pluggable font backends#256

Open
tinyjin wants to merge 1 commit intomainfrom
jinny/font-provider
Open

webcanvas: added FontProvider for pluggable font backends#256
tinyjin wants to merge 1 commit intomainfrom
jinny/font-provider

Conversation

@tinyjin
Copy link
Member

@tinyjin tinyjin commented Mar 18, 2026

CleanShot 2026-03-19 at 19 02 08@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) => {
      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

🚀 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
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

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.

@tinyjin tinyjin force-pushed the jinny/font-provider branch from 2493a24 to 1028316 Compare March 20, 2026 06:35
@tinyjin tinyjin deployed to external March 20, 2026 06:35 — with GitHub Actions Active
@github-actions
Copy link

🚀 Playground preview deployment ready!

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

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.

2 participants