use super::{Style, Subset, families};
use crate::UnicodeRanges;
#[derive(Debug, Clone, Copy)]
pub struct Family {
pub id: &'static str,
pub ident: &'static str,
pub name: &'static str,
pub subsets: &'static [Subset],
pub weights: &'static [u16],
pub styles: &'static [Style],
pub default_subset: Subset,
pub variable: bool,
pub category: &'static str,
pub license: &'static str,
pub provider: &'static str,
pub unicode_ranges: &'static [(Subset, UnicodeRanges)],
}
impl Family {
#[must_use]
pub fn by_id(id: &str) -> Option<&'static Family> {
families::ALL
.binary_search_by(|f| f.id.cmp(id))
.ok()
.map(|i| families::ALL[i])
}
#[must_use]
pub fn by_ident(ident: &str) -> Option<&'static Family> {
families::ALL.iter().copied().find(|f| f.ident == ident)
}
#[must_use]
pub fn by_name(name: &str) -> Option<&'static Family> {
families::ALL.iter().copied().find(|f| f.name == name)
}
#[must_use]
pub const fn has_weight(&self, weight: u16) -> bool {
let mut i = 0;
while i < self.weights.len() {
if self.weights[i] == weight {
return true;
}
i += 1;
}
false
}
#[must_use]
pub const fn has_style(&self, style: Style) -> bool {
let mut i = 0;
while i < self.styles.len() {
if self.styles[i] as u16 == style as u16 {
return true;
}
i += 1;
}
false
}
#[must_use]
pub const fn has_subset(&self, subset: Subset) -> bool {
let mut i = 0;
while i < self.subsets.len() {
if self.subsets[i] as u16 == subset as u16 {
return true;
}
i += 1;
}
false
}
#[must_use]
pub const fn unicode_range(&self, subset: Subset) -> Option<UnicodeRanges> {
let mut i = 0;
while i < self.unicode_ranges.len() {
if self.unicode_ranges[i].0 as u16 == subset as u16 {
return Some(self.unicode_ranges[i].1);
}
i += 1;
}
None
}
#[must_use]
pub fn jsdelivr_url(&self, subset: Subset, weight: u16, style: Style) -> String {
format!(
"https://cdn.jsdelivr.net/fontsource/fonts/{}@latest/{}-{weight}-{}.woff2",
self.id,
subset.as_str(),
style.as_str(),
)
}
}