use waft_embedder::{
CaptionBehavior, CaptionSetting, CaptionStyle, CaptionTypeface, Color, Cue, CueTextEdge,
CueTextRun, GenericCaptionFont,
};
const DEFAULT_FOREGROUND: Color = Color::new(0xFFFF_FFFF);
const DEFAULT_BACKGROUND: Color = Color::new(0xFF00_0000);
const DEFAULT_WINDOW: Color = Color::new(0x0000_0000);
const DEFAULT_EDGE_COLOR: Color = Color::new(0xFF00_0000);
#[derive(Clone, Debug, PartialEq)]
pub(super) struct StyledRun<'a> {
pub(super) text: &'a str,
pub(super) bold: bool,
pub(super) italic: bool,
pub(super) underline: bool,
pub(super) color: Color,
pub(super) background: Color,
pub(super) font_size: f32,
pub(super) font_family: Option<String>,
pub(super) edge: CueTextEdge,
pub(super) edge_color: Color,
}
pub(super) fn styled_runs<'a>(runs: &'a [CueTextRun], style: &CaptionStyle) -> Vec<StyledRun<'a>> {
runs.iter().map(|run| styled_run(run, style)).collect()
}
pub(super) fn window_color(cue: &Cue, style: &CaptionStyle) -> Color {
merged_color(
cue.window_color,
style.window_color.as_ref(),
style.window_opacity.as_ref(),
DEFAULT_WINDOW,
)
}
pub(super) fn window_corner_radius(style: &CaptionStyle) -> f64 {
style
.window_corner_radius
.as_ref()
.map_or(0.0, |radius| radius.value)
}
fn styled_run<'a>(run: &'a CueTextRun, style: &CaptionStyle) -> StyledRun<'a> {
StyledRun {
text: &run.text,
bold: run.bold,
italic: run.italic,
underline: run.underline,
color: merged_color(
run.color,
style.foreground_color.as_ref(),
style.foreground_opacity.as_ref(),
DEFAULT_FOREGROUND,
),
background: merged_color(
run.background,
style.background_color.as_ref(),
style.background_opacity.as_ref(),
DEFAULT_BACKGROUND,
),
font_size: font_size(run, style),
font_family: said(run.font_family.clone(), family_of(style).as_ref()),
edge: merged(run.edge, style.edge.as_ref(), CueTextEdge::None),
edge_color: merged(
run.edge_color,
style.edge_color.as_ref(),
DEFAULT_EDGE_COLOR,
),
}
}
fn font_size(run: &CueTextRun, style: &CaptionStyle) -> f32 {
let scaled = style.text_scale.as_ref().map(|scale| CaptionSetting {
value: (f64::from(CueTextRun::DEFAULT_FONT_SIZE) * scale.value) as f32,
behavior: scale.behavior,
});
merged(
run.font_size,
scaled.as_ref(),
CueTextRun::DEFAULT_FONT_SIZE,
)
}
fn merged<T: Clone>(media: Option<T>, setting: Option<&CaptionSetting<T>>, default: T) -> T {
said(media, setting).unwrap_or(default)
}
fn said<T: Clone>(media: Option<T>, setting: Option<&CaptionSetting<T>>) -> Option<T> {
match setting {
Some(setting) if setting.behavior == CaptionBehavior::Always => Some(setting.value.clone()),
_ => media.or_else(|| setting.map(|setting| setting.value.clone())),
}
}
fn merged_color(
media: Option<Color>,
color: Option<&CaptionSetting<Color>>,
opacity: Option<&CaptionSetting<f64>>,
default: Color,
) -> Color {
let merged_rgb = merged(media, color, default);
let merged_opacity = merged(media.map(|color| color.a), opacity, default.a);
merged_rgb.with_values(Some(merged_opacity.clamp(0.0, 1.0)), None, None, None, None)
}
fn family_of(style: &CaptionStyle) -> Option<CaptionSetting<String>> {
let typeface = style.typeface.as_ref()?;
Some(CaptionSetting {
value: family_name(&typeface.value).to_owned(),
behavior: typeface.behavior,
})
}
fn family_name(typeface: &CaptionTypeface) -> &str {
match typeface {
CaptionTypeface::Family(family) => family,
CaptionTypeface::Generic(kind) => match kind {
GenericCaptionFont::MonospacedWithSerifs => "Courier New",
GenericCaptionFont::ProportionalWithSerifs => "Times New Roman",
GenericCaptionFont::MonospacedWithoutSerifs => "Consolas",
GenericCaptionFont::ProportionalWithoutSerifs | GenericCaptionFont::SmallCapitals => {
"Tahoma"
}
GenericCaptionFont::Casual => "Segoe Print",
GenericCaptionFont::Cursive => "Segoe Script",
},
}
}
#[cfg(test)]
mod tests {
use waft_embedder::{CueContent, GenericCaptionFont};
use super::*;
const RED: Color = Color::new(0xFFFF_0000);
const BLUE: Color = Color::new(0xFF00_00FF);
const YELLOW: Color = Color::new(0xFFFF_FF00);
fn run(text: &str) -> CueTextRun {
CueTextRun::new(text)
}
#[test]
fn where_neither_the_media_nor_the_user_says_media3s_default_style_draws() {
let plain = run("Hi");
let drawn = styled_run(&plain, &CaptionStyle::default());
assert_eq!(drawn.color, DEFAULT_FOREGROUND);
assert_eq!(drawn.background, DEFAULT_BACKGROUND);
assert_eq!(drawn.font_size, CueTextRun::DEFAULT_FONT_SIZE);
assert_eq!(drawn.font_family, None);
assert_eq!(drawn.edge, CueTextEdge::None);
let cue = Cue::new(CueContent::Text(vec![plain.clone()]));
assert_eq!(window_color(&cue, &CaptionStyle::default()), DEFAULT_WINDOW);
assert_eq!(window_corner_radius(&CaptionStyle::default()), 0.0);
}
#[test]
fn a_setting_that_applies_always_wins_and_one_for_where_the_media_says_nothing_fills_in() {
let plain = run("Hi");
let mut said = run("Hi");
said.color = Some(RED);
said.edge = Some(CueTextEdge::Raised);
let always = CaptionStyle {
foreground_color: Some(CaptionSetting::always(BLUE)),
edge: Some(CaptionSetting::always(CueTextEdge::Outline)),
..CaptionStyle::default()
};
let drawn = styled_run(&said, &always);
assert_eq!(drawn.color, BLUE, "the user's colour over the media's");
assert_eq!(drawn.edge, CueTextEdge::Outline);
let filling_in = CaptionStyle {
foreground_color: Some(CaptionSetting::where_media_says_nothing(BLUE)),
edge: Some(CaptionSetting::where_media_says_nothing(
CueTextEdge::Outline,
)),
..CaptionStyle::default()
};
let drawn = styled_run(&said, &filling_in);
assert_eq!(drawn.color, RED, "the media's colour where it says one");
assert_eq!(drawn.edge, CueTextEdge::Raised);
let drawn = styled_run(&plain, &filling_in);
assert_eq!(drawn.color, BLUE, "the user's where the media says none");
assert_eq!(drawn.edge, CueTextEdge::Outline);
assert_eq!(drawn.edge_color, DEFAULT_EDGE_COLOR);
}
#[test]
fn a_colours_opacity_is_merged_on_its_own() {
let mut said = run("Hi");
said.color = Some(RED.with_values(Some(0.5), None, None, None, None));
let style = CaptionStyle {
foreground_color: Some(CaptionSetting::always(BLUE)),
foreground_opacity: Some(CaptionSetting::where_media_says_nothing(0.8)),
background_color: Some(CaptionSetting::where_media_says_nothing(YELLOW)),
background_opacity: Some(CaptionSetting::always(0.25)),
..CaptionStyle::default()
};
let drawn = styled_run(&said, &style);
assert_eq!(
drawn.color,
BLUE.with_values(Some(0.5), None, None, None, None),
"the user's colour at the media's opacity"
);
assert_eq!(
drawn.background,
YELLOW.with_values(Some(0.25), None, None, None, None),
"the user's colour, which the media leaves unsaid, at the user's opacity"
);
}
#[test]
fn the_text_scale_scales_the_default_size_and_replaces_the_medias_only_where_it_applies_always()
{
let plain = run("Hi");
let mut said = run("Hi");
said.font_size = Some(0.1);
let scaled = |behavior| CaptionStyle {
text_scale: Some(CaptionSetting {
value: 2.0,
behavior,
}),
..CaptionStyle::default()
};
let twice_the_default = CueTextRun::DEFAULT_FONT_SIZE * 2.0;
assert_eq!(
styled_run(&said, &scaled(CaptionBehavior::WhereMediaSaysNothing)).font_size,
0.1
);
assert_eq!(
styled_run(&plain, &scaled(CaptionBehavior::WhereMediaSaysNothing)).font_size,
twice_the_default
);
assert_eq!(
styled_run(&said, &scaled(CaptionBehavior::Always)).font_size,
twice_the_default
);
}
#[test]
fn a_typeface_is_drawn_in_its_family_and_a_kind_of_font_in_the_family_chromium_names() {
let plain = run("Hi");
let typeface = |typeface| CaptionStyle {
typeface: Some(CaptionSetting::where_media_says_nothing(typeface)),
..CaptionStyle::default()
};
let family = styled_run(
&plain,
&typeface(CaptionTypeface::Family("Avenir".to_owned())),
);
assert_eq!(family.font_family.as_deref(), Some("Avenir"));
let kind = styled_run(
&plain,
&typeface(CaptionTypeface::Generic(
GenericCaptionFont::MonospacedWithSerifs,
)),
);
assert_eq!(kind.font_family.as_deref(), Some("Courier New"));
let mut said = run("Hi");
said.font_family = Some("Menlo".to_owned());
let media = styled_run(
&said,
&typeface(CaptionTypeface::Family("Avenir".to_owned())),
);
assert_eq!(media.font_family.as_deref(), Some("Menlo"));
}
#[test]
fn the_window_is_the_medias_unless_the_users_applies_always() {
let mut cue = Cue::new(CueContent::Text(vec![run("Hi")]));
cue.window_color = Some(RED);
let style = |setting| CaptionStyle {
window_color: Some(setting),
window_opacity: Some(CaptionSetting::where_media_says_nothing(0.5)),
window_corner_radius: Some(CaptionSetting::where_media_says_nothing(4.0)),
..CaptionStyle::default()
};
assert_eq!(
window_color(&cue, &style(CaptionSetting::where_media_says_nothing(BLUE))),
RED
);
assert_eq!(
window_color(&cue, &style(CaptionSetting::always(BLUE))),
BLUE,
"the user's colour at the media's opacity"
);
assert_eq!(
window_corner_radius(&style(CaptionSetting::always(BLUE))),
4.0
);
}
}