use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use indexmap::IndexMap;
pub type ObjRef = Rc<RefCell<PsObject>>;
pub type DictRef = Rc<RefCell<IndexMap<Key, ObjRef>>>;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Key {
None,
Int(i64),
Float(u64),
Str(String),
Bytes(Vec<u8>),
File(u64),
}
impl Key {
pub fn as_str(&self) -> Option<&str> {
match self {
Key::Str(s) => Some(s),
_ => None,
}
}
}
impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Key::None => f.write_str("None"),
Key::Int(i) => write!(f, "{i}"),
Key::Float(bits) => f.write_str(&crate::py::float_repr(f64::from_bits(*bits))),
Key::Str(s) => f.write_str(s),
Key::Bytes(b) => f.write_str(&crate::py::bytes_repr(b)),
Key::File(_) => f.write_str("<fontTools.misc.psLib.PSTokenizer object>"),
}
}
}
impl From<&str> for Key {
fn from(s: &str) -> Self {
Key::Str(s.to_owned())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PsString {
Text(String),
Bytes(Vec<u8>),
}
impl PsString {
pub fn len(&self) -> usize {
match self {
PsString::Text(s) => s.chars().count(),
PsString::Bytes(b) => b.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[derive(Clone)]
pub enum Kind {
Operator(&'static str),
Procedure(Vec<ObjRef>),
Name(String),
Literal(String),
Array(Vec<Option<ObjRef>>),
Dict(DictRef),
Font(DictRef),
Mark,
ProcMark,
Null,
File(u64),
Boolean {
value: bool,
py_bool: bool,
},
String(PsString),
Integer(i64),
Real(f64),
}
impl fmt::Debug for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Kind::Operator(n) => write!(f, "<operator {n}>"),
Kind::Procedure(_) => f.write_str("<procedure>"),
Kind::Name(n) => write!(f, "<name {}>", crate::py::str_repr(n)),
Kind::Literal(n) => write!(f, "<literal {}>", crate::py::str_repr(n)),
Kind::Array(_) => f.write_str("<array>"),
Kind::Dict(_) => f.write_str("<dict>"),
Kind::Font(_) => f.write_str("<font>"),
Kind::Mark => f.write_str("<mark 'mark'>"),
Kind::ProcMark => f.write_str("<procmark 'procmark'>"),
Kind::Null => f.write_str("<null None>"),
Kind::File(_) => f.write_str("<file>"),
Kind::Boolean { value, .. } => write!(f, "<boolean {value}>"),
Kind::String(s) => write!(f, "<string {s:?}>"),
Kind::Integer(i) => write!(f, "<integer {i}>"),
Kind::Real(r) => write!(f, "<real {}>", crate::py::float_repr(*r)),
}
}
}
#[derive(Debug, Clone)]
pub struct PsObject {
pub kind: Kind,
pub literal: bool,
pub access: u8,
}
impl PsObject {
pub fn new(kind: Kind) -> ObjRef {
let literal = !matches!(kind, Kind::Operator(_) | Kind::Procedure(_) | Kind::Name(_));
Rc::new(RefCell::new(PsObject {
kind,
literal,
access: 0,
}))
}
pub fn type_name(&self) -> &'static str {
match self.kind {
Kind::Operator(_) => "operatortype",
Kind::Procedure(_) => "proceduretype",
Kind::Name(_) => "nametype",
Kind::Literal(_) => "literaltype",
Kind::Array(_) => "arraytype",
Kind::Dict(_) => "dicttype",
Kind::Font(_) => "fonttype",
Kind::Mark => "marktype",
Kind::ProcMark => "procmarktype",
Kind::Null => "nulltype",
Kind::File(_) => "filetype",
Kind::Boolean { .. } => "booleantype",
Kind::String(_) => "stringtype",
Kind::Integer(_) => "integertype",
Kind::Real(_) => "realtype",
}
}
}
pub const ACCESS_STRINGS: [&str; 4] = ["", "readonly", "executeonly", "noaccess"];
pub const TYPE1_PRE_EEXEC_ORDER: [&str; 10] = [
"FontInfo",
"FontName",
"Encoding",
"PaintType",
"FontType",
"FontMatrix",
"FontBBox",
"UniqueID",
"Metrics",
"StrokeWidth",
];
pub const TYPE1_FONTINFO_ORDER: [&str; 9] = [
"version",
"Notice",
"FullName",
"FamilyName",
"Weight",
"ItalicAngle",
"isFixedPitch",
"UnderlinePosition",
"UnderlineThickness",
];
pub const TYPE1_POST_EEXEC_ORDER: [&str; 3] = ["Private", "CharStrings", "FID"];