summaryrefslogtreecommitdiff
path: root/gnu/packages/patches/rust-pinot-0.1-implement-math-table.patch
blob: d9594f09c9b18804e2236541f0aca0f717bc6f20 (plain)
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
From f93bac061c59a6efab309b43eb893bf041c93ee1 Mon Sep 17 00:00:00 2001
From: Peter Williams <peter@newton.cx>
Date: Sun, 30 Jan 2022 11:12:58 -0500
Subject: [PATCH] Start implementing the MATH table

---
 src/font.rs       |   6 ++
 src/lib.rs        |   1 +
 src/math.rs       | 155 ++++++++++++++++++++++++++++++++++++++++++++++
 src/otl/shared.rs |   2 +-
 4 files changed, 163 insertions(+), 1 deletion(-)
 create mode 100644 src/math.rs

diff --git a/src/font.rs b/src/font.rs
index aeb17b5..762ff71 100644
--- a/src/font.rs
+++ b/src/font.rs
@@ -13,6 +13,7 @@ use super::{
     hhea::*,
     hmtx::*,
     hvar::*,
+    math::*,
     maxp::*,
     name::*,
     os2::*,
@@ -329,6 +330,11 @@ pub trait TableProvider<'a> {
     fn gpos(&self) -> Option<Gpos<'a>> {
         Some(Gpos::new(self.table_data(GPOS)?, self.gdef()))
     }
+
+    /// Returns the mathemetical typesetting table.
+    fn math(&self) -> Option<Math<'a>> {
+        Some(Math::new(self.table_data(MATH)?))
+    }
 }
 
 impl<'a> TableProvider<'a> for FontRef<'a> {
diff --git a/src/lib.rs b/src/lib.rs
index 8203630..d5e473d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -14,6 +14,7 @@ pub mod head;
 pub mod hhea;
 pub mod hmtx;
 pub mod hvar;
+pub mod math;
 pub mod maxp;
 pub mod name;
 pub mod os2;
