pub(crate) mod krilla_svg;
pub(crate) mod resvg;
pub(crate) mod usvg;
const LAYER_MASK_FUNCTION: &str = "-ferri-pdf-mask-layers";
pub(crate) fn image_rendering(rendering: usvg::ImageRendering) -> crate::paint::ImageRendering {
use crate::paint::ImageRendering;
match rendering {
usvg::ImageRendering::OptimizeQuality
| usvg::ImageRendering::Smooth
| usvg::ImageRendering::HighQuality => ImageRendering::Smooth,
usvg::ImageRendering::OptimizeSpeed | usvg::ImageRendering::CrispEdges => {
ImageRendering::CrispEdges
}
usvg::ImageRendering::Pixelated => ImageRendering::Pixelated,
}
}
pub(crate) fn layer_mask_value(index: usize) -> String {
format!("{LAYER_MASK_FUNCTION}({index})")
}
pub(crate) const TEXT_STYLE_ATTRIBUTE: &str = "ferri-pdf-text-style";
pub(crate) const SWEEP_GRADIENT_ELEMENT: &str = "ferri-pdf-sweep-gradient";
pub(crate) const SWEEP_START_ANGLE_ATTRIBUTE: &str = "ferri-pdf-start-angle";
pub(crate) const SWEEP_END_ANGLE_ATTRIBUTE: &str = "ferri-pdf-end-angle";
pub(crate) const FIRST_LINE_ATTRIBUTE: &str = "ferri-pdf-first-line";
#[derive(Clone, Debug, Default)]
pub(crate) struct DocumentStyling {
pub(crate) layer_masks: std::sync::Arc<[std::sync::Arc<crate::graphics::LoadedMasks>]>,
pub(crate) text_styles: std::sync::Arc<[std::sync::Arc<crate::style::Style>]>,
}
pub(crate) fn layer_mask_index(value: &str) -> Option<usize> {
value
.strip_prefix(LAYER_MASK_FUNCTION)?
.strip_prefix('(')?
.strip_suffix(')')?
.parse()
.ok()
}
pub(crate) trait LayerMasks {
fn push(
&self,
mask: &usvg::LayerMask,
bounds: usvg::Rect,
surface: &mut krilla::surface::Surface<'_>,
) -> u16;
fn apply(
&self,
mask: &usvg::LayerMask,
transform: tiny_skia::Transform,
pixmap: &mut tiny_skia::Pixmap,
);
}
pub(crate) fn reference_tree(
xml: &str,
styling: DocumentStyling,
property: &str,
reference: &str,
bounds: Option<crate::graphics::Rect>,
documents: &dyn Fn(&str) -> Option<usvg::LinkedDocument>,
) -> Option<usvg::Tree> {
use std::fmt::Write;
let [x, y, width, height] = bounds.map_or([0.0; 4], |rect| {
[rect.x, rect.y, rect.width, rect.height]
.map(|value| value / crate::graphics::POINTS_PER_PIXEL)
});
let mut url = String::new();
cssparser::serialize_string(reference, &mut url).expect("writing a String cannot fail");
let mut attribute = String::new();
crate::document::svg::escape(&mut attribute, &format!("url({url})"), true);
let mut document = String::new();
let _ = write!(
document,
"<svg xmlns=\"http://www.w3.org/2000/svg\"><defs>{xml}</defs>\
<rect x=\"{x}\" y=\"{y}\" width=\"{width}\" height=\"{height}\" fill=\"white\" {property}=\"{attribute}\"/></svg>"
);
let options = usvg::Options {
styling,
..usvg::Options::default()
};
usvg::Tree::from_str_with_documents(&document, &options, documents).ok()
}
pub(crate) fn referenced_filter(
reference: &str,
bounds: Option<crate::graphics::Rect>,
definitions: &crate::document::svg::SvgDefinitions,
) -> Option<std::sync::Arc<usvg::filter::Filter>> {
let tree = definitions.reference_tree(FILTER_ELEMENT, FILTER_ELEMENT, reference, bounds)?;
first_filter(tree.root())
}
const FILTER_ELEMENT: &str = "filter";
pub(crate) fn first_filter(group: &usvg::Group) -> Option<std::sync::Arc<usvg::filter::Filter>> {
group.filters().first().cloned().or_else(|| {
group.children().iter().find_map(|node| match node {
usvg::Node::Group(group) => first_filter(group),
_ => None,
})
})
}