#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![allow(clippy::field_reassign_with_default)]
#![allow(clippy::identity_op)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::wrong_self_convention)]
pub use tiny_skia;
pub use usvg;
mod clip;
mod filter;
mod geom;
mod image;
mod mask;
mod path;
mod render;
pub struct RenderOptions {
current_color: tiny_skia::Color,
}
impl RenderOptions {
pub fn set_color(&mut self, color: tiny_skia::Color) -> &mut Self {
self.current_color = color;
self
}
}
impl Default for RenderOptions {
fn default() -> Self {
Self {
current_color: tiny_skia::Color::from_rgba8(0, 0, 0, 255),
}
}
}
pub fn render(
tree: &usvg::Tree,
transform: tiny_skia::Transform,
pixmap: &mut tiny_skia::PixmapMut,
options: &RenderOptions,
) {
let target_size = tiny_skia::IntSize::from_wh(pixmap.width(), pixmap.height()).unwrap();
let max_bbox = tiny_skia::IntRect::from_xywh(
-(target_size.width() as i32) * 2,
-(target_size.height() as i32) * 2,
target_size.width() * 5,
target_size.height() * 5,
)
.unwrap();
let ctx = render::Context { max_bbox };
render::render_nodes(tree.root(), &ctx, transform, pixmap, options);
}
pub fn render_node(
node: &usvg::Node,
mut transform: tiny_skia::Transform,
pixmap: &mut tiny_skia::PixmapMut,
options: &RenderOptions,
) -> Option<()> {
let bbox = node.abs_layer_bounding_box()?;
let target_size = tiny_skia::IntSize::from_wh(pixmap.width(), pixmap.height()).unwrap();
let max_bbox = tiny_skia::IntRect::from_xywh(
-(target_size.width() as i32) * 2,
-(target_size.height() as i32) * 2,
target_size.width() * 5,
target_size.height() * 5,
)
.unwrap();
transform = transform.pre_translate(-bbox.x(), -bbox.y());
let ctx = render::Context { max_bbox };
render::render_node(node, &ctx, transform, pixmap, options);
Some(())
}
pub(crate) trait OptionLog {
fn log_none<F: FnOnce()>(self, f: F) -> Self;
}
impl<T> OptionLog for Option<T> {
#[inline]
fn log_none<F: FnOnce()>(self, f: F) -> Self {
self.or_else(|| {
f();
None
})
}
}