Split Image handling from handwriting/mod.rs
This commit is contained in:
81
src/handwriting/canvas_rasterizer.rs
Normal file
81
src/handwriting/canvas_rasterizer.rs
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use egui::{
|
||||||
|
Color32, ColorImage, CornerRadius, Painter, Pos2, Rect, Stroke, StrokeKind, TextureHandle,
|
||||||
|
emath::TSTransform,
|
||||||
|
epaint::{Brush, RectShape, Vertex},
|
||||||
|
load::SizedTexture,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::rasterizer::rasterize_onto;
|
||||||
|
|
||||||
|
use super::StrokeBlendMode;
|
||||||
|
|
||||||
|
/// Rasterize onto a resizeable canvas.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct CanvasRasterizer {
|
||||||
|
image: ColorImage,
|
||||||
|
texture: Option<TextureHandle>,
|
||||||
|
texture_is_dirty: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CanvasRasterizer {
|
||||||
|
//pub fn clear(&mut self) {
|
||||||
|
// self.image = ColorImage::new(self.image.size, Color32::TRANSPARENT);
|
||||||
|
// self.texture_is_dirty = true;
|
||||||
|
//}
|
||||||
|
|
||||||
|
//pub fn set_size(&mut self, width: usize, height: usize) {
|
||||||
|
// if self.image.size != [width, height] {
|
||||||
|
// self.image = ColorImage::new([width, height], Color32::TRANSPARENT);
|
||||||
|
// self.texture_is_dirty = true;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
pub fn clear_and_set_size(&mut self, width: usize, height: usize) {
|
||||||
|
self.image = ColorImage::new([width, height], Color32::TRANSPARENT);
|
||||||
|
self.texture_is_dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rasterize<'a>(
|
||||||
|
&mut self,
|
||||||
|
point_to_pixel: TSTransform,
|
||||||
|
triangles: impl Iterator<Item = [&'a Vertex; 3]>,
|
||||||
|
) {
|
||||||
|
rasterize_onto::<StrokeBlendMode>(&mut self.image, point_to_pixel, triangles);
|
||||||
|
self.texture_is_dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show(&mut self, ctx: &egui::Context, painter: &Painter, at: Rect) {
|
||||||
|
if self.texture_is_dirty {
|
||||||
|
self.texture_is_dirty = false;
|
||||||
|
let new_image = || {
|
||||||
|
let image = ColorImage::new(self.image.size, Color32::TRANSPARENT);
|
||||||
|
ctx.load_texture("handwriting", image, Default::default())
|
||||||
|
};
|
||||||
|
let texture = self.texture.get_or_insert_with(new_image);
|
||||||
|
texture.set(self.image.clone(), Default::default());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(texture) = &mut self.texture {
|
||||||
|
let texture = SizedTexture::new(texture.id(), texture.size_vec2());
|
||||||
|
let shape = RectShape {
|
||||||
|
rect: at,
|
||||||
|
corner_radius: CornerRadius::ZERO,
|
||||||
|
fill: Color32::WHITE,
|
||||||
|
stroke: Stroke::NONE,
|
||||||
|
stroke_kind: StrokeKind::Inside,
|
||||||
|
round_to_pixels: None,
|
||||||
|
blur_width: 0.0,
|
||||||
|
brush: Some(Arc::new(Brush {
|
||||||
|
fill_texture_id: texture.id,
|
||||||
|
uv: Rect {
|
||||||
|
min: Pos2::ZERO,
|
||||||
|
max: Pos2::new(1.0, 1.0),
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
painter.add(shape);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,6 @@
|
|||||||
|
mod canvas_rasterizer;
|
||||||
|
mod disk_format;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{self, Display},
|
fmt::{self, Display},
|
||||||
iter, mem,
|
iter, mem,
|
||||||
@ -6,27 +9,22 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use base64::{Engine, prelude::BASE64_STANDARD};
|
use base64::{Engine, prelude::BASE64_STANDARD};
|
||||||
|
use canvas_rasterizer::CanvasRasterizer;
|
||||||
use disk_format::{DiskFormat, RawStroke, RawStrokeHeader, f16_le};
|
use disk_format::{DiskFormat, RawStroke, RawStrokeHeader, f16_le};
|
||||||
use egui::{
|
use egui::{
|
||||||
Color32, ColorImage, CornerRadius, Event, Frame, Id, Mesh, PointerButton, Pos2, Rect, Sense,
|
Color32, Event, Frame, Id, Mesh, PointerButton, Pos2, Rect, Sense, Shape, Stroke, Theme, Ui,
|
||||||
Shape, Stroke, TextureHandle, Theme, Ui, Vec2,
|
Vec2,
|
||||||
emath::{self, TSTransform},
|
emath::{self, TSTransform},
|
||||||
epaint::{Brush, RectShape, TessellationOptions, Tessellator, Vertex},
|
epaint::{TessellationOptions, Tessellator, Vertex},
|
||||||
load::SizedTexture,
|
|
||||||
};
|
};
|
||||||
use eyre::{Context, bail};
|
use eyre::{Context, bail};
|
||||||
use eyre::{OptionExt, eyre};
|
use eyre::{OptionExt, eyre};
|
||||||
use half::f16;
|
use half::f16;
|
||||||
use zerocopy::{FromBytes, IntoBytes};
|
use zerocopy::{FromBytes, IntoBytes};
|
||||||
|
|
||||||
use crate::{
|
use crate::{custom_code_block::try_from_custom_code_block, rasterizer};
|
||||||
custom_code_block::try_from_custom_code_block,
|
|
||||||
rasterizer::{self, rasterize, rasterize_onto},
|
|
||||||
};
|
|
||||||
use crate::{custom_code_block::write_custom_code_block, util::random_id};
|
use crate::{custom_code_block::write_custom_code_block, util::random_id};
|
||||||
|
|
||||||
mod disk_format;
|
|
||||||
|
|
||||||
const HANDWRITING_MIN_HEIGHT: f32 = 100.0;
|
const HANDWRITING_MIN_HEIGHT: f32 = 100.0;
|
||||||
const HANDWRITING_BOTTOM_PADDING: f32 = 80.0;
|
const HANDWRITING_BOTTOM_PADDING: f32 = 80.0;
|
||||||
const HANDWRITING_MARGIN: f32 = 0.05;
|
const HANDWRITING_MARGIN: f32 = 0.05;
|
||||||
@ -76,6 +74,8 @@ pub struct Handwriting {
|
|||||||
struct Ephemeral {
|
struct Ephemeral {
|
||||||
id: Id,
|
id: Id,
|
||||||
|
|
||||||
|
canvas_rasterizer: CanvasRasterizer,
|
||||||
|
|
||||||
/// The stroke that is currently being drawed.
|
/// The stroke that is currently being drawed.
|
||||||
current_stroke: Vec<Pos2>,
|
current_stroke: Vec<Pos2>,
|
||||||
|
|
||||||
@ -87,10 +87,6 @@ struct Ephemeral {
|
|||||||
/// Tessellated mesh of all strokes
|
/// Tessellated mesh of all strokes
|
||||||
mesh: Arc<Mesh>,
|
mesh: Arc<Mesh>,
|
||||||
|
|
||||||
texture: Option<TextureHandle>,
|
|
||||||
|
|
||||||
image: ColorImage,
|
|
||||||
|
|
||||||
refresh_texture: bool,
|
refresh_texture: bool,
|
||||||
|
|
||||||
/// Context of the last mesh render.
|
/// Context of the last mesh render.
|
||||||
@ -115,31 +111,6 @@ struct MeshContext {
|
|||||||
pub stroke: Stroke,
|
pub stroke: Stroke,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get [Painting::texture], initializing it if necessary.
|
|
||||||
macro_rules! texture {
|
|
||||||
($self_:expr, $ui:expr, $mesh_context:expr) => {{
|
|
||||||
let ui: &Ui = $ui;
|
|
||||||
let mesh_context: &MeshContext = $mesh_context;
|
|
||||||
let image_size = mesh_context.pixel_size();
|
|
||||||
|
|
||||||
let new_image = || {
|
|
||||||
let image = ColorImage::new(image_size, Color32::TRANSPARENT);
|
|
||||||
ui.ctx()
|
|
||||||
.load_texture("handwriting", image, Default::default())
|
|
||||||
};
|
|
||||||
|
|
||||||
let texture = $self_.texture.get_or_insert_with(new_image);
|
|
||||||
|
|
||||||
if texture.size() != image_size {
|
|
||||||
$self_.refresh_texture = true;
|
|
||||||
// TODO: don't redraw the entire mesh, just blit the old texture onto the new one
|
|
||||||
*texture = new_image()
|
|
||||||
};
|
|
||||||
|
|
||||||
texture
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MeshContext {
|
impl MeshContext {
|
||||||
/// Calculate canvas size in pixels
|
/// Calculate canvas size in pixels
|
||||||
pub fn pixel_size(&self) -> [usize; 2] {
|
pub fn pixel_size(&self) -> [usize; 2] {
|
||||||
@ -163,11 +134,10 @@ impl Default for Ephemeral {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: random_id(),
|
id: random_id(),
|
||||||
|
canvas_rasterizer: Default::default(),
|
||||||
current_stroke: Default::default(),
|
current_stroke: Default::default(),
|
||||||
tessellator: None,
|
tessellator: None,
|
||||||
mesh: Default::default(),
|
mesh: Default::default(),
|
||||||
texture: None,
|
|
||||||
image: ColorImage::new([0, 0], Color32::WHITE),
|
|
||||||
refresh_texture: true,
|
refresh_texture: true,
|
||||||
last_mesh_ctx: None,
|
last_mesh_ctx: None,
|
||||||
unblitted_lines: Default::default(),
|
unblitted_lines: Default::default(),
|
||||||
@ -400,7 +370,7 @@ impl Handwriting {
|
|||||||
painter.add(shape);
|
painter.add(shape);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get the dimensions of the image
|
// Get the position and dimensions of the image
|
||||||
let mesh_rect = response
|
let mesh_rect = response
|
||||||
.rect
|
.rect
|
||||||
.with_max_y(response.rect.min.y + self.desired_height);
|
.with_max_y(response.rect.min.y + self.desired_height);
|
||||||
@ -420,38 +390,19 @@ impl Handwriting {
|
|||||||
|
|
||||||
if self.e.refresh_texture {
|
if self.e.refresh_texture {
|
||||||
// ...if we do, rasterize the entire texture from scratch
|
// ...if we do, rasterize the entire texture from scratch
|
||||||
self.refresh_texture(style, new_context, ui);
|
self.refresh_texture(style, new_context);
|
||||||
self.e.unblitted_lines.clear();
|
self.e.unblitted_lines.clear();
|
||||||
} else if !self.e.unblitted_lines.is_empty() {
|
} else if !self.e.unblitted_lines.is_empty() {
|
||||||
// ...if we don't, we can get away with only rasterizing the *new* lines onto the
|
// ...if we don't, we can get away with only rasterizing the *new* lines onto the
|
||||||
// existing texture.
|
// existing texture.
|
||||||
for [from, to] in std::mem::take(&mut self.e.unblitted_lines) {
|
for [from, to] in std::mem::take(&mut self.e.unblitted_lines) {
|
||||||
self.draw_line_to_texture(from, to, &new_context, ui);
|
self.draw_line_to_texture(from, to, &new_context);
|
||||||
}
|
}
|
||||||
self.e.unblitted_lines.clear();
|
self.e.unblitted_lines.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the texture
|
// Draw the texture
|
||||||
if let Some(texture) = &self.e.texture {
|
self.e.canvas_rasterizer.show(ui.ctx(), &painter, mesh_rect);
|
||||||
let texture = SizedTexture::new(texture.id(), texture.size_vec2());
|
|
||||||
let shape = RectShape {
|
|
||||||
rect: mesh_rect,
|
|
||||||
corner_radius: CornerRadius::ZERO,
|
|
||||||
fill: Color32::WHITE,
|
|
||||||
stroke: Stroke::NONE,
|
|
||||||
stroke_kind: egui::StrokeKind::Inside,
|
|
||||||
round_to_pixels: None,
|
|
||||||
blur_width: 0.0,
|
|
||||||
brush: Some(Arc::new(Brush {
|
|
||||||
fill_texture_id: texture.id,
|
|
||||||
uv: Rect {
|
|
||||||
min: Pos2::ZERO,
|
|
||||||
max: Pos2::new(1.0, 1.0),
|
|
||||||
},
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
painter.add(shape);
|
|
||||||
}
|
|
||||||
|
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
@ -463,12 +414,7 @@ impl Handwriting {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Tessellate and rasterize the strokes into a new texture.
|
/// Tessellate and rasterize the strokes into a new texture.
|
||||||
fn refresh_texture(
|
fn refresh_texture(&mut self, style: &HandwritingStyle, mesh_context: MeshContext) {
|
||||||
&mut self,
|
|
||||||
style: &HandwritingStyle,
|
|
||||||
mesh_context: MeshContext,
|
|
||||||
ui: &mut Ui,
|
|
||||||
) {
|
|
||||||
let Ephemeral {
|
let Ephemeral {
|
||||||
current_stroke,
|
current_stroke,
|
||||||
tessellator,
|
tessellator,
|
||||||
@ -511,13 +457,14 @@ impl Handwriting {
|
|||||||
// debug_assert!(vertex.pos.y.is_finite(), "{} must be finite", vertex.pos.y);
|
// debug_assert!(vertex.pos.y.is_finite(), "{} must be finite", vertex.pos.y);
|
||||||
//}
|
//}
|
||||||
|
|
||||||
let texture = texture!(self.e, ui, &mesh_context);
|
|
||||||
let triangles = mesh_triangles(&self.e.mesh);
|
|
||||||
|
|
||||||
let [px_x, px_y] = mesh_context.pixel_size();
|
let [px_x, px_y] = mesh_context.pixel_size();
|
||||||
let point_to_pixel = TSTransform::from_scaling(mesh_context.pixels_per_point);
|
let point_to_pixel = TSTransform::from_scaling(mesh_context.pixels_per_point);
|
||||||
self.e.image = rasterize::<StrokeBlendMode>(px_x, px_y, point_to_pixel, triangles);
|
let triangles = mesh_triangles(&self.e.mesh);
|
||||||
texture.set(self.e.image.clone(), Default::default());
|
|
||||||
|
self.e.canvas_rasterizer.clear_and_set_size(px_x, px_y);
|
||||||
|
self.e
|
||||||
|
.canvas_rasterizer
|
||||||
|
.rasterize(point_to_pixel, triangles);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
{
|
{
|
||||||
@ -563,13 +510,7 @@ impl Handwriting {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Draw a single line onto the existing texture.
|
/// Draw a single line onto the existing texture.
|
||||||
fn draw_line_to_texture(
|
fn draw_line_to_texture(&mut self, from: Pos2, to: Pos2, mesh_context: &MeshContext) {
|
||||||
&mut self,
|
|
||||||
from: Pos2,
|
|
||||||
to: Pos2,
|
|
||||||
mesh_context: &MeshContext,
|
|
||||||
ui: &mut Ui,
|
|
||||||
) {
|
|
||||||
// INVARIANT: if this function was called, then pixels_per_point is the same as last frame,
|
// INVARIANT: if this function was called, then pixels_per_point is the same as last frame,
|
||||||
// so there's no need to create a new tessellator.
|
// so there's no need to create a new tessellator.
|
||||||
let tessellator = self
|
let tessellator = self
|
||||||
@ -581,15 +522,16 @@ impl Handwriting {
|
|||||||
let line = egui::Shape::line_segment([from, to], mesh_context.stroke);
|
let line = egui::Shape::line_segment([from, to], mesh_context.stroke);
|
||||||
tessellator.tessellate_shape(line, &mut mesh);
|
tessellator.tessellate_shape(line, &mut mesh);
|
||||||
|
|
||||||
self.draw_mesh_to_texture(&mesh, mesh_context, ui);
|
self.draw_mesh_to_texture(&mesh, mesh_context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draw a single mesh onto the existing texture.
|
/// Draw a single mesh onto the existing texture.
|
||||||
fn draw_mesh_to_texture(&mut self, mesh: &Mesh, mesh_context: &MeshContext, ui: &mut Ui) {
|
fn draw_mesh_to_texture(&mut self, mesh: &Mesh, mesh_context: &MeshContext) {
|
||||||
let triangles = mesh_triangles(mesh);
|
let triangles = mesh_triangles(mesh);
|
||||||
let point_to_pixel = TSTransform::from_scaling(mesh_context.pixels_per_point);
|
let point_to_pixel = TSTransform::from_scaling(mesh_context.pixels_per_point);
|
||||||
rasterize_onto::<StrokeBlendMode>(&mut self.e.image, point_to_pixel, triangles);
|
self.e
|
||||||
texture!(self.e, ui, mesh_context).set(self.e.image.clone(), Default::default());
|
.canvas_rasterizer
|
||||||
|
.rasterize(point_to_pixel, triangles);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn strokes(&self) -> &[Vec<Pos2>] {
|
pub fn strokes(&self) -> &[Vec<Pos2>] {
|
||||||
|
|||||||
Reference in New Issue
Block a user