owned_ttf_parser/
owned.rs
1use crate::preparse::{FaceSubtables, PreParsedSubtables};
2#[cfg(not(feature = "std"))]
3use alloc::{boxed::Box, vec::Vec};
4use core::{fmt, marker::PhantomPinned, mem, pin::Pin, slice};
5
6pub struct OwnedFace(Pin<Box<SelfRefVecFace>>);
8
9impl OwnedFace {
10 pub fn from_vec(data: Vec<u8>, index: u32) -> Result<Self, ttf_parser::FaceParsingError> {
22 let inner = SelfRefVecFace::try_from_vec(data, index)?;
23 Ok(Self(inner))
24 }
25
26 pub(crate) fn pre_parse_subtables(self) -> PreParsedSubtables<'static, Self> {
27 let subtables = FaceSubtables::from(match self.0.face.as_ref() {
29 Some(f) => f,
30 None => unsafe { core::hint::unreachable_unchecked() },
31 });
32
33 PreParsedSubtables {
35 face: self,
36 subtables,
37 }
38 }
39
40 pub fn as_slice(&self) -> &[u8] {
51 &self.0.data
52 }
53
54 pub fn into_vec(self) -> Vec<u8> {
65 self.0.into_vec()
66 }
67}
68
69impl fmt::Debug for OwnedFace {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 write!(f, "OwnedFace()")
72 }
73}
74
75impl crate::convert::AsFaceRef for OwnedFace {
76 #[inline]
77 fn as_face_ref(&self) -> &ttf_parser::Face<'_> {
78 self.0.inner_ref()
79 }
80}
81
82impl crate::convert::AsFaceRef for &OwnedFace {
83 #[inline]
84 fn as_face_ref(&self) -> &ttf_parser::Face<'_> {
85 self.0.inner_ref()
86 }
87}
88
89impl crate::convert::FaceMut for OwnedFace {
90 fn set_variation(&mut self, axis: ttf_parser::Tag, value: f32) -> Option<()> {
91 unsafe {
92 let mut_ref = Pin::as_mut(&mut self.0);
93 let mut_inner = mut_ref.get_unchecked_mut();
94 match mut_inner.face.as_mut() {
95 Some(face) => face.set_variation(axis, value),
96 None => None,
97 }
98 }
99 }
100}
101impl crate::convert::FaceMut for &mut OwnedFace {
102 #[inline]
103 fn set_variation(&mut self, axis: ttf_parser::Tag, value: f32) -> Option<()> {
104 (*self).set_variation(axis, value)
105 }
106}
107
108struct SelfRefVecFace {
110 data: Vec<u8>,
111 face: Option<ttf_parser::Face<'static>>,
112 _pin: PhantomPinned,
113}
114
115impl SelfRefVecFace {
116 fn try_from_vec(
118 data: Vec<u8>,
119 index: u32,
120 ) -> Result<Pin<Box<Self>>, ttf_parser::FaceParsingError> {
121 let face = Self {
122 data,
123 face: None,
124 _pin: PhantomPinned,
125 };
126 let mut b = Box::pin(face);
127 unsafe {
128 let slice: &'static [u8] = slice::from_raw_parts(b.data.as_ptr(), b.data.len());
130 let mut_ref: Pin<&mut Self> = Pin::as_mut(&mut b);
131 let mut_inner = mut_ref.get_unchecked_mut();
132 mut_inner.face = Some(ttf_parser::Face::parse(slice, index)?);
133 }
134 Ok(b)
135 }
136
137 #[inline]
141 #[allow(clippy::needless_lifetimes)] fn inner_ref<'a>(self: &'a Pin<Box<Self>>) -> &'a ttf_parser::Face<'a> {
143 unsafe { self.face.as_ref().unwrap_unchecked() }
145 }
146
147 fn into_vec(self: Pin<Box<Self>>) -> Vec<u8> {
148 let mut me = unsafe { Pin::into_inner_unchecked(self) };
150 me.face.take(); mem::take(&mut me.data)
152 }
153}
154
155impl Drop for SelfRefVecFace {
156 fn drop(&mut self) {
157 self.face.take(); }
159}