All files / skin-lr2/src font.ts

50% Statements 46/92
32.25% Branches 20/62
75% Functions 6/8
57.14% Lines 44/77

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                                                    54x 54x 54x 54x 54x                                                         1x           1x 1x 3x 3x 12x 3x 3x 3x 3x 1x 2x   2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x           3x 3x       8x 8x 8x                     8x 8x   8x 8x                                                                                                                                                   3x 3x 8x   3x    
/**
 * LR2 bitmap font support — `.lr2font` text-format parser, glyph map builder, and LR2 → Unicode character-code
 * conversion.
 *
 * `.lr2font` directives (per `docs/LR2SkinHelp.md` line 2099+):
 *
 * - `#S <n>` — base font size (px). Reference height for the glyph cells; DST height is divided by this to compute the
 *   render-time scale factor.
 * - `#M <n>` — character spacing (px), can be negative for tight text where successive glyphs overlap.
 * - `#T <gr> <relative-path>` — declares image #`gr` referencing a PNG / BMP. Path is RELATIVE TO THE `.lr2font` FILE
 *   (not the skin CSV like `#IMAGE`).
 * - `#R <charCode> <gr> <x> <y> <w> <h>` — glyph rect: take from image `gr` the rect `(x, y, w, h)` and treat it as the
 *   bitmap for LR2 character code `charCode`. Glyph height is the base size (`#S`); a height different from `#S` is
 *   rescaled at render time.
 *
 * LR2 character codes are NOT Unicode. They follow:
 *
 * - `0..255` — ASCII direct
 * - `256..8126` — Shift-JIS multibyte minus `32832` offset
 * - `8127..15306` — Shift-JIS multibyte minus `49281` offset
 *
 * The two SJIS branches encode the upper / lower SJIS double-byte ranges (`0x81xx..0x9Fxx` and `0xE0xx..0xFCxx`), with
 * the offset picked so the result fits in a small dense int range. We round- trip through a runtime-built UTF-16 ↔ SJIS
 * table for full Japanese coverage without bundling a static SJIS table.
 */
 
const SJIS_OFFSET_RANGE_1 = 32832;
const SJIS_OFFSET_RANGE_2 = 49281;
const SJIS_RANGE_1_END = 8126;
const SJIS_RANGE_2_START = 8127;
const SJIS_RANGE_2_END = 15306;
 
/** Glyph rect inside a font's source image. */
export interface LR2FontGlyph {
  /** Image index (`#T <gr>` declaration order). */
  gr: number;
  x: number;
  y: number;
  w: number;
  h: number;
}
 
/** A parsed `.lr2font` file. */
export interface LR2BitmapFont {
  /** Base reference size (`#S`). DST height ÷ this = render scale. */
  baseSize: number;
  /** Per-character spacing in source-pixel units (`#M`). Can be negative. */
  spacing: number;
  /** Image index → relative path from the `.lr2font` file. */
  images: Map<number, string>;
  /** LR2 character code → glyph rect. */
  glyphs: Map<number, LR2FontGlyph>;
}
 
/**
 * Parses a `.lr2font` source text into the structured form. Lines are tokenized on whitespace; unknown directives are
 * silently skipped so future `#X` keywords don't break the parse. Unparseable numeric fields fall back to `0`.
 */
export function parseLR2Font(source: string): LR2BitmapFont {
  const font: LR2BitmapFont = {
    baseSize: 22,
    spacing: 0,
    images: new Map(),
    glyphs: new Map(),
  };
  const lines = source.split(/\r?\n/);
  for (const rawLine of lines) {
    const line = stripLineComment(rawLine).trim();
    Iif (line.length === 0) continue;
    const tokens = line.split(/[\s,]+/).filter((token) => token.length > 0);
    const head = tokens[0];
    Iif (!head || !head.startsWith('#')) continue;
    const command = head.toUpperCase();
    if (command === '#S') {
      font.baseSize = toIntOrZero(tokens[1]) || font.baseSize;
    } else Iif (command === '#M') {
      font.spacing = toIntOrZero(tokens[1]);
    } else if (command === '#T') {
      const gr = toIntOrZero(tokens[1]);
      const path = tokens.slice(2).join(' ');
      Eif (path.length > 0) font.images.set(gr, path);
    } else Eif (command === '#R') {
      const code = toIntOrZero(tokens[1]);
      const gr = toIntOrZero(tokens[2]);
      const x = toIntOrZero(tokens[3]);
      const y = toIntOrZero(tokens[4]);
      const w = toIntOrZero(tokens[5]);
      const h = toIntOrZero(tokens[6]);
      Eif (w > 0 && h > 0) {
        font.glyphs.set(code, { gr, x, y, w, h });
      }
    }
  }
  return font;
}
 
