oximedia-subtitle 0.1.8

Subtitle and closed caption rendering for OxiMedia
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! WebVTT region definitions and extended cue settings with builder API.
//!
//! Provides [`WebVttRegion`] and [`WebVttCueSettings`] with full builder patterns,
//! serialization, and parsing for the WebVTT specification.
//!
//! ## Example
//! ```
//! use oximedia_subtitle::formats::webvtt_regions::{WebVttRegion, WebVttCueSettings};
//!
//! let region = WebVttRegion::new("bottom")
//!     .with_width(40.0)
//!     .with_lines(3)
//!     .with_scroll_up();
//! assert_eq!(region.scroll, oximedia_subtitle::formats::webvtt_regions::WebVttScroll::Up);
//!
//! let settings = WebVttCueSettings::parse("line:5 align:start");
//! assert_eq!(settings.align, oximedia_subtitle::formats::webvtt_regions::WebVttAlign::Start);
//! ```

// ============================================================================
// Region types
// ============================================================================

/// Scroll direction for a WebVTT region.
#[derive(Debug, Clone, PartialEq)]
pub enum WebVttScroll {
    /// No scrolling — region is static.
    None,
    /// Lines scroll upward as new lines arrive.
    Up,
}

impl Default for WebVttScroll {
    fn default() -> Self {
        Self::None
    }
}

/// A WebVTT region definition (W3C WebVTT spec §3.5).
///
/// Regions allow cues to be placed in named areas of the viewport.
#[derive(Debug, Clone)]
pub struct WebVttRegion {
    /// Unique identifier for this region.
    pub id: String,
    /// Width of the region as a percentage of the viewport (0.0–100.0).
    pub width_pct: f32,
    /// Maximum number of lines visible at once.
    pub lines: u32,
    /// Region anchor point (x%, y%) within the region box.
    pub region_anchor: (f32, f32),
    /// Viewport anchor point (x%, y%) in the viewport coordinate space.
    pub viewport_anchor: (f32, f32),
    /// Scroll direction.
    pub scroll: WebVttScroll,
}

impl WebVttRegion {
    /// Create a new region with the given id and sensible defaults.
    ///
    /// Defaults: width=100%, lines=3, region_anchor=(0,100), viewport_anchor=(0,100), scroll=None.
    #[must_use]
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            width_pct: 100.0,
            lines: 3,
            region_anchor: (0.0, 100.0),
            viewport_anchor: (0.0, 100.0),
            scroll: WebVttScroll::None,
        }
    }

    /// Set the width percentage of this region.
    #[must_use]
    pub fn with_width(mut self, pct: f32) -> Self {
        self.width_pct = pct.clamp(0.0, 100.0);
        self
    }

    /// Set the number of visible lines.
    #[must_use]
    pub fn with_lines(mut self, lines: u32) -> Self {
        self.lines = lines;
        self
    }

    /// Set the region anchor (x%, y%).
    #[must_use]
    pub fn with_region_anchor(mut self, x: f32, y: f32) -> Self {
        self.region_anchor = (x, y);
        self
    }

    /// Set the viewport anchor (x%, y%).
    #[must_use]
    pub fn with_viewport_anchor(mut self, x: f32, y: f32) -> Self {
        self.viewport_anchor = (x, y);
        self
    }

    /// Enable scroll-up behaviour.
    #[must_use]
    pub fn with_scroll_up(mut self) -> Self {
        self.scroll = WebVttScroll::Up;
        self
    }

    /// Serialise this region to WebVTT format.
    #[must_use]
    pub fn to_webvtt(&self) -> String {
        let mut out = String::from("REGION\n");
        out.push_str(&format!("id:{}\n", self.id));
        out.push_str(&format!("width:{:.1}%\n", self.width_pct));
        out.push_str(&format!("lines:{}\n", self.lines));
        out.push_str(&format!(
            "regionanchor:{:.1}%,{:.1}%\n",
            self.region_anchor.0, self.region_anchor.1
        ));
        out.push_str(&format!(
            "viewportanchor:{:.1}%,{:.1}%\n",
            self.viewport_anchor.0, self.viewport_anchor.1
        ));
        if self.scroll == WebVttScroll::Up {
            out.push_str("scroll:up\n");
        }
        out
    }
}

// ============================================================================
// Cue settings types
// ============================================================================

/// Vertical text writing direction for WebVTT cues.
#[derive(Debug, Clone, PartialEq)]
pub enum WebVttVertical {
    /// Normal horizontal text (default).
    Horizontal,
    /// Vertical columns flow left-to-right ("lr").
    LeftToRight,
    /// Vertical columns flow right-to-left ("rl").
    RightToLeft,
}

impl Default for WebVttVertical {
    fn default() -> Self {
        Self::Horizontal
    }
}

