oximedia-subtitle 0.1.4

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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! TTML (Timed Text Markup Language) subtitle parser and generator.
//!
//! TTML is a W3C standard XML-based format for timed text.
//!
//! ```xml
//! <?xml version="1.0" encoding="UTF-8"?>
//! <tt xmlns="http://www.w3.org/ns/ttml">
//!   <head>
//!     <styling>
//!       <style xml:id="s1" tts:color="white" tts:fontSize="24px"/>
//!     </styling>
//!   </head>
//!   <body>
//!     <div>
//!       <p begin="00:00:01.000" end="00:00:04.000">Hello world!</p>
//!     </div>
//!   </body>
//! </tt>
//! ```

use crate::style::{Alignment, Color, SubtitleStyle};
use crate::{Subtitle, SubtitleError, SubtitleResult};
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::{Reader, Writer};
use std::collections::HashMap;
use std::io::Cursor;

/// TTML namespace.
const TTML_NS: &str = "http://www.w3.org/ns/ttml";
const TTS_NS: &str = "http://www.w3.org/ns/ttml#styling";
const TTP_NS: &str = "http://www.w3.org/ns/ttml#parameter";

/// TTML document with metadata and styles.
#[derive(Clone, Debug)]
pub struct TtmlDocument {
    /// Subtitle cues.
    pub subtitles: Vec<Subtitle>,
    /// Named styles.
    pub styles: HashMap<String, SubtitleStyle>,
    /// Document metadata.
    pub metadata: HashMap<String, String>,
}

/// Parse TTML subtitle file.
///
/// # Errors
///
/// Returns error if the file is not valid TTML format.
pub fn parse(data: &[u8]) -> SubtitleResult<Vec<Subtitle>> {
    let doc = parse_ttml(data)?;
    Ok(doc.subtitles)
}

/// Parse TTML document with full metadata.
///
/// # Errors
///
/// Returns error if parsing fails.
pub fn parse_ttml(data: &[u8]) -> SubtitleResult<TtmlDocument> {
    let mut reader = Reader::from_reader(data);
    reader.config_mut().trim_text(true);

    let mut styles = HashMap::new();
    let metadata = HashMap::new();
    let mut subtitles = Vec::new();
    let mut in_styling = false;
    let mut _in_metadata = false;
    let mut in_body = false;
    let mut current_p: Option<PElement> = None;
    let mut buf = Vec::new();

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(e)) | Ok(Event::Empty(e)) => {
                let elem_name = e.name();
                let name = std::str::from_utf8(elem_name.as_ref())
                    .map_err(|e| SubtitleError::ParseError(format!("UTF-8 error: {e}")))?;

                match name {
                    "styling" => in_styling = true,
                    "metadata" => _in_metadata = true,
                    "body" => in_body = true,
                    "style" if in_styling => {
                        if let Some((id, style)) = parse_style_element(&e)? {
                            styles.insert(id, style);
                        }
                    }
                    "p" if in_body => {
                        current_p = Some(parse_p_element(&e)?);
                    }
                    _ => {}
                }
            }
            Ok(Event::End(e)) => {
                let elem_name = e.name();
                let name = std::str::from_utf8(elem_name.as_ref())
                    .map_err(|e| SubtitleError::ParseError(format!("UTF-8 error: {e}")))?;

                match name {
                    "styling" => in_styling = false,
                    "metadata" => _in_metadata = false,
                    "body" => in_body = false,
                    "p" if in_body => {
                        if let Some(mut p) = current_p.take() {
                            // Apply style if referenced
                            if let Some(style_id) = &p.style_id {
                                if let Some(style) = styles.get(style_id) {
                                    p.subtitle.style = Some(style.clone());
                                }
                            }
                            subtitles.push(p.subtitle);
                        }
                    }
                    _ => {}
                }
            }
            Ok(Event::Text(e)) => {
                if let Some(p) = &mut current_p {
                    let text = reader
                        .decoder()
                        .decode(e.as_ref())
                        .map_err(|e| SubtitleError::ParseError(format!("Decode error: {e}")))?;
                    if !p.subtitle.text.is_empty() {
                        p.subtitle.text.push(' ');
                    }
                    p.subtitle.text.push_str(&text);
                }
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(SubtitleError::ParseError(format!("XML parse error: {e}"))),
            _ => {}
        }

        buf.clear();
    }

    Ok(TtmlDocument {
        subtitles,
        styles,
        metadata,
    })
}

