use super::renderer::colorref_to_skia;
use crate::renderer::{render_tree::BoundingBox, GradientFillInfo, PatternFillInfo, ShapeStyle};
use skia_safe::{paint, surfaces, FilterMode, MipmapMode, Paint, Rect, SamplingOptions, TileMode};
pub(super) fn shape_fill(
bounds: &BoundingBox,
style: &ShapeStyle,
gradient: Option<&GradientFillInfo>,
antialias: bool,
) -> Option<Paint> {
let raster_bounds = Rect::from_xywh(
bounds.x as f32,
bounds.y as f32,
bounds.width as f32,
bounds.height as f32,
);
if let Some(mut paint) = gradient
.filter(|gradient| {
(2..=4096).contains(&gradient.colors.len())
&& raster_bounds.is_finite()
&& !raster_bounds.is_empty()
})
.and_then(|gradient| super::fill::gradient_paint(gradient, bounds, style.opacity))
{
paint.set_anti_alias(antialias);
return Some(paint);
}
let mut paint = Paint::default();
paint.set_anti_alias(antialias);
if let Some(shader) = style.pattern.as_ref().and_then(make_pattern_shader) {
paint.set_alpha_f(style.opacity.clamp(0.0, 1.0) as f32);
paint.set_shader(shader);
} else {
let color = gradient
.and_then(|gradient| gradient.colors.first().copied())
.or(style.pattern.map(|pattern| pattern.background_color))
.or(style.fill_color)?;
paint.set_color(colorref_to_skia(color, style.opacity as f32));
}
Some(paint)
}
const PATTERN_TILE_DOTS: i32 = 8;
fn pattern_ink(hatch: crate::model::style::HatchStyle, x: i32, y: i32) -> bool {
use crate::model::style::HatchStyle;
let horizontal = y == 0;
let vertical = x == 0;
let rising = (x + y) % PATTERN_TILE_DOTS == 0;
let falling = x == y;
match hatch {
HatchStyle::Horizontal => horizontal,
HatchStyle::Vertical => vertical,
HatchStyle::Falling => falling,
HatchStyle::Rising => rising,
HatchStyle::Cross => horizontal || vertical,
HatchStyle::DiagonalCross => rising || falling,
}
}
fn make_pattern_shader(pattern: &PatternFillInfo) -> Option<skia_safe::Shader> {
let hatch = crate::model::style::HatchStyle::from_pattern_type(pattern.pattern_type);
let mut surface = surfaces::raster_n32_premul((PATTERN_TILE_DOTS, PATTERN_TILE_DOTS))?;
let canvas = surface.canvas();
let mut background = Paint::default();
background.set_anti_alias(false);
background.set_style(paint::Style::Fill);
background.set_color(colorref_to_skia(pattern.background_color, 1.0));
let side = PATTERN_TILE_DOTS as f32;
canvas.draw_rect(Rect::from_xywh(0.0, 0.0, side, side), &background);
let mut foreground = Paint::default();
foreground.set_anti_alias(false);
foreground.set_style(paint::Style::Fill);
foreground.set_color(colorref_to_skia(pattern.pattern_color, 1.0));
if let Some(hatch) = hatch {
for y in 0..PATTERN_TILE_DOTS {
for x in 0..PATTERN_TILE_DOTS {
if pattern_ink(hatch, x, y) {
canvas.draw_rect(Rect::from_xywh(x as f32, y as f32, 1.0, 1.0), &foreground);
}
}
}
}
let scale = if pattern.tile_size.is_finite() && pattern.tile_size > 0.0 {
(pattern.tile_size / PATTERN_TILE_DOTS as f64) as f32
} else {
1.0
};
let matrix = skia_safe::Matrix::new_all(
scale,
0.0,
-scale / 2.0,
0.0,
scale,
-scale / 2.0,
0.0,
0.0,
1.0,
);
surface.image_snapshot().to_shader(
Some((TileMode::Repeat, TileMode::Repeat)),
SamplingOptions::new(FilterMode::Linear, MipmapMode::None),
Some(&matrix),
)
}