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

Allow some of the SVG support in no_std #356

Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This release has an [MSRV][] of 1.65.
### Changed

- Move `Self: Sized` bound from `Shape` to methods. ([#340] by [@waywardmonkeys])
- Enable partial SVG path support in `no_std` builds. ([#356] by [@waywardmonkeys])

### Fixed

Expand All @@ -49,6 +50,7 @@ Note: A changelog was not kept for or before this release
[#343]: https://github.com/linebender/kurbo/pull/343
[#347]: https://github.com/linebender/kurbo/pull/347
[#354]: https://github.com/linebender/kurbo/pull/354
[#356]: https://github.com/linebender/kurbo/pull/356

[Unreleased]: https://github.com/linebender/kurbo/compare/v0.11.0...HEAD
[0.11.0]: https://github.com/linebender/kurbo/releases/tag/v0.11.0
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ mod shape;
pub mod simplify;
mod size;
mod stroke;
#[cfg(feature = "std")]
mod svg;
mod translate_scale;
mod vec2;
Expand All @@ -134,7 +133,6 @@ pub use crate::rounded_rect_radii::*;
pub use crate::shape::*;
pub use crate::size::*;
pub use crate::stroke::*;
#[cfg(feature = "std")]
pub use crate::svg::*;
pub use crate::translate_scale::*;
pub use crate::vec2::*;
14 changes: 12 additions & 2 deletions src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

//! SVG path representation.

use alloc::vec::Vec;
use core::f64::consts::PI;
use core::fmt::{self, Display, Formatter};
// MSRV: Once our MSRV is 1.81, we can switch to `core::error`
#[cfg(feature = "std")]
use std::error::Error;
use std::f64::consts::PI;
use std::fmt::{self, Display, Formatter};
#[cfg(feature = "std")]
use std::io::{self, Write};

use crate::{Arc, BezPath, ParamCurve, PathEl, PathSeg, Point, Vec2};

#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

// Note: the SVG arc logic is heavily adapted from https://github.com/nical/lyon

/// A single SVG arc segment.
Expand Down Expand Up @@ -60,13 +67,15 @@ impl BezPath {
///
/// The current implementation doesn't take any special care to produce a
/// short string (reducing precision, using relative movement).
#[cfg(feature = "std")]
pub fn to_svg(&self) -> String {
let mut buffer = Vec::new();
self.write_to(&mut buffer).unwrap();
String::from_utf8(buffer).unwrap()
}

/// Write the SVG representation of this path to the provided buffer.
#[cfg(feature = "std")]
pub fn write_to<W: Write>(&self, mut writer: W) -> io::Result<()> {
for (i, el) in self.elements().iter().enumerate() {
if i > 0 {
Expand Down Expand Up @@ -264,6 +273,7 @@ impl Display for SvgParseError {
}
}

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

struct SvgLexer<'a> {
Expand Down