summaryrefslogtreecommitdiff
path: root/src/font.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/font.rs')
-rw-r--r--src/font.rs41
1 files changed, 22 insertions, 19 deletions
diff --git a/src/font.rs b/src/font.rs
index 1e34ac05..539f3188 100644
--- a/src/font.rs
+++ b/src/font.rs
@@ -1,25 +1,34 @@
//! Font handling.
use std::cell::RefCell;
-use std::ops::Deref;
use std::rc::Rc;
-use fontdock::{ContainsChar, FaceFromVec, FontLoader, FontProvider};
+use fontdock::{ContainsChar, FaceFromVec, FontProvider};
use ttf_parser::Face;
-/// A referenced-count shared font loader backed by a dynamic provider.
-pub type SharedFontLoader = Rc<RefCell<FontLoader<Box<DynProvider>>>>;
+/// A reference-counted shared font loader backed by a dynamic font provider.
+pub type SharedFontLoader = Rc<RefCell<FontLoader>>;
-/// The dynamic font provider type backing the font loader.
+/// A font loader backed by a dynamic provider.
+pub type FontLoader = fontdock::FontLoader<Box<DynProvider>>;
+
+/// The dynamic font provider backing the font loader.
pub type DynProvider = dyn FontProvider<Face = OwnedFace>;
/// An owned font face.
pub struct OwnedFace {
- data: Vec<u8>,
+ data: Box<[u8]>,
face: Face<'static>,
}
impl OwnedFace {
+ /// Get a reference to the underlying face.
+ pub fn get<'a>(&'a self) -> &'a Face<'a> {
+ // We can't implement Deref because that would leak the internal 'static
+ // lifetime.
+ &self.face
+ }
+
/// The raw face data.
pub fn data(&self) -> &[u8] {
&self.data
@@ -28,13 +37,15 @@ impl OwnedFace {
impl FaceFromVec for OwnedFace {
fn from_vec(vec: Vec<u8>, i: u32) -> Option<Self> {
- // The vec's location is stable in memory since we don't touch it and
- // it can't be touched from outside this type.
+ let data = vec.into_boxed_slice();
+
+ // SAFETY: The slices's location is stable in memory since we don't
+ // touch it and it can't be touched from outside this type.
let slice: &'static [u8] =
- unsafe { std::slice::from_raw_parts(vec.as_ptr(), vec.len()) };
+ unsafe { std::slice::from_raw_parts(data.as_ptr(), data.len()) };
Some(Self {
- data: vec,
+ data,
face: Face::from_slice(slice, i).ok()?,
})
}
@@ -42,14 +53,6 @@ impl FaceFromVec for OwnedFace {
impl ContainsChar for OwnedFace {
fn contains_char(&self, c: char) -> bool {
- self.glyph_index(c).is_some()
- }
-}
-
-impl Deref for OwnedFace {
- type Target = Face<'static>;
-
- fn deref(&self) -> &Self::Target {
- &self.face
+ self.get().glyph_index(c).is_some()
}
}