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

Add support for no_std #63

Merged
merged 1 commit into from
Nov 7, 2024
Merged
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
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ jobs:
- uses: Swatinem/rust-cache@v2
- run: cargo test

build-no-std:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
# Use a target without `std` to make sure we don't link to `std`
targets: "x86_64-unknown-none,thumbv7m-none-eabi"
- run: cargo build --target x86_64-unknown-none --no-default-features --features libm
- run: cargo build --target x86_64-unknown-none --no-default-features --features libm,scale
- run: cargo build --target x86_64-unknown-none --no-default-features --features libm,render
- run: cargo build --target thumbv7m-none-eabi --no-default-features --features libm
- run: cargo build --target thumbv7m-none-eabi --no-default-features --features libm,scale
- run: cargo build --target thumbv7m-none-eabi --no-default-features --features libm,render

miri:
name: Miri
runs-on: ubuntu-latest
Expand Down
12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ homepage = "https://github.com/dfrg/swash"
readme = "README.md"

[features]
default = ["scale", "render"]
default = ["std", "scale", "render"]

scale = ["dep:yazi", "dep:zeno", "zeno/std"]
libm = ["dep:core_maths", "skrifa/libm", "zeno/libm"]
std = ["skrifa/std", "zeno/std"]

scale = ["dep:yazi", "dep:zeno"]
render = ["scale", "zeno/eval"]

[lints]
clippy.doc_markdown = "warn"
clippy.semicolon_if_nothing_returned = "warn"

[dependencies]
yazi = { version = "0.2.0", optional = true }
core_maths = { version = "0.1.0", optional = true }
yazi = { version = "0.2.0", optional = true, default-features = false }
zeno = { version = "0.3.1", optional = true, default-features = false }
skrifa = { version = "0.22.3" }
skrifa = { version = "0.22.3", default-features = false }
1 change: 1 addition & 0 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::FontRef;
use alloc::vec::Vec;

/// Uniquely generated value for identifying and caching fonts.
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
Expand Down
1 change: 1 addition & 0 deletions src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub trait RawFont<'a>: Sized {
/// Returns the offset to the table directory.
fn offset(&self) -> u32;

