All files / player-browser/src/skin/beatoraja fonts.ts

72.64% Statements 85/117
55.81% Branches 24/43
92.85% Functions 13/14
73.87% Lines 82/111

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366                                                              22x                                                                                                                 10x 10x 10x         10x       10x   2x 2x       2x     2x         2x 2x 1x             1x 1x 1x   1x   1x 1x         1x 1x 1x 1x 1x 1x           10x             10x 10x 2x 3x 2x   3x 2x 2x 2x 2x 2x 2x           2x 1x   1x 1x 1x               2x 1x 1x       1x 1x               2x         2x                                                 1x 1x 1x       1x                 1x 1x 2x 2x 2x 2x       2x 2x         2x   2x 2x 2x 2x 2x         1x         1x                                                                 1x 1x 1x   1x 1x   1x                         1x                                                           10x 10x 164x 164x   10x    
// Skin font loader for beatoraja `.luaskin` / `.json` themes.
//
// The skin's top-level `font[]` declares per-id font paths. Skins ship two flavors:
//   1. **TTF / OTF** — registered via the document's `FontFace` registry; consumed by Pixi `Text`
//      through CSS `font-family`.
//   2. **AngelCode `.fnt` (BMFont, text format)** — pre-rendered glyph atlas; parsed into a Pixi
//      `BitmapFont` and registered in Pixi's runtime cache; consumed by `BitmapText` through the
//      same `style.fontFamily` channel.
//
// Both branches surface the same per-id `(font-family, kind)` tuple so the skin view can pick the
// right `Text` / `BitmapText` constructor without needing to know how the font was loaded. Failures
// (path missing, decode error, parser hiccup) are logged and skipped — the cache lookup returns
// `undefined` and the consumer falls back to platform sans-serif via Pixi `Text`.
//
// Why BMFont matters: many community themes (e.g. GroundbreakinG) ship hand-tuned bitmap fonts for
// title / artist / score displays. Without BMFont support every label collapses to sans-serif and
// the design's intended metrics fall apart. Browsers can't load `.fnt` through `FontFace` (the
// magic bytes don't pass OpenType validation — OTS rejects them with `invalid sfntVersion`), so the
// loader has to detect the format and route to Pixi's BMFont stack instead.
 
import {
  asLoadedBytes,
  loadAssetBytes,
  resolveBeatorajaPath,
  type BeatorajaSkinFontId,
  type BeatorajaSkinFileEntry,
} from '@be-music/skin-beatoraja';
import { BitmapFont, Cache, Texture, bitmapFontTextParser } from 'pixi.js';
import { logger } from '../../logger.ts';
import { loadTextureFromBytes } from '../../media/textures.ts';
 
const log = logger('beatoraja-fonts');
 
/** What rendering pipe a loaded font is bound to. Drives the skin view's `Text` vs `BitmapText` pick. */
export type BeatorajaFontKind = 'css' | 'bitmap';
 
export interface BeatorajaLoadedFont {
  /** Author-declared font slot id from `font[].id`. */
  id: BeatorajaSkinFontId;
  /**
   * `font-family` name that addresses this font:
   * - `kind: 'css'` → the family registered with `document.fonts`. Used in `style.fontFamily`.
   * - `kind: 'bitmap'` → the key the Pixi runtime cache holds the `BitmapFont` under (the value
   *   stored in the cache is keyed `${family}-bitmap`, but `BitmapText.style.fontFamily` takes the
   *   bare family name and Pixi appends `-bitmap` internally).
   */
  family: string;
  /** Resolved path inside the dropped file map. Used for diagnostics. */
  path: string;
  /** Which Pixi text class to render with. */
  kind: BeatorajaFontKind;
}
 
export interface BeatorajaFontCache {
  /** Returns the registered family name for a font id, or `undefined` when the slot wasn't loaded. */
  family(id: BeatorajaSkinFontId): string | undefined;
  /** Returns the rendering kind for a loaded slot, or `undefined` when the slot wasn't loaded. */
  kind(id: BeatorajaSkinFontId): BeatorajaFontKind | undefined;
  /** All successfully-loaded fonts, in declaration order. */
  values(): ReadonlyArray<BeatorajaLoadedFont>;
  /** Unregisters CSS/bitmap fonts owned by this cache. Safe to call more than once. */
  dispose(): void;
}
 
interface BeatorajaFontOwnedResources {
  cssFaces: FontFace[];
  bitmapFonts: { cacheKey: string; font: BitmapFont }[];
}
 
interface RegisteredBitmapFont {
  loaded: BeatorajaLoadedFont;
  cacheKey: string;
  font: BitmapFont;
}
 
/**
 * Load every `font[]` declaration into the appropriate registry — `document.fonts` for TTF/OTF or
 * Pixi's `Cache` for AngelCode `.fnt`. Each font gets a unique `font-family` name
 * (`beatoraja-skin-<entry>-<id>`) so concurrent skins don't clobber each other's registrations.
 *
 * Failures (path not found, unsupported format, decode error) are logged and skipped — the matching
 * lookup returns `undefined`, and the consumer falls back to the platform sans-serif via `Text`.
 */
export async function loadBeatorajaFonts(options: {
  files: ReadonlyMap<string, BeatorajaSkinFileEntry>;
  entryPath: string;
  fonts: ReadonlyArray<{ id: BeatorajaSkinFontId; path: string }>;
}): Promise<BeatorajaFontCache> {
  const out = new Map<BeatorajaSkinFontId, BeatorajaLoadedFont>();
  const ownedResources: BeatorajaFontOwnedResources = { cssFaces: [], bitmapFonts: [] };
  const familyPrefix = `beatoraja-skin-${stableEntryHash(options.entryPath)}`;
  // CSS-side prerequisites only matter for the FontFace branch. The BMFont branch needs Pixi's
  // runtime cache, which exists in any environment that loaded `pixi.js` — tests use jsdom which
  // doesn't ship `FontFace`, so guard the CSS branch and let BMFonts continue loading.
  const canRegisterCSSFont =
    typeof globalThis !== 'undefined' &&
    typeof FontFace !== 'undefined' &&
    typeof document !== 'undefined' &&
    'fonts' in document;
  await Promise.all(
    options.fonts.map(async (decl) => {
      const resolved = resolveBeatorajaPath(options.files, options.entryPath, decl.path);
      Iif (resolved === undefined) {
        log.warn(`font[${decl.id}] '${decl.path}': not found in dropped file map`);
        return;
      }
      const entry = options.files.get(resolved);
      // Fonts are typically eagerly loaded into the byte map (TTFS are tiny). If the entry was deferred,
      // pull it now — same `loadAssetBytes` helper the texture loader uses.
      const bytes = asLoadedBytes(entry) ?? (await loadAssetBytes(entry));
      if (bytes === undefined) {
        log.warn(`font[${decl.id}] '${resolved}': bytes not available`);
        return;
      }
      const family = `${familyPrefix}-${fontIdFamilySuffix(decl.id)}`;
      if (isAngelCodeFntPath(resolved)) {
        const registered = await registerBitmapFont({
          declId: decl.id,
          family,
          fntPath: resolved,
          fntBytes: bytes,
          files: options.files,
        });
        Eif (registered !== undefined) {
          out.set(decl.id, registered.loaded);
          ownedResources.bitmapFonts.push({ cacheKey: registered.cacheKey, font: registered.font });
        }
        return;
      }
      Iif (!canRegisterCSSFont) return;
      try {
        // Copy to a fresh, non-shared `ArrayBuffer`. The dropped-file map can hand back a
        // `Uint8Array<SharedArrayBuffer>` (some browsers' file readers return one), and
        // `FontFace`'s constructor signature only accepts a plain `ArrayBuffer`. Slicing the typed
        // array materializes a regular `ArrayBuffer` regardless of the source.
        const buffer = bytes.slice().buffer as ArrayBuffer;
        const face = new FontFace(family, buffer);
        await face.load();
        document.fonts.add(face);
        out.set(decl.id, { id: decl.id, family, path: resolved, kind: 'css' });
        ownedResources.cssFaces.push(face);
      } catch (error) {
        log.warn(`font[${decl.id}] '${resolved}': failed to register`, error);
      }
    }),
  );
  return makeCache(out, ownedResources);
}
 
function makeCache(
  map: Map<BeatorajaSkinFontId, BeatorajaLoadedFont>,
  ownedResources: BeatorajaFontOwnedResources,
): BeatorajaFontCache {
  let disposed = false;
  return {
    family: (id) => map.get(id)?.family,
    kind: (id) => map.get(id)?.kind,
    values: () => Array.from(map.values()),
    dispose: () => {
      if (disposed) return;
      disposed = true;
      unregisterCSSFaces(ownedResources.cssFaces);
      unregisterBitmapFonts(ownedResources.bitmapFonts);
      ownedResources.cssFaces.length = 0;
      ownedResources.bitmapFonts.length = 0;
      map.clear();
    },
  };
}
 
function unregisterCSSFaces(faces: ReadonlyArray<FontFace>): void {
  if (typeof document === 'undefined' || !('fonts' in document) || typeof document.fonts.delete !== 'function') {
    return;
  }
  for (const face of faces) {
    try {
      document.fonts.delete(face);
    } catch (error) {
      log.warn(`failed to unregister font face '${face.family}'`, error);
    }
  }
}
 
function unregisterBitmapFonts(fonts: ReadonlyArray<{ cacheKey: string; font: BitmapFont }>): void {
  for (const { cacheKey, font } of fonts) {
    try {
      Cache.remove(cacheKey);
    } catch (error) {
      log.warn(`failed to remove bitmap font cache '${cacheKey}'`, error);
    }
    try {
      font.destroy();
    } catch (error) {
      log.warn(`failed to destroy bitmap font '${cacheKey}'`, error);
    }
  }
}
 
function fontIdFamilySuffix(id: BeatorajaSkinFontId): string {
  return typeof id === 'number' ? String(id) : `s-${stableEntryHash(id)}`;
}
 
/** Identify AngelCode BMFont declarations by file extension — text-format `.fnt` is the only flavor we ship. */
function isAngelCodeFntPath(path: string): boolean {
  return path.toLowerCase().endsWith('.fnt');
}
 
/**
 * Decode a `.fnt` file (text-format AngelCode BMFont), resolve its `page[].file` entries to page
 * textures from the dropped file map, build a `BitmapFont`, and register it in Pixi's runtime
 * cache under the `${family}-bitmap` key (matching what `BitmapText` looks up internally).
 *
 * Why we don't use Pixi's `loadBitmapFont`: that loader assumes the page images live behind a URL
 * the asset loader can fetch. Our pages live as `Uint8Array` in the dropped-file map, so we mirror
 * the same five steps (parse → resolve pages → decode → construct → cache) without going through
 * the asset URL machinery.
 */