/// Intermediate structure for `<p>` element parsing.
struct PElement {
    subtitle: Subtitle,
    style_id: Option<String>,
}

/// Parse `<style>` element.
fn parse_style_element(element: &BytesStart) -> SubtitleResult<Option<(String, SubtitleStyle)>> {
    let mut id = None;
    let mut style = SubtitleStyle::default();

    for attr in element.attributes() {
        let attr = attr.map_err(|e| SubtitleError::ParseError(format!("Attribute error: {e}")))?;
        let key = std::str::from_utf8(attr.key.as_ref())
            .map_err(|e| SubtitleError::ParseError(format!("UTF-8 error: {e}")))?;
        let value = std::str::from_utf8(&attr.value)
            .map_err(|e| SubtitleError::ParseError(format!("UTF-8 error: {e}")))?;

        match key {
            "xml:id" | "id" => id = Some(value.to_string()),
            "tts:color" | "color" => {
                if let Ok(color) = parse_ttml_color(value) {
                    style.primary_color = color;
                }
            }
            "tts:fontSize" | "fontSize" => {
                if let Some(size) = parse_font_size(value) {
                    style.font_size = size;
                }
            }
            "tts:fontFamily" | "fontFamily" => {
                // Font family is ignored - we use the provided font
            }
            "tts:fontWeight" | "fontWeight" => {
                style.font_weight = match value {
                    "bold" | "700" => crate::style::FontWeight::Bold,
                    "normal" | "400" => crate::style::FontWeight::Normal,
                    "extra-bold" | "800" => crate::style::FontWeight::ExtraBold,
                    "semi-bold" | "600" => crate::style::FontWeight::SemiBold,
                    "light" | "300" => crate::style::FontWeight::Light,
                    _ => crate::style::FontWeight::Normal,
                };
            }
            "tts:fontStyle" | "fontStyle" => {
                style.font_style = match value {
                    "italic" => crate::style::FontStyle::Italic,
                    "oblique" => crate::style::FontStyle::Oblique,
                    _ => crate::style::FontStyle::Normal,
                };
            }
            "tts:textAlign" | "textAlign" => {
                style.alignment = match value {
                    "left" | "start" => Alignment::Left,
                    "center" => Alignment::Center,
                    "right" | "end" => Alignment::Right,
                    _ => Alignment::Center,
                };
            }
            _ => {}
        }
    }

    if let Some(id) = id {
        Ok(Some((id, style)))
    } else {
        Ok(None)
    }
}

/// Parse `<p>` element.
fn parse_p_element(element: &BytesStart) -> SubtitleResult<PElement> {
    let mut begin: Option<i64> = None;
    let mut end: Option<i64> = None;
    let mut style_id: Option<String> = None;

    for attr in element.attributes() {
        let attr = attr.map_err(|e| SubtitleError::ParseError(format!("Attribute error: {e}")))?;
        let key = std::str::from_utf8(attr.key.as_ref())
            .map_err(|e| SubtitleError::ParseError(format!("UTF-8 error: {e}")))?;
        let value = std::str::from_utf8(&attr.value)
            .map_err(|e| SubtitleError::ParseError(format!("UTF-8 error: {e}")))?;

        match key {
            "begin" => begin = parse_ttml_time(value),
            "end" => end = parse_ttml_time(value),
            "style" => style_id = Some(value.to_string()),
            _ => {}
        }
    }

    let start_time = begin
        .ok_or_else(|| SubtitleError::InvalidTimestamp("Missing begin attribute".to_string()))?;
    let end_time =
        end.ok_or_else(|| SubtitleError::InvalidTimestamp("Missing end attribute".to_string()))?;

    Ok(PElement {
        subtitle: Subtitle::new(start_time, end_time, String::new()),
        style_id,
    })
}

