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
use crate::binary::read::{ReadArray, ReadBinaryDep, ReadCtxt};
use crate::binary::write::{WriteBinary, WriteContext};
use crate::binary::{U16Be, U32Be};
use crate::error::{ParseError, WriteError};
use crate::tables::IndexToLocFormat;
#[derive(Clone, Debug)]
pub struct LocaTable<'a> {
pub offsets: LocaOffsets<'a>,
}
#[derive(Clone, Debug)]
pub enum LocaOffsets<'a> {
Short(ReadArray<'a, U16Be>),
Long(ReadArray<'a, U32Be>),
}
pub struct LocaOffsetsIter<'a, 'b> {
offsets: &'b LocaOffsets<'a>,
index: usize,
}
impl<'a> ReadBinaryDep<'a> for LocaTable<'a> {
type Args = (usize, IndexToLocFormat);
type HostType = Self;
fn read_dep(
ctxt: &mut ReadCtxt<'a>,
(num_glyphs, index_to_loc_format): (usize, IndexToLocFormat),
) -> Result<Self, ParseError> {
let offsets = match index_to_loc_format {
IndexToLocFormat::Short => {
LocaOffsets::Short(ctxt.read_array::<U16Be>(num_glyphs + 1)?)
}
IndexToLocFormat::Long => {
LocaOffsets::Long(ctxt.read_array::<U32Be>(num_glyphs + 1)?)
}
};
Ok(LocaTable { offsets })
}
}
impl<'a> WriteBinary for LocaTable<'a> {
type Output = ();
fn write<C: WriteContext>(ctxt: &mut C, loca: LocaTable<'a>) -> Result<(), WriteError> {
match loca.offsets {
LocaOffsets::Long(array) => ctxt.write_array(&array),
LocaOffsets::Short(array) => ctxt.write_array(&array),
}?;
Ok(())
}
}
impl<'a> LocaTable<'a> {
pub fn empty() -> Self {
LocaTable {
offsets: LocaOffsets::Long(ReadArray::empty()),
}
}
}
impl<'a> LocaOffsets<'a> {
pub fn iter<'b>(&'b self) -> LocaOffsetsIter<'a, 'b> {
LocaOffsetsIter {
offsets: &self,
index: 0,
}
}
pub fn len(&self) -> usize {
match self {
LocaOffsets::Short(array) => array.len(),
LocaOffsets::Long(array) => array.len(),
}
}
}
impl<'a, 'b> Iterator for LocaOffsetsIter<'a, 'b> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.offsets.len() {
let item = match self.offsets {
LocaOffsets::Short(array) => u32::from(array.get_item(self.index)) * 2,
LocaOffsets::Long(array) => array.get_item(self.index),
};
self.index += 1;
Some(item)
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = match self.offsets {
LocaOffsets::Short(array) => array.len(),
LocaOffsets::Long(array) => array.len(),
};
if self.index < len {
let upper = len - self.index;
(upper, Some(upper))
} else {
(0, Some(0))
}
}
}
pub mod owned {
use core::convert::TryFrom;
use alloc::vec::Vec;
use super::{IndexToLocFormat, U16Be, U32Be, WriteContext, WriteError};
use crate::binary::write::{WriteBinary, WriteBinaryDep};
pub struct LocaTable {
pub offsets: Vec<u32>,
}
impl<'a> WriteBinaryDep<Self> for LocaTable {
type Output = ();
type Args = IndexToLocFormat;
fn write_dep<C: WriteContext>(
ctxt: &mut C,
loca: LocaTable,
index_to_loc_format: Self::Args,
) -> Result<(), WriteError> {
match index_to_loc_format {
IndexToLocFormat::Short => {
match loca.offsets.last() {
Some(&last) if (last / 2) > u32::from(core::u16::MAX) => {
return Err(WriteError::BadValue)
}
_ => {}
}
for offset in loca.offsets {
if offset & 1 == 1 {
return Err(WriteError::BadValue);
}
let short_offset = u16::try_from(offset / 2)?;
U16Be::write(ctxt, short_offset)?;
}
Ok(())
}
IndexToLocFormat::Long => ctxt.write_vec::<U32Be>(loca.offsets),
}
}
}
}