use crate::schema::GenericFamily;
use std::rc::Rc;
pub const FONT_REF_LEN: usize = 12;
pub const FONT_REF_KIND_AT: usize = 0;
pub const FONT_REF_VALUE_AT: usize = 4;
pub const FONT_REF_LENGTH_AT: usize = 8;
pub const FONT_REF_ASSET: u32 = 1;
pub const FONT_REF_SYSTEM: u32 = 2;
pub const FONT_REF_GENERIC: u32 = 3;
pub const MAX_FONT_REFS: usize = 8;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FontRef {
Asset(u32),
System(String),
Generic(GenericFamily),
}
#[derive(Clone, Default, PartialEq)]
pub struct FontRefs {
bytes: Option<Rc<[u8]>>,
}
impl std::fmt::Debug for FontRefs {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.debug_list().entries(self.refs()).finish()
}
}
impl FontRefs {
pub fn new(refs: impl IntoIterator<Item = FontRef>) -> Self {
let refs: Vec<FontRef> = refs.into_iter().take(MAX_FONT_REFS).collect();
if refs.is_empty() {
return Self::default();
}
let mut bytes = Vec::with_capacity(4 + refs.len() * FONT_REF_LEN);
bytes.extend_from_slice(&(refs.len() as u32).to_le_bytes());
let names_at = 4 + refs.len() * FONT_REF_LEN;
write_records(&refs, &mut bytes, names_at as u32);
Self {
bytes: Some(bytes.into()),
}
}
pub fn from_bytes(bytes: impl Into<Rc<[u8]>>) -> Self {
let bytes = bytes.into();
Self {
bytes: (!bytes.is_empty()).then_some(bytes),
}
}
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_deref().unwrap_or(&[])
}
pub fn is_empty(&self) -> bool {
self.bytes.is_none()
}
pub fn len(&self) -> usize {
let bytes = self.as_bytes();
if bytes.len() < 4 {
return 0;
}
u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize
}
pub fn refs(&self) -> Vec<FontRef> {
let bytes = self.as_bytes();
(0..self.len())
.filter_map(|index| decode_record(bytes, 4 + index * FONT_REF_LEN))
.collect()
}
pub(crate) fn write_relocated(&self, out: &mut Vec<u8>, base: u32) {
write_records(&self.refs(), out, base);
}
}
fn write_records(refs: &[FontRef], out: &mut Vec<u8>, names_base: u32) {
let mut names: Vec<u8> = Vec::new();
for font in refs {
let (kind, value, length) = match font {
FontRef::Asset(id) => (FONT_REF_ASSET, *id, 0),
FontRef::System(name) => {
let at = names_base + names.len() as u32;
names.extend_from_slice(name.as_bytes());
(FONT_REF_SYSTEM, at, name.len() as u32)
}
FontRef::Generic(family) => (FONT_REF_GENERIC, u32::from(u16::from(*family)), 0),
};
out.extend_from_slice(&kind.to_le_bytes());
out.extend_from_slice(&value.to_le_bytes());
out.extend_from_slice(&length.to_le_bytes());
}
out.extend_from_slice(&names);
}
pub(crate) fn decode_record(bytes: &[u8], at: usize) -> Option<FontRef> {
let word = |offset: usize| -> Option<u32> {
let slice = bytes.get(at + offset..at + offset + 4)?;
Some(u32::from_le_bytes([slice[0], slice[1], slice[2], slice[3]]))
};
let value = word(FONT_REF_VALUE_AT)?;
match word(FONT_REF_KIND_AT)? {
FONT_REF_ASSET => Some(FontRef::Asset(value)),
FONT_REF_SYSTEM => {
let length = word(FONT_REF_LENGTH_AT)? as usize;
let name = bytes.get(value as usize..value as usize + length)?;
std::str::from_utf8(name)
.ok()
.map(|name| FontRef::System(name.to_owned()))
}
FONT_REF_GENERIC => u16::try_from(value)
.ok()
.and_then(|tag| GenericFamily::try_from(tag).ok())
.map(FontRef::Generic),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fr40_a_font_list_round_trips_in_order() {
let list = FontRefs::new([
FontRef::Asset(7),
FontRef::System("JetBrains Mono".to_owned()),
FontRef::Generic(GenericFamily::Monospace),
]);
assert_eq!(
list.refs(),
vec![
FontRef::Asset(7),
FontRef::System("JetBrains Mono".to_owned()),
FontRef::Generic(GenericFamily::Monospace),
]
);
assert_eq!(list.len(), 3);
}
#[test]
fn fr40_a_font_list_holds_at_most_eight() {
let list = FontRefs::new((0..12).map(FontRef::Asset));
assert_eq!(list.len(), MAX_FONT_REFS);
assert_eq!(list.refs().last(), Some(&FontRef::Asset(7)));
}
#[test]
fn fr40_an_empty_font_list_travels_as_nothing() {
assert!(FontRefs::new([]).is_empty());
assert!(FontRefs::new([]).as_bytes().is_empty());
}
}