Files
horizon-airdef/crates/horizon-client/examples/subtitle_fixture.rs
T
Ludovic Marie bbae62fc4b Render subtitles exactly as vanilla's SubtitleOverlay
The overlay listens to the sound engine from the frame that renders it with
Show Subtitles on until one renders with it off (keeping its captions),
forgets plays as it renders, picks the latest play in range (the first of
full ties), fades in float arithmetic, lays the boxes out in integers under
the pose translation and takes its background from the text background
opacity options. Translations now apply the game's deprecated.json renames,
as ClientLanguage does ("Chick hurts"). tools/test_subtitle_parity.py runs
vanilla's SubtitleOverlay on 1500 scripted cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:07:47 +02:00

107 lines
4.5 KiB
Rust

//! The subtitle overlay for differential tests against vanilla's
//! `SubtitleOverlay` (tools/test_subtitle_parity.py): replays each case's
//! sound plays, clock changes and frames through
//! `horizon_client::subtitles` and prints every frame's fills and texts with
//! their pose, in VanillaSubtitleOracle's JSON lines.
//!
//! usage: subtitle_fixture RESOURCES INPUTS OUTPUT
use horizon_client::{
hud::HudAtlas,
lang,
subtitles::{Draw, Frame, SubtitleOverlay, background_color},
};
use serde_json::{Value, json};
use std::{fs, io::Write, path::PathBuf};
fn vec3(v: &Value) -> [f64; 3] {
std::array::from_fn(|i| v[i].as_f64().unwrap_or(0.0))
}
fn pose(t: [f32; 2]) -> Value {
json!([1.0, 0.0, 0.0, 1.0, t[0], t[1]])
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<PathBuf> = std::env::args_os().skip(1).map(PathBuf::from).collect();
let [root, inputs, output] = args.as_slice() else {
return Err("usage: subtitle_fixture RESOURCES INPUTS OUTPUT".into());
};
lang::set(lang::Language::load(&[root], lang::DEFAULT));
let atlas = HudAtlas::build(root)?;
let mut out = std::io::BufWriter::new(fs::File::create(output)?);
for line in fs::read_to_string(inputs)?
.lines()
.filter(|l| !l.is_empty())
{
let c: Value = serde_json::from_str(line)?;
let mut overlay = SubtitleOverlay::default();
let mut now = 0i64;
let mut frames = Vec::new();
for op in c["ops"].as_array().into_iter().flatten() {
match op["op"].as_str().unwrap_or_default() {
// Util.getMillis: nanoseconds / 1_000_000.
"time" => now = op["ms"].as_i64().unwrap_or(0),
"play" => {
if overlay.listening() {
let range = op["range"].as_f64().map_or(f32::INFINITY, |r| r as f32);
overlay.on_play(
op["key"].as_str(),
vec3(&op["at"]),
range,
op["looping"].as_bool().unwrap_or(false),
now,
);
}
}
"frame" => {
let frame = Frame {
show: op["show"].as_bool().unwrap_or(false),
position: vec3(&op["pos"]),
forward: vec3(&op["forward"]),
up: vec3(&op["up"]),
multiplier: op["mult"].as_f64().unwrap_or(1.0),
background: background_color(
0.8,
op["opacity"].as_f64().unwrap_or(0.5) as f32,
op["chat_only"].as_bool().unwrap_or(true),
),
width: op["w"].as_i64().unwrap_or(0) as i32,
height: op["h"].as_i64().unwrap_or(0) as i32,
};
let draws: Vec<Value> = overlay
.extract(&frame, &atlas.font, now)
.into_iter()
.map(|d| match d {
Draw::Stratum => json!({"type": "stratum"}),
Draw::Fill {
x0,
y0,
x1,
y1,
color,
translate,
} => json!({
"type": "fill", "x0": x0, "y0": y0, "x1": x1, "y1": y1,
"color": format!("{color:x}"), "pose": pose(translate),
}),
Draw::Text {
text,
x,
y,
color,
translate,
} => json!({
"type": "text", "text": text.plain(), "x": x, "y": y,
"color": format!("{color:x}"), "shadow": true, "pose": pose(translate),
}),
})
.collect();
frames.push(Value::Array(draws));
}
other => return Err(format!("op {other}").into()),
}
}
writeln!(out, "{}", json!({"case": c["case"], "frames": frames}))?;
}
Ok(())
}