Skip to content

Commit

Permalink
Implement IsPointInPath
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Dec 6, 2015
1 parent 2af23dc commit 789a90a
Show file tree
Hide file tree
Showing 23 changed files with 71 additions and 128 deletions.
11 changes: 11 additions & 0 deletions components/canvas/canvas_paint_task.rs
Expand Up @@ -136,6 +136,9 @@ impl<'a> CanvasPaintTask<'a> {
Canvas2dMsg::Fill => painter.fill(),
Canvas2dMsg::Stroke => painter.stroke(),
Canvas2dMsg::Clip => painter.clip(),
Canvas2dMsg::IsPointInPath(x, y, fill_rule, chan) => {
painter.is_point_in_path(x, y, fill_rule, chan)
},
Canvas2dMsg::DrawImage(imagedata, image_size, dest_rect, source_rect,
smoothing_enabled) => {
painter.draw_image(imagedata, image_size, dest_rect, source_rect, smoothing_enabled)
Expand Down Expand Up @@ -318,6 +321,14 @@ impl<'a> CanvasPaintTask<'a> {
self.drawtarget.push_clip(&self.path_builder.finish());
}

fn is_point_in_path(&mut self, x: f64, y: f64,
_fill_rule: FillRule, chan: IpcSender<bool>) {
let path = self.path_builder.finish();
let result = path.contains_point(x, y, &self.state.transform);
self.path_builder = path.copy_to_builder();
chan.send(result).unwrap();
}

fn draw_image(&self, image_data: Vec<u8>, image_size: Size2D<f64>,
dest_rect: Rect<f64>, source_rect: Rect<f64>, smoothing_enabled: bool) {
// We round up the floating pixel values to draw the pixels
Expand Down
7 changes: 7 additions & 0 deletions components/canvas_traits/lib.rs
Expand Up @@ -42,6 +42,12 @@ use std::str::FromStr;
use std::sync::mpsc::Sender;
use util::mem::HeapSizeOf;

#[derive(Clone, Deserialize, Serialize)]
pub enum FillRule {
Nonzero,
Evenodd,
}

#[derive(Clone, Deserialize, Serialize)]
pub enum CanvasMsg {
Canvas2d(Canvas2dMsg),
Expand Down Expand Up @@ -93,6 +99,7 @@ pub enum Canvas2dMsg {
Fill,
FillRect(Rect<f32>),
GetImageData(Rect<i32>, Size2D<f64>, IpcSender<Vec<u8>>),
IsPointInPath(f64, f64, FillRule, IpcSender<bool>),
LineTo(Point2D<f32>),
MoveTo(Point2D<f32>),
PutImageData(Vec<u8>, Point2D<f64>, Size2D<f64>, Rect<f64>),
Expand Down
15 changes: 14 additions & 1 deletion components/script/dom/canvasrenderingcontext2d.rs
Expand Up @@ -5,7 +5,7 @@
use canvas::canvas_paint_task::RectToi32;
use canvas_traits::{Canvas2dMsg, CanvasCommonMsg, CanvasMsg};
use canvas_traits::{CompositionOrBlending, LineCapStyle, LineJoinStyle};
use canvas_traits::{FillOrStrokeStyle, LinearGradientStyle, RadialGradientStyle, RepetitionStyle};
use canvas_traits::{FillOrStrokeStyle, FillRule, LinearGradientStyle, RadialGradientStyle, RepetitionStyle};
use cssparser::Color as CSSColor;
use cssparser::{Parser, RGBA};
use dom::bindings::cell::DOMRefCell;
Expand Down Expand Up @@ -695,6 +695,19 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
self.ipc_renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::Clip)).unwrap();
}

// https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath
fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
let fill_rule = match fill_rule {
CanvasFillRule::Nonzero => FillRule::Nonzero,
CanvasFillRule::Evenodd => FillRule::Evenodd,
};
let (sender, receiver) = ipc::channel::<bool>().unwrap();
self.ipc_renderer
.send(CanvasMsg::Canvas2d(Canvas2dMsg::IsPointInPath(x, y, fill_rule, sender)))
.unwrap();
receiver.recv().unwrap()
}

// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage(&self,
image: HTMLImageElementOrHTMLCanvasElementOrCanvasRenderingContext2D,
Expand Down
9 changes: 4 additions & 5 deletions components/script/dom/webidls/CanvasRenderingContext2D.webidl
Expand Up @@ -94,13 +94,12 @@ interface CanvasRenderingContext2D {
void clip(optional CanvasFillRule fillRule = "nonzero");
//void clip(Path2D path, optional CanvasFillRule fillRule = "nonzero");
//void resetClip();
//boolean isPointInPath(unrestricted double x, unrestricted double y,
boolean isPointInPath(unrestricted double x, unrestricted double y,
optional CanvasFillRule fillRule = "nonzero");
//boolean isPointInPath(Path2D path, unrestricted double x, unrestricted double y,
// optional CanvasFillRule fillRule = "nonzero");
//boolean isPointInPath(Path2D path, unrestricted double x, unrestricted
// double y, optional CanvasFillRule fillRule = "nonzero");
//boolean isPointInStroke(unrestricted double x, unrestricted double y);
// boolean isPointInStroke(Path2D path, unrestricted double x,
// unrestricted double y);
//boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y);

// text (see also the CanvasDrawingStyles interface)
//void fillText(DOMString text, unrestricted double x, unrestricted double y,
Expand Down
22 changes: 11 additions & 11 deletions components/servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions ports/cef/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions ports/gonk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 789a90a

Please sign in to comment.