ttj 1.1.4

Serialize OpenType tables to JSON
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use super::{variable_scalars::SerializeValueRecordLike, SerializeSubtable};
use crate::{context::SerializationContext, monkeypatching::MonkeyPatchClassDef};
use read_fonts::{
    tables::gpos::{
        CursivePosFormat1, MarkBasePosFormat1, MarkLigPosFormat1, MarkMarkPosFormat1, PairPos,
        SinglePos,
    },
    ReadError,
};
use serde_json::{json, Map, Value};

impl SerializeSubtable for SinglePos<'_> {
    fn serialize_subtable(&self, context: &SerializationContext) -> Result<Value, ReadError> {
        let mut map = Map::new();
        map.insert("type".to_string(), "single".into());
        let coverage = match self {
            SinglePos::Format1(s) => s.coverage()?,
            SinglePos::Format2(s) => s.coverage()?,
        };
        match self {
            SinglePos::Format1(s) => {
                let value = s.value_record().serialize(self.offset_data(), context)?;
                for glyph in coverage.iter() {
                    let name = context.names.get(glyph);
                    map.insert(name, value.clone());
                }
            }
            SinglePos::Format2(s) => {
                for (vr, glyph) in s.value_records().iter().flatten().zip(coverage.iter()) {
                    let name = context.names.get(glyph);
                    map.insert(name, vr.serialize(self.offset_data(), context)?);
                }
            }
        }
        Ok(Value::Object(map))
    }
}

impl SerializeSubtable for PairPos<'_> {
    fn serialize_subtable(&self, context: &SerializationContext) -> Result<Value, ReadError> {
        let mut map = Map::new();
        map.insert("type".to_string(), "pair".into());
        match self {
            PairPos::Format1(s) => {
                for (left_glyph, pairs) in s.coverage()?.iter().zip(s.pair_sets().iter()) {
                    let left_name = context.names.get(left_glyph);
                    let pairs = pairs?;
                    for pair in pairs.pair_value_records().iter() {
                        let pair = pair?;
                        let right_name = context.names.get(pair.second_glyph());
                        let value_1 = pair
                            .value_record1()
                            .serialize(pairs.offset_data(), context)?;
                        let value_2 = pair
                            .value_record2()
                            .serialize(pairs.offset_data(), context)?;
                        let pair_value =
                            if value_2.as_object().map(|x| x.is_empty()).unwrap_or(false) {
                                value_1
                            } else {
                                json!({
                                    "first": value_1,
                                    "second": value_2
                                })
                            };
                        map.entry(left_name.clone())
                            .or_insert_with(|| Value::Object(Map::new()))
                            .as_object_mut()
                            .unwrap()
                            .insert(right_name, pair_value);
                    }
                }
            }
            PairPos::Format2(s) => {
                let class1 = s.class_def1()?;
                let class2 = s.class_def2()?;
                let mut classes = Map::new();
                let mut kerns = Map::new();
                for left_class in 0..s.class1_count() {
                    let mut left_class_glyphs =
                        class1.class_glyphs(left_class, Some(s.coverage()?));
                    left_class_glyphs.sort();
                    classes.insert(
                        format!("@CLASS_L_{}", left_class),
                        Value::Array(
                            left_class_glyphs
                                .into_iter()
                                .map(|gid| Value::String(context.names.get(gid)))
                                .collect(),
                        ),
                    );
                }
                for right_class in 1..s.class2_count() {
                    let mut right_class_glyphs = class2.class_glyphs(right_class, None);
                    right_class_glyphs.sort();
                    classes.insert(
                        format!("@CLASS_R_{}", right_class),
                        Value::Array(
                            right_class_glyphs
                                .into_iter()
                                .map(|gid| Value::String(context.names.get(gid)))
                                .collect(),
                        ),
                    );
                }
                for (left_class, class1_record) in s.class1_records().iter().enumerate() {
                    let class1_record = class1_record?;
                    for (right_class, class2_record) in
                        class1_record.class2_records().iter().enumerate()
                    {
                        let class2_record = class2_record?;
                        let left_name = format!("@CLASS_L_{}", left_class);
                        let right_name = if right_class == 0 {
                            "@All".to_string()
                        } else {
                            format!("@CLASS_R_{}", right_class)
                        };
                        let value_1 = class2_record
                            .value_record1()
                            .serialize(s.offset_data(), context)?;
                        let value_2 = class2_record
                            .value_record2()
                            .serialize(s.offset_data(), context)?;
                        let pair_value =
                            if value_2.as_object().map(|x| x.is_empty()).unwrap_or(false) {
                                value_1
                            } else {
                                json!({
                                    "first": value_1,
                                    "second": value_2
                                })
                            };
                        kerns
                            .entry(left_name.clone())
                            .or_insert_with(|| Value::Object(Map::new()))
                            .as_object_mut()
                            .unwrap()
                            .insert(right_name, pair_value);
                    }
                }
                map.insert("classes".to_string(), classes.into());
                map.insert("kerns".to_string(), kerns.into());
            }
        }
        Ok(Value::Object(map))
    }
}

impl SerializeSubtable for CursivePosFormat1<'_> {
    fn serialize_subtable(&self, context: &SerializationContext) -> Result<Value, ReadError> {
        let mut map = Map::new();
        map.insert("type".to_string(), "cursive".into());
        for (glyph_id, record) in self.coverage()?.iter().zip(self.entry_exit_record()) {
            let name = context.names.get(glyph_id);
            let mut entrymap = Map::new();
            if let Some(entry) = record
                .entry_anchor(self.offset_data())
                .map(|a| a?.serialize(self.offset_data(), context))
                .transpose()?
            {
                entrymap.insert("entry".to_string(), entry);
            }
            if let Some(exit) = record
                .exit_anchor(self.offset_data())
                .map(|a| a?.serialize(self.offset_data(), context))
                .transpose()?
            {
                entrymap.insert("exit".to_string(), exit);
            }
            map.insert(name, Value::Object(entrymap));
        }

        Ok(Value::Object(map))
    }
}

impl SerializeSubtable for MarkBasePosFormat1<'_> {
    fn serialize_subtable(&self, context: &SerializationContext) -> Result<Value, ReadError> {
        let mut map = Map::new();
        map.insert("type".to_string(), "mark_to_base".into());
        let mark_array = self.mark_array()?;
        let mut marks = Map::new();
        for (mark_glyph, mark_record) in self.mark_coverage()?.iter().zip(mark_array.mark_records())
        {
            let mark_name = context.names.get(mark_glyph);
            let class_name = format!("anchor_{}", mark_record.mark_class());
            // "marks": {
            //    "anchor_0": {
            //         "glyph": "anchor",
            //         "glyph": "anchor",
            //    }, ...
            // }
            marks
                .entry(class_name)
                .or_insert_with(|| Map::new().into())
                .as_object_mut()
                .unwrap()
                .insert(
                    mark_name,
                    mark_record
                        .mark_anchor(mark_array.offset_data())?
                        .serialize(self.offset_data(), context)?,
                );
        }
        map.insert("marks".to_string(), marks.into());

        let mut bases = Map::new();
        let base_array = self.base_array()?;
        for (base_glyph, base_record) in self
            .base_coverage()?
            .iter()
            .zip(base_array.base_records().iter().flatten())
        {
            let mut anchors = Map::new();
            for (class, anchor) in base_record
                .base_anchors(base_array.offset_data())
                .iter()
                .enumerate()
            {
                if let Some(Ok(anchor)) = anchor {
                    anchors.insert(
                        format!("anchor_{}", class),
                        anchor.serialize(self.offset_data(), context)?,
                    );
                }
            }

            let base_name = context.names.get(base_glyph);
            bases.insert(base_name, anchors.into());
        }
        map.insert("bases".to_string(), bases.into());

        Ok(Value::Object(map))
    }
}

