包详细信息

@unicode-font-resolver/client

lojjic49MIT1.0.2

A JavaScript client for resolving a unicode string to a set of fonts that cover its characters

自述文件

Unicode Font Resolver - Client

The Unicode standard defines upward of 150K characters, way too many to include in a single font file of reasonable size. This makes it difficult for web apps that perform custom text rendering to provide full Unicode support.

This project provides an online database of small subset font files, sourced from Google's Noto Fonts, and a collection of index and metadata JSON files which allow it to efficiently resolve any Unicode string to a set of fonts that cover all its characters.


The client package lets you query for the set of .woff fonts to cover a Unicode string.

npm install @unicode-font-resolver/client

Usage

import {getFontsForString} from '@unicode-font-resolver/client'

//...

const {fontUrls, chars} = await getFontsForString(
  'Your string of text 🔥',
  {
    // ...options (see below)
  }
)

for (let i = 0; i < chars.length; i++) {
  const fontUrlForChar = fontUrls[chars[i]];
}

Options

The getFontsForString function accepts the following options; they are all optional:

  • category - A string "sans-serif" (the default), "serif", or "monospace"; a matching font will be chosen if available.
  • style - If set to "italic", an italic font file will be chosen if available; otherwise the "normal" font will be used.
  • weight - A numerical font weight hint; the font file with the closest weight to this number will be chosen.
  • lang - A language code, used as a hint for resolving particular characters that are covered by multiple language-specific fonts. Currently supports the following languages for CJK unicode blocks:
    • "ja" for Japanese
    • "ko" for Korean
    • "zh-Hant" for Traditional Chinese
    • Otherwise CJK characters will resolve to Simplified Chinese.
  • dataUrl - A custom location for the unicode-font-resolver data and font files, if you don't want to use the default jsDelivr CDN. See the instructions for how to self host.

Response

The response is an object with these properties:

  • fontUrls - an array of URLs to all the fonts needed by your input string. These URLs point to .woff files.
  • chars - a Uint8Array of the same length as your input string. Each item is the index in fontUrls of the font for that character in the string.

更新日志

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.2 (2023-10-02)

Bug Fixes

  • fallback to other langs when preferred lang doesn't have coverage (c743aff)

1.0.1 (2023-09-29)

Bug Fixes

  • better lang detection using regex unicode script classes (c5c3d5f)
  • detect ko/jp when no lang is set (bbc894c)
  • exclude Korean/Japanese chars from Chinese unicode ranges (b776496)
  • use consistent uppercase in unicode ranges (5324d88)

1.0.0 (2023-09-08)

Note: Version bump only for package unicode-font-resolver

0.4.0 (2023-09-07)

Bug Fixes

  • override incorrect unicode ranges for symbols fonts (#2) (b4cea49)

Features

  • add schema version to the data files and check it in the client (900ff30)

0.3.2 (2023-07-10)

Bug Fixes

  • fix font weight resolution (58b1019)
  • remove unused exported function (a45df5a)

0.3.1 (2023-06-28)

Bug Fixes

  • cache JSON requests in flight to avoid redundant fetches in parallel (a053621)

0.3.0 (2023-06-27)

Features

Performance Improvements

  • experimental alternate CodePointSet implementations, use the simplest for now (dc637ed)
  • strip leading zeros in css range strings (bf71487)

0.2.3 (2023-06-05)

Performance Improvements

  • parallelize bucket and font meta requests (64f91eb)

0.2.2 (2023-06-01)

Note: Version bump only for package unicode-font-resolver

0.2.1 (2023-06-01)

Note: Version bump only for package unicode-font-resolver