All files / skin-beatoraja/src/elements text.ts

90.27% Statements 65/72
84.81% Branches 67/79
100% Functions 8/8
100% Lines 54/54

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                                                                                                                                                                                102x 102x 102x 22x 22x   102x       22x 22x 22x 21x 21x 22x                                                           42x 4x 42x     2x 2x 2x 2x 2x   2x 1x 1x 1x   1x       21x 5x 5x   17x   3x 2x   15x       147x 147x       21x 21x 21x               21x 21x 9x 8x               12x 2x 2x 5x 5x 5x 5x 5x 5x 5x 4x 3x   2x    
// Strict-typed normalization for beatoraja `text[]` and `font[]` declarations.
//
// Beatoraja text elements differ from `image[]` / `value[]` in that they have no source rect — instead they pair a
// font id with a size and an op-code reference (`ref`). The renderer pulls the actual string from the runtime
// (song title, current judge label, etc.) using `ref`, lays it out in the font, and paints the result inside the
// destination's bounding box.
//
// At parser level we only normalize the declaration shape. Font loading and text-content resolution are renderer
// concerns that live in `@be-music/player-browser`.
 
import { flattenBeatorajaElements, type NormalizedElement } from './base.ts';
import { isBeatorajaLuaFunctionValue, type BeatorajaLuaFunctionValue } from '../lua.ts';
import type { BeatorajaImageId } from './image.ts';
import type { BeatorajaSkinFontId } from '../types.ts';
 
export type BeatorajaStringPropertyRef = number | BeatorajaLuaFunctionValue;
 
/** Optional alignment hint. Most authors leave it unset (`'left'` is the beatoraja default). */
export type BeatorajaTextAlign = 'left' | 'center' | 'right';
 
export interface BeatorajaTextElement {
  /**
   * Same id space as `image[]` / `value[]` — destinations reference text elements with `dst[].id` matching this.
   */
  id: BeatorajaImageId;
  /** Font slot id (`font[].id`). Numeric and symbolic string ids are both valid. */
  fontId: BeatorajaSkinFontId;
  /** Font size in skin-pixel units. */
  size: number;
  /**
   * Op-code reference for the dynamic string contents (song title, artist, current combo's word form, etc.).
   * `0` means "no ref" — a static label the author baked in.
   */
  ref: number;
  /** Optional `value` StringProperty. Beatoraja evaluates this instead of `ref` when authored. */
  valueProperty?: BeatorajaStringPropertyRef;
  /**
   * Optional pre-rendered string. Beatoraja's Lua skins use this to bake in a literal label
   * (e.g. `"Avg 5.23 ms"` computed at scene mount via `main_state.number(374)` and stored as a
   * static string for the renderer). When set, takes precedence over `ref` / `valueProperty`.
   */
  constantText?: string;
  /** Rendering alignment. Defaults to `'left'`. */
  align: BeatorajaTextAlign;
  /**
   * Beatoraja's `overflow` flag controls how text wider than its destination box is handled. The
   * surveyed skins (GdbG_Skin, ModernChic) only ever author `overflow = 1` (= shrink-to-fit, the
   * mode that scales the rendered text down by `dst.w / measured.w` so it stays inside the box);
   * other values (`0` = no handling, `2` = clip in the upstream Java) are uncommon in practice but
   * preserved verbatim here so the renderer can branch on them. Defaults to `0`.
   */
  overflow: number;
  /**
   * Outline / drop-shadow styling (audit 2.10). Beatoraja's JSONSkin.Text serializes colors as
   * 8-char `RRGGBBAA` hex strings (e.g. `"165423ff"` = beginner green). When the outline / shadow
   * is "off" the alpha byte is `00` so the renderer can gate on `alpha > 0` instead of needing a
   * separate enabled flag.
   */
  outlineColor: BeatorajaTextRGBA;
  outlineWidth: number;
  shadowColor: BeatorajaTextRGBA;
  shadowOffsetX: number;
  shadowOffsetY: number;
  shadowSmoothness: number;
  /**
   * Word-wrap toggle (`boolean wrapping = false` upstream). When true the renderer wraps the
   * rendered text inside the destination box rather than letting it overflow / shrink.
   */
  wrapping: boolean;
  /** `if` codes that gate visibility (from `if`/`values` flattening). */
  ifCodes: ReadonlyArray<number>;
}
 
/** Parsed `RRGGBBAA` hex color split into Pixi-friendly components. */
export interface BeatorajaTextRGBA {
  /** 24-bit RGB packed `0xRRGGBB`. */
  rgb: number;
  /** Alpha 0..1 (so it can be passed straight into Pixi `style.dropShadow.alpha`). */
  alpha: number;
}
 
export interface BeatorajaFontElement {
  id: BeatorajaSkinFontId;
  /** Path relative to the skin file. */
  path: string;
}
 
export function normalizeBeatorajaTexts(input: unknown): BeatorajaTextElement[] {
  const flattened = flattenBeatorajaElements(input);
  const out: BeatorajaTextElement[] = [];
  for (const entry of flattened) {
    const normalized = normalizeOne(entry);
    if (normalized !== undefined) out.push(normalized);
  }
  return out;
}
 
