Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make allsorts run on no_std #42

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,29 @@ keywords = ["font", "shaping", "opentype", "truetype", "parse"]
categories = ["text-processing"]

[dependencies]
bitflags = "1.0"
bitreader = "0.3.2"
brotli-decompressor = "2.3"
byteorder = "1.2"
encoding_rs = "0.8.16"
flate2 = { version = "1.0", default-features = false, features = ["zlib"] }
glyph-names = "0.1"
itertools = "0.8"
lazy_static = "1.3.0"
libc = "0.2"
log = "0.4"
num-traits = "0.2"
rental = "0.5.5"
rustc-hash = "1.1.0"
tinyvec = { version = "1", features = ["alloc"] }
ucd-trie = "0.1.2"
unicode-general-category = "0.3"
unicode-joining-type = "0.4.0"
bitflags = { version = "1.0", default-features = false }
bitreader = { version = "0.3.2", default-features = false }
brotli-decompressor = { version = "2.3", default-features = false }
byteorder = { version = "1.2", default-features = false }
encoding_rs = { version = "0.8.27", default-features = false }
miniz_oxide = { version = "0.4.3", default-features = false }
glyph-names = { version = "0.1", default-features = false }
itertools = { version = "0.8", default-features = false }
lazy_static = { version = "1.3.0", default-features = false }
libc = { version = "0.2", default-features = false }
log = { version = "0.4", default-features = false }
num-traits = { version = "0.2", default-features = false }
allsorts-rental = { version = "0.5.6", default-features = false, features = ["alloc"] }
rustc-hash = { version = "1.1.0", default-features = false, features = ["std"], optional = true } # note: feature "std" disabled!
tinyvec = { version = "1", default-features = false, features = ["alloc"] }
ucd-trie = { version = "0.1.2", default-features = false }
unicode-general-category = { version = "0.3.0", default-features = false }
unicode-joining-type = { version = "0.5.0", default-features = false }
alloc-no-stdlib = { version = "2.0.1", default-features = false }

[dev-dependencies]
criterion = "0.3"
regex = "1.1.6"
criterion = { version = "0.3", default-features = false }
regex = { version = "1.1.6", default-features = false }

[[bench]]
name = "bench-shape"
Expand All @@ -49,4 +50,6 @@ name = "bench-read"
harness = false

[features]
prince = []
std = ["rustc-hash", "allsorts-rental/std"]
prince = ["std"]
default = ["std"]
20 changes: 11 additions & 9 deletions src/binary/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ use crate::binary::{I16Be, I32Be, I64Be, U16Be, U24Be, U32Be, I8, U8};
use crate::error::ParseError;
use crate::layout::{LayoutCache, LayoutTableType};
use crate::size;
use std::borrow::Cow;
use std::cmp;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt;
use std::marker::PhantomData;
use std::rc::Rc;
use alloc::borrow::Cow;
use alloc::vec::Vec;
use alloc::boxed::Box;
use core::cmp;
use alloc::collections::btree_map::Entry;
use alloc::collections::BTreeMap;
use core::fmt;
use core::marker::PhantomData;
use alloc::rc::Rc;

#[derive(Debug, Copy, Clone)]
pub struct ReadEof {}
Expand Down Expand Up @@ -59,7 +61,7 @@ pub struct ReadCtxt<'a> {
}

pub struct ReadCache<T> {
map: HashMap<usize, Rc<T>>,
map: BTreeMap<usize, Rc<T>>,
}

pub trait ReadBinary<'a> {
Expand Down Expand Up @@ -317,7 +319,7 @@ impl<'a> ReadScope<'a> {

impl<T> ReadCache<T> {
pub fn new() -> Self {
let map = HashMap::new();
let map = BTreeMap::new();
ReadCache { map }
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/binary/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

//! Write binary data

use std::iter;
use std::marker::PhantomData;
use core::iter;
use core::marker::PhantomData;
use alloc::vec::Vec;

use crate::binary::read::{ReadArray, ReadArrayCow, ReadScope, ReadUnchecked};
use crate::binary::{I16Be, I32Be, I64Be, U16Be, U24Be, U32Be, I8, U8};
Expand Down Expand Up @@ -521,7 +522,7 @@ mod tests {
assert_eq!(ctxt.bytes(), &[1, 2, 3]);

// Check out of range value
match U24Be::write(&mut ctxt, std::u32::MAX) {
match U24Be::write(&mut ctxt, core::u32::MAX) {
Err(WriteError::BadValue) => {}
_ => panic!("Expected WriteError::BadValue"),
}
Expand Down
1 change: 1 addition & 0 deletions src/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod sbix;
use num_traits as num;

use crate::error::ParseError;
use alloc::boxed::Box;

/// Bit depth of bitmap data.
#[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd)]
Expand Down
22 changes: 11 additions & 11 deletions src/bitmap/cbdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

//! Bitmap fonts in `EBLC`/`EBDT` and `CBLC`/`CBDT` tables.

use std::convert::TryFrom;
use std::fmt;
use core::convert::TryFrom;
use core::fmt;
use alloc::vec::Vec;
use alloc::boxed::Box;

use bitreader::{BitReader, BitReaderError};

Expand Down Expand Up @@ -1475,18 +1477,16 @@ fn bgra_to_rgba(bit_depth: BitDepth, mut data: Vec<u8>) -> Result<Vec<u8>, Parse
#[cfg(test)]
mod tests {
use itertools::Itertools;
use std::borrow::Borrow;
use std::path::Path;
use alloc::borrow::Borrow;

use super::*;
use crate::font_data::FontData;
use crate::tables::FontTableProvider;
use crate::tag;
use crate::tests::read_fixture;

#[test]
fn test_parse_cblc() {
let cblc_data = read_fixture(Path::new("tests/fonts/opentype/CBLC.bin"));
let cblc_data = include_bytes!("../../tests/fonts/opentype/CBLC.bin");
let cblc = ReadScope::new(&cblc_data).read::<CBLCTable<'_>>().unwrap();

let strikes = &cblc.bitmap_sizes;
Expand All @@ -1496,13 +1496,13 @@ mod tests {
.index_sub_table_records
.iter()
.map(|rec| rec.first_glyph_index..=rec.last_glyph_index)
.collect_vec();
.collect::<Vec<_>>();
assert_eq!(ranges, &[4..=17, 19..=1316, 1354..=3112]);
}

#[test]
fn test_parse_eblc() {
let buffer = read_fixture(Path::new("tests/fonts/opentype/TerminusTTF-4.47.0.ttf"));
let buffer = include_bytes!("../../tests/fonts/opentype/TerminusTTF-4.47.0.ttf");
let scope = ReadScope::new(&buffer);
let font_file = scope
.read::<FontData<'_>>()
Expand All @@ -1523,7 +1523,7 @@ mod tests {

#[test]
fn test_lookup_eblc() {
let buffer = read_fixture(Path::new("tests/fonts/opentype/TerminusTTF-4.47.0.ttf"));
let buffer = include_bytes!("../../tests/fonts/opentype/TerminusTTF-4.47.0.ttf");
let scope = ReadScope::new(&buffer);
let font_file = scope
.read::<FontData<'_>>()
Expand Down Expand Up @@ -1559,9 +1559,9 @@ mod tests {
#[test]
fn test_lookup_cblc() {
// Test tables are from Noto Color Emoji
let cblc_data = read_fixture(Path::new("tests/fonts/opentype/CBLC.bin"));
let cblc_data = include_bytes!("../../tests/fonts/opentype/CBLC.bin");
let cblc = ReadScope::new(&cblc_data).read::<CBLCTable<'_>>().unwrap();
let cbdt_data = read_fixture(Path::new("tests/fonts/opentype/CBDT.bin"));
let cbdt_data = include_bytes!("../../tests/fonts/opentype/CBDT.bin");
let cbdt = ReadScope::new(&cbdt_data).read::<CBDTTable<'_>>().unwrap();

// Glyph 1077 is Nerd Face U+1F913
Expand Down
4 changes: 3 additions & 1 deletion src/bitmap/sbix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
//! * [Microsoft](https://docs.microsoft.com/en-us/typography/opentype/spec/sbix)
//! * [Apple](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6sbix.html)

use std::convert::TryFrom;
use core::convert::TryFrom;
use alloc::vec::Vec;
use alloc::boxed::Box;

use super::{
BitDepth, Bitmap, BitmapGlyph, EncapsulatedBitmap, EncapsulatedFormat, Metrics, OriginOffset,
Expand Down
30 changes: 19 additions & 11 deletions src/cff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
//! Refer to [Technical Note #5176](http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/font/pdfs/5176.CFF.pdf)
//! for more information.

use std::convert::{TryFrom, TryInto};
use std::iter;
use std::marker::PhantomData;
use std::mem;
use core::convert::{TryFrom, TryInto};
use alloc::borrow::ToOwned;
use core::iter;
use core::marker::PhantomData;
use core::mem;
use alloc::vec::Vec;
use alloc::boxed::Box;
use alloc::string::String;

use byteorder::{BigEndian, ByteOrder};
use itertools::Itertools;
Expand Down Expand Up @@ -113,6 +117,8 @@ pub struct MaybeOwnedIndexIterator<'a> {
}

mod owned {

use alloc::vec::Vec;
use super::{TryFrom, U16Be, WriteBinary, WriteContext, WriteError, U8};

#[derive(Clone)]
Expand Down Expand Up @@ -447,7 +453,7 @@ impl<'a> WriteBinary<&Self> for CFF<'a> {
fn write<C: WriteContext>(ctxt: &mut C, cff: &CFF<'a>) -> Result<(), WriteError> {
Header::write(ctxt, &cff.header)?;
Index::write(ctxt, &cff.name_index)?;
let top_dicts = cff.fonts.iter().map(|font| &font.top_dict).collect_vec();
let top_dicts= cff.fonts.iter().map(|font| &font.top_dict).collect::<Vec<_>>();
let top_dict_index_length =
Index::calculate_size::<TopDict, _>(top_dicts.as_slice(), DictDelta::new())?;
let top_dict_index_placeholder = ctxt.reserve::<Index<'_>, _>(top_dict_index_length)?;
Expand Down Expand Up @@ -610,6 +616,8 @@ fn read_string_index_string<'a>(
string_index: &MaybeOwnedIndex<'a>,
sid: SID,
) -> Result<String, ParseError> {
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
let sid = usize::from(sid);
// When the client needs to determine the string that corresponds to a particular SID it
// performs the following: test if SID is in standard range then fetch from internal table,
Expand Down Expand Up @@ -3024,7 +3032,7 @@ mod tests {
let format0_encoding = ctxt.read::<CustomEncoding<'_>>().unwrap();
match format0_encoding {
CustomEncoding::Format0 { codes } => {
assert_eq!(codes.iter().collect_vec(), vec![4, 5, 6])
assert_eq!(codes.iter().collect::<Vec<_>>(), vec![4, 5, 6])
}
_ => panic!("expected CustomEncoding::Format0 got something else"),
}
Expand All @@ -3037,7 +3045,7 @@ mod tests {
let format1_encoding = ctxt.read::<CustomEncoding<'_>>().unwrap();
match format1_encoding {
CustomEncoding::Format1 { ranges } => assert_eq!(
ranges.iter().collect_vec(),
ranges.iter().collect::<Vec<_>>(),
vec![
Range {
first: 4,
Expand All @@ -3061,7 +3069,7 @@ mod tests {
let format0_charset = ctxt.read_dep::<CustomCharset<'_>>(n_glyphs).unwrap();
match format0_charset {
CustomCharset::Format0 { glyphs } => {
assert_eq!(glyphs.iter().collect_vec(), vec![0xAABB])
assert_eq!(glyphs.iter().collect::<Vec<_>>(), vec![0xAABB])
}
_ => panic!("expected CustomCharset::Format0 got something else"),
}
Expand All @@ -3075,7 +3083,7 @@ mod tests {
let format1_charset = ctxt.read_dep::<CustomCharset<'_>>(n_glyphs).unwrap();
match format1_charset {
CustomCharset::Format1 { ranges } => assert_eq!(
ranges.iter().collect_vec(),
ranges.iter().collect::<Vec<_>>(),
vec![Range {
first: 1,
n_left: 3
Expand All @@ -3093,7 +3101,7 @@ mod tests {
let format2_charset = ctxt.read_dep::<CustomCharset<'_>>(n_glyphs).unwrap();
match format2_charset {
CustomCharset::Format2 { ranges } => assert_eq!(
ranges.iter().collect_vec(),
ranges.iter().collect::<Vec<_>>(),
vec![Range {
first: 1,
n_left: 3
Expand Down Expand Up @@ -3311,7 +3319,7 @@ mod tests {
Range { first: 2972, n_left: 2, },
]);
let charset = CustomCharset::Format2 { ranges };
let actual = charset.iter().collect_vec();
let actual = charset.iter().collect::<Vec<_>>();
let expected = vec![0, 111, 112, 113, 114, 115, 1, 2, 3, 4, 2972, 2973, 2974];

assert_eq!(actual, expected);
Expand Down
2 changes: 1 addition & 1 deletion src/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Checksum calculation routines.

use std::num::Wrapping;
use core::num::Wrapping;

use crate::binary::read::ReadScope;
use crate::binary::U32Be;
Expand Down
4 changes: 2 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Utilities for performing contextual lookup in gpos and gsub.

use std::marker::PhantomData;
use std::rc::Rc;
use core::marker::PhantomData;
use alloc::rc::Rc;

use crate::gdef;
use crate::layout::{ClassDef, Coverage, GDEFTable};
Expand Down
Loading