use std::f32;
use app_units::{Au, MAX_AU, MIN_AU};
use euclid::default::{Point2D as UntypedPoint2D, Rect as UntypedRect, Size2D as UntypedSize2D};
use euclid::{Box2D, Length, Point2D, SideOffsets2D, Size2D, Vector2D};
use malloc_size_of_derive::MallocSizeOf;
use webrender_api::units::{FramebufferPixel, LayoutPoint, LayoutRect, LayoutSize};
pub type FramebufferUintLength = Length<u32, FramebufferPixel>;
#[derive(Clone, Copy, Debug, MallocSizeOf)]
pub enum DeviceIndependentPixel {}
pub type DeviceIndependentIntRect = Box2D<i32, DeviceIndependentPixel>;
pub type DeviceIndependentIntPoint = Point2D<i32, DeviceIndependentPixel>;
pub type DeviceIndependentIntSize = Size2D<i32, DeviceIndependentPixel>;
pub type DeviceIndependentIntLength = Length<i32, DeviceIndependentPixel>;
pub type DeviceIndependentIntSideOffsets = SideOffsets2D<i32, DeviceIndependentPixel>;
pub type DeviceIndependentIntVector2D = Vector2D<i32, DeviceIndependentPixel>;
pub type DeviceIndependentRect = Box2D<f32, DeviceIndependentPixel>;
pub type DeviceIndependentBox2D = Box2D<f32, DeviceIndependentPixel>;
pub type DeviceIndependentPoint = Point2D<f32, DeviceIndependentPixel>;
pub type DeviceIndependentVector2D = Vector2D<f32, DeviceIndependentPixel>;
pub type DeviceIndependentSize = Size2D<f32, DeviceIndependentPixel>;
pub trait MaxRect {
fn max_rect() -> Self;
}
impl MaxRect for UntypedRect<Au> {
#[inline]
fn max_rect() -> Self {
Self::new(
UntypedPoint2D::new(MIN_AU / 2, MIN_AU / 2),
UntypedSize2D::new(MAX_AU, MAX_AU),
)
}
}
impl MaxRect for LayoutRect {
#[inline]
fn max_rect() -> Self {
Self::from_origin_and_size(
LayoutPoint::new(f32::MIN / 2.0, f32::MIN / 2.0),
LayoutSize::new(f32::MAX, f32::MAX),
)
}
}
pub fn f32_rect_to_au_rect(rect: UntypedRect<f32>) -> UntypedRect<Au> {
UntypedRect::new(
Point2D::new(
Au::from_f32_px(rect.origin.x),
Au::from_f32_px(rect.origin.y),
),
Size2D::new(
Au::from_f32_px(rect.size.width),
Au::from_f32_px(rect.size.height),
),
)
}
pub fn au_rect_to_f32_rect(rect: UntypedRect<Au>) -> UntypedRect<f32> {
UntypedRect::new(
Point2D::new(rect.origin.x.to_f32_px(), rect.origin.y.to_f32_px()),
Size2D::new(rect.size.width.to_f32_px(), rect.size.height.to_f32_px()),
)
}