|
1 |
| -// SPDX-License-Identifier: MIT OR Apache-2.0 |
2 |
| -pub(crate) mod fallback; |
3 |
| - |
4 |
| -use alloc::boxed::Box; |
5 |
| -use alloc::sync::Arc; |
6 |
| - |
7 |
| -pub use self::system::*; |
8 |
| -mod system; |
9 |
| - |
10 |
| -pub use font_inner::Font; |
11 |
| - |
12 |
| -/// Encapsulates the self-referencing `Font` struct to ensure all field accesses have to go through |
13 |
| -/// safe methods. |
14 |
| -mod font_inner { |
15 |
| - use super::*; |
16 |
| - use aliasable::boxed::AliasableBox; |
17 |
| - use core::fmt; |
18 |
| - |
19 |
| - /// A font |
20 |
| - // |
21 |
| - // # Safety invariant |
22 |
| - // |
23 |
| - // `data` must never have a mutable reference taken, nor be modified during the lifetime of |
24 |
| - // this `Font`. |
25 |
| - pub struct Font { |
26 |
| - #[cfg(feature = "swash")] |
27 |
| - swash: (u32, swash::CacheKey), |
28 |
| - rustybuzz: rustybuzz::Face<'static>, |
29 |
| - // Note: This field must be after rustybuzz to ensure that it is dropped later. Otherwise |
30 |
| - // there would be a dangling reference when dropping rustybuzz. |
31 |
| - data: aliasable::boxed::AliasableBox<Arc<dyn AsRef<[u8]> + Send + Sync>>, |
32 |
| - id: fontdb::ID, |
33 |
| - } |
34 |
| - |
35 |
| - impl fmt::Debug for Font { |
36 |
| - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
37 |
| - f.debug_struct("Font") |
38 |
| - .field("id", &self.id) |
39 |
| - .finish_non_exhaustive() |
40 |
| - } |
41 |
| - } |
42 |
| - |
43 |
| - pub(super) struct FontTryBuilder< |
44 |
| - RustybuzzBuilder: for<'this> FnOnce( |
45 |
| - &'this Arc<dyn AsRef<[u8]> + Send + Sync>, |
46 |
| - ) -> Option<rustybuzz::Face<'this>>, |
47 |
| - > { |
48 |
| - pub(super) id: fontdb::ID, |
49 |
| - pub(super) data: Arc<dyn AsRef<[u8]> + Send + Sync>, |
50 |
| - pub(super) rustybuzz_builder: RustybuzzBuilder, |
51 |
| - #[cfg(feature = "swash")] |
52 |
| - pub(super) swash: (u32, swash::CacheKey), |
53 |
| - } |
54 |
| - impl< |
55 |
| - RustybuzzBuilder: for<'this> FnOnce( |
56 |
| - &'this Arc<dyn AsRef<[u8]> + Send + Sync>, |
57 |
| - ) -> Option<rustybuzz::Face<'this>>, |
58 |
| - > FontTryBuilder<RustybuzzBuilder> |
59 |
| - { |
60 |
| - pub(super) fn try_build(self) -> Option<Font> { |
61 |
| - unsafe fn change_lifetime<'old, 'new: 'old, T: 'new>(data: &'old T) -> &'new T { |
62 |
| - &*(data as *const _) |
63 |
| - } |
64 |
| - |
65 |
| - let data: AliasableBox<Arc<dyn AsRef<[u8]> + Send + Sync>> = |
66 |
| - AliasableBox::from_unique(Box::new(self.data)); |
67 |
| - |
68 |
| - // Safety: We use AliasableBox to allow the references in rustybuzz::Face to alias with |
69 |
| - // the data stored behind the AliasableBox. In addition the entire public interface of |
70 |
| - // Font ensures that no mutable reference is given to data. And finally we use |
71 |
| - // for<'this> for the rustybuzz_builder to ensure it can't leak a reference. Combined |
72 |
| - // this ensures that it is sound to produce a self-referential type. |
73 |
| - let rustybuzz = (self.rustybuzz_builder)(unsafe { change_lifetime(&*data) })?; |
74 |
| - |
75 |
| - Some(Font { |
76 |
| - id: self.id, |
77 |
| - data, |
78 |
| - rustybuzz, |
79 |
| - #[cfg(feature = "swash")] |
80 |
| - swash: self.swash, |
81 |
| - }) |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - impl Font { |
86 |
| - pub fn id(&self) -> fontdb::ID { |
87 |
| - self.id |
88 |
| - } |
89 |
| - |
90 |
| - pub fn data(&self) -> &[u8] { |
91 |
| - // Safety: This only gives an immutable access to `data`. |
92 |
| - (**self.data).as_ref() |
93 |
| - } |
94 |
| - |
95 |
| - pub fn rustybuzz(&self) -> &rustybuzz::Face<'_> { |
96 |
| - &self.rustybuzz |
97 |
| - } |
98 |
| - |
99 |
| - #[cfg(feature = "swash")] |
100 |
| - pub fn as_swash(&self) -> swash::FontRef<'_> { |
101 |
| - let swash = &self.swash; |
102 |
| - swash::FontRef { |
103 |
| - data: self.data(), |
104 |
| - offset: swash.0, |
105 |
| - key: swash.1, |
106 |
| - } |
107 |
| - } |
108 |
| - } |
109 |
| -} |
110 |
| - |
111 |
| -impl Font { |
112 |
| - pub fn new(info: &fontdb::FaceInfo) -> Option<Self> { |
113 |
| - #[allow(unused_variables)] |
114 |
| - let data = match &info.source { |
115 |
| - fontdb::Source::Binary(data) => Arc::clone(data), |
116 |
| - #[cfg(feature = "std")] |
117 |
| - fontdb::Source::File(path) => { |
118 |
| - log::warn!("Unsupported fontdb Source::File('{}')", path.display()); |
119 |
| - return None; |
120 |
| - } |
121 |
| - #[cfg(feature = "std")] |
122 |
| - fontdb::Source::SharedFile(_path, data) => Arc::clone(data), |
123 |
| - }; |
124 |
| - font_inner::FontTryBuilder { |
125 |
| - id: info.id, |
126 |
| - #[cfg(feature = "swash")] |
127 |
| - swash: { |
128 |
| - let swash = swash::FontRef::from_index((*data).as_ref(), info.index as usize)?; |
129 |
| - (swash.offset, swash.key) |
130 |
| - }, |
131 |
| - data, |
132 |
| - rustybuzz_builder: |data| rustybuzz::Face::from_slice((**data).as_ref(), info.index), |
133 |
| - } |
134 |
| - .try_build() |
135 |
| - } |
136 |
| -} |
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | +pub(crate) mod fallback; |
| 3 | + |
| 4 | +use alloc::boxed::Box; |
| 5 | +use alloc::sync::Arc; |
| 6 | + |
| 7 | +pub use self::system::*; |
| 8 | +mod system; |
| 9 | + |
| 10 | +pub use font_inner::Font; |
| 11 | + |
| 12 | +/// Encapsulates the self-referencing `Font` struct to ensure all field accesses have to go through |
| 13 | +/// safe methods. |
| 14 | +mod font_inner { |
| 15 | + use super::*; |
| 16 | + use aliasable::boxed::AliasableBox; |
| 17 | + use core::fmt; |
| 18 | + |
| 19 | + /// A font |
| 20 | + // |
| 21 | + // # Safety invariant |
| 22 | + // |
| 23 | + // `data` must never have a mutable reference taken, nor be modified during the lifetime of |
| 24 | + // this `Font`. |
| 25 | + pub struct Font { |
| 26 | + #[cfg(feature = "swash")] |
| 27 | + swash: (u32, swash::CacheKey), |
| 28 | + rustybuzz: rustybuzz::Face<'static>, |
| 29 | + // Note: This field must be after rustybuzz to ensure that it is dropped later. Otherwise |
| 30 | + // there would be a dangling reference when dropping rustybuzz. |
| 31 | + data: aliasable::boxed::AliasableBox<Arc<dyn AsRef<[u8]> + Send + Sync>>, |
| 32 | + id: fontdb::ID, |
| 33 | + } |
| 34 | + |
| 35 | + impl fmt::Debug for Font { |
| 36 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 37 | + f.debug_struct("Font") |
| 38 | + .field("id", &self.id) |
| 39 | + .finish_non_exhaustive() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + pub(super) struct FontTryBuilder< |
| 44 | + RustybuzzBuilder: for<'this> FnOnce( |
| 45 | + &'this Arc<dyn AsRef<[u8]> + Send + Sync>, |
| 46 | + ) -> Option<rustybuzz::Face<'this>>, |
| 47 | + > { |
| 48 | + pub(super) id: fontdb::ID, |
| 49 | + pub(super) data: Arc<dyn AsRef<[u8]> + Send + Sync>, |
| 50 | + pub(super) rustybuzz_builder: RustybuzzBuilder, |
| 51 | + #[cfg(feature = "swash")] |
| 52 | + pub(super) swash: (u32, swash::CacheKey), |
| 53 | + } |
| 54 | + impl< |
| 55 | + RustybuzzBuilder: for<'this> FnOnce( |
| 56 | + &'this Arc<dyn AsRef<[u8]> + Send + Sync>, |
| 57 | + ) -> Option<rustybuzz::Face<'this>>, |
| 58 | + > FontTryBuilder<RustybuzzBuilder> |
| 59 | + { |
| 60 | + pub(super) fn try_build(self) -> Option<Font> { |
| 61 | + unsafe fn change_lifetime<'old, 'new: 'old, T: 'new>(data: &'old T) -> &'new T { |
| 62 | + &*(data as *const _) |
| 63 | + } |
| 64 | + |
| 65 | + let data: AliasableBox<Arc<dyn AsRef<[u8]> + Send + Sync>> = |
| 66 | + AliasableBox::from_unique(Box::new(self.data)); |
| 67 | + |
| 68 | + // Safety: We use AliasableBox to allow the references in rustybuzz::Face to alias with |
| 69 | + // the data stored behind the AliasableBox. In addition the entire public interface of |
| 70 | + // Font ensures that no mutable reference is given to data. And finally we use |
| 71 | + // for<'this> for the rustybuzz_builder to ensure it can't leak a reference. Combined |
| 72 | + // this ensures that it is sound to produce a self-referential type. |
| 73 | + let rustybuzz = (self.rustybuzz_builder)(unsafe { change_lifetime(&*data) })?; |
| 74 | + |
| 75 | + Some(Font { |
| 76 | + id: self.id, |
| 77 | + data, |
| 78 | + rustybuzz, |
| 79 | + #[cfg(feature = "swash")] |
| 80 | + swash: self.swash, |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + impl Font { |
| 86 | + pub fn id(&self) -> fontdb::ID { |
| 87 | + self.id |
| 88 | + } |
| 89 | + |
| 90 | + pub fn data(&self) -> &[u8] { |
| 91 | + // Safety: This only gives an immutable access to `data`. |
| 92 | + (**self.data).as_ref() |
| 93 | + } |
| 94 | + |
| 95 | + pub fn rustybuzz(&self) -> &rustybuzz::Face<'_> { |
| 96 | + &self.rustybuzz |
| 97 | + } |
| 98 | + |
| 99 | + #[cfg(feature = "swash")] |
| 100 | + pub fn as_swash(&self) -> swash::FontRef<'_> { |
| 101 | + let swash = &self.swash; |
| 102 | + swash::FontRef { |
| 103 | + data: self.data(), |
| 104 | + offset: swash.0, |
| 105 | + key: swash.1, |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl Font { |
| 112 | + pub fn new(info: &fontdb::FaceInfo) -> Option<Self> { |
| 113 | + #[allow(unused_variables)] |
| 114 | + let data = match &info.source { |
| 115 | + fontdb::Source::Binary(data) => Arc::clone(data), |
| 116 | + #[cfg(feature = "std")] |
| 117 | + fontdb::Source::File(path) => { |
| 118 | + log::warn!("Unsupported fontdb Source::File('{}')", path.display()); |
| 119 | + return None; |
| 120 | + } |
| 121 | + }; |
| 122 | + font_inner::FontTryBuilder { |
| 123 | + id: info.id, |
| 124 | + #[cfg(feature = "swash")] |
| 125 | + swash: { |
| 126 | + let swash = swash::FontRef::from_index((*data).as_ref(), info.index as usize)?; |
| 127 | + (swash.offset, swash.key) |
| 128 | + }, |
| 129 | + data, |
| 130 | + rustybuzz_builder: |data| rustybuzz::Face::from_slice((**data).as_ref(), info.index), |
| 131 | + } |
| 132 | + .try_build() |
| 133 | + } |
| 134 | +} |
0 commit comments