we (web engine): Experimental web browser project to understand the limits of Claude
1//! `head` — Font Header table.
2//!
3//! Contains global font metrics and flags.
4//! Reference: <https://learn.microsoft.com/en-us/typography/opentype/spec/head>
5
6use crate::font::parse::Reader;
7use crate::font::FontError;
8
9/// Parsed `head` table.
10#[derive(Debug)]
11pub struct HeadTable {
12 /// Major version (should be 1).
13 pub major_version: u16,
14 /// Minor version (should be 0).
15 pub minor_version: u16,
16 /// Font revision (fixed-point 16.16).
17 pub font_revision: i32,
18 /// Units per em (typically 1000 or 2048).
19 pub units_per_em: u16,
20 /// Bounding box: minimum x.
21 pub x_min: i16,
22 /// Bounding box: minimum y.
23 pub y_min: i16,
24 /// Bounding box: maximum x.
25 pub x_max: i16,
26 /// Bounding box: maximum y.
27 pub y_max: i16,
28 /// Mac style flags (bit 0 = bold, bit 1 = italic).
29 pub mac_style: u16,
30 /// Smallest readable size in pixels.
31 pub lowest_rec_ppem: u16,
32 /// 0 = short offsets in loca, 1 = long offsets.
33 pub index_to_loc_format: i16,
34}
35
36impl HeadTable {
37 /// Parse the `head` table from raw bytes.
38 pub fn parse(data: &[u8]) -> Result<HeadTable, FontError> {
39 let r = Reader::new(data);
40 // Minimum head table size is 54 bytes.
41 if r.len() < 54 {
42 return Err(FontError::MalformedTable("head"));
43 }
44
45 let major_version = r.u16(0)?;
46 let minor_version = r.u16(2)?;
47 let font_revision = r.i32(4)?;
48 // skip checksumAdjustment(4) + magicNumber(4) + flags(2)
49 let units_per_em = r.u16(18)?;
50 // skip created(8) + modified(8)
51 let x_min = r.i16(36)?;
52 let y_min = r.i16(38)?;
53 let x_max = r.i16(40)?;
54 let y_max = r.i16(42)?;
55 let mac_style = r.u16(44)?;
56 let lowest_rec_ppem = r.u16(46)?;
57 // skip fontDirectionHint(2)
58 let index_to_loc_format = r.i16(50)?;
59
60 Ok(HeadTable {
61 major_version,
62 minor_version,
63 font_revision,
64 units_per_em,
65 x_min,
66 y_min,
67 x_max,
68 y_max,
69 mac_style,
70 lowest_rec_ppem,
71 index_to_loc_format,
72 })
73 }
74}