Skip to content

feat: Add a feature that allows piet to be used in no_std environments #557

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

Open
wants to merge 6 commits into
base: main
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
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions piet-svg/src/text.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Text functionality for Piet svg backend

#![allow(clippy::arc_with_non_send_sync)]

use std::{
collections::HashSet,
fs, io,
Expand Down
9 changes: 7 additions & 2 deletions piet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
11 changes: 6 additions & 5 deletions piet/src/color.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A simple representation of color

use std::fmt::{Debug, Formatter};
use core::fmt::{Debug, Formatter};

/// A datatype representing color.
///
Expand Down Expand Up @@ -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.);
Expand Down Expand Up @@ -341,13 +341,13 @@ const fn hex_from_ascii_byte(b: u8) -> Result<u8, u8> {
}

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 } => {
Expand All @@ -357,6 +357,7 @@ impl std::fmt::Display for ColorParseError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for ColorParseError {}
#[cfg(test)]
mod tests {
Expand Down
6 changes: 5 additions & 1 deletion piet/src/error.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -17,6 +17,7 @@ pub enum Error {
/// A stack pop failed.
StackUnbalance,
/// The backend failed unexpectedly.
#[cfg(feature = "std")]
BackendError(Box<dyn std::error::Error>),
/// A font could not be found.
MissingFont,
Expand All @@ -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)
Expand All @@ -50,8 +52,10 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

#[cfg(feature = "std")]
impl From<Box<dyn std::error::Error>> for Error {
fn from(e: Box<dyn std::error::Error>) -> Error {
Error::BackendError(e)
Expand Down
2 changes: 1 addition & 1 deletion piet/src/font.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Font families, weights, etcetera

use std::sync::Arc;
use alloc::sync::Arc;

/// A reference to a font family.
///
Expand Down
6 changes: 4 additions & 2 deletions piet/src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
6 changes: 3 additions & 3 deletions piet/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions piet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions piet/src/null_renderer.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -223,7 +223,7 @@ impl IntoBrush<NullRenderContext> for NullBrush {
&'b self,
_piet: &mut NullRenderContext,
_bbox: impl FnOnce() -> Rect,
) -> std::borrow::Cow<'b, NullBrush> {
) -> Cow<'b, NullBrush> {
Cow::Borrowed(self)
}
}
Expand Down
2 changes: 1 addition & 1 deletion piet/src/render_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The main render context trait.

use std::borrow::Cow;
use alloc::borrow::{Cow, ToOwned};

use kurbo::{Affine, Point, Rect, Shape};

Expand Down
4 changes: 2 additions & 2 deletions piet/src/shapes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Options for drawing paths.

use std::rc::Rc;
use alloc::rc::Rc;

/// Options for drawing stroked lines.
///
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions piet/src/text.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -570,31 +571,31 @@ impl From<FontStyle> for TextAttribute {
}
}

impl TextStorage for std::sync::Arc<str> {
impl TextStorage for alloc::sync::Arc<str> {
fn as_str(&self) -> &str {
self
}
}

impl TextStorage for std::rc::Rc<str> {
impl TextStorage for alloc::rc::Rc<str> {
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<String> {
impl TextStorage for alloc::sync::Arc<alloc::string::String> {
fn as_str(&self) -> &str {
self
}
}

impl TextStorage for std::rc::Rc<String> {
impl TextStorage for alloc::rc::Rc<alloc::string::String> {
fn as_str(&self) -> &str {
self
}
Expand Down
7 changes: 5 additions & 2 deletions piet/src/util.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand Down