impl SerializeSubtable for MarkLigPosFormat1<'_> {
    fn serialize_subtable(&self, context: &SerializationContext) -> Result<Value, ReadError> {
        let mut map = Map::new();
        map.insert("type".to_string(), "mark_to_lig".into());
        let mark_array = self.mark_array()?;
        let mut marks = Map::new();
        for (mark_glyph, mark_record) in self.mark_coverage()?.iter().zip(mark_array.mark_records())
        {
            let mark_name = context.names.get(mark_glyph);
            let class_name = format!("anchor_{}", mark_record.mark_class());
            // "marks": {
            //    "anchor_0": {
            //         "glyph": "anchor",
            //         "glyph": "anchor",
            //    }, ...
            // }
            marks
                .entry(class_name)
                .or_insert_with(|| Map::new().into())
                .as_object_mut()
                .unwrap()
                .insert(
                    mark_name,
                    mark_record
                        .mark_anchor(mark_array.offset_data())?
                        .serialize(self.offset_data(), context)?,
                );
        }
        map.insert("marks".to_string(), marks.into());

        let mut ligatures = Map::new();
        let ligature_array = self.ligature_array()?;
        for (ligature_glyph, ligature_attach_record) in self
            .ligature_coverage()?
            .iter()
            .zip(ligature_array.ligature_attaches().iter().flatten())
        {
            let mut anchors = Map::new();
            for (component_id, component_record) in ligature_attach_record
                .component_records()
                .iter()
                .flatten()
                .enumerate()
            {
                for (class, anchor) in component_record
                    .ligature_anchors(ligature_attach_record.offset_data())
                    .iter()
                    .enumerate()
                {
                    if let Some(Ok(anchor)) = anchor {
                        anchors.insert(
                            format!("anchor_{}_{}", class, component_id + 1),
                            anchor.serialize(self.offset_data(), context)?,
                        );
                    }
                }
            }

            let ligature_name = context.names.get(ligature_glyph);
            ligatures.insert(ligature_name, anchors.into());
        }
        map.insert("ligatures".to_string(), ligatures.into());

        Ok(Value::Object(map))
    }
}

impl SerializeSubtable for MarkMarkPosFormat1<'_> {
    fn serialize_subtable(&self, context: &SerializationContext) -> Result<Value, ReadError> {
        let mut map = Map::new();
        map.insert("type".to_string(), "mark_to_mark".into());
        let mark_array = self.mark1_array()?;
        let mut marks = Map::new();
        for (mark_glyph, mark_record) in
            self.mark1_coverage()?.iter().zip(mark_array.mark_records())
        {
            let mark_name = context.names.get(mark_glyph);
            let class_name = format!("anchor_{}", mark_record.mark_class());
            // "marks": {
            //    "anchor_0": {
            //         "glyph": "anchor",
            //         "glyph": "anchor",
            //    }, ...
            // }
            marks
                .entry(class_name)
                .or_insert_with(|| Map::new().into())
                .as_object_mut()
                .unwrap()
                .insert(
                    mark_name,
                    mark_record
                        .mark_anchor(mark_array.offset_data())?
                        .serialize(self.offset_data(), context)?,
                );
        }
        map.insert("marks".to_string(), marks.into());

        let mut bases = Map::new();
        let base_array = self.mark2_array()?;
        for (base_glyph, base_record) in self
            .mark2_coverage()?
            .iter()
            .zip(base_array.mark2_records().iter().flatten())
        {
            let mut anchors = Map::new();
            for (class, anchor) in base_record
                .mark2_anchors(base_array.offset_data())
                .iter()
                .enumerate()
            {
                if let Some(Ok(anchor)) = anchor {
                    anchors.insert(
                        format!("anchor_{}", class),
                        anchor.serialize(self.offset_data(), context)?,
                    );
                }
            }

            let base_name = context.names.get(base_glyph);
            bases.insert(base_name, anchors.into());
        }
        map.insert("basemarks".to_string(), bases.into());

        Ok(Value::Object(map))
    }
}

