ferri-pdf 0.2.0

HTML and CSS to PDF renderer for print, in pure Rust
Documentation
// Copyright 2018 the Resvg Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::sync::Arc;

use crate::svg::usvg::FontResolver;
use crate::svg::usvg::{ImageHrefResolver, ImageRendering, ShapeRendering, Size, TextRendering};

/// Processing options.
#[derive(Debug)]
pub struct Options<'a> {
    /// Target DPI.
    ///
    /// Impacts units conversion.
    ///
    /// Default: 96.0
    pub dpi: f32,

    /// A default font family.
    ///
    /// Will be used when no `font-family` attribute is set in the SVG.
    ///
    /// Default: Times New Roman
    pub font_family: String,

    /// A default font size.
    ///
    /// Will be used when no `font-size` attribute is set in the SVG.
    ///
    /// Default: 12
    pub font_size: f32,

    /// A list of languages.
    ///
    /// Will be used to resolve a `systemLanguage` conditional attribute.
    ///
    /// Format: en, en-US.
    ///
    /// Default: `[en]`
    pub languages: Vec<String>,

    /// Specifies the default shape rendering method.
    ///
    /// Will be used when an SVG element's `shape-rendering` property is set to `auto`.
    ///
    /// Default: GeometricPrecision
    pub shape_rendering: ShapeRendering,

    /// Specifies the default text rendering method.
    ///
    /// Will be used when an SVG element's `text-rendering` property is set to `auto`.
    ///
    /// Default: OptimizeLegibility
    pub text_rendering: TextRendering,

    /// Specifies the default image rendering method.
    ///
    /// Will be used when an SVG element's `image-rendering` property is set to `auto`.
    ///
    /// Default: OptimizeQuality
    pub image_rendering: ImageRendering,

    /// Default viewport size to assume if there is no `viewBox` attribute and
    /// the `width` or `height` attributes are relative.
    ///
    /// Default: `(100, 100)`
    pub default_size: Size,

    /// Specifies the way `xlink:href` in `<image>` elements should be handled.
    ///
    /// Default: see type's documentation for details
    pub image_href_resolver: ImageHrefResolver<'a>,

    /// Specifies how fonts should be resolved and loaded.
    pub font_resolver: FontResolver<'a>,

    /// A database of fonts usable by text.
    ///
    /// This is a base database. If a custom `font_resolver` is specified,
    /// additional fonts can be loaded during parsing. Those will be added to a
    /// copy of this database. The full database containing all fonts referenced
    /// in a `Tree` becomes available as [`Tree::fontdb`](crate::svg::usvg::Tree::fontdb)
    /// after parsing. If no fonts were loaded dynamically, that database will
    /// be the same as this one.
    pub fontdb: Arc<fontdb::Database>,
    /// A CSS stylesheet that should be injected into the SVG. Can be used to overwrite
    /// certain attributes.
    pub style_sheet: Option<String>,
    /// What the document attaches to the SVG content it styles.
    pub(crate) styling: crate::svg::DocumentStyling,
    /// How deep its conversion nests in those of the documents drawing it as an image.
    pub(crate) nesting: super::converter::Nesting,
}

impl Default for Options<'_> {
    fn default() -> Options<'static> {
        Options {
            dpi: 96.0,
            // Default font is user-agent dependent so we can use whichever we like.
            font_family: "Times New Roman".to_owned(),
            font_size: 12.0,
            languages: vec!["en".to_string()],
            shape_rendering: ShapeRendering::default(),
            text_rendering: TextRendering::default(),
            image_rendering: ImageRendering::default(),
            default_size: Size::from_wh(100.0, 100.0).unwrap(),
            image_href_resolver: ImageHrefResolver::default(),
            font_resolver: FontResolver::default(),
            fontdb: Arc::new(fontdb::Database::new()),
            style_sheet: None,
            styling: crate::svg::DocumentStyling::default(),
            nesting: super::converter::Nesting::default(),
        }
    }
}

impl Options<'_> {
    /// Mutably acquires the database.
    ///
    /// This clones the database if it is currently shared.
    pub fn fontdb_mut(&mut self) -> &mut fontdb::Database {
        Arc::make_mut(&mut self.fontdb)
    }
}