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