#![allow(clippy::collapsible_if)]
use kurbo::{Affine, Rect, Shape, Stroke, Vec2};
use peniko::{BlendMode, Color, Fill, FontData, ImageBrushRef, StyleRef};
use std::{any::Any, sync::Arc};
pub mod filters;
pub use filters::Filter;
pub mod wasm_send_sync;
pub use wasm_send_sync::*;
pub mod types;
pub use types::*;
mod shapes;
pub use shapes::*;
mod box_shadow;
pub use box_shadow::*;
mod null_backend;
pub use null_backend::*;
pub mod recording;
pub use recording::Scene;
mod pattern;
pub use pattern::*;
mod resource_id;
pub use resource_id::ResourceId;
#[cfg(feature = "serde")]
mod svg_path_parser;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum RegisterResourceErrorKind {
UnsupportedResourceKind,
Other,
Unimplemented,
NotActive,
}
#[derive(Debug, Clone)]
pub struct RegisterResourceError {
pub kind: RegisterResourceErrorKind,
pub message: Option<String>,
}
impl From<RegisterResourceErrorKind> for RegisterResourceError {
fn from(kind: RegisterResourceErrorKind) -> Self {
Self {
kind,
message: None,
}
}
}
pub trait RenderContext {
fn try_register_custom_resource(
&mut self,
resource: Box<dyn Any>,
) -> Result<ResourceId, RegisterResourceError> {
let _ = resource;
Err(RegisterResourceErrorKind::Unimplemented.into())
}
fn unregister_resource(&mut self, resource_id: ResourceId) {
let _ = resource_id;
}
fn renderer_specific_context(&self) -> Option<Box<dyn Any>> {
None
}
}
pub trait WindowRenderer: RenderContext {
type ScenePainter<'a>: PaintScene
where
Self: 'a;
fn resume<F: FnOnce() + 'static>(
&mut self,
window: Arc<dyn WindowHandle>,
width: u32,
height: u32,
on_ready: F,
);
fn complete_resume(&mut self) -> bool;
fn suspend(&mut self);
fn is_active(&self) -> bool;
fn is_pending(&self) -> bool {
false
}
fn set_size(&mut self, width: u32, height: u32);
fn render<F: FnOnce(&mut Self::ScenePainter<'_>)>(&mut self, draw_fn: F);
}
pub trait ImageRenderer: RenderContext {
type ScenePainter<'a>: PaintScene
where
Self: 'a;
fn new(width: u32, height: u32) -> Self;
fn resize(&mut self, width: u32, height: u32);
fn reset(&mut self);
fn render_to_vec<F: FnOnce(&mut Self::ScenePainter<'_>)>(
&mut self,
draw_fn: F,
vec: &mut Vec<u8>,
);
fn render<F: FnOnce(&mut Self::ScenePainter<'_>)>(&mut self, draw_fn: F, buffer: &mut [u8]);
}
pub fn render_to_buffer<R: ImageRenderer, F: FnOnce(&mut R::ScenePainter<'_>)>(
draw_fn: F,
width: u32,
height: u32,
) -> Vec<u8> {
let mut buf = Vec::with_capacity((width * height * 4) as usize);
let mut renderer = R::new(width, height);
renderer.render_to_vec(draw_fn, &mut buf);
buf
}
pub trait PaintScene: RenderContext {
fn reset(&mut self);
#[allow(clippy::too_many_arguments)]
fn push_layer(
&mut self,
fill: Fill,
blend: impl Into<BlendMode>,
alpha: f32,
transform: Affine,
clip: &impl Shape,
filter: Option<Arc<Filter>>,
backdrop_filter: Option<Arc<Filter>>,
);
fn push_clip_layer(&mut self, fill: Fill, transform: Affine, clip: &impl Shape);
fn push_mask_layer(&mut self, fill: Fill, mode: MaskMode, transform: Affine, clip: &impl Shape);
fn pop_layer(&mut self);
fn stroke<'a>(
&mut self,
style: &Stroke,
transform: Affine,
brush: impl Into<PaintRef<'a>>,
brush_transform: Option<Affine>,
shape: &impl Shape,
anti_aliasing: AntiAliasing,
);
fn fill<'a>(
&mut self,
style: Fill,
transform: Affine,
brush: impl Into<PaintRef<'a>>,
brush_transform: Option<Affine>,
shape: &impl Shape,
anti_aliasing: AntiAliasing,
);
#[allow(clippy::too_many_arguments)]
fn draw_glyphs<'a, 's: 'a>(
&'s mut self,
font: &'a FontData,
font_size: f32,
hint: bool,
normalized_coords: &'a [NormalizedCoord],
embolden: kurbo::Vec2,
style: impl Into<StyleRef<'a>>,
brush: impl Into<PaintRef<'a>>,
brush_alpha: f32,
transform: Affine,
glyph_transform: Option<Affine>,
glyphs: impl Iterator<Item = Glyph> + Clone,
anti_aliasing: AntiAliasing,
);
#[allow(clippy::too_many_arguments)]
fn draw_box_shadow(
&mut self,
transform: Affine,
box_shape: &NonUniformRoundedRect,
offset: Vec2,
spread: f64,
std_dev: f64,
color: Color,
kind: BoxShadowKind,
);
fn append_scene(&mut self, scene: Scene, scene_transform: Affine) {
scene.replay(self, scene_transform);
}
fn draw_image(&mut self, image: ImageBrushRef, transform: Affine) {
self.fill(
Fill::NonZero,
transform,
image,
None,
&Rect::new(
0.0,
0.0,
image.image.width as f64,
image.image.height as f64,
),
AntiAliasing::Enabled,
);
}
}