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 | 34x 8x 1x 7x 9x 8x 5x 2x 3x 2x 3x 5x 3x | import { Container, Text, TextStyle } from 'pixi.js';
import { normalizeRect } from './render.ts';
import type { LR2DestinationRect, LR2TextElement } from '@be-music/skin-lr2';
import { makeLR2BitmapTextSprite, type LR2LoadedFont } from './bitmap-text.ts';
const FALLBACK_TEXT_FONT = 'ui-sans-serif, system-ui, sans-serif';
export { resolveScaledViewport, type ScaledViewport } from '../../stage/viewport.ts';
export interface LR2TextSpriteOptions {
maxFontSize?: number;
/**
* Loaded LR2 bitmap fonts keyed by font index (`#LR2FONT` declaration order). When the element's `font` field hits a
* loaded entry, `makeLR2TextSprite` returns a bitmap-text `Container` instead of the system-font `Text` fallback —
* matching what real LR2 paints with `#SRC_TEXT`.
*/
bitmapFonts?: ReadonlyMap<number, LR2LoadedFont>;
/**
* `#FONT,size,...` declarations indexed by the same global font slot as `bitmapFonts`. Used by the system-font
* fallback branch to pick the size the skin actually asked for — without this, every system-font label collapses to
* the generic `rect.h - 2` heuristic and visibly disagrees with the LR2 reference (e.g. the LR2 default theme's PLAY
* OPTION row declares `#FONT,18,...` but the rect height is closer to 30, so the heuristic over-shoots).
*/
systemFontSizes?: ReadonlyArray<number>;
}
export function isDestinationVisible(
destination: LR2DestinationRect,
ops: ReadonlySet<number>,
timerActive: (timer: number) => boolean,
): boolean {
if (!timerActive(destination.timer)) {
return false;
}
for (const op of destination.ops) {
if (op === 0) continue;
if (op > 0) {
if (!ops.has(op)) {
return false;
}
} else if (ops.has(-op)) {
return false;
}
}
return true;
}
/**
* Renders an LR2 `#SRC_TEXT` element into a Pixi node — a bitmap- font `Container` when the element's `font` index
* points at a loaded `#LR2FONT`, or a system-font `Text` fallback otherwise.
*
* Returning a `Container` (instead of `Text` specifically) is the union of both paths; callers attach the result with
* `addChild` either way, which is the only API contract the scene renderers rely on.
*/
export function makeLR2TextSprite(
value: string,
element: LR2TextElement,
dst: LR2DestinationRect = element.destination,
options: LR2TextSpriteOptions = {},
): Container {
// Bitmap path — chosen when the host loaded the matching `#LR2FONT` payload. Empty strings are still painted as an
// empty Container so the caller's `addChild` doesn't need to null-check; the visible result is just nothing, same as
// the text fallback would produce for "".
const loaded = options.bitmapFonts?.get(element.font);
if (loaded) {
return makeLR2BitmapTextSprite(value, element, dst, loaded);
}
const rect = normalizeRect(dst);
// Prefer the size declared by the skin's `#FONT` for this slot. Falls back to the rect-height heuristic only when
// there's no declaration at all (e.g. older skins that omit `#FONT` and rely on rect-driven sizing). The clamp still
// applies as a safety net — a malformed `#FONT,9999` shouldn't blow out the text bounds.
const declaredSize = options.systemFontSizes?.[element.font];
const desiredSize = declaredSize !== undefined && declaredSize > 0 ? declaredSize : rect.h - 2;
const fontSize = clampFontSize(desiredSize, 8, options.maxFontSize ?? 64);
// No `wordWrap` — LR2's `#SRC_TEXT` spec calls for HORIZONTAL shrink-to-fit when the rendered string overruns `dst.w`
// (`docs/LR2SkinHelp.md` line 1343), not for line-wrapping. Letting the text overflow on a single line and then
// squeezing it via `scale.x` matches that semantic.
const text = new Text({
text: value,
style: new TextStyle({
fill: dst.alpha > 0 ? (dst.r << 16) | (dst.g << 8) | dst.b : 0xffffff,
fontSize,
fontFamily: FALLBACK_TEXT_FONT,
stroke: { color: 0x000000, width: 2, alignment: 0.5, join: 'round' },
}),
});
text.label = `text[st=${element.st}]`;
text.alpha = dst.alpha;
// LR2 alignment is ANCHOR-based: `dst.x` is the alignment-dependent anchor point (left edge / center / right edge),
// not a box origin paired with `dst.w` as a box width. See `skin/lr2/bitmap-text.ts` for the empirical justification —
// same skin-author convention applies to the system-font path.
if (element.alignment === 'center') {
text.anchor.set(0.5, 0);
} else if (element.alignment === 'right') {
text.anchor.set(1, 0);
} else {
text.anchor.set(0, 0);
}
text.position.set(rect.x, rect.y);
// LR2 shrink-to-fit: when the rendered string overflows the DST's `w`, compress it horizontally so it lands inside
// the authored rectangle. Pixi `Text.width` is the post-style bounds width, so we can read it immediately after
// construction. The `scale` applies around the text's anchor (0 / 0.5 / 1 above), which keeps the alignment edge
// fixed — left-aligned text squeezes toward `rect.x`, right-aligned squeezes toward `rect.x` (right edge), center
// stays centered on `rect.x`.
if (rect.w > 0 && text.width > rect.w) {
text.scale.x = rect.w / text.width;
}
return text;
}
export function clampFontSize(value: number, min: number, max: number): number {
if (!Number.isFinite(value)) return min;
return Math.max(min, Math.min(max, Math.floor(value)));
}
|