diff options
author | Anthony Ramine <n.oxyde@gmail.com> | 2018-10-08 13:49:58 +0200 |
---|---|---|
committer | Anthony Ramine <n.oxyde@gmail.com> | 2018-10-09 14:57:02 +0200 |
commit | 6c469b90b1ae34bddcb7da19eacfa6ad4467cf35 (patch) | |
tree | 1746b4cef5b9710368c7c86b360906a417e0a78f /components/pixels/lib.rs | |
parent | 05ef233097e17c3cdd0000f434d1592e8e26ff54 (diff) | |
download | servo-6c469b90b1ae34bddcb7da19eacfa6ad4467cf35.tar.gz servo-6c469b90b1ae34bddcb7da19eacfa6ad4467cf35.zip |
Share some code between 2D canvas and WebGL
Diffstat (limited to 'components/pixels/lib.rs')
-rw-r--r-- | components/pixels/lib.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/components/pixels/lib.rs b/components/pixels/lib.rs index dc6285ebaab..de29e5fa2ee 100644 --- a/components/pixels/lib.rs +++ b/components/pixels/lib.rs @@ -4,7 +4,7 @@ extern crate euclid; -use euclid::{Rect, Size2D}; +use euclid::{Point2D, Rect, Size2D}; use std::borrow::Cow; pub fn get_rect(pixels: &[u8], size: Size2D<u32>, rect: Rect<u32>) -> Cow<[u8]> { @@ -63,3 +63,21 @@ pub fn premultiply_inplace(pixels: &mut [u8]) -> bool { pub fn multiply_u8_color(a: u8, b: u8) -> u8 { return (a as u32 * b as u32 / 255) as u8; } + +pub fn clip( + mut origin: Point2D<i32>, + mut size: Size2D<u32>, + surface: Size2D<u32>, +) -> Option<Rect<u32>> { + if origin.x < 0 { + size.width = size.width.saturating_sub(-origin.x as u32); + origin.x = 0; + } + if origin.y < 0 { + size.height = size.height.saturating_sub(-origin.y as u32); + origin.y = 0; + } + Rect::new(origin.to_u32(), size) + .intersection(&Rect::from_size(surface)) + .filter(|rect| !rect.is_empty()) +} |