threers 0.1.0

Drop-in three.js replacement for Rust and WebAssembly (wgpu) — THREE.* API, scene graph, PBR, post-fx, mesh BVH, CSG, an OpenSCAD front end with a watertight exact-CSG kernel, headless render, and native media codecs
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
//! Pair kerning from `GPOS`: the `kern` feature's pair-adjustment lookups
//! for Latin text (type 2, directly or through an extension), glyph pairs
//! and class pairs, with variable fonts' deltas applied through `GDEF`'s
//! variation store.
//!
//! A modern font keeps its kerning here and nowhere else — SF Compact, Inter,
//! Roboto have no `kern` table at all — so a reader of `kern` alone sets every
//! "AV", "To" and "Ty" loose.

use super::ttf_var::ItemVariationStore;
use std::collections::HashMap;

fn u16_at(b: &[u8], p: usize) -> Option<u16> {
    Some(u16::from_be_bytes([*b.get(p)?, *b.get(p + 1)?]))
}
fn u32_at(b: &[u8], p: usize) -> Option<u32> {
    Some(u32::from_be_bytes([
        *b.get(p)?,
        *b.get(p + 1)?,
        *b.get(p + 2)?,
        *b.get(p + 3)?,
    ]))
}

/// One pair-adjustment subtable, resolved at the font's location.
#[derive(Debug, Clone)]
enum PairTable {
    /// Explicit pairs: first glyph's coverage, then the second glyph.
    Glyphs {
        coverage: HashMap<u16, u16>,
        pairs: Vec<HashMap<u16, f32>>,
    },
    /// Class pairs: `values[c1 * class2_count + c2]`.
    Classes {
        coverage: HashMap<u16, u16>,
        class1: HashMap<u16, u16>,
        class2: HashMap<u16, u16>,
        class2_count: usize,
        values: Vec<f32>,
    },
}

/// The `kern` feature's pair adjustments, as lookups of subtables.
#[derive(Debug, Clone, Default)]
pub struct GposKerning {
    lookups: Vec<Vec<PairTable>>,
}

impl GposKerning {
    /// Whether the font had any pair kerning in `GPOS`.
    pub fn is_empty(&self) -> bool {
        self.lookups.is_empty()
    }

    /// The x-advance adjustment between two glyphs, in font units.
    ///
    /// Each lookup contributes once: its first subtable that covers `left`
    /// (and, for explicit pairs, lists `right`) decides, as a shaper applies
    /// it.
    pub fn kern(&self, left: u16, right: u16) -> f32 {
        let mut total = 0.0;
        for lookup in &self.lookups {
            for table in lookup {
                match table {
                    PairTable::Glyphs { coverage, pairs } => {
                        let Some(&i) = coverage.get(&left) else {
                            continue;
                        };
                        if let Some(v) = pairs.get(i as usize).and_then(|p| p.get(&right)) {
                            total += v;
                            break;
                        }
                    }
                    PairTable::Classes {
                        coverage,
                        class1,
                        class2,
                        class2_count,
                        values,
                    } => {
                        if !coverage.contains_key(&left) {
                            continue;
                        }
                        let c1 = class1.get(&left).copied().unwrap_or(0) as usize;
                        let c2 = class2.get(&right).copied().unwrap_or(0) as usize;
                        total += values.get(c1 * class2_count + c2).copied().unwrap_or(0.0);
                        break;
                    }
                }
            }
        }
        total
    }
}