fn wrap_in_map(v: Value) -> Map<String, Value> {
    if let Some(v) = v.as_object() {
        return v.clone();
    }
    let mut map = Map::new();
    map.insert("default".to_string(), v);
    map
}

fn insert_or_merge(m: &mut Map<String, Value>, key: String, new: Map<String, Value>) {
    if let Some(existing) = m.get_mut(&key).map(|x| x.as_object_mut().unwrap()) {
        // existing and new are both either {x: {loc: 123}, y: {loc: 456}} or {x: 123, y: 456}
        for (k, v) in new.into_iter() {
            let new_obj = wrap_in_map(v);
            let mut old_value = existing
                .get(&k)
                .map(|e| wrap_in_map(e.clone()))
                .unwrap_or_default();
            for (kk, vv) in new_obj.iter() {
                let old = old_value.get(kk).map(|x| x.as_i64().unwrap()).unwrap_or(0);
                old_value.insert(
                    kk.to_string(),
                    Value::Number((vv.as_i64().unwrap() + old).into()),
                );
            }
        }
    } else {
        m.insert(key, new.into());
    }
}
// Since we created the data structure, we're going to be unwrap()ping with gay abandon.
pub fn just_kerns(font: Value) -> Value {
    let mut flatkerns = Map::new();
    for lookup in font
        .get("GPOS")
        .and_then(|x| x.get("lookup_list"))
        .and_then(|x| x.as_object())
        .map(|x| x.values())
        .into_iter()
        .flatten()
        .flat_map(|x| x.as_array().unwrap().iter())
        .filter(|x| x.get("type").map(|x| x == "pair").unwrap_or(false))
        .map(|x| x.as_object().unwrap())
    {
        if lookup.contains_key("kerns") && lookup.contains_key("classes") {
            // Flatten class kerning
            let classes = lookup.get("classes").unwrap().as_object().unwrap();
            let kerns = lookup.get("kerns").unwrap().as_object().unwrap();
            for (left_class, value) in kerns.iter() {
                for (right_class, kern) in value.as_object().unwrap().iter() {
                    if kern == &json!({})
                        || kern == &json!({"x": 0})
                        || kern == &json!({"x": 0, "x_placement": 0})
                    {
                        continue;
                    }
                    let kern = kern.as_object().unwrap();
                    for left_glyph in classes
                        .get(left_class)
                        .unwrap_or(&json!(["@All"]))
                        .as_array()
                        .unwrap()
                        .iter()
                    {
                        // println!("left (class): {:#?} (ID {})", left_glyph, left_class);
                        // println!(
                        //     "right class: {:#?} ({:#?})",
                        //     right_class,
                        //     classes.get(right_class)
                        // );

                        for right_glyph in classes
                            .get(right_class)
                            .unwrap_or(&json!(["@All"]))
                            .as_array()
                            .unwrap()
                            .iter()
                        {
                            // println!("   right (class): {:#?}, kern: {:#?}", right_glyph, kern);
                            let key = left_glyph.as_str().unwrap().to_owned()
                                + "/"
                                + right_glyph.as_str().unwrap();
                            insert_or_merge(&mut flatkerns, key, kern.clone());
                        }
                    }
                }
            }
        } else {
            for (left, value_map) in lookup.iter() {
                // println!("left: {:#?}", left);
                if left == "type" {
                    continue;
                }
                for (right, value) in value_map.as_object().unwrap().iter() {
                    if value == &json!({"x": 0}) {
                        continue;
                    }
                    // println!(" right: {:#?}, value: {:?}", right, value);

                    let kern = value.as_object().unwrap();
                    let key = left.to_owned() + "/" + right.as_str();
                    insert_or_merge(&mut flatkerns, key, kern.clone());
                }
            }
        }
    }
    Value::Object(flatkerns)
}