use tiny_skia::{LineCap, StrokeDash, Transform};
use zenith_core::{AssetProvider, FontProvider};
use zenith_scene::{LineCap as IrLineCap, SceneCommand};
use super::draw;
#[derive(Clone, Copy)]
pub(in crate::tiny_skia) struct DrawCtx {
pub(in crate::tiny_skia) current_ts: Transform,
pub(in crate::tiny_skia) effective_clip: (f64, f64, f64, f64),
pub(in crate::tiny_skia) width: u32,
pub(in crate::tiny_skia) height: u32,
}
pub(in crate::tiny_skia) fn draw_command(
target: &mut tiny_skia::Pixmap,
ctx: DrawCtx,
cmd: &SceneCommand,
fonts: &dyn FontProvider,
assets: &dyn AssetProvider,
svg_fontdb: &mut Option<resvg::usvg::fontdb::Database>,
) {
match cmd {
SceneCommand::FillRect { .. } => draw::shapes::fill_rect(target, ctx, cmd),
SceneCommand::FillEllipse { .. } => draw::shapes::fill_ellipse(target, ctx, cmd),
SceneCommand::StrokeEllipse { .. } => draw::shapes::stroke_ellipse(target, ctx, cmd),
SceneCommand::StrokeLine { .. } => draw::shapes::stroke_line(target, ctx, cmd),
SceneCommand::FillPolygon { .. } => draw::shapes::fill_polygon(target, ctx, cmd),
SceneCommand::StrokePolyline { .. } => draw::shapes::stroke_polyline(target, ctx, cmd),
SceneCommand::StrokeRect { .. } => draw::shapes::stroke_rect(target, ctx, cmd),
SceneCommand::FillRoundedRect { .. } => draw::shapes::fill_rounded_rect(target, ctx, cmd),
SceneCommand::StrokeRoundedRect { .. } => {
draw::shapes::stroke_rounded_rect(target, ctx, cmd)
}
SceneCommand::DrawGlyphRun { .. } => draw::text::draw_glyph_run(target, ctx, cmd, fonts),
SceneCommand::DrawImage { .. } => {
draw::image::draw_image(target, ctx, cmd, assets, fonts, svg_fontdb)
}
SceneCommand::DrawSvgAsset { .. } => {}
SceneCommand::PushClip { .. }
| SceneCommand::PopClip
| SceneCommand::PushLayer { .. }
| SceneCommand::PopLayer
| SceneCommand::PushTransform { .. }
| SceneCommand::PopTransform
| SceneCommand::BeginShadow { .. }
| SceneCommand::EndShadow
| SceneCommand::BeginBlur { .. }
| SceneCommand::EndBlur
| SceneCommand::BeginFilter { .. }
| SceneCommand::EndFilter
| SceneCommand::BeginMask { .. }
| SceneCommand::EndMask => {}
}
}
pub(in crate::tiny_skia) fn map_line_cap(lc: Option<IrLineCap>) -> LineCap {
match lc {
Some(IrLineCap::Round) => LineCap::Round,
Some(IrLineCap::Square) => LineCap::Square,
Some(IrLineCap::Butt) | None => LineCap::Butt,
}
}
pub(in crate::tiny_skia) fn build_stroke_dash(
dash: Option<f64>,
gap: Option<f64>,
) -> Option<StrokeDash> {
let d = dash?;
if d <= 0.0 {
return None;
}
let g = gap.unwrap_or(d).max(0.0);
StrokeDash::new(vec![d as f32, g as f32], 0.0)
}