Commit d870325e authored by Alex Butler's avatar Alex Butler

Fix doc tests & no-std build

parent 3076234b
......@@ -28,7 +28,7 @@ features = ["gpu_cache"]
[dependencies]
ordered-float = { version = "1", default-features = false }
approx = { version = "0.3", default-features = false }
ttf-parser = "0.4"
ttf-parser = { version = "0.4", default-features = false }
libm = { version = "0.2.1", default-features = false, optional = true }
......@@ -44,9 +44,9 @@ num_cpus = { version = "1.0", optional = true }
# don't add any, instead use ./dev
[features]
default = ["std", "has-atomics"]
default = ["std"]
# Activates usage of std.
std = ["has-atomics"]
std = ["has-atomics", "ttf-parser/default"]
# Uses libm when not using std. This needs to be active in that case.
libm-math = ["libm"]
# Some targets don't have atomics, this activates usage of Arc<T> instead of Rc<T>.
......
......@@ -39,25 +39,20 @@ impl fmt::Debug for Font<'_> {
}
impl Font<'_> {
/// Constructs a font from a byte-slice.
/// Creates a Font from byte-slice data.
///
/// Returns `None` for invalid data.
pub fn try_from_bytes(bytes: &[u8]) -> Option<Font<'_>> {
Self::try_from_bytes_and_index(bytes, 0)
}
/// Creates a Font from byte-slice data & a font collection `index`.
///
/// Returns `None` for invalid data.
pub fn try_from_bytes_and_index(bytes: &[u8], index: u32) -> Option<Font<'_>> {
let inner = Arc::new(ttf_parser::Font::from_data(bytes, index)?);
Some(Font::Ref(inner))
}
/// Constructs a font that owns it's data.
pub fn try_from_vec(data: Vec<u8>) -> Option<Font<'static>> {
Self::try_from_vec_and_index(data, 0)
}
pub fn try_from_vec_and_index(data: Vec<u8>, index: u32) -> Option<Font<'static>> {
let inner = owned_ttf_parser::VecFont::try_from_vec(data, index)?;
Some(Font::Owned(inner))
}
}
impl<'font> Font<'font> {
......@@ -225,14 +220,37 @@ impl<'font> Font<'font> {
}
}
/// Functionality to allow owned font data using ttf-parser.
///
/// This requires _unsafe_ usage to implement pinned self referencing, as ttf-parser does not
/// currently support owned data directly.
mod owned_ttf_parser {
use alloc::sync::Arc;
use super::{Font, Arc};
use core::marker::PhantomPinned;
use core::pin::Pin;
use core::slice;
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec::Vec};
pub type OwnedFont = Pin<Box<VecFont>>;
impl Font<'_> {
/// Creates a Font from owned font data.
///
/// Returns `None` for invalid data.
pub fn try_from_vec(data: Vec<u8>) -> Option<Font<'static>> {
Self::try_from_vec_and_index(data, 0)
}
/// Creates a Font from owned font data & a font collection `index`.
///
/// Returns `None` for invalid data.
pub fn try_from_vec_and_index(data: Vec<u8>, index: u32) -> Option<Font<'static>> {
let inner = VecFont::try_from_vec(data, index)?;
Some(Font::Owned(inner))
}
}
pub struct VecFont {
data: Vec<u8>,
font: Option<ttf_parser::Font<'static>>,
......@@ -240,6 +258,7 @@ mod owned_ttf_parser {
}
impl VecFont {
/// Creates an underlying font object from owned data.
pub fn try_from_vec(data: Vec<u8>, index: u32) -> Option<Arc<Pin<Box<Self>>>> {
let font = Self {
data,
......@@ -248,7 +267,7 @@ mod owned_ttf_parser {
};
let mut b = Box::pin(font);
unsafe {
// 'static lifetime is a lie this data is owned
// 'static lifetime is a lie, this data is owned, it has pseudo-self lifetime.
let slice: &'static [u8] = slice::from_raw_parts(b.data.as_ptr(), b.data.len());
let mut_ref: Pin<&mut Self> = Pin::as_mut(&mut b);
let mut_inner = mut_ref.get_unchecked_mut();
......@@ -257,7 +276,8 @@ mod owned_ttf_parser {
Some(Arc::new(b))
}
// Note: Must not leak the fake 'static lifetime
// Must not leak the fake 'static lifetime that we lied about earlier to the compiler.
// Since the lifetime 'a will not outlive our owned data it's safe to provide Font<'a>
#[inline]
pub fn inner_ref<'a>(self: &'a Pin<Box<Self>>) -> &'a ttf_parser::Font<'a> {
match self.font.as_ref() {
......
......@@ -45,7 +45,7 @@
//! # use std::error::Error;
//! # fn example() -> Result<(), Box<dyn Error>> {
//! # let font_data: &[u8] = include_bytes!("../dev/fonts/dejavu/DejaVuSansMono.ttf");
//! # let font: Font<'static> = Font::from_bytes(font_data)?;
//! # let font: Font<'static> = Font::try_from_bytes(font_data).unwrap();
//! # let glyph = font.glyph('a').scaled(Scale::uniform(25.0)).positioned(point(0.0, 0.0));
//! # let glyph2 = glyph.clone();
//! # fn update_gpu_texture(_: rusttype::Rect<u32>, _: &[u8]) {};
......
......@@ -116,7 +116,7 @@ use core::fmt;
#[cfg(all(feature = "libm-math", not(feature = "std")))]
use crate::nostd_float::FloatExt;
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec::Vec};
use alloc::vec::Vec;
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct GlyphId(pub u16);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment