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 no_std compatibility #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ keywords = ["2d", "graphics"]
categories = ["graphics"]

[dependencies]
euclid = "0.22"
euclid = { version = "0.22", default-features = false, features = ["libm"] }
font-kit = { version = "0.11", optional = true }
lyon_geom = "1.0"
lyon_geom = { version = "1.0", default-features = false }
pathfinder_geometry = { version = "0.5", optional = true }
png = { version = "0.17", optional = true }
typed-arena = "2.0"
typed-arena = { version = "2.0", default-features = false }
sw-composite = "0.7.15"
num-traits = { version = "0.2.17", default-features = false, features = ["libm"] }

[features]
default = ["text", "png"]
default = ["std", "text", "png"]
std = ["typed-arena/std", "euclid/std"]
text = ["font-kit", "pathfinder_geometry"]
png = ["dep:png", "std"]
7 changes: 6 additions & 1 deletion src/blitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ use sw_composite::*;
use crate::{IntPoint, Point, Transform};
use crate::draw_target::{ExtendMode, Source, FilterMode};

use alloc::{
boxed::Box,
vec, vec::Vec
};

use euclid::vec2;
use std::marker::PhantomData;
use core::marker::PhantomData;

pub trait Blitter {
fn blit_span(&mut self, y: i32, x1: i32, x2: i32, mask: &[u8]);
Expand Down
2 changes: 2 additions & 0 deletions src/dash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::path_builder::*;

use crate::Point;

use alloc::vec::Vec;

use lyon_geom::LineSegment;

#[derive(Clone, Copy)]
Expand Down
11 changes: 8 additions & 3 deletions src/draw_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ use crate::path_builder::*;
pub use crate::path_builder::Winding;
use lyon_geom::CubicBezierSegment;

use alloc::{vec, vec::Vec};

#[cfg(not(feature = "std"))]
use num_traits::Float;

#[cfg(feature = "text")]
mod fk {
pub use font_kit::canvas::{Canvas, Format, RasterizationOptions};
Expand Down Expand Up @@ -1072,7 +1077,7 @@ impl<Backing : AsRef<[u32]> + AsMut<[u32]>> DrawTarget<Backing> {
let len = buf.len();
// we want to return an [u8] slice instead of a [u32] slice. This is a safe thing to
// do because requirements of a [u32] slice are stricter.
unsafe { std::slice::from_raw_parts(p as *const u8, len * std::mem::size_of::<u32>()) }
unsafe { core::slice::from_raw_parts(p as *const u8, len * core::mem::size_of::<u32>()) }
}

/// Returns a mut reference to the underlying pixel data as individual bytes with the order BGRA
Expand All @@ -1083,7 +1088,7 @@ impl<Backing : AsRef<[u32]> + AsMut<[u32]>> DrawTarget<Backing> {
let len = buf.len();
// we want to return an [u8] slice instead of a [u32] slice. This is a safe thing to
// do because requirements of a [u32] slice are stricter.
unsafe { std::slice::from_raw_parts_mut(p as *mut u8, len * std::mem::size_of::<u32>()) }
unsafe { core::slice::from_raw_parts_mut(p as *mut u8, len * core::mem::size_of::<u32>()) }
}

/// Take ownership of the buffer backing the DrawTarget
Expand All @@ -1094,7 +1099,7 @@ impl<Backing : AsRef<[u32]> + AsMut<[u32]>> DrawTarget<Backing> {
/// Saves the current pixel to a png file at `path`
#[cfg(feature = "png")]
pub fn write_png<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), png::EncodingError> {
let file = File::create(path)?;
let file = std::fs::File::create(path)?;

let w = &mut BufWriter::new(file);

Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ Produces:

#![warn(missing_copy_implementations)]

#![no_std]

extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

mod blitter;
mod dash;
mod draw_target;
Expand Down
2 changes: 2 additions & 0 deletions src/path_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use lyon_geom::QuadraticBezierSegment;

use crate::{Point, Transform, Vector};

use alloc::vec::Vec;

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Winding {
EvenOdd,
Expand Down
6 changes: 4 additions & 2 deletions src/rasterizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use crate::blitter::RasterBlitter;
use crate::path_builder::Winding;
use crate::geom::intrect;

use std::ptr::NonNull;
use core::ptr::NonNull;

use alloc::vec::Vec;

// One reason to have separate Edge/ActiveEdge is reduce the
// memory usage of inactive edges. On the other hand
Expand Down Expand Up @@ -313,7 +315,7 @@ impl Rasterizer {
// how do we deal with edges to the right and left of the canvas?
let e = self.edge_arena.alloc(ActiveEdge::new());
if end.y < start.y {
std::mem::swap(&mut start, &mut end);
core::mem::swap(&mut start, &mut end);
e.winding = -1;
} else {
e.winding = 1;
Expand Down
7 changes: 6 additions & 1 deletion src/stroke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
use crate::path_builder::{Path, PathBuilder, PathOp};
use crate::{Point, Vector};

use alloc::vec::Vec;

#[cfg(not(feature = "std"))]
use num_traits::Float;

#[derive(Clone, PartialEq, Debug)]
pub struct StrokeStyle {
pub width: f32,
Expand Down Expand Up @@ -242,7 +247,7 @@ fn join_line(
if is_interior_angle(s1_normal, s2_normal) {
s2_normal = flip(s2_normal);
s1_normal = flip(s1_normal);
std::mem::swap(&mut s1_normal, &mut s2_normal);
core::mem::swap(&mut s1_normal, &mut s2_normal);
}

// XXX: joining uses `pt` which can cause seams because it lies halfway on a line and the
Expand Down
7 changes: 4 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(test)]
mod tests {

use alloc::{vec, vec::Vec};

use crate::geom::intrect;
use crate::*;
const WHITE_SOURCE: Source = Source::Solid(SolidSource {
Expand Down Expand Up @@ -301,7 +302,7 @@ mod tests {
&pb.finish(),
&WHITE_SOURCE,
&StrokeStyle {
width: std::f32::MIN,
width: core::f32::MIN,
..Default::default()
},
&DrawOptions::new(),
Expand Down Expand Up @@ -757,7 +758,7 @@ mod tests {
#[test]
fn arc_contains() {
let mut pb = PathBuilder::new();
pb.arc(50., 25., 10., 0., std::f32::consts::PI);
pb.arc(50., 25., 10., 0., core::f32::consts::PI);
let path = pb.finish();
assert!(!path.contains_point(0.1, 50., 10.));
assert!(!path.contains_point(0.1, 50., 20.));
Expand Down