rasterize 0.1.2

Simple and small 2D rendering library
Documentation
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
433
434
435
436
437
#![deny(warnings)]

use rasterize::*;
use std::{
    cmp::Ordering,
    collections::{BTreeSet, HashMap},
    hash::{Hash, Hasher},
    io::Write,
};

const EPSILON: Scalar = 1e-6;

// intersection between a dividing line and a segment
#[derive(Debug, Clone, Copy)]
struct Intersection {
    // segment under intersection
    segment: Segment,
    // segment id (subpath_index, segment_index)
    segment_id: (usize, usize),
    // parameter `t` of the intersection
    segment_t: Scalar,
    // whether to follow segminet or line
    segment_follow: bool,
    // winding delta introduced by intersection
    winding_delta: i32,
    // winding number following intersection
    winding: i32,
    // x coordinate of the intersection
    x_coord: Scalar,
    // intersection with low_y or high_y line
    y_low: bool,
    // index starting from intersections with lower x coordinate
    index: usize,
}

impl Ord for Intersection {
    fn cmp(&self, other: &Self) -> Ordering {
        (self.index, self.y_low).cmp(&(other.index, other.y_low))
    }
}

impl PartialOrd for Intersection {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Hash for Intersection {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.index.hash(state);
        self.y_low.hash(state);
    }
}

impl PartialEq for Intersection {
    fn eq(&self, other: &Self) -> bool {
        self.index == other.index && self.y_low == other.y_low
    }
}

impl Eq for Intersection {}

// Intersect path specified by segments with `y = 0` when `tr` transformation is applied.
fn intersect(segments: &Vec<Vec<Segment>>, tr: Transform, y_low: bool) -> Vec<Intersection> {
    let mut ints = Vec::new();
    for (subpath_index, subpath) in segments.iter().enumerate() {
        for (segment_index, segment) in subpath.iter().enumerate() {
            let segment_id = (subpath_index, segment_index);
            let segment_tr = segment.transform(tr);
            for root in segment_tr.roots() {
                let deriv_y = segment_tr.deriv().at(root).y();
                if deriv_y.abs() < EPSILON {
                    // throw away tangent intersections
                    continue;
                }
                ints.push(Intersection {
                    segment: *segment,
                    segment_id,
                    segment_t: root,
                    segment_follow: if y_low { deriv_y > 0.0 } else { deriv_y < 0.0 },
                    winding_delta: if deriv_y < 0.0 { -1 } else { 1 },
                    winding: 0,
                    x_coord: segment_tr.at(root).x(),
                    y_low,
                    index: 0,
                });
            }
        }
    }

    // order intersections by `x` coordinate
    ints.sort_by(|a, b| {
        a.x_coord
            .partial_cmp(&b.x_coord)
            .expect("invalid coordinate value")
    });

    let mut winding = 0;
    let mut output: Vec<Intersection> = Vec::with_capacity(ints.len());
    for mut int in ints {
        // remove duplicate intersections
        if output.len() > 0 {
            let int_prev = output[output.len() - 1];
            if (int.x_coord - int_prev.x_coord).abs() < EPSILON {
                if int.winding_delta + int_prev.winding_delta == 0 {
                    // this might happend when line touches the corner of the path
                    winding -= int_prev.winding_delta;
                    output.pop();
                }
                continue;
            }
        }
        // update winding and index
        winding += int.winding_delta;
        int.winding = winding;
        int.index = output.len();
        output.push(int);
    }
    output
}