/// The lookups the features tagged `tags` name in a `GSUB` or `GPOS` table
/// at `table`, for one script's default language system — Latin, else the
/// default script, else the first — in lookup-list order, as a shaper
/// applies them to a run of that script. Different scripts name different
/// lookups, and the union of them all would kern (or ligate) Latin text
/// with another script's rules. The language system's required feature
/// counts when its tag is asked for.
pub(crate) fn feature_lookups(bytes: &[u8], table: usize, tags: &[&[u8; 4]]) -> Option<Vec<u16>> {
    let scripts = table + u16_at(bytes, table + 4)? as usize;
    let features = table + u16_at(bytes, table + 6)? as usize;
    let script_count = u16_at(bytes, scripts)? as usize;
    let script_at = |tag: &[u8; 4]| -> Option<usize> {
        (0..script_count).find_map(|i| {
            let r = scripts + 2 + 6 * i;
            (bytes.get(r..r + 4)? == tag)
                .then(|| u16_at(bytes, r + 4).map(|o| scripts + o as usize))
                .flatten()
        })
    };
    let script = script_at(b"latn")
        .or_else(|| script_at(b"DFLT"))
        .or_else(|| {
            (script_count > 0)
                .then(|| u16_at(bytes, scripts + 6).map(|o| scripts + o as usize))
                .flatten()
        })?;
    let lang_sys = u16_at(bytes, script)? as usize;
    if lang_sys == 0 {
        return Some(Vec::new());
    }
    let lang_sys = script + lang_sys;
    let feature_count = u16_at(bytes, features)? as usize;
    let required = u16_at(bytes, lang_sys + 2)? as usize;
    let n = u16_at(bytes, lang_sys + 4)? as usize;
    let chosen = (0..n)
        .filter_map(|k| u16_at(bytes, lang_sys + 6 + 2 * k).map(usize::from))
        .chain((required != 0xFFFF).then_some(required))
        .filter(|&i| i < feature_count);
    let mut indices = Vec::new();
    for i in chosen {
        let r = features + 2 + 6 * i;
        let tag = bytes.get(r..r + 4)?;
        if !tags.iter().any(|t| t[..] == *tag) {
            continue;
        }
        let f = features + u16_at(bytes, r + 4)? as usize;
        let n = u16_at(bytes, f + 2)? as usize;
        for k in 0..n {
            indices.push(u16_at(bytes, f + 4 + 2 * k)?);
        }
    }
    indices.sort_unstable();
    indices.dedup();
    Some(indices)
}

/// Lookup `index`'s subtables in a `GSUB` or `GPOS` table at `table`, as
/// `(type, offset)`, through extension lookups (`extension`: 7 in `GSUB`, 9
/// in `GPOS`).
pub(crate) fn lookup_subtables(
    bytes: &[u8],
    table: usize,
    index: u16,
    extension: u16,
) -> Option<Vec<(u16, usize)>> {
    let lookups = table + u16_at(bytes, table + 8)? as usize;
    if index >= u16_at(bytes, lookups)? {
        return None;
    }
    let l = lookups + u16_at(bytes, lookups + 2 + 2 * index as usize)? as usize;
    let kind = u16_at(bytes, l)?;
    let n = u16_at(bytes, l + 4)? as usize;
    let mut out = Vec::with_capacity(n);
    for s in 0..n {
        let mut sub = l + u16_at(bytes, l + 6 + 2 * s)? as usize;
        let mut sub_kind = kind;
        if kind == extension {
            sub_kind = u16_at(bytes, sub + 2)?;
            sub += u32_at(bytes, sub + 4)? as usize;
        }
        out.push((sub_kind, sub));
    }
    Some(out)
}

/// `GPOS` at `gpos`; `store` resolves variation deltas when the font has them.
pub(crate) fn parse(
    bytes: &[u8],
    gpos: usize,
    store: Option<&ItemVariationStore>,
) -> Option<GposKerning> {
    let mut out = GposKerning::default();
    for index in feature_lookups(bytes, gpos, &[b"kern"])? {
        let Some(subtables) = lookup_subtables(bytes, gpos, index, 9) else {
            continue;
        };
        let mut tables = Vec::new();
        for (kind, sub) in subtables {
            if kind != 2 {
                continue;
            }
            if let Some(t) = pair_table(bytes, sub, store) {
                tables.push(t);
            }
        }
        if !tables.is_empty() {
            out.lookups.push(tables);
        }
    }
    Some(out)
}

