Enable unrestricted types for CanvasRenderingContext2D.

This commit is contained in:
Tetsuharu OHZEKI 2015-03-04 21:45:29 +09:00
parent 9cd1b2c158
commit 05c6d046dd
3 changed files with 68 additions and 42 deletions

View file

@ -9,6 +9,7 @@ use dom::bindings::codegen::Bindings::CanvasGradientBinding;
use dom::bindings::codegen::Bindings::CanvasGradientBinding::CanvasGradientMethods; use dom::bindings::codegen::Bindings::CanvasGradientBinding::CanvasGradientMethods;
use dom::bindings::global::GlobalRef; use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JSRef, Temporary}; use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::num::Finite;
use dom::bindings::utils::{Reflector, reflect_dom_object}; use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::canvasrenderingcontext2d::parse_color; use dom::canvasrenderingcontext2d::parse_color;
@ -41,7 +42,7 @@ impl CanvasGradient {
} }
impl<'a> CanvasGradientMethods for JSRef<'a, CanvasGradient> { impl<'a> CanvasGradientMethods for JSRef<'a, CanvasGradient> {
fn AddColorStop(self, offset: f32, color: String) { fn AddColorStop(self, offset: Finite<f32>, color: String) {
let default_black = RGBA { let default_black = RGBA {
red: 0.0, red: 0.0,
green: 0.0, green: 0.0,
@ -50,7 +51,7 @@ impl<'a> CanvasGradientMethods for JSRef<'a, CanvasGradient> {
}; };
self.stops.borrow_mut().push(CanvasGradientStop { self.stops.borrow_mut().push(CanvasGradientStop {
offset: offset as f64, offset: (*offset) as f64,
color: parse_color(color.as_slice()).unwrap_or(default_black), color: parse_color(color.as_slice()).unwrap_or(default_black),
}); });
} }

View file

@ -12,6 +12,7 @@ use dom::bindings::error::Error::{IndexSize, TypeError};
use dom::bindings::error::Fallible; use dom::bindings::error::Fallible;
use dom::bindings::global::{GlobalRef, GlobalField}; use dom::bindings::global::{GlobalRef, GlobalField};
use dom::bindings::js::{JS, JSRef, LayoutJS, Temporary}; use dom::bindings::js::{JS, JSRef, LayoutJS, Temporary};
use dom::bindings::num::Finite;
use dom::bindings::utils::{Reflector, reflect_dom_object}; use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::canvasgradient::{CanvasGradient, CanvasGradientStyle, ToFillOrStrokeStyle}; use dom::canvasgradient::{CanvasGradient, CanvasGradientStyle, ToFillOrStrokeStyle};
use dom::htmlcanvaselement::{HTMLCanvasElement, HTMLCanvasElementHelpers}; use dom::htmlcanvaselement::{HTMLCanvasElement, HTMLCanvasElementHelpers};
@ -383,7 +384,13 @@ impl<'a> CanvasRenderingContext2DMethods for JSRef<'a, CanvasRenderingContext2D>
Point2D(x as f32, y as f32))).unwrap(); Point2D(x as f32, y as f32))).unwrap();
} }
fn Arc(self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) { fn Arc(self, x: Finite<f64>, y: Finite<f64>, r: Finite<f64>, start: Finite<f64>, end: Finite<f64>, ccw: bool) {
let x = *x;
let y = *y;
let r = *r;
let start = *start;
let end = *end;
self.renderer.send(CanvasMsg::Arc(Point2D(x as f32, y as f32), r as f32, self.renderer.send(CanvasMsg::Arc(Point2D(x as f32, y as f32), r as f32,
start as f32, end as f32, ccw)).unwrap(); start as f32, end as f32, ccw)).unwrap();
} }
@ -467,7 +474,10 @@ impl<'a> CanvasRenderingContext2DMethods for JSRef<'a, CanvasRenderingContext2D>
Ok(ImageData::new(self.global.root().r(), imagedata.Width(), imagedata.Height(), None)) Ok(ImageData::new(self.global.root().r(), imagedata.Width(), imagedata.Height(), None))
} }
fn GetImageData(self, sx: f64, sy: f64, sw: f64, sh: f64) -> Fallible<Temporary<ImageData>> { fn GetImageData(self, sx: Finite<f64>, sy: Finite<f64>, sw: Finite<f64>, sh: Finite<f64>) -> Fallible<Temporary<ImageData>> {
let sw = *sw;
let sh = *sh;
if sw == 0.0 || sh == 0.0 { if sw == 0.0 || sh == 0.0 {
return Err(IndexSize) return Err(IndexSize)
} }
@ -480,15 +490,15 @@ impl<'a> CanvasRenderingContext2DMethods for JSRef<'a, CanvasRenderingContext2D>
Ok(ImageData::new(self.global.root().r(), sw.abs().to_u32().unwrap(), sh.abs().to_u32().unwrap(), Some(data))) Ok(ImageData::new(self.global.root().r(), sw.abs().to_u32().unwrap(), sh.abs().to_u32().unwrap(), Some(data)))
} }
fn PutImageData(self, imagedata: JSRef<ImageData>, dx: f64, dy: f64) { fn PutImageData(self, imagedata: JSRef<ImageData>, dx: Finite<f64>, dy: Finite<f64>) {
let data = imagedata.get_data_array(&self.global.root().r()); let data = imagedata.get_data_array(&self.global.root().r());
let image_data_rect = Rect(Point2D(dx.to_i32().unwrap(), dy.to_i32().unwrap()), imagedata.get_size()); let image_data_rect = Rect(Point2D(dx.to_i32().unwrap(), dy.to_i32().unwrap()), imagedata.get_size());
let dirty_rect = None; let dirty_rect = None;
self.renderer.send(CanvasMsg::PutImageData(data, image_data_rect, dirty_rect)).unwrap() self.renderer.send(CanvasMsg::PutImageData(data, image_data_rect, dirty_rect)).unwrap()
} }
fn PutImageData_(self, imagedata: JSRef<ImageData>, dx: f64, dy: f64, fn PutImageData_(self, imagedata: JSRef<ImageData>, dx: Finite<f64>, dy: Finite<f64>,
dirtyX: f64, dirtyY: f64, dirtyWidth: f64, dirtyHeight: f64) { dirtyX: Finite<f64>, dirtyY: Finite<f64>, dirtyWidth: Finite<f64>, dirtyHeight: Finite<f64>) {
let data = imagedata.get_data_array(&self.global.root().r()); let data = imagedata.get_data_array(&self.global.root().r());
let image_data_rect = Rect(Point2D(dx.to_i32().unwrap(), dy.to_i32().unwrap()), let image_data_rect = Rect(Point2D(dx.to_i32().unwrap(), dy.to_i32().unwrap()),
Size2D(imagedata.Width().to_i32().unwrap(), Size2D(imagedata.Width().to_i32().unwrap(),
@ -499,7 +509,13 @@ impl<'a> CanvasRenderingContext2DMethods for JSRef<'a, CanvasRenderingContext2D>
self.renderer.send(CanvasMsg::PutImageData(data, image_data_rect, dirty_rect)).unwrap() self.renderer.send(CanvasMsg::PutImageData(data, image_data_rect, dirty_rect)).unwrap()
} }
fn CreateLinearGradient(self, x0: f64, y0: f64, x1: f64, y1: f64) -> Fallible<Temporary<CanvasGradient>> { fn CreateLinearGradient(self, x0: Finite<f64>, y0: Finite<f64>,
x1: Finite<f64>, y1: Finite<f64>) -> Fallible<Temporary<CanvasGradient>> {
let x0 = *x0;
let y0 = *y0;
let x1 = *x1;
let y1 = *y1;
if [x0, y0, x1, y1].iter().any(|x| x.is_nan() || x.is_infinite()) { if [x0, y0, x1, y1].iter().any(|x| x.is_nan() || x.is_infinite()) {
return Err(TypeError("One of the arguments of createLinearGradient() is not a finite floating-point value.".to_owned())); return Err(TypeError("One of the arguments of createLinearGradient() is not a finite floating-point value.".to_owned()));
} }
@ -507,7 +523,15 @@ impl<'a> CanvasRenderingContext2DMethods for JSRef<'a, CanvasRenderingContext2D>
CanvasGradientStyle::Linear(LinearGradientStyle::new(x0, y0, x1, y1, Vec::new())))) CanvasGradientStyle::Linear(LinearGradientStyle::new(x0, y0, x1, y1, Vec::new()))))
} }
fn CreateRadialGradient(self, x0: f64, y0: f64, r0: f64, x1: f64, y1: f64, r1: f64) -> Fallible<Temporary<CanvasGradient>> { fn CreateRadialGradient(self, x0: Finite<f64>, y0: Finite<f64>, r0: Finite<f64>,
x1: Finite<f64>, y1: Finite<f64>, r1: Finite<f64>) -> Fallible<Temporary<CanvasGradient>> {
let x0 = *x0;
let y0 = *y0;
let r0 = *r0;
let x1 = *x1;
let y1 = *y1;
let r1 = *r1;
if [x0, y0, r0, x1, y1, r1].iter().any(|x| x.is_nan() || x.is_infinite()) { if [x0, y0, r0, x1, y1, r1].iter().any(|x| x.is_nan() || x.is_infinite()) {
return Err(TypeError("One of the arguments of createRadialGradient() is not a finite floating-point value.".to_owned())); return Err(TypeError("One of the arguments of createRadialGradient() is not a finite floating-point value.".to_owned()));
} }

View file

@ -31,21 +31,21 @@ interface CanvasRenderingContext2D {
// transformations (default transform is the identity matrix) // transformations (default transform is the identity matrix)
// attribute SVGMatrix currentTransform; // attribute SVGMatrix currentTransform;
void scale(/*unrestricted*/ double x, /*unrestricted*/ double y); void scale(unrestricted double x, unrestricted double y);
//void rotate(unrestricted double angle); //void rotate(unrestricted double angle);
void translate(/*unrestricted*/ double x, /*unrestricted*/ double y); void translate(unrestricted double x, unrestricted double y);
void transform(/*unrestricted*/ double a, void transform(unrestricted double a,
/*unrestricted*/ double b, unrestricted double b,
/*unrestricted*/ double c, unrestricted double c,
/*unrestricted*/ double d, unrestricted double d,
/*unrestricted*/ double e, unrestricted double e,
/*unrestricted*/ double f); unrestricted double f);
void setTransform(/*unrestricted*/ double a, void setTransform(unrestricted double a,
/*unrestricted*/ double b, unrestricted double b,
/*unrestricted*/ double c, unrestricted double c,
/*unrestricted*/ double d, unrestricted double d,
/*unrestricted*/ double e, unrestricted double e,
/*unrestricted*/ double f); unrestricted double f);
//void resetTransform(); //void resetTransform();
// compositing // compositing
@ -71,15 +71,12 @@ interface CanvasRenderingContext2D {
// attribute DOMString shadowColor; // (default transparent black) // attribute DOMString shadowColor; // (default transparent black)
// rects // rects
//void clearRect(unrestricted double x, unrestricted double y, unrestricted double w, unrestricted double h);
//[LenientFloat] //[LenientFloat]
void clearRect(double x, double y, double w, double h); void clearRect(unrestricted double x, unrestricted double y, unrestricted double w, unrestricted double h);
//void fillRect(unrestricted double x, unrestricted double y, unrestricted double w, unrestricted double h);
//[LenientFloat] //[LenientFloat]
void fillRect(double x, double y, double w, double h); void fillRect(unrestricted double x, unrestricted double y, unrestricted double w, unrestricted double h);
//void strokeRect(unrestricted double x, unrestricted double y, unrestricted double w, unrestricted double h);
//[LenientFloat] //[LenientFloat]
void strokeRect(double x, double y, double w, double h); void strokeRect(unrestricted double x, unrestricted double y, unrestricted double w, unrestricted double h);
// path API (see also CanvasPathMethods) // path API (see also CanvasPathMethods)
void beginPath(); void beginPath();
@ -108,11 +105,15 @@ interface CanvasRenderingContext2D {
// drawing images // drawing images
[Throws] [Throws]
void drawImage(CanvasImageSource image, /* unrestricted */ double dx, /* unrestricted */ double dy); void drawImage(CanvasImageSource image, unrestricted double dx, unrestricted double dy);
[Throws] [Throws]
void drawImage(CanvasImageSource image, /* unrestricted */ double dx, /* unrestricted */ double dy, /* unrestricted */ double dw, /* unrestricted */ double dh); void drawImage(CanvasImageSource image, unrestricted double dx, unrestricted double dy,
unrestricted double dw, unrestricted double dh);
[Throws] [Throws]
void drawImage(CanvasImageSource image, /* unrestricted */ double sx, /* unrestricted */ double sy, /* unrestricted */ double sw, /* unrestricted */ double sh, /* unrestricted */ double dx, /* unrestricted */ double dy, /* unrestricted */ double dw, /* unrestricted */ double dh); void drawImage(CanvasImageSource image, unrestricted double sx, unrestricted double sy,
unrestricted double sw, unrestricted double sh,
unrestricted double dx, unrestricted double dy,
unrestricted double dw, unrestricted double dh);
// hit regions // hit regions
//void addHitRegion(optional HitRegionOptions options); //void addHitRegion(optional HitRegionOptions options);
@ -120,7 +121,7 @@ interface CanvasRenderingContext2D {
// pixel manipulation // pixel manipulation
[Throws] [Throws]
ImageData createImageData(double sw, double sh); ImageData createImageData(unrestricted double sw, unrestricted double sh);
[Throws] [Throws]
ImageData createImageData(ImageData imagedata); ImageData createImageData(ImageData imagedata);
[Throws] [Throws]
@ -133,16 +134,16 @@ interface CanvasRenderingContext2D {
interface CanvasPathMethods { interface CanvasPathMethods {
// shared path API methods // shared path API methods
void closePath(); void closePath();
void moveTo(/*unrestricted*/ double x, /*unrestricted*/ double y); void moveTo(unrestricted double x, unrestricted double y);
void lineTo(double x, double y); void lineTo(unrestricted double x, unrestricted double y);
void quadraticCurveTo(double cpx, double cpy, double x, double y); void quadraticCurveTo(unrestricted double cpx, unrestricted double cpy, unrestricted double x, unrestricted double y);
void bezierCurveTo(/*unrestricted*/ double cp1x, void bezierCurveTo(unrestricted double cp1x,
/*unrestricted*/ double cp1y, unrestricted double cp1y,
/*unrestricted*/ double cp2x, unrestricted double cp2x,
/*unrestricted*/ double cp2y, unrestricted double cp2y,
/*unrestricted*/ double x, unrestricted double x,
/*unrestricted*/ double y); unrestricted double y);
//void arcTo(double x1, double y1, double x2, double y2, double radius); //void arcTo(double x1, double y1, double x2, double y2, double radius);
// NOT IMPLEMENTED [LenientFloat] void arcTo(double x1, double y1, double x2, double y2, double radiusX, double radiusY, double rotation); // NOT IMPLEMENTED [LenientFloat] void arcTo(double x1, double y1, double x2, double y2, double radiusX, double radiusY, double rotation);