raden 2026.1.1

2D Vector Graphics Library
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/// TrueType テーブルパーサ。
///
/// sfnt ヘッダからテーブルディレクトリをパースし、
/// head, maxp, hhea, hmtx, loca, cmap, glyf の各テーブルを読み出す。
use crate::font::FontError;

// ---------------------------------------------------------------------------
// バイト読み取りヘルパー
// ---------------------------------------------------------------------------

fn read_u16(data: &[u8], offset: usize) -> Result<u16, FontError> {
    let end = offset + 2;
    if end > data.len() {
        return Err(FontError::InvalidData("unexpected end of data (u16)"));
    }
    Ok(u16::from_be_bytes([data[offset], data[offset + 1]]))
}

fn read_i16(data: &[u8], offset: usize) -> Result<i16, FontError> {
    let end = offset + 2;
    if end > data.len() {
        return Err(FontError::InvalidData("unexpected end of data (i16)"));
    }
    Ok(i16::from_be_bytes([data[offset], data[offset + 1]]))
}

fn read_u32(data: &[u8], offset: usize) -> Result<u32, FontError> {
    let end = offset + 4;
    if end > data.len() {
        return Err(FontError::InvalidData("unexpected end of data (u32)"));
    }
    Ok(u32::from_be_bytes([
        data[offset],
        data[offset + 1],
        data[offset + 2],
        data[offset + 3],
    ]))
}

// ---------------------------------------------------------------------------
// テーブルディレクトリ
// ---------------------------------------------------------------------------

/// テーブルの位置情報。
#[derive(Debug, Clone, Copy)]
struct TableRecord {
    offset: u32,
    length: u32,
}

/// テーブルディレクトリ。tag → (offset, length) のマッピング。
struct TableDirectory {
    records: Vec<(u32, TableRecord)>,
}

impl TableDirectory {
    fn parse(data: &[u8], start: usize) -> Result<Self, FontError> {
        if data.len() < start + 12 {
            return Err(FontError::InvalidData("data too short for sfnt header"));
        }

        let sfnt_version = read_u32(data, start)?;
        // TrueType: 0x00010000, OpenType with TrueType outlines: 0x00010000
        // 'true' (0x74727565) も許容 (Apple)
        if sfnt_version != 0x0001_0000 && sfnt_version != 0x7472_7565 {
            return Err(FontError::InvalidData("unsupported sfnt version"));
        }

        let num_tables = read_u16(data, start + 4)? as usize;
        let header_end = start + 12 + num_tables * 16;
        if data.len() < header_end {
            return Err(FontError::InvalidData("data too short for table directory"));
        }

        let mut records = Vec::with_capacity(num_tables);
        for i in 0..num_tables {
            let base = start + 12 + i * 16;
            let tag = read_u32(data, base)?;
            let offset = read_u32(data, base + 8)?;
            let length = read_u32(data, base + 12)?;

            // テーブル範囲が実データに収まるか検証
            let end = offset as u64 + length as u64;
            if end > data.len() as u64 {
                return Err(FontError::InvalidData("table record exceeds data length"));
            }

            records.push((tag, TableRecord { offset, length }));
        }

        Ok(Self { records })
    }

    fn find(&self, tag: u32) -> Option<TableRecord> {
        self.records
            .iter()
            .find(|(t, _)| *t == tag)
            .map(|(_, r)| *r)
    }

    fn require(&self, tag: u32) -> Result<TableRecord, FontError> {
        self.find(tag)
            .ok_or(FontError::InvalidData("required table not found"))
    }
}

/// 4 文字の ASCII タグを u32 に変換する。
const fn tag(b: &[u8; 4]) -> u32 {
    ((b[0] as u32) << 24) | ((b[1] as u32) << 16) | ((b[2] as u32) << 8) | (b[3] as u32)
}

const TAG_HEAD: u32 = tag(b"head");
const TAG_MAXP: u32 = tag(b"maxp");
const TAG_HHEA: u32 = tag(b"hhea");
const TAG_HMTX: u32 = tag(b"hmtx");
const TAG_LOCA: u32 = tag(b"loca");
const TAG_CMAP: u32 = tag(b"cmap");
const TAG_GLYF: u32 = tag(b"glyf");

// ---------------------------------------------------------------------------
// head テーブル
// ---------------------------------------------------------------------------

pub(crate) struct HeadTable {
    pub units_per_em: u16,
    pub index_to_loc_format: i16,
}

