Skip to content

Commit

Permalink
Allow some of the SVG support in no_std
Browse files Browse the repository at this point in the history
Some of the SVG functionality builds with `no_std` and can
reasonably be enabled.
  • Loading branch information
waywardmonkeys committed Aug 3, 2024
1 parent a568ab2 commit ebe4d48
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
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

0 comments on commit ebe4d48

Please sign in to comment.