Skip to content

Commit

Permalink
Implement no_std support
Browse files Browse the repository at this point in the history
Turns out that supporting no_std is pretty trivial, so here are the
changes necessary for that.
  • Loading branch information
CryZe committed Feb 7, 2021
1 parent 2096ac5 commit 6c31ead
Show file tree
Hide file tree
Showing 30 changed files with 114 additions and 91 deletions.
4 changes: 2 additions & 2 deletions scripts/gen-tag-table.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,8 @@ def same_tag(bcp_47_tag, ot_tags):
print('}')
print()
print('fn strncmp(s1: &str, s2: &str, n: usize) -> bool {')
print(' let n1 = std::cmp::min(n, s1.len());')
print(' let n2 = std::cmp::min(n, s2.len());')
print(' let n1 = core::cmp::min(n, s1.len());')
print(' let n2 = core::cmp::min(n, s2.len());')
print(' &s1[..n1] == &s2[..n2]')
print('}')
print()
Expand Down
2 changes: 1 addition & 1 deletion src/aat/extended_kerning.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::convert::TryFrom;
use core::convert::TryFrom;

use crate::Face;
use crate::buffer::{BufferScratchFlags, Buffer};
Expand Down
5 changes: 3 additions & 2 deletions src/aat/map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use alloc::vec::Vec;

use crate::{Face, Tag, Mask};
use super::feature_mappings::FEATURE_MAPPINGS;


#[repr(u8)]
#[derive(Clone, Copy, PartialEq)]
pub enum FeatureType {
Expand Down Expand Up @@ -123,7 +124,7 @@ impl MapBuilder {
} else if !a.is_exclusive && (a.setting & !1) != (b.setting & !1) {
a.setting.cmp(&b.setting)
} else {
std::cmp::Ordering::Equal
core::cmp::Ordering::Equal
}
});

Expand Down
33 changes: 17 additions & 16 deletions src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::convert::TryFrom;
use alloc::{string::String, vec::Vec};
use core::convert::TryFrom;

use ttf_parser::GlyphId;

Expand Down Expand Up @@ -756,13 +757,13 @@ impl Buffer {

if self.have_separate_output {
// Swap info and pos buffers.
let info: Vec<GlyphPosition> = bytemuck::cast_vec(std::mem::take(&mut self.info));
let pos: Vec<GlyphInfo> = bytemuck::cast_vec(std::mem::take(&mut self.pos));
let info: Vec<GlyphPosition> = bytemuck::cast_vec(core::mem::take(&mut self.info));
let pos: Vec<GlyphInfo> = bytemuck::cast_vec(core::mem::take(&mut self.pos));
self.pos = info;
self.info = pos;
}

std::mem::swap(&mut self.len, &mut self.out_len);
core::mem::swap(&mut self.len, &mut self.out_len);

self.idx = 0;
}
Expand Down Expand Up @@ -951,7 +952,7 @@ impl Buffer {
return;
}

if cluster_start == 0 && cluster_end == std::u32::MAX {
if cluster_start == 0 && cluster_end == core::u32::MAX {
for info in &mut self.info[..self.len] {
info.mask = (info.mask & not_mask) | value;
}
Expand Down Expand Up @@ -983,7 +984,7 @@ impl Buffer {
let mut cluster = self.info[start].cluster;

for i in start+1..end {
cluster = std::cmp::min(cluster, self.info[i].cluster);
cluster = core::cmp::min(cluster, self.info[i].cluster);
}

// Extend end
Expand Down Expand Up @@ -1022,7 +1023,7 @@ impl Buffer {
let mut cluster = self.out_info()[start].cluster;

for i in start+1..end {
cluster = std::cmp::min(cluster, self.out_info()[i].cluster);
cluster = core::cmp::min(cluster, self.out_info()[i].cluster);
}

// Extend start
Expand Down Expand Up @@ -1143,7 +1144,7 @@ impl Buffer {
}

fn unsafe_to_break_impl(&mut self, start: usize, end: usize) {
let mut cluster = std::u32::MAX;
let mut cluster = core::u32::MAX;
cluster = Self::_unsafe_to_break_find_min_cluster(&self.info, start, end, cluster);
let unsafe_to_break = Self::_unsafe_to_break_set_mask(&mut self.info, start, end, cluster);
if unsafe_to_break {
Expand All @@ -1160,7 +1161,7 @@ impl Buffer {
assert!(start <= self.out_len);
assert!(self.idx <= end);

let mut cluster = std::u32::MAX;
let mut cluster = core::u32::MAX;
cluster = Self::_unsafe_to_break_find_min_cluster(self.out_info(), start, self.out_len, cluster);
cluster = Self::_unsafe_to_break_find_min_cluster(&self.info, self.idx, end, cluster);
let idx = self.idx;
Expand Down Expand Up @@ -1323,7 +1324,7 @@ impl Buffer {

fn _unsafe_to_break_find_min_cluster(info: &[GlyphInfo], start: usize, end: usize, mut cluster: u32) -> u32 {
for i in start..end {
cluster = std::cmp::min(cluster, info[i].cluster);
cluster = core::cmp::min(cluster, info[i].cluster);
}

cluster
Expand Down Expand Up @@ -1630,8 +1631,8 @@ impl UnicodeBuffer {
}
}

impl std::fmt::Debug for UnicodeBuffer {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for UnicodeBuffer {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt.debug_struct("UnicodeBuffer")
.field("direction", &self.direction())
.field("language", &self.language())
Expand Down Expand Up @@ -1693,8 +1694,8 @@ impl GlyphBuffer {
self.serialize_impl(face, flags).unwrap_or_default()
}

fn serialize_impl(&self, face: &Face, flags: SerializeFlags) -> Result<String, std::fmt::Error> {
use std::fmt::Write;
fn serialize_impl(&self, face: &Face, flags: SerializeFlags) -> Result<String, core::fmt::Error> {
use core::fmt::Write;

let mut s = String::with_capacity(64);

Expand Down Expand Up @@ -1757,8 +1758,8 @@ impl GlyphBuffer {
}
}

impl std::fmt::Debug for GlyphBuffer {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for GlyphBuffer {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt.debug_struct("GlyphBuffer")
.field("glyph_positions", &self.glyph_positions())
.field("glyph_infos", &self.glyph_infos())
Expand Down
23 changes: 12 additions & 11 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ops::{Bound, RangeBounds};
use alloc::string::String;
use core::ops::{Bound, RangeBounds};

use ttf_parser::Tag;

Expand Down Expand Up @@ -152,7 +153,7 @@ impl Default for Direction {
}
}

impl std::str::FromStr for Direction {
impl core::str::FromStr for Direction {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down Expand Up @@ -184,7 +185,7 @@ impl Language {
}
}

impl std::str::FromStr for Language {
impl core::str::FromStr for Language {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down Expand Up @@ -249,7 +250,7 @@ impl Script {
}
}

impl std::str::FromStr for Script {
impl core::str::FromStr for Script {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down Expand Up @@ -486,7 +487,7 @@ impl Feature {
}
}

impl std::str::FromStr for Feature {
impl core::str::FromStr for Feature {
type Err = &'static str;

/// Parses a `Feature` form a string.
Expand Down Expand Up @@ -544,18 +545,18 @@ impl std::str::FromStr for Feature {
p.advance(1);
p.consume_i32().unwrap_or(-1) as u32 // negative value overflow is ok
} else {
if start_opt.is_some() && start != std::u32::MAX {
if start_opt.is_some() && start != core::u32::MAX {
start + 1
} else {
std::u32::MAX
core::u32::MAX
}
};

p.consume_byte(b']')?;

(start, end)
} else {
(0, std::u32::MAX)
(0, core::u32::MAX)
};

// Parse postfix.
Expand Down Expand Up @@ -591,7 +592,7 @@ impl std::str::FromStr for Feature {
#[cfg(test)]
mod tests_features {
use super::*;
use std::str::FromStr;
use core::str::FromStr;

macro_rules! test {
($name:ident, $text:expr, $tag:expr, $value:expr, $range:expr) => (
Expand Down Expand Up @@ -620,7 +621,7 @@ mod tests_features {
test!(parse_13, "kern[3:5]=2", b"kern", 2, 3..=5);
test!(parse_14, "kern[3;5]=2", b"kern", 2, 3..=5);
test!(parse_15, "kern[:-1]", b"kern", 1, ..);
test!(parse_16, "kern[-1]", b"kern", 1, std::u32::MAX as usize..);
test!(parse_16, "kern[-1]", b"kern", 1, core::u32::MAX as usize..);
test!(parse_17, "kern=on", b"kern", 1, ..);
test!(parse_18, "kern=off", b"kern", 0, ..);
test!(parse_19, "kern=oN", b"kern", 1, ..);
Expand All @@ -637,7 +638,7 @@ pub struct Variation {
pub value: f32,
}

impl std::str::FromStr for Variation {
impl core::str::FromStr for Variation {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down
2 changes: 2 additions & 0 deletions src/complex/arabic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::boxed::Box;

use crate::{script, Tag, Face, GlyphInfo, Mask, Script};
use crate::buffer::{Buffer, BufferScratchFlags};
use crate::ot::{feature, FeatureFlags};
Expand Down
2 changes: 2 additions & 0 deletions src/complex/hangul.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::boxed::Box;

use crate::{Face, GlyphInfo, Mask};
use crate::buffer::{Buffer, BufferFlags, BufferClusterLevel};
use crate::ot::{feature, FeatureFlags, Map};
Expand Down
7 changes: 4 additions & 3 deletions src/complex/indic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::convert::TryFrom;
use std::cmp;
use std::ops::Range;
use alloc::boxed::Box;
use core::cmp;
use core::convert::TryFrom;
use core::ops::Range;

use ttf_parser::GlyphId;

Expand Down
2 changes: 2 additions & 0 deletions src/complex/khmer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::boxed::Box;

use crate::{Tag, Mask, Face, GlyphInfo};
use crate::buffer::{Buffer, BufferFlags};
use crate::ot::{feature, FeatureFlags};
Expand Down
3 changes: 2 additions & 1 deletion src/complex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ mod universal_machine;
mod universal_table;
mod vowel_constraints;

use std::any::Any;
use alloc::boxed::Box;
use core::any::Any;

use crate::{script, Direction, Face, Script, Tag};
use crate::buffer::Buffer;
Expand Down
2 changes: 1 addition & 1 deletion src/complex/myanmar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ fn initial_reordering_consonant_syllable(start: usize, end: usize, buffer: &mut
}
}

buffer.sort(start, end, |a, b| a.indic_position().cmp(&b.indic_position()) == std::cmp::Ordering::Greater);
buffer.sort(start, end, |a, b| a.indic_position().cmp(&b.indic_position()) == core::cmp::Ordering::Greater);
}

fn override_features(planner: &mut ShapePlanner) {
Expand Down
2 changes: 1 addition & 1 deletion src/complex/universal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ fn setup_rphf_mask(plan: &ShapePlan, buffer: &mut Buffer) {
let limit = if buffer.info[start].use_category() == category::R {
1
} else {
std::cmp::min(3, end - start)
core::cmp::min(3, end - start)
};

for i in start..start+limit {
Expand Down
2 changes: 1 addition & 1 deletion src/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl<'a> Face<'a> {
pub(crate) fn glyph_extents(&self, glyph: GlyphId) -> Option<GlyphExtents> {
let pixels_per_em = match self.pixels_per_em {
Some(ppem) => ppem.0,
None => std::u16::MAX,
None => core::u16::MAX,
};

if let Some(img) = self.ttfp_face.glyph_raster_image(glyph, pixels_per_em) {
Expand Down
8 changes: 5 additions & 3 deletions src/glyph_set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cmp::{self, Ordering};
use std::ops::RangeInclusive;
use alloc::vec::Vec;
use core::cmp::{self, Ordering};
use core::ops::RangeInclusive;

use ttf_parser::GlyphId;

Expand All @@ -14,7 +15,7 @@ pub struct GlyphSet {
impl GlyphSet {
/// Create a new glyph set builder.
pub fn builder() -> GlyphSetBuilder {
GlyphSetBuilder { ranges: vec![] }
GlyphSetBuilder { ranges: Vec::new() }
}

/// Check whether the glyph is contained in the set.
Expand Down Expand Up @@ -93,6 +94,7 @@ impl GlyphSetBuilder {
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;

#[test]
fn test_empty() {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
`rustybuzz` is an attempt to incrementally port [harfbuzz](https://github.com/harfbuzz/harfbuzz) to Rust.
*/

#![no_std]
#![doc(html_root_url = "https://docs.rs/rustybuzz/0.3.0")]
#![warn(missing_docs)]

extern crate alloc;

#[macro_use]
mod buffer;
mod aat;
Expand Down
2 changes: 1 addition & 1 deletion src/ot/layout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! OpenType layout.

use std::ops::{Index, IndexMut};
use core::ops::{Index, IndexMut};

use ttf_parser::GlyphId;

Expand Down
Loading

0 comments on commit 6c31ead

Please sign in to comment.