mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-06 08:46:06 -10:00
Compare commits
1
Commits
main
...
fonts-stand-ins
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a4fc86535 |
@@ -122,8 +122,36 @@ pub fn load_now(family: &str) -> usize {
|
||||
faces.len()
|
||||
}
|
||||
|
||||
/// A family's Latin faces from Google Fonts, or none
|
||||
/// A family's Latin faces from Google Fonts; for a family Google doesn't have, its free stand-in's (`stand_in`); or none
|
||||
fn fetch(family: &str) -> Vec<(Bytes, f32, bool)> {
|
||||
let faces = fetch_google(family);
|
||||
if !faces.is_empty() {
|
||||
return faces;
|
||||
}
|
||||
match stand_in(family) {
|
||||
Some(s) => {
|
||||
bevy::log::info!("raea-page: {family:?} isn't on Google Fonts: drawn in {s:?}, its closest free family");
|
||||
fetch_google(s)
|
||||
}
|
||||
None => faces,
|
||||
}
|
||||
}
|
||||
|
||||
/// The free family a page's font is drawn in where Google Fonts hasn't got it and the machine hasn't either (12-06: box
|
||||
/// cuts named "Inter Display" and "Helvetica Neue", and drew in DejaVu Sans on the farm): a size of a Google family by
|
||||
/// its own name, or a licensed face's closest free one on its metrics (Arimo is Arial's and Helvetica's)
|
||||
pub fn stand_in(family: &str) -> Option<&'static str> {
|
||||
let f = family.trim().trim_matches(|c| c == '"' || c == '\'').to_ascii_lowercase();
|
||||
match f.as_str() {
|
||||
"inter display" | "inter var" | "inter variable" => Some("Inter"),
|
||||
"helvetica neue" | "helvetica" | "arial" | "helveticaneue" => Some("Arimo"),
|
||||
"sf pro" | "sf pro display" | "sf pro text" => Some("Inter"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A family's Latin faces from Google Fonts, or none
|
||||
fn fetch_google(family: &str) -> Vec<(Bytes, f32, bool)> {
|
||||
let url = format!("https://fonts.googleapis.com/css?family={}:{WEIGHTS}", family.replace(' ', "+"));
|
||||
let css = match raea_io_net::cache::read(&url).and_then(|b| String::from_utf8(b).map_err(|e| e.to_string())) {
|
||||
Ok(css) => css,
|
||||
@@ -165,3 +193,16 @@ pub fn face_now(family: &str, url: &str) -> usize {
|
||||
LOADED.lock().unwrap().push(key);
|
||||
1
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::stand_in;
|
||||
|
||||
#[test]
|
||||
fn a_family_google_has_not_got_draws_in_its_free_stand_in() {
|
||||
assert_eq!(stand_in("Inter Display"), Some("Inter"));
|
||||
assert_eq!(stand_in("\"Helvetica Neue\""), Some("Arimo"));
|
||||
assert_eq!(stand_in("Arial"), Some("Arimo"));
|
||||
assert_eq!(stand_in("Roboto Slab"), None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user