diff --git a/src/math.rs b/src/math.rs
new file mode 100644
index 0000000..10a5ec6
--- /dev/null
+++ b/src/math.rs
@@ -0,0 +1,155 @@
+//! Mathematical typesetting table.
+
+use super::otl::Coverage;
+use super::parse_prelude::*;
+
+/// Tag for the `math` table.
+pub const MATH: Tag = Tag::new(b"MATH");
+
+/// Mathematical typesetting table.
+///
+/// <https://docs.microsoft.com/en-us/typography/opentype/spec/math>
+///
+/// The math constants and math glyph information subtables are not (yet)
+/// implemented.
+#[derive(Copy, Clone)]
+pub struct Math<'a>(Buffer<'a>);
+
+impl<'a> Math<'a> {
+    /// Creates a new math table from a byte slice containing the table data.
+    pub fn new(data: &'a [u8]) -> Self {
+        Self(Buffer::new(data))
+    }
+
+    /// Returns the major version.
+    pub fn major_version(&self) -> u16 {
+        self.0.read(0).unwrap_or(0)
+    }
+
+    /// Returns the minor version.
+    pub fn minor_version(&self) -> u16 {
+        self.0.read(2).unwrap_or(0)
+    }
+
+    /// Returns the MathVariants subtable.
+    pub fn variants(&self) -> Option<MathVariants> {
+        let offset = self.0.read_offset16(8, 0)?;
+        Some(MathVariants { math: self, offset })
+    }
+}
+
+/// Mathematical variants subtable.
+///
+/// <https://docs.microsoft.com/en-us/typography/opentype/spec/math#mathvariants-table>
+#[derive(Copy, Clone)]
+pub struct MathVariants<'a> {
+    math: &'a Math<'a>,
+    offset: u32,
+}
+
+impl<'a> MathVariants<'a> {
+    /// Returns the minimum overlap of connecting glyphs during glyph
+    /// construction.
+    pub fn min_connector_overlap(&self) -> UfWord {
+        self.math.0.read(self.offset as usize).unwrap_or(0)
+    }
+
+    /// Returns the number of glyphs for which information is provided for
+    /// vertically growing variants.
+    pub fn vert_glyph_count(&self) -> u16 {
+        self.math.0.read(self.offset as usize + 6).unwrap_or(0)
+    }
+
+    /// Returns the number of glyphs for which information is provided for
+    /// horizontally growing variants.
+    pub fn horiz_glyph_count(&self) -> u16 {
+        self.math.0.read(self.offset as usize + 8).unwrap_or(0)
+    }
+
+    /// Returns the coverage table associated with vertically growing glyphs.
+    pub fn vert_glyph_coverage(&self) -> Option<Coverage> {
+        let offset = self
+            .math
+            .0
+            .read_offset16(self.offset as usize + 2, self.offset)?;
+        Some(Coverage::new(self.math.0, offset))
+    }
+
+    /// Returns the coverage table associated with horizontally growing glyphs.
+    pub fn horiz_glyph_coverage(&self) -> Option<Coverage> {
+        let offset = self
+            .math
+            .0
+            .read_offset16(self.offset as usize + 4, self.offset)?;
+        Some(Coverage::new(self.math.0, offset))
+    }
+
+    /// Returns information about how to a construct vertically growing glyph,
+    /// based on its coverage index.
+    pub fn vert_glyph_construction(&self, coverage_index: u16) -> Option<MathGlyphConstruction> {
+        let offset = self.offset as usize + 10 + 2 * coverage_index as usize;
+        let offset = self.math.0.read_offset16(offset, self.offset)?;
+        Some(MathGlyphConstruction {
+            math: self.math,
+            offset: offset,
+        })
+    }
+
+    /// Returns information about how to a construct horizontally growing glyph,
+    /// based on its coverage index.
+    pub fn horiz_glyph_construction(&self, coverage_index: u16) -> Option<MathGlyphConstruction> {
+        let offset = self.offset as usize
+            + 10
+            + 2 * self.vert_glyph_count() as usize
+            + 2 * coverage_index as usize;
+        let offset = self.math.0.read_offset16(offset, self.offset)?;
+        Some(MathGlyphConstruction {
+            math: self.math,
+            offset: offset,
+        })
+    }
+}
+
+/// Mathematical glyph construction subtable.
+///
+/// <https://docs.microsoft.com/en-us/typography/opentype/spec/math#mathvariants-table>
+///
+/// The "glyph assembly" subtable is not (yet) implemented.
+#[derive(Copy, Clone)]
+pub struct MathGlyphConstruction<'a> {
+    math: &'a Math<'a>,
+    offset: u32,
+}
+
+impl<'a> MathGlyphConstruction<'a> {
+    /// Returns the number of growing variants for this glyph.
+    pub fn variant_count(&self) -> u16 {
+        self.math.0.read(self.offset as usize + 2).unwrap_or(0)
+    }
+
+    /// Return the growing variants associated with this glyph.
+    pub fn variants(&self) -> Option<Slice<'a, MathGlyphVariantRecord>> {
+        self.math
+            .0
+            .read_slice(self.offset as usize + 4, self.variant_count() as usize)
+    }
+}
+
+/// Information about a math glyph variant.
+#[derive(Copy, Clone, Debug)]
+pub struct MathGlyphVariantRecord {
+    /// The variant glyph
+    pub variant_glyph: GlyphId,
+    /// The advance width/height of the variant, in the direction of this
+    /// record's associated table.
+    pub advance_measurement: UfWord,
+}
+
+impl ReadData for MathGlyphVariantRecord {
+    unsafe fn read_data_unchecked(buf: &[u8], offset: usize) -> Self {
+        Self {
+            variant_glyph: GlyphId::read_data_unchecked(buf, offset),
+            advance_measurement: UfWord::read_data_unchecked(buf, offset + 2),
+        }
+    }
+}
diff --git a/src/otl/shared.rs b/src/otl/shared.rs
index 24f0f1d..f1a00fb 100644
--- a/src/otl/shared.rs
+++ b/src/otl/shared.rs
@@ -13,7 +13,7 @@ pub struct Coverage<'a> {
 }
 
 impl<'a> Coverage<'a> {
-    pub(super) fn new(data: Buffer<'a>, offset: u32) -> Self {
+    pub(crate) fn new(data: Buffer<'a>, offset: u32) -> Self {
         Self { data, offset }
     }