/// Parse TTML time expression.
///
/// Supports:
/// - Clock time: `HH:MM:SS.mmm`
/// - Offset time: `12.5s`, `100ms`, `1h`, `30m`
fn parse_ttml_time(time: &str) -> Option<i64> {
    let time = time.trim();

    // Try clock time format (HH:MM:SS.mmm)
    if time.contains(':') {
        return parse_clock_time(time);
    }

    // Try offset time (e.g., "12.5s", "100ms")
    parse_offset_time(time)
}

/// Parse clock time format (HH:MM:SS.mmm or MM:SS.mmm).
fn parse_clock_time(time: &str) -> Option<i64> {
    let parts: Vec<&str> = time.split(':').collect();

    match parts.len() {
        2 => {
            // MM:SS or MM:SS.mmm
            let minutes: i64 = parts[0].parse().ok()?;
            let seconds_parts: Vec<&str> = parts[1].split('.').collect();
            let seconds: i64 = seconds_parts[0].parse().ok()?;
            let millis: i64 = if seconds_parts.len() > 1 {
                // Pad or truncate to 3 digits
                let ms_str = format!("{:0<3}", &seconds_parts[1][..seconds_parts[1].len().min(3)]);
                ms_str.parse().ok()?
            } else {
                0
            };
            Some(minutes * 60000 + seconds * 1000 + millis)
        }
        3 => {
            // HH:MM:SS or HH:MM:SS.mmm
            let hours: i64 = parts[0].parse().ok()?;
            let minutes: i64 = parts[1].parse().ok()?;
            let seconds_parts: Vec<&str> = parts[2].split('.').collect();
            let seconds: i64 = seconds_parts[0].parse().ok()?;
            let millis: i64 = if seconds_parts.len() > 1 {
                let ms_str = format!("{:0<3}", &seconds_parts[1][..seconds_parts[1].len().min(3)]);
                ms_str.parse().ok()?
            } else {
                0
            };
            Some(hours * 3600000 + minutes * 60000 + seconds * 1000 + millis)
        }
        _ => None,
    }
}

/// Parse offset time (e.g., "12.5s", "100ms", "1h", "30m").
fn parse_offset_time(time: &str) -> Option<i64> {
    let time = time.trim();

    if let Some(value_str) = time.strip_suffix("ms") {
        // Milliseconds
        let value: f64 = value_str.parse().ok()?;
        return Some(value as i64);
    }

    if let Some(value_str) = time.strip_suffix('s') {
        // Seconds
        let value: f64 = value_str.parse().ok()?;
        return Some((value * 1000.0) as i64);
    }

    if let Some(value_str) = time.strip_suffix('m') {
        // Minutes
        let value: f64 = value_str.parse().ok()?;
        return Some((value * 60000.0) as i64);
    }

    if let Some(value_str) = time.strip_suffix('h') {
        // Hours
        let value: f64 = value_str.parse().ok()?;
        return Some((value * 3600000.0) as i64);
    }

    None
}

/// Parse TTML color.
fn parse_ttml_color(color: &str) -> Result<Color, SubtitleError> {
    let color = color.trim();

    // Named colors
    match color.to_lowercase().as_str() {
        "white" => return Ok(Color::white()),
        "black" => return Ok(Color::black()),
        "red" => return Ok(Color::rgb(255, 0, 0)),
        "green" => return Ok(Color::rgb(0, 255, 0)),
        "blue" => return Ok(Color::rgb(0, 0, 255)),
        "yellow" => return Ok(Color::rgb(255, 255, 0)),
        "cyan" => return Ok(Color::rgb(0, 255, 255)),
        "magenta" => return Ok(Color::rgb(255, 0, 255)),
        _ => {}
    }

    // Hex color
    if color.starts_with('#') {
        return Color::from_hex(color);
    }

    // RGB/RGBA function
    if color.starts_with("rgb") {
        return parse_rgb_color(color);
    }

    Err(SubtitleError::InvalidColor(color.to_string()))
}