/// Line positioning for a WebVTT cue.
#[derive(Debug, Clone, PartialEq)]
pub enum WebVttLine {
    /// Browser determines line placement automatically.
    Auto,
    /// Snap-to-lines integer (positive = from top, negative = from bottom).
    Number(i32),
    /// Percentage distance from the top of the video.
    Percentage(f32),
}

impl Default for WebVttLine {
    fn default() -> Self {
        Self::Auto
    }
}

/// Text alignment within a WebVTT cue box.
#[derive(Debug, Clone, PartialEq)]
pub enum WebVttAlign {
    /// Align to the start of the text direction.
    Start,
    /// Center alignment.
    Center,
    /// Align to the end of the text direction.
    End,
    /// Left alignment (absolute).
    Left,
    /// Right alignment (absolute).
    Right,
}

impl Default for WebVttAlign {
    fn default() -> Self {
        Self::Center
    }
}

/// Extended cue settings parsed from a WebVTT timing line.
///
/// Corresponds to the settings block after `-->` on a WebVTT cue timing line.
#[derive(Debug, Clone)]
pub struct WebVttCueSettings {
    /// Region to anchor this cue into.
    pub region_id: Option<String>,
    /// Vertical text writing direction.
    pub vertical: WebVttVertical,
    /// Line placement.
    pub line: WebVttLine,
    /// Horizontal position percentage.
    pub position_pct: Option<f32>,
    /// Cue box size as a percentage of the video width (default 100%).
    pub size_pct: f32,
    /// Text alignment within the cue box.
    pub align: WebVttAlign,
}

impl Default for WebVttCueSettings {
    fn default() -> Self {
        Self {
            region_id: None,
            vertical: WebVttVertical::Horizontal,
            line: WebVttLine::Auto,
            position_pct: None,
            size_pct: 100.0,
            align: WebVttAlign::Center,
        }
    }
}

impl WebVttCueSettings {
    /// Parse cue settings from a settings string such as `"line:5 align:start position:10%"`.
    ///
    /// Unknown tokens are silently ignored.
    #[must_use]
    pub fn parse(s: &str) -> Self {
        let mut settings = Self::default();

        for token in s.split_whitespace() {
            if let Some(val) = token.strip_prefix("region:") {
                settings.region_id = Some(val.to_string());
            } else if let Some(val) = token.strip_prefix("vertical:") {
                settings.vertical = match val {
                    "lr" => WebVttVertical::LeftToRight,
                    "rl" => WebVttVertical::RightToLeft,
                    _ => WebVttVertical::Horizontal,
                };
            } else if let Some(val) = token.strip_prefix("line:") {
                settings.line = parse_line_value(val);
            } else if let Some(val) = token.strip_prefix("position:") {
                settings.position_pct = parse_percent(val);
            } else if let Some(val) = token.strip_prefix("size:") {
                if let Some(pct) = parse_percent(val) {
                    settings.size_pct = pct;
                }
            } else if let Some(val) = token.strip_prefix("align:") {
                settings.align = match val {
                    "start" => WebVttAlign::Start,
                    "center" | "middle" => WebVttAlign::Center,
                    "end" => WebVttAlign::End,
                    "left" => WebVttAlign::Left,
                    "right" => WebVttAlign::Right,
                    _ => WebVttAlign::Center,
                };
            }
        }

        settings
    }

    /// Serialise the settings back to a settings string (only non-default values are emitted).
    #[must_use]
    pub fn to_settings_string(&self) -> String {
        let mut parts: Vec<String> = Vec::new();

        if let Some(ref rid) = self.region_id {
            parts.push(format!("region:{rid}"));
        }

        match self.vertical {
            WebVttVertical::LeftToRight => parts.push("vertical:lr".to_string()),
            WebVttVertical::RightToLeft => parts.push("vertical:rl".to_string()),
            WebVttVertical::Horizontal => {}
        }

        match &self.line {
            WebVttLine::Auto => {}
            WebVttLine::Number(n) => parts.push(format!("line:{n}")),
            WebVttLine::Percentage(p) => parts.push(format!("line:{p:.1}%")),
        }

        if let Some(pos) = self.position_pct {
            parts.push(format!("position:{pos:.1}%"));
        }

        if (self.size_pct - 100.0).abs() > f32::EPSILON {
            parts.push(format!("size:{:.1}%", self.size_pct));
        }

        if self.align != WebVttAlign::Center {
            let a = match self.align {
                WebVttAlign::Start => "start",
                WebVttAlign::End => "end",
                WebVttAlign::Left => "left",
                WebVttAlign::Right => "right",
                WebVttAlign::Center => "center",
            };
            parts.push(format!("align:{a}"));
        }

        parts.join(" ")
    }
}

// ============================================================================
// Private helpers
// ============================================================================

/// Parse a percentage string like "50%" → 50.0.
fn parse_percent(s: &str) -> Option<f32> {
    s.trim_end_matches('%').parse::<f32>().ok()
}