function stripLineComment(line: string): string {
  // LR2 conventionally uses `//` (C++) line comments inside skin files. `.lr2font` follows the same convention for
  // compatibility with FontUtil-generated output.
  const slash = line.indexOf('//');
  return slash >= 0 ? line.slice(0, slash) : line;
}
 
function toIntOrZero(value: string | undefined): number {
  Iif (typeof value !== 'string') return 0;
  const parsed = Number.parseInt(value, 10);
  return Number.isFinite(parsed) ? parsed : 0;
}
 
/**
 * Converts a single Unicode code point (UTF-16 BMP) to an LR2 char code, or `undefined` when the character has no SJIS
 * encoding (so the renderer can substitute a placeholder rect).
 *
 * Lazy-built SJIS encoding table — first call costs ~5-10 ms while we walk every double-byte SJIS sequence and decode
 * it via the platform's `TextDecoder('shift-jis')`. Subsequent calls are O(1) map lookups.
 */
export function unicodeToLR2CharCode(char: string): number | undefined {
  Iif (char.length === 0) return undefined;
  const codePoint = char.codePointAt(0)!;
  // ASCII branch — LR2 maps `0..255` straight to ASCII.
  Eif (codePoint < 0x80) {
    return codePoint;
  }
  const sjis = unicodeToSJIS(codePoint);
  if (sjis === undefined) return undefined;
  // SJIS double-byte: split into high/low and decimal-encode as `(high << 8) | low` (i.e. `high * 256 + low` per LR2
  // spec; the original Japanese phrasing translates to "convert the Shift-JIS character code to decimal").
  const decimal = sjis;
  if (decimal >= 33088 && decimal < 33088 + (SJIS_RANGE_1_END - 256 + 1) * 1) {
    // Range 1: `0x8140..0x9FFC` → 256..8126
    const lr2 = decimal - SJIS_OFFSET_RANGE_1;
    if (lr2 >= 256 && lr2 <= SJIS_RANGE_1_END) return lr2;
  }
  if (decimal >= 57408) {
    // Range 2: `0xE040..0xFCFC` → 8127..15306
    const lr2 = decimal - SJIS_OFFSET_RANGE_2;
    if (lr2 >= SJIS_RANGE_2_START && lr2 <= SJIS_RANGE_2_END) return lr2;
  }
  return undefined;
}
 
let unicodeToSJISTable: Map<number, number> | undefined;
 
function unicodeToSJIS(codePoint: number): number | undefined {
  if (!unicodeToSJISTable) {
    unicodeToSJISTable = buildUnicodeToSJISTable();
  }
  return unicodeToSJISTable.get(codePoint);
}
 
/**
 * Walks every valid SJIS double-byte sequence, decodes via the platform `TextDecoder('shift-jis')`, and indexes the
 * result by the resulting Unicode code point. Skips REPLACEMENT (U+FFFD) results so unmapped sequences don't poison the
 * table.
 *
 * Built lazily on first use. The cost is bounded — about 7000 valid SJIS pairs total, decoded in two byte loops.
 */
function buildUnicodeToSJISTable(): Map<number, number> {
  const map = new Map<number, number>();
  const decoder = new TextDecoder('shift-jis', { fatal: false });
  const buffer = new Uint8Array(2);
  const ranges: Array<readonly [number, number]> = [
    [0x81, 0x9f],
    [0xe0, 0xfc],
  ];
  for (const [highStart, highEnd] of ranges) {
    for (let high = highStart; high <= highEnd; high += 1) {
      for (let low = 0x40; low <= 0xfc; low += 1) {
        if (low === 0x7f) continue; // SJIS reserves 0x7F.
        buffer[0] = high;
        buffer[1] = low;
        const decoded = decoder.decode(buffer);
        if (decoded.length === 0) continue;
        const code = decoded.codePointAt(0);
        if (code === undefined || code === 0xfffd) continue;
        // Multi-character outputs (rare — combining marks etc.) are skipped; we can't represent them as a single LR2
        // char code anyway.
        if (decoded.length > 1) continue;
        const sjis = (high << 8) | low;
        // Multiple SJIS sequences can decode to the same Unicode (very rare). Keep the FIRST mapping so range-1
        // characters take precedence over range-2 duplicates, matching the LR2 char-code allocation order.
        if (!map.has(code)) {
          map.set(code, sjis);
        }
      }
    }
  }
  return map;
}
 
/**
 * Converts a string (UTF-16) into an array of LR2 character codes. Characters that don't map to LR2 (no SJIS encoding)
 * are returned as `undefined` slots so the renderer can stamp a placeholder.
 */
export function stringToLR2CharCodes(value: string): Array<number | undefined> {
  const codes: Array<number | undefined> = [];
  for (const char of value) {
    codes.push(unicodeToLR2CharCode(char));
  }
  return codes;
}