fn parse_head(data: &[u8], rec: TableRecord) -> Result<HeadTable, FontError> {
    let off = rec.offset as usize;
    if rec.length < 54 {
        return Err(FontError::InvalidData("head table too short"));
    }
    let units_per_em = read_u16(data, off + 18)?;
    if units_per_em == 0 {
        return Err(FontError::InvalidData("units_per_em is zero"));
    }
    let index_to_loc_format = read_i16(data, off + 50)?;
    Ok(HeadTable {
        units_per_em,
        index_to_loc_format,
    })
}

// ---------------------------------------------------------------------------
// maxp テーブル
// ---------------------------------------------------------------------------

fn parse_maxp(data: &[u8], rec: TableRecord) -> Result<u16, FontError> {
    let off = rec.offset as usize;
    if rec.length < 6 {
        return Err(FontError::InvalidData("maxp table too short"));
    }
    read_u16(data, off + 4)
}

// ---------------------------------------------------------------------------
// hhea テーブル
// ---------------------------------------------------------------------------

pub(crate) struct HheaTable {
    pub ascent: i16,
    pub descent: i16,
    pub line_gap: i16,
    pub number_of_h_metrics: u16,
}

fn parse_hhea(data: &[u8], rec: TableRecord) -> Result<HheaTable, FontError> {
    let off = rec.offset as usize;
    if rec.length < 36 {
        return Err(FontError::InvalidData("hhea table too short"));
    }
    let ascent = read_i16(data, off + 4)?;
    let descent = read_i16(data, off + 6)?;
    let line_gap = read_i16(data, off + 8)?;
    let number_of_h_metrics = read_u16(data, off + 34)?;
    Ok(HheaTable {
        ascent,
        descent,
        line_gap,
        number_of_h_metrics,
    })
}

// ---------------------------------------------------------------------------
// hmtx テーブル
// ---------------------------------------------------------------------------

#[derive(Clone)]
pub(crate) struct HmtxTable {
    /// advance_width[i] (i < number_of_h_metrics のときは各グリフ固有、
    /// i >= number_of_h_metrics のときは最後の値を使う)。
    pub advance_widths: Vec<u16>,
    #[allow(dead_code)]
    pub lsbs: Vec<i16>,
}

fn parse_hmtx(
    data: &[u8],
    rec: TableRecord,
    num_h_metrics: u16,
    num_glyphs: u16,
) -> Result<HmtxTable, FontError> {
    let off = rec.offset as usize;
    let nh = num_h_metrics as usize;
    let ng = num_glyphs as usize;

    // longHorMetric[nh] + leftSideBearing[ng - nh]
    let min_len = nh * 4 + (ng.saturating_sub(nh)) * 2;
    if (rec.length as usize) < min_len {
        return Err(FontError::InvalidData("hmtx table too short"));
    }

    let mut advance_widths = Vec::with_capacity(ng);
    let mut lsbs = Vec::with_capacity(ng);

    for i in 0..nh {
        let base = off + i * 4;
        advance_widths.push(read_u16(data, base)?);
        lsbs.push(read_i16(data, base + 2)?);
    }

    // 残りのグリフは最後の advance_width を継承
    let last_aw = advance_widths.last().copied().unwrap_or(0);
    let extra_base = off + nh * 4;
    for i in 0..(ng - nh) {
        advance_widths.push(last_aw);
        lsbs.push(read_i16(data, extra_base + i * 2)?);
    }

    Ok(HmtxTable {
        advance_widths,
        lsbs,
    })
}

// ---------------------------------------------------------------------------
// loca テーブル
// ---------------------------------------------------------------------------

fn parse_loca(
    data: &[u8],
    rec: TableRecord,
    num_glyphs: u16,
    index_to_loc_format: i16,
) -> Result<Vec<u32>, FontError> {
    let off = rec.offset as usize;
    let n = num_glyphs as usize + 1; // loca は num_glyphs + 1 エントリ

    let offsets = if index_to_loc_format == 0 {
        // short format: u16 * 2
        let min_len = n * 2;
        if (rec.length as usize) < min_len {
            return Err(FontError::InvalidData("loca table too short (short)"));
        }
        (0..n)
            .map(|i| read_u16(data, off + i * 2).map(|v| v as u32 * 2))
            .collect::<Result<Vec<_>, _>>()?
    } else {
        // long format: u32
        let min_len = n * 4;
        if (rec.length as usize) < min_len {
            return Err(FontError::InvalidData("loca table too short (long)"));
        }
        (0..n)
            .map(|i| read_u32(data, off + i * 4))
            .collect::<Result<Vec<_>, _>>()?
    };

    Ok(offsets)
}

// ---------------------------------------------------------------------------
// cmap テーブル
// ---------------------------------------------------------------------------

/// Unicode → GlyphID ルックアップ。
#[derive(Clone)]
pub(crate) enum CmapLookup {
    Format4(CmapFormat4),
    Format12(CmapFormat12),
}

#[derive(Clone)]
pub(crate) struct CmapFormat4 {
    segments: Vec<CmapFormat4Segment>,
    /// glyphIdArray 全体 (id_range_offset で参照する)。
    glyph_id_array: Vec<u16>,
    /// glyphIdArray のバイトオフセット (id_range_offset の基点計算用)。
    glyph_id_array_offset: usize,
    /// idRangeOffset 配列のバイトオフセット。
    id_range_offset_base: usize,
}

#[derive(Clone)]
struct CmapFormat4Segment {
    end_code: u16,
    start_code: u16,
    id_delta: i16,
    id_range_offset: u16,
}

#[derive(Clone)]
pub(crate) struct CmapFormat12 {
    groups: Vec<CmapFormat12Group>,
}

#[derive(Clone)]
struct CmapFormat12Group {
    start_char: u32,
    end_char: u32,
    start_glyph_id: u32,
}

impl CmapLookup {
    pub fn map(&self, codepoint: u32) -> u16 {
        match self {
            CmapLookup::Format4(f4) => f4.map(codepoint),
            CmapLookup::Format12(f12) => f12.map(codepoint),
        }
    }
}

impl CmapFormat4 {
    fn map(&self, codepoint: u32) -> u16 {
        if codepoint > 0xFFFF {
            return 0;
        }
        let cp = codepoint as u16;

        // 二分探索で該当セグメントを見つける
        let idx = match self.segments.binary_search_by(|seg| seg.end_code.cmp(&cp)) {
            Ok(i) => i,
            Err(i) => {
                if i >= self.segments.len() {
                    return 0;
                }
                i
            }
        };

        let seg = &self.segments[idx];
        if cp < seg.start_code {
            return 0;
        }

        if seg.id_range_offset == 0 {
            // id_delta 方式
            (cp as i32 + seg.id_delta as i32) as u16
        } else {
            // id_range_offset 方式
            // 仕様: glyphId = glyphIdArray[
            //   idRangeOffset[i]/2 + (cp - startCode) + &idRangeOffset[i] - glyphIdArrayStart
            // ]
            // 実装: idRangeOffset のバイト位置からの相対オフセットで計算
            let seg_byte_offset = self.id_range_offset_base + idx * 2;
            let target_byte_offset =
                seg_byte_offset + seg.id_range_offset as usize + (cp - seg.start_code) as usize * 2;
            let array_index = (target_byte_offset - self.glyph_id_array_offset) / 2;

            if array_index >= self.glyph_id_array.len() {
                return 0;
            }

            let glyph_id = self.glyph_id_array[array_index];
            if glyph_id == 0 {
                0
            } else {
                (glyph_id as i32 + seg.id_delta as i32) as u16
            }
        }
    }
}

impl CmapFormat12 {
    fn map(&self, codepoint: u32) -> u16 {
        // 二分探索
        let idx = match self
            .groups
            .binary_search_by(|g| g.start_char.cmp(&codepoint))
        {
            Ok(i) => i,
            Err(0) => return 0,
            Err(i) => i - 1,
        };

        let g = &self.groups[idx];
        if codepoint > g.end_char {
            return 0;
        }

        let glyph_id = g.start_glyph_id + (codepoint - g.start_char);
        glyph_id as u16
    }
}

