use crate::*;
use tiny_skia_path::{PathStroker, Scalar, ScreenIntRect, SCALAR_MAX};
use crate::clip::SubClipMaskRef;
use crate::pipeline::RasterPipelineBlitter;
use crate::pixmap::SubPixmapMut;
use crate::scan;
#[cfg(all(not(feature = "std"), feature = "no-std-float"))]
use tiny_skia_path::NoStdFloat;
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum FillRule {
Winding,
EvenOdd,
}
impl Default for FillRule {
fn default() -> Self {
FillRule::Winding
}
}
#[derive(Clone, PartialEq, Debug)]
pub struct Paint<'a> {
pub shader: Shader<'a>,
pub blend_mode: BlendMode,
pub anti_alias: bool,
pub force_hq_pipeline: bool,
}
impl Default for Paint<'_> {
fn default() -> Self {
Paint {
shader: Shader::SolidColor(Color::BLACK),
blend_mode: BlendMode::default(),
anti_alias: false,
force_hq_pipeline: false,
}
}
}
impl<'a> Paint<'a> {
pub fn set_color(&mut self, color: Color) {
self.shader = Shader::SolidColor(color);
}
pub fn set_color_rgba8(&mut self, r: u8, g: u8, b: u8, a: u8) {
self.set_color(Color::from_rgba8(r, g, b, a))
}
pub fn is_solid_color(&self) -> bool {
matches!(self.shader, Shader::SolidColor(_))
}
}
impl Pixmap {
pub fn fill_rect(
&mut self,
rect: Rect,
paint: &Paint,
transform: Transform,
clip_mask: Option<&ClipMask>,
) -> Option<()> {
self.as_mut().fill_rect(rect, paint, transform, clip_mask)
}
pub fn fill_path(
&mut self,
path: &Path,
paint: &Paint,
fill_rule: FillRule,
transform: Transform,
clip_mask: Option<&ClipMask>,
) -> Option<()> {
self.as_mut()
.fill_path(path, paint, fill_rule, transform, clip_mask)
}
pub fn stroke_path(
&mut self,
path: &Path,
paint: &Paint,
stroke: &Stroke,
transform: Transform,
clip_mask: Option<&ClipMask>,
) -> Option<()> {
self.as_mut()
.stroke_path(path, paint, stroke, transform, clip_mask)
}
pub fn draw_pixmap(
&mut self,
x: i32,
y: i32,
pixmap: PixmapRef,
paint: &PixmapPaint,
transform: Transform,
clip_mask: Option<&ClipMask>,
) -> Option<()> {
self.as_mut()
.draw_pixmap(x, y, pixmap, paint, transform, clip_mask)
}
}
impl PixmapMut<'_> {
pub fn fill_rect(
&mut self,
rect: Rect,
paint: &Paint,
transform: Transform,
clip_mask: Option<&ClipMask>,
) -> Option<()> {
if transform.is_identity() && !DrawTiler::required(self.width(), self.height()) {
let clip = self.size().to_screen_int_rect(0, 0);
let clip_mask = clip_mask.map(|mask| mask.as_submask());
let mut subpix = self.as_subpixmap();
let mut blitter = RasterPipelineBlitter::new(paint, clip_mask, &mut subpix)?;
if paint.anti_alias {
scan::fill_rect_aa(&rect, &clip, &mut blitter)
} else {
scan::fill_rect(&rect, &clip, &mut blitter)
}
} else {
let path = PathBuilder::from_rect(rect);
self.fill_path(&path, paint, FillRule::Winding, transform, clip_mask)
}
}
pub fn fill_path(
&mut self,
path: &Path,
paint: &Paint,
fill_rule: FillRule,
transform: Transform,
clip_mask: Option<&ClipMask>,
) -> Option<()> {
if transform.is_identity() {
let path_bounds = path.bounds();
if path_bounds.width().is_nearly_zero() || path_bounds.height().is_nearly_zero() {
return None;
}
if is_too_big_for_math(path) {
return None;
}
if let Some(tiler) = DrawTiler::new(self.width(), self.height()) {
let mut path = path.clone(); let mut paint = paint.clone();
for tile in tiler {
let ts = Transform::from_translate(-(tile.x() as f32), -(tile.y() as f32));
path = path.transform(ts)?;
paint.shader.transform(ts);
let clip_rect = tile.size().to_screen_int_rect(0, 0);
let mut subpix = self.subpixmap(tile.to_int_rect())?;
let submask = clip_mask.and_then(|mask| mask.submask(tile.to_int_rect()));
let mut blitter = RasterPipelineBlitter::new(&paint, submask, &mut subpix)?;
if paint.anti_alias {
scan::path_aa::fill_path(&path, fill_rule, &clip_rect, &mut blitter);
} else {
scan::path::fill_path(&path, fill_rule, &clip_rect, &mut blitter);
}
let ts = Transform::from_translate(tile.x() as f32, tile.y() as f32);
path = path.transform(ts)?;
paint.shader.transform(ts);
}
Some(())
} else {
let clip_rect = self.size().to_screen_int_rect(0, 0);
let submask = clip_mask.map(|mask| mask.as_submask());
let mut subpix = self.as_subpixmap();
let mut blitter = RasterPipelineBlitter::new(paint, submask, &mut subpix)?;
if paint.anti_alias {
scan::path_aa::fill_path(path, fill_rule, &clip_rect, &mut blitter)
} else {
scan::path::fill_path(path, fill_rule, &clip_rect, &mut blitter)
}
}
} else {
let path = path.clone().transform(transform)?;
let mut paint = paint.clone();
paint.shader.transform(transform);
self.fill_path(&path, &paint, fill_rule, Transform::identity(), clip_mask)
}
}
pub fn stroke_path(
&mut self,
path: &Path,
paint: &Paint,
stroke: &Stroke,
transform: Transform,
clip_mask: Option<&ClipMask>,
) -> Option<()> {
if stroke.width < 0.0 {
return None;
}
let res_scale = PathStroker::compute_resolution_scale(&transform);
let dash_path;
let path = if let Some(ref dash) = stroke.dash {
dash_path = path.dash(dash, res_scale)?;
&dash_path
} else {
path
};
if let Some(coverage) = treat_as_hairline(paint, stroke, transform) {
let mut paint = paint.clone();
if coverage == 1.0 {
} else if paint.blend_mode.should_pre_scale_coverage() {
let scale = (coverage * 256.0) as i32;
let new_alpha = (255 * scale) >> 8;
paint.shader.apply_opacity(new_alpha as f32 / 255.0);
}
if let Some(tiler) = DrawTiler::new(self.width(), self.height()) {
let mut path = path.clone(); let mut paint = paint.clone();
if !transform.is_identity() {
paint.shader.transform(transform);
path = path.transform(transform)?;
}
for tile in tiler {
let ts = Transform::from_translate(-(tile.x() as f32), -(tile.y() as f32));
path = path.transform(ts)?;
paint.shader.transform(ts);
let mut subpix = self.subpixmap(tile.to_int_rect())?;
let submask = clip_mask.and_then(|mask| mask.submask(tile.to_int_rect()));
Self::stroke_hairline(&path, &paint, stroke.line_cap, submask, &mut subpix);
let ts = Transform::from_translate(tile.x() as f32, tile.y() as f32);
path = path.transform(ts)?;
paint.shader.transform(ts);
}
Some(())
} else {
let subpix = &mut self.as_subpixmap();
let submask = clip_mask.map(|mask| mask.as_submask());
if !transform.is_identity() {
paint.shader.transform(transform);
let path = path.clone().transform(transform)?; Self::stroke_hairline(&path, &paint, stroke.line_cap, submask, subpix)
} else {
Self::stroke_hairline(path, &paint, stroke.line_cap, submask, subpix)
}
}
} else {
let path = path.stroke(stroke, res_scale)?;
self.fill_path(&path, paint, FillRule::Winding, transform, clip_mask)
}
}
fn stroke_hairline(
path: &Path,
paint: &Paint,
line_cap: LineCap,
clip_mask: Option<SubClipMaskRef>,
pixmap: &mut SubPixmapMut,
) -> Option<()> {
let clip = pixmap.size.to_screen_int_rect(0, 0);
let mut blitter = RasterPipelineBlitter::new(paint, clip_mask, pixmap)?;
if paint.anti_alias {
scan::hairline_aa::stroke_path(path, line_cap, &clip, &mut blitter)
} else {
scan::hairline::stroke_path(path, line_cap, &clip, &mut blitter)
}
}
pub fn draw_pixmap(
&mut self,
x: i32,
y: i32,
pixmap: PixmapRef,
paint: &PixmapPaint,
transform: Transform,
clip_mask: Option<&ClipMask>,
) -> Option<()> {
let rect = pixmap.size().to_int_rect(x, y).to_rect();
let patt_transform = Transform::from_translate(x as f32, y as f32);
let paint = Paint {
shader: Pattern::new(
pixmap,
SpreadMode::Pad, paint.quality,
paint.opacity,
patt_transform,
),
blend_mode: paint.blend_mode,
anti_alias: false, force_hq_pipeline: false, };
self.fill_rect(rect, &paint, transform, clip_mask)
}
}
fn treat_as_hairline(paint: &Paint, stroke: &Stroke, mut ts: Transform) -> Option<f32> {
fn fast_len(p: Point) -> f32 {
let mut x = p.x.abs();
let mut y = p.y.abs();
if x < y {
core::mem::swap(&mut x, &mut y);
}
x + y.half()
}
debug_assert!(stroke.width >= 0.0);
if stroke.width == 0.0 {
return Some(1.0);
}
if !paint.anti_alias {
return None;
}
ts.tx = 0.0;
ts.ty = 0.0;
let mut points = [
Point::from_xy(stroke.width, 0.0),
Point::from_xy(0.0, stroke.width),
];
ts.map_points(&mut points);
let len0 = fast_len(points[0]);
let len1 = fast_len(points[1]);
if len0 <= 1.0 && len1 <= 1.0 {
return Some(len0.ave(len1));
}
None
}
fn is_too_big_for_math(path: &Path) -> bool {
const SCALE_DOWN_TO_ALLOW_FOR_SMALL_MULTIPLIES: f32 = 0.25;
const MAX: f32 = SCALAR_MAX * SCALE_DOWN_TO_ALLOW_FOR_SMALL_MULTIPLIES;
let b = path.bounds();
!(b.left() >= -MAX && b.top() >= -MAX && b.right() <= MAX && b.bottom() <= MAX)
}
pub(crate) struct DrawTiler {
image_width: u32,
image_height: u32,
x_offset: u32,
y_offset: u32,
finished: bool,
}
impl DrawTiler {
const MAX_DIMENSIONS: u32 = 8192 - 1;
fn required(image_width: u32, image_height: u32) -> bool {
image_width > Self::MAX_DIMENSIONS || image_height > Self::MAX_DIMENSIONS
}
pub(crate) fn new(image_width: u32, image_height: u32) -> Option<Self> {
if Self::required(image_width, image_height) {
Some(DrawTiler {
image_width,
image_height,
x_offset: 0,
y_offset: 0,
finished: false,
})
} else {
None
}
}
}
impl Iterator for DrawTiler {
type Item = ScreenIntRect;
fn next(&mut self) -> Option<Self::Item> {
if self.finished {
return None;
}
if self.x_offset < self.image_width && self.y_offset < self.image_height {
let h = if self.y_offset < self.image_height {
(self.image_height - self.y_offset).min(Self::MAX_DIMENSIONS)
} else {
self.image_height
};
let r = ScreenIntRect::from_xywh(
self.x_offset,
self.y_offset,
(self.image_width - self.x_offset).min(Self::MAX_DIMENSIONS),
h,
);
self.x_offset += Self::MAX_DIMENSIONS;
if self.x_offset >= self.image_width {
self.x_offset = 0;
self.y_offset += Self::MAX_DIMENSIONS;
}
return r;
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
const MAX_DIM: u32 = DrawTiler::MAX_DIMENSIONS;
#[test]
fn skip() {
assert!(DrawTiler::new(100, 500).is_none());
}
#[test]
fn horizontal() {
let mut iter = DrawTiler::new(10000, 500).unwrap();
assert_eq!(iter.next(), ScreenIntRect::from_xywh(0, 0, MAX_DIM, 500));
assert_eq!(
iter.next(),
ScreenIntRect::from_xywh(MAX_DIM, 0, 10000 - MAX_DIM, 500)
);
assert_eq!(iter.next(), None);
}
#[test]
fn vertical() {
let mut iter = DrawTiler::new(500, 10000).unwrap();
assert_eq!(iter.next(), ScreenIntRect::from_xywh(0, 0, 500, MAX_DIM));
assert_eq!(
iter.next(),
ScreenIntRect::from_xywh(0, MAX_DIM, 500, 10000 - MAX_DIM)
);
assert_eq!(iter.next(), None);
}
#[test]
fn rect() {
let mut iter = DrawTiler::new(10000, 10000).unwrap();
assert_eq!(
iter.next(),
ScreenIntRect::from_xywh(0, 0, MAX_DIM, MAX_DIM)
);
assert_eq!(
iter.next(),
ScreenIntRect::from_xywh(MAX_DIM, 0, 10000 - MAX_DIM, MAX_DIM)
);
assert_eq!(
iter.next(),
ScreenIntRect::from_xywh(0, MAX_DIM, MAX_DIM, 10000 - MAX_DIM)
);
assert_eq!(
iter.next(),
ScreenIntRect::from_xywh(MAX_DIM, MAX_DIM, 10000 - MAX_DIM, 10000 - MAX_DIM)
);
assert_eq!(iter.next(), None);
}
}