webfont-generator 0.9.0

Generate webfonts (SVG, TTF, EOT, WOFF, WOFF2) from SVG icons
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
use std::hash::Hasher;
use std::io::{Error, ErrorKind};

use rustc_hash::FxHasher;
use write_fonts::read::tables::compute_checksum;
use write_fonts::read::tables::glyf::{Glyf, Glyph, PointFlags, SimpleGlyph};
use write_fonts::read::tables::loca::Loca;
use write_fonts::read::types::{GlyphId, Point};
use write_fonts::read::{FontData, FontRead};

use crate::byte_helpers::BigEndian;
use crate::sfnt::SerializedTable;

use super::Woff2TransformPayload;

pub(super) const GLYF_ENCODER_VERSION: u8 = 2;
const GLYF_TRANSFORM_HEADER_SIZE: usize = 36;

pub(super) struct NormalizedGlyfLoca {
    pub(super) glyf_len: usize,
    pub(super) glyf_checksum: u32,
    pub(super) loca_format: i16,
    pub(super) loca_len: usize,
    pub(super) loca_checksum: u32,
}

pub(super) fn transform_glyf_loca(
    glyf_table: &SerializedTable,
    loca_table: &SerializedTable,
    source_index_format: i16,
    num_glyphs: u16,
) -> Result<Woff2TransformPayload, Error> {
    let glyf = Glyf::read(FontData::new(&glyf_table.bytes))
        .map_err(|_| invalid_data("invalid glyf table"))?;
    let loca = Loca::read(FontData::new(&loca_table.bytes), source_index_format == 1)
        .map_err(|_| invalid_data("invalid loca table"))?;
    if loca.len() != usize::from(num_glyphs) {
        return Err(invalid_data("maxp numGlyphs does not match loca length"));
    }
    if !loca.all_offsets_are_ascending()
        || loca.get_raw(usize::from(num_glyphs)).unwrap_or(u32::MAX) as usize
            > glyf_table.bytes.len()
    {
        return Err(invalid_data(
            "loca offsets must be ascending and within glyf",
        ));
    }

    let mut normalized_glyf = Vec::new();
    let mut offsets = Vec::with_capacity(usize::from(num_glyphs) + 1);
    let mut streams = GlyfStreams {
        bbox_bitmap: vec![0; usize::from(num_glyphs).div_ceil(32) * 4],
        ..Default::default()
    };
    let mut points = Vec::new();
    let mut flags = Vec::new();
    let mut normalized_scratch = NormalizedGlyphScratch::default();
    for glyph_id in 0..num_glyphs {
        offsets.push(normalized_glyf.len());
        let glyph = loca
            .get_glyf(GlyphId::new(u32::from(glyph_id)), &glyf)
            .map_err(|_| invalid_data("invalid glyph record"))?;
        let Some(glyph) = glyph else {
            streams.contours.extend_from_slice(&0_u16.to_be_bytes());
            continue;
        };
        let Glyph::Simple(glyph) = glyph else {
            return Err(invalid_data("composite glyphs are not supported"));
        };
        let contour_count = glyph.number_of_contours();
        if contour_count <= 0 {
            return Err(invalid_data("non-empty zero-contour glyph is malformed"));
        }
        let end_points = glyph.end_pts_of_contours();
        if end_points.len() != contour_count as usize
            || end_points
                .windows(2)
                .any(|pair| pair[0].get() >= pair[1].get())
        {
            return Err(invalid_data("invalid simple glyph contour endpoints"));
        }
        let point_count = glyph.num_points();
        points.resize(point_count, Point::<i32>::default());
        flags.resize(point_count, PointFlags::default());
        glyph
            .read_points_fast(&mut points, &mut flags)
            .map_err(|_| invalid_data("invalid simple glyph coordinates"))?;

        write_normalized_simple_glyph(
            &glyph,
            &points,
            &flags,
            &mut normalized_scratch,
            &mut normalized_glyf,
        )?;
        normalized_glyf.resize((normalized_glyf.len() + 3) & !3, 0);

        streams
            .contours
            .extend_from_slice(&(contour_count as u16).to_be_bytes());
        let mut contour_start = 0_usize;
        for end in end_points {
            let contour_end = usize::from(end.get()) + 1;
            write_255_u16(
                u16::try_from(contour_end - contour_start)
                    .map_err(|_| invalid_data("simple glyph contour is too large"))?,
                &mut streams.points,
            );
            contour_start = contour_end;
        }

        let mut last_x = 0;
        let mut last_y = 0;
        for (point, flag) in points.iter().zip(&flags) {
            write_triplet(
                flag.is_on_curve(),
                point.x - last_x,
                point.y - last_y,
                &mut streams.flags,
                &mut streams.glyphs,
            );
            last_x = point.x;
            last_y = point.y;
        }
        write_255_u16(glyph.instruction_length(), &mut streams.glyphs);
        streams.instructions.extend_from_slice(glyph.instructions());

        if glyph.has_overlapping_contours() {
            if streams.overlap_bitmap.is_empty() {
                streams
                    .overlap_bitmap
                    .resize(usize::from(num_glyphs).div_ceil(8), 0);
            }
            set_bitmap_bit(&mut streams.overlap_bitmap, usize::from(glyph_id));
        }
        let computed_bbox = points.iter().fold(
            (points[0].x, points[0].y, points[0].x, points[0].y),
            |(x_min, y_min, x_max, y_max), point| {
                (
                    x_min.min(point.x),
                    y_min.min(point.y),
                    x_max.max(point.x),
                    y_max.max(point.y),
                )
            },
        );
        if computed_bbox
            != (
                i32::from(glyph.x_min()),
                i32::from(glyph.y_min()),
                i32::from(glyph.x_max()),
                i32::from(glyph.y_max()),
            )
        {
            set_bitmap_bit(&mut streams.bbox_bitmap, usize::from(glyph_id));
            let mut writer = BigEndian::new(&mut streams.bboxes);
            for value in [glyph.x_min(), glyph.y_min(), glyph.x_max(), glyph.y_max()] {
                writer.push_i16(value);
            }
        }
    }
    offsets.push(normalized_glyf.len());
    let loca_format =
        i16::from(source_index_format == 1 || normalized_glyf.len() / 2 > usize::from(u16::MAX));
    let mut normalized_loca =
        Vec::with_capacity(offsets.len() * if loca_format == 0 { 2 } else { 4 });
    let mut writer = BigEndian::new(&mut normalized_loca);
    for offset in offsets {
        if loca_format == 0 {
            writer.push_u16((offset / 2) as u16);
        } else {
            writer.push_u32(
                u32::try_from(offset)
                    .map_err(|_| invalid_data("normalized glyf size exceeds u32"))?,
            );
        }
    }
    Ok(Woff2TransformPayload {
        transformed: finish_glyf_transform(streams, num_glyphs, loca_format)?,
        normalized_glyf_len: normalized_glyf.len(),
        normalized_glyf_checksum: compute_checksum(&normalized_glyf),
        normalized_loca_format: loca_format,
        normalized_loca_len: normalized_loca.len(),
        normalized_loca_checksum: compute_checksum(&normalized_loca),
    })
}