/// Parse RGB/RGBA color function.
fn parse_rgb_color(color: &str) -> Result<Color, SubtitleError> {
    let color = color.trim();

    let is_rgba = color.starts_with("rgba");
    let start = if is_rgba { 5 } else { 4 };

    let inner = color
        .get(start..color.len().saturating_sub(1))
        .ok_or_else(|| SubtitleError::InvalidColor(color.to_string()))?;

    let parts: Vec<&str> = inner.split(',').map(str::trim).collect();

    if (is_rgba && parts.len() != 4) || (!is_rgba && parts.len() != 3) {
        return Err(SubtitleError::InvalidColor(color.to_string()));
    }

    let r = parts[0]
        .parse::<u8>()
        .map_err(|_| SubtitleError::InvalidColor(color.to_string()))?;
    let g = parts[1]
        .parse::<u8>()
        .map_err(|_| SubtitleError::InvalidColor(color.to_string()))?;
    let b = parts[2]
        .parse::<u8>()
        .map_err(|_| SubtitleError::InvalidColor(color.to_string()))?;

    let a = if is_rgba {
        let alpha_f = parts[3]
            .parse::<f32>()
            .map_err(|_| SubtitleError::InvalidColor(color.to_string()))?;
        (alpha_f * 255.0) as u8
    } else {
        255
    };

    Ok(Color::new(r, g, b, a))
}

/// Parse font size (e.g., "24px", "1.5em", "120%").
fn parse_font_size(size: &str) -> Option<f32> {
    let size = size.trim();

    if let Some(px) = size.strip_suffix("px") {
        return px.parse().ok();
    }

    if let Some(em) = size.strip_suffix("em") {
        let value: f32 = em.parse().ok()?;
        // Assume base font size of 24px
        return Some(value * 24.0);
    }

    if let Some(pct) = size.strip_suffix('%') {
        let value: f32 = pct.parse().ok()?;
        // Assume base font size of 24px
        return Some(value * 24.0 / 100.0);
    }

    // Try plain number (assume pixels)
    size.parse().ok()
}

/// Format milliseconds as TTML clock time.
#[must_use]
pub fn format_timestamp(ms: i64) -> String {
    let hours = ms / 3600000;
    let minutes = (ms % 3600000) / 60000;
    let seconds = (ms % 60000) / 1000;
    let millis = ms % 1000;

    format!("{hours:02}:{minutes:02}:{seconds:02}.{millis:03}")
}

