harumi 1.2.0

Pure-Rust PDF — CJK font embedding (Chinese/Japanese/Korean), OCR text overlay, text extraction, HTML→PDF, page merge/split. WASM-ready, zero C deps.
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Internal pure-Rust TTF font subsetter.
//!
//! Replaces the `subsetter` crate to minimize dependencies.
//! Handles TrueType (glyf-based) fonts; CFF fonts are rejected as unsupported.

use std::collections::{BTreeMap, BTreeSet};
use ttf_parser::Face;

/// Tracks which glyphs (by original GID) should be preserved in the subset.
/// Provides GID remapping: original GID → new GID (0..N after subsetting).
pub(super) struct GlyphRemapper {
    orig_gids: BTreeSet<u16>,
}

impl GlyphRemapper {
    /// Create a new empty remapper.
    pub fn new() -> Self {
        GlyphRemapper {
            orig_gids: BTreeSet::new(),
        }
    }

    /// Mark a glyph (by original GID) to be preserved in the subset.
    pub fn remap(&mut self, gid: u16) {
        self.orig_gids.insert(gid);
    }

    /// Get the new GID for an original GID.
    /// Returns None if the glyph is not in the subset.
    pub fn get(&self, orig: u16) -> Option<u32> {
        let sorted: Vec<_> = self.orig_gids.iter().copied().collect();
        sorted.iter().position(|&g| g == orig).map(|i| i as u32)
    }

    /// Get sorted list of original GIDs to preserve (for internal use).
    fn sorted_gids(&self) -> Vec<u16> {
        self.orig_gids.iter().copied().collect()
    }
}

/// Subset a TrueType font to include only the specified glyphs.
///
/// # Errors
/// Returns an error if the font is malformed, CFF-based, or subsetting fails.
pub(super) fn subset(
    data: &[u8],
    face_index: u32,
    remapper: &GlyphRemapper,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    let face = Face::parse(data, face_index)?;

    // For TTC (TrueType Collection), find the font data at the specified face_index.
    let is_ttc = data.len() >= 4 && &data[0..4] == b"ttcf";
    let font_data_start = if is_ttc {
        // TTC format: read face offset from TTC header.
        if data.len() < 12 + (face_index as usize + 1) * 4 {
            return Err("TTC header truncated".into());
        }
        u32::from_be_bytes([
            data[12 + face_index as usize * 4],
            data[12 + face_index as usize * 4 + 1],
            data[12 + face_index as usize * 4 + 2],
            data[12 + face_index as usize * 4 + 3],
        ]) as usize
    } else {
        0
    };

    // Parse the font structure using the font data offset.
    // For TTC: font_data is the face data, table offsets are absolute from TTC file start.
    // For TTF: font_data is the same as data, table offsets are from file start.
    let font_data = &data[font_data_start..];
    let offset_table = parse_offset_table(font_data)?;

    // Parse table records with raw offsets (not yet validated as slices).
    let table_recs_raw = parse_table_records_raw(font_data, &offset_table)?;

    // For TTC, offsets in the table directory are absolute from the TTC file start (offset 0 of full data).
    // When we read from font_data, the offsets are still absolute (they're just numbers in the directory).
    // For TTF, font_data_start == 0, so offsets are naturally correct.
    let table_records: Vec<(String, &[u8])> = {
        let mut result = Vec::new();
        for (tag, raw_offset, length) in table_recs_raw {
            // raw_offset is always absolute from the full data start.
            if raw_offset + length <= data.len() {
                result.push((tag, &data[raw_offset..raw_offset + length]));
            }
        }
        result
    };

    // Get required tables from the records.
    let mut tables = std::collections::HashMap::new();
    for (tag, slice) in table_records.iter() {
        tables.insert(tag.as_str(), *slice);
    }

    let head = *tables.get("head").ok_or("required table head not found")?;
    let hhea = *tables.get("hhea").ok_or("required table hhea not found")?;
    let maxp = *tables.get("maxp").ok_or("required table maxp not found")?;
    let glyf = *tables.get("glyf").ok_or("required table glyf not found")?;
    let loca = *tables.get("loca").ok_or("required table loca not found")?;
    let hmtx = *tables.get("hmtx").ok_or("required table hmtx not found")?;

    // Read head to determine loca format.
    let index_to_loc_format = if head.len() >= 52 {
        u16::from_be_bytes([head[50], head[51]]) & 0x0001
    } else {
        return Err("head table too short".into());
    };

    // Read number of metrics from hhea.
    let num_h_metrics = if hhea.len() >= 34 {
        u16::from_be_bytes([hhea[34], hhea[35]]) as usize
    } else {
        return Err("hhea table too short".into());
    };

    // Read numGlyphs from maxp.
    let num_glyphs = if maxp.len() >= 4 {
        u16::from_be_bytes([maxp[4], maxp[5]]) as usize
    } else {
        return Err("maxp table too short".into());
    };

    // Parse loca to get glyph offsets.
    let glyph_offsets = parse_loca(loca, num_glyphs, index_to_loc_format as u8)?;

    // Collect all glyphs to preserve (including composite dependencies).
    let sorted_gids = remapper.sorted_gids();
    let mut gids_to_keep = BTreeSet::new();
    for &gid in &sorted_gids {
        gids_to_keep.insert(gid);
        collect_composite_deps(glyf, &glyph_offsets, gid as usize, &mut gids_to_keep)?;
    }

    // Build new glyf and loca tables.
    let (new_glyf, new_glyph_offsets) = build_glyf(&glyph_offsets, glyf, &gids_to_keep)?;
    let new_loca = build_loca(&new_glyph_offsets, index_to_loc_format as u8)?;

    // Build new hmtx table.
    let new_hmtx = build_hmtx(
        hmtx,
        num_h_metrics,
        face.units_per_em() as usize,
        &gids_to_keep,
    )?;

    // Patch head, hhea, maxp tables with new metrics.
    let mut new_head = head.to_vec();
    let mut new_hhea = hhea.to_vec();
    let mut new_maxp = maxp.to_vec();

    // Update head.indexToLocFormat if needed.
    if new_loca.len() > (num_glyphs + 1) * 2 {
        if new_head.len() > 50 {
            new_head[50] = 1; // long format
        }
    }

    // Update hhea.numberOfHMetrics.
    let num_new_metrics = gids_to_keep.len().min(num_h_metrics);
    if new_hhea.len() >= 36 {
        let bytes = (num_new_metrics as u16).to_be_bytes();
        new_hhea[34..36].copy_from_slice(&bytes);
    }

    // Update maxp.numGlyphs.
    if new_maxp.len() >= 6 {
        let bytes = (gids_to_keep.len() as u16).to_be_bytes();
        new_maxp[4..6].copy_from_slice(&bytes);
    }

    // Collect other tables to preserve (copy as-is).
    let mut output_tables: BTreeMap<String, Vec<u8>> = BTreeMap::new();
    output_tables.insert("head".into(), new_head);
    output_tables.insert("hhea".into(), new_hhea);
    output_tables.insert("maxp".into(), new_maxp);
    output_tables.insert("glyf".into(), new_glyf);
    output_tables.insert("loca".into(), new_loca);
    output_tables.insert("hmtx".into(), new_hmtx);

    // Copy over optional tables (excluding cmap and OS/2 which CIDFont PDF handles).
    for (tag, data_slice) in table_records.iter() {
        if matches!(tag.as_str(), "cmap" | "OS/2" | "VORG") {
            continue; // Skip; not needed for PDF CIDFont embedding.
        }
        if !matches!(
            tag.as_str(),
            "head" | "hhea" | "maxp" | "glyf" | "loca" | "hmtx"
        ) {
            output_tables.insert(tag.clone(), data_slice.to_vec());
        }
    }

    // Assemble the new font binary.
    assemble_font(&output_tables, offset_table.is_truetype)
}

// ──────────────────────────────────────────────────────────────────────────

struct OffsetTable {
    is_truetype: bool,
    num_tables: usize,
}

fn parse_offset_table(data: &[u8]) -> Result<OffsetTable, Box<dyn std::error::Error>> {
    if data.len() < 12 {
        return Err("font too short for offset table".into());
    }

    let scaler = u32::from_be_bytes([data[0], data[1], data[2], data[3]]);
    let is_truetype = scaler == 0x00010000 || scaler == 0x74727565; // 0x00010000 or 'true'

    let num_tables = u16::from_be_bytes([data[4], data[5]]) as usize;
    Ok(OffsetTable {
        is_truetype,
        num_tables,
    })
}

