ThorVG WebCanvas API - v1.1.4
    Preparing search index...

    Class Font

    Font loader class for managing custom fonts. Fonts are loaded globally and can be referenced by name in Text objects.

    // Load font from raw data
    const fontData = await fetch('/fonts/Roboto-Regular.ttf').then(r => r.arrayBuffer());
    TVG.Font.load('Roboto', new Uint8Array(fontData));

    const text = new TVG.Text();
    text.font('Roboto').fontSize(48).text('Hello!').fill(50, 50, 50);
    // Auto-load from the configured font provider (fontsource CDN by default)
    await TVG.Font.load('poppins');
    await TVG.Font.load('roboto', { weight: 700, style: 'italic' });

    const text = new TVG.Text();
    text.font('poppins').fontSize(48).text('Hello!').fill(50, 50, 50);
    // Use a custom font provider
    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');
    Index
    • Set the font provider used when calling Font.load() without raw data.

      The default provider fetches from the fontsource CDN. Replace it to load fonts from your own CDN or any other source.

      Parameters

      Returns void

      TVG.Font.provider({
      fetch: async (name) => {
      const res = await fetch(`https://my-cdn.com/fonts/${name}.ttf`);
      return { data: new Uint8Array(await res.arrayBuffer()), type: 'ttf' };
      }
      });
    • Load font from raw data.

      Parameters

      • name: string

        Unique name to identify this font

      • data: Uint8Array

        Raw font binary data

      • Optionaloptions: LoadFontOptions

        Load options

      Returns void

    • Auto-load a font using the configured font provider.

      With the default FontsourceProvider, the name must match a fontsource package slug (e.g. 'poppins', 'open-sans').

      Parameters

      • name: string

        Font name passed to the provider

      • Optionaloptions: Record<string, unknown>

        Provider-specific options (see FontsourceOptions for defaults)

      Returns Promise<void>

      await TVG.Font.load('poppins');
      await TVG.Font.load('roboto', { weight: 700 });
      await TVG.Font.load('noto-sans', { subset: 'latin-ext' });
    • Unload a previously loaded font.

      Parameters

      • name: string

        Font name to unload

      Returns void