#[derive(Default)]
struct NormalizedGlyphScratch {
    flags: Vec<u8>,
    x_coordinates: Vec<u8>,
    y_coordinates: Vec<u8>,
}

fn write_normalized_simple_glyph(
    glyph: &SimpleGlyph<'_>,
    points: &[Point<i32>],
    source_flags: &[PointFlags],
    scratch: &mut NormalizedGlyphScratch,
    output: &mut Vec<u8>,
) -> Result<(), Error> {
    let contour_count = glyph.number_of_contours();
    if contour_count <= 0 {
        return Err(invalid_data("non-empty zero-contour glyph is malformed"));
    }
    {
        let mut writer = BigEndian::new(&mut *output);
        writer.push_i16(contour_count);
        for value in [glyph.x_min(), glyph.y_min(), glyph.x_max(), glyph.y_max()] {
            writer.push_i16(value);
        }
        for end_point in glyph.end_pts_of_contours() {
            writer.push_u16(end_point.get());
        }
        writer.push_u16(glyph.instruction_length());
    }
    output.extend_from_slice(glyph.instructions());

    scratch.flags.clear();
    scratch.flags.reserve(points.len());
    scratch.x_coordinates.clear();
    scratch.y_coordinates.clear();
    let mut last_x = 0_i32;
    let mut last_y = 0_i32;
    for (index, (point, source_flag)) in points.iter().zip(source_flags).enumerate() {
        let dx = point.x - last_x;
        let dy = point.y - last_y;
        let mut flag = u8::from(source_flag.is_on_curve());
        if index == 0 && glyph.has_overlapping_contours() {
            flag |= 1 << 6;
        }
        write_normalized_coordinate(dx, 1 << 1, 1 << 4, &mut flag, &mut scratch.x_coordinates)?;
        write_normalized_coordinate(dy, 1 << 2, 1 << 5, &mut flag, &mut scratch.y_coordinates)?;
        scratch.flags.push(flag);
        last_x = point.x;
        last_y = point.y;
    }
    let mut index = 0;
    while index < scratch.flags.len() {
        let flag = scratch.flags[index];
        let run_len = scratch.flags[index..]
            .iter()
            .take(256)
            .take_while(|next| **next == flag)
            .count();
        output.push(flag | u8::from(run_len > 1) << 3);
        if run_len > 1 {
            output.push((run_len - 1) as u8);
        }
        index += run_len;
    }
    output.extend_from_slice(&scratch.x_coordinates);
    output.extend_from_slice(&scratch.y_coordinates);
    Ok(())
}

