use crate::fonts::{FeatureValueType, FontFeature};
use crate::style::{
FontKerning, FontVariantAlternates, FontVariantCaps, FontVariantPosition, Style,
};
pub(super) const KERNING: [u8; 4] = *b"kern";
const LIGATURES: [u8; 4] = *b"liga";
const CONTEXTUAL_LIGATURES: [u8; 4] = *b"clig";
const SMALL_CAPITALS: [u8; 4] = *b"smcp";
const CAPITALS_TO_SMALL: [u8; 4] = *b"c2sc";
const PETITE_CAPITALS: [u8; 4] = *b"pcap";
const CAPITALS_TO_PETITE: [u8; 4] = *b"c2pc";
const UNICASE: [u8; 4] = *b"unic";
const TITLING: [u8; 4] = *b"titl";
const SUBSCRIPT: [u8; 4] = *b"subs";
const SUPERSCRIPT: [u8; 4] = *b"sups";
const HISTORICAL_FORMS: [u8; 4] = *b"hist";
const STYLISTIC_ALTERNATES: [u8; 4] = *b"salt";
const SWASH: [u8; 4] = *b"swsh";
const CONTEXTUAL_SWASH: [u8; 4] = *b"cswh";
const ORNAMENTS: [u8; 4] = *b"ornm";
const ANNOTATION: [u8; 4] = *b"nalt";
const STYLESET_PREFIX: [u8; 2] = *b"ss";
const CHARACTER_VARIANT_PREFIX: [u8; 2] = *b"cv";
const NUMBERED_FEATURES: std::ops::RangeInclusive<u32> = 1..=99;
const DECIMAL: u32 = 10;
const SMALL_CAPS: &[FontFeature] = &[FontFeature::on(SMALL_CAPITALS)];
const ALL_SMALL_CAPS: &[FontFeature] = &[
FontFeature::on(CAPITALS_TO_SMALL),
FontFeature::on(SMALL_CAPITALS),
];
const PETITE_CAPS: &[FontFeature] = &[FontFeature::on(PETITE_CAPITALS)];
const ALL_PETITE_CAPS: &[FontFeature] = &[
FontFeature::on(CAPITALS_TO_PETITE),
FontFeature::on(PETITE_CAPITALS),
];
const UNICASE_CAPS: &[FontFeature] = &[FontFeature::on(UNICASE)];
const TITLING_CAPS: &[FontFeature] = &[FontFeature::on(TITLING)];
const TRACKED_LIGATURES: [FontFeature; 2] = [
FontFeature::off(LIGATURES),
FontFeature::off(CONTEXTUAL_LIGATURES),
];
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct CapsPlan {
pub features: &'static [FontFeature],
pub synthetic_lower: bool,
pub synthetic_upper: bool,
}
impl CapsPlan {
const NATIVE_NONE: Self = Self::native(&[]);
const fn native(features: &'static [FontFeature]) -> Self {
Self {
features,
synthetic_lower: false,
synthetic_upper: false,
}
}
pub(super) fn synthetic(self) -> bool {
self.synthetic_lower || self.synthetic_upper
}
}
pub(super) fn caps_plan(
caps: FontVariantCaps,
has: impl Fn([u8; 4]) -> bool,
synthesis: bool,
) -> CapsPlan {
let synthesized = |lower, upper| CapsPlan {
features: &[],
synthetic_lower: lower && synthesis,
synthetic_upper: upper && synthesis,
};
let small = || has(SMALL_CAPITALS);
let all_small = || has(SMALL_CAPITALS) && has(CAPITALS_TO_SMALL);
match caps {
FontVariantCaps::Normal => CapsPlan::NATIVE_NONE,
FontVariantCaps::SmallCaps if small() => CapsPlan::native(SMALL_CAPS),
FontVariantCaps::PetiteCaps if has(PETITE_CAPITALS) => CapsPlan::native(PETITE_CAPS),
FontVariantCaps::PetiteCaps if small() => CapsPlan::native(SMALL_CAPS),
FontVariantCaps::SmallCaps | FontVariantCaps::PetiteCaps => synthesized(true, false),
FontVariantCaps::AllSmallCaps if all_small() => CapsPlan::native(ALL_SMALL_CAPS),
FontVariantCaps::AllPetiteCaps if has(PETITE_CAPITALS) && has(CAPITALS_TO_PETITE) => {
CapsPlan::native(ALL_PETITE_CAPS)
}
FontVariantCaps::AllPetiteCaps if all_small() => CapsPlan::native(ALL_SMALL_CAPS),
FontVariantCaps::AllSmallCaps | FontVariantCaps::AllPetiteCaps => synthesized(true, true),
FontVariantCaps::Unicase if has(UNICASE) => CapsPlan::native(UNICASE_CAPS),
FontVariantCaps::Unicase => synthesized(false, true),
FontVariantCaps::TitlingCaps if has(TITLING) => CapsPlan::native(TITLING_CAPS),
FontVariantCaps::TitlingCaps => CapsPlan::NATIVE_NONE,
}
}
pub(super) fn position_feature(position: FontVariantPosition) -> Option<FontFeature> {
match position {
FontVariantPosition::Normal => None,
FontVariantPosition::Sub => Some(FontFeature::on(SUBSCRIPT)),
FontVariantPosition::Super => Some(FontFeature::on(SUPERSCRIPT)),
}
}
pub(super) fn alternate_features<'a>(
alternates: &FontVariantAlternates,
lookup: impl Fn(FeatureValueType, &str) -> Option<&'a [u32]>,
) -> Vec<FontFeature> {
let mut features = Vec::new();
let single = |kind, name: &Option<Box<str>>| {
name.as_deref()
.and_then(|name| lookup(kind, name))
.and_then(|values| values.first().copied())
};
if let Some(index) = single(FeatureValueType::Stylistic, &alternates.stylistic) {
features.push(FontFeature {
tag: STYLISTIC_ALTERNATES,
value: index,
});
}
if alternates.historical_forms {
features.push(FontFeature::on(HISTORICAL_FORMS));
}
for name in &alternates.styleset {
for set in lookup(FeatureValueType::Styleset, name).unwrap_or_default() {
if let Some(tag) = numbered(STYLESET_PREFIX, *set) {
features.push(FontFeature::on(tag));
}
}
}
for name in &alternates.character_variant {
if let Some(&[variant, ref value @ ..]) = lookup(FeatureValueType::CharacterVariant, name)
&& let Some(tag) = numbered(CHARACTER_VARIANT_PREFIX, variant)
{
features.push(FontFeature {
tag,
value: value.first().copied().unwrap_or(FontFeature::ON),
});
}
}
if let Some(index) = single(FeatureValueType::Swash, &alternates.swash) {
for tag in [SWASH, CONTEXTUAL_SWASH] {
features.push(FontFeature { tag, value: index });
}
}
for (kind, name, tag) in [
(
FeatureValueType::Ornaments,
&alternates.ornaments,
ORNAMENTS,
),
(
FeatureValueType::Annotation,
&alternates.annotation,
ANNOTATION,
),
] {
if let Some(index) = single(kind, name) {
features.push(FontFeature { tag, value: index });
}
}
features
}
fn numbered(prefix: [u8; 2], index: u32) -> Option<[u8; 4]> {
if !NUMBERED_FEATURES.contains(&index) {
return None;
}
let digit = |value: u32| b'0' + u8::try_from(value).expect("decimal digit");
Some([
prefix[0],
prefix[1],
digit(index / DECIMAL),
digit(index % DECIMAL),
])
}
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct FaceFeatures<'a> {
pub descriptor: &'a [FontFeature],
pub alternates: &'a [FontFeature],
pub caps: &'static [FontFeature],
pub position: Option<FontFeature>,
}
pub(super) fn run_features(
style: &Style,
face: FaceFeatures<'_>,
tracked: bool,
) -> Vec<FontFeature> {
let features = &style.font_features;
let mut list = face.descriptor.to_vec();
match features.kerning {
FontKerning::Auto => {}
FontKerning::Normal => list.push(FontFeature::on(KERNING)),
FontKerning::None => list.push(FontFeature::off(KERNING)),
}
list.extend_from_slice(&features.ligatures);
list.extend(face.position);
list.extend_from_slice(face.caps);
list.extend_from_slice(&features.numeric);
list.extend_from_slice(face.alternates);
list.extend_from_slice(&features.east_asian);
if tracked {
list.extend_from_slice(&TRACKED_LIGATURES);
}
list.extend_from_slice(&features.settings);
list
}