oxideav-ass 0.0.7

ASS/SSA subtitle codec + container for oxideav
Documentation
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! End-to-end animated-tag tests: parse a Dialogue line, extract the
//! typed animation, evaluate it at several timestamps, and verify the
//! resulting `RenderState`.

use oxideav_ass as ass;
use oxideav_ass::{extract_cue_animation, AnimatedTag, ClipRect};

const HEADER: &str = r"[Script Info]
ScriptType: v4.00+

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, Alignment, MarginL, MarginR, MarginV, Outline, Shadow
Style: Default,Arial,20,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,2,10,10,10,1,0

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
";

#[test]
fn fad_evaluates_alpha_curve() {
    let src =
        format!("{HEADER}Dialogue: 0,0:00:00.00,0:00:02.00,Default,,0,0,0,,{{\\fad(200,300)}}hi\n");
    let t = ass::parse(src.as_bytes()).unwrap();
    let cue = &t.cues[0];
    let anim = extract_cue_animation(cue);
    // 1 tag: \fad(200, 300).
    assert_eq!(anim.tags.len(), 1, "tags: {:?}", anim.tags);
    assert!(matches!(
        anim.tags[0],
        AnimatedTag::Fad {
            t1_ms: 200,
            t2_ms: 300
        }
    ));

    let dur_ms = ((cue.end_us - cue.start_us) / 1000) as i32;
    assert_eq!(dur_ms, 2000);
    // At t=0 fully transparent.
    assert!((anim.evaluate_at(0, dur_ms).alpha_mul - 0.0).abs() < 1e-6);
    // After fade-in finished: opaque.
    assert!((anim.evaluate_at(200, dur_ms).alpha_mul - 1.0).abs() < 1e-6);
    // Mid-cue: opaque.
    assert!((anim.evaluate_at(1000, dur_ms).alpha_mul - 1.0).abs() < 1e-6);
    // Halfway through fade-out (last 300ms):
    assert!((anim.evaluate_at(1850, dur_ms).alpha_mul - 0.5).abs() < 1e-6);
    // At end: transparent.
    assert!((anim.evaluate_at(2000, dur_ms).alpha_mul - 0.0).abs() < 1e-6);
}

#[test]
fn move_evaluates_translation() {
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\move(0,0,200,400)}}hello\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    assert!(matches!(anim.tags[0], AnimatedTag::Move { .. }));
    let s_start = anim.evaluate_at(0, 1000);
    assert_eq!(s_start.translate, Some((0.0, 0.0)));
    let s_mid = anim.evaluate_at(500, 1000);
    assert_eq!(s_mid.translate, Some((100.0, 200.0)));
    let s_end = anim.evaluate_at(1000, 1000);
    assert_eq!(s_end.translate, Some((200.0, 400.0)));
}

#[test]
fn frz_static_rotation() {
    let src =
        format!("{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\frz45}}rotated\n");
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    assert!(matches!(anim.tags[0], AnimatedTag::Frz(45.0)));
    let st = anim.evaluate_at(500, 1000);
    assert!((st.rotate_radians - std::f32::consts::FRAC_PI_4).abs() < 1e-5);
}

#[test]
fn t_interpolates_scale_and_rotate() {
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:02.00,Default,,0,0,0,,{{\\t(0,2000,\\fscx200\\frz90)}}grow\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    // Should have one T tag with two inner tags.
    assert_eq!(anim.tags.len(), 1);
    let dur_ms = ((t.cues[0].end_us - t.cues[0].start_us) / 1000) as i32;
    let st0 = anim.evaluate_at(0, dur_ms);
    assert_eq!(st0.scale, (1.0, 1.0));
    assert!(st0.rotate_radians.abs() < 1e-6);

    let st_mid = anim.evaluate_at(1000, dur_ms);
    // Halfway: scale.x = 1.5, rotate = 45deg.
    assert!((st_mid.scale.0 - 1.5).abs() < 1e-6);
    assert!((st_mid.rotate_radians - std::f32::consts::FRAC_PI_4).abs() < 1e-5);

    let st_end = anim.evaluate_at(2000, dur_ms);
    assert!((st_end.scale.0 - 2.0).abs() < 1e-6);
    assert!((st_end.rotate_radians - std::f32::consts::FRAC_PI_2).abs() < 1e-5);
}