/// Convert path to hatched version of the path
///
/// All hatches are perpendicular to `normal` with perioud of `normal.lenght()`,
/// with covered width equal to `period * ratio`.
fn hatch(path: &Path, normal: Line, ratio: Scalar) -> Path {
    if ratio <= 0.0 || ratio >= 1.0 {
        return path.clone();
    }
    let period = normal.length();

    // find transformation which makes hatch lines horizontal
    let dir = normal.direction();
    let tangent = Line::new(
        normal.start(),
        normal.start() + Point::new(-dir.y(), dir.x()),
    );
    let tr = Transform::make_horizontal(tangent);

    // find transformation so origin would be the first line effecting the path
    let bbox_tr = match path.bbox(tr) {
        None => return path.clone(),
        Some(bbox) => bbox,
    };
    let offset_y = -(bbox_tr.y() / period).floor() * period;
    let tr = Transform::new_translate(-bbox_tr.x(), offset_y) * tr;

    // all segmentes grouped by subpaths with included closing lines
    let segments: Vec<_> = path
        .subpaths()
        .iter()
        .map(|subpath| {
            let last = subpath
                .end()
                .is_close_to(subpath.start())
                .then(|| Line::new(subpath.end(), subpath.start()).into());
            subpath
                .segments()
                .iter()
                .copied()
                .chain(last)
                .collect::<Vec<_>>()
        })
        .collect();

    let mut subpaths_out = Vec::new();
    let mut offset_y = 0.0;
    while offset_y < bbox_tr.height() + period {
        let ints_low = intersect(
            &segments,
            Transform::new_translate(0.0, -offset_y) * tr,
            true,
        );
        let ints_high = intersect(
            &segments,
            Transform::new_translate(0.0, -offset_y - period * ratio) * tr,
            false,
        );

        // mapping from segment_id to a list of intersections ordered by curves `t` parameter
        let mut id_to_ints = HashMap::new();
        for int in ints_low.iter().copied().chain(ints_high.iter().copied()) {
            id_to_ints
                .entry(int.segment_id)
                .or_insert_with(Vec::new)
                .push(int);
        }
        for (_, ints) in id_to_ints.iter_mut() {
            // order intersections by `t`
            ints.sort_by(|a, b| {
                a.segment_t
                    .partial_cmp(&b.segment_t)
                    .expect("invalid curve parameter value")
            });
        }

        let mut unvisited: BTreeSet<_> = ints_low
            .iter()
            .copied()
            .chain(ints_high.iter().copied())
            .collect();
        while !unvisited.is_empty() {
            let int_start = match unvisited.range(..).take(1).next() {
                Some(int_start) => *int_start,
                None => break,
            };
            let mut subpath_out = Vec::new();
            let mut int = int_start;
            while unvisited.contains(&int) {
                unvisited.remove(&int);

                if int.segment_follow {
                    // check if current segment have more intersections with higher `t`
                    let mut int_next = None;
                    for int_other in id_to_ints[&int.segment_id].iter() {
                        if int_other.segment_t > int.segment_t {
                            let seg = int.segment.cut(int.segment_t, int_other.segment_t);
                            subpath_out.push(seg);
                            int_next = Some(*int_other);
                            break;
                        }
                    }

                    match int_next {
                        Some(int_next) => {
                            int = int_next;
                        }
                        None => {
                            let (_, seg) = int.segment.split_at(int.segment_t);
                            subpath_out.push(seg);

                            // find and add all not intersecting segments
                            let mut segment_id = int.segment_id;
                            let start_id = segment_id;
                            loop {
                                let (subpath_index, segment_index) = segment_id;
                                let subpath = &segments[subpath_index];
                                let segment_index = (segment_index + 1) % subpath.len();
                                segment_id = (subpath_index, segment_index);
                                if segment_id == start_id {
                                    break;
                                }
                                if let Some(ints) = id_to_ints.get(&segment_id) {
                                    int = *ints
                                        .first()
                                        .expect("id to intersections map contains empty list");
                                    let (seg, _) = int.segment.split_at(int.segment_t);
                                    subpath_out.push(seg);
                                    break;
                                }
                                subpath_out.push(subpath[segment_index]);
                            }
                        }
                    }
                } else {
                    let index = if int.winding == 0 {
                        // going in the direction of lower x
                        int.index - 1
                    } else if int.winding == int.winding_delta {
                        // going in the direction of higher x
                        int.index + 1
                    } else {
                        panic!(
                            "path with winding not in [-1..1] are not supported: {:?}",
                            int
                        );
                    };
                    let int_next = if int.y_low {
                        ints_low[index]
                    } else {
                        ints_high[index]
                    };
                    let line = Line::new(
                        int.segment.at(int.segment_t),
                        int_next.segment.at(int_next.segment_t),
                    );
                    subpath_out.push(line.into());
                    int = int_next;
                }
                if int == int_start {
                    break;
                }
            }
            if let Some(subpath) = SubPath::new(subpath_out, true) {
                subpaths_out.push(subpath);
            }
        }

        // TODO:
        //   - include subpaths, when bounding box fits between low and high lines
        offset_y += period;
    }

    Path::new(subpaths_out)
}

