ferri-pdf 0.1.4

HTML and CSS to PDF renderer for print, in pure Rust
Documentation
//! Inlined SVG libraries, each kept beside its upstream licences and provenance.
pub(crate) mod krilla_svg;
pub(crate) mod resvg;
pub(crate) mod usvg;

/// The `mask` value naming, by index, the CSS mask layers that the document attaches to SVG
/// content it writes; no style sheet can produce it.
const LAYER_MASK_FUNCTION: &str = "-ferri-pdf-mask-layers";

/// SVG 2 §13.10 `image-rendering` as CSS Images 3 §5.2 uses it; the parser resolves `auto`
/// as `optimizeQuality`.
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,
    }
}

/// The `mask` value naming the `index`th CSS mask layers of an SVG document.
pub(crate) fn layer_mask_value(index: usize) -> String {
    format!("{LAYER_MASK_FUNCTION}({index})")
}

/// The attribute naming, by index, the computed style the document gives a text content element
/// it writes.
pub(crate) const TEXT_STYLE_ATTRIBUTE: &str = "ferri-pdf-text-style";

/// The paint server element drawing a sweep gradient (OpenType COLR `PaintSweepGradient`), which
/// SVG lacks: around its centre `cx`, `cy`, its colour line runs from the angle
/// [`SWEEP_START_ANGLE_ATTRIBUTE`] to [`SWEEP_END_ANGLE_ATTRIBUTE`], in degrees from the
/// positive x axis towards the positive y axis.
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";

/// The attribute marking the `tspan` that stands for a `text` element's `::first-line`
/// (CSS Pseudo-Elements 4 §2.1), around the contents of its first formatted line. The last
/// such child of a written `text` element holds those contents as the first line styles them.
pub(crate) const FIRST_LINE_ATTRIBUTE: &str = "ferri-pdf-first-line";

/// What the document attaches to the SVG content it styles, by the indices its written XML
/// names: the CSS mask layers that [`layer_mask_value`] values name, and the computed styles
/// of text content elements that [`TEXT_STYLE_ATTRIBUTE`] values name.
#[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>]>,
}

/// The index a `mask` value written by [`layer_mask_value`] names.
pub(crate) fn layer_mask_index(value: &str) -> Option<usize> {
    value
        .strip_prefix(LAYER_MASK_FUNCTION)?
        .strip_prefix('(')?
        .strip_suffix(')')?
        .parse()
        .ok()
}

/// Draws the CSS mask layers and mask border of SVG content (CSS Masking 1 §7, §8) as the
/// document draws those of its boxes, for both SVG renderers.
pub(crate) trait LayerMasks {
    /// Masks what `surface` draws next in the masked group's user space, where its content
    /// lies within `bounds`; returns the number of pushes to pop.
    fn push(
        &self,
        mask: &usvg::LayerMask,
        bounds: usvg::Rect,
        surface: &mut krilla::surface::Surface<'_>,
    ) -> u16;
    /// Multiplies the alpha of `pixmap`, onto which `transform` places the masked group's user
    /// space, by the mask's values.
    fn apply(
        &self,
        mask: &usvg::LayerMask,
        transform: tiny_skia::Transform,
        pixmap: &mut tiny_skia::Pixmap,
    );
}

/// A tree in which a white rectangle spanning `bounds` (points), or no area without them,
/// references SVG resource `reference` of document `xml` through presentation attribute
/// `property`, so the SVG parser resolves the resource's units against that box. Tree units
/// are CSS pixels. `styling` is what the document attaches to `xml`'s content, and
/// `documents` supplies the other documents that references name.
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()
}

/// The `filter` element `reference` names, resolved for an element with border box
/// `bounds`, through the SVG parser's filter conversion (§7, §8).
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())
}

/// The `filter` element and the property naming it.
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,
        })
    })
}