async function registerBitmapFont(args: {
  declId: BeatorajaSkinFontId;
  family: string;
  fntPath: string;
  fntBytes: Uint8Array;
  files: ReadonlyMap<string, BeatorajaSkinFileEntry>;
}): Promise<RegisteredBitmapFont | undefined> {
  // BMFont text format is ASCII / Latin-1 — TextDecoder defaults to UTF-8 which is a strict
  // superset for the printable subset BMFont uses, so this is safe even when the file was authored
  // on Windows with code-page 1252 line endings (the parser tolerates `\r\n`).
  let parsed: ReturnType<typeof bitmapFontTextParser.parse>;
  let font: BitmapFont | undefined;
  try {
    const text = new TextDecoder('utf-8').decode(args.fntBytes);
    Iif (!bitmapFontTextParser.test(text)) {
      log.warn(`font[${args.declId}] '${args.fntPath}': not a recognized BMFont text format`);
      return undefined;
    }
    parsed = bitmapFontTextParser.parse(text);
  } catch (error) {
    log.warn(`font[${args.declId}] '${args.fntPath}': failed to parse`, error);
    return undefined;
  }
 
  // BMFont's `pages[]` lists page-image filenames relative to the `.fnt` file's directory. Resolve
  // each through the dropped-file map and decode to a Pixi `Texture`. Skip pages that fail to load
  // — a partially-decoded font still renders most glyphs.
  const pageTextures: Texture[] = [];
  for (let i = 0; i < parsed.pages.length; i += 1) {
    const page = parsed.pages[i];
    Iif (page === undefined) continue;
    const resolvedPagePath = resolveBeatorajaPath(args.files, args.fntPath, page.file);
    Iif (resolvedPagePath === undefined) {
      log.warn(`font[${args.declId}] page '${page.file}': not found relative to '${args.fntPath}'`);
      continue;
    }
    const pageEntry = args.files.get(resolvedPagePath);
    const pageBytes = asLoadedBytes(pageEntry) ?? (await loadAssetBytes(pageEntry));
    if (pageBytes === undefined) {
      log.warn(`font[${args.declId}] page '${resolvedPagePath}': bytes not available`);
      continue;
    }
    try {
      // BMFont sheets can be TGA (WMII_FHD); browsers cannot decode those directly.
      const texture = await loadTextureFromBytes(resolvedPagePath, pageBytes);
      Iif (texture === undefined) throw new Error('Unsupported bitmap font page');
      texture.label = resolvedPagePath;
      texture.source.label = resolvedPagePath;
      pageTextures.push(texture);
    } catch (error) {
      log.warn(`font[${args.declId}] page '${resolvedPagePath}': failed to decode`, error);
    }
  }
  Iif (pageTextures.length === 0) {
    log.warn(`font[${args.declId}] '${args.fntPath}': no page textures decoded — abandoning font`);
    return undefined;
  }
 
  try {
    // Override `fontFamily` on the parsed data so the cache key matches our skin-prefixed family
    // (the parser fills it from the `info face=` line, which collides across skins). The resulting
    // BitmapFont reads its identity from this field.
    //
    // Normalize `fontSize` to its absolute value to mirror upstream's *effective* behavior.
    //
    // AngelCode BMFont's `info size=N` line uses the sign as a UNIT HINT — POSITIVE = points,
    // NEGATIVE = pixels — and the generator embeds that sign in the `.fnt` output verbatim.
    // GroundbreakinG's `Title.fnt` ships `size=-120` ("120 pixel glyphs"), and skins targeting
    // it pair the convention by authoring `text.size = -118` to match.
    //
    // Upstream `SkinTextBitmap.SkinTextBitmapSource.createCacheableFont` parses the `size=N` value
    // verbatim into `originalSize` (the negative is preserved), and `SkinTextBitmap.draw` line 59
    // computes `scale = this.size / originalSize`. Because BOTH `this.size` (= `text.size`) and
    // `originalSize` carry the same sign, the negatives CANCEL at scale time and produce a
    // positive ratio (`-118 / -120 = +0.983`). Final rendered glyph height ends up
    // `|originalSize| * scale ≈ |text.size|` px.
    //
    // Pixi's `bitmapFontTextParser` likewise carries the literal `-120` straight into
    // `BitmapFont.baseMeasurementFontSize`, but `getBitmapTextLayout` computes
    // `scale = style.fontSize / baseMeasurementFontSize` — and Pixi's TextStyle treats
    // `fontSize` as an unsigned magnitude (a negative input has no well-defined meaning in the
    // CSS/canvas pipelines `Text` and `BitmapText` share). We can't pass through a negative
    // `style.fontSize` to reproduce upstream's double-negative cancellation, so we sign-strip
    // BOTH sides instead — `Math.abs(parsed.fontSize)` here, paired with `Math.abs(element.size)`
    // in `view/beatoraja/skin-view/text.ts`'s `buildTextEntry`. The result is mathematically
    // equivalent: `+118 / +120 = +0.983`, same scale, same rendered glyph height.
    //
    // User report: without this normalization, `style.fontSize = abs(text.size)` (the only
    // value Pixi will accept) divided by negative `parsed.fontSize` gave a NEGATIVE Pixi scale,
    // and `AbstractBitmapTextPipe` rendered the text 180° flipped (gdbg DECIDE's title and
    // artist appeared upside-down).
    const normalizedFontSize = Math.abs(parsed.fontSize);
    const data = { ...parsed, fontFamily: args.family, fontSize: normalizedFontSize };
    font = new BitmapFont({ data, textures: pageTextures });
    // Pixi's `BitmapText` looks up `Cache.get(`${family}-bitmap`)` — register under that exact key.
    const cacheKey = `${args.family}-bitmap`;
    Cache.set(cacheKey, font);
    // eslint-disable-next-line no-console
    console.log(
      '[beatoraja-fonts] bitmap font registered',
      JSON.stringify({
        id: args.declId,
        family: args.family,
        path: args.fntPath,
        pages: pageTextures.length,
        chars: Object.keys(parsed.chars).length,
        lineHeight: parsed.lineHeight,
        fontSize: normalizedFontSize,
        rawFontSize: parsed.fontSize,
      }),
    );
    return {
      loaded: { id: args.declId, family: args.family, path: args.fntPath, kind: 'bitmap' },
      cacheKey,
      font,
    };
  } catch (error) {
    if (font !== undefined) {
      font.destroy();
    } else {
      destroyPageTextures(pageTextures);
    }
    log.warn(`font[${args.declId}] '${args.fntPath}': failed to construct BitmapFont`, error);
    return undefined;
  }
}
 
function destroyPageTextures(textures: ReadonlyArray<Texture>): void {
  for (const texture of textures) {
    if (!texture.destroyed) {
      texture.destroy(true);
    }
  }
}
 
/**
 * Stable, dependency-free 32-bit hash of a path string. We only use it to derive a unique font-family
 * suffix per entry, so a real cryptographic hash is overkill — collision probability for the few dozen
 * entries a session loads is negligible. Pure JS so the build doesn't pull in `node:crypto`.
 */
function stableEntryHash(path: string): string {
  let h = 0x811c9dc5;
  for (let i = 0; i < path.length; i += 1) {
    h ^= path.charCodeAt(i);
    h = Math.imul(h, 0x01000193);
  }
  return (h >>> 0).toString(16).padStart(8, '0');
}