Skip to content

Commit 5d4a2bc

Browse files
authored
feat: add an alloc feature for no-std builds with an allocator (#214)
Name::to_string and the gvar-alloc heap spill required std purely for the allocator. Breaking: gvar-alloc now implies alloc rather than std, so it no longer transitively satisfies the std/no-std-float guard.
1 parent 071f0b2 commit 5d4a2bc

4 files changed

Lines changed: 19 additions & 15 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ exclude = ["benches/", "tests/", "testing-tools/", "examples/", "meson.build"]
2222
core_maths = { version = "0.1.1", optional = true } # only for no_std builds
2323

2424
[features]
25-
default = ["std", "opentype-layout", "apple-layout", "variable-fonts", "glyph-names"]
25+
default = ["std", "alloc", "opentype-layout", "apple-layout", "variable-fonts", "glyph-names"]
2626
# Enables the use of the standard library.
2727
# When disabled, the `no-std-float` feature must be enabled instead.
28-
std = []
28+
std = ["alloc"]
2929
no-std-float = ["core_maths"]
30+
# Enables the use of the allocator without using std
31+
alloc = []
3032
# Enables variable fonts support. Increases binary size almost twice.
3133
# Includes avar, CFF2, fvar, gvar, HVAR, MVAR and VVAR tables.
3234
variable-fonts = []
@@ -45,7 +47,7 @@ glyph-names = []
4547
# while the spec allows up to 4095. Most variable fonts use 10-20 tuples,
4648
# so our limit is suitable for most of the cases. But if you need full support, you have to
4749
# enable this feature.
48-
gvar-alloc = ["std"]
50+
gvar-alloc = ["alloc"]
4951

5052
[dev-dependencies]
5153
base64 = "0.23"

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Font parsing starts with a [`Face`].
4949
#[macro_use]
5050
extern crate std;
5151

52+
#[cfg(feature = "alloc")]
53+
extern crate alloc;
54+
5255
#[cfg(not(any(feature = "std", feature = "no-std-float")))]
5356
compile_error!("You have to activate either the `std` or the `no-std-float` feature.");
5457

@@ -795,8 +798,7 @@ impl core::fmt::Display for FaceParsingError {
795798
}
796799
}
797800

798-
#[cfg(feature = "std")]
799-
impl std::error::Error for FaceParsingError {}
801+
impl core::error::Error for FaceParsingError {}
800802

801803
/// A raw font face.
802804
///

src/tables/gvar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ enum VariationTuples<'a> {
6666
},
6767
#[cfg(feature = "gvar-alloc")]
6868
Heap {
69-
vec: std::vec::Vec<VariationTuple<'a>>,
69+
vec: alloc::vec::Vec<VariationTuple<'a>>,
7070
},
7171
}
7272

@@ -87,7 +87,7 @@ impl<'a> VariationTuples<'a> {
8787
if capacity > MAX_STACK_TUPLES_LEN {
8888
// ... and we're currently on the stack, move to the heap.
8989
if let Self::Stack { headers, len } = self {
90-
let mut vec = std::vec::Vec::with_capacity(capacity as usize);
90+
let mut vec = alloc::vec::Vec::with_capacity(capacity as usize);
9191
for header in headers.iter_mut().take(*len as usize) {
9292
let header = core::mem::take(header);
9393
vec.push(header);

src/tables/name.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! A [Naming Table](
22
//! https://docs.microsoft.com/en-us/typography/opentype/spec/name) implementation.
33
4-
#[cfg(feature = "std")]
5-
use std::string::String;
6-
#[cfg(feature = "std")]
7-
use std::vec::Vec;
4+
#[cfg(feature = "alloc")]
5+
use alloc::string::String;
6+
#[cfg(feature = "alloc")]
7+
use alloc::vec::Vec;
88

99
use crate::parser::{FromData, LazyArray16, Offset, Offset16, Stream};
1010
use crate::Language;
@@ -140,7 +140,7 @@ impl<'a> Name<'a> {
140140
/// - Unicode Platform ID
141141
/// - Windows Platform ID + Symbol
142142
/// - Windows Platform ID + Unicode BMP
143-
#[cfg(feature = "std")]
143+
#[cfg(feature = "alloc")]
144144
#[inline(never)]
145145
pub fn to_string(&self) -> Option<String> {
146146
if self.is_unicode() {
@@ -156,7 +156,7 @@ impl<'a> Name<'a> {
156156
is_unicode_encoding(self.platform_id, self.encoding_id)
157157
}
158158

159-
#[cfg(feature = "std")]
159+
#[cfg(feature = "alloc")]
160160
#[inline(never)]
161161
fn name_from_utf16_be(&self) -> Option<String> {
162162
let mut name: Vec<u16> = Vec::new();
@@ -182,7 +182,7 @@ impl<'a> Name<'a> {
182182
}
183183
}
184184

185-
#[cfg(feature = "std")]
185+
#[cfg(feature = "alloc")]
186186
impl<'a> core::fmt::Debug for Name<'a> {
187187
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
188188
let name = self.to_string();
@@ -197,7 +197,7 @@ impl<'a> core::fmt::Debug for Name<'a> {
197197
}
198198
}
199199

200-
#[cfg(not(feature = "std"))]
200+
#[cfg(not(feature = "alloc"))]
201201
impl<'a> core::fmt::Debug for Name<'a> {
202202
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
203203
f.debug_struct("Name")

0 commit comments

Comments
 (0)