ferri-pdf 0.1.8

HTML and CSS to PDF renderer for print, in pure Rust
Documentation
use crate::fonts::{
    FontAdjustments, FontBook, FontData, FontError, FontFaceStyle, FontId, FontInstance, FontRange,
    FontSelection, FontSlant, FontSource, FontSources, FontStyle, FontVariation, FontWeight,
    LoadedFont, matching, sfnt, validate,
};
use crate::resources::ResourceResolver;

const WEIGHT_AXIS: ttf_parser::Tag = ttf_parser::Tag::from_bytes(b"wght");
const WIDTH_AXIS: ttf_parser::Tag = ttf_parser::Tag::from_bytes(b"wdth");
const SLANT_AXIS: ttf_parser::Tag = ttf_parser::Tag::from_bytes(b"slnt");
const ITALIC_AXIS: ttf_parser::Tag = ttf_parser::Tag::from_bytes(b"ital");
pub(crate) const OPTICAL_SIZE_AXIS: ttf_parser::Tag = ttf_parser::Tag::from_bytes(b"opsz");

impl FontBook {
    /// The face at `size` with the axis values of CSS Fonts 4 §7.2: matching, then the
    /// `@font-face` descriptor, optical sizing and `font-variation-settings`.
    pub(crate) fn instance(
        &self,
        id: FontId,
        requested: FontSelection,
        adjustments: &FontAdjustments,
        size: f32,
        resolver: &dyn ResourceResolver,
    ) -> Result<Option<FontInstance<'_>>, FontError> {
        self.instance_data(
            id,
            requested,
            adjustments,
            size,
            self.resolve(id, resolver)?,
        )
    }

    /// Measures a face whose source list has already been attempted in this job, as font
    /// matching and its `@font-face` rule select it.
    pub(crate) fn cached_instance(
        &self,
        id: FontId,
        requested: FontSelection,
    ) -> Result<Option<FontInstance<'_>>, FontError> {
        self.instance_data(
            id,
            requested,
            &FontAdjustments::none(),
            0.0,
            self.resolve_cached(id)?,
        )
    }

    fn instance_data<'a>(
        &'a self,
        id: FontId,
        requested: FontSelection,
        adjustments: &FontAdjustments,
        size: f32,
        data: Option<&'a [u8]>,
    ) -> Result<Option<FontInstance<'a>>, FontError> {
        let Some(data) = data else {
            return Ok(None);
        };
        let face_entry = self.face(id).ok_or(FontError::UnknownFont(id))?;
        let descriptor = &face_entry.descriptor;
        let mut face = ttf_parser::Face::parse(data, 0).map_err(FontError::InvalidFont)?;
        let italic_axis = face
            .variation_axes()
            .into_iter()
            .any(|axis| axis.tag == ITALIC_AXIS);
        let (use_italic, angle) = match descriptor.style {
            FontFaceStyle::Auto => match requested.style {
                FontStyle::Italic if italic_axis => (true, FontSlant::NORMAL),
                FontStyle::Italic => (false, FontSlant::ITALIC_MATCH),
                FontStyle::Normal => (false, FontSlant::NORMAL),
                FontStyle::Oblique => (false, requested.slant),
                // An `ital` axis gives no slant direction; `slnt` gives one.
                style @ (FontStyle::Left | FontStyle::Right) => (
                    false,
                    FontSlant::ITALIC_MATCH.directed(style.italic_direction()),
                ),
            },
            FontFaceStyle::Italic | FontFaceStyle::Left | FontFaceStyle::Right => {
                (true, FontSlant::NORMAL)
            }
            other => (false, matching::style_match(other, requested).1),
        };
        let mut weight = match descriptor.weight {
            FontRange::Auto => FontWeight::NORMAL.value(),
            range @ FontRange::Values { .. } => range.clamp(requested.weight).value(),
        };
        let mut style = match descriptor.style {
            FontFaceStyle::Italic => FontStyle::Italic,
            FontFaceStyle::Left => FontStyle::Left,
            FontFaceStyle::Right => FontStyle::Right,
            FontFaceStyle::Oblique(_) if angle != FontSlant::NORMAL => FontStyle::Oblique,
            _ => FontStyle::Normal,
        };
        let mut variations = Vec::new();
        for axis in face.variation_axes() {
            let value = match axis.tag {
                WEIGHT_AXIS => descriptor.weight.clamp(requested.weight).value(),
                WIDTH_AXIS => descriptor.width.clamp(requested.width).value(),
                ITALIC_AXIS if use_italic => 1.0,
                SLANT_AXIS if !use_italic => -angle.value(),
                _ => continue,
            }
            .clamp(axis.min_value, axis.max_value);
            if axis.tag == WEIGHT_AXIS {
                weight = value;
            }
            if descriptor.style == FontFaceStyle::Auto {
                if axis.tag == ITALIC_AXIS && value != 0.0 {
                    style = FontStyle::Italic;
                }
                if axis.tag == SLANT_AXIS && value != 0.0 {
                    style = FontStyle::Oblique;
                }
            }
            variations.push(FontVariation {
                tag: axis.tag.to_bytes(),
                value,
            });
        }
        let (default_weight, default_style) = (weight, style);
        let optical = adjustments.optical_sizing.then_some(FontVariation {
            tag: OPTICAL_SIZE_AXIS.to_bytes(),
            value: size,
        });
        // Later settings replace earlier ones; axes the font lacks are ignored (CSS Fonts 4 §8.2).
        for setting in face_entry
            .settings
            .variations
            .iter()
            .chain(&optical)
            .chain(adjustments.variations.iter())
        {
            let Some(axis) = face
                .variation_axes()
                .into_iter()
                .find(|axis| axis.tag.to_bytes() == setting.tag)
            else {
                continue;
            };
            let value = setting.value.clamp(axis.min_value, axis.max_value);
            match variations.iter_mut().find(|v| v.tag == setting.tag) {
                Some(existing) => existing.value = value,
                None => variations.push(FontVariation {
                    tag: setting.tag,
                    value,
                }),
            }
        }
        // A face whose variation data cannot take a value keeps that axis's default; one that
        // cannot be embedded at its values keeps every axis's default.
        variations.retain(|variation| {
            face.set_variation(ttf_parser::Tag::from_bytes(&variation.tag), variation.value)
                .is_some()
        });
        if !variations.is_empty() && !self.embeds_instance(id, data, &face, &variations) {
            face = ttf_parser::Face::parse(data, 0).map_err(FontError::InvalidFont)?;
            variations.clear();
            (weight, style) = (default_weight, default_style);
        }
        Ok(Some(FontInstance {
            face,
            variations: variations.into(),
            metric_overrides: face_entry.settings.metric_overrides,
            weight,
            style,
        }))
    }

    /// Whether the face has the `opsz` axis that `font-optical-sizing` sets.
    pub(crate) fn has_optical_axis(
        &self,
        id: FontId,
        resolver: &dyn ResourceResolver,
    ) -> Result<bool, FontError> {
        let Some(data) = self.resolve(id, resolver)? else {
            return Ok(false);
        };
        Ok(ttf_parser::Face::parse(data, 0)
            .map_err(FontError::InvalidFont)?
            .variation_axes()
            .into_iter()
            .any(|axis| axis.tag == OPTICAL_SIZE_AXIS))
    }

    pub(crate) fn resolve(
        &self,
        id: FontId,
        resolver: &dyn ResourceResolver,
    ) -> Result<Option<&[u8]>, FontError> {
        let face = self.face(id).ok_or(FontError::UnknownFont(id))?;
        if let FontData::Lazy { sources, data } = &face.data
            && data.get().is_none()
        {
            let _ = data.set(self.load_sources(sources, resolver));
        }
        Ok(face.data())
    }

    /// Whether the author face `id`, whose `data` is `face`, embeds at `variations`; checked
    /// once per instance, recording why not. Bundled faces always embed.
    fn embeds_instance(
        &self,
        id: FontId,
        data: &[u8],
        face: &ttf_parser::Face<'_>,
        variations: &[FontVariation],
    ) -> bool {
        let Some(FontData::Lazy {
            sources,
            data: loaded,
        }) = self.face(id).map(|face| &face.data)
        else {
            return true;
        };
        let Some(url) = loaded
            .get()
            .and_then(Option::as_ref)
            .and_then(|loaded| loaded.url.clone())
        else {
            return true;
        };
        let key = (
            id,
            variations
                .iter()
                .map(|variation| (variation.tag, variation.value.to_bits()))
                .collect(),
        );
        if let Some(embeds) = self
            .instances
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .get(&key)
        {
            return *embeds;
        }
        let embedded = crate::fonts::embed_all_glyphs(data, face, variations);
        let embeds = embedded.is_ok();
        if let Err(error) = embedded {
            self.failures
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .push(crate::fonts::FontFailure {
                    base: sources.base.clone(),
                    reference: url,
                    error: FontError::UnembeddableInstance {
                        variations: variations.to_vec(),
                        error,
                    },
                });
        }
        self.instances
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .insert(key, embeds);
        embeds
    }

    /// Reads an already attempted face without acquiring additional resources.
    pub(crate) fn resolve_cached(&self, id: FontId) -> Result<Option<&[u8]>, FontError> {
        let face = self.face(id).ok_or(FontError::UnknownFont(id))?;
        if let FontData::Lazy { data, .. } = &face.data
            && data.get().is_none()
        {
            return Err(FontError::UnresolvedFont(id));
        }
        Ok(face.data())
    }

    /// The first of `sources` that loads as a valid font (CSS Fonts 4 §4.3); a URL that
    /// cannot be fetched, policy denies, or that holds no valid font is skipped, recorded.
    /// WOFF and WOFF2 files are decoded and collections give the face the URL's fragment
    /// names (§4.3.2) before validation.
    fn load_sources(
        &self,
        sources: &FontSources,
        resolver: &dyn ResourceResolver,
    ) -> Option<LoadedFont> {
        for source in &sources.sources {
            let reference = match source {
                FontSource::Local(name) => match self.local_data(name) {
                    Some(data) => {
                        return Some(LoadedFont {
                            data: data.to_vec(),
                            url: None,
                        });
                    }
                    None => continue,
                },
                FontSource::Url(reference) => reference,
            };
            let loaded = sfnt::postscript_fragment(reference)
                .map_err(FontError::from)
                .and_then(|fragment| {
                    let resource = resolver.resolve(reference, &sources.base)?;
                    let data = sfnt::decode(resource.bytes, fragment.as_deref())?;
                    validate(&data).map(|()| data)
                });
            match loaded {
                Ok(data) => {
                    return Some(LoadedFont {
                        data,
                        url: Some(reference.clone()),
                    });
                }
                Err(error) => self
                    .failures
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner)
                    .push(crate::fonts::FontFailure {
                        base: sources.base.clone(),
                        reference: reference.clone(),
                        error,
                    }),
            }
        }
        None
    }

    fn local_data(&self, name: &str) -> Option<&[u8]> {
        self.faces.iter().find_map(|face| {
            let FontData::Bundled(data) = &face.data else {
                return None;
            };
            let parsed = ttf_parser::Face::parse(data, 0).ok()?;
            let matched = parsed.names().into_iter().any(|record| {
                matches!(
                    record.name_id,
                    ttf_parser::name_id::FULL_NAME | ttf_parser::name_id::POST_SCRIPT_NAME
                ) && record
                    .to_string()
                    .is_some_and(|value| matching::names_match(&value, name))
            });
            matched.then_some(*data)
        })
    }
}