function normalizeOne(entry: NormalizedElement): BeatorajaTextElement | undefined {
  const f = entry.fields;
  const id = f.id;
  if (typeof id !== 'string' && typeof id !== 'number') return undefined;
  const valueProperty = stringPropertyField(f.value);
  const constantText = typeof f.constantText === 'string' ? f.constantText : undefined;
  return {
    id,
    fontId: fontIdField(f, 'font', 0),
    size: numberField(f, 'size', 24),
    ref: numberField(f, 'ref', 0),
    ...(valueProperty !== undefined ? { valueProperty } : {}),
    ...(constantText !== undefined ? { constantText } : {}),
    align: alignField(f.align),
    overflow: numberField(f, 'overflow', 0),
    // Outline / shadow styling (audit 2.10). The default `RRGGBBAA = "ffffff00"` is white with
    // 0% alpha — same sentinel beatoraja uses for "feature disabled". The renderer should gate
    // its stroke / dropShadow application on `alpha > 0`.
    outlineColor: parseBeatorajaTextRGBA(f.outlineColor, 0xffffff, 0),
    outlineWidth: numberField(f, 'outlineWidth', 0),
    shadowColor: parseBeatorajaTextRGBA(f.shadowColor, 0xffffff, 0),
    shadowOffsetX: numberField(f, 'shadowOffsetX', 0),
    shadowOffsetY: numberField(f, 'shadowOffsetY', 0),
    shadowSmoothness: numberField(f, 'shadowSmoothness', 0),
    wrapping: f.wrapping === true || f.wrapping === 1,
    ifCodes: entry.ifCodes,
  };
}
 
/**
 * Parse beatoraja's `RRGGBBAA` 8-char hex string into `{rgb, alpha}`. Tolerates 6-char
 * `RRGGBB` (alpha defaults to 1.0) and falls back to the supplied default when the value is
 * missing / malformed. Used by both the runtime parser (for `text[]` outline / shadow) and
 * future bpmgraph / gaugegraph color fields.
 */
function parseBeatorajaTextRGBA(value: unknown, fallbackRGB: number, fallbackAlpha: number): BeatorajaTextRGBA {
  if (typeof value !== 'string') return { rgb: fallbackRGB, alpha: fallbackAlpha };
  const trimmed = value.startsWith('#') ? value.slice(1) : value;
  if (trimmed.length === 8) {
    // RRGGBBAA. Parse as base-16; if any byte is invalid, the integer fall back to NaN and
    // we drop to the default. The alpha byte gets normalized to 0..1.
    const num = Number.parseInt(trimmed, 16);
    Iif (!Number.isFinite(num)) return { rgb: fallbackRGB, alpha: fallbackAlpha };
    const rgb = (num >>> 8) & 0xffffff;
    const alpha = (num & 0xff) / 255;
    return { rgb, alpha };
  }
  if (trimmed.length === 6) {
    const num = Number.parseInt(trimmed, 16);
    Iif (!Number.isFinite(num)) return { rgb: fallbackRGB, alpha: fallbackAlpha };
    return { rgb: num & 0xffffff, alpha: 1 };
  }
  return { rgb: fallbackRGB, alpha: fallbackAlpha };
}
 
function alignField(value: unknown): BeatorajaTextAlign {
  if (typeof value === 'string') {
    const lower = value.toLowerCase();
    if (lower === 'center' || lower === 'right' || lower === 'left') return lower;
  }
  if (typeof value === 'number') {
    // Beatoraja accepts numeric align codes in some skins: 0 = left, 1 = center, 2 = right.
    if (value === 1) return 'center';
    if (value === 2) return 'right';
  }
  return 'left';
}
 
function numberField(record: Readonly<Record<string, unknown>>, key: string, fallback: number): number {
  const v = record[key];
  return typeof v === 'number' && Number.isFinite(v) ? v : fallback;
}
 
function stringPropertyField(value: unknown): BeatorajaStringPropertyRef | undefined {
  Iif (typeof value === 'number' && Number.isFinite(value)) return value;
  Iif (isBeatorajaLuaFunctionValue(value)) return value;
  return undefined;
}
 
function fontIdField(
  record: Readonly<Record<string, unknown>>,
  key: string,
  fallback: BeatorajaSkinFontId,
): BeatorajaSkinFontId {
  const v = record[key];
  if (typeof v === 'number' && Number.isFinite(v)) return v;
  if (typeof v === 'string' && v.length > 0) return v;
  return fallback;
}
 
/**
 * Normalize the top-level `font[]` array. Returns `[]` when the skin doesn't declare any fonts. Paths are
 * preserved verbatim — the renderer is responsible for resolving them through the dropped file map.
 */
export function normalizeBeatorajaFonts(input: unknown): BeatorajaFontElement[] {
  if (!Array.isArray(input)) return [];
  const out: BeatorajaFontElement[] = [];
  for (const entry of input) {
    Iif (entry === null || typeof entry !== 'object') continue;
    const obj = entry as Readonly<Record<string, unknown>>;
    const id = obj.id;
    const path = obj.path;
    Iif (typeof id !== 'number' && typeof id !== 'string') continue;
    Iif (typeof id === 'number' && !Number.isFinite(id)) continue;
    if (typeof id === 'string' && id.length === 0) continue;
    if (typeof path !== 'string' || path.length === 0) continue;
    out.push({ id, path });
  }
  return out;
}