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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use druid::kurbo::{BezPath, ParamCurve, PathEl, PathSeg, Point};
use druid::piet::{LineCap, LineJoin, StrokeStyle};
use druid::{Color, RenderContext};
use serde::ser::SerializeSeq;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::effect::Effects;
use crate::time::Time;

mod serde_color {
    use super::*;

    pub fn serialize<S: Serializer>(c: &Color, ser: S) -> Result<S::Ok, S::Error> {
        ser.serialize_u32(c.as_rgba_u32())
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result<Color, D::Error> {
        Ok(Color::from_rgba32_u32(u32::deserialize(de)?))
    }
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct LineStyle {
    #[serde(with = "serde_color")]
    pub color: Color,
    pub thickness: f64,
}

// piet::Color doesn't implement PartialEq, so we can't derive this.
impl PartialEq for LineStyle {
    fn eq(&self, other: &LineStyle) -> bool {
        self.thickness == other.thickness && self.color.as_rgba_u32() == other.color.as_rgba_u32()
    }
}

#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct SegmentData {
    pub style: LineStyle,
    pub effects: Effects,
}

impl From<LineStyle> for SegmentData {
    fn from(style: LineStyle) -> SegmentData {
        SegmentData {
            style,
            effects: Effects::default(),
        }
    }
}

// TODO: there is something strange about the API of Curve: we only allow it to
// be created as a polyline, but most of the time (and particularly for
// serializing/deserializing) we expect it to have cubic segments. That's
// because in practice we always simplify and smooth the polyline. But it would
// be better to express this using 2 different types.
#[derive(Clone, Debug)]
pub struct Curve {
    pub path: BezPath,
    pub times: Vec<Time>,

    // The path can consist of many different "segments" (continuous parts between
    // pen lifts). This contains the first index of each segment, which will always
    // point to a `MoveTo`.
    seg_boundaries: Vec<usize>,

    // This has the same length as `seg_boundaries`.
    seg_data: Vec<SegmentData>,
}

impl Curve {
    pub fn new() -> Curve {
        Curve {
            path: BezPath::new(),
            times: Vec::new(),
            seg_boundaries: Vec::new(),
            seg_data: Vec::new(),
        }
    }

    pub fn line_to(&mut self, p: Point, time: Time) {
        self.path.line_to(p);
        self.times.push(time);
    }

    pub fn move_to(&mut self, p: Point, time: Time, style: LineStyle, effects: Effects) {
        let seg_data = SegmentData { style, effects };
        self.seg_boundaries.push(self.path.elements().len());
        self.seg_data.push(seg_data);
        self.path.move_to(p);
        self.times.push(time);
    }

    pub fn append_segment(&mut self, p: BezPath, t: Vec<Time>, data: SegmentData) {
        assert_eq!(p.elements().len(), t.len());
        if t.is_empty() {
            return;
        }
        if let Some(&last_time) = self.times.last() {
            assert!(last_time <= t[0]);
        }

        self.seg_boundaries.push(self.times.len());
        self.seg_data.push(data);
        self.times.extend_from_slice(&t[..]);
        // FIXME: this is inefficient, particularly when there are many segments.
        let mut elts = self.path.elements().to_owned();
        elts.extend_from_slice(p.elements());
        self.path = BezPath::from_vec(elts);
    }

    pub fn segments<'a>(&'a self) -> impl Iterator<Item = Segment<'a>> + 'a {
        self.seg_boundaries
            .iter()
            .enumerate()
            .map(move |(idx, &seg_start_idx)| {
                let seg_end_idx = self
                    .seg_boundaries
                    .get(idx + 1)
                    .cloned()
                    .unwrap_or(self.times.len());
                Segment {
                    style: self.seg_data[idx].style.clone(),
                    effects: &self.seg_data[idx].effects,
                    elements: &self.path.elements()[seg_start_idx..seg_end_idx],
                    times: &self.times[seg_start_idx..seg_end_idx],
                }
            })
    }

    // TODO: test this. Maybe add a check_consistent function to check the invariants of `Curve`
    pub fn smoothed(&self, distance_threshold: f64, angle_threshold: f64) -> Curve {
        let mut ret = Curve::new();
        let mut path = Vec::new();
        if self.times.is_empty() {
            return ret;
        }

        for seg in self.segments() {
            let points: Vec<Point> = seg
                .elements
                .iter()
                .map(|el| match el {
                    PathEl::MoveTo(p) => *p,
                    PathEl::LineTo(p) => *p,
                    _ => panic!("can only smooth polylines"),
                })
                .collect();

            let point_indices = crate::simplify::simplify(&points, distance_threshold);
            let times: Vec<Time> = point_indices.iter().map(|&i| seg.times[i]).collect();
            let points: Vec<Point> = point_indices.iter().map(|&i| points[i]).collect();
            let curve = crate::smooth::smooth(&points, 0.4, angle_threshold);

            let seg_data = SegmentData {
                style: seg.style.clone(),
                effects: seg.effects.to_owned(),
            };
            ret.times.extend_from_slice(&times);
            ret.seg_data.push(seg_data);
            ret.seg_boundaries.push(path.len());
            path.extend_from_slice(curve.elements());
        }

        ret.path = BezPath::from_vec(path);
        ret
    }

