Skip to content

Commit

Permalink
Merge pull request #83 from rust-skia/as-ref
Browse files Browse the repository at this point in the history
Complete AsRef<> support for all functions that receive Rect, IRect, and RRect
  • Loading branch information
Brooooooklyn committed Apr 17, 2019
2 parents f1f23a8 + 088c1af commit 0705887
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 82 deletions.
2 changes: 1 addition & 1 deletion skia-safe/examples/skia-org/skcanvas_overview.rs
Expand Up @@ -56,7 +56,7 @@ fn draw_hello_skia(canvas: &mut Canvas) {

let oval = RRect::new_oval(rect).offset((40.0, 60.0));
paint.set_color(Color::BLUE);
canvas.draw_rrect(&oval, &paint);
canvas.draw_rrect(oval, &paint);

paint.set_color(Color::CYAN);
canvas.draw_circle((180.0, 50.0), 25.0, &paint);
Expand Down
2 changes: 1 addition & 1 deletion skia-safe/examples/skia-org/skpaint_overview.rs
Expand Up @@ -341,7 +341,7 @@ fn draw_line_2d_effect(canvas: &mut Canvas) {
fn draw_path_1d_effect(canvas: &mut Canvas) {
let mut paint = Paint::default();
let mut path = Path::default();
path.add_oval(&Rect::from_size((16.0, 6.0)), None);
path.add_oval(Rect::from_size((16.0, 6.0)), None);
paint.set_path_effect(Path1DPathEffect::new(
&path, 32.0, 0.0, Path1DPathEffectStyle::Rotate).as_ref());
paint.set_anti_alias(true);
Expand Down
8 changes: 4 additions & 4 deletions skia-safe/src/core/bitmap.rs
Expand Up @@ -226,8 +226,8 @@ impl Handle<SkBitmap> {
unsafe { C_SkBitmap_eraseARGB(self.native(), a.into(), r.into(), g.into(), b.into()) }
}

pub fn erase<C: Into<Color>>(&self, c: C, area: &IRect) {
unsafe { self.native().erase(c.into().into_native(), &area.into_native()) }
pub fn erase<C: Into<Color>, IR: AsRef<IRect>>(&self, c: C, area: IR) {
unsafe { self.native().erase(c.into().into_native(), area.as_ref().native()) }
}

pub fn get_color(&self, p: IPoint) -> Color {
Expand All @@ -243,8 +243,8 @@ impl Handle<SkBitmap> {
self.native().getAddr(p.x, p.y)
}

pub fn extract_subset(&self, dst: &mut Self, subset: &IRect) -> bool {
unsafe { self.native().extractSubset(dst.native_mut(), &subset.into_native() ) }
pub fn extract_subset<IR: AsRef<IRect>>(&self, dst: &mut Self, subset: IR) -> bool {
unsafe { self.native().extractSubset(dst.native_mut(), subset.as_ref().native() ) }
}

pub unsafe fn read_pixels(&self, dst_info: &ImageInfo, dst_pixels: *mut ffi::c_void, dst_row_bytes: usize, src_x: i32, src_y: i32) -> bool {
Expand Down
31 changes: 15 additions & 16 deletions skia-safe/src/core/canvas.rs
Expand Up @@ -462,10 +462,10 @@ impl Canvas {
self
}

pub fn clip_rrect(&mut self, rrect: &RRect, options: CanvasClipOptions) -> &mut Self {
pub fn clip_rrect<RR: AsRef<RRect>>(&mut self, rrect: RR, options: CanvasClipOptions) -> &mut Self {
unsafe {
self.native_mut().clipRRect(
rrect.native(),
rrect.as_ref().native(),
options.op.into_native(), options.do_anti_alias)
}
self
Expand Down Expand Up @@ -568,9 +568,9 @@ impl Canvas {
self
}

pub fn draw_irect(&mut self, rect: &IRect, paint: &Paint) -> &mut Self {
pub fn draw_irect<IR: AsRef<IRect>>(&mut self, rect: IR, paint: &Paint) -> &mut Self {
unsafe {
self.native_mut().drawIRect(rect.native(), paint.native())
self.native_mut().drawIRect(rect.as_ref().native(), paint.native())
}
self
}
Expand All @@ -589,16 +589,16 @@ impl Canvas {
self
}

pub fn draw_rrect(&mut self, rrect: &RRect, paint: &Paint) -> &mut Self {
pub fn draw_rrect<RR: AsRef<RRect>>(&mut self, rrect: RR, paint: &Paint) -> &mut Self {
unsafe {
self.native_mut().drawRRect(rrect.native(), paint.native())
self.native_mut().drawRRect(rrect.as_ref().native(), paint.native())
}
self
}

pub fn draw_drrect(&mut self, outer: &RRect, inner: &RRect, paint: &Paint) -> &mut Self {
pub fn draw_drrect<ORR: AsRef<RRect>, IRR: AsRef<RRect>>(&mut self, outer: ORR, inner: IRR, paint: &Paint) -> &mut Self {
unsafe {
self.native_mut().drawDRRect(outer.native(), inner.native(), paint.native())
self.native_mut().drawDRRect(outer.as_ref().native(), inner.as_ref().native(), paint.native())
}
self
}
Expand Down Expand Up @@ -670,12 +670,12 @@ impl Canvas {
self
}

pub fn draw_image_nine<R: AsRef<Rect>>(
&mut self, image: &Image, center: &IRect,
dst: R, paint: Option<&Paint>) -> &mut Self {
pub fn draw_image_nine<CIR: AsRef<IRect>, DR: AsRef<Rect>>(
&mut self, image: &Image, center: CIR,
dst: DR, paint: Option<&Paint>) -> &mut Self {
unsafe {
self.native_mut().drawImageNine(
image.native(), center.native(),
image.native(), center.as_ref().native(),
dst.as_ref().native(), paint.native_ptr_or_null())
}
self
Expand Down Expand Up @@ -716,12 +716,12 @@ impl Canvas {
self
}

pub fn draw_bitmap_nine<DR: AsRef<Rect>>(
&mut self, bitmap: &Bitmap, center: &IRect,
pub fn draw_bitmap_nine<CIR: AsRef<IRect>, DR: AsRef<Rect>>(
&mut self, bitmap: &Bitmap, center: CIR,
dst: DR, paint: Option<&Paint>) -> &mut Self {
unsafe {
self.native_mut().drawBitmapNine(
bitmap.native(), center.native(),
bitmap.native(), center.as_ref().native(),
dst.as_ref().native(), paint.native_ptr_or_null())
}
self
Expand Down Expand Up @@ -855,7 +855,6 @@ impl Canvas {
}

impl QuickReject<Rect> for Canvas {
// TODO: can we support AsRef<Rect> here?
fn quick_reject(&self, other: &Rect) -> bool {
unsafe {
self.native().quickReject(other.native())
Expand Down
4 changes: 2 additions & 2 deletions skia-safe/src/core/matrix.rs
Expand Up @@ -365,9 +365,9 @@ impl Matrix {
self
}

pub fn from_rect_to_rect(src: &Rect, dst: &Rect, stf: MatrixScaleToFit) -> Option<Matrix> {
pub fn from_rect_to_rect<SR: AsRef<Rect>, DR: AsRef<Rect>>(src: SR, dst: DR, stf: MatrixScaleToFit) -> Option<Matrix> {
let mut m = Matrix::new_identity();
unsafe { m.native_mut().setRectToRect(&src.into_native(), &dst.into_native(), stf.native().to_owned()) }
unsafe { m.native_mut().setRectToRect(src.as_ref().native(), dst.as_ref().native(), stf.native().to_owned()) }
.if_true_some(m)
}

Expand Down
28 changes: 14 additions & 14 deletions skia-safe/src/core/path.rs
Expand Up @@ -298,8 +298,8 @@ impl Handle<SkPath> {
})
}

pub fn conservatively_contains_rect(&self, rect: &Rect) -> bool {
unsafe { self.native().conservativelyContainsRect(rect.native()) }
pub fn conservatively_contains_rect<R: AsRef<Rect>>(&self, rect: R) -> bool {
unsafe { self.native().conservativelyContainsRect(rect.as_ref().native()) }
}

pub fn inc_reserve(&mut self, extra_pt_count: usize) -> &mut Self {
Expand Down Expand Up @@ -367,8 +367,8 @@ impl Handle<SkPath> {
self
}

pub fn arc_to(&mut self, oval: &Rect, start_angle: scalar, sweep_angle: scalar, force_move_to: bool) -> &mut Self {
unsafe { self.native_mut().arcTo(oval.native(), start_angle, sweep_angle, force_move_to) };
pub fn arc_to<O: AsRef<Rect>>(&mut self, oval: O, start_angle: scalar, sweep_angle: scalar, force_move_to: bool) -> &mut Self {
unsafe { self.native_mut().arcTo(oval.as_ref().native(), start_angle, sweep_angle, force_move_to) };
self
}

Expand Down Expand Up @@ -428,20 +428,20 @@ impl Handle<SkPath> {
.if_true_some((rects, dirs))
}

pub fn add_rect(&mut self, rect: &Rect, dir_start: Option<(PathDirection, usize)>) -> &mut Self {
pub fn add_rect<R: AsRef<Rect>>(&mut self, rect: R, dir_start: Option<(PathDirection, usize)>) -> &mut Self {
let dir = dir_start.map(|ds| ds.0).unwrap_or_default();
let start = dir_start.map(|ds| ds.1).unwrap_or_default();
unsafe {
self.native_mut().addRect1(rect.native(), dir.into_native(), start.try_into().unwrap())
self.native_mut().addRect1(rect.as_ref().native(), dir.into_native(), start.try_into().unwrap())
};
self
}

pub fn add_oval(&mut self, oval: &Rect, dir_start: Option<(PathDirection, usize)>) -> &mut Self {
pub fn add_oval<OR: AsRef<Rect>>(&mut self, oval: OR, dir_start: Option<(PathDirection, usize)>) -> &mut Self {
let dir = dir_start.map(|ds| ds.0).unwrap_or_default();
let start = dir_start.map(|ds| ds.1).unwrap_or_default();
unsafe {
self.native_mut().addOval1(oval.native(), dir.into_native(), start.try_into().unwrap())
self.native_mut().addOval1(oval.as_ref().native(), dir.into_native(), start.try_into().unwrap())
};
self
}
Expand All @@ -455,28 +455,28 @@ impl Handle<SkPath> {
self
}

pub fn add_arc(&mut self, oval: &Rect, start_angle: scalar, sweep_angle: scalar) -> &mut Self {
pub fn add_arc<OR: AsRef<Rect>>(&mut self, oval: OR, start_angle: scalar, sweep_angle: scalar) -> &mut Self {
unsafe {
self.native_mut().addArc(oval.native(), start_angle, sweep_angle)
self.native_mut().addArc(oval.as_ref().native(), start_angle, sweep_angle)
};
self
}

// decided to use the simpler variant of the two, if more radii need to be specified,
// add_rrect can be used.
pub fn add_round_rect(&mut self, rect: &Rect, (rx, ry): (scalar, scalar), dir: Option<PathDirection>) -> &mut Self {
pub fn add_round_rect<R: AsRef<Rect>>(&mut self, rect: R, (rx, ry): (scalar, scalar), dir: Option<PathDirection>) -> &mut Self {
let dir = dir.unwrap_or_default();
unsafe {
self.native_mut().addRoundRect(rect.native(), rx, ry, dir.into_native())
self.native_mut().addRoundRect(rect.as_ref().native(), rx, ry, dir.into_native())
};
self
}

pub fn add_rrect(&mut self, rrect: &RRect, dir_start: Option<(PathDirection, usize)>) -> &mut Self {
pub fn add_rrect<RR: AsRef<RRect>>(&mut self, rrect: RR, dir_start: Option<(PathDirection, usize)>) -> &mut Self {
let dir = dir_start.map(|ds| ds.0).unwrap_or_default();
let start = dir_start.map(|ds| ds.1).unwrap_or_default();
unsafe {
self.native_mut().addRRect1(rrect.native(), dir.into_native(), start.try_into().unwrap())
self.native_mut().addRRect1(rrect.as_ref().native(), dir.into_native(), start.try_into().unwrap())
};
self
}
Expand Down
20 changes: 10 additions & 10 deletions skia-safe/src/core/path_effect.rs
Expand Up @@ -116,38 +116,38 @@ impl RCHandle<SkPathEffect> {
}).unwrap()
}

pub fn filter_path_inplace(
pub fn filter_path_inplace<R: AsRef<Rect>>(
&self, dst: &mut Path, src: &Path,
stroke_rec: &mut StrokeRec, cull_rect: &Rect) -> bool {
stroke_rec: &mut StrokeRec, cull_rect: R) -> bool {
unsafe {
self.native().filterPath(
dst.native_mut(), src.native(),
stroke_rec.native_mut(),
&cull_rect.into_native())
cull_rect.as_ref().native())
}
}

// for convenience
pub fn filter_path(&self, src: &Path, stroke_rec: &StrokeRec, cull_rect: &Rect)
pub fn filter_path<R: AsRef<Rect>>(&self, src: &Path, stroke_rec: &StrokeRec, cull_rect: R)
-> Option<(Path, StrokeRec)> {
let mut dst = Path::default();
let mut stroke_rec_r = stroke_rec.clone();
self.filter_path_inplace(&mut dst, src, &mut stroke_rec_r, &cull_rect)
self.filter_path_inplace(&mut dst, src, &mut stroke_rec_r, cull_rect)
.if_true_some((dst, stroke_rec_r))
}

pub fn compute_fast_bounds(&self, src: &Rect) -> Rect {
pub fn compute_fast_bounds<R: AsRef<Rect>>(&self, src: R) -> Rect {
let mut r : Rect = Rect::default();
unsafe { self.native().computeFastBounds(r.native_mut(), &src.into_native()) };
unsafe { self.native().computeFastBounds(r.native_mut(), src.as_ref().native()) };
r
}

pub fn as_points(
pub fn as_points<CR: AsRef<Rect>>(
&self,
src: &Path,
stroke_rect: &StrokeRec,
matrix: &Matrix,
cull_rect: &Rect)
cull_rect: CR)
-> Option<PathEffectPointData> {
let mut point_data = PathEffectPointData::default();
unsafe {
Expand All @@ -156,7 +156,7 @@ impl RCHandle<SkPathEffect> {
src.native(),
stroke_rect.native(),
matrix.native(),
&cull_rect.into_native())
cull_rect.as_ref().native())
}.if_true_some(point_data)
}

Expand Down
4 changes: 2 additions & 2 deletions skia-safe/src/core/picture.rs
Expand Up @@ -53,9 +53,9 @@ impl RCHandle<SkPicture> {
}).unwrap()
}

pub fn new_placeholder(cull: &Rect) -> Picture {
pub fn new_placeholder<C: AsRef<Rect>>(cull: C) -> Picture {
Picture::from_ptr(unsafe {
C_SkPicture_MakePlaceholder(&cull.into_native())
C_SkPicture_MakePlaceholder(cull.as_ref().native())
}).unwrap()
}

Expand Down
6 changes: 3 additions & 3 deletions skia-safe/src/core/picture_recorder.rs
Expand Up @@ -34,15 +34,15 @@ impl Handle<SkPictureRecorder> {
unsafe { SkPictureRecorder::new() }.into_handle()
}

pub fn begin_recording(
pub fn begin_recording<BR: AsRef<Rect>>(
&mut self,
bounds: &Rect,
bounds: BR,
mut bbh_factory: Option<&mut BBHFactory>,
record_flags: PictureRecorderRecordFlags) -> &mut Canvas {

let canvas_ref = unsafe {
&mut *self.native_mut().beginRecording(
&bounds.into_native(),
bounds.as_ref().native(),
bbh_factory.native_ptr_or_null_mut(),
record_flags.bits())
};
Expand Down
21 changes: 14 additions & 7 deletions skia-safe/src/core/rect.rs
Expand Up @@ -30,6 +30,12 @@ fn test_irect_layout() {
IRect::test_layout();
}

impl AsRef<IRect> for IRect {
fn as_ref(&self) -> &IRect {
self
}
}

impl IRect {

pub fn new(left: i32, top: i32, right: i32, bottom: i32) -> IRect {
Expand Down Expand Up @@ -179,6 +185,7 @@ impl Contains<&IRect> for IRect {
}

impl Contains<&Rect> for IRect {
// TODO: can we support AsRef<Rect> here?
fn contains(&self, other: &Rect) -> bool {
unsafe { self.native().contains3(other.native()) }
}
Expand Down Expand Up @@ -307,19 +314,19 @@ impl Rect {
Self::new(new_p.x, new_p.y, new_p.x - self.left, new_p.y - self.top)
}

pub fn intersect(a: &Rect, b: &Rect) -> Option<Rect> {
pub fn intersect<A: AsRef<Rect>, B: AsRef<Rect>>(a: A, b: B) -> Option<Rect> {
let mut intersection = Rect::default();
unsafe { intersection.native_mut().intersect2(a.native(), b.native()) }
unsafe { intersection.native_mut().intersect2(a.as_ref().native(), b.as_ref().native()) }
.if_true_some(intersection)
}

pub fn intersects(a: &Rect, b: &Rect) -> bool {
unsafe { SkRect::Intersects(a.native(), b.native()) }
pub fn intersects<A: AsRef<Rect>, B: AsRef<Rect>>(a: &Rect, b: &Rect) -> bool {
unsafe { SkRect::Intersects(a.as_ref().native(), b.as_ref().native()) }
}

pub fn join(a: &Rect, b: &Rect) -> Rect {
let mut joined = *a;
unsafe { joined.native_mut().join1(b.native()) }
pub fn join<A: AsRef<Rect>, B: AsRef<Rect>>(a: A, b: B) -> Rect {
let mut joined = *(a.as_ref());
unsafe { joined.native_mut().join1(b.as_ref().native()) }
joined
}

Expand Down

0 comments on commit 0705887

Please sign in to comment.