ferri-pdf 0.1.12

HTML and CSS to PDF renderer for print, in pure Rust
Documentation
use crate::fonts::{
    FontData, FontFace, FontFaceStyle, FontId, FontSelection, FontSlant, FontStyle, FontWeight,
    FontWidth,
};
use std::cmp::Ordering;

pub(super) fn names_match(left: &str, right: &str) -> bool {
    if left.is_ascii() && right.is_ascii() {
        left.eq_ignore_ascii_case(right)
    } else {
        let mapper = icu_casemap::CaseMapper::new();
        mapper.fold_string(left) == mapper.fold_string(right)
    }
}

/// `name` case-folded, so that two names match exactly when their folded forms are equal.
#[cfg(file_system)]
pub(super) fn folded(name: &str) -> String {
    if name.is_ascii() {
        name.to_ascii_lowercase()
    } else {
        icu_casemap::CaseMapper::new()
            .fold_string(name)
            .into_owned()
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct Rank(u8, f32);
impl Eq for Rank {}
impl Ord for Rank {
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.cmp(&other.0).then(self.1.total_cmp(&other.1))
    }
}
impl PartialOrd for Rank {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// The best matches for `requested` among `family`, the ascending IDs of `faces` named by
/// one family name. Author faces hide bundled and installed ones of the name, and bundled
/// faces hide installed ones (CSS Fonts 4 ยง4.2).
pub(super) fn candidates(
    faces: &[FontFace],
    family: &[FontId],
    requested: FontSelection,
    installed_only: bool,
) -> Vec<FontId> {
    let family = || {
        family.iter().filter_map(|&id| {
            let face = faces.get(id.0)?;
            (!installed_only || !matches!(face.data, FontData::Lazy { .. })).then_some((id, face))
        })
    };
    let origin = family().map(|(_, face)| face.origin()).min();
    let eligible = |face: &FontFace| Some(face.origin()) == origin;
    let rank = |face: &FontFace| {
        let descriptor = &face.descriptor;
        let (min, max) = descriptor.width.bounds(FontWidth::NORMAL);
        let point = requested.width.value().clamp(min.value(), max.value());
        let target = requested.width.value();
        let preferred = if target <= FontWidth::NORMAL.value() {
            point <= target
        } else {
            point >= target
        };
        let width = Rank(u8::from(!preferred), (point - target).abs());
        let (min, max) = descriptor.weight.bounds(FontWeight::NORMAL);
        let point = requested.weight.value().clamp(min.value(), max.value());
        (
            width,
            style_match(descriptor.style, requested).0,
            weight_rank(point, requested.weight.value()),
        )
    };
    let best = family()
        .filter(|(_, face)| eligible(face))
        .map(|(_, face)| rank(face))
        .min();
    let mut found: Vec<FontId> = family()
        .filter(|(_, face)| eligible(face) && Some(rank(face)) == best)
        .map(|(id, _)| id)
        .collect();
    found.reverse();
    found
}

pub(super) fn style_match(face: FontFaceStyle, requested: FontSelection) -> (Rank, FontSlant) {
    let italic = face.is_italic();
    let (min, max) = match face {
        FontFaceStyle::Oblique(range) => range.bounds(FontSlant::DEFAULT),
        _ => (FontSlant::NORMAL, FontSlant::NORMAL),
    };
    let (mut min, mut max) = (min.value(), max.value());
    let normal = requested.style == FontStyle::Normal
        || (requested.style == FontStyle::Oblique && requested.slant == FontSlant::NORMAL);
    let (rank, angle) = if let Some(sign) = requested.style.italic_direction() {
        // CSS Fonts 4 ยง2.4: `left` and `right` match an italic of their slant direction, else
        // an oblique slanted the same way, searched as `italic` searches obliques.
        let face_sign = match face {
            FontFaceStyle::Left => Some(1.0),
            FontFaceStyle::Right => Some(-1.0),
            _ => None,
        };
        if sign < 0.0 {
            (min, max) = (-max, -min);
        }
        let target = FontSlant::ITALIC_MATCH.value();
        let (rank, point) = if face_sign == Some(sign) {
            (Rank(0, 0.0), 0.0)
        } else if face_sign.is_some() {
            (Rank(6, 0.0), 0.0)
        } else if italic {
            (Rank(3, 0.0), 0.0)
        } else if max >= target {
            let point = min.max(target);
            (Rank(1, point - target), point)
        } else if max > 0.0 {
            (Rank(2, target - max), max)
        } else if max == 0.0 {
            (Rank(4, 0.0), 0.0)
        } else {
            (Rank(5, -max), max)
        };
        (rank, point * sign)
    } else if normal {
        if italic {
            (Rank(1, 0.0), 0.0)
        } else if max >= 0.0 {
            let point = min.max(0.0);
            (Rank(0, point), point)
        } else {
            (Rank(2, -max), max)
        }
    } else if requested.style == FontStyle::Italic {
        let target = FontSlant::ITALIC_MATCH.value();
        if italic {
            (Rank(0, 0.0), 0.0)
        } else if max >= target {
            let point = min.max(target);
            (Rank(1, point - target), point)
        } else if max > 0.0 {
            (Rank(2, target - max), max)
        } else if max == 0.0 {
            (Rank(3, 0.0), 0.0)
        } else {
            (Rank(4, -max), max)
        }
    } else {
        let sign = requested.slant.value().signum();
        if sign < 0.0 {
            (min, max) = (-max, -min);
        }
        let target = requested.slant.value().abs();
        let (rank, point) = if italic {
            (Rank(4, 0.0), 0.0)
        } else if max > 0.0 {
            // A subnormal maximum bounds the positive slants below the smallest normal one.
            let point = target.clamp(min.max(f32::MIN_POSITIVE).min(max), max);
            let group = if point == target {
                0
            } else if (target >= FontSlant::ITALIC_MATCH.value()) == (point > target) {
                1
            } else {
                2
            };
            (Rank(group, (point - target).abs()), point)
        } else if max == 0.0 && requested.synthesis.oblique {
            (Rank(3, 0.0), 0.0)
        } else {
            (Rank(5, -max), max)
        };
        (rank, point * sign)
    };
    (
        rank,
        FontSlant::new(angle).expect("matching stays within the validated slant bounds"),
    )
}

fn weight_rank(face: f32, target: f32) -> Rank {
    let normal = FontWeight::NORMAL.value();
    let medium = FontWeight::MEDIUM.value();
    if (normal..=medium).contains(&target) {
        if (target..=medium).contains(&face) {
            Rank(0, face - target)
        } else if face < target {
            Rank(1, target - face)
        } else {
            Rank(2, face - medium)
        }
    } else if target < normal {
        if face <= target {
            Rank(0, target - face)
        } else {
            Rank(1, face - target)
        }
    } else if face >= target {
        Rank(0, face - target)
    } else {
        Rank(1, target - face)
    }
}