fn parse_cmap(data: &[u8], rec: TableRecord) -> Result<CmapLookup, FontError> {
    let off = rec.offset as usize;
    if rec.length < 4 {
        return Err(FontError::InvalidData("cmap table too short"));
    }

    let num_tables = read_u16(data, off + 2)? as usize;
    if rec.length < (4 + num_tables * 8) as u32 {
        return Err(FontError::InvalidData(
            "cmap table too short for encoding records",
        ));
    }

    // 最適なサブテーブルを選択する
    // 優先: platformID=3 encodingID=10 (UCS-4) > platformID=3 encodingID=1 (UCS-2)
    //       platformID=0 encodingID=3..6 も許容
    let mut best_offset: Option<u32> = None;
    let mut best_priority = 0u8;

    for i in 0..num_tables {
        let base = off + 4 + i * 8;
        let platform_id = read_u16(data, base)?;
        let encoding_id = read_u16(data, base + 2)?;
        let subtable_offset = read_u32(data, base + 4)?;

        let priority = match (platform_id, encoding_id) {
            (3, 10) => 4,    // Windows UCS-4
            (0, 4..=6) => 3, // Unicode full
            (3, 1) => 2,     // Windows UCS-2
            (0, 3) => 1,     // Unicode BMP
            _ => 0,
        };

        if priority > best_priority {
            best_priority = priority;
            best_offset = Some(subtable_offset);
        }
    }

    let subtable_off =
        off + best_offset.ok_or(FontError::InvalidData("no suitable cmap subtable"))? as usize;

    if subtable_off + 2 > data.len() {
        return Err(FontError::InvalidData("cmap subtable offset out of range"));
    }

    let format = read_u16(data, subtable_off)?;

    match format {
        4 => parse_cmap_format4(data, subtable_off),
        12 => parse_cmap_format12(data, subtable_off),
        _ => Err(FontError::InvalidData("unsupported cmap format")),
    }
}

fn parse_cmap_format4(data: &[u8], off: usize) -> Result<CmapLookup, FontError> {
    if off + 14 > data.len() {
        return Err(FontError::InvalidData("cmap format 4 header too short"));
    }
    let length = read_u16(data, off + 2)? as usize;
    if off + length > data.len() {
        return Err(FontError::InvalidData("cmap format 4 length exceeds data"));
    }

    let seg_count_x2 = read_u16(data, off + 6)? as usize;
    let seg_count = seg_count_x2 / 2;

    // 各配列のオフセット
    let end_codes_off = off + 14;
    // reservedPad: 2 bytes after end_codes
    let start_codes_off = end_codes_off + seg_count * 2 + 2;
    let id_deltas_off = start_codes_off + seg_count * 2;
    let id_range_offsets_off = id_deltas_off + seg_count * 2;
    let glyph_id_array_off = id_range_offsets_off + seg_count * 2;

    let table_end = off + length;
    if glyph_id_array_off > table_end {
        return Err(FontError::InvalidData(
            "cmap format 4 arrays exceed table length",
        ));
    }

    let mut segments = Vec::with_capacity(seg_count);
    for i in 0..seg_count {
        let end_code = read_u16(data, end_codes_off + i * 2)?;
        let start_code = read_u16(data, start_codes_off + i * 2)?;
        let id_delta = read_i16(data, id_deltas_off + i * 2)?;
        let id_range_offset = read_u16(data, id_range_offsets_off + i * 2)?;

        segments.push(CmapFormat4Segment {
            end_code,
            start_code,
            id_delta,
            id_range_offset,
        });
    }

    // glyphIdArray
    let glyph_id_count = (table_end - glyph_id_array_off) / 2;
    let mut glyph_id_array = Vec::with_capacity(glyph_id_count);
    for i in 0..glyph_id_count {
        glyph_id_array.push(read_u16(data, glyph_id_array_off + i * 2)?);
    }

    Ok(CmapLookup::Format4(CmapFormat4 {
        segments,
        glyph_id_array,
        glyph_id_array_offset: glyph_id_array_off,
        id_range_offset_base: id_range_offsets_off,
    }))
}

fn parse_cmap_format12(data: &[u8], off: usize) -> Result<CmapLookup, FontError> {
    // format 12 は fixed32 format (先頭 u16=12, 次の u16=0, 次 u32=length)
    if off + 16 > data.len() {
        return Err(FontError::InvalidData("cmap format 12 header too short"));
    }
    let num_groups = read_u32(data, off + 12)? as usize;
    let groups_off = off + 16;

    if groups_off + num_groups * 12 > data.len() {
        return Err(FontError::InvalidData("cmap format 12 groups exceed data"));
    }

    let mut groups = Vec::with_capacity(num_groups);
    for i in 0..num_groups {
        let base = groups_off + i * 12;
        let start_char = read_u32(data, base)?;
        let end_char = read_u32(data, base + 4)?;
        let start_glyph_id = read_u32(data, base + 8)?;
        groups.push(CmapFormat12Group {
            start_char,
            end_char,
            start_glyph_id,
        });
    }

    Ok(CmapLookup::Format12(CmapFormat12 { groups }))
}

