use crate::document::{DocumentError, formatting::ImageResources};
use crate::dom::Element;
use crate::fonts::FontNamespace;
use crate::images::Image;
use crate::style::{POINTS_PER_PIXEL, Style};
use std::sync::Arc;
const NORMAL_LINE_HEIGHT: f32 = 1.2;
const CONTROLS_ASPECT_RATIO: f32 = 8.0;
const AUDIO_CONTROLS: &str = concat!(
r#"<rect x="0.02" y="0.02" width="7.96" height="0.96" rx="0.48" fill="none" "#,
r#"stroke="{color}" stroke-opacity="{faint}" stroke-width="0.04"/>"#,
r#"<path d="M0.38 0.28L0.74 0.5L0.38 0.72Z" fill="{color}" fill-opacity="{alpha}"/>"#,
r#"<rect x="1.15" y="0.47" width="5.7" height="0.06" rx="0.03" fill="{color}" "#,
r#"fill-opacity="{faint}"/>"#,
r#"<circle cx="1.2" cy="0.5" r="0.11" fill="{color}" fill-opacity="{alpha}"/>"#,
r#"<path d="M7.1 0.42H7.22L7.38 0.28V0.72L7.22 0.58H7.1Z" fill="{color}" "#,
r#"fill-opacity="{alpha}"/>"#,
r#"<path d="M7.5 0.38Q7.6 0.5 7.5 0.62" fill="none" stroke="{color}" "#,
r#"stroke-opacity="{alpha}" stroke-width="0.05"/>"#,
);
const FAINT_OPACITY: f32 = 0.35;
pub(super) fn poster(
element: Element<'_>,
style: &Style,
resources: &mut ImageResources<'_>,
) -> Arc<Image> {
element
.attr("poster")
.filter(|reference| !reference.is_empty())
.and_then(|reference| {
resources.cache.load_optional(
reference,
resources.base,
resources.resolver,
style.element_font_namespace(),
)
})
.map_or_else(
|| Arc::new(Image::Transparent),
|image| crate::images::content_image(image, style, None),
)
}
pub(super) fn audio_controls(
style: &Style,
resources: &mut ImageResources<'_>,
) -> Result<Arc<Image>, DocumentError> {
let height = style
.line_height
.resolve(style.size)
.unwrap_or(style.size * NORMAL_LINE_HEIGHT)
/ POINTS_PER_PIXEL;
let [red, green, blue] = style.color.rgb;
let controls = AUDIO_CONTROLS
.replace("{color}", &format!("rgb({red},{green},{blue})"))
.replace("{alpha}", &style.color.alpha.to_string())
.replace("{faint}", &(style.color.alpha * FAINT_OPACITY).to_string());
let svg = format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{}" height="{height}" viewBox="0 0 {CONTROLS_ASPECT_RATIO} 1">{controls}</svg>"#,
height * CONTROLS_ASPECT_RATIO,
);
Ok(resources.cache.inline_svg_in_namespace(
svg,
Default::default(),
resources.base,
resources.resolver,
FontNamespace::ROOT,
)?)
}