fn parse_table_records_raw(
    data: &[u8],
    offset_table: &OffsetTable,
) -> Result<Vec<(String, usize, usize)>, Box<dyn std::error::Error>> {
    let mut records = Vec::new();
    for i in 0..offset_table.num_tables {
        let base = 12 + i * 16;
        if base + 16 > data.len() {
            return Err("table directory truncated".into());
        }
        let tag = String::from_utf8_lossy(&data[base..base + 4]).to_string();
        let offset = u32::from_be_bytes([
            data[base + 8],
            data[base + 9],
            data[base + 10],
            data[base + 11],
        ]) as usize;
        let length = u32::from_be_bytes([
            data[base + 12],
            data[base + 13],
            data[base + 14],
            data[base + 15],
        ]) as usize;
        records.push((tag, offset, length));
    }
    Ok(records)
}

// ──────────────────────────────────────────────────────────────────────────

fn parse_loca(
    loca: &[u8],
    num_glyphs: usize,
    format: u8,
) -> Result<Vec<u32>, Box<dyn std::error::Error>> {
    let mut offsets = Vec::new();
    if format == 0 {
        // Short format: offsets are u16 × 2
        for i in 0..=num_glyphs {
            if i * 2 + 2 > loca.len() {
                return Err("loca table truncated".into());
            }
            let offset = u16::from_be_bytes([loca[i * 2], loca[i * 2 + 1]]) as u32 * 2;
            offsets.push(offset);
        }
    } else {
        // Long format: offsets are u32
        for i in 0..=num_glyphs {
            if i * 4 + 4 > loca.len() {
                return Err("loca table truncated".into());
            }
            let offset = u32::from_be_bytes([
                loca[i * 4],
                loca[i * 4 + 1],
                loca[i * 4 + 2],
                loca[i * 4 + 3],
            ]);
            offsets.push(offset);
        }
    }
    Ok(offsets)
}

// ──────────────────────────────────────────────────────────────────────────

fn collect_composite_deps(
    glyf: &[u8],
    glyph_offsets: &[u32],
    gid: usize,
    visited: &mut BTreeSet<u16>,
) -> Result<(), Box<dyn std::error::Error>> {
    if gid >= glyph_offsets.len() - 1 {
        return Ok(());
    }

    let start = glyph_offsets[gid] as usize;
    let end = glyph_offsets[gid + 1] as usize;
    if start >= glyf.len() || end > glyf.len() {
        return Ok(()); // Invalid or empty glyph
    }

    let glyph_data = &glyf[start..end];
    if glyph_data.len() < 2 {
        return Ok(());
    }

    let num_contours = i16::from_be_bytes([glyph_data[0], glyph_data[1]]);
    if num_contours >= 0 {
        return Ok(()); // Simple glyph, no components
    }

    // Composite glyph: parse component records.
    let mut offset = 10; // Skip header (xMin, yMin, xMax, yMax)
    while offset < glyph_data.len() {
        if offset + 4 > glyph_data.len() {
            break;
        }

        let flags = u16::from_be_bytes([glyph_data[offset], glyph_data[offset + 1]]);
        let comp_gid = u16::from_be_bytes([glyph_data[offset + 2], glyph_data[offset + 3]]);

        if visited.insert(comp_gid) {
            collect_composite_deps(glyf, glyph_offsets, comp_gid as usize, visited)?;
        }

        offset += 4;

        // Skip arg bytes.
        if (flags & 0x0001) != 0 {
            offset += 4; // Two i16
        } else {
            offset += 2; // Two i8
        }

        // Skip scale/matrix.
        if (flags & 0x0008) != 0 {
            offset += 2; // F2Dot14
        } else if (flags & 0x0040) != 0 {
            offset += 4; // Two F2Dot14
        } else if (flags & 0x0080) != 0 {
            offset += 8; // 2x2 matrix
        }

        if (flags & 0x0020) == 0 {
            break; // No more components
        }
    }

    Ok(())
}

// ──────────────────────────────────────────────────────────────────────────