    pub fn render(&self, ctx: &mut impl RenderContext, time: Time) {
        let stroke_style = StrokeStyle {
            line_join: Some(LineJoin::Round),
            line_cap: Some(LineCap::Round),
            ..StrokeStyle::new()
        };

        for seg in self.segments() {
            if let Some(last) = seg.times.last() {
                if *last <= time {
                    let color = if let Some(fade) = seg.effects.fade() {
                        if time >= *last + fade.pause + fade.fade {
                            // The curve has faded out; no need to draw it at all
                            continue;
                        } else if time >= *last + fade.pause {
                            let ratio = (time - (*last + fade.pause)).as_micros() as f64
                                / fade.fade.as_micros() as f64;
                            seg.style.color.with_alpha(1.0 - ratio)
                        } else {
                            seg.style.color
                        }
                    } else {
                        seg.style.color
                    };
                    ctx.stroke_styled(&seg.elements, &color, seg.style.thickness, &stroke_style);
                } else {
                    // For the last segment, we construct a new segment whose end time is interpolated
                    // up until the current time.
                    // Note: we're doing some unnecessary cloning, just for the convenience of being able
                    // to use BezPath::get_seg.
                    let c = BezPath::from_vec(seg.elements.to_owned());
                    let t_idx = seg.times.binary_search(&time).unwrap_or_else(|i| i);

                    if t_idx == 0 {
                        // If we only contain the first element of the curve, it's a MoveTo and doesn't
                        // need to be drawn anyway.
                        break;
                    }

                    // We already checked that time > seg.times.last().
                    assert!(t_idx < seg.times.len());
                    assert_eq!(seg.times.len(), seg.elements.len());
                    let last_seg = c.get_seg(t_idx).unwrap();
                    // The indexing is ok, because we already checked t_idx > 0.
                    let prev_t = seg.times[t_idx - 1].as_micros() as f64;
                    let next_t = seg.times[t_idx].as_micros() as f64;
                    let t_ratio = if prev_t == next_t {
                        1.0
                    } else {
                        (time.as_micros() as f64 - prev_t) / (next_t - prev_t)
                    };
                    let last_seg = last_seg.subsegment(0.0..t_ratio);

                    let mut c: BezPath = c.iter().take(t_idx).collect();
                    match last_seg {
                        PathSeg::Cubic(x) => c.curve_to(x.p1, x.p2, x.p3),
                        PathSeg::Quad(x) => c.quad_to(x.p1, x.p2),
                        PathSeg::Line(x) => c.line_to(x.p1),
                    }

                    ctx.stroke_styled(&c, &seg.style.color, seg.style.thickness, &stroke_style);

                    // We've already rendered the segment spanning the ending time, so we're done.
                    break;
                }
            }
        }
    }
}

// A curve gets serialized as a sequence of segments.
impl Serialize for Curve {
    fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        let mut seq = ser.serialize_seq(Some(self.seg_data.len()))?;

        for seg in self.segments() {
            seq.serialize_element(&seg)?;
        }

        seq.end()
    }
}

impl<'a> Deserialize<'a> for Curve {
    fn deserialize<D: Deserializer<'a>>(de: D) -> Result<Curve, D::Error> {
        let segments: Vec<SavedSegment> = Deserialize::deserialize(de)?;
        let mut curve = Curve::new();

        for seg in segments {
            let p = |(x, y)| Point::new(x as f64 / 10_000.0, y as f64 / 10_000.0);

            let mut path = BezPath::new();
            if seg.elements.is_empty() {
                continue;
            }
            path.move_to(p(seg.elements[0]));
            for points in seg.elements[1..].chunks(3) {
                path.curve_to(p(points[0]), p(points[1]), p(points[2]));
            }

            let times = seg
                .times
                .into_iter()
                .map(|x| Time::from_micros(x as i64))
                .collect();
            let seg_data = SegmentData {
                style: seg.style,
                effects: seg.effects,
            };
            curve.append_segment(path, times, seg_data);
        }

        Ok(curve)
    }
}

