use fontoxide_misc::fixed_tools::{fixed_to_float, float_to_fixed};
use fontoxide_misc::xml_writer::AttrValue;
use std::borrow::Cow;
use crate::binary::{Reader, WriteBytes};
use crate::{Error, Result};
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Int(i64),
Float(f64),
Bytes(Vec<u8>),
}
impl AttrValue for Value {
fn attr_string(&self) -> Cow<'_, str> {
match self {
Value::Int(i) => Cow::Owned(i.to_string()),
Value::Float(f) => Cow::Owned(fontoxide_misc::py::float_repr(*f)),
Value::Bytes(b) => Cow::Owned(bytes_to_py_str(b)),
}
}
}
pub fn bytes_to_py_str(b: &[u8]) -> String {
if b.is_ascii() {
b.iter().map(|&c| c as char).collect()
} else {
fontoxide_misc::py::bytes_repr(b)
}
}
pub trait Kind {
type Value: Clone + Default + std::fmt::Debug + PartialEq;
const CODE: &'static str;
const FRACTION_BITS: Option<u32> = None;
const SIZE: usize;
fn read(r: &mut Reader<'_>) -> Result<Self::Value>;
fn write(v: &Self::Value, out: &mut Vec<u8>, field: &str) -> Result<()>;
fn value(v: &Self::Value) -> Value;
fn from_literal(lit: &fontoxide_misc::text_tools::Literal, field: &str) -> Result<Self::Value>;
}
fn bad_literal(field: &str) -> Error {
Error::value(format!("invalid value for {field}"))
}
macro_rules! int_kind {
($name:ident, $t:ty, $code:literal, $read:ident, $put:ident) => {
#[doc = concat!("`", $code, "`: `", stringify!($t), "`.")]
#[derive(Debug)]
pub enum $name {}
impl Kind for $name {
type Value = $t;
const CODE: &'static str = $code;
const SIZE: usize = std::mem::size_of::<$t>();
fn read(r: &mut Reader<'_>) -> Result<$t> {
r.$read()
}
fn write(v: &$t, out: &mut Vec<u8>, _field: &str) -> Result<()> {
out.$put(*v);
Ok(())
}
fn value(v: &$t) -> Value {
Value::Int(*v as i64)
}
fn from_literal(lit: &fontoxide_misc::text_tools::Literal, field: &str) -> Result<$t> {
let i = lit.as_int().ok_or_else(|| bad_literal(field))?;
crate::binary::fit(i, field)
}
}
};
}
int_kind!(U8, u8, "B", u8, put_u8);
int_kind!(I8, i8, "b", i8, put_i8);
int_kind!(U16, u16, "H", u16, put_u16);
#[derive(Debug)]
pub enum U16Wide {}
impl Kind for U16Wide {
type Value = u32;
const CODE: &'static str = "H";
const SIZE: usize = 2;
fn read(r: &mut Reader<'_>) -> Result<u32> {
Ok(u32::from(r.u16()?))
}
fn write(v: &u32, out: &mut Vec<u8>, field: &str) -> Result<()> {
out.put_u16(crate::binary::fit(*v, field)?);
Ok(())
}
fn value(v: &u32) -> Value {
Value::Int(i64::from(*v))
}
fn from_literal(lit: &fontoxide_misc::text_tools::Literal, field: &str) -> Result<u32> {
let i = lit.as_int().ok_or_else(|| bad_literal(field))?;
crate::binary::fit(i, field)
}
}
int_kind!(I16, i16, "h", i16, put_i16);
int_kind!(U32, u32, "L", u32, put_u32);
int_kind!(I32, i32, "l", i32, put_i32);
int_kind!(U64, u64, "Q", u64, put_u64);
int_kind!(I64, i64, "q", i64, put_i64);
macro_rules! fixed_kind {
($name:ident, $code:literal, $bits:literal, $raw:ty, $read:ident, $put:ident) => {
#[doc = concat!("A fixed-point number with ", stringify!($bits), " fraction bits, held as `f64`.")]
#[derive(Debug)]
pub enum $name {}
impl Kind for $name {
type Value = f64;
const CODE: &'static str = $code;
const FRACTION_BITS: Option<u32> = Some($bits);
const SIZE: usize = std::mem::size_of::<$raw>();
fn read(r: &mut Reader<'_>) -> Result<f64> {
Ok(fixed_to_float(r.$read()? as i64, $bits))
}
fn write(v: &f64, out: &mut Vec<u8>, field: &str) -> Result<()> {
let raw: $raw = crate::binary::fit(float_to_fixed(*v, $bits), field)?;
out.$put(raw);
Ok(())
}
fn value(v: &f64) -> Value {
Value::Float(*v)
}
fn from_literal(lit: &fontoxide_misc::text_tools::Literal, field: &str) -> Result<f64> {
lit.as_float().ok_or_else(|| bad_literal(field))
}
}
};
}
fixed_kind!(F2Dot14, "h", 14, i16, i16, put_i16);
fixed_kind!(F16Dot16, "l", 16, i32, i32, put_i32);
#[derive(Debug)]
pub enum Bytes<const N: usize> {}
impl<const N: usize> Kind for Bytes<N>
where
[u8; N]: Default,
{
type Value = [u8; N];
const CODE: &'static str = "s";
const SIZE: usize = N;
fn read(r: &mut Reader<'_>) -> Result<[u8; N]> {
r.array()
}
fn write(v: &[u8; N], out: &mut Vec<u8>, _field: &str) -> Result<()> {
out.extend_from_slice(v);
Ok(())
}
fn value(v: &[u8; N]) -> Value {
Value::Bytes(v.to_vec())
}
fn from_literal(lit: &fontoxide_misc::text_tools::Literal, field: &str) -> Result<[u8; N]> {
use fontoxide_misc::text_tools::Literal;
let bytes = match lit {
Literal::Bytes(b) => b.clone(),
Literal::Str(s) => s.bytes().collect::<Vec<u8>>(),
_ => return Err(bad_literal(field)),
};
let mut out = [0u8; N];
let n = bytes.len().min(N);
out[..n].copy_from_slice(&bytes[..n]);
Ok(out)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FieldInfo {
pub name: &'static str,
pub code: &'static str,
pub size: usize,
pub fraction_bits: Option<u32>,
}
#[macro_export]
#[doc(hidden)]
macro_rules! sstruct {
(
$(#[$meta:meta])*
$vis:vis struct $name:ident = $format:literal {
$(
$(#[$fmeta:meta])*
$fvis:vis $field:ident : $ty:ty = $py:literal as $kind:ty
),* $(,)?
}
$(extra {
$(
$(#[$xmeta:meta])*
$xvis:vis $xfield:ident : $xty:ty
),* $(,)?
})?
) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq, Default)]
$vis struct $name {
$(
$(#[$fmeta])*
#[doc = concat!("\n\nfontTools: `", $py, "`.")]
$fvis $field: $ty,
)*
$($(
$(#[$xmeta])*
$xvis $xfield: $xty,
)*)?
}
impl $name {
pub const FORMAT_NAME: &'static str = $format;
pub const SIZE: usize = 0 $(+ <$kind as $crate::sstruct::Kind>::SIZE)*;
pub const FIELDS: &'static [$crate::sstruct::FieldInfo] = &[$(
$crate::sstruct::FieldInfo {
name: $py,
code: <$kind as $crate::sstruct::Kind>::CODE,
size: <$kind as $crate::sstruct::Kind>::SIZE,
fraction_bits: <$kind as $crate::sstruct::Kind>::FRACTION_BITS,
},
)*];
pub fn unpack(r: &mut $crate::binary::Reader<'_>) -> $crate::Result<Self> {
Ok($name {
$($field: <$kind as $crate::sstruct::Kind>::read(r)?,)*
$($($xfield: Default::default(),)*)?
})
}
pub fn unpack2(data: &[u8]) -> $crate::Result<(Self, &[u8])> {
if data.len() < Self::SIZE {
return Err($crate::Error::Struct(format!(
"unpack requires a buffer of {} bytes",
Self::SIZE
)));
}
let mut r = $crate::binary::Reader::new(data);
let v = Self::unpack(&mut r)?;
Ok((v, r.rest()))
}
pub fn pack(&self, out: &mut Vec<u8>) -> $crate::Result<()> {
$(<$kind as $crate::sstruct::Kind>::write(&self.$field, out, $py)?;)*
Ok(())
}
pub fn values(&self) -> Vec<(&'static str, $crate::sstruct::Value)> {
vec![$(($py, <$kind as $crate::sstruct::Kind>::value(&self.$field)),)*]
}
pub fn set_literal(
&mut self,
name: &str,
literal: &fontoxide_misc::text_tools::Literal,
) -> $crate::Result<bool> {
match name {
$($py => {
self.$field = <$kind as $crate::sstruct::Kind>::from_literal(literal, $py)?;
Ok(true)
})*
_ => Ok(false),
}
}
}
};
}
pub fn eval(value: &str) -> Result<fontoxide_misc::text_tools::Literal> {
fontoxide_misc::text_tools::safe_eval(value)
.ok_or_else(|| Error::value(format!("malformed value: {value:?}")))
}
#[cfg(test)]
pub(crate) mod tests {
use super::FieldInfo;
#[derive(serde::Deserialize)]
struct Export {
formats: Vec<Format>,
}
#[derive(serde::Deserialize)]
struct Format {
name: String,
struct_format: String,
fields: Vec<String>,
fixes: std::collections::HashMap<String, u32>,
}
pub(crate) fn check(format_name: &str, fields: &[FieldInfo]) {
let export: Export = serde_json::from_str(include_str!("../codegen/sstruct.json"))
.expect("valid sstruct.json");
let format = export
.formats
.iter()
.find(|f| f.name == format_name)
.unwrap_or_else(|| panic!("{format_name} not found in sstruct.json"));
let names: Vec<&str> = fields.iter().map(|f| f.name).collect();
assert_eq!(names, format.fields, "{format_name}: field names");
let mut codes = String::new();
for f in fields {
if f.code == "s" {
codes.push_str(&format!("{}s", f.size));
} else {
codes.push_str(f.code);
}
}
let upstream = format
.struct_format
.trim_start_matches(['>', '<', '!', '='])
.replace('I', "L")
.replace('i', "l");
assert_eq!(codes, upstream, "{format_name}: struct format");
for f in fields {
assert_eq!(
f.fraction_bits,
format.fixes.get(f.name).copied(),
"{format_name}.{}: fixed point",
f.name
);
}
}
}