fn generate_bar(
    frac: Scalar,
    bbox: BBox,
    border: Scalar,
    radii: Point,
    hatch_normal: Line,
    hatch_ratio: Scalar,
) -> Path {
    let frac = frac.clamp(0.0, 1.0);
    let mut path = Path::builder()
        .move_to((bbox.x(), bbox.y()))
        .rbox((bbox.width(), bbox.height()), radii)
        .build();
    if frac < 1.0 {
        let bx = bbox.x() + border;
        let by = bbox.y() + border;
        let bh = (bbox.height() - 2.0 * border) * (1.0 - frac);
        let bw = bbox.width() - 2.0 * border;
        let border = Path::builder()
            .move_to((bx, by))
            .rbox((bw, bh), radii)
            .build()
            .reverse();
        path.extend(border);
        let ib = hatch_normal.length() * hatch_ratio;
        let hatch_path = Path::builder()
            .move_to((bx + ib, by + ib))
            .rbox((bw - 2.0 * ib, bh - 2.0 * ib), radii)
            .build();
        path.extend(hatch(&hatch_path, hatch_normal, hatch_ratio));
    }
    path
}

fn generate_charging(bbox: BBox) -> Path {
    Path::builder()
        .move_to((bbox.x() + bbox.width() * 0.6, bbox.y()))
        .line_to((
            bbox.x() + bbox.width() * 0.6,
            bbox.y() + bbox.height() * 0.4,
        ))
        .line_to((bbox.x() + bbox.width(), bbox.y() + bbox.height() * 0.4))
        .line_to((bbox.x() + bbox.width() * 0.4, bbox.y() + bbox.height()))
        .line_to((
            bbox.x() + bbox.width() * 0.4,
            bbox.y() + bbox.height() * 0.6,
        ))
        .line_to((bbox.x(), bbox.y() + bbox.height() * 0.6))
        .close()
        .build()
}

struct Glyph {
    path: Path,
    name: String,
    unicode: String,
}

fn generate_font(
    out: &mut dyn Write,
    family: &str,
    glyphs: impl IntoIterator<Item = Glyph>,
) -> Result<(), std::io::Error> {
    writeln!(out, "<?xml version=\"1.0\" standalone=\"no\"?>")?;
    writeln!(out, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\" >")?;
    writeln!(out, "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\"> ")?;
    writeln!(out, "<defs>")?;
    writeln!(out, "  <font horiz-adv-x=\"500\">")?;
    writeln!(out, "  <font-face")?;
    writeln!(out, "    font-family=\"{}\"", family)?;
    writeln!(out, "    units-per-em=\"1000\"")?;
    writeln!(out, "    ascent=\"800\"")?;
    writeln!(out, "    descent=\"-200\"")?;
    writeln!(out, "  />")?;
    for glyph in glyphs {
        writeln!(
            out,
            "<glyph glyph-name=\"{}\" unicode=\"{}\"  d=\"{}\" />",
            glyph.name,
            glyph.unicode,
            glyph.path.to_svg_path(),
        )?;
    }
    writeln!(out, "  </font>")?;
    writeln!(out, "</defs>")?;
    writeln!(out, "</svg>")?;
    Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let tr = Transform::new_translate(0.0, 1000.0).pre_scale(1.0, -1.0);
    let mut glyphs = Vec::new();

    // bars
    let offset = '0' as u32;
    for index in 0..11 {
        let mut path = generate_bar(
            index as Scalar / 10.0,
            BBox::new((0.0, 0.0), (460.0, 1200.0)),
            70.0,
            Point::new(50.0, 50.0),
            Line::new((0.0, 0.0), (70.0, 70.0)),
            0.3,
        );
        path.transform(tr);
        glyphs.push(Glyph {
            path,
            name: format!("bar-{}", index),
            unicode: std::char::from_u32(offset + index).unwrap().to_string(),
        });
    }

    // bars charging
    let charging = generate_charging(BBox::new((100.0, 100.0), (360.0, 1100.0)));
    for (index, unicode) in [";", "&lt;", "=", "&gt;", "?", "@", "A", "B", "C", "D", "E"]
        .iter()
        .enumerate()
    {
        let mut path = generate_bar(
            index as Scalar / 10.0,
            BBox::new((0.0, 0.0), (460.0, 1200.0)),
            70.0,
            Point::new(50.0, 50.0),
            Line::new((0.0, 0.0), (70.0, 70.0)),
            0.3,
        );
        path.extend(charging.clone());
        path.transform(tr);
        glyphs.push(Glyph {
            path,
            name: format!("bar-charging-{}", index),
            unicode: unicode.to_string(),
        });
    }

    let mut out = Vec::new();
    generate_font(&mut out, "Hatchet", glyphs)?;
    println!("{}", String::from_utf8_lossy(&out));

    Ok(())
}