/// Parse a line value: "5" (integer), "80%" (percentage), or "auto".
fn parse_line_value(s: &str) -> WebVttLine {
    if s == "auto" {
        return WebVttLine::Auto;
    }
    if s.ends_with('%') {
        if let Some(pct) = parse_percent(s) {
            return WebVttLine::Percentage(pct);
        }
    }
    if let Ok(n) = s.parse::<i32>() {
        return WebVttLine::Number(n);
    }
    WebVttLine::Auto
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_region_default_values() {
        let r = WebVttRegion::new("test");
        assert_eq!(r.id, "test");
        assert!((r.width_pct - 100.0).abs() < f32::EPSILON);
        assert_eq!(r.lines, 3);
        assert_eq!(r.region_anchor, (0.0, 100.0));
        assert_eq!(r.viewport_anchor, (0.0, 100.0));
        assert_eq!(r.scroll, WebVttScroll::None);
    }

    #[test]
    fn test_region_with_scroll_up() {
        let r = WebVttRegion::new("r1").with_scroll_up();
        assert_eq!(r.scroll, WebVttScroll::Up);
    }

    #[test]
    fn test_region_builder_chain() {
        let r = WebVttRegion::new("bottom")
            .with_width(40.0)
            .with_lines(5)
            .with_region_anchor(0.0, 100.0)
            .with_viewport_anchor(10.0, 90.0)
            .with_scroll_up();

        assert!((r.width_pct - 40.0).abs() < f32::EPSILON);
        assert_eq!(r.lines, 5);
        assert_eq!(r.viewport_anchor, (10.0, 90.0));
        assert_eq!(r.scroll, WebVttScroll::Up);
    }

    #[test]
    fn test_region_to_webvtt_contains_required_fields() {
        let r = WebVttRegion::new("sub").with_width(60.0).with_scroll_up();
        let s = r.to_webvtt();
        assert!(s.contains("REGION"));
        assert!(s.contains("id:sub"));
        assert!(s.contains("width:60.0%"));
        assert!(s.contains("scroll:up"));
    }

    #[test]
    fn test_parse_settings_line_and_align() {
        let settings = WebVttCueSettings::parse("line:5 align:start");
        assert_eq!(settings.line, WebVttLine::Number(5));
        assert_eq!(settings.align, WebVttAlign::Start);
    }

    #[test]
    fn test_parse_settings_vertical_rl() {
        let settings = WebVttCueSettings::parse("vertical:rl");
        assert_eq!(settings.vertical, WebVttVertical::RightToLeft);
    }

    #[test]
    fn test_parse_settings_vertical_lr() {
        let settings = WebVttCueSettings::parse("vertical:lr");
        assert_eq!(settings.vertical, WebVttVertical::LeftToRight);
    }

    #[test]
    fn test_parse_settings_position_percent() {
        let settings = WebVttCueSettings::parse("position:10%");
        assert!(settings.position_pct.is_some());
        let p = settings.position_pct.expect("position_pct should be set");
        assert!((p - 10.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_parse_settings_region_id() {
        let settings = WebVttCueSettings::parse("region:bottom line:0");
        assert_eq!(settings.region_id, Some("bottom".to_string()));
        assert_eq!(settings.line, WebVttLine::Number(0));
    }

    #[test]
    fn test_settings_to_string_round_trip() {
        // Build settings, serialise, then re-parse and compare
        let mut orig = WebVttCueSettings::default();
        orig.vertical = WebVttVertical::RightToLeft;
        orig.line = WebVttLine::Number(-1);
        orig.align = WebVttAlign::Start;
        orig.position_pct = Some(25.0);
        orig.size_pct = 80.0;

        let s = orig.to_settings_string();
        let parsed = WebVttCueSettings::parse(&s);

        assert_eq!(parsed.vertical, WebVttVertical::RightToLeft);
        assert_eq!(parsed.line, WebVttLine::Number(-1));
        assert_eq!(parsed.align, WebVttAlign::Start);
        let pos = parsed.position_pct.expect("position_pct present");
        assert!((pos - 25.0).abs() < 0.01);
        assert!((parsed.size_pct - 80.0).abs() < 0.01);
    }

    #[test]
    fn test_settings_default_no_output() {
        // Default settings should produce empty string (all defaults suppressed)
        let settings = WebVttCueSettings::default();
        let s = settings.to_settings_string();
        assert!(s.is_empty());
    }

    #[test]
    fn test_parse_settings_line_percentage() {
        let settings = WebVttCueSettings::parse("line:80%");
        assert_eq!(settings.line, WebVttLine::Percentage(80.0));
    }

    #[test]
    fn test_parse_settings_size() {
        let settings = WebVttCueSettings::parse("size:50%");
        assert!((settings.size_pct - 50.0).abs() < f32::EPSILON);
    }
}