Refactor bezier-rs' to_svg functionality (#758)

* Change Bezier to_svg to include handles and endpoints

* Move params into brackets of format macro, remove unused comment

* Use write macro instead of format

* Fix path for watched directory

* Refactor functions to remove ToSVGOptions
This commit is contained in:
Hannah Li
2022-09-01 23:13:39 -04:00
committed by Keavon Chambers
parent 1e109dc552
commit f74b6ed111
9 changed files with 388 additions and 347 deletions

View File

@@ -1,4 +1,5 @@
use super::*;
use std::fmt::Write;
/// Functionality relating to core `Bezier` operations, such as constructors and `abs_diff_eq`.
impl Bezier {
@@ -142,14 +143,44 @@ impl Bezier {
}
}
/// Convert `Bezier` to SVG `path`.
pub fn to_svg(&self) -> String {
format!(
r#"<path d="{SVG_ARG_MOVE}{} {} {}" stroke="black" fill="none"/>"#,
self.start.x,
self.start.y,
self.svg_curve_argument()
)
/// Appends to the `svg` mutable string with an SVG shape representation of the curve.
pub fn curve_to_svg(&self, svg: &mut String, attributes: String) {
let _ = write!(svg, r#"<path d="{SVG_ARG_MOVE}{} {} {}" {}/>"#, self.start.x, self.start.y, self.svg_curve_argument(), attributes);
}
/// Appends to the `svg` mutable string with an SVG shape representation of the handle lines.
pub fn handle_lines_to_svg(&self, svg: &mut String, attributes: String) {
let _ = write!(svg, r#"<path d="{}" {}/>"#, self.svg_handle_line_argument().unwrap_or_else(|| "".to_string()), attributes);
}
/// Appends to the `svg` mutable string with an SVG shape representation of the anchors.
pub fn anchors_to_svg(&self, svg: &mut String, attributes: String) {
let _ = write!(
svg,
r#"<circle cx="{}" cy="{}" {attributes}/><circle cx="{}" cy="{}" {attributes}/>"#,
self.start.x, self.start.y, self.end.x, self.end.y
);
}
/// Appends to the `svg` mutable string with an SVG shape representation of the handles.
pub fn handles_to_svg(&self, svg: &mut String, attributes: String) {
if let BezierHandles::Quadratic { handle } = self.handles {
let _ = write!(svg, r#"<circle cx="{}" cy="{}" {attributes}/>"#, handle.x, handle.y);
} else if let BezierHandles::Cubic { handle_start, handle_end } = self.handles {
let _ = write!(
svg,
r#"<circle cx="{}" cy="{}" {attributes}/><circle cx="{}" cy="{}" {attributes}/>"#,
handle_start.x, handle_start.y, handle_end.x, handle_end.y
);
};
}
/// Appends to the `svg` mutable string with an SVG shape representation that includes the curve, the handle lines, the anchors, and the handles.
pub fn to_svg(&self, svg: &mut String, curve_attributes: String, anchor_attributes: String, handle_attributes: String, handle_line_attributes: String) {
self.curve_to_svg(svg, curve_attributes);
self.handle_lines_to_svg(svg, handle_line_attributes);
self.anchors_to_svg(svg, anchor_attributes);
self.handles_to_svg(svg, handle_attributes);
}
/// Returns true if the corresponding points of the two `Bezier`s are within the provided absolute value difference from each other.

View File

@@ -3,7 +3,9 @@
mod bezier;
mod consts;
mod subpath;
mod svg;
mod utils;
pub use bezier::*;
pub use subpath::*;
pub use svg::*;

View File

@@ -1,5 +1,6 @@
use super::*;
use crate::consts::*;
use crate::ToSVGOptions;
/// Functionality relating to core `Subpath` operations, such as constructors and `iter`.
impl Subpath {

View File

@@ -6,78 +6,3 @@ pub struct ManipulatorGroup {
pub in_handle: Option<DVec2>,
pub out_handle: Option<DVec2>,
}
/// Structure to represent optional parameters that can be passed to the `into_svg` function.
pub struct ToSVGOptions {
/// Color of the line segments along the `Subpath`. Defaulted to `black`.
pub curve_stroke_color: String,
/// Width of the line segments along the `Subpath`. Defaulted to `2.`.
pub curve_stroke_width: f64,
/// Stroke color outlining circles marking anchors on the `Subpath`. Defaulted to `black`.
pub anchor_stroke_color: String,
/// Stroke width outlining circles marking anchors on the `Subpath`. Defaulted to `2.`.
pub anchor_stroke_width: f64,
/// Radius of the circles marking anchors on the `Subpath`. Defaulted to `4.`.
pub anchor_radius: f64,
/// Fill color of the circles marking anchors on the `Subpath`. Defaulted to `white`.
pub anchor_fill: String,
/// Color of the line segments connecting anchors to handle points. Defaulted to `gray`.
pub handle_line_stroke_color: String,
/// Width of the line segments connecting anchors to handle points. Defaulted to `1.`.
pub handle_line_stroke_width: f64,
/// Stroke color outlining circles marking the handles of `Subpath`. Defaulted to `gray`.
pub handle_point_stroke_color: String,
/// Stroke color outlining circles marking the handles of `Subpath`. Defaulted to `1.5`.
pub handle_point_stroke_width: f64,
/// Radius of the circles marking the handles of `Subpath`. Defaulted to `3.`.
pub handle_point_radius: f64,
/// Fill color of the circles marking the handles of `Subpath`. Defaulted to `white`.
pub handle_point_fill: String,
}
impl ToSVGOptions {
/// Combine and format curve styling options for an SVG path.
pub(crate) fn formatted_curve_arguments(&self) -> String {
format!(r#"stroke="{}" stroke-width="{}" fill="none""#, self.curve_stroke_color, self.curve_stroke_width)
}
/// Combine and format anchor styling options an SVG circle.
pub(crate) fn formatted_anchor_arguments(&self) -> String {
format!(
r#"r="{}", stroke="{}" stroke-width="{}" fill="{}""#,
self.anchor_radius, self.anchor_stroke_color, self.anchor_stroke_width, self.anchor_fill
)
}
/// Combine and format handle point styling options for an SVG circle.
pub(crate) fn formatted_handle_point_arguments(&self) -> String {
format!(
r#"r="{}", stroke="{}" stroke-width="{}" fill="{}""#,
self.handle_point_radius, self.handle_point_stroke_color, self.handle_point_stroke_width, self.handle_point_fill
)
}
/// Combine and format handle line styling options an SVG path.
pub(crate) fn formatted_handle_line_arguments(&self) -> String {
format!(r#"stroke="{}" stroke-width="{}" fill="none""#, self.handle_line_stroke_color, self.handle_line_stroke_width)
}
}
impl Default for ToSVGOptions {
fn default() -> Self {
ToSVGOptions {
curve_stroke_color: String::from("black"),
curve_stroke_width: 2.,
anchor_stroke_color: String::from("black"),
anchor_stroke_width: 2.,
anchor_radius: 4.,
anchor_fill: String::from("white"),
handle_line_stroke_color: String::from("gray"),
handle_line_stroke_width: 1.,
handle_point_stroke_color: String::from("gray"),
handle_point_stroke_width: 1.5,
handle_point_radius: 3.,
handle_point_fill: String::from("white"),
}
}
}

View File

@@ -0,0 +1,74 @@
/// Structure to represent optional parameters that can be passed to the `into_svg` function.
pub struct ToSVGOptions {
/// Color of the line segments along the `Subpath`. Defaulted to `black`.
pub curve_stroke_color: String,
/// Width of the line segments along the `Subpath`. Defaulted to `2.`.
pub curve_stroke_width: f64,
/// Stroke color outlining circles marking anchors on the `Subpath`. Defaulted to `black`.
pub anchor_stroke_color: String,
/// Stroke width outlining circles marking anchors on the `Subpath`. Defaulted to `2.`.
pub anchor_stroke_width: f64,
/// Radius of the circles marking anchors on the `Subpath`. Defaulted to `4.`.
pub anchor_radius: f64,
/// Fill color of the circles marking anchors on the `Subpath`. Defaulted to `white`.
pub anchor_fill: String,
/// Color of the line segments connecting anchors to handle points. Defaulted to `gray`.
pub handle_line_stroke_color: String,
/// Width of the line segments connecting anchors to handle points. Defaulted to `1.`.
pub handle_line_stroke_width: f64,
/// Stroke color outlining circles marking the handles of `Subpath`. Defaulted to `gray`.
pub handle_point_stroke_color: String,
/// Stroke color outlining circles marking the handles of `Subpath`. Defaulted to `1.5`.
pub handle_point_stroke_width: f64,
/// Radius of the circles marking the handles of `Subpath`. Defaulted to `3.`.
pub handle_point_radius: f64,
/// Fill color of the circles marking the handles of `Subpath`. Defaulted to `white`.
pub handle_point_fill: String,
}
impl ToSVGOptions {
/// Combine and format curve styling options for an SVG path.
pub(crate) fn formatted_curve_arguments(&self) -> String {
format!(r#"stroke="{}" stroke-width="{}" fill="none""#, self.curve_stroke_color, self.curve_stroke_width)
}
/// Combine and format anchor styling options an SVG circle.
pub(crate) fn formatted_anchor_arguments(&self) -> String {
format!(
r#"r="{}", stroke="{}" stroke-width="{}" fill="{}""#,
self.anchor_radius, self.anchor_stroke_color, self.anchor_stroke_width, self.anchor_fill
)
}
/// Combine and format handle point styling options for an SVG circle.
pub(crate) fn formatted_handle_point_arguments(&self) -> String {
format!(
r#"r="{}", stroke="{}" stroke-width="{}" fill="{}""#,
self.handle_point_radius, self.handle_point_stroke_color, self.handle_point_stroke_width, self.handle_point_fill
)
}
/// Combine and format handle line styling options an SVG path.
pub(crate) fn formatted_handle_line_arguments(&self) -> String {
format!(r#"stroke="{}" stroke-width="{}" fill="none""#, self.handle_line_stroke_color, self.handle_line_stroke_width)
}
}
impl Default for ToSVGOptions {
fn default() -> Self {
ToSVGOptions {
curve_stroke_color: String::from("black"),
curve_stroke_width: 2.,
anchor_stroke_color: String::from("black"),
anchor_stroke_width: 2.,
anchor_radius: 4.,
anchor_fill: String::from("white"),
handle_line_stroke_color: String::from("gray"),
handle_line_stroke_width: 1.,
handle_point_stroke_color: String::from("gray"),
handle_point_stroke_width: 1.5,
handle_point_radius: 3.,
handle_point_fill: String::from("white"),
}
}
}