use std::borrow::Cow;
use std::collections::BTreeMap;
use crate::{
FontSizeFit, LineClamp, MAX_TEXT_CONTENT_BYTES, Paint, TextAlign, TextAnimator, TextBackground,
TextShadow, TextStroke, TextStyle, Vec2,
};
use alpha2_time::TimeRange;
use serde::{Deserialize, Serialize};
pub const MAX_CAPTION_STYLES: usize = 32;
pub const MAX_CAPTION_NAME_BYTES: usize = 64;
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct Captions {
#[serde(default)]
pub style: TextStyle,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub styles: BTreeMap<String, TextStylePatch>,
#[serde(default)]
#[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_CLIPS_PER_TRACK }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_CLIPS_PER_TRACK)))]
pub cues: Vec<CaptionCue>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from_words: Option<crate::CaptionsFromWords>,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct CaptionCue {
pub timeline_range: TimeRange,
pub text: crate::TextContent,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub speaker: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub style: Option<String>,
}
impl CaptionCue {
#[must_use]
pub fn new(timeline_range: TimeRange, text: impl Into<crate::TextContent>) -> Self {
Self {
timeline_range,
text: text.into(),
speaker: None,
style: None,
}
}
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct TextStylePatch {
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "double_option"
)]
#[cfg_attr(feature = "schema", schemars(with = "Option<String>"))]
pub font_family: Option<Option<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub font_size: Option<f32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub fill: Option<Paint>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub line_height: Option<f32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub letter_spacing: Option<f32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub font_weight: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub align: Option<TextAlign>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_width: Option<f32>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "double_option"
)]
#[cfg_attr(feature = "schema", schemars(with = "Option<f32>"))]
pub max_height: Option<Option<f32>>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "double_option"
)]
#[cfg_attr(feature = "schema", schemars(with = "Option<FontSizeFit>"))]
pub font_size_fit: Option<Option<FontSizeFit>>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "double_option"
)]
#[cfg_attr(feature = "schema", schemars(with = "Option<LineClamp>"))]
pub line_clamp: Option<Option<LineClamp>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub position: Option<Vec2>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub anchor: Option<Vec2>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(deserialize_with = "crate::bounded::option_seq::<_, _, { crate::MAX_TEXT_STROKES }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TEXT_STROKES)))]
pub strokes: Option<Vec<TextStroke>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(deserialize_with = "crate::bounded::option_seq::<_, _, { crate::MAX_TEXT_SHADOWS }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TEXT_SHADOWS)))]
pub shadows: Option<Vec<TextShadow>>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "double_option"
)]
#[cfg_attr(feature = "schema", schemars(with = "Option<TextBackground>"))]
pub background: Option<Option<TextBackground>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(
deserialize_with = "crate::bounded::option_seq::<_, _, { crate::MAX_TEXT_ANIMATORS }>"
)]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TEXT_ANIMATORS)))]
pub animators: Option<Vec<TextAnimator>>,
}
impl TextStylePatch {
#[must_use]
pub fn apply(&self, base: &TextStyle) -> TextStyle {
let mut style = base.clone();
if let Some(family) = &self.font_family {
style.font_family.clone_from(family);
}
if let Some(size) = self.font_size {
style.font_size = size;
}
if let Some(fill) = &self.fill {
style.fill = fill.clone();
}
if let Some(height) = self.line_height {
style.line_height = height;
}
if let Some(spacing) = self.letter_spacing {
style.letter_spacing = spacing;
}
if let Some(weight) = self.font_weight {
style.font_weight = weight;
}
if let Some(align) = self.align {
style.align = align;
}
if let Some(width) = self.max_width {
style.max_width = width;
}
if let Some(height) = self.max_height {
style.max_height = height;
}
if let Some(fit) = self.font_size_fit {
style.font_size_fit = fit;
}
if let Some(clamp) = &self.line_clamp {
style.line_clamp.clone_from(clamp);
}
if let Some(position) = self.position {
style.position = position;
}
if let Some(anchor) = self.anchor {
style.anchor = anchor;
}
if let Some(strokes) = &self.strokes {
style.strokes.clone_from(strokes);
}
if let Some(shadows) = &self.shadows {
style.shadows.clone_from(shadows);
}
if let Some(background) = self.background {
style.background = background;
}
if let Some(animators) = &self.animators {
style.animators.clone_from(animators);
}
style
}
}
pub const MAX_CAPTION_FONT_SIZE: f32 = 0.25;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum CaptionIssue {
Invalid,
UnknownStyle { cue: usize, name: String },
CueStyle { cue: usize },
}
impl Captions {
#[must_use]
pub fn cue_patch(&self, cue: &CaptionCue) -> Option<&TextStylePatch> {
match &cue.style {
Some(name) => self.styles.get(name),
None => cue
.speaker
.as_ref()
.and_then(|speaker| self.styles.get(speaker)),
}
}
#[must_use]
pub fn cue_style(&self, cue: &CaptionCue) -> Cow<'_, TextStyle> {
match self.cue_patch(cue) {
Some(patch) => Cow::Owned(patch.apply(&self.style)),
None => Cow::Borrowed(&self.style),
}
}
pub(crate) fn validate(&self) -> Result<(), CaptionIssue> {
let name = |name: &str| !name.is_empty() && name.len() <= MAX_CAPTION_NAME_BYTES;
let valid = (self.from_words.is_none() || self.cues.is_empty())
&& self.style.is_valid(MAX_CAPTION_FONT_SIZE)
&& self.styles.len() <= MAX_CAPTION_STYLES
&& self.styles.keys().all(|key| name(key))
&& self.cues.iter().all(|cue| {
!cue.timeline_range.is_empty()
&& cue.timeline_range.start().ticks() >= 0
&& cue.text.is_valid(MAX_TEXT_CONTENT_BYTES)
&& cue.speaker.as_deref().is_none_or(name)
&& run_times_lie_in(cue)
})
&& self
.cues
.windows(2)
.all(|pair| pair[0].timeline_range.end() <= pair[1].timeline_range.start());
if !valid {
return Err(CaptionIssue::Invalid);
}
for (index, cue) in self.cues.iter().enumerate() {
if let Some(style) = &cue.style
&& !self.styles.contains_key(style)
{
return Err(CaptionIssue::UnknownStyle {
cue: index,
name: style.clone(),
});
}
if self.cue_patch(cue).is_some() && !self.cue_style(cue).is_valid(MAX_CAPTION_FONT_SIZE)
{
return Err(CaptionIssue::CueStyle { cue: index });
}
}
Ok(())
}
}
fn run_times_lie_in(cue: &CaptionCue) -> bool {
let duration = cue
.timeline_range
.duration()
.map_or(0, alpha2_time::MediaTime::ticks);
cue.text.runs().iter().all(|run| {
run.time
.is_none_or(|time| time.start.ticks() >= 0 && time.end.ticks() <= duration)
})
}
#[allow(clippy::option_option, clippy::ref_option)]
mod double_option {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub(super) fn serialize<T: Serialize, S: Serializer>(
value: &Option<Option<T>>,
serializer: S,
) -> Result<S::Ok, S::Error> {
match value {
Some(inner) => inner.serialize(serializer),
None => serializer.serialize_none(),
}
}
pub(super) fn deserialize<'de, T: Deserialize<'de>, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<Option<T>>, D::Error> {
Option::<T>::deserialize(deserializer).map(Some)
}
}
fn word_separator(left: &str, right: &str) -> &'static str {
match (left.chars().next_back(), right.chars().next()) {
(Some(last), Some(first)) if unspaced(last) && unspaced(first) => "",
_ => " ",
}
}
fn unspaced(character: char) -> bool {
matches!(
u32::from(character),
0x0E00..=0x0EFF | 0x1000..=0x109F | 0x1780..=0x17FF | 0x19E0..=0x19FF | 0x2E80..=0x2FDF | 0x3000..=0x303F | 0x3040..=0x30FF | 0x3100..=0x312F | 0x31F0..=0x31FF | 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xA9E0..=0xA9FF | 0xAA60..=0xAA7F | 0xF900..=0xFAFF | 0xFF00..=0xFFEF | 0x20000..=0x3FFFF )
}
pub const WORD_PAUSE_TICKS: i64 = 72_000;
pub const MAX_DERIVED_CUES: usize = crate::MAX_CAPTION_FILE_CUES;
#[must_use]
pub fn group_words(words: &[crate::DataWord], max_chars: u32) -> Vec<CaptionCue> {
type Words = (
i64,
i64,
Option<String>,
Vec<(String, i64, i64)>,
usize,
usize,
);
let mut groups: Vec<Words> = Vec::new();
let mut current: Option<Words> = None;
for word in words {
let (start, end) = (word.start.ticks(), word.end.ticks());
let text = word.text.trim().to_owned();
if text.is_empty() {
continue;
}
let characters = text.chars().count();
if let Some((_, cue_end, speaker, held, held_length, held_bytes)) = &mut current {
let last = held.last().map_or("", |(word, ..)| word.as_str());
let separator = word_separator(last, &text).len();
let length = *held_length + separator + characters;
let bytes = *held_bytes + separator + text.len();
let sentence_end = last.ends_with([
'.', '?', '!', '\u{3002}', '\u{ff01}', '\u{ff0e}', '\u{ff1f}',
]);
if length <= max_chars as usize
&& bytes <= MAX_TEXT_CONTENT_BYTES
&& held.len() < crate::MAX_TEXT_RUNS
&& start - *cue_end <= WORD_PAUSE_TICKS
&& !sentence_end
&& *speaker == word.speaker
{
held.push((text, start, end));
*cue_end = (*cue_end).max(end);
*held_length = length;
*held_bytes = bytes;
continue;
}
groups.extend(current.take());
}
let bytes = text.len();
current = Some((
start,
end,
word.speaker.clone(),
vec![(text, start, end)],
characters,
bytes,
));
}
groups.extend(current);
let mut cues = groups
.into_iter()
.filter_map(|(start, end, speaker, held, _, _)| {
let separators = held
.windows(2)
.map(|pair| word_separator(&pair[0].0, &pair[1].0))
.chain([""])
.collect::<Vec<_>>();
let runs = held
.into_iter()
.zip(separators)
.map(|((mut text, from, to), separator)| {
text.push_str(separator);
crate::TextRun {
text,
class: Vec::new(),
time: Some(crate::RunTime {
start: alpha2_time::MediaTime(from - start),
end: alpha2_time::MediaTime(to - start),
}),
}
})
.collect();
let range =
TimeRange::new(alpha2_time::MediaTime(start), alpha2_time::MediaTime(end)).ok()?;
Some(CaptionCue {
speaker,
..CaptionCue::new(range, crate::TextContent::Runs(runs))
})
})
.collect::<Vec<_>>();
end_cues_where_the_next_starts(&mut cues);
cues
}
pub fn end_cues_where_the_next_starts(cues: &mut Vec<CaptionCue>) {
for index in 1..cues.len() {
let next = cues[index].timeline_range.start();
let previous = &mut cues[index - 1];
if previous.timeline_range.end() > next {
let start = previous.timeline_range.start().min(next);
previous.timeline_range =
TimeRange::new(start, next).unwrap_or(previous.timeline_range);
clip_runs(previous);
}
}
cues.retain(|cue| !cue.timeline_range.is_empty());
}
pub fn clip_runs(cue: &mut CaptionCue) {
let duration = cue
.timeline_range
.duration()
.map_or(0, alpha2_time::MediaTime::ticks);
if let crate::TextContent::Runs(runs) = &mut cue.text {
for run in runs {
run.time = run.time.and_then(|time| {
let end = time.end.ticks().min(duration);
(time.start.ticks() < end).then_some(crate::RunTime {
start: time.start,
end: alpha2_time::MediaTime(end),
})
});
}
}
}