// ---------------------------------------------------------------------------
// パース済みテーブル集合
// ---------------------------------------------------------------------------

/// フォントファイルからパースした全テーブル情報。
#[derive(Clone)]
pub(crate) struct ParsedTables {
    pub units_per_em: u16,
    #[allow(dead_code)]
    pub num_glyphs: u16,
    pub ascent: i16,
    pub descent: i16,
    pub line_gap: i16,
    pub loca_offsets: Vec<u32>,
    pub glyf_offset: u32,
    #[allow(dead_code)]
    pub glyf_length: u32,
    pub cmap: CmapLookup,
    pub hmtx: HmtxTable,
}

/// TTC タグ 'ttcf'。
const TAG_TTCF: u32 = tag(b"ttcf");

/// TTC ファイルから指定インデックスのフォントオフセットを取得する。
fn ttc_font_offset(data: &[u8], index: u32) -> Result<usize, FontError> {
    if data.len() < 12 {
        return Err(FontError::InvalidData("TTC header too short"));
    }
    let num_fonts = read_u32(data, 8)?;
    if index >= num_fonts {
        return Err(FontError::InvalidData("TTC font index out of range"));
    }
    let offset_pos = 12 + index as usize * 4;
    if offset_pos + 4 > data.len() {
        return Err(FontError::InvalidData("TTC offset table too short"));
    }
    let offset = read_u32(data, offset_pos)? as usize;
    Ok(offset)
}

/// フォントデータの全テーブルをパースする。
///
/// `index` は TTC (TrueType Collection) 内のフォントインデックス。
/// 単体フォントの場合は 0 を指定する。
pub(crate) fn parse_all(data: &[u8], index: u32) -> Result<ParsedTables, FontError> {
    // TTC ヘッダを検出し、指定インデックスのフォントオフセットを取得する
    let sfnt_start = if data.len() >= 4 && read_u32(data, 0)? == TAG_TTCF {
        ttc_font_offset(data, index)?
    } else {
        0
    };

    let dir = TableDirectory::parse(data, sfnt_start)?;

    let head_rec = dir.require(TAG_HEAD)?;
    let maxp_rec = dir.require(TAG_MAXP)?;
    let hhea_rec = dir.require(TAG_HHEA)?;
    let hmtx_rec = dir.require(TAG_HMTX)?;
    let loca_rec = dir.require(TAG_LOCA)?;
    let cmap_rec = dir.require(TAG_CMAP)?;
    let glyf_rec = dir.require(TAG_GLYF)?;

    let head = parse_head(data, head_rec)?;
    let num_glyphs = parse_maxp(data, maxp_rec)?;
    let hhea = parse_hhea(data, hhea_rec)?;
    let hmtx = parse_hmtx(data, hmtx_rec, hhea.number_of_h_metrics, num_glyphs)?;
    let loca_offsets = parse_loca(data, loca_rec, num_glyphs, head.index_to_loc_format)?;
    let cmap = parse_cmap(data, cmap_rec)?;

    Ok(ParsedTables {
        units_per_em: head.units_per_em,
        num_glyphs,
        ascent: hhea.ascent,
        descent: hhea.descent,
        line_gap: hhea.line_gap,
        loca_offsets,
        glyf_offset: glyf_rec.offset,
        glyf_length: glyf_rec.length,
        cmap,
        hmtx,
    })
}

// ---------------------------------------------------------------------------
// 公開ヘルパー (glyph.rs から利用)
// ---------------------------------------------------------------------------

/// バイト列から u16 (Big Endian) を読み取る。
pub(crate) fn be_u16(data: &[u8], offset: usize) -> Result<u16, FontError> {
    read_u16(data, offset)
}

/// バイト列から i16 (Big Endian) を読み取る。
pub(crate) fn be_i16(data: &[u8], offset: usize) -> Result<i16, FontError> {
    read_i16(data, offset)
}

/// バイト列から u8 を読み取る。
pub(crate) fn be_u8(data: &[u8], offset: usize) -> Result<u8, FontError> {
    if offset >= data.len() {
        return Err(FontError::InvalidData("unexpected end of data (u8)"));
    }
    Ok(data[offset])
}

/// バイト列から i8 を読み取る。
pub(crate) fn be_i8(data: &[u8], offset: usize) -> Result<i8, FontError> {
    if offset >= data.len() {
        return Err(FontError::InvalidData("unexpected end of data (i8)"));
    }
    Ok(data[offset] as i8)
}