/// Write subtitles in TTML format.
///
/// # Errors
///
/// Returns error if writing fails.
pub fn write(subtitles: &[Subtitle]) -> SubtitleResult<String> {
    let mut writer = Writer::new(Cursor::new(Vec::new()));

    // XML declaration
    writer
        .write_event(Event::Decl(quick_xml::events::BytesDecl::new(
            "1.0",
            Some("UTF-8"),
            None,
        )))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    // Root <tt> element
    let mut tt_elem = BytesStart::new("tt");
    tt_elem.push_attribute(("xmlns", TTML_NS));
    tt_elem.push_attribute(("xmlns:tts", TTS_NS));
    tt_elem.push_attribute(("xmlns:ttp", TTP_NS));
    tt_elem.push_attribute(("xml:lang", "en"));
    writer
        .write_event(Event::Start(tt_elem))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    // <head> with default style
    writer
        .write_event(Event::Start(BytesStart::new("head")))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    writer
        .write_event(Event::Start(BytesStart::new("styling")))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    let mut style_elem = BytesStart::new("style");
    style_elem.push_attribute(("xml:id", "default"));
    style_elem.push_attribute(("tts:color", "white"));
    style_elem.push_attribute(("tts:fontSize", "24px"));
    style_elem.push_attribute(("tts:fontFamily", "sans-serif"));
    style_elem.push_attribute(("tts:textAlign", "center"));
    writer
        .write_event(Event::Empty(style_elem))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    writer
        .write_event(Event::End(BytesEnd::new("styling")))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    writer
        .write_event(Event::End(BytesEnd::new("head")))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    // <body>
    writer
        .write_event(Event::Start(BytesStart::new("body")))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    writer
        .write_event(Event::Start(BytesStart::new("div")))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    // Write subtitle cues
    for subtitle in subtitles {
        let mut p_elem = BytesStart::new("p");
        p_elem.push_attribute(("begin", format_timestamp(subtitle.start_time).as_str()));
        p_elem.push_attribute(("end", format_timestamp(subtitle.end_time).as_str()));
        p_elem.push_attribute(("style", "default"));

        writer
            .write_event(Event::Start(p_elem))
            .map_err(|e| SubtitleError::IoError(e.to_string()))?;

        writer
            .write_event(Event::Text(BytesText::new(&subtitle.text)))
            .map_err(|e| SubtitleError::IoError(e.to_string()))?;

        writer
            .write_event(Event::End(BytesEnd::new("p")))
            .map_err(|e| SubtitleError::IoError(e.to_string()))?;
    }

    writer
        .write_event(Event::End(BytesEnd::new("div")))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    writer
        .write_event(Event::End(BytesEnd::new("body")))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    writer
        .write_event(Event::End(BytesEnd::new("tt")))
        .map_err(|e| SubtitleError::IoError(e.to_string()))?;

    let result = writer.into_inner().into_inner();
    String::from_utf8(result).map_err(|e| SubtitleError::IoError(e.to_string()))
}

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

    #[test]
    fn test_parse_clock_time() {
        assert_eq!(parse_clock_time("00:00:01.000"), Some(1000));
        assert_eq!(parse_clock_time("00:01:30.500"), Some(90500));
        assert_eq!(parse_clock_time("01:30:45.123"), Some(5445123));
        assert_eq!(parse_clock_time("10:20.500"), Some(620500));
    }

    #[test]
    fn test_parse_offset_time() {
        assert_eq!(parse_offset_time("1000ms"), Some(1000));
        assert_eq!(parse_offset_time("1.5s"), Some(1500));
        assert_eq!(parse_offset_time("2m"), Some(120000));
        assert_eq!(parse_offset_time("1h"), Some(3600000));
    }

    #[test]
    fn test_parse_ttml_color() {
        assert_eq!(
            parse_ttml_color("white").expect("should succeed in test"),
            Color::white()
        );
        assert_eq!(
            parse_ttml_color("#FFFFFF").expect("should succeed in test"),
            Color::white()
        );
        assert_eq!(
            parse_ttml_color("rgb(255,255,255)").expect("should succeed in test"),
            Color::white()
        );
        assert_eq!(
            parse_ttml_color("rgba(255,255,255,1.0)").expect("should succeed in test"),
            Color::white()
        );
    }

    #[test]
    fn test_parse_font_size() {
        assert_eq!(parse_font_size("24px"), Some(24.0));
        assert_eq!(parse_font_size("1.5em"), Some(36.0));
        assert_eq!(parse_font_size("150%"), Some(36.0));
    }

    #[test]
    fn test_format_timestamp() {
        assert_eq!(format_timestamp(1000), "00:00:01.000");
        assert_eq!(format_timestamp(90500), "00:01:30.500");
        assert_eq!(format_timestamp(5445123), "01:30:45.123");
    }

    #[test]
    fn test_parse_simple_ttml() {
        let ttml = r#"<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="http://www.w3.org/ns/ttml">
  <body>
    <div>
      <p begin="00:00:01.000" end="00:00:04.000">Hello world!</p>
    </div>
  </body>
</tt>"#;

        let doc = parse_ttml(ttml.as_bytes()).expect("should succeed in test");
        assert_eq!(doc.subtitles.len(), 1);
        assert_eq!(doc.subtitles[0].start_time, 1000);
        assert_eq!(doc.subtitles[0].end_time, 4000);
        assert_eq!(doc.subtitles[0].text, "Hello world!");
    }

    #[test]
    fn test_write_ttml() {
        let subtitles = vec![
            Subtitle::new(1000, 4000, "Hello world!".to_string()),
            Subtitle::new(5000, 8000, "Second subtitle".to_string()),
        ];

        let output = write(&subtitles).expect("should succeed in test");
        assert!(output.contains("<tt"));
        assert!(output.contains("begin=\"00:00:01.000\""));
        assert!(output.contains("end=\"00:00:04.000\""));
        assert!(output.contains("Hello world!"));
        assert!(output.contains("Second subtitle"));
    }
}