#[test]
fn clip_rect_is_extracted() {
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\clip(10,20,300,200)}}clipped\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    let st = anim.evaluate_at(0, 1000);
    assert_eq!(
        st.clip_rect,
        Some(ClipRect {
            x1: 10.0,
            y1: 20.0,
            x2: 300.0,
            y2: 200.0,
        })
    );
}

#[test]
fn blur_is_extracted() {
    let src =
        format!("{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\blur4.5}}fuzzy\n");
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    let st = anim.evaluate_at(0, 1000);
    assert!((st.blur_sigma - 4.5).abs() < 1e-6);
}

#[test]
fn fscx_fscy_static() {
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\fscx200\\fscy50}}stretched\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    let st = anim.evaluate_at(0, 1000);
    assert_eq!(st.scale, (2.0, 0.5));
}

#[test]
fn round_trip_preserves_animated_tags() {
    // After implementing the renderer view, the textual round-trip
    // must still emit the original animated tags verbatim — the
    // encoder side is unchanged.
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:02.00,Default,,0,0,0,,{{\\fad(100,200)\\move(0,0,100,100)\\frz45\\blur2}}hello\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let out = String::from_utf8(ass::write(&t)).unwrap();
    for needle in [
        "\\fad(100,200)",
        "\\move(0,0,100,100)",
        "\\frz45",
        "\\blur2",
        "hello",
    ] {
        assert!(out.contains(needle), "missing {needle:?} in:\n{out}");
    }
    // Re-extraction works on the re-parsed text.
    let t2 = ass::parse(out.as_bytes()).unwrap();
    let anim2 = extract_cue_animation(&t2.cues[0]);
    assert!(anim2
        .tags
        .iter()
        .any(|t| matches!(t, AnimatedTag::Fad { .. })));
    assert!(anim2
        .tags
        .iter()
        .any(|t| matches!(t, AnimatedTag::Move { .. })));
    assert!(anim2.tags.iter().any(|t| matches!(t, AnimatedTag::Frz(_))));
    assert!(anim2.tags.iter().any(|t| matches!(t, AnimatedTag::Blur(_))));
}

#[test]
fn t_inside_dialogue_round_trip_ok() {
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:02.00,Default,,0,0,0,,{{\\t(0,2000,\\fscx200)}}grow\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let out = String::from_utf8(ass::write(&t)).unwrap();
    assert!(out.contains("\\t(0,2000,\\fscx200)"), "got:\n{out}");
}

#[test]
fn move_with_default_times_uses_full_cue() {
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:02.00,Default,,0,0,0,,{{\\move(0,0,100,200)}}drift\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    let dur_ms = ((t.cues[0].end_us - t.cues[0].start_us) / 1000) as i32;
    let st = anim.evaluate_at(1000, dur_ms);
    assert_eq!(st.translate, Some((50.0, 100.0)));
}

#[test]
fn empty_cue_yields_empty_animation() {
    let src = format!("{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,plain text\n");
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    assert!(anim.is_empty());
    let st = anim.evaluate_at(500, 1000);
    assert_eq!(st.alpha_mul, 1.0);
    assert!(st.transform.is_identity());
    assert!(st.translate.is_none());
}

// -----------------------------------------------------------------------
// r76 typed-tag coverage: \bord, \shad, \fax/\fay, \iclip, \be — exercised
// end-to-end through the full parse → extract → evaluate pipeline against
// fixtures that mirror typical karaoke / typesetting subtitle authoring.

#[test]
fn typesetting_bord_shad_blur_combination() {
    // Common typesetting setup: text gets a thick outline + soft shadow
    // + Gaussian blur as a single override block.
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\bord3\\shad2\\blur1.5\\be1}}typeset\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    assert_eq!(anim.tags.len(), 4, "tags: {:?}", anim.tags);
    let st = anim.evaluate_at(500, 1000);
    assert_eq!(st.border, Some((3.0, 3.0)));
    assert_eq!(st.shadow, Some((2.0, 2.0)));
    assert!((st.blur_sigma - 1.5).abs() < 1e-6);
    assert_eq!(st.be_strength, 1);
}

