JSPM

@telazer/font-loader

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10
  • Score
    100M100P100Q26906F
  • License MIT

A library for loading fonts

Package Exports

  • @telazer/font-loader

Readme

Telazer - FontLoader

GitHub

For more helpers and utilities, check out the Telazer NPM Page

A lightweight TypeScript utility for dynamically loading fonts in the browser, with support for CSS Fonts Level 4 metric overrides (ascentOverride, descentOverride, lineGapOverride) to fine-tune font baselines and line spacing.


Installation

npm install @telazer/font-loader

Key Features

  • Dynamically load fonts via the FontFace API

  • Exposes each font as a CSS variable: var(--font-<key>) for easy use in stylesheets

  • Supports standard @font-face descriptors:

    • weight
    • style
    • stretch
    • display
  • Supports CSS Fonts Level 4 overrides for adjusting font metrics:

    • ascentOverride (baseline up/down)
    • descentOverride (descenders)
    • lineGapOverride (line spacing)

Quick Start

import FontLoader from '@telazer/font-loader';

await FontLoader.load([
  {
    key: 'MyFont',
    url: '/fonts/MyFont.woff2',
    weight: 400,
    style: 'normal',
    display: 'swap',
    ascentOverride: 115, // nudges text slightly down
    descentOverride: 10,
    lineGapOverride: 0,
  },
]);

// Now available in CSS:
body {
  font-family: var(--font-MyFont), sans-serif;
}

Font Metric Overrides

Property Example Effect
ascentOverride 115 Increases ascent → shifts glyphs down (more space above).
descentOverride 10 Controls space below baseline (descenders like "y", "g").
lineGapOverride 0 Extra space between lines (0 removes gap, >0 adds spacing).

Notes:

  • Values are interpreted as percentages of the font’s em square.
  • 100 = use the font’s built-in metrics.
  • <100 reduces space, >100 increases space.
  • Negative values are invalid and ignored.

API

FontLoader.load(fonts: FontLoadOptions[]): Promise<void>

Loads one or more fonts and exposes them via CSS variables.

FontLoadOptions

export interface FontLoadOptions {
  key: string; // Unique identifier for font-family (used in CSS var)
  url: string; // URL to font file (.woff2 recommended)

  weight?: string | number; // "normal", "bold", 100–900
  style?: string; // "normal", "italic", "oblique"
  stretch?: string; // "condensed", "expanded", "100%"
  display?: string; // "auto" | "swap" | "block" | "fallback" | "optional"

  ascentOverride?: number; // ≥0, adjusts baseline (100 = default, >100 = down, <100 = up)
  descentOverride?: number; // ≥0, adjusts descender space
  lineGapOverride?: number; // ≥0, adjusts line spacing (0 = none)
}

Examples

// Simple load
await FontLoader.load([
  { key: 'Roboto', url: '/fonts/roboto.woff2' }
]);

// Italic + custom overrides
await FontLoader.load([
  {
    key: 'RobotoItalic',
    url: '/fonts/roboto-italic.woff2',
    style: 'italic',
    ascentOverride: 110,
  },
]);

// Using in CSS
.title {
  font-family: var(--font-RobotoItalic), sans-serif;
}

Development

git clone https://github.com/Telazer/font-loader
npm install
npm run watch
npm test
npm run build

License

MIT License

Copyright (c) 2025 Telazer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.