fn pair_table(b: &[u8], sub: usize, store: Option<&ItemVariationStore>) -> Option<PairTable> {
    let format = u16_at(b, sub)?;
    let coverage = parse_coverage(b, sub + u16_at(b, sub + 2)? as usize)?;
    let vf1 = u16_at(b, sub + 4)?;
    let vf2 = u16_at(b, sub + 6)?;
    let (size1, size2) = (vf1.count_ones() as usize * 2, vf2.count_ones() as usize * 2);
    match format {
        1 => {
            let sets = u16_at(b, sub + 8)? as usize;
            let mut pairs = Vec::with_capacity(sets);
            for i in 0..sets {
                let set = sub + u16_at(b, sub + 10 + 2 * i)? as usize;
                let n = u16_at(b, set)? as usize;
                let mut map = HashMap::with_capacity(n);
                let rec = 2 + size1 + size2;
                for k in 0..n {
                    let r = set + 2 + k * rec;
                    let second = u16_at(b, r)?;
                    // A pair set's device offsets are from the pair set.
                    let v = x_advance(b, r + 2, vf1, set, store)?;
                    map.insert(second, v);
                }
                pairs.push(map);
            }
            Some(PairTable::Glyphs { coverage, pairs })
        }
        2 => {
            let class1 = parse_class_def(b, sub + u16_at(b, sub + 8)? as usize)?;
            let class2 = parse_class_def(b, sub + u16_at(b, sub + 10)? as usize)?;
            let c1n = u16_at(b, sub + 12)? as usize;
            let c2n = u16_at(b, sub + 14)? as usize;
            let rec = size1 + size2;
            let mut values = Vec::with_capacity(c1n * c2n);
            for c1 in 0..c1n {
                for c2 in 0..c2n {
                    let r = sub + 16 + (c1 * c2n + c2) * rec;
                    values.push(x_advance(b, r, vf1, sub, store)?);
                }
            }
            Some(PairTable::Classes {
                coverage,
                class1,
                class2,
                class2_count: c2n,
                values,
            })
        }
        _ => None,
    }
}

/// A value record's XAdvance plus its variation delta, in font units.
fn x_advance(
    b: &[u8],
    record: usize,
    format: u16,
    base: usize,
    store: Option<&ItemVariationStore>,
) -> Option<f32> {
    if format & 0x0004 == 0 {
        return Some(0.0);
    }
    // Fields before XAdvance: XPlacement, YPlacement.
    let at = record + 2 * (format & 0x0003).count_ones() as usize;
    let mut v = u16_at(b, at)? as i16 as f32;
    if format & 0x0040 != 0 {
        // XAdvDevice: after XAdvance, YAdvance, XPlaDevice, YPlaDevice.
        let dev_at = record + 2 * (format & 0x003F).count_ones() as usize;
        let dev = u16_at(b, dev_at)? as usize;
        if dev != 0
            && let Some(store) = store
        {
            let d = base + dev;
            // A VariationIndex table (deltaFormat 0x8000); hinting device
            // tables are for one ppem and are not applied.
            if u16_at(b, d + 4)? == 0x8000 {
                v += store.delta(u16_at(b, d)?, u16_at(b, d + 2)?);
            }
        }
    }
    Some(v)
}

/// A coverage table: glyph id to coverage index.
pub(crate) fn parse_coverage(b: &[u8], off: usize) -> Option<HashMap<u16, u16>> {
    let format = u16_at(b, off)?;
    let n = u16_at(b, off + 2)? as usize;
    let mut out = HashMap::new();
    match format {
        1 => {
            for i in 0..n {
                out.insert(u16_at(b, off + 4 + 2 * i)?, i as u16);
            }
        }
        2 => {
            for i in 0..n {
                let r = off + 4 + 6 * i;
                let (start, end, index) = (u16_at(b, r)?, u16_at(b, r + 2)?, u16_at(b, r + 4)?);
                for g in start..=end.max(start) {
                    out.insert(g, index + (g - start));
                }
            }
        }
        _ => return None,
    }
    Some(out)
}

/// A class definition: glyph id to class, absent glyphs class 0.
pub(crate) fn parse_class_def(b: &[u8], off: usize) -> Option<HashMap<u16, u16>> {
    let format = u16_at(b, off)?;
    let mut out = HashMap::new();
    match format {
        1 => {
            let start = u16_at(b, off + 2)?;
            let n = u16_at(b, off + 4)? as usize;
            for i in 0..n {
                let c = u16_at(b, off + 6 + 2 * i)?;
                if c != 0 {
                    out.insert(start.wrapping_add(i as u16), c);
                }
            }
        }
        2 => {
            let n = u16_at(b, off + 2)? as usize;
            for i in 0..n {
                let r = off + 4 + 6 * i;
                let (start, end, c) = (u16_at(b, r)?, u16_at(b, r + 2)?, u16_at(b, r + 4)?);
                if c != 0 {
                    for g in start..=end.max(start) {
                        out.insert(g, c);
                    }
                }
            }
        }
        _ => return None,
    }
    Some(out)
}

