mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 19:47:53 +00:00
* craft-captions: TTML cue text collapses only XML whitespace and keeps edge line breaks Ported from storytold/filmcraft#3145. char::is_whitespace also matched U+00A0, U+2009 and U+3000, so no-break spaces became ordinary spaces on import, and a leading or trailing <br/> was trimmed away. * craft-captions: TTML/DFXP export drops characters XML 1.0 forbids Ported from storytold/filmcraft#4467. A control character in cue text made the exported file unreadable. The filter also covers U+FFFE and U+FFFF, which the review of that pull request asked for. * craft-captions: WebVTT cue-text arrow escape is reversible Ported from storytold/filmcraft#4437. A literal --> in cue text came back as --> after a write and parse; the writer now adds one amp; level and the reader removes exactly one. * craft-captions: a WebVTT speaker name with > no longer spills into the cue text Ported from storytold/filmcraft#4470. The writer puts the lookalike U+203A in place of > inside the voice tag. Also from that pull request's review: a line break in a name is written as a space, and a name ending in - is kept apart from the tag's closing >. * craft-captions: an SRT text line that looks like a timing line survives a round trip Ported from storytold/filmcraft#4448. The writer marks such a line with a leading zero-width space and the reader removes it, so the cue is not split. * craft-captions: back-to-back SCC captions with the same text stay separate cues Ported from storytold/filmcraft#3808. Every End of Caption now closes the open cue. MCC's CEA-608 path uses the same decoder. * craft-captions: an SCC pop-on load may run across the previous caption's erase Ported from storytold/filmcraft#1837 (filmcraft#1791). A caption that closely follows another popped on about a second late, because its load had to be one unbroken run of free frames. Doubled codes stay adjacent; pauses are allowed between them. * craft-captions: CEA-708 export starts each caption at its window's top left Ported from storytold/filmcraft#4043 (filmcraft#4042). Each window load now sends DeleteWindows before DefineWindow in place of the ClearWindows after it: a window made anew is empty with the pen at its top left in any decoder, for the same number of bytes, so cue timing is unchanged. * craft-captions: TTML/DFXP export keeps literal < and > text Ported from storytold/filmcraft#2830. The writer treated every < with a later > as a tag and wrote nothing for unknown names, so "1 < 2 and 3 > 2" lost its middle. Only a well-formed tag is markup now; a > inside a quoted attribute value does not end the tag. * craft-captions: TTML/DFXP export keeps cue IDs unique and valid Unicode IDs as written Ported from the TTML commit of storytold/filmcraft#1839. A cue named s1 could collide with the style ID, and a generated c1 could take the ID of a later cue. Valid explicit IDs are reserved first; missing, invalid or repeated ones get a replacement that is in no one's way. IDs are checked as XML NCNames. * craft-captions: compare the craft-xml and roxmltree views of TTML documents The TTML reader moved from roxmltree to craft-xml. This dev-only test checks that both hand it the same elements, attributes and text, by namespace, on TTML documents and on the writer's output. roxmltree becomes a dev-dependency, which the cue ID tests use too. * craft-captions: use craft-timeline's exact time instead of a private copy craft-captions carried a 290-line subset of the tick, frame rate and timecode field code because craft-libs had none when it was added. craft-timeline now has the whole module with the same tick unit. craft_captions::time is a re-export of craft_timeline::time, so every public path still resolves and the types are now the same ones craft-timeline's callers hold. * craft-captions: SCC and MCC time codes count from the document's start timecode Ported from the caption-format part of storytold/filmcraft#4131 (filmcraft#4130). Document::start_timecode is the label of time zero; the SCC and MCC writers count from the 29.97 frame with the same label, so the label carries across rates. Readers note the rate and drop-frame mode of a file's labels in Document::labels, and Document::count_from counts cues back from a start time code. charset_warnings names the time codes the file has, which that pull request's review found missing. * craft-captions: MCC export can write a packet on every frame Ported from the caption-format part of storytold/filmcraft#4037 (filmcraft#4024). WriteOptions::every_frame_until fills every 29.97 fps frame before that time that has no caption data with a packet of CEA-608 nulls and CEA-708 padding, as SDI and frame-wrapped MXF carry one per frame. Off by default, at most 24 hours (mcc::MAX_PADDED_FRAMES), and write_to streams the file line by line. * craft-captions: README has its MCC writer section, with the every-frame option The section's text was cut off when the crate was added; only a stray heading marker was left. * craft-captions: export warnings name STL's ? and text cut to fit Ported from storytold/filmcraft#4157 (filmcraft#4155, filmcraft#4156). charset_warnings now names the characters EBU STL writes as ? (from the writer's own encoder, at the STL file's time code) and each caption that CEA-608's 15 rows or the CEA-708 window's 12 rows cut, with how much it lost. The CEA-708 writer loads only the 12 rows its window holds. The time codes count from the document's start time code, as the files do. * craft-captions: TTML import keeps inherited and reset text styles Ported from the TTML style part of storytold/filmcraft#1723 (filmcraft#1721). Italic, bold and underline set on <body> or on a timed <span> cue root now reach the cue text; normal, noUnderline and inherit reset or restore the parent value; styling is read from the TTML styling namespaces (modern and 2006 drafts) with unqualified attributes as a fallback, and foreign namespaces are ignored. The inline text collector is iterative. Line breaks at the start or end of a cue are kept, as #3145's port does, so this differs from the source pull request there. * craft-captions: literal < and & stay text beside cue style tags Ported from the literal-text part of storytold/filmcraft#1723. The TTML and EBU STL readers keep a literal < as < (and an & that would read as an entity as &), as a WebVTT import already keeps its entities, so text like "<b>" in a TTML file never becomes a bold tag. The TTML and STL writers decode those entities, so a cue read from WebVTT with "1 < 2" is no longer written as "1 &lt; 2". entity_at and decode_entities are shared with plain_text. --------- Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Co-authored-by: SeeRay11 <searay112023@gmail.com> Co-authored-by: jnohclee-rgb <336857373+jnohclee-rgb@users.noreply.github.com> Co-authored-by: JonRC <jonarc06@gmail.com> Co-authored-by: sanyamsingh001 <314051937+sanyamsingh001@users.noreply.github.com> Co-authored-by: nuclearspike <liquidmonkey@gmail.com>
228 lines
12 KiB
Rust
228 lines
12 KiB
Rust
//! TTML body styles reach caption paragraphs; timed spans, whitespace, and nested cue content retain their styles.
|
|
|
|
use craft_captions::{Format, parse};
|
|
use craft_captions::{TICKS_PER_SECOND, Tick};
|
|
|
|
fn caption(xml: &str, format: Format) -> String {
|
|
let doc = parse(xml.as_bytes(), format).unwrap();
|
|
assert_eq!(doc.cues.len(), 1);
|
|
assert_eq!((doc.cues[0].start, doc.cues[0].end), (Tick(TICKS_PER_SECOND), Tick(TICKS_PER_SECOND * 2)));
|
|
assert!(doc.warnings.is_empty(), "{:?}", doc.warnings);
|
|
doc.cues[0].text.clone()
|
|
}
|
|
|
|
#[test]
|
|
fn body_inline_styles_are_inherited_by_paragraphs() {
|
|
for format in [Format::Ttml, Format::Dfxp] {
|
|
for (attribute, expected) in [
|
|
(r#"tts:fontStyle="italic""#, "<i>Hello</i>"),
|
|
(r#"tts:fontWeight="bold""#, "<b>Hello</b>"),
|
|
(r#"tts:textDecoration="underline""#, "<u>Hello</u>"),
|
|
] {
|
|
let xml = format!(
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"><body {attribute}><div><p begin="1s" end="2s">Hello</p></div></body></tt>"#
|
|
);
|
|
assert_eq!(caption(&xml, format), expected, "{format:?}, {attribute}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn body_referenced_styles_are_inherited_by_paragraphs() {
|
|
let xml = r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling">
|
|
<head><styling><style xml:id="emphasis" tts:fontStyle="italic" tts:fontWeight="bold"/></styling></head>
|
|
<body style="emphasis"><div><p begin="1s" end="2s">Hello</p></div></body></tt>"#;
|
|
for format in [Format::Ttml, Format::Dfxp] {
|
|
assert_eq!(caption(xml, format), "<i><b>Hello</b></i>");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn child_spans_can_reset_inherited_and_paragraph_root_styles() {
|
|
for format in [Format::Ttml, Format::Dfxp] {
|
|
for (attribute, reset, tag) in [
|
|
(r#"tts:fontStyle="italic""#, r#"tts:fontStyle="normal""#, "i"),
|
|
(r#"tts:fontWeight="bold""#, r#"tts:fontWeight="normal""#, "b"),
|
|
(r#"tts:textDecoration="underline""#, r#"tts:textDecoration="noUnderline""#, "u"),
|
|
] {
|
|
for (body, paragraph) in [(attribute, ""), ("", attribute)] {
|
|
let xml = format!(
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"><body {body}><div><p {paragraph} begin="1s" end="2s">Before<span {reset}>plain</span>After</p></div></body></tt>"#
|
|
);
|
|
assert_eq!(
|
|
caption(&xml, format),
|
|
format!("<{tag}>Before</{tag}>plain<{tag}>After</{tag}>"),
|
|
"{format:?}, {body}, {paragraph}, {reset}"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn draft_inherit_keeps_parent_styles() {
|
|
for (parent, inherited, expected) in [
|
|
(r#"tts:fontStyle="italic""#, r#"tts:fontStyle="inherit""#, "<i>Hello</i>"),
|
|
(r#"tts:fontWeight="bold""#, r#"tts:fontWeight="inherit""#, "<b>Hello</b>"),
|
|
(r#"tts:textDecoration="underline""#, r#"tts:textDecoration="inherit""#, "<u>Hello</u>"),
|
|
] {
|
|
for cue in
|
|
[format!(r#"<p {inherited} begin="1s" end="2s">Hello</p>"#), format!(r#"<p><span {inherited} begin="1s" end="2s">Hello</span></p>"#)]
|
|
{
|
|
let xml = format!(
|
|
r#"<tt xmlns="http://www.w3.org/2006/04/ttaf1" xmlns:tts="http://www.w3.org/2006/04/ttaf1#style"><body {parent}><div>{cue}</div></body></tt>"#
|
|
);
|
|
assert_eq!(caption(&xml, Format::Dfxp), expected, "{parent}, {cue}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn referenced_draft_inherit_restores_the_parent_after_earlier_styles() {
|
|
for (property, on, off, tag) in [
|
|
("fontStyle", r#"tts:fontStyle="italic""#, r#"tts:fontStyle="normal""#, "i"),
|
|
("fontWeight", r#"tts:fontWeight="bold""#, r#"tts:fontWeight="normal""#, "b"),
|
|
("textDecoration", r#"tts:textDecoration="underline""#, r#"tts:textDecoration="noUnderline""#, "u"),
|
|
] {
|
|
let inherit = format!(r#"tts:{property}="inherit""#);
|
|
for (parent, first, expected) in [(on, off, format!("<{tag}>Hello</{tag}>")), ("", on, "Hello".to_string())] {
|
|
for cue in [
|
|
r#"<p style="first restore" begin="1s" end="2s">Hello</p>"#.to_string(),
|
|
format!(r#"<p style="first" {inherit} begin="1s" end="2s">Hello</p>"#),
|
|
] {
|
|
let xml = format!(
|
|
r#"<tt xmlns="http://www.w3.org/2006/04/ttaf1" xmlns:tts="http://www.w3.org/2006/04/ttaf1#style"><head><styling><style xml:id="first" {first}/><style xml:id="restore" {inherit}/></styling></head><body {parent}><div>{cue}</div></body></tt>"#
|
|
);
|
|
assert_eq!(caption(&xml, Format::Dfxp), expected, "{parent}, {first}, {cue}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_font_values_do_not_reset_inherited_styles() {
|
|
for (parent, unsupported, expected) in [
|
|
(r#"tts:fontStyle="italic""#, r#"tts:fontStyle="garbage""#, "<i>Hello</i>"),
|
|
(r#"tts:fontWeight="bold""#, r#"tts:fontWeight="bolder""#, "<b>Hello</b>"),
|
|
] {
|
|
let xml = format!(
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"><body {parent}><div><p {unsupported} begin="1s" end="2s">Hello</p></div></body></tt>"#
|
|
);
|
|
assert_eq!(caption(&xml, Format::Ttml), expected);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn referenced_normal_styles_override_inherited_values() {
|
|
for format in [Format::Ttml, Format::Dfxp] {
|
|
for (attribute, reset) in [
|
|
(r#"tts:fontStyle="italic""#, r#"tts:fontStyle="normal""#),
|
|
(r#"tts:fontWeight="bold""#, r#"tts:fontWeight="normal""#),
|
|
(r#"tts:textDecoration="underline""#, r#"tts:textDecoration="noUnderline""#),
|
|
] {
|
|
let xml = format!(
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"><head><styling><style xml:id="plain" {reset}/></styling></head><body {attribute}><div><p style="plain" begin="1s" end="2s">Hello</p></div></body></tt>"#
|
|
);
|
|
assert_eq!(caption(&xml, format), "Hello", "{format:?}, {attribute}, {reset}");
|
|
}
|
|
let xml = r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"><head><styling><style xml:id="plain" tts:fontStyle="normal"/></styling></head><body tts:fontStyle="italic" tts:fontWeight="bold"><div><p style="plain" begin="1s" end="2s">Hello</p></div></body></tt>"#;
|
|
assert_eq!(caption(xml, format), "<b>Hello</b>", "{format:?}, unspecified bold remains inherited");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pending_spaces_keep_their_source_text_style() {
|
|
for format in [Format::Ttml, Format::Dfxp] {
|
|
for (body_style, text, expected) in [
|
|
("", r#"Before<span tts:textDecoration="underline"> world</span>After"#, "Before<u> world</u>After"),
|
|
(
|
|
r#"tts:textDecoration="underline""#,
|
|
r#"Before<span tts:textDecoration="noUnderline"> world</span>After"#,
|
|
"<u>Before</u> world<u>After</u>",
|
|
),
|
|
(
|
|
r#"tts:textDecoration="underline""#,
|
|
r#"Before <span tts:textDecoration="noUnderline">world</span>After"#,
|
|
"<u>Before </u>world<u>After</u>",
|
|
),
|
|
("", r#"Before<span tts:textDecoration="underline">world </span>After"#, "Before<u>world </u>After"),
|
|
("", r#"Before<span tts:textDecoration="underline"> </span><span tts:textDecoration="noUnderline"> </span>After"#, "Before<u> </u>After"),
|
|
("", r#"Before<span tts:textDecoration="noUnderline"> </span><span tts:textDecoration="underline"> </span>After"#, "Before After"),
|
|
("", r#"<span tts:textDecoration="underline"> </span><span tts:textDecoration="noUnderline"> </span>After"#, "After"),
|
|
(r#"tts:textDecoration="underline""#, r#"Before<span style="plain"> world</span>After"#, "<u>Before</u> world<u>After</u>"),
|
|
] {
|
|
for cue in [format!(r#"<p begin="1s" end="2s">{text}</p>"#), format!(r#"<p><span begin="1s" end="2s">{text}</span></p>"#)] {
|
|
let xml = format!(
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"><head><styling><style xml:id="plain" tts:textDecoration="noUnderline"/></styling></head><body {body_style}><div>{cue}</div></body></tt>"#
|
|
);
|
|
assert_eq!(caption(&xml, format), expected, "{format:?}, {body_style}, {cue}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn root_styles_keep_fractional_timing_and_speaker_metadata() {
|
|
for format in [Format::Ttml, Format::Dfxp] {
|
|
for cue in
|
|
[r#"<p begin="1.5t" end="2.5t" ttm:agent="voice">Hello</p>"#, r#"<p><span begin="1.5t" end="2.5t" ttm:agent="voice">Hello</span></p>"#]
|
|
{
|
|
let xml = format!(
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" ttp:tickRate="10"><head><metadata><ttm:agent xml:id="voice" type="person"><ttm:name type="full">Voice_One 日本</ttm:name></ttm:agent></metadata></head><body begin="5s" tts:fontStyle="italic"><div begin="2s">{cue}</div></body></tt>"#
|
|
);
|
|
let doc = parse(xml.as_bytes(), format).unwrap();
|
|
assert!(doc.warnings.is_empty(), "{format:?}, {cue}: {:?}", doc.warnings);
|
|
assert_eq!(doc.cues.len(), 1, "{format:?}, {cue}");
|
|
let parsed = &doc.cues[0];
|
|
assert_eq!(
|
|
(parsed.start, parsed.end),
|
|
(Tick(TICKS_PER_SECOND * 7 + TICKS_PER_SECOND * 3 / 20), Tick(TICKS_PER_SECOND * 7 + TICKS_PER_SECOND / 4))
|
|
);
|
|
assert_eq!(parsed.text, "<i>Hello</i>", "{format:?}, {cue}");
|
|
assert_eq!(parsed.speaker.as_deref(), Some("Voice_One 日本"), "{format:?}, {cue}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn descendant_overrides_and_unstyled_body_keep_their_behavior() {
|
|
for xml in [
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"><body tts:fontStyle="italic"><div><p tts:fontStyle="normal" begin="1s" end="2s">Hello</p></div></body></tt>"#,
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml"><body><div><p begin="1s" end="2s">Hello</p></div></body></tt>"#,
|
|
] {
|
|
assert_eq!(caption(xml, Format::Ttml), "Hello");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn non_xml_spaces_remain_visible_inside_styled_cues() {
|
|
for space in ['\u{a0}', '\u{202f}', '\u{3000}'] {
|
|
let visible = format!("{space}A{space}B{space}");
|
|
let xml = format!(
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"><body tts:fontStyle="italic"><div><p begin="1s" end="2s">{visible}</p></div></body></tt>"#
|
|
);
|
|
assert_eq!(caption(&xml, Format::Ttml), format!("<i>{visible}</i>"));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn deeply_nested_inline_styles_respect_the_xml_depth_limit() {
|
|
for format in [Format::Ttml, Format::Dfxp] {
|
|
for depth in [123, 124, 1_000] {
|
|
let mut xml = String::from(
|
|
r#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling"><body><div><p begin="1s" end="2s">"#,
|
|
);
|
|
xml.push_str(&"<span>".repeat(depth));
|
|
xml.push_str("<span tts:fontStyle=\"italic\">Deep</span>");
|
|
xml.push_str(&"</span>".repeat(depth));
|
|
xml.push_str("</p></div></body></tt>");
|
|
// Four containers plus the styled span leave room for 123 enclosing spans.
|
|
if depth == 123 {
|
|
assert_eq!(caption(&xml, format), "<i>Deep</i>");
|
|
} else {
|
|
assert!(parse(xml.as_bytes(), format).is_err(), "{format:?}, depth {depth}");
|
|
}
|
|
}
|
|
}
|
|
}
|