/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.46.0/generated/generated_glyf.rs
Line | Count | Source |
1 | | // THIS FILE IS AUTOGENERATED. |
2 | | // Any changes to this file will be overwritten. |
3 | | // For more information about how codegen works, see font-codegen/README.md |
4 | | |
5 | | #[allow(unused_imports)] |
6 | | use crate::codegen_prelude::*; |
7 | | |
8 | | impl TopLevelTable for Glyf<'_> { |
9 | | /// `glyf` |
10 | | const TAG: Tag = Tag::new(b"glyf"); |
11 | | } |
12 | | |
13 | | impl ReadArgs for Glyf<'_> { |
14 | | type Args = (); |
15 | | } |
16 | | |
17 | | impl<'a> FontRead<'a> for Glyf<'a> { |
18 | 1.83k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
19 | | #[allow(clippy::absurd_extreme_comparisons)] |
20 | 1.83k | if data.len() < Self::MIN_SIZE { |
21 | 0 | return Err(ReadError::OutOfBounds); |
22 | 1.83k | } |
23 | 1.83k | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
24 | 0 | return Err(ReadError::OutOfBounds); |
25 | | }; |
26 | 1.83k | Ok(Self { data }) |
27 | 1.83k | } |
28 | | } |
29 | | |
30 | | /// The fields at the start of the table, which are read directly. |
31 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
32 | | #[repr(C, packed)] |
33 | | #[doc(hidden)] |
34 | | pub struct GlyfFixedHeader {} |
35 | | |
36 | | #[allow(clippy::absurd_extreme_comparisons)] |
37 | | const _: () = assert!(std::mem::size_of::<GlyfFixedHeader>() <= Glyf::MIN_SIZE); |
38 | | |
39 | | /// The [glyf (Glyph Data)](https://docs.microsoft.com/en-us/typography/opentype/spec/glyf) table |
40 | | #[derive(Clone)] |
41 | | pub struct Glyf<'a> { |
42 | | data: TableDataRef<'a, GlyfFixedHeader>, |
43 | | } |
44 | | |
45 | | #[allow(clippy::needless_lifetimes)] |
46 | | impl<'a> Glyf<'a> { |
47 | | pub const MIN_SIZE: usize = 0; |
48 | | basic_table_impls!(impl_the_methods); |
49 | | } |
50 | | |
51 | | #[allow(clippy::absurd_extreme_comparisons)] |
52 | | const _: () = assert!(FontData::default_data_long_enough(Glyf::MIN_SIZE)); |
53 | | |
54 | | impl Default for Glyf<'_> { |
55 | 0 | fn default() -> Self { |
56 | | Self { |
57 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
58 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
59 | | } |
60 | 0 | } |
61 | | } |
62 | | |
63 | | impl<'a> MinByteRange<'a> for SimpleGlyph<'a> { |
64 | 0 | fn min_byte_range(&self) -> Range<usize> { |
65 | 0 | 0..self.glyph_data_byte_range().end |
66 | 0 | } |
67 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
68 | 0 | let range = self.min_byte_range(); |
69 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
70 | 0 | } |
71 | | } |
72 | | |
73 | | impl ReadArgs for SimpleGlyph<'_> { |
74 | | type Args = (); |
75 | | } |
76 | | |
77 | | impl<'a> FontRead<'a> for SimpleGlyph<'a> { |
78 | 3.09k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
79 | | #[allow(clippy::absurd_extreme_comparisons)] |
80 | 3.09k | if data.len() < Self::MIN_SIZE { |
81 | 84 | return Err(ReadError::OutOfBounds); |
82 | 3.01k | } |
83 | 3.01k | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
84 | 0 | return Err(ReadError::OutOfBounds); |
85 | | }; |
86 | 3.01k | Ok(Self { data }) |
87 | 3.09k | } |
88 | | } |
89 | | |
90 | | /// The fields at the start of the table, which are read directly. |
91 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
92 | | #[repr(C, packed)] |
93 | | #[doc(hidden)] |
94 | | pub struct SimpleGlyphFixedHeader { |
95 | | pub number_of_contours: BigEndian<i16>, |
96 | | pub x_min: BigEndian<i16>, |
97 | | pub y_min: BigEndian<i16>, |
98 | | pub x_max: BigEndian<i16>, |
99 | | pub y_max: BigEndian<i16>, |
100 | | } |
101 | | |
102 | | #[allow(clippy::absurd_extreme_comparisons)] |
103 | | const _: () = assert!(std::mem::size_of::<SimpleGlyphFixedHeader>() <= SimpleGlyph::MIN_SIZE); |
104 | | |
105 | | /// The [Glyph Header](https://docs.microsoft.com/en-us/typography/opentype/spec/glyf#glyph-headers) |
106 | | #[derive(Clone)] |
107 | | pub struct SimpleGlyph<'a> { |
108 | | data: TableDataRef<'a, SimpleGlyphFixedHeader>, |
109 | | } |
110 | | |
111 | | #[allow(clippy::needless_lifetimes)] |
112 | | impl<'a> SimpleGlyph<'a> { |
113 | | pub const MIN_SIZE: usize = (i16::RAW_BYTE_LEN |
114 | | + i16::RAW_BYTE_LEN |
115 | | + i16::RAW_BYTE_LEN |
116 | | + i16::RAW_BYTE_LEN |
117 | | + i16::RAW_BYTE_LEN |
118 | | + u16::RAW_BYTE_LEN); |
119 | | basic_table_impls!(impl_the_methods); |
120 | | |
121 | | /// If the number of contours is greater than or equal to zero, |
122 | | /// this is a simple glyph. If negative, this is a composite glyph |
123 | | /// — the value -1 should be used for composite glyphs. |
124 | 24.0k | pub fn number_of_contours(&self) -> i16 { |
125 | 24.0k | self.data.header().number_of_contours.get() |
126 | 24.0k | } |
127 | | |
128 | | /// Minimum x for coordinate data. |
129 | 2.00k | pub fn x_min(&self) -> i16 { |
130 | 2.00k | self.data.header().x_min.get() |
131 | 2.00k | } |
132 | | |
133 | | /// Minimum y for coordinate data. |
134 | 76 | pub fn y_min(&self) -> i16 { |
135 | 76 | self.data.header().y_min.get() |
136 | 76 | } |
137 | | |
138 | | /// Maximum x for coordinate data. |
139 | 76 | pub fn x_max(&self) -> i16 { |
140 | 76 | self.data.header().x_max.get() |
141 | 76 | } |
142 | | |
143 | | /// Maximum y for coordinate data. |
144 | 2.00k | pub fn y_max(&self) -> i16 { |
145 | 2.00k | self.data.header().y_max.get() |
146 | 2.00k | } |
147 | | |
148 | | /// Array of point indices for the last point of each contour, |
149 | | /// in increasing numeric order |
150 | 14.7k | pub fn end_pts_of_contours(&self) -> &'a [BigEndian<u16>] { |
151 | 14.7k | let range = self.end_pts_of_contours_byte_range(); |
152 | 14.7k | self.offset_data() |
153 | 14.7k | .read_array(range) |
154 | 14.7k | .ok() |
155 | 14.7k | .unwrap_or_default() |
156 | 14.7k | } |
157 | | |
158 | | /// Total number of bytes for instructions. If instructionLength is |
159 | | /// zero, no instructions are present for this glyph, and this |
160 | | /// field is followed directly by the flags field. |
161 | 5.88k | pub fn instruction_length(&self) -> u16 { |
162 | 5.88k | let range = self.instruction_length_byte_range(); |
163 | 5.88k | self.offset_data() |
164 | 5.88k | .read_at(range.start) |
165 | 5.88k | .ok() |
166 | 5.88k | .unwrap_or_default() |
167 | 5.88k | } |
168 | | |
169 | | /// Array of instruction byte code for the glyph. |
170 | 0 | pub fn instructions(&self) -> &'a [u8] { |
171 | 0 | let range = self.instructions_byte_range(); |
172 | 0 | self.offset_data() |
173 | 0 | .read_array(range) |
174 | 0 | .ok() |
175 | 0 | .unwrap_or_default() |
176 | 0 | } |
177 | | |
178 | | /// the raw data for flags & x/y coordinates |
179 | 3.28k | pub fn glyph_data(&self) -> &'a [u8] { |
180 | 3.28k | let range = self.glyph_data_byte_range(); |
181 | 3.28k | self.offset_data() |
182 | 3.28k | .read_array(range) |
183 | 3.28k | .ok() |
184 | 3.28k | .unwrap_or_default() |
185 | 3.28k | } |
186 | | |
187 | 23.9k | pub fn number_of_contours_byte_range(&self) -> Range<usize> { |
188 | 23.9k | let start = 0; |
189 | 23.9k | let end = start + i16::RAW_BYTE_LEN; |
190 | 23.9k | start..end |
191 | 23.9k | } |
192 | | |
193 | 23.9k | pub fn x_min_byte_range(&self) -> Range<usize> { |
194 | 23.9k | let start = self.number_of_contours_byte_range().end; |
195 | 23.9k | let end = start + i16::RAW_BYTE_LEN; |
196 | 23.9k | start..end |
197 | 23.9k | } |
198 | | |
199 | 23.9k | pub fn y_min_byte_range(&self) -> Range<usize> { |
200 | 23.9k | let start = self.x_min_byte_range().end; |
201 | 23.9k | let end = start + i16::RAW_BYTE_LEN; |
202 | 23.9k | start..end |
203 | 23.9k | } |
204 | | |
205 | 23.9k | pub fn x_max_byte_range(&self) -> Range<usize> { |
206 | 23.9k | let start = self.y_min_byte_range().end; |
207 | 23.9k | let end = start + i16::RAW_BYTE_LEN; |
208 | 23.9k | start..end |
209 | 23.9k | } |
210 | | |
211 | 23.9k | pub fn y_max_byte_range(&self) -> Range<usize> { |
212 | 23.9k | let start = self.x_max_byte_range().end; |
213 | 23.9k | let end = start + i16::RAW_BYTE_LEN; |
214 | 23.9k | start..end |
215 | 23.9k | } |
216 | | |
217 | 23.9k | pub fn end_pts_of_contours_byte_range(&self) -> Range<usize> { |
218 | 23.9k | let number_of_contours = self.number_of_contours(); |
219 | 23.9k | let start = self.y_max_byte_range().end; |
220 | 23.9k | let end = |
221 | 23.9k | start + (transforms::to_usize(number_of_contours)).saturating_mul(u16::RAW_BYTE_LEN); |
222 | 23.9k | start..end |
223 | 23.9k | } |
224 | | |
225 | 9.16k | pub fn instruction_length_byte_range(&self) -> Range<usize> { |
226 | 9.16k | let start = self.end_pts_of_contours_byte_range().end; |
227 | 9.16k | let end = start + u16::RAW_BYTE_LEN; |
228 | 9.16k | start..end |
229 | 9.16k | } |
230 | | |
231 | 3.28k | pub fn instructions_byte_range(&self) -> Range<usize> { |
232 | 3.28k | let instruction_length = self.instruction_length(); |
233 | 3.28k | let start = self.instruction_length_byte_range().end; |
234 | 3.28k | let end = |
235 | 3.28k | start + (transforms::to_usize(instruction_length)).saturating_mul(u8::RAW_BYTE_LEN); |
236 | 3.28k | start..end |
237 | 3.28k | } |
238 | | |
239 | 3.28k | pub fn glyph_data_byte_range(&self) -> Range<usize> { |
240 | 3.28k | let start = self.instructions_byte_range().end; |
241 | 3.28k | let end = start |
242 | 3.28k | + self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN; |
243 | 3.28k | start..end |
244 | 3.28k | } |
245 | | } |
246 | | |
247 | | const _: () = assert!(FontData::default_data_long_enough(SimpleGlyph::MIN_SIZE)); |
248 | | |
249 | | impl Default for SimpleGlyph<'_> { |
250 | 0 | fn default() -> Self { |
251 | | Self { |
252 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
253 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
254 | | } |
255 | 0 | } |
256 | | } |
257 | | |
258 | | /// Flags used in [SimpleGlyph] |
259 | | #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)] |
260 | | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
261 | | #[repr(transparent)] |
262 | | pub struct SimpleGlyphFlags { |
263 | | bits: u8, |
264 | | } |
265 | | |
266 | | impl SimpleGlyphFlags { |
267 | | /// Bit 0: If set, the point is on the curve; otherwise, it is off |
268 | | /// the curve. |
269 | | pub const ON_CURVE_POINT: Self = Self { bits: 0x01 }; |
270 | | |
271 | | /// Bit 1: If set, the corresponding x-coordinate is 1 byte long, |
272 | | /// and the sign is determined by the |
273 | | /// X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR flag. If not set, its |
274 | | /// interpretation depends on the |
275 | | /// X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR flag: If that other flag |
276 | | /// is set, the x-coordinate is the same as the previous |
277 | | /// x-coordinate, and no element is added to the xCoordinates |
278 | | /// array. If both flags are not set, the corresponding element in |
279 | | /// the xCoordinates array is two bytes and interpreted as a signed |
280 | | /// integer. See the description of the |
281 | | /// X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR flag for additional |
282 | | /// information. |
283 | | pub const X_SHORT_VECTOR: Self = Self { bits: 0x02 }; |
284 | | |
285 | | /// Bit 2: If set, the corresponding y-coordinate is 1 byte long, |
286 | | /// and the sign is determined by the |
287 | | /// Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR flag. If not set, its |
288 | | /// interpretation depends on the |
289 | | /// Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR flag: If that other flag |
290 | | /// is set, the y-coordinate is the same as the previous |
291 | | /// y-coordinate, and no element is added to the yCoordinates |
292 | | /// array. If both flags are not set, the corresponding element in |
293 | | /// the yCoordinates array is two bytes and interpreted as a signed |
294 | | /// integer. See the description of the |
295 | | /// Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR flag for additional |
296 | | /// information. |
297 | | pub const Y_SHORT_VECTOR: Self = Self { bits: 0x04 }; |
298 | | |
299 | | /// Bit 3: If set, the next byte (read as unsigned) specifies the |
300 | | /// number of additional times this flag byte is to be repeated in |
301 | | /// the logical flags array — that is, the number of additional |
302 | | /// logical flag entries inserted after this entry. (In the |
303 | | /// expanded logical array, this bit is ignored.) In this way, the |
304 | | /// number of flags listed can be smaller than the number of points |
305 | | /// in the glyph description. |
306 | | pub const REPEAT_FLAG: Self = Self { bits: 0x08 }; |
307 | | |
308 | | /// Bit 4: This flag has two meanings, depending on how the |
309 | | /// X_SHORT_VECTOR flag is set. If X_SHORT_VECTOR is set, this bit |
310 | | /// describes the sign of the value, with 1 equalling positive and |
311 | | /// 0 negative. If X_SHORT_VECTOR is not set and this bit is set, |
312 | | /// then the current x-coordinate is the same as the previous |
313 | | /// x-coordinate. If X_SHORT_VECTOR is not set and this bit is also |
314 | | /// not set, the current x-coordinate is a signed 16-bit delta |
315 | | /// vector. |
316 | | pub const X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR: Self = Self { bits: 0x10 }; |
317 | | |
318 | | /// Bit 5: This flag has two meanings, depending on how the |
319 | | /// Y_SHORT_VECTOR flag is set. If Y_SHORT_VECTOR is set, this bit |
320 | | /// describes the sign of the value, with 1 equalling positive and |
321 | | /// 0 negative. If Y_SHORT_VECTOR is not set and this bit is set, |
322 | | /// then the current y-coordinate is the same as the previous |
323 | | /// y-coordinate. If Y_SHORT_VECTOR is not set and this bit is also |
324 | | /// not set, the current y-coordinate is a signed 16-bit delta |
325 | | /// vector. |
326 | | pub const Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR: Self = Self { bits: 0x20 }; |
327 | | |
328 | | /// Bit 6: If set, contours in the glyph description may overlap. |
329 | | /// Use of this flag is not required in OpenType — that is, it is |
330 | | /// valid to have contours overlap without having this flag set. It |
331 | | /// may affect behaviors in some platforms, however. (See the |
332 | | /// discussion of “Overlapping contours” in Apple’s |
333 | | /// specification for details regarding behavior in Apple |
334 | | /// platforms.) When used, it must be set on the first flag byte |
335 | | /// for the glyph. See additional details below. |
336 | | pub const OVERLAP_SIMPLE: Self = Self { bits: 0x40 }; |
337 | | |
338 | | /// Bit 7: Off-curve point belongs to a cubic-Bezier segment in GLYF |
339 | | /// (ISO/IEC 14496-22:2026, 5.2.8). |
340 | | pub const CUBIC: Self = Self { bits: 0x80 }; |
341 | | } |
342 | | |
343 | | impl SimpleGlyphFlags { |
344 | | /// Returns an empty set of flags. |
345 | | #[inline] |
346 | 0 | pub const fn empty() -> Self { |
347 | 0 | Self { bits: 0 } |
348 | 0 | } |
349 | | |
350 | | /// Returns the set containing all flags. |
351 | | #[inline] |
352 | 26.0k | pub const fn all() -> Self { |
353 | 26.0k | Self { |
354 | 26.0k | bits: Self::ON_CURVE_POINT.bits |
355 | 26.0k | | Self::X_SHORT_VECTOR.bits |
356 | 26.0k | | Self::Y_SHORT_VECTOR.bits |
357 | 26.0k | | Self::REPEAT_FLAG.bits |
358 | 26.0k | | Self::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR.bits |
359 | 26.0k | | Self::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR.bits |
360 | 26.0k | | Self::OVERLAP_SIMPLE.bits |
361 | 26.0k | | Self::CUBIC.bits, |
362 | 26.0k | } |
363 | 26.0k | } |
364 | | |
365 | | /// Returns the raw value of the flags currently stored. |
366 | | #[inline] |
367 | 0 | pub const fn bits(&self) -> u8 { |
368 | 0 | self.bits |
369 | 0 | } |
370 | | |
371 | | /// Convert from underlying bit representation, unless that |
372 | | /// representation contains bits that do not correspond to a flag. |
373 | | #[inline] |
374 | 0 | pub const fn from_bits(bits: u8) -> Option<Self> { |
375 | 0 | if (bits & !Self::all().bits()) == 0 { |
376 | 0 | Some(Self { bits }) |
377 | | } else { |
378 | 0 | None |
379 | | } |
380 | 0 | } |
381 | | |
382 | | /// Convert from underlying bit representation, dropping any bits |
383 | | /// that do not correspond to flags. |
384 | | #[inline] |
385 | 26.0k | pub const fn from_bits_truncate(bits: u8) -> Self { |
386 | 26.0k | Self { |
387 | 26.0k | bits: bits & Self::all().bits, |
388 | 26.0k | } |
389 | 26.0k | } |
390 | | |
391 | | /// Returns `true` if no flags are currently stored. |
392 | | #[inline] |
393 | 0 | pub const fn is_empty(&self) -> bool { |
394 | 0 | self.bits() == Self::empty().bits() |
395 | 0 | } |
396 | | |
397 | | /// Returns `true` if there are flags common to both `self` and `other`. |
398 | | #[inline] |
399 | 0 | pub const fn intersects(&self, other: Self) -> bool { |
400 | 0 | !(Self { |
401 | 0 | bits: self.bits & other.bits, |
402 | 0 | }) |
403 | 0 | .is_empty() |
404 | 0 | } |
405 | | |
406 | | /// Returns `true` if all of the flags in `other` are contained within `self`. |
407 | | #[inline] |
408 | 44.9k | pub const fn contains(&self, other: Self) -> bool { |
409 | 44.9k | (self.bits & other.bits) == other.bits |
410 | 44.9k | } |
411 | | |
412 | | /// Inserts the specified flags in-place. |
413 | | #[inline] |
414 | 0 | pub fn insert(&mut self, other: Self) { |
415 | 0 | self.bits |= other.bits; |
416 | 0 | } |
417 | | |
418 | | /// Removes the specified flags in-place. |
419 | | #[inline] |
420 | 0 | pub fn remove(&mut self, other: Self) { |
421 | 0 | self.bits &= !other.bits; |
422 | 0 | } |
423 | | |
424 | | /// Toggles the specified flags in-place. |
425 | | #[inline] |
426 | 0 | pub fn toggle(&mut self, other: Self) { |
427 | 0 | self.bits ^= other.bits; |
428 | 0 | } |
429 | | |
430 | | /// Returns the intersection between the flags in `self` and |
431 | | /// `other`. |
432 | | /// |
433 | | /// Specifically, the returned set contains only the flags which are |
434 | | /// present in *both* `self` *and* `other`. |
435 | | /// |
436 | | /// This is equivalent to using the `&` operator (e.g. |
437 | | /// [`ops::BitAnd`]), as in `flags & other`. |
438 | | /// |
439 | | /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html |
440 | | #[inline] |
441 | | #[must_use] |
442 | 0 | pub const fn intersection(self, other: Self) -> Self { |
443 | 0 | Self { |
444 | 0 | bits: self.bits & other.bits, |
445 | 0 | } |
446 | 0 | } |
447 | | |
448 | | /// Returns the union of between the flags in `self` and `other`. |
449 | | /// |
450 | | /// Specifically, the returned set contains all flags which are |
451 | | /// present in *either* `self` *or* `other`, including any which are |
452 | | /// present in both. |
453 | | /// |
454 | | /// This is equivalent to using the `|` operator (e.g. |
455 | | /// [`ops::BitOr`]), as in `flags | other`. |
456 | | /// |
457 | | /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html |
458 | | #[inline] |
459 | | #[must_use] |
460 | 0 | pub const fn union(self, other: Self) -> Self { |
461 | 0 | Self { |
462 | 0 | bits: self.bits | other.bits, |
463 | 0 | } |
464 | 0 | } |
465 | | |
466 | | /// Returns the difference between the flags in `self` and `other`. |
467 | | /// |
468 | | /// Specifically, the returned set contains all flags present in |
469 | | /// `self`, except for the ones present in `other`. |
470 | | /// |
471 | | /// It is also conceptually equivalent to the "bit-clear" operation: |
472 | | /// `flags & !other` (and this syntax is also supported). |
473 | | /// |
474 | | /// This is equivalent to using the `-` operator (e.g. |
475 | | /// [`ops::Sub`]), as in `flags - other`. |
476 | | /// |
477 | | /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html |
478 | | #[inline] |
479 | | #[must_use] |
480 | 0 | pub const fn difference(self, other: Self) -> Self { |
481 | 0 | Self { |
482 | 0 | bits: self.bits & !other.bits, |
483 | 0 | } |
484 | 0 | } |
485 | | } |
486 | | |
487 | | impl std::ops::BitOr for SimpleGlyphFlags { |
488 | | type Output = Self; |
489 | | |
490 | | /// Returns the union of the two sets of flags. |
491 | | #[inline] |
492 | 0 | fn bitor(self, other: SimpleGlyphFlags) -> Self { |
493 | 0 | Self { |
494 | 0 | bits: self.bits | other.bits, |
495 | 0 | } |
496 | 0 | } |
497 | | } |
498 | | |
499 | | impl std::ops::BitOrAssign for SimpleGlyphFlags { |
500 | | /// Adds the set of flags. |
501 | | #[inline] |
502 | 0 | fn bitor_assign(&mut self, other: Self) { |
503 | 0 | self.bits |= other.bits; |
504 | 0 | } |
505 | | } |
506 | | |
507 | | impl std::ops::BitXor for SimpleGlyphFlags { |
508 | | type Output = Self; |
509 | | |
510 | | /// Returns the left flags, but with all the right flags toggled. |
511 | | #[inline] |
512 | 0 | fn bitxor(self, other: Self) -> Self { |
513 | 0 | Self { |
514 | 0 | bits: self.bits ^ other.bits, |
515 | 0 | } |
516 | 0 | } |
517 | | } |
518 | | |
519 | | impl std::ops::BitXorAssign for SimpleGlyphFlags { |
520 | | /// Toggles the set of flags. |
521 | | #[inline] |
522 | 0 | fn bitxor_assign(&mut self, other: Self) { |
523 | 0 | self.bits ^= other.bits; |
524 | 0 | } |
525 | | } |
526 | | |
527 | | impl std::ops::BitAnd for SimpleGlyphFlags { |
528 | | type Output = Self; |
529 | | |
530 | | /// Returns the intersection between the two sets of flags. |
531 | | #[inline] |
532 | 0 | fn bitand(self, other: Self) -> Self { |
533 | 0 | Self { |
534 | 0 | bits: self.bits & other.bits, |
535 | 0 | } |
536 | 0 | } |
537 | | } |
538 | | |
539 | | impl std::ops::BitAndAssign for SimpleGlyphFlags { |
540 | | /// Disables all flags disabled in the set. |
541 | | #[inline] |
542 | 0 | fn bitand_assign(&mut self, other: Self) { |
543 | 0 | self.bits &= other.bits; |
544 | 0 | } |
545 | | } |
546 | | |
547 | | impl std::ops::Sub for SimpleGlyphFlags { |
548 | | type Output = Self; |
549 | | |
550 | | /// Returns the set difference of the two sets of flags. |
551 | | #[inline] |
552 | 0 | fn sub(self, other: Self) -> Self { |
553 | 0 | Self { |
554 | 0 | bits: self.bits & !other.bits, |
555 | 0 | } |
556 | 0 | } |
557 | | } |
558 | | |
559 | | impl std::ops::SubAssign for SimpleGlyphFlags { |
560 | | /// Disables all flags enabled in the set. |
561 | | #[inline] |
562 | 0 | fn sub_assign(&mut self, other: Self) { |
563 | 0 | self.bits &= !other.bits; |
564 | 0 | } |
565 | | } |
566 | | |
567 | | impl std::ops::Not for SimpleGlyphFlags { |
568 | | type Output = Self; |
569 | | |
570 | | /// Returns the complement of this set of flags. |
571 | | #[inline] |
572 | 0 | fn not(self) -> Self { |
573 | 0 | Self { bits: !self.bits } & Self::all() |
574 | 0 | } |
575 | | } |
576 | | |
577 | | impl std::fmt::Debug for SimpleGlyphFlags { |
578 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
579 | 0 | let members: &[(&str, Self)] = &[ |
580 | 0 | ("ON_CURVE_POINT", Self::ON_CURVE_POINT), |
581 | 0 | ("X_SHORT_VECTOR", Self::X_SHORT_VECTOR), |
582 | 0 | ("Y_SHORT_VECTOR", Self::Y_SHORT_VECTOR), |
583 | 0 | ("REPEAT_FLAG", Self::REPEAT_FLAG), |
584 | 0 | ( |
585 | 0 | "X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR", |
586 | 0 | Self::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR, |
587 | 0 | ), |
588 | 0 | ( |
589 | 0 | "Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR", |
590 | 0 | Self::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR, |
591 | 0 | ), |
592 | 0 | ("OVERLAP_SIMPLE", Self::OVERLAP_SIMPLE), |
593 | 0 | ("CUBIC", Self::CUBIC), |
594 | 0 | ]; |
595 | 0 | let mut first = true; |
596 | 0 | for (name, value) in members { |
597 | 0 | if self.contains(*value) { |
598 | 0 | if !first { |
599 | 0 | f.write_str(" | ")?; |
600 | 0 | } |
601 | 0 | first = false; |
602 | 0 | f.write_str(name)?; |
603 | 0 | } |
604 | | } |
605 | 0 | if first { |
606 | 0 | f.write_str("(empty)")?; |
607 | 0 | } |
608 | 0 | Ok(()) |
609 | 0 | } |
610 | | } |
611 | | |
612 | | impl std::fmt::Binary for SimpleGlyphFlags { |
613 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
614 | 0 | std::fmt::Binary::fmt(&self.bits, f) |
615 | 0 | } |
616 | | } |
617 | | |
618 | | impl std::fmt::Octal for SimpleGlyphFlags { |
619 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
620 | 0 | std::fmt::Octal::fmt(&self.bits, f) |
621 | 0 | } |
622 | | } |
623 | | |
624 | | impl std::fmt::LowerHex for SimpleGlyphFlags { |
625 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
626 | 0 | std::fmt::LowerHex::fmt(&self.bits, f) |
627 | 0 | } |
628 | | } |
629 | | |
630 | | impl std::fmt::UpperHex for SimpleGlyphFlags { |
631 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
632 | 0 | std::fmt::UpperHex::fmt(&self.bits, f) |
633 | 0 | } |
634 | | } |
635 | | |
636 | | impl font_types::Scalar for SimpleGlyphFlags { |
637 | | type Raw = <u8 as font_types::Scalar>::Raw; |
638 | 0 | fn to_raw(self) -> Self::Raw { |
639 | 0 | self.bits().to_raw() |
640 | 0 | } |
641 | 790 | fn from_raw(raw: Self::Raw) -> Self { |
642 | 790 | let t = <u8>::from_raw(raw); |
643 | 790 | Self::from_bits_truncate(t) |
644 | 790 | } |
645 | | } |
646 | | |
647 | | impl<'a> MinByteRange<'a> for CompositeGlyph<'a> { |
648 | 0 | fn min_byte_range(&self) -> Range<usize> { |
649 | 0 | 0..self.component_data_byte_range().end |
650 | 0 | } |
651 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
652 | 0 | let range = self.min_byte_range(); |
653 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
654 | 0 | } |
655 | | } |
656 | | |
657 | | impl ReadArgs for CompositeGlyph<'_> { |
658 | | type Args = (); |
659 | | } |
660 | | |
661 | | impl<'a> FontRead<'a> for CompositeGlyph<'a> { |
662 | 23.0k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
663 | | #[allow(clippy::absurd_extreme_comparisons)] |
664 | 23.0k | if data.len() < Self::MIN_SIZE { |
665 | 79 | return Err(ReadError::OutOfBounds); |
666 | 22.9k | } |
667 | 22.9k | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
668 | 0 | return Err(ReadError::OutOfBounds); |
669 | | }; |
670 | 22.9k | Ok(Self { data }) |
671 | 23.0k | } |
672 | | } |
673 | | |
674 | | /// The fields at the start of the table, which are read directly. |
675 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
676 | | #[repr(C, packed)] |
677 | | #[doc(hidden)] |
678 | | pub struct CompositeGlyphFixedHeader { |
679 | | pub number_of_contours: BigEndian<i16>, |
680 | | pub x_min: BigEndian<i16>, |
681 | | pub y_min: BigEndian<i16>, |
682 | | pub x_max: BigEndian<i16>, |
683 | | pub y_max: BigEndian<i16>, |
684 | | } |
685 | | |
686 | | #[allow(clippy::absurd_extreme_comparisons)] |
687 | | const _: () = assert!(std::mem::size_of::<CompositeGlyphFixedHeader>() <= CompositeGlyph::MIN_SIZE); |
688 | | |
689 | | /// [CompositeGlyph](https://docs.microsoft.com/en-us/typography/opentype/spec/glyf#glyph-headers) |
690 | | #[derive(Clone)] |
691 | | pub struct CompositeGlyph<'a> { |
692 | | data: TableDataRef<'a, CompositeGlyphFixedHeader>, |
693 | | } |
694 | | |
695 | | #[allow(clippy::needless_lifetimes)] |
696 | | impl<'a> CompositeGlyph<'a> { |
697 | | pub const MIN_SIZE: usize = (i16::RAW_BYTE_LEN |
698 | | + i16::RAW_BYTE_LEN |
699 | | + i16::RAW_BYTE_LEN |
700 | | + i16::RAW_BYTE_LEN |
701 | | + i16::RAW_BYTE_LEN); |
702 | | basic_table_impls!(impl_the_methods); |
703 | | |
704 | | /// If the number of contours is greater than or equal to zero, |
705 | | /// this is a simple glyph. If negative, this is a composite glyph |
706 | | /// — the value -1 should be used for composite glyphs. |
707 | 54 | pub fn number_of_contours(&self) -> i16 { |
708 | 54 | self.data.header().number_of_contours.get() |
709 | 54 | } |
710 | | |
711 | | /// Minimum x for coordinate data. |
712 | 739 | pub fn x_min(&self) -> i16 { |
713 | 739 | self.data.header().x_min.get() |
714 | 739 | } |
715 | | |
716 | | /// Minimum y for coordinate data. |
717 | 54 | pub fn y_min(&self) -> i16 { |
718 | 54 | self.data.header().y_min.get() |
719 | 54 | } |
720 | | |
721 | | /// Maximum x for coordinate data. |
722 | 54 | pub fn x_max(&self) -> i16 { |
723 | 54 | self.data.header().x_max.get() |
724 | 54 | } |
725 | | |
726 | | /// Maximum y for coordinate data. |
727 | 739 | pub fn y_max(&self) -> i16 { |
728 | 739 | self.data.header().y_max.get() |
729 | 739 | } |
730 | | |
731 | | /// component flag |
732 | | /// glyph index of component |
733 | 28.3k | pub fn component_data(&self) -> &'a [u8] { |
734 | 28.3k | let range = self.component_data_byte_range(); |
735 | 28.3k | self.offset_data() |
736 | 28.3k | .read_array(range) |
737 | 28.3k | .ok() |
738 | 28.3k | .unwrap_or_default() |
739 | 28.3k | } |
740 | | |
741 | 28.3k | pub fn number_of_contours_byte_range(&self) -> Range<usize> { |
742 | 28.3k | let start = 0; |
743 | 28.3k | let end = start + i16::RAW_BYTE_LEN; |
744 | 28.3k | start..end |
745 | 28.3k | } |
746 | | |
747 | 28.3k | pub fn x_min_byte_range(&self) -> Range<usize> { |
748 | 28.3k | let start = self.number_of_contours_byte_range().end; |
749 | 28.3k | let end = start + i16::RAW_BYTE_LEN; |
750 | 28.3k | start..end |
751 | 28.3k | } |
752 | | |
753 | 28.3k | pub fn y_min_byte_range(&self) -> Range<usize> { |
754 | 28.3k | let start = self.x_min_byte_range().end; |
755 | 28.3k | let end = start + i16::RAW_BYTE_LEN; |
756 | 28.3k | start..end |
757 | 28.3k | } |
758 | | |
759 | 28.3k | pub fn x_max_byte_range(&self) -> Range<usize> { |
760 | 28.3k | let start = self.y_min_byte_range().end; |
761 | 28.3k | let end = start + i16::RAW_BYTE_LEN; |
762 | 28.3k | start..end |
763 | 28.3k | } |
764 | | |
765 | 28.3k | pub fn y_max_byte_range(&self) -> Range<usize> { |
766 | 28.3k | let start = self.x_max_byte_range().end; |
767 | 28.3k | let end = start + i16::RAW_BYTE_LEN; |
768 | 28.3k | start..end |
769 | 28.3k | } |
770 | | |
771 | 28.3k | pub fn component_data_byte_range(&self) -> Range<usize> { |
772 | 28.3k | let start = self.y_max_byte_range().end; |
773 | 28.3k | let end = start |
774 | 28.3k | + self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN; |
775 | 28.3k | start..end |
776 | 28.3k | } |
777 | | } |
778 | | |
779 | | const _: () = assert!(FontData::default_data_long_enough(CompositeGlyph::MIN_SIZE)); |
780 | | |
781 | | impl Default for CompositeGlyph<'_> { |
782 | 0 | fn default() -> Self { |
783 | | Self { |
784 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
785 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
786 | | } |
787 | 0 | } |
788 | | } |
789 | | |
790 | | /// Flags used in [CompositeGlyph] |
791 | | #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)] |
792 | | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
793 | | #[repr(transparent)] |
794 | | pub struct CompositeGlyphFlags { |
795 | | bits: u16, |
796 | | } |
797 | | |
798 | | impl CompositeGlyphFlags { |
799 | | /// Bit 0: If this is set, the arguments are 16-bit (uint16 or |
800 | | /// int16); otherwise, they are bytes (uint8 or int8). |
801 | | pub const ARG_1_AND_2_ARE_WORDS: Self = Self { bits: 0x0001 }; |
802 | | |
803 | | /// Bit 1: If this is set, the arguments are signed xy values, |
804 | | /// otherwise, they are unsigned point numbers. |
805 | | pub const ARGS_ARE_XY_VALUES: Self = Self { bits: 0x0002 }; |
806 | | |
807 | | /// Bit 2: If set and ARGS_ARE_XY_VALUES is also set, the xy values |
808 | | /// are rounded to the nearest grid line. Ignored if |
809 | | /// ARGS_ARE_XY_VALUES is not set. |
810 | | pub const ROUND_XY_TO_GRID: Self = Self { bits: 0x0004 }; |
811 | | |
812 | | /// Bit 3: This indicates that there is a simple scale for the |
813 | | /// component. Otherwise, scale = 1.0. |
814 | | pub const WE_HAVE_A_SCALE: Self = Self { bits: 0x0008 }; |
815 | | |
816 | | /// Bit 5: Indicates at least one more glyph after this one. |
817 | | pub const MORE_COMPONENTS: Self = Self { bits: 0x0020 }; |
818 | | |
819 | | /// Bit 6: The x direction will use a different scale from the y |
820 | | /// direction. |
821 | | pub const WE_HAVE_AN_X_AND_Y_SCALE: Self = Self { bits: 0x0040 }; |
822 | | |
823 | | /// Bit 7: There is a 2 by 2 transformation that will be used to |
824 | | /// scale the component. |
825 | | pub const WE_HAVE_A_TWO_BY_TWO: Self = Self { bits: 0x0080 }; |
826 | | |
827 | | /// Bit 8: Following the last component are instructions for the |
828 | | /// composite character. |
829 | | pub const WE_HAVE_INSTRUCTIONS: Self = Self { bits: 0x0100 }; |
830 | | |
831 | | /// Bit 9: If set, this forces the aw and lsb (and rsb) for the |
832 | | /// composite to be equal to those from this component glyph. This |
833 | | /// works for hinted and unhinted glyphs. |
834 | | pub const USE_MY_METRICS: Self = Self { bits: 0x0200 }; |
835 | | |
836 | | /// Bit 10: If set, the components of the compound glyph overlap. |
837 | | /// Use of this flag is not required in OpenType — that is, it is |
838 | | /// valid to have components overlap without having this flag set. |
839 | | /// It may affect behaviors in some platforms, however. (See |
840 | | /// Apple’s specification for details regarding behavior in Apple |
841 | | /// platforms.) When used, it must be set on the flag word for the |
842 | | /// first component. See additional remarks, above, for the similar |
843 | | /// OVERLAP_SIMPLE flag used in simple-glyph descriptions. |
844 | | pub const OVERLAP_COMPOUND: Self = Self { bits: 0x0400 }; |
845 | | |
846 | | /// Bit 11: The composite is designed to have the component offset |
847 | | /// scaled. Ignored if ARGS_ARE_XY_VALUES is not set. |
848 | | pub const SCALED_COMPONENT_OFFSET: Self = Self { bits: 0x0800 }; |
849 | | |
850 | | /// Bit 12: The composite is designed not to have the component |
851 | | /// offset scaled. Ignored if ARGS_ARE_XY_VALUES is not set. |
852 | | pub const UNSCALED_COMPONENT_OFFSET: Self = Self { bits: 0x1000 }; |
853 | | |
854 | | /// Bit 13: The component glyph identifier uses 24-bit encoding in GLYF |
855 | | /// (ISO/IEC 14496-22:2026, 5.2.8). |
856 | | pub const GID_IS_24_BIT: Self = Self { bits: 0x2000 }; |
857 | | } |
858 | | |
859 | | impl CompositeGlyphFlags { |
860 | | /// Returns an empty set of flags. |
861 | | #[inline] |
862 | 28.6k | pub const fn empty() -> Self { |
863 | 28.6k | Self { bits: 0 } |
864 | 28.6k | } |
865 | | |
866 | | /// Returns the set containing all flags. |
867 | | #[inline] |
868 | 84.8k | pub const fn all() -> Self { |
869 | 84.8k | Self { |
870 | 84.8k | bits: Self::ARG_1_AND_2_ARE_WORDS.bits |
871 | 84.8k | | Self::ARGS_ARE_XY_VALUES.bits |
872 | 84.8k | | Self::ROUND_XY_TO_GRID.bits |
873 | 84.8k | | Self::WE_HAVE_A_SCALE.bits |
874 | 84.8k | | Self::MORE_COMPONENTS.bits |
875 | 84.8k | | Self::WE_HAVE_AN_X_AND_Y_SCALE.bits |
876 | 84.8k | | Self::WE_HAVE_A_TWO_BY_TWO.bits |
877 | 84.8k | | Self::WE_HAVE_INSTRUCTIONS.bits |
878 | 84.8k | | Self::USE_MY_METRICS.bits |
879 | 84.8k | | Self::OVERLAP_COMPOUND.bits |
880 | 84.8k | | Self::SCALED_COMPONENT_OFFSET.bits |
881 | 84.8k | | Self::UNSCALED_COMPONENT_OFFSET.bits |
882 | 84.8k | | Self::GID_IS_24_BIT.bits, |
883 | 84.8k | } |
884 | 84.8k | } |
885 | | |
886 | | /// Returns the raw value of the flags currently stored. |
887 | | #[inline] |
888 | 536 | pub const fn bits(&self) -> u16 { |
889 | 536 | self.bits |
890 | 536 | } |
891 | | |
892 | | /// Convert from underlying bit representation, unless that |
893 | | /// representation contains bits that do not correspond to a flag. |
894 | | #[inline] |
895 | 0 | pub const fn from_bits(bits: u16) -> Option<Self> { |
896 | 0 | if (bits & !Self::all().bits()) == 0 { |
897 | 0 | Some(Self { bits }) |
898 | | } else { |
899 | 0 | None |
900 | | } |
901 | 0 | } |
902 | | |
903 | | /// Convert from underlying bit representation, dropping any bits |
904 | | /// that do not correspond to flags. |
905 | | #[inline] |
906 | 84.8k | pub const fn from_bits_truncate(bits: u16) -> Self { |
907 | 84.8k | Self { |
908 | 84.8k | bits: bits & Self::all().bits, |
909 | 84.8k | } |
910 | 84.8k | } |
911 | | |
912 | | /// Returns `true` if no flags are currently stored. |
913 | | #[inline] |
914 | 268 | pub const fn is_empty(&self) -> bool { |
915 | 268 | self.bits() == Self::empty().bits() |
916 | 268 | } |
917 | | |
918 | | /// Returns `true` if there are flags common to both `self` and `other`. |
919 | | #[inline] |
920 | 268 | pub const fn intersects(&self, other: Self) -> bool { |
921 | 268 | !(Self { |
922 | 268 | bits: self.bits & other.bits, |
923 | 268 | }) |
924 | 268 | .is_empty() |
925 | 268 | } |
926 | | |
927 | | /// Returns `true` if all of the flags in `other` are contained within `self`. |
928 | | #[inline] |
929 | 477k | pub const fn contains(&self, other: Self) -> bool { |
930 | 477k | (self.bits & other.bits) == other.bits |
931 | 477k | } |
932 | | |
933 | | /// Inserts the specified flags in-place. |
934 | | #[inline] |
935 | 0 | pub fn insert(&mut self, other: Self) { |
936 | 0 | self.bits |= other.bits; |
937 | 0 | } |
938 | | |
939 | | /// Removes the specified flags in-place. |
940 | | #[inline] |
941 | 0 | pub fn remove(&mut self, other: Self) { |
942 | 0 | self.bits &= !other.bits; |
943 | 0 | } |
944 | | |
945 | | /// Toggles the specified flags in-place. |
946 | | #[inline] |
947 | 0 | pub fn toggle(&mut self, other: Self) { |
948 | 0 | self.bits ^= other.bits; |
949 | 0 | } |
950 | | |
951 | | /// Returns the intersection between the flags in `self` and |
952 | | /// `other`. |
953 | | /// |
954 | | /// Specifically, the returned set contains only the flags which are |
955 | | /// present in *both* `self` *and* `other`. |
956 | | /// |
957 | | /// This is equivalent to using the `&` operator (e.g. |
958 | | /// [`ops::BitAnd`]), as in `flags & other`. |
959 | | /// |
960 | | /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html |
961 | | #[inline] |
962 | | #[must_use] |
963 | 0 | pub const fn intersection(self, other: Self) -> Self { |
964 | 0 | Self { |
965 | 0 | bits: self.bits & other.bits, |
966 | 0 | } |
967 | 0 | } |
968 | | |
969 | | /// Returns the union of between the flags in `self` and `other`. |
970 | | /// |
971 | | /// Specifically, the returned set contains all flags which are |
972 | | /// present in *either* `self` *or* `other`, including any which are |
973 | | /// present in both. |
974 | | /// |
975 | | /// This is equivalent to using the `|` operator (e.g. |
976 | | /// [`ops::BitOr`]), as in `flags | other`. |
977 | | /// |
978 | | /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html |
979 | | #[inline] |
980 | | #[must_use] |
981 | 0 | pub const fn union(self, other: Self) -> Self { |
982 | 0 | Self { |
983 | 0 | bits: self.bits | other.bits, |
984 | 0 | } |
985 | 0 | } |
986 | | |
987 | | /// Returns the difference between the flags in `self` and `other`. |
988 | | /// |
989 | | /// Specifically, the returned set contains all flags present in |
990 | | /// `self`, except for the ones present in `other`. |
991 | | /// |
992 | | /// It is also conceptually equivalent to the "bit-clear" operation: |
993 | | /// `flags & !other` (and this syntax is also supported). |
994 | | /// |
995 | | /// This is equivalent to using the `-` operator (e.g. |
996 | | /// [`ops::Sub`]), as in `flags - other`. |
997 | | /// |
998 | | /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html |
999 | | #[inline] |
1000 | | #[must_use] |
1001 | 0 | pub const fn difference(self, other: Self) -> Self { |
1002 | 0 | Self { |
1003 | 0 | bits: self.bits & !other.bits, |
1004 | 0 | } |
1005 | 0 | } |
1006 | | } |
1007 | | |
1008 | | impl std::ops::BitOr for CompositeGlyphFlags { |
1009 | | type Output = Self; |
1010 | | |
1011 | | /// Returns the union of the two sets of flags. |
1012 | | #[inline] |
1013 | 674 | fn bitor(self, other: CompositeGlyphFlags) -> Self { |
1014 | 674 | Self { |
1015 | 674 | bits: self.bits | other.bits, |
1016 | 674 | } |
1017 | 674 | } |
1018 | | } |
1019 | | |
1020 | | impl std::ops::BitOrAssign for CompositeGlyphFlags { |
1021 | | /// Adds the set of flags. |
1022 | | #[inline] |
1023 | 0 | fn bitor_assign(&mut self, other: Self) { |
1024 | 0 | self.bits |= other.bits; |
1025 | 0 | } |
1026 | | } |
1027 | | |
1028 | | impl std::ops::BitXor for CompositeGlyphFlags { |
1029 | | type Output = Self; |
1030 | | |
1031 | | /// Returns the left flags, but with all the right flags toggled. |
1032 | | #[inline] |
1033 | 0 | fn bitxor(self, other: Self) -> Self { |
1034 | 0 | Self { |
1035 | 0 | bits: self.bits ^ other.bits, |
1036 | 0 | } |
1037 | 0 | } |
1038 | | } |
1039 | | |
1040 | | impl std::ops::BitXorAssign for CompositeGlyphFlags { |
1041 | | /// Toggles the set of flags. |
1042 | | #[inline] |
1043 | 0 | fn bitxor_assign(&mut self, other: Self) { |
1044 | 0 | self.bits ^= other.bits; |
1045 | 0 | } |
1046 | | } |
1047 | | |
1048 | | impl std::ops::BitAnd for CompositeGlyphFlags { |
1049 | | type Output = Self; |
1050 | | |
1051 | | /// Returns the intersection between the two sets of flags. |
1052 | | #[inline] |
1053 | 138 | fn bitand(self, other: Self) -> Self { |
1054 | 138 | Self { |
1055 | 138 | bits: self.bits & other.bits, |
1056 | 138 | } |
1057 | 138 | } |
1058 | | } |
1059 | | |
1060 | | impl std::ops::BitAndAssign for CompositeGlyphFlags { |
1061 | | /// Disables all flags disabled in the set. |
1062 | | #[inline] |
1063 | 0 | fn bitand_assign(&mut self, other: Self) { |
1064 | 0 | self.bits &= other.bits; |
1065 | 0 | } |
1066 | | } |
1067 | | |
1068 | | impl std::ops::Sub for CompositeGlyphFlags { |
1069 | | type Output = Self; |
1070 | | |
1071 | | /// Returns the set difference of the two sets of flags. |
1072 | | #[inline] |
1073 | 0 | fn sub(self, other: Self) -> Self { |
1074 | 0 | Self { |
1075 | 0 | bits: self.bits & !other.bits, |
1076 | 0 | } |
1077 | 0 | } |
1078 | | } |
1079 | | |
1080 | | impl std::ops::SubAssign for CompositeGlyphFlags { |
1081 | | /// Disables all flags enabled in the set. |
1082 | | #[inline] |
1083 | 0 | fn sub_assign(&mut self, other: Self) { |
1084 | 0 | self.bits &= !other.bits; |
1085 | 0 | } |
1086 | | } |
1087 | | |
1088 | | impl std::ops::Not for CompositeGlyphFlags { |
1089 | | type Output = Self; |
1090 | | |
1091 | | /// Returns the complement of this set of flags. |
1092 | | #[inline] |
1093 | 0 | fn not(self) -> Self { |
1094 | 0 | Self { bits: !self.bits } & Self::all() |
1095 | 0 | } |
1096 | | } |
1097 | | |
1098 | | impl std::fmt::Debug for CompositeGlyphFlags { |
1099 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
1100 | 0 | let members: &[(&str, Self)] = &[ |
1101 | 0 | ("ARG_1_AND_2_ARE_WORDS", Self::ARG_1_AND_2_ARE_WORDS), |
1102 | 0 | ("ARGS_ARE_XY_VALUES", Self::ARGS_ARE_XY_VALUES), |
1103 | 0 | ("ROUND_XY_TO_GRID", Self::ROUND_XY_TO_GRID), |
1104 | 0 | ("WE_HAVE_A_SCALE", Self::WE_HAVE_A_SCALE), |
1105 | 0 | ("MORE_COMPONENTS", Self::MORE_COMPONENTS), |
1106 | 0 | ("WE_HAVE_AN_X_AND_Y_SCALE", Self::WE_HAVE_AN_X_AND_Y_SCALE), |
1107 | 0 | ("WE_HAVE_A_TWO_BY_TWO", Self::WE_HAVE_A_TWO_BY_TWO), |
1108 | 0 | ("WE_HAVE_INSTRUCTIONS", Self::WE_HAVE_INSTRUCTIONS), |
1109 | 0 | ("USE_MY_METRICS", Self::USE_MY_METRICS), |
1110 | 0 | ("OVERLAP_COMPOUND", Self::OVERLAP_COMPOUND), |
1111 | 0 | ("SCALED_COMPONENT_OFFSET", Self::SCALED_COMPONENT_OFFSET), |
1112 | 0 | ("UNSCALED_COMPONENT_OFFSET", Self::UNSCALED_COMPONENT_OFFSET), |
1113 | 0 | ("GID_IS_24_BIT", Self::GID_IS_24_BIT), |
1114 | 0 | ]; |
1115 | 0 | let mut first = true; |
1116 | 0 | for (name, value) in members { |
1117 | 0 | if self.contains(*value) { |
1118 | 0 | if !first { |
1119 | 0 | f.write_str(" | ")?; |
1120 | 0 | } |
1121 | 0 | first = false; |
1122 | 0 | f.write_str(name)?; |
1123 | 0 | } |
1124 | | } |
1125 | 0 | if first { |
1126 | 0 | f.write_str("(empty)")?; |
1127 | 0 | } |
1128 | 0 | Ok(()) |
1129 | 0 | } |
1130 | | } |
1131 | | |
1132 | | impl std::fmt::Binary for CompositeGlyphFlags { |
1133 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
1134 | 0 | std::fmt::Binary::fmt(&self.bits, f) |
1135 | 0 | } |
1136 | | } |
1137 | | |
1138 | | impl std::fmt::Octal for CompositeGlyphFlags { |
1139 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
1140 | 0 | std::fmt::Octal::fmt(&self.bits, f) |
1141 | 0 | } |
1142 | | } |
1143 | | |
1144 | | impl std::fmt::LowerHex for CompositeGlyphFlags { |
1145 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
1146 | 0 | std::fmt::LowerHex::fmt(&self.bits, f) |
1147 | 0 | } |
1148 | | } |
1149 | | |
1150 | | impl std::fmt::UpperHex for CompositeGlyphFlags { |
1151 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
1152 | 0 | std::fmt::UpperHex::fmt(&self.bits, f) |
1153 | 0 | } |
1154 | | } |
1155 | | |
1156 | | impl font_types::Scalar for CompositeGlyphFlags { |
1157 | | type Raw = <u16 as font_types::Scalar>::Raw; |
1158 | 0 | fn to_raw(self) -> Self::Raw { |
1159 | 0 | self.bits().to_raw() |
1160 | 0 | } |
1161 | 84.8k | fn from_raw(raw: Self::Raw) -> Self { |
1162 | 84.8k | let t = <u16>::from_raw(raw); |
1163 | 84.8k | Self::from_bits_truncate(t) |
1164 | 84.8k | } |
1165 | | } |
1166 | | |
1167 | | /// Simple or composite glyph. |
1168 | | #[derive(Clone)] |
1169 | | pub enum Glyph<'a> { |
1170 | | Simple(SimpleGlyph<'a>), |
1171 | | Composite(CompositeGlyph<'a>), |
1172 | | } |
1173 | | |
1174 | | impl Default for Glyph<'_> { |
1175 | 0 | fn default() -> Self { |
1176 | 0 | Self::Simple(Default::default()) |
1177 | 0 | } |
1178 | | } |
1179 | | |
1180 | | impl<'a> Glyph<'a> { |
1181 | | ///Return the `FontData` used to resolve offsets for this table. |
1182 | 0 | pub fn offset_data(&self) -> FontData<'a> { |
1183 | 0 | match self { |
1184 | 0 | Self::Simple(item) => item.offset_data(), |
1185 | 0 | Self::Composite(item) => item.offset_data(), |
1186 | | } |
1187 | 0 | } |
1188 | | |
1189 | | /// If the number of contours is greater than or equal to zero, |
1190 | | /// this is a simple glyph. If negative, this is a composite glyph |
1191 | | /// — the value -1 should be used for composite glyphs. |
1192 | 183 | pub fn number_of_contours(&self) -> i16 { |
1193 | 183 | match self { |
1194 | 129 | Self::Simple(item) => item.number_of_contours(), |
1195 | 54 | Self::Composite(item) => item.number_of_contours(), |
1196 | | } |
1197 | 183 | } |
1198 | | |
1199 | | /// Minimum x for coordinate data. |
1200 | 2.74k | pub fn x_min(&self) -> i16 { |
1201 | 2.74k | match self { |
1202 | 2.00k | Self::Simple(item) => item.x_min(), |
1203 | 739 | Self::Composite(item) => item.x_min(), |
1204 | | } |
1205 | 2.74k | } |
1206 | | |
1207 | | /// Minimum y for coordinate data. |
1208 | 130 | pub fn y_min(&self) -> i16 { |
1209 | 130 | match self { |
1210 | 76 | Self::Simple(item) => item.y_min(), |
1211 | 54 | Self::Composite(item) => item.y_min(), |
1212 | | } |
1213 | 130 | } |
1214 | | |
1215 | | /// Maximum x for coordinate data. |
1216 | 130 | pub fn x_max(&self) -> i16 { |
1217 | 130 | match self { |
1218 | 76 | Self::Simple(item) => item.x_max(), |
1219 | 54 | Self::Composite(item) => item.x_max(), |
1220 | | } |
1221 | 130 | } |
1222 | | |
1223 | | /// Maximum y for coordinate data. |
1224 | 2.74k | pub fn y_max(&self) -> i16 { |
1225 | 2.74k | match self { |
1226 | 2.00k | Self::Simple(item) => item.y_max(), |
1227 | 739 | Self::Composite(item) => item.y_max(), |
1228 | | } |
1229 | 2.74k | } |
1230 | | } |
1231 | | |
1232 | | impl ReadArgs for Glyph<'_> { |
1233 | | type Args = (); |
1234 | | } |
1235 | | |
1236 | | impl<'a> FontRead<'a> for Glyph<'a> { |
1237 | | #[inline] |
1238 | 26.1k | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
1239 | 26.1k | let format: i16 = data.read_at(0usize)?; |
1240 | | |
1241 | | #[allow(clippy::redundant_guards)] |
1242 | 26.1k | match format { |
1243 | 26.1k | format if format >= 0 => Ok(Self::Simple(FontRead::read(data)?)), |
1244 | 23.0k | format if format < 0 => Ok(Self::Composite(FontRead::read(data)?)), |
1245 | 0 | other => Err(ReadError::InvalidFormat(other.into())), |
1246 | | } |
1247 | 26.1k | } |
1248 | | } |
1249 | | |
1250 | | impl<'a> MinByteRange<'a> for Glyph<'a> { |
1251 | 0 | fn min_byte_range(&self) -> Range<usize> { |
1252 | 0 | match self { |
1253 | 0 | Self::Simple(item) => item.min_byte_range(), |
1254 | 0 | Self::Composite(item) => item.min_byte_range(), |
1255 | | } |
1256 | 0 | } |
1257 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
1258 | 0 | match self { |
1259 | 0 | Self::Simple(item) => item.min_table_bytes(), |
1260 | 0 | Self::Composite(item) => item.min_table_bytes(), |
1261 | | } |
1262 | 0 | } |
1263 | | } |