-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathbrushstroke.rs
376 lines (329 loc) · 12.7 KB
/
brushstroke.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Imports
use super::content::GeneratedContentImages;
use super::Content;
use crate::Drawable;
use crate::{
render::{self},
strokes::content,
};
use p2d::bounding_volume::{Aabb, BoundingVolume};
use rnote_compose::ext::AabbExt;
use rnote_compose::penpath::{Element, Segment};
use rnote_compose::shapes::Shapeable;
use rnote_compose::style::Composer;
use rnote_compose::transform::Transformable;
use rnote_compose::{PenPath, Style};
use serde::{Deserialize, Serialize};
use tracing::error;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "brushstroke")]
pub struct BrushStroke {
#[serde(rename = "path")]
pub path: PenPath,
#[serde(default, rename = "style")]
pub style: Style,
// since the path can have many hitboxes, we store them here and update them when the stroke geometry changes
#[serde(skip)]
hitboxes: Vec<Aabb>,
}
impl Content for BrushStroke {
fn gen_images(
&self,
viewport: Aabb,
image_scale: f64,
) -> Result<GeneratedContentImages, anyhow::Error> {
/// The threshold of the image size on either axis.
/// When below it the stroke is rendered as a single image
const IMAGES_SIZE_THRESHOLD: f64 = 1000.0;
/// The threshold of the stroke width in relation to either bound axis.
/// When above it the stroke is rendered as a single image
const IMAGES_STROKE_WIDTH_BOUNDS_THRESHOLD: f64 = 0.2;
let bounds = self.bounds();
let partial = !viewport.contains(&bounds);
let Some(bounds) = viewport.intersection(&bounds) else {
return Ok(GeneratedContentImages::Partial {
images: vec![],
viewport,
});
};
let bounds_extents = bounds.extents();
let image_size_condition = bounds_extents[0] < IMAGES_SIZE_THRESHOLD / image_scale
&& bounds_extents[1] < IMAGES_SIZE_THRESHOLD / image_scale;
let stroke_width_condition = self.style.stroke_width()
> IMAGES_STROKE_WIDTH_BOUNDS_THRESHOLD * bounds_extents[0]
|| self.style.stroke_width() > IMAGES_STROKE_WIDTH_BOUNDS_THRESHOLD * bounds_extents[1];
// if these conditions evaluate true the stroke is rendered as a single image
let images = if image_size_condition || stroke_width_condition {
// generate a single image when bounds are smaller than threshold
match &self.style {
Style::Smooth(options) => {
let image = render::Image::gen_with_piet(
|piet_cx| {
self.path.draw_composed(piet_cx, options);
Ok(())
},
bounds,
image_scale,
);
match image {
Ok(image) => vec![image],
Err(e) => {
error!("Generating images for brushstroke failed , Err: {e:?}");
vec![]
}
}
}
Style::Rough(_options) => {
// Unsupported
vec![]
}
Style::Textured(options) => {
let image = render::Image::gen_with_piet(
|piet_cx| {
self.path.draw_composed(piet_cx, options);
Ok(())
},
bounds,
image_scale,
);
match image {
Ok(image) => vec![image],
Err(e) => {
error!("Generating images for brushstroke failed , Err: {e:?}");
vec![]
}
}
}
}
} else {
match &self.style {
Style::Smooth(options) => {
let mut images = Vec::with_capacity(self.path.segments.len());
let mut prev = self.path.start;
for seg in self.path.segments.iter() {
let seg_path = PenPath::new_w_segments(prev, [*seg]);
let image = render::Image::gen_with_piet(
|piet_cx| {
seg_path.draw_composed(piet_cx, options);
Ok(())
},
seg_path.composed_bounds(options),
image_scale,
);
match image {
Ok(image) => images.push(image),
Err(e) => {
error!("generating images for brushstroke failed , Err: {e:?}");
}
}
prev = seg.end();
}
images
}
Style::Rough(_) => {
// Unsupported
vec![]
}
Style::Textured(options) => {
let mut options = options.clone();
let mut images = Vec::with_capacity(self.path.segments.len());
let mut prev = self.path.start;
for seg in self.path.segments.iter() {
let seg_path = PenPath::new_w_segments(prev, [*seg]);
let image = render::Image::gen_with_piet(
|piet_cx| {
seg_path.draw_composed(piet_cx, &options);
Ok(())
},
seg_path.composed_bounds(&options),
image_scale,
);
match image {
Ok(image) => images.push(image),
Err(e) => {
error!("generating images for brushstroke failed , Err: {e:?}");
}
}
options.advance_seed();
prev = seg.end();
}
images
}
}
};
if partial {
Ok(GeneratedContentImages::Partial { images, viewport })
} else {
Ok(GeneratedContentImages::Full(images))
}
}
fn draw_highlight(
&self,
cx: &mut impl piet::RenderContext,
total_zoom: f64,
) -> anyhow::Result<()> {
const PATH_HIGHLIGHT_MIN_STROKE_WIDTH: f64 = 5.0;
const DRAW_BOUNDS_THRESHOLD_AREA: f64 = 10_u32.pow(2) as f64;
let bounds = self.bounds();
if bounds.scale(total_zoom).volume() < DRAW_BOUNDS_THRESHOLD_AREA {
cx.fill(bounds.to_kurbo_rect(), &content::CONTENT_HIGHLIGHT_COLOR);
} else {
cx.stroke_styled(
self.outline_path(),
&content::CONTENT_HIGHLIGHT_COLOR,
(PATH_HIGHLIGHT_MIN_STROKE_WIDTH / total_zoom)
.max(self.style.stroke_width() + 3.0 / total_zoom),
&piet::StrokeStyle::new()
.line_join(piet::LineJoin::Round)
.line_cap(piet::LineCap::Round),
);
}
Ok(())
}
fn update_geometry(&mut self) {
self.hitboxes = self.gen_hitboxes_int();
}
}
impl Drawable for BrushStroke {
fn draw(&self, cx: &mut impl piet::RenderContext, _image_scale: f64) -> anyhow::Result<()> {
cx.save().map_err(|e| anyhow::anyhow!("{e:?}"))?;
match &self.style {
Style::Smooth(options) => self.path.draw_composed(cx, options),
Style::Rough(_) => {
// Rough style currently unsupported for pen paths
unimplemented!()
}
Style::Textured(options) => self.path.draw_composed(cx, options),
};
cx.restore().map_err(|e| anyhow::anyhow!("{e:?}"))?;
Ok(())
}
}
impl Shapeable for BrushStroke {
fn bounds(&self) -> Aabb {
match &self.style {
Style::Smooth(options) => self.path.composed_bounds(options),
Style::Rough(_options) => unimplemented!(),
Style::Textured(options) => self.path.composed_bounds(options),
}
}
fn hitboxes(&self) -> Vec<Aabb> {
self.hitboxes.clone()
}
fn outline_path(&self) -> kurbo::BezPath {
self.path.outline_path()
}
}
impl Transformable for BrushStroke {
fn translate(&mut self, offset: na::Vector2<f64>) {
self.path.translate(offset);
}
fn rotate(&mut self, angle: f64, center: na::Point2<f64>) {
self.path.rotate(angle, center);
}
fn scale(&mut self, scale: na::Vector2<f64>) {
self.path.scale(scale);
// Using the geometric mean behaves the best when scaling non-uniformly.
let scale_scalar = (scale[0] * scale[1]).sqrt();
self.style
.set_stroke_width(self.style.stroke_width() * scale_scalar);
}
}
impl BrushStroke {
pub fn new(start: Element, style: Style) -> Self {
let path = PenPath::new(start);
Self::from_penpath(path, style)
}
/// Construct from the given pen path and style.
pub fn from_penpath(path: PenPath, style: Style) -> Self {
let mut new_brushstroke = Self {
path,
style,
hitboxes: vec![],
};
new_brushstroke.update_geometry();
new_brushstroke
}
pub fn push_segment(&mut self, segment: Segment) {
self.path.segments.push(segment);
}
pub fn extend_w_segments(&mut self, segments: impl IntoIterator<Item = Segment>) {
self.path.extend(segments);
}
/// Replace the current path with the given new one. the new path must not be empty.
pub fn replace_path(&mut self, path: PenPath) {
self.path = path;
self.update_geometry();
}
// internal method generating the current hitboxes.
fn gen_hitboxes_int(&self) -> Vec<Aabb> {
let stroke_width = self.style.stroke_width();
self.path
.hitboxes()
.into_iter()
.map(|hb| hb.loosened(stroke_width * 0.5))
.collect()
}
pub fn gen_image_for_last_segments(
&self,
n_last_segments: usize,
image_scale: f64,
) -> Result<Option<render::Image>, anyhow::Error> {
let image = match &self.style {
Style::Smooth(options) => {
let path_len = self.path.segments.len();
let start_el = self
.path
.segments
.get(path_len.saturating_sub(n_last_segments).saturating_sub(1))
.map(|s| s.end())
.unwrap_or(self.path.start);
let range_path = PenPath::new_w_segments(
start_el,
self.path.segments[path_len.saturating_sub(n_last_segments)..]
.iter()
.copied(),
);
let image = render::Image::gen_with_piet(
|piet_cx| {
range_path.draw_composed(piet_cx, options);
Ok(())
},
range_path.composed_bounds(options),
image_scale,
)?;
Some(image)
}
Style::Rough(_) => None,
Style::Textured(options) => {
let mut options = options.clone();
let path_len = self.path.segments.len();
(0..path_len.saturating_sub(n_last_segments)).for_each(|_| {
options.advance_seed();
});
let start_el = self
.path
.segments
.get(path_len.saturating_sub(n_last_segments).saturating_sub(1))
.map(|s| s.end())
.unwrap_or(self.path.start);
let range_path = PenPath::new_w_segments(
start_el,
self.path.segments[path_len.saturating_sub(n_last_segments)..]
.iter()
.copied(),
);
let image = render::Image::gen_with_piet(
|piet_cx| {
range_path.draw_composed(piet_cx, &options);
Ok(())
},
range_path.composed_bounds(&options),
image_scale,
)?;
Some(image)
}
};
Ok(image)
}
}