/// One glyph's class in the class definition at `off` (0 when it has
/// none), read in place: an index for format 1, a binary search over the
/// ranges for format 2 (which the format keeps sorted by first glyph).
pub(crate) fn class_of(b: &[u8], off: usize, glyph: u16) -> u16 {
    let found = || -> Option<u16> {
        match u16_at(b, off)? {
            1 => {
                let start = u16_at(b, off + 2)?;
                let i = glyph.checked_sub(start)?;
                if i >= u16_at(b, off + 4)? {
                    return None;
                }
                u16_at(b, off + 6 + 2 * usize::from(i))
            }
            2 => {
                let (mut lo, mut hi) = (0, usize::from(u16_at(b, off + 2)?));
                while lo < hi {
                    let mid = (lo + hi) / 2;
                    let r = off + 4 + 6 * mid;
                    let (first, last) = (u16_at(b, r)?, u16_at(b, r + 2)?);
                    if glyph < first {
                        hi = mid;
                    } else if glyph > last {
                        lo = mid + 1;
                    } else {
                        return u16_at(b, r + 4);
                    }
                }
                None
            }
            _ => None,
        }
    };
    found().unwrap_or(0)
}

/// `GDEF`'s item variation store offset (version 1.3), absolute.
pub(crate) fn gdef_var_store(b: &[u8], gdef: usize) -> Option<usize> {
    if u16_at(b, gdef)? != 1 || u16_at(b, gdef + 2)? < 3 {
        return None;
    }
    let off = u32_at(b, gdef + 14)? as usize;
    (off != 0).then_some(gdef + off)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_class_read_in_place_matches_the_whole_definition() {
        // Format 1 from glyph 5: classes 1, 0, 3; format 2: 10..=12 class 2,
        // 20..=29 class 1, 40..=40 class 3.
        let one = [0, 1, 0, 5, 0, 3, 0, 1, 0, 0, 0, 3];
        let two = [
            0, 2, 0, 3, 0, 10, 0, 12, 0, 2, 0, 20, 0, 29, 0, 1, 0, 40, 0, 40, 0, 3,
        ];
        let mut defs: Vec<(Vec<u8>, usize)> = vec![(one.to_vec(), 0), (two.to_vec(), 0)];
        // And real ones: each font's GDEF glyph classes.
        for path in [
            "src/openscad/DejaVuSans.ttf",
            "/System/Library/Fonts/Supplemental/Arial.ttf",
            "/System/Library/Fonts/SFNS.ttf",
        ] {
            let Ok(b) = std::fs::read(path) else { continue };
            let n = u16_at(&b, 4).unwrap() as usize;
            let gdef = (0..n)
                .map(|i| 12 + 16 * i)
                .find(|&r| &b[r..r + 4] == b"GDEF");
            if let Some(r) = gdef {
                let at = u32_at(&b, r + 8).unwrap() as usize;
                let classes = u16_at(&b, at + 4).unwrap() as usize;
                if classes != 0 {
                    defs.push((b.clone(), at + classes));
                }
            }
        }
        assert!(
            defs.len() >= 3,
            "no font with glyph classes to check against"
        );
        for (b, off) in &defs {
            let all = parse_class_def(b, *off).unwrap();
            for g in 0..=u16::MAX {
                assert_eq!(
                    class_of(b, *off, g),
                    all.get(&g).copied().unwrap_or(0),
                    "glyph {g}"
                );
            }
        }
    }

    #[test]
    fn coverage_ranges_number_glyphs_from_their_start_index() {
        // Format 2: glyphs 10..=12 from index 0, 20..=20 from index 3.
        let b = [0, 2, 0, 2, 0, 10, 0, 12, 0, 0, 0, 20, 0, 20, 0, 3];
        let c = parse_coverage(&b, 0).unwrap();
        assert_eq!(c.get(&11), Some(&1));
        assert_eq!(c.get(&20), Some(&3));
        assert_eq!(c.get(&13), None);
    }

    #[test]
    fn a_class_pair_applies_even_when_its_value_is_zero() {
        // A shaper stops at the first subtable covering the left glyph, so a
        // later subtable must not add to a pair the first one decided.
        let coverage: HashMap<u16, u16> = [(1, 0)].into();
        let first = PairTable::Classes {
            coverage: coverage.clone(),
            class1: HashMap::new(),
            class2: HashMap::new(),
            class2_count: 1,
            values: vec![0.0],
        };
        let second = PairTable::Glyphs {
            coverage,
            pairs: vec![[(2, -50.0)].into()],
        };
        let k = GposKerning {
            lookups: vec![vec![first, second]],
        };
        assert_eq!(k.kern(1, 2), 0.0);
    }
}