fn build_glyf(
    glyph_offsets: &[u32],
    glyf: &[u8],
    gids_to_keep: &BTreeSet<u16>,
) -> Result<(Vec<u8>, Vec<u32>), Box<dyn std::error::Error>> {
    let mut new_glyf = Vec::new();
    let mut new_offsets = vec![0u32];

    for gid in gids_to_keep.iter() {
        let gid_usize = *gid as usize;
        if gid_usize + 1 >= glyph_offsets.len() {
            return Err("glyph index out of range".into());
        }
        let start = glyph_offsets[gid_usize] as usize;
        let end = glyph_offsets[gid_usize + 1] as usize;
        if start > glyf.len() || end > glyf.len() {
            return Err("glyph offset out of bounds".into());
        }
        new_glyf.extend_from_slice(&glyf[start..end]);
        new_offsets.push(new_glyf.len() as u32);
    }

    Ok((new_glyf, new_offsets))
}

fn build_loca(glyph_offsets: &[u32], format: u8) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    let mut loca = Vec::new();
    if format == 0 {
        for &offset in glyph_offsets {
            let half = (offset / 2) as u16;
            loca.extend_from_slice(&half.to_be_bytes());
        }
    } else {
        for &offset in glyph_offsets {
            loca.extend_from_slice(&offset.to_be_bytes());
        }
    }
    Ok(loca)
}

fn build_hmtx(
    hmtx: &[u8],
    num_h_metrics: usize,
    units_per_em: usize,
    gids_to_keep: &BTreeSet<u16>,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    let mut new_hmtx = Vec::new();

    for &gid in gids_to_keep.iter() {
        let gid_usize = gid as usize;
        let metrics_offset = if gid_usize < num_h_metrics {
            gid_usize * 4
        } else {
            num_h_metrics * 4 + (gid_usize - num_h_metrics) * 2
        };

        // Read advance width.
        if metrics_offset + 2 <= hmtx.len() {
            new_hmtx.extend_from_slice(&hmtx[metrics_offset..metrics_offset + 2]);
        } else {
            // Default advance.
            new_hmtx.extend_from_slice(&(units_per_em as u16).to_be_bytes());
        }

        // Read left side bearing.
        if metrics_offset + 4 <= hmtx.len() {
            new_hmtx.extend_from_slice(&hmtx[metrics_offset + 2..metrics_offset + 4]);
        } else {
            // Default lsb (0).
            new_hmtx.extend_from_slice(&0i16.to_be_bytes());
        }
    }

    Ok(new_hmtx)
}

// ──────────────────────────────────────────────────────────────────────────

fn assemble_font(
    tables: &BTreeMap<String, Vec<u8>>,
    is_truetype: bool,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    let num_tables = tables.len();
    let search_range: u32 = (1u32 << (31 - (num_tables as u32).leading_zeros())) * 16;
    let entry_selector = (31 - (num_tables as u32).leading_zeros()) as u16;
    let range_shift = (num_tables as u16 * 16) - search_range as u16;

    let mut font = Vec::new();

    // Offset table (12 bytes).
    let scaler = if is_truetype {
        0x00010000u32
    } else {
        0x4F544F54u32
    }; // 0x00010000 or 'OTTO'
    font.extend_from_slice(&scaler.to_be_bytes());
    font.extend_from_slice(&(num_tables as u16).to_be_bytes());
    font.extend_from_slice(&search_range.to_be_bytes());
    font.extend_from_slice(&entry_selector.to_be_bytes());
    font.extend_from_slice(&range_shift.to_be_bytes());

    // Table directory (num_tables × 16 bytes).
    let mut dir_offset = 12 + num_tables * 16;
    let mut table_offsets = Vec::new();
    for (tag, data) in tables.iter() {
        // Align to 4-byte boundary.
        dir_offset = (dir_offset + 3) & !3;
        table_offsets.push((tag.clone(), dir_offset, data.len()));
        dir_offset += data.len();
    }

    for (tag, offset, length) in table_offsets.iter() {
        font.extend_from_slice(tag.as_bytes());
        // Placeholder checksum (0).
        font.extend_from_slice(&0u32.to_be_bytes());
        font.extend_from_slice(&(*offset as u32).to_be_bytes());
        font.extend_from_slice(&(*length as u32).to_be_bytes());
    }

    // Table data.
    let mut current_offset = font.len();
    for (_tag, data) in tables.iter() {
        // Align to 4-byte boundary.
        current_offset = (current_offset + 3) & !3;
        while font.len() < current_offset {
            font.push(0);
        }
        font.extend_from_slice(data);
        current_offset += data.len();
    }

    Ok(font)
}