fn write_normalized_coordinate(
    delta: i32,
    short_bit: u8,
    same_or_positive_bit: u8,
    flag: &mut u8,
    output: &mut Vec<u8>,
) -> Result<(), Error> {
    if delta == 0 {
        *flag |= same_or_positive_bit;
    } else if (-255..=255).contains(&delta) {
        *flag |= short_bit;
        if delta > 0 {
            *flag |= same_or_positive_bit;
        }
        output.push(delta.unsigned_abs() as u8);
    } else {
        BigEndian::new(output).push_i16(
            i16::try_from(delta)
                .map_err(|_| invalid_data("simple glyph coordinate delta exceeds i16"))?,
        );
    }
    Ok(())
}

#[derive(Default)]
struct GlyfStreams {
    contours: Vec<u8>,
    points: Vec<u8>,
    flags: Vec<u8>,
    glyphs: Vec<u8>,
    composites: Vec<u8>,
    bbox_bitmap: Vec<u8>,
    bboxes: Vec<u8>,
    instructions: Vec<u8>,
    overlap_bitmap: Vec<u8>,
}

fn finish_glyf_transform(
    streams: GlyfStreams,
    num_glyphs: u16,
    transformed_index_format: i16,
) -> Result<Vec<u8>, Error> {
    let bbox_len = streams
        .bbox_bitmap
        .len()
        .checked_add(streams.bboxes.len())
        .ok_or_else(|| invalid_data("transformed glyf size overflow"))?;
    let lengths = [
        streams.contours.len(),
        streams.points.len(),
        streams.flags.len(),
        streams.glyphs.len(),
        streams.composites.len(),
        bbox_len,
        streams.instructions.len(),
    ];
    let payload_len = lengths
        .iter()
        .try_fold(GLYF_TRANSFORM_HEADER_SIZE, |size, length| {
            size.checked_add(*length)
        })
        .and_then(|size| size.checked_add(streams.overlap_bitmap.len()))
        .ok_or_else(|| invalid_data("transformed glyf size overflow"))?;
    let mut output = Vec::with_capacity(payload_len);
    {
        let mut writer = BigEndian::new(&mut output);
        writer.push_u16(0);
        writer.push_u16(u16::from(!streams.overlap_bitmap.is_empty()));
        writer.push_u16(num_glyphs);
        writer.push_i16(transformed_index_format);
        for length in lengths {
            writer.push_u32(
                u32::try_from(length)
                    .map_err(|_| invalid_data("transformed glyf stream exceeds u32"))?,
            );
        }
    }
    output.extend_from_slice(&streams.contours);
    output.extend_from_slice(&streams.points);
    output.extend_from_slice(&streams.flags);
    output.extend_from_slice(&streams.glyphs);
    output.extend_from_slice(&streams.composites);
    output.extend_from_slice(&streams.bbox_bitmap);
    output.extend_from_slice(&streams.bboxes);
    output.extend_from_slice(&streams.instructions);
    output.extend_from_slice(&streams.overlap_bitmap);
    Ok(output)
}

fn set_bitmap_bit(bitmap: &mut [u8], glyph_id: usize) {
    bitmap[glyph_id >> 3] |= 0x80 >> (glyph_id & 7);
}

pub(super) fn write_255_u16(value: u16, output: &mut Vec<u8>) {
    match value {
        0..=252 => output.push(value as u8),
        253..=505 => output.extend_from_slice(&[255, (value - 253) as u8]),
        506..=761 => output.extend_from_slice(&[254, (value - 506) as u8]),
        _ => {
            output.push(253);
            output.extend_from_slice(&value.to_be_bytes());
        }
    }
}

pub(super) fn write_triplet(
    on_curve: bool,
    x: i32,
    y: i32,
    flags: &mut Vec<u8>,
    glyphs: &mut Vec<u8>,
) {
    let abs_x = x.unsigned_abs();
    let abs_y = y.unsigned_abs();
    let on_curve_bit = if on_curve { 0 } else { 128 };
    let x_sign_bit = u8::from(x >= 0);
    let y_sign_bit = u8::from(y >= 0);
    let signs = x_sign_bit + 2 * y_sign_bit;
    if x == 0 && abs_y < 1280 {
        flags.push(on_curve_bit + ((abs_y & 0xf00) >> 7) as u8 + y_sign_bit);
        glyphs.push(abs_y as u8);
    } else if y == 0 && abs_x < 1280 {
        flags.push(on_curve_bit + 10 + ((abs_x & 0xf00) >> 7) as u8 + x_sign_bit);
        glyphs.push(abs_x as u8);
    } else if abs_x < 65 && abs_y < 65 {
        flags.push(
            on_curve_bit
                + 20
                + ((abs_x.wrapping_sub(1) & 0x30) as u8)
                + (((abs_y.wrapping_sub(1) & 0x30) >> 2) as u8)
                + signs,
        );
        glyphs.push((((abs_x.wrapping_sub(1) & 0xf) << 4) | (abs_y.wrapping_sub(1) & 0xf)) as u8);
    } else if abs_x < 769 && abs_y < 769 {
        flags.push(
            on_curve_bit
                + 84
                + (12 * ((abs_x.wrapping_sub(1) & 0x300) >> 8)) as u8
                + ((abs_y.wrapping_sub(1) & 0x300) >> 6) as u8
                + signs,
        );
        glyphs.extend_from_slice(&[abs_x.wrapping_sub(1) as u8, abs_y.wrapping_sub(1) as u8]);
    } else if abs_x < 4096 && abs_y < 4096 {
        flags.push(on_curve_bit + 120 + signs);
        glyphs.extend_from_slice(&[
            (abs_x >> 4) as u8,
            (((abs_x & 0xf) << 4) | (abs_y >> 8)) as u8,
            abs_y as u8,
        ]);
    } else {
        flags.push(on_curve_bit + 124 + signs);
        glyphs.extend_from_slice(&[
            (abs_x >> 8) as u8,
            abs_x as u8,
            (abs_y >> 8) as u8,
            abs_y as u8,
        ]);
    }
}

pub(super) fn transform_cache_key(tag: [u8; 4], transform_version: u8, inputs: &[&[u8]]) -> u64 {
    let mut hasher = FxHasher::default();
    hasher.write(&tag);
    hasher.write_u8(transform_version);
    for input in inputs {
        hasher.write(input);
    }
    hasher.finish()
}

pub(super) fn invalid_data(message: &'static str) -> Error {
    Error::new(ErrorKind::InvalidData, message)
}