1use std::cell::RefCell;
9use std::fmt;
10use std::rc::Rc;
11
12use indexmap::IndexMap;
13
14pub type ObjRef = Rc<RefCell<PsObject>>;
17
18pub type DictRef = Rc<RefCell<IndexMap<Key, ObjRef>>>;
21
22#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
26pub enum Key {
27 None,
29 Int(i64),
31 Float(u64),
33 Str(String),
35 Bytes(Vec<u8>),
37 File(u64),
39}
40
41impl Key {
42 pub fn as_str(&self) -> Option<&str> {
44 match self {
45 Key::Str(s) => Some(s),
46 _ => None,
47 }
48 }
49}
50
51impl fmt::Display for Key {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 match self {
55 Key::None => f.write_str("None"),
56 Key::Int(i) => write!(f, "{i}"),
57 Key::Float(bits) => f.write_str(&crate::py::float_repr(f64::from_bits(*bits))),
58 Key::Str(s) => f.write_str(s),
59 Key::Bytes(b) => f.write_str(&crate::py::bytes_repr(b)),
60 Key::File(_) => f.write_str("<fontTools.misc.psLib.PSTokenizer object>"),
61 }
62 }
63}
64
65impl From<&str> for Key {
66 fn from(s: &str) -> Self {
67 Key::Str(s.to_owned())
68 }
69}
70
71#[derive(Debug, Clone, PartialEq, Eq)]
74pub enum PsString {
75 Text(String),
77 Bytes(Vec<u8>),
79}
80
81impl PsString {
82 pub fn len(&self) -> usize {
84 match self {
85 PsString::Text(s) => s.chars().count(),
86 PsString::Bytes(b) => b.len(),
87 }
88 }
89
90 pub fn is_empty(&self) -> bool {
92 self.len() == 0
93 }
94}
95
96#[derive(Clone)]
98pub enum Kind {
99 Operator(&'static str),
101 Procedure(Vec<ObjRef>),
103 Name(String),
105 Literal(String),
107 Array(Vec<Option<ObjRef>>),
109 Dict(DictRef),
111 Font(DictRef),
113 Mark,
115 ProcMark,
117 Null,
119 File(u64),
121 Boolean {
125 value: bool,
127 py_bool: bool,
129 },
130 String(PsString),
132 Integer(i64),
134 Real(f64),
136}
137
138impl fmt::Debug for Kind {
139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 match self {
141 Kind::Operator(n) => write!(f, "<operator {n}>"),
142 Kind::Procedure(_) => f.write_str("<procedure>"),
143 Kind::Name(n) => write!(f, "<name {}>", crate::py::str_repr(n)),
144 Kind::Literal(n) => write!(f, "<literal {}>", crate::py::str_repr(n)),
145 Kind::Array(_) => f.write_str("<array>"),
146 Kind::Dict(_) => f.write_str("<dict>"),
147 Kind::Font(_) => f.write_str("<font>"),
148 Kind::Mark => f.write_str("<mark 'mark'>"),
149 Kind::ProcMark => f.write_str("<procmark 'procmark'>"),
150 Kind::Null => f.write_str("<null None>"),
151 Kind::File(_) => f.write_str("<file>"),
152 Kind::Boolean { value, .. } => write!(f, "<boolean {value}>"),
153 Kind::String(s) => write!(f, "<string {s:?}>"),
154 Kind::Integer(i) => write!(f, "<integer {i}>"),
155 Kind::Real(r) => write!(f, "<real {}>", crate::py::float_repr(*r)),
156 }
157 }
158}
159
160#[derive(Debug, Clone)]
162pub struct PsObject {
163 pub kind: Kind,
165 pub literal: bool,
167 pub access: u8,
170}
171
172impl PsObject {
173 pub fn new(kind: Kind) -> ObjRef {
175 let literal = !matches!(kind, Kind::Operator(_) | Kind::Procedure(_) | Kind::Name(_));
176 Rc::new(RefCell::new(PsObject {
177 kind,
178 literal,
179 access: 0,
180 }))
181 }
182
183 pub fn type_name(&self) -> &'static str {
185 match self.kind {
186 Kind::Operator(_) => "operatortype",
187 Kind::Procedure(_) => "proceduretype",
188 Kind::Name(_) => "nametype",
189 Kind::Literal(_) => "literaltype",
190 Kind::Array(_) => "arraytype",
191 Kind::Dict(_) => "dicttype",
192 Kind::Font(_) => "fonttype",
193 Kind::Mark => "marktype",
194 Kind::ProcMark => "procmarktype",
195 Kind::Null => "nulltype",
196 Kind::File(_) => "filetype",
197 Kind::Boolean { .. } => "booleantype",
198 Kind::String(_) => "stringtype",
199 Kind::Integer(_) => "integertype",
200 Kind::Real(_) => "realtype",
201 }
202 }
203}
204
205pub const ACCESS_STRINGS: [&str; 4] = ["", "readonly", "executeonly", "noaccess"];
207
208pub const TYPE1_PRE_EEXEC_ORDER: [&str; 10] = [
211 "FontInfo",
212 "FontName",
213 "Encoding",
214 "PaintType",
215 "FontType",
216 "FontMatrix",
217 "FontBBox",
218 "UniqueID",
219 "Metrics",
220 "StrokeWidth",
221];
222
223pub const TYPE1_FONTINFO_ORDER: [&str; 9] = [
225 "version",
226 "Notice",
227 "FullName",
228 "FamilyName",
229 "Weight",
230 "ItalicAngle",
231 "isFixedPitch",
232 "UnderlinePosition",
233 "UnderlineThickness",
234];
235
236pub const TYPE1_POST_EEXEC_ORDER: [&str; 3] = ["Private", "CharStrings", "FID"];