mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Share some code between 2D canvas and WebGL
This commit is contained in:
parent
05ef233097
commit
6c469b90b1
6 changed files with 58 additions and 84 deletions
|
@ -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())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue