///|
/// 2D drawing canvas backed by a pixmap
#derive(Debug)
pub struct Canvas {
pixmap : Pixmap
mut transform : Transform
mut clip_rect : Rect?
state_stack : Array[CanvasState]
dirty_region : DirtyRegion
/// Reused shape-fill scratch: an idle frame issues dozens of rounded fills
/// and strokes, and allocating a coverage row, a colour row, and four
/// sub-scanline span rows per shape dominated the per-fill cost. The buffers
/// grow to the widest shape and are then reused; every fill zeroes exactly
/// the slices it reads, so stale samples can never leak between shapes.
mut cover_scratch : Array[Double]
mut solid_pattern_scratch : FixedArray[Byte]
mut solid_pattern_red : Int32
mut solid_pattern_green : Int32
mut solid_pattern_blue : Int32
/// Pixels of `solid_pattern_scratch` written with the current colour. A
/// request wider than this must refill: the row blit reads `[0, width)` and
/// a stale tail would paint the previous shape's colour into the new one.
mut solid_pattern_width : Int32
sample_outer_lo : FixedArray[Double]
sample_outer_hi : FixedArray[Double]
sample_hole_lo : FixedArray[Double]
sample_hole_hi : FixedArray[Double]
}
///|
pub extend Canvas with Debug::{to_repr}
///|
#derive(Debug)
struct CanvasState {
transform : Transform
clip_rect : Rect?
}
///|
pub extend CanvasState with Debug::{to_repr}
///|
/// Create a new canvas with the given dimensions
pub fn Canvas::new(width : Int32, height : Int32) -> Canvas {
let steps = sun_shape_coverage_steps
{
pixmap: Pixmap::new(width, height),
transform: Transform::identity(),
clip_rect: None,
state_stack: [],
dirty_region: DirtyRegion::new(),
cover_scratch: [],
solid_pattern_scratch: FixedArray::make(0, b"\xff"[0]),
solid_pattern_red: -1,
solid_pattern_green: -1,
solid_pattern_blue: -1,
solid_pattern_width: 0,
sample_outer_lo: FixedArray::make(steps, 0.0),
sample_outer_hi: FixedArray::make(steps, 0.0),
sample_hole_lo: FixedArray::make(steps, 0.0),
sample_hole_hi: FixedArray::make(steps, 0.0),
}
}
///|
/// Get the underlying pixmap
pub fn Canvas::get_pixmap(self : Canvas) -> Pixmap {
self.pixmap
}
///|
/// Clear the canvas with a color
pub fn Canvas::clear(self : Canvas, color : Color) -> Unit {
self.pixmap.clear(color)
self.dirty_region.add_all(self.pixmap.get_width(), self.pixmap.get_height())
}
///|
/// Reset the canvas for a new frame while keeping its pixel buffer.
///
/// A hosted surface keeps its physical size across most frames, and allocating
/// a fresh canvas every frame costs a full-surface allocation plus a zero fill.
/// The buffer is reusable once the per-frame drawing state — transform, clip,
/// and saved-state stack — is cleared. Pixels are still cleared separately with
/// the frame's clear colour.
pub fn Canvas::reset_for_frame(self : Canvas) -> Unit {
self.transform = Transform::identity()
self.clip_rect = None
self.state_stack.clear()
self.dirty_region.clear()
}
///|
/// Get the accumulated dirty region for incremental rendering.
pub fn Canvas::get_dirty_region(self : Canvas) -> DirtyRegion {
self.dirty_region
}
///|
/// Clear accumulated dirty rectangles.
pub fn Canvas::clear_dirty_region(self : Canvas) -> Unit {
self.dirty_region.clear()
}
///|
/// Set the current transform
pub fn Canvas::set_transform(self : Canvas, transform : Transform) -> Unit {
self.transform = transform
}
///|
/// Reset the current transform to identity.
pub fn Canvas::reset_transform(self : Canvas) -> Unit {
self.transform = Transform::identity()
}
///|
/// Concatenate a transform onto the current transform.
pub fn Canvas::concat_transform(self : Canvas, transform : Transform) -> Unit {
self.transform = self.transform.multiply(transform)
}
///|
/// Translate subsequent transformed drawing operations.
pub fn Canvas::translate(self : Canvas, tx~ : Double, ty~ : Double) -> Unit {
self.concat_transform(Transform::translate(tx~, ty~))
}
///|
/// Scale subsequent transformed drawing operations.
pub fn Canvas::scale(self : Canvas, sx~ : Double, sy~ : Double) -> Unit {
self.concat_transform(Transform::scale(sx~, sy~))
}
///|
/// Rotate subsequent transformed drawing operations by an angle in radians.
pub fn Canvas::rotate(self : Canvas, angle : Double) -> Unit {
self.concat_transform(Transform::rotate(angle))
}
///|
/// Get the current transform
pub fn Canvas::get_transform(self : Canvas) -> Transform {
self.transform
}
///|
/// Save the current transform and clip state.
pub fn Canvas::save(self : Canvas) -> Unit {
self.state_stack.push({
transform: self.transform,
clip_rect: self.clip_rect,
})
}
///|
/// Restore the most recently saved transform and clip state.
pub fn Canvas::restore(self : Canvas) -> Bool {
guard self.state_stack.length() > 0 else { return false }
let state = self.state_stack.remove(self.state_stack.length() - 1)
self.transform = state.transform
self.clip_rect = state.clip_rect
true
}
///|
/// Get the number of saved canvas states.
pub fn Canvas::get_save_count(self : Canvas) -> Int32 {
self.state_stack.length()
}
///|
/// Set the current clip rectangle in device coordinates.
pub fn Canvas::set_clip_rect(self : Canvas, rect : Rect) -> Unit {
if !rect.is_empty() {
self.clip_rect = Some(rect)
} else {
self.clip_rect = Some(Rect::new(x=0.0, y=0.0, width=0.0, height=0.0))
}
}
///|
/// Intersect the current clip rectangle with a new device-space rectangle.
pub fn Canvas::intersect_clip_rect(self : Canvas, rect : Rect) -> Unit {
guard !rect.is_empty() else {
self.clip_rect = Some(Rect::new(x=0.0, y=0.0, width=0.0, height=0.0))
return
}
match self.clip_rect {
Some(current) =>
match current.intersection(rect) {
Some(intersection) => self.clip_rect = Some(intersection)
None =>
self.clip_rect = Some(Rect::new(x=0.0, y=0.0, width=0.0, height=0.0))
}
None => self.set_clip_rect(rect)
}
}
///|
/// Clear the current clip rectangle.
pub fn Canvas::clear_clip(self : Canvas) -> Unit {
self.clip_rect = None
}
///|
/// Get the current clip rectangle, if any.
pub fn Canvas::get_clip_rect(self : Canvas) -> Rect? {
self.clip_rect
}
///|
/// Draw a single pixel (for testing)
pub fn Canvas::draw_pixel(
self : Canvas,
x : Int32,
y : Int32,
color : Color,
) -> Unit {
guard is_pixel_in_clip(self, x, y) else { return }
self.pixmap.set_pixel(x, y, color)
self.mark_dirty_bounds(x, y, x + 1, y + 1)
}
///|
/// Draw placeholder text boxes at the given position.
pub fn Canvas::draw_placeholder_text(
self : Canvas,
text : String,
x : Double,
y : Double,
font_size : Double,
color : Color,
) -> Unit {
let char_width = font_size * 0.6
for i in 0..