use crate::{GgsqlError, Result};
pub fn register_font(bytes: impl Into<Vec<u8>>) -> Result<Vec<String>> {
let families = hephaestus::text::register_font_families(decode_webfont(bytes.into())?);
if families.is_empty() {
return Err(GgsqlError::WriterError(
"no font faces found: the bytes are not a TTF, OTF, TTC or OTC file \
(a WOFF or WOFF2 container has to be decoded first)"
.to_string(),
));
}
Ok(families)
}
pub fn registered_font_families() -> Vec<String> {
hephaestus::text::registered_families()
}
pub fn set_generic_family(kind: &str, families: &[String]) -> Result<()> {
use hephaestus::text::GenericFamilyKind as K;
let kind = match kind {
"serif" => K::Serif,
"sans-serif" => K::SansSerif,
"monospace" | "mono" => K::Mono,
"cursive" => K::Cursive,
"fantasy" => K::Fantasy,
"system-ui" => K::SystemUi,
other => {
return Err(GgsqlError::WriterError(format!(
"unknown generic family {other:?}: expected one of serif, \
sans-serif, monospace, cursive, fantasy, system-ui"
)))
}
};
hephaestus::text::set_generic_family(kind, families);
Ok(())
}
#[cfg(feature = "webfonts")]
fn decode_webfont(bytes: Vec<u8>) -> Result<Vec<u8>> {
match bytes.get(..4) {
Some(b"wOF2") => wuff::decompress_woff2(&bytes).map_err(|e| {
GgsqlError::WriterError(format!("could not decode the WOFF2 font: {e:?}"))
}),
Some(b"wOFF") => wuff::decompress_woff1(&bytes)
.map_err(|e| GgsqlError::WriterError(format!("could not decode the WOFF font: {e:?}"))),
_ => Ok(bytes),
}
}
#[cfg(not(feature = "webfonts"))]
fn decode_webfont(bytes: Vec<u8>) -> Result<Vec<u8>> {
match bytes.get(..4) {
Some(b"wOF2") | Some(b"wOFF") => Err(GgsqlError::WriterError(
"this build cannot decode WOFF or WOFF2: use TTF, OTF, TTC or OTC, \
or rebuild with the `webfonts` feature"
.to_string(),
)),
_ => Ok(bytes),
}
}