mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-27 02:28:12 +08:00
Added Subpaths to bezier-rs (#730)
* Added Subpath constructor, iterator and length * Asserted that subpaths of len < 2 cannot be closed. Added len() and comments * Added subpath to_svg(), made structs public, made quadratic use either in_handle or out_handle * add bezier handles * Added basic interactivity and index traits * Added SubPath interactivity * Added svg styling * Broke subpath impl across multiple files. More sylistic changes per review * Fixed format error * Added closed subpath to documentation page * Modified subpath to_svg to use functional style * Stylistic changes per review * Fixed build errors * More sylistic changes per review * Moved svg commands to constants * Moved formatting for svg arguments to ToSVGOptions * Renamed files in git * Even more stylistic changes per review
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
pub mod subpath;
|
||||
mod svg_drawing;
|
||||
|
||||
use bezier_rs::{Bezier, ProjectionOptions};
|
||||
use glam::DVec2;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -31,7 +34,7 @@ fn to_js_value<T: Serialize>(data: T) -> JsValue {
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl WasmBezier {
|
||||
/// Expect js_points to be a list of 3 pairs.
|
||||
/// Expect js_points to be a list of 2 pairs.
|
||||
pub fn new_linear(js_points: &JsValue) -> WasmBezier {
|
||||
let points: [DVec2; 2] = js_points.into_serde().unwrap();
|
||||
WasmBezier(Bezier::from_linear_dvec2(points[0], points[1]))
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
use bezier_rs::subpath::{ManipulatorGroup, Subpath, ToSVGOptions};
|
||||
use glam::DVec2;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use crate::svg_drawing::*;
|
||||
|
||||
/// Wrapper of the `Subpath` struct to be used in JS.
|
||||
#[wasm_bindgen]
|
||||
pub struct WasmSubpath(Subpath);
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl WasmSubpath {
|
||||
/// Expects js_points to be an unbounded list of triples, where each item is a tuple of floats.
|
||||
pub fn from_triples(js_points: &JsValue, closed: bool) -> WasmSubpath {
|
||||
let point_triples: Vec<[Option<DVec2>; 3]> = js_points.into_serde().unwrap();
|
||||
let manipulator_groups = point_triples
|
||||
.into_iter()
|
||||
.map(|point_triple| ManipulatorGroup {
|
||||
anchor: point_triple[0].unwrap(),
|
||||
in_handle: point_triple[1],
|
||||
out_handle: point_triple[2],
|
||||
})
|
||||
.collect();
|
||||
WasmSubpath(Subpath::new(manipulator_groups, closed))
|
||||
}
|
||||
|
||||
pub fn set_anchor(&mut self, index: usize, x: f64, y: f64) {
|
||||
self.0[index].anchor = DVec2::new(x, y);
|
||||
}
|
||||
|
||||
pub fn set_in_handle(&mut self, index: usize, x: f64, y: f64) {
|
||||
self.0[index].in_handle = Some(DVec2::new(x, y));
|
||||
}
|
||||
|
||||
pub fn set_out_handle(&mut self, index: usize, x: f64, y: f64) {
|
||||
self.0[index].out_handle = Some(DVec2::new(x, y));
|
||||
}
|
||||
|
||||
pub fn to_svg(&self) -> String {
|
||||
format!("{}{}{}", SVG_OPEN_TAG, self.0.to_svg(ToSVGOptions::default()), SVG_CLOSE_TAG)
|
||||
}
|
||||
|
||||
pub fn length(&self) -> String {
|
||||
let length_text = draw_text(format!("Length: {:.2}", self.0.length(None)), 5., 193., BLACK);
|
||||
format!("{}{}{}{}", SVG_OPEN_TAG, self.0.to_svg(ToSVGOptions::default()), length_text, SVG_CLOSE_TAG)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// SVG drawing constants
|
||||
pub const SVG_OPEN_TAG: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px">"#;
|
||||
pub const SVG_CLOSE_TAG: &str = "</svg>";
|
||||
|
||||
// Sylistic constants
|
||||
pub const BLACK: &str = "black";
|
||||
|
||||
/// Helper function to create an SVG text entitty.
|
||||
pub fn draw_text(text: String, x_pos: f64, y_pos: f64, fill: &str) -> String {
|
||||
format!(r#"<text x="{x_pos}" y="{y_pos}" fill="{fill}">{text}</text>"#)
|
||||
}
|
||||
Reference in New Issue
Block a user