Skip to content

Added save_png to drawing #196

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ cfg-if = "0.1"

parking_lot = "0.10"

nsvg = "0.5.1"
image = "0.23"

[dependencies.futures-util]
version = "0.3"
default-features = false
Expand Down
Binary file added docs/assets/images/docs/squares.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/async_drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ impl AsyncDrawing {
self.client.export_svg(path.as_ref().to_path_buf()).await
}

pub async fn save_png<P: AsRef<Path>>(&self, path: P) -> Result<(), ExportError> {
self.client.export_png(path.as_ref().to_path_buf()).await
}

//TODO: If we move to a shared memory architecture, we wouldn't need to make
// any request here and thus would not need this method at all. We should
// think things through before making this method public.
Expand Down
45 changes: 45 additions & 0 deletions src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,51 @@ impl Drawing {
pub fn save_svg<P: AsRef<Path>>(&self, path: P) -> Result<(), ExportError> {
block_on(self.drawing.save_svg(path))
}

/// Saves the current drawings in SVG format at the location specified by `path`.
///
/// ```rust,no_run
/// use turtle::{Drawing, Turtle, Color, ExportError};
///
/// fn main() -> Result<(), ExportError> {
/// let mut drawing = Drawing::new();
/// let mut turtle = drawing.add_turtle();
/// drawing.set_background_color("pink");
///
/// for i in 0..36 {
/// let base_color: Color = if i % 2 == 0 {
/// "red".into()
/// } else {
/// "white".into()
/// };
///
/// turtle.set_fill_color(base_color.with_alpha(1.0 - i as f64 / 54.0));
/// turtle.begin_fill();
/// square(&mut turtle);
/// turtle.end_fill();
/// turtle.right(10.0);
/// }
///
/// turtle.hide();
/// drawing.save_png("squares.png")?;
///
/// Ok(())
/// }
///
/// fn square(turtle: &mut Turtle) {
/// for _ in 0..4 {
/// turtle.forward(200.0);
/// turtle.right(90.0);
/// }
/// }
/// ```
///
/// This will produce the following image in the current directory under the name `squares.png`:
///
/// ![squares](https://raw.githubusercontent.com/sunjay/turtle/master/docs/assets/images/docs/squares.png?sanitize=true)
pub fn save_png<P: AsRef<Path>>(&self, path: P) -> Result<(), ExportError> {
block_on(self.drawing.save_png(path))
}
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/ipc_protocol/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub enum RotationDirection {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExportFormat {
Svg,
Png,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
10 changes: 10 additions & 0 deletions src/ipc_protocol/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ impl ProtocolClient {
}
}

pub async fn export_png(&self, path: PathBuf) -> Result<(), ExportError> {
self.client.send(ClientRequest::Export(path, ExportFormat::Png));

let response = self.client.recv().await;
match response {
ServerResponse::ExportComplete(res) => res,
_ => unreachable!("bug: expected to receive `ExportComplete` in response to `Export` request"),
}
}

pub async fn poll_event(&self) -> Option<Event> {
self.client.send(ClientRequest::PollEvent);

Expand Down
1 change: 1 addition & 0 deletions src/renderer_server/handlers/export_drawings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) fn export_drawings(
use ExportFormat::*;
let res = match format {
Svg => export::save_svg(display_list, drawing, path),
Png => export::save_png(display_list, drawing, path),
};

conn.send(ServerResponse::ExportComplete(res))?;
Expand Down
65 changes: 55 additions & 10 deletions src/renderer_server/renderer/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::path::Path as FilePath;
use thiserror::Error;
use serde::{Serialize, Deserialize};
use svg::node::element::{Line, Polygon, Rectangle};
use image;
use nsvg;

use crate::Color;

Expand All @@ -13,10 +15,10 @@ use super::super::{
state::DrawingState,
};

/// Converts a color to its RGBA color string (suitable for SVG)
fn rgba(color: Color) -> String {
let Color {red, green, blue, alpha} = color;
format!("rgba({}, {}, {}, {})", red as u8, green as u8, blue as u8, alpha)
/// Converts a color to its RGB color string (suitable for SVG)
fn rgb(color: Color) -> String {
let Color {red, green, blue, alpha: _} = color;
format!("rgb({}, {}, {})", red as u8, green as u8, blue as u8)
}

/// Converts a value into a string with the unit "px"
Expand All @@ -43,19 +45,19 @@ fn pairs(mut items: impl Iterator<Item=ScreenPoint>) -> String {
#[error("{0}")]
pub struct ExportError(String);

pub fn save_svg(
fn create_svg_document(
display_list: &DisplayList,
drawing: &DrawingState,
path: &FilePath,
) -> Result<(), ExportError> {
) -> Result<svg::Document, ExportError> {
let mut document = svg::Document::new()
.set("viewBox", (0, 0, drawing.width, drawing.height));

// set background color - https://stackoverflow.com/a/11293812/9276882
let background = Rectangle::new()
.set("width", "100%")
.set("height", "100%")
.set("fill", rgba(drawing.background));
.set("fill-opacity", drawing.background.alpha)
.set("fill", rgb(drawing.background));
document = document.add(background);

let center = drawing.center;
Expand All @@ -76,7 +78,8 @@ pub fn save_svg(
.set("y2", end.y)
.set("stroke-linecap", "round")
.set("stroke-linejoin", "round")
.set("stroke", rgba(color))
.set("stroke", rgb(color))
.set("stroke-opacity", color.alpha)
.set("stroke-width", px(thickness));

document = document.add(line);
Expand All @@ -93,12 +96,54 @@ pub fn save_svg(
let polygon = Polygon::new()
.set("points", pairs(points))
.set("fill-rule", "nonzero")
.set("fill", rgba(fill_color));
.set("fill-opacity", fill_color.alpha)
.set("fill", rgb(fill_color));

document = document.add(polygon);
},
}
}

Ok(document)
}


pub fn save_svg(
display_list: &DisplayList,
drawing: &DrawingState,
path: &FilePath,
) -> Result<(), ExportError> {
let document = create_svg_document(display_list, drawing)?;
svg::save(path, &document).map_err(|err| ExportError(err.to_string()))
}

pub fn save_png(
display_list: &DisplayList,
drawing: &DrawingState,
path: &FilePath,
) -> Result<(), ExportError> {
let document = create_svg_document(display_list, drawing)?;

// Some SVG features are not supported by nsvg:
// - Text elements are ignored, although text can simply be converted to a path and it will work just fine
// - Embedded bitmap images are ignored
// - Scripts are ignored
// - Animations are ignored
// If this becomes an issue we can "usvg" to convert the svg into a svg of just paths.
let svg = nsvg::parse_str(&document.to_string(), nsvg::Units::Pixel, 96.0)
.map_err(|err| ExportError(err.to_string()))?;

// Rasterize the loaded SVG and return an RgbaImage
let image = svg.rasterize(1.0).map_err(|err| ExportError(err.to_string()))?;

let (width, height) = image.dimensions();

// Write the image to disk as a PNG
image::save_buffer(
path,
&image.into_raw(),
width,
height,
image::ColorType::Rgba8,
).map_err(|err| ExportError(err.to_string()))
}