use super::DocumentId;
use crate::named::named;
use std::collections::BTreeMap;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct FontId(pub(super) usize);
named! {
pub(crate) enum Fallback {
Body = "body",
Heading = "heading",
Mono = "mono",
}
}
named! {
pub(crate) enum Weight {
Normal = "normal",
Bold = "bold",
}
}
named! {
pub(crate) enum Style {
Normal = "normal",
Italic = "italic",
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Font {
pub(crate) document: DocumentId,
pub(crate) name: String,
pub(crate) family: String,
pub(crate) fallback: Fallback,
pub(crate) files: Vec<FontFile>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct FontFile {
pub(crate) from: String,
pub(crate) weight: Weight,
pub(crate) style: Style,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum FontBinding {
Font(FontId),
Ambiguous(Vec<String>),
Dropped,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct FontNames {
bindings: BTreeMap<String, FontBinding>,
incomplete: bool,
}
impl FontNames {
pub(crate) fn get(&self, name: &str) -> Option<&FontBinding> {
const DROPPED: &FontBinding = &FontBinding::Dropped;
self.bindings
.get(name)
.or_else(|| self.incomplete.then_some(DROPPED))
}
pub(crate) fn all(&self) -> impl Iterator<Item = &str> {
self.bindings.keys().map(String::as_str)
}
}
#[derive(Debug, Default)]
pub(crate) struct FontNamesBuilder {
declared: BTreeMap<String, FontBinding>,
shared: BTreeMap<String, Vec<(FontId, String)>>,
incomplete: bool,
}
impl FontNamesBuilder {
pub(crate) fn is_declared(&self, name: &str) -> bool {
self.declared.contains_key(name)
}
pub(crate) fn declare(&mut self, name: &str, binding: FontBinding) {
self.declared.insert(name.to_string(), binding);
}
pub(crate) fn share(&mut self, name: &str, id: FontId, source: &str) {
let providers = self.shared.entry(name.to_string()).or_default();
if !providers.iter().any(|(known, _)| *known == id) {
providers.push((id, source.to_string()));
}
}
pub(crate) fn incomplete(&mut self) {
self.incomplete = true;
}
pub(crate) fn build(self) -> FontNames {
let mut bindings = self.declared;
for (name, providers) in self.shared {
bindings
.entry(name)
.or_insert_with(|| match &providers[..] {
[(id, _)] => FontBinding::Font(*id),
_ => FontBinding::Ambiguous(
providers.into_iter().map(|(_, source)| source).collect(),
),
});
}
FontNames {
bindings,
incomplete: self.incomplete,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn declared_names_shadow_wildcard_ones_and_two_wildcards_are_ambiguous() {
let mut names = FontNamesBuilder::default();
names.declare("serif", FontBinding::Font(FontId(0)));
names.share("serif", FontId(1), "@a/look");
names.share("sans", FontId(2), "@a/look");
names.share("sans", FontId(3), "@b/look");
names.share("mono-x", FontId(4), "@a/look");
names.share("mono-x", FontId(4), "@c/re-export");
assert!(names.is_declared("serif") && !names.is_declared("sans"));
let names = names.build();
assert_eq!(names.get("serif"), Some(&FontBinding::Font(FontId(0))));
assert_eq!(
names.get("sans"),
Some(&FontBinding::Ambiguous(vec![
"@a/look".into(),
"@b/look".into()
]))
);
assert_eq!(names.get("mono-x"), Some(&FontBinding::Font(FontId(4))));
assert_eq!(names.get("other"), None);
assert_eq!(names.all().collect::<Vec<_>>(), ["mono-x", "sans", "serif"]);
}
#[test]
fn an_incomplete_wildcard_import_may_hold_any_name() {
let mut names = FontNamesBuilder::default();
names.incomplete();
assert_eq!(names.build().get("any"), Some(&FontBinding::Dropped));
}
#[test]
fn the_tokens_weights_and_styles_have_their_names() {
assert_eq!(Fallback::from_name("heading"), Some(Fallback::Heading));
assert_eq!(Weight::Bold.name(), "bold");
assert_eq!(Style::from_name("oblique"), None);
}
}