#[derive(Debug, Deserialize)]
struct SavedSegment {
    elements: Vec<(i32, i32)>,
    times: Vec<u64>,
    style: LineStyle,
    effects: Effects,
}

/// A single continuous segment of a [`Curve`], with data borrowed from the curve.
///
/// The segments of a curve can be obtained from [`Curve::segments`].
#[derive(Serialize)]
pub struct Segment<'a> {
    /// The elements of this segment. This will always start with a `PathEl::MoveTo`.
    #[serde(serialize_with = "serialize_path_els")]
    pub elements: &'a [PathEl],

    /// The times at which the elements were drawn. This slice has the same length as `elements`.
    pub times: &'a [Time],

    /// The style for drawing this segment.
    pub style: LineStyle,

    /// The effects to use for this segment.
    pub effects: &'a Effects,
}

// We do manual serialization for curves (and segments), mainly to ensure that
// the file format stays stable.
fn serialize_path_els<S: Serializer>(path: &[PathEl], ser: S) -> Result<S::Ok, S::Error> {
    // We serialize as a list of tuples. Each moveto gives one; each curveto gives three.
    let len: usize = path
        .iter()
        .map(|el| match el {
            PathEl::MoveTo(_) => 1,
            PathEl::CurveTo(..) => 3,
            _ => 0,
        })
        .sum();

    let mut seq = ser.serialize_seq(Some(len))?;

    let mut point = |p: &Point| -> Result<(), S::Error> {
        let x = (p.x * 10_000.0)
            .round()
            .max(i32::MIN as f64)
            .min(i32::MAX as f64) as i32;
        let y = (p.y * 10_000.0)
            .round()
            .max(i32::MIN as f64)
            .min(i32::MAX as f64) as i32;
        seq.serialize_element(&(x, y))
    };

    for el in path {
        match el {
            PathEl::MoveTo(p) => {
                point(p)?;
            }
            PathEl::CurveTo(p1, p2, p3) => {
                point(p1)?;
                point(p2)?;
                point(p3)?;
            }
            _ => {
                log::error!("error serializing: unexpected path element {:?}", el);
            }
        }
    }
    seq.end()
}

#[cfg(test)]
pub mod tests {
    use super::*;

    pub fn basic_curve() -> Curve {
        let mut c = Curve::new();
        let style = LineStyle {
            color: Color::WHITE,
            thickness: 1.0,
        };
        c.move_to(
            Point::new(0.0, 0.0),
            Time::from_micros(1),
            style.clone(),
            Effects::default(),
        );
        c.line_to(Point::new(1.0, 1.0), Time::from_micros(2));
        c.line_to(Point::new(2.0, 2.0), Time::from_micros(3));

        c.move_to(
            Point::new(4.0, 0.0),
            Time::from_micros(6),
            style.clone(),
            Effects::default(),
        );
        c.line_to(Point::new(1.0, 1.0), Time::from_micros(7));
        c.line_to(Point::new(2.0, 2.0), Time::from_micros(8));

        c.smoothed(0.01, 2.0)
    }

    #[test]
    fn segments() {
        let c = basic_curve();
        assert_eq!(c.segments().count(), 2);
    }

    #[test]
    fn serialize_curve() {
        let mut c = Curve::new();
        let style = LineStyle {
            color: Color::WHITE,
            thickness: 1.0,
        };

        let mut path = BezPath::new();
        path.move_to((0.0, 0.0));
        path.curve_to((0.0, 1.0), (1.0, 1.0), (1.0, 0.0));
        c.append_segment(
            path,
            vec![Time::from_micros(1), Time::from_micros(2)],
            style.into(),
        );

        let ser = serde_json::to_string(&c).unwrap();
        assert_eq!(
            &ser,
            r#"[{"elements":[[0,0],[0,10000],[10000,10000],[10000,0]],"times":[1,2],"style":{"color":4294967295,"thickness":1.0},"effects":[]}]"#
        );

        let deserialized: Curve = serde_json::from_str(&ser).unwrap();
        // BezPath doesn't implement PartialEq, so just compare the other parts.
        assert_eq!(deserialized.times, c.times);
        assert_eq!(deserialized.seg_boundaries, c.seg_boundaries);
        assert_eq!(deserialized.seg_data, c.seg_data);
    }

    #[test]
    fn serde_two_segments() {
        let c = basic_curve();
        let written = serde_cbor::to_vec(&c).unwrap();
        let read: Curve = serde_cbor::from_slice(&written[..]).unwrap();
        assert_eq!(read.times, c.times);
        assert_eq!(read.seg_boundaries, c.seg_boundaries);
        assert_eq!(read.seg_data, c.seg_data);
    }
}