#[test]
fn t_animated_border_growth() {
    // Outline pulses from 0 to 4 over the cue — typical attention-grabber.
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\bord0\\t(0,1000,\\bord4)}}grow\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    // 2 tags: the static \bord0 plus the \t(...) wrapper.
    assert_eq!(anim.tags.len(), 2);
    let st_q = anim.evaluate_at(250, 1000);
    assert_eq!(st_q.border, Some((1.0, 1.0)));
    let st_h = anim.evaluate_at(500, 1000);
    assert_eq!(st_h.border, Some((2.0, 2.0)));
    let st_e = anim.evaluate_at(1000, 1000);
    assert_eq!(st_e.border, Some((4.0, 4.0)));
}

#[test]
fn fax_skew_for_3d_perspective_label() {
    // \fax pseudo-3D skew — common signage typesetting trick.
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:02.00,Default,,0,0,0,,{{\\fax-0.3\\fay0.15}}skewed\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    assert_eq!(anim.tags.len(), 2);
    let st = anim.evaluate_at(500, 2000);
    assert!((st.shear.0 + 0.3).abs() < 1e-6);
    assert!((st.shear.1 - 0.15).abs() < 1e-6);
}

#[test]
fn iclip_rect_inverse_window() {
    // Inverse rectangular clip — used for "subtitle hides behind a
    // chyron" or "fade through a mask" effects.
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:02.00,Default,,0,0,0,,{{\\iclip(100,50,540,250)}}masked\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    assert_eq!(anim.tags.len(), 1);
    let st = anim.evaluate_at(0, 2000);
    let c = st.iclip_rect.unwrap();
    assert_eq!((c.x1, c.y1, c.x2, c.y2), (100.0, 50.0, 540.0, 250.0));
    // Forward clip stays untouched.
    assert!(st.clip_rect.is_none());
}

#[test]
fn iclip_drawing_path_preserved() {
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:02.00,Default,,0,0,0,,{{\\iclip(m 0 0 l 50 0 l 50 50 l 0 50)}}vector\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    assert!(matches!(anim.tags[0], AnimatedTag::IClipDrawing(_)));
    let st = anim.evaluate_at(0, 2000);
    assert!(st.iclip_drawing.as_deref().unwrap().contains("m 0 0"));
}

#[test]
fn xbord_ybord_anamorphic_correction_pattern() {
    // Per Aegisub spec, \xbord+\ybord exist to correct anamorphic
    // displays. Authors sometimes follow with a \bord which overrides
    // both. Pin both behaviours.
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\xbord2\\ybord4}}anamorphic\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    let st = anim.evaluate_at(0, 1000);
    assert_eq!(st.border, Some((2.0, 4.0)));

    let src2 = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\xbord2\\ybord4\\bord1}}override\n"
    );
    let t2 = ass::parse(src2.as_bytes()).unwrap();
    let st2 = extract_cue_animation(&t2.cues[0]).evaluate_at(0, 1000);
    assert_eq!(st2.border, Some((1.0, 1.0)));
}

#[test]
fn xshad_yshad_directional_shadow() {
    // Negative \xshad/\yshad places the shadow to the top-left.
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\xshad-3\\yshad-2}}drop\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    let st = anim.evaluate_at(0, 1000);
    assert_eq!(st.shadow, Some((-3.0, -2.0)));
}

#[test]
fn unknown_tags_alongside_typed_tags_dont_panic() {
    // The base parser stuffs unknown tags into Raw blocks; animate
    // skips what it doesn't recognise. Verify a mix is handled cleanly.
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\bord2\\xyz(1,2)\\fax0.1\\unknown}}mixed\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t.cues[0]);
    // Only \bord and \fax should surface as typed tags.
    assert_eq!(anim.tags.len(), 2);
    let st = anim.evaluate_at(0, 1000);
    assert_eq!(st.border, Some((2.0, 2.0)));
    assert!((st.shear.0 - 0.1).abs() < 1e-6);
}

