diff --git a/Cargo.toml b/Cargo.toml index 3fab74d90..8b660fcce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,6 @@ leb128 = "0.2" libc = "0.2" log = "0.4" open = "1.2" -rental = "0.5" native-tls = "0.2" # file change notification notify = "4.0" diff --git a/src/font/parser.rs b/src/font/parser.rs index 2ed4914f6..c887628c7 100644 --- a/src/font/parser.rs +++ b/src/font/parser.rs @@ -5,6 +5,7 @@ use crate::config::Config; use crate::config::FontAttributes; use crate::font::locator::FontDataHandle; +use allsorts::binary::read::{ReadScope, ReadScopeOwned}; use allsorts::fontfile::FontFile; use allsorts::tables::{ FontTableProvider, HeadTable, MaxpTable, NameTable, OffsetTable, OpenTypeFont, TTCHeader, @@ -16,8 +17,11 @@ use std::path::{Path, PathBuf}; /// Represents a parsed font pub struct ParsedFont { index: usize, - file: gah::OwnedFontFile, + file: FontFile<'static>, names: Names, + + // Must be last: this keeps the 'static items alive + _scope: ReadScopeOwned, } #[derive(Debug)] @@ -97,15 +101,20 @@ impl ParsedFont { let index = index as usize; - let file = parse(data)?; + let owned_scope = ReadScopeOwned::new(ReadScope::new(&data)); - let names = file.rent(|file| -> Fallible { - let name_table = name_table_data(file, index)?; - let names = Names::from_name_table_data(name_table)?; - Ok(names) - })?; + let file: FontFile<'static> = + unsafe { std::mem::transmute(owned_scope.scope().read::()?) }; - Ok(Self { index, file, names }) + let name_table = name_table_data(&file, index)?; + let names = Names::from_name_table_data(name_table)?; + + Ok(Self { + index, + file, + names, + _scope: owned_scope, + }) } pub fn names(&self) -> &Names { @@ -113,30 +122,6 @@ impl ParsedFont { } } -rental! { - /// This is horrible, but I don't see an obvious way to - /// end up with an owned FontFile instance from the standard - /// set of functions exported by allsorts - mod gah { - use super::*; - #[rental] - pub struct OwnedFontFile { - head: Vec, - file: FontFile<'head>, - } - } -} - -/// Parse bytes into an OwnedFontFile -fn parse(data: Vec) -> Fallible { - gah::OwnedFontFile::try_new(data, |data| { - let scope = allsorts::binary::read::ReadScope::new(&data); - let file = scope.read::()?; - Ok(file) - }) - .map_err(|e| e.0) -} - fn collect_font_info( name_table_data: &[u8], path: &Path, diff --git a/src/main.rs b/src/main.rs index 833ff363f..011756e72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,6 @@ // Don't create a new standard console window when launched from the windows GUI. #![windows_subsystem = "windows"] -#[macro_use] -extern crate rental; - use failure::{err_msg, format_err, Error, Fallible}; use promise::Future; use std::ffi::OsString;