diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03ea9194..1756612b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,6 +63,12 @@ jobs: command: clippy args: --manifest-path=piet/Cargo.toml --all-targets --all-features -- -D warnings + - name: cargo clippy piet with no default features + uses: actions-rs/cargo@v1 + with: + command: clippy + args: --manifest-path=piet/Cargo.toml --all-targets --no-default-features --features libm -- -D warnings + - name: cargo clippy piet-cairo uses: actions-rs/cargo@v1 with: @@ -103,6 +109,12 @@ jobs: command: test args: --manifest-path=piet/Cargo.toml --all-features + - name: cargo test piet with no default features + uses: actions-rs/cargo@v1 + with: + command: test + args: --manifest-path=piet/Cargo.toml --no-default-features --features libm + - name: cargo test piet-cairo uses: actions-rs/cargo@v1 with: @@ -233,6 +245,12 @@ jobs: command: test args: --manifest-path=piet/Cargo.toml --all-features + - name: cargo test piet with no default features + uses: actions-rs/cargo@v1 + with: + command: test + args: --manifest-path=piet/Cargo.toml --no-default-features --features libm + - name: cargo test piet-cairo uses: actions-rs/cargo@v1 with: diff --git a/piet-svg/src/text.rs b/piet-svg/src/text.rs index 7a77750f..bbf54c9f 100644 --- a/piet-svg/src/text.rs +++ b/piet-svg/src/text.rs @@ -1,5 +1,7 @@ //! Text functionality for Piet svg backend +#![allow(clippy::arc_with_non_send_sync)] + use std::{ collections::HashSet, fs, io, diff --git a/piet/Cargo.toml b/piet/Cargo.toml index 636a4efa..6dfa25f1 100644 --- a/piet/Cargo.toml +++ b/piet/Cargo.toml @@ -13,17 +13,22 @@ include = ["src/**/*", "Cargo.toml", "snapshots/resources/*"] [dependencies] image = { version = "0.24.5", optional = true, default-features = false } -kurbo = "0.9" +kurbo = { version = "0.9", default-features = false } pico-args = { version = "0.4.2", optional = true } png = { version = "0.17.7", optional = true } os_info = { version = "3.6.0", optional = true, default-features = false } unic-bidi = "0.9.0" [features] -samples = ["pico-args", "png", "os_info"] +default = ["std"] +std = ["kurbo/std"] +libm = ["kurbo/libm"] + +samples = ["pico-args", "png", "os_info", "std"] # passing on all the image features. AVIF is not supported because it does not # support decoding, and thats al we use `Image` for. +image = ["dep:image", "std"] image_png = ["image/png", "image"] jpeg = ["image/jpeg", "image"] jpeg_rayon = ["image/jpeg_rayon", "image"] diff --git a/piet/src/color.rs b/piet/src/color.rs index caa044dd..6a1495be 100644 --- a/piet/src/color.rs +++ b/piet/src/color.rs @@ -1,6 +1,6 @@ //! A simple representation of color -use std::fmt::{Debug, Formatter}; +use core::fmt::{Debug, Formatter}; /// A datatype representing color. /// @@ -138,7 +138,7 @@ impl Color { 3. * d * d * (t - 4. / 29.) } } - let th = h * (std::f64::consts::PI / 180.); + let th = h * (core::f64::consts::PI / 180.); let a = c * th.cos(); let b = c * th.sin(); let ll = (L + 16.) * (1. / 116.); @@ -341,13 +341,13 @@ const fn hex_from_ascii_byte(b: u8) -> Result { } impl Debug for Color { - fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { write!(f, "#{:08x}", self.as_rgba_u32()) } } -impl std::fmt::Display for ColorParseError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl core::fmt::Display for ColorParseError { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { match self { ColorParseError::WrongSize(n) => write!(f, "Input string has invalid length {n}"), ColorParseError::NotHex { idx, byte } => { @@ -357,6 +357,7 @@ impl std::fmt::Display for ColorParseError { } } +#[cfg(feature = "std")] impl std::error::Error for ColorParseError {} #[cfg(test)] mod tests { diff --git a/piet/src/error.rs b/piet/src/error.rs index 1c6f1747..bba6e15f 100644 --- a/piet/src/error.rs +++ b/piet/src/error.rs @@ -1,6 +1,6 @@ //! The common error type for piet operations. -use std::fmt; +use core::fmt; /// An error that can occur while rendering 2D graphics. #[derive(Debug)] @@ -17,6 +17,7 @@ pub enum Error { /// A stack pop failed. StackUnbalance, /// The backend failed unexpectedly. + #[cfg(feature = "std")] BackendError(Box), /// A font could not be found. MissingFont, @@ -40,6 +41,7 @@ impl fmt::Display for Error { "This functionality is not yet implemented for this backend" ), Error::MissingFeature(feature) => write!(f, "Missing feature '{feature}'"), + #[cfg(feature = "std")] Error::BackendError(e) => { write!(f, "Backend error: ")?; e.fmt(f) @@ -50,8 +52,10 @@ impl fmt::Display for Error { } } +#[cfg(feature = "std")] impl std::error::Error for Error {} +#[cfg(feature = "std")] impl From> for Error { fn from(e: Box) -> Error { Error::BackendError(e) diff --git a/piet/src/font.rs b/piet/src/font.rs index e13bef89..9b3cf3a5 100644 --- a/piet/src/font.rs +++ b/piet/src/font.rs @@ -1,6 +1,6 @@ //! Font families, weights, etcetera -use std::sync::Arc; +use alloc::sync::Arc; /// A reference to a font family. /// diff --git a/piet/src/gradient.rs b/piet/src/gradient.rs index 402b969a..ac2a0e73 100644 --- a/piet/src/gradient.rs +++ b/piet/src/gradient.rs @@ -20,8 +20,10 @@ //! //! [unit square]: https://en.wikipedia.org/wiki/Unit_square -use std::borrow::Cow; -use std::hash::{Hash, Hasher}; +use alloc::borrow::{Cow, ToOwned}; +use alloc::vec::Vec; + +use core::hash::{Hash, Hasher}; use kurbo::{Point, Rect, Size, Vec2}; diff --git a/piet/src/image.rs b/piet/src/image.rs index 846847b0..5b0a542c 100644 --- a/piet/src/image.rs +++ b/piet/src/image.rs @@ -14,11 +14,11 @@ //! Types for working with images +use alloc::sync::Arc; #[cfg(feature = "image")] use std::error::Error; #[cfg(feature = "image")] use std::path::Path; -use std::sync::Arc; use crate::kurbo::Size; use crate::util::unpremul; @@ -205,8 +205,8 @@ impl ImageBuf { } } -impl std::fmt::Debug for ImageBuf { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl core::fmt::Debug for ImageBuf { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { f.debug_struct("ImageBuf") .field("size", &self.pixels.len()) .field("width", &self.width) diff --git a/piet/src/lib.rs b/piet/src/lib.rs index c788661b..871d6ea2 100644 --- a/piet/src/lib.rs +++ b/piet/src/lib.rs @@ -23,6 +23,9 @@ #![warn(missing_docs)] #![deny(clippy::trivially_copy_pass_by_ref, rustdoc::broken_intra_doc_links)] +#![cfg_attr(not(feature = "std"), no_std)] + +extern crate alloc; pub use kurbo; diff --git a/piet/src/null_renderer.rs b/piet/src/null_renderer.rs index 9b22c945..b4ddf20f 100644 --- a/piet/src/null_renderer.rs +++ b/piet/src/null_renderer.rs @@ -1,7 +1,7 @@ //! A render context that does nothing. -use std::borrow::Cow; -use std::ops::RangeBounds; +use alloc::borrow::Cow; +use core::ops::RangeBounds; use kurbo::{Affine, Point, Rect, Shape, Size}; @@ -223,7 +223,7 @@ impl IntoBrush for NullBrush { &'b self, _piet: &mut NullRenderContext, _bbox: impl FnOnce() -> Rect, - ) -> std::borrow::Cow<'b, NullBrush> { + ) -> Cow<'b, NullBrush> { Cow::Borrowed(self) } } diff --git a/piet/src/render_context.rs b/piet/src/render_context.rs index ffb55df8..636de222 100644 --- a/piet/src/render_context.rs +++ b/piet/src/render_context.rs @@ -1,6 +1,6 @@ //! The main render context trait. -use std::borrow::Cow; +use alloc::borrow::{Cow, ToOwned}; use kurbo::{Affine, Point, Rect, Shape}; diff --git a/piet/src/shapes.rs b/piet/src/shapes.rs index 5ee8d53c..d5327cbb 100644 --- a/piet/src/shapes.rs +++ b/piet/src/shapes.rs @@ -1,6 +1,6 @@ //! Options for drawing paths. -use std::rc::Rc; +use alloc::rc::Rc; /// Options for drawing stroked lines. /// @@ -229,7 +229,7 @@ impl Default for LineJoin { } } -impl std::ops::Deref for StrokeDash { +impl core::ops::Deref for StrokeDash { type Target = [f64]; fn deref(&self) -> &Self::Target { self.alloc.as_deref().unwrap_or(self.slice) diff --git a/piet/src/text.rs b/piet/src/text.rs index 6b6f1be1..e09e771f 100644 --- a/piet/src/text.rs +++ b/piet/src/text.rs @@ -1,6 +1,7 @@ //! Traits for fonts and text handling. -use std::ops::{Range, RangeBounds}; +use alloc::vec::Vec; +use core::ops::{Deref, Range, RangeBounds}; use crate::kurbo::{Point, Rect, Size}; use crate::{Color, Error, FontFamily, FontStyle, FontWeight}; @@ -122,7 +123,7 @@ pub trait TextStorage: 'static { fn as_str(&self) -> &str; } -impl std::ops::Deref for dyn TextStorage { +impl Deref for dyn TextStorage { type Target = str; fn deref(&self) -> &Self::Target { self.as_str() @@ -570,31 +571,31 @@ impl From for TextAttribute { } } -impl TextStorage for std::sync::Arc { +impl TextStorage for alloc::sync::Arc { fn as_str(&self) -> &str { self } } -impl TextStorage for std::rc::Rc { +impl TextStorage for alloc::rc::Rc { fn as_str(&self) -> &str { self } } -impl TextStorage for String { +impl TextStorage for alloc::string::String { fn as_str(&self) -> &str { self.as_str() } } -impl TextStorage for std::sync::Arc { +impl TextStorage for alloc::sync::Arc { fn as_str(&self) -> &str { self } } -impl TextStorage for std::rc::Rc { +impl TextStorage for alloc::rc::Rc { fn as_str(&self) -> &str { self } diff --git a/piet/src/util.rs b/piet/src/util.rs index 06f4ef50..14fe04b2 100644 --- a/piet/src/util.rs +++ b/piet/src/util.rs @@ -1,6 +1,8 @@ //! Code useful for multiple backends -use std::ops::{Bound, Range, RangeBounds}; +use alloc::vec; +use alloc::vec::Vec; +use core::ops::{Bound, Range, RangeBounds}; use crate::kurbo::{Rect, Size}; use crate::{Color, Error, FontFamily, FontStyle, FontWeight, LineMetric, TextAttribute}; @@ -143,7 +145,7 @@ pub fn compute_blurred_rect(rect: Rect, radius: f64, stride: usize, buf: &mut [u // See https://raphlinus.github.io/audio/2018/09/05/sigmoid.html for a little // explanation of this approximation to the erf function. fn compute_erf7(x: f64) -> f64 { - let x = x * std::f64::consts::FRAC_2_SQRT_PI; + let x = x * core::f64::consts::FRAC_2_SQRT_PI; let xx = x * x; let x = x + (0.24295 + (0.03395 + 0.0104 * xx) * xx) * (x * xx); x / (1.0 + x * x).sqrt() @@ -290,6 +292,7 @@ pub fn image_buffer_to_tightly_packed( #[cfg(test)] mod tests { use super::*; + use alloc::string::ToString; #[test] fn test_count_until_utf16() {