#[test]
fn typed_tags_survive_round_trip_as_passthrough() {
    // The typed tags aren't re-emitted by the writer (the round-trip
    // path is via Segment::Raw). Confirm the raw block is preserved
    // verbatim so the output script remains semantically identical.
    let src = format!(
        "{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\bord3\\shad2\\iclip(10,10,100,100)}}roundtrip\n"
    );
    let t = ass::parse(src.as_bytes()).unwrap();
    let out = String::from_utf8(ass::write(&t)).unwrap();
    assert!(out.contains("\\bord3"), "out:\n{out}");
    assert!(out.contains("\\shad2"));
    assert!(out.contains("\\iclip(10,10,100,100)"));
    // Re-parse and re-extract: the typed values should match.
    let t2 = ass::parse(out.as_bytes()).unwrap();
    let anim = extract_cue_animation(&t2.cues[0]);
    let st = anim.evaluate_at(0, 1000);
    assert_eq!(st.border, Some((3.0, 3.0)));
    assert_eq!(st.shadow, Some((2.0, 2.0)));
    assert!(st.iclip_rect.is_some());
}

#[test]
fn an_surfaces_alignment_on_render_state() {
    // End-to-end: the base parser previously consumed `\an` and only
    // kept the L/C/R nibble on `cue.positioning.align`; the full
    // numpad value (which tells the renderer which corner to anchor
    // `\pos`/`\move` against) was lost. The animate-module surface
    // now exposes it.
    let src =
        format!("{HEADER}Dialogue: 0,0:00:00.00,0:00:02.00,Default,,0,0,0,,{{\\an7}}top-left\n");
    let t = ass::parse(src.as_bytes()).unwrap();
    let cue = &t.cues[0];
    let anim = extract_cue_animation(cue);
    assert!(
        anim.tags.contains(&AnimatedTag::An(7)),
        "tags: {:?}",
        anim.tags
    );
    let dur_ms = ((cue.end_us - cue.start_us) / 1000) as i32;
    let st = anim.evaluate_at(dur_ms / 2, dur_ms);
    assert_eq!(st.alignment, Some(7));
    // The cue-level CuePosition.align is still set so existing
    // consumers that read cue.positioning unchanged.
    let cp = cue.positioning.as_ref().expect("positioning set");
    assert_eq!(cp.align, oxideav_core::TextAlign::Left);
}

#[test]
fn an_round_trip_preserves_full_numpad() {
    // The writer used to lose the vertical row of the numpad
    // alignment (`\an7` came back as `\an1` in practice — and
    // actually wasn't re-emitted at all because the writer didn't
    // know how to spell it). Now the tag survives via Segment::Raw,
    // so a parse → write → reparse cycle preserves the numpad code.
    let src =
        format!("{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\an8}}top-center\n");
    let t = ass::parse(src.as_bytes()).unwrap();
    let out = String::from_utf8(ass::write(&t)).unwrap();
    assert!(out.contains("\\an8"), "writer output missing \\an8:\n{out}");

    let t2 = ass::parse(out.as_bytes()).unwrap();
    let anim2 = extract_cue_animation(&t2.cues[0]);
    let st2 = anim2.evaluate_at(0, 1000);
    assert_eq!(st2.alignment, Some(8));
}

#[test]
fn legacy_a_surfaces_alignment_on_render_state() {
    // `\a6` is the canonical legacy "top-center" code (sub-position
    // 2 + top-flag 4); per Aegisub it should behave identically to
    // `\an8`. The animate module converts on apply so renderers only
    // ever see the numpad value 1..=9.
    let src =
        format!("{HEADER}Dialogue: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,{{\\a6}}top-center\n");
    let t = ass::parse(src.as_bytes()).unwrap();
    let cue = &t.cues[0];
    let anim = extract_cue_animation(cue);
    assert!(
        anim.tags.contains(&AnimatedTag::A(6)),
        "tags: {:?}",
        anim.tags
    );
    let st = anim.evaluate_at(0, 1000);
    assert_eq!(st.alignment, Some(8));
}

// Suppress an unused-import warning when only some helper types are used.
#[allow(dead_code)]
fn _ensure_cliprect_import_used(_: ClipRect) {}