servo/components/script/dom/path2d.rs
sagudev 9b5b26386c
canvas: Use wrapped kurbo::BezPath for path everywhere (#37967)
This PR removes existing path(segment) abstractions in favor of
`kurbo::BezPath`, well actually wrapped `kurbo::BezPath`, to ensure
building of valid paths. This allows us better Path2D building in script
and doing all validation and segmentation there and also allows us
remove blocking is_point_in_path on Path2D as we can now do this in
script. Current path is still done on canvas thread side as it will be
harder to move to script (will be done as a follow up), but it now uses
this new path abstraction.

Using kurbo also allows us to ditch our manual svgpath parser with the
one provided by kurbo.

Same code is stolen from: https://github.com/servo/servo/pull/36821.

Testing: Existing WPT tests
Fixes: #37904

wpt run: https://github.com/sagudev/servo/actions/runs/16172191716

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-07-12 10:37:47 +00:00

177 lines
5.2 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::RefCell;
use canvas_traits::canvas::Path;
use dom_struct::dom_struct;
use js::rust::HandleObject;
use script_bindings::str::DOMString;
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::Path2DMethods;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct Path2D {
reflector_: Reflector,
#[ignore_malloc_size_of = "Defined in kurbo."]
#[no_trace]
path: RefCell<Path>,
}
impl Path2D {
pub(crate) fn new() -> Path2D {
Self {
reflector_: Reflector::new(),
path: RefCell::new(Path::new()),
}
}
pub(crate) fn new_with_path(other: &Path2D) -> Path2D {
Self {
reflector_: Reflector::new(),
path: other.path.clone(),
}
}
pub(crate) fn new_with_str(path: &str) -> Path2D {
Self {
reflector_: Reflector::new(),
path: RefCell::new(Path::from_svg(path)),
}
}
pub(crate) fn segments(&self) -> Path {
self.path.borrow().clone()
}
}
impl Path2DMethods<crate::DomTypeHolder> for Path2D {
/// <https://html.spec.whatwg.org/multipage/#dom-path2d-addpath>
fn AddPath(&self, other: &Path2D) {
let other = other.segments();
// Step 7. Add all the subpaths in c to a.
self.path.borrow_mut().0.extend(other.0);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-closepath>
fn ClosePath(&self) {
self.path.borrow_mut().close_path();
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-moveto>
fn MoveTo(&self, x: f64, y: f64) {
self.path.borrow_mut().move_to(x, y);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-lineto>
fn LineTo(&self, x: f64, y: f64) {
self.path.borrow_mut().line_to(x, y);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-quadraticcurveto>
fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
self.path.borrow_mut().quadratic_curve_to(cpx, cpy, x, y);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-beziercurveto>
fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
self.path
.borrow_mut()
.bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arcto>
fn ArcTo(&self, x1: f64, y1: f64, x2: f64, y2: f64, radius: f64) -> Fallible<()> {
self.path
.borrow_mut()
.arc_to(x1, y1, x2, y2, radius)
.map_err(|_| Error::IndexSize)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-rect>
fn Rect(&self, x: f64, y: f64, w: f64, h: f64) {
self.path.borrow_mut().rect(x, y, w, h);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arc>
fn Arc(
&self,
x: f64,
y: f64,
radius: f64,
start_angle: f64,
end_angle: f64,
counterclockwise: bool,
) -> Fallible<()> {
self.path
.borrow_mut()
.arc(x, y, radius, start_angle, end_angle, counterclockwise)
.map_err(|_| Error::IndexSize)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ellipse>
fn Ellipse(
&self,
x: f64,
y: f64,
radius_x: f64,
radius_y: f64,
rotation_angle: f64,
start_angle: f64,
end_angle: f64,
counterclockwise: bool,
) -> Fallible<()> {
self.path
.borrow_mut()
.ellipse(
x,
y,
radius_x,
radius_y,
rotation_angle,
start_angle,
end_angle,
counterclockwise,
)
.map_err(|_| Error::IndexSize)
}
/// <https://html.spec.whatwg.org/multipage/#dom-path2d-dev>
fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<Path2D> {
reflect_dom_object_with_proto(Box::new(Self::new()), global, proto, can_gc)
}
/// <https://html.spec.whatwg.org/multipage/#dom-path2d-dev>
fn Constructor_(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
other: &Path2D,
) -> DomRoot<Path2D> {
reflect_dom_object_with_proto(Box::new(Self::new_with_path(other)), global, proto, can_gc)
}
/// <https://html.spec.whatwg.org/multipage/#dom-path2d-dev>
fn Constructor__(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
path_string: DOMString,
) -> DomRoot<Path2D> {
reflect_dom_object_with_proto(
Box::new(Self::new_with_str(path_string.str())),
global,
proto,
can_gc,
)
}
}