#[cfg(feature = "std")]
fn dump_tables(&self) -> Option<()> {
let base = self.offset() as usize;
let b = Bytes::new(self.data());
Expand Down
2 changes: 1 addition & 1 deletion src/internal/vorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn origin(data: &[u8], vorg: u32, glyph_id: u16) -> Option<i16> {
let mut l = 0;
let mut h = count;
while l < h {
use std::cmp::Ordering::*;
use core::cmp::Ordering::*;
let i = (l + h) / 2;
let rec = base + 8 + i * 4;
let g = b.read::<u16>(rec)?;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ the respective modules.
#![allow(clippy::needless_lifetimes)]
#![allow(clippy::redundant_static_lifetimes)]
#![allow(clippy::too_many_arguments)]
#![cfg_attr(not(any(test, feature = "std")), no_std)]
extern crate alloc;

#[cfg(feature = "scale")]
pub use zeno;
Expand Down
8 changes: 6 additions & 2 deletions src/scale/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#![allow(dead_code)]

use alloc::vec::Vec;
#[cfg(feature = "libm")]
use core_maths::CoreFloat;

mod png;

/// Decodes a PNG image.
Expand Down Expand Up @@ -345,7 +349,7 @@ fn sample_dir<Input, Output, Filter>(
}

fn sinc(t: f32) -> f32 {
let a = t * std::f32::consts::PI;
let a = t * core::f32::consts::PI;
if t == 0. {
1.
} else {
Expand Down Expand Up @@ -403,5 +407,5 @@ fn nearest(_x: f32) -> f32 {
}

fn gaussian(x: f32, r: f32) -> f32 {
((2. * std::f32::consts::PI).sqrt() * r).recip() * (-x.powi(2) / (2. * r.powi(2))).exp()
((2. * core::f32::consts::PI).sqrt() * r).recip() * (-x.powi(2) / (2. * r.powi(2))).exp()
}
10 changes: 7 additions & 3 deletions src/scale/bitmap/png.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! PNG decoder.

use alloc::vec::Vec;

/// PNG magic bytes.
pub const SIGNATURE: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];

Expand All @@ -24,10 +26,12 @@ pub enum DecodeError {
CorruptData,
/// An "end of file" was reached prematurely.
UnexpectedEof,
#[cfg(feature = "std")]
/// Underlying IO error.
Io(std::io::Error),
}

#[cfg(feature = "std")]
impl From<std::io::Error> for DecodeError {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
Expand Down Expand Up @@ -328,7 +332,7 @@ fn decode_data<E: Emit>(
normalize(line, out_line, depth, has_palette, cols, trunc_16)?;
E::emit(state, out_line, target, start, y, w, inc, cols)?;
}
std::mem::swap(&mut prev_line, &mut line);
core::mem::swap(&mut prev_line, &mut line);
y += row_inc;
}
if pass == 6 {
Expand All @@ -348,7 +352,7 @@ fn decode_data<E: Emit>(
let ty = *source.get(0)?;
defilter(ty, source.get(1..)?, line, prev_line, bwidth)?;
E::emit(state, line, target, 0, y, w, 1, w)?;
std::mem::swap(&mut prev_line, &mut line);
core::mem::swap(&mut prev_line, &mut line);
}
} else {
for y in 0..h {
Expand All @@ -359,7 +363,7 @@ fn decode_data<E: Emit>(
defilter(ty, source.get(1..)?, line, prev_line, bwidth)?;
normalize(line, out_line, depth, has_palette, w, trunc_16)?;
E::emit(state, out_line, target, 0, y, w, 1, w)?;
std::mem::swap(&mut prev_line, &mut line);
core::mem::swap(&mut prev_line, &mut line);
}
}
Some(())
Expand Down
2 changes: 2 additions & 0 deletions src/scale/hinting_cache.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use alloc::vec::Vec;
use skrifa::{
instance::{NormalizedCoord, Size},
outline::{
HintingInstance, HintingMode, LcdLayout, OutlineGlyphCollection, OutlineGlyphFormat,
},
};

/// We keep this small to enable a simple LRU cache with a linear
/// search. Regenerating hinting data is low to medium cost so it's fine
/// to redo it occasionally.
Expand Down
1 change: 1 addition & 0 deletions src/scale/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Rendered glyph image.
*/

use super::Source;
use alloc::vec::Vec;
use zeno::Placement;

/// Content of a scaled glyph image.
Expand Down
3 changes: 3 additions & 0 deletions src/scale/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ use skrifa::{

use super::internal;
use super::{cache::FontCache, setting::Setting, FontRef, GlyphId, NormalizedCoord};
use alloc::vec::Vec;
use core::borrow::Borrow;
#[cfg(all(feature = "libm", feature = "render"))]
use core_maths::CoreFloat;
use proxy::*;
use zeno::Placement;
#[cfg(feature = "render")]
Expand Down
1 change: 1 addition & 0 deletions src/scale/outline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Glyph outline.
*/

use alloc::vec::Vec;
use zeno::{Bounds, PathData, Point, Transform, Verb};

/// Scaled glyph outline represented as a collection of layers and a sequence
Expand Down
2 changes: 2 additions & 0 deletions src/shape/at.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::internal::{at::*, *};
use super::{buffer::*, feature::*, Direction};
use crate::text::Script;

use alloc::vec::Vec;
use core::ops::Range;

pub type FeatureBit = u16;
Expand Down
2 changes: 2 additions & 0 deletions src/shape/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::text::{
cluster::{Char, CharCluster, ClusterInfo, ShapeClass, SourceRange, MAX_CLUSTER_SIZE},
JoiningType,
};

use alloc::vec::Vec;
use core::ops::Range;

// Glyph flags.
Expand Down
2 changes: 2 additions & 0 deletions src/shape/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use super::engine::EngineMetadata;
use super::internal::var::Fvar;
use crate::{charmap::CharmapProxy, metrics::MetricsProxy, FontRef};

use alloc::vec::Vec;

pub type Epoch = u64;

pub struct FontEntry {
Expand Down
1 change: 1 addition & 0 deletions src/shape/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::internal::{self, at::Gdef, raw_tag, Bytes, RawFont, RawTag};
use crate::font::FontRef;
use crate::text::{Language, Script};

use alloc::vec::Vec;
use core::ops::Range;

/// Shaping engine that handles the various methods available in
Expand Down
1 change: 1 addition & 0 deletions src/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ use crate::text::{
cluster::{CharCluster, Parser, ShapeClass, Token},
Language, Script,
};
use alloc::vec::Vec;
use at::{FeatureMask, FeatureStore, FeatureStoreBuilder};
use buffer::*;
use cache::{FeatureCache, FontEntry};
Expand Down
5 changes: 5 additions & 0 deletions src/strike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
use super::internal::*;
use super::{FontRef, GlyphId};

#[cfg(feature = "scale")]
use alloc::vec::Vec;
#[cfg(all(feature = "libm", feature = "scale"))]
use core_maths::CoreFloat;

/// Proxy for rematerializing strike collections.
#[derive(Copy, Clone)]
pub struct BitmapStrikesProxy {
Expand Down
2 changes: 1 addition & 1 deletion src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Localized names and other metadata.
*/

use std::fmt::Write;
use core::fmt::Write;

use super::internal::*;
use super::FontRef;
Expand Down