Registry / RepositoryTypeScriptRustPython
Rollpie
ReadmeFiles
Versions
Info
Download
0.2.0528.5 KB2026-10-160.1.1542.8 KB2026-10-150.1.0541.1 KB2026-10-15
Version
0.1.1
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-10-15
Size
542.8 KB
Downloads
9
Checksum
2e36f65a5798a2a0281457d1ed7f5a4e1a74967fea2880b4961ede38808a3682
Dependencies
None

outline.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
//! Glyph outlines
//!
//! Coordinates stay in font units, and y points up. When written out, the coordinates are
//! multiplied by the size, and y is flipped.

use crate::text::number;


/// One segment that makes up an outline
#[derive(Debug, Clone, PartialEq)]
pub enum Segment {
    /// Moves to a point without drawing
    Move {x: f64, y: f64},
    /// A straight line
    Line {x: f64, y: f64},
    /// A quadratic Bezier curve. TrueType glyphs use it
    Quadratic {x1: f64, y1: f64, x: f64, y: f64},
    /// A cubic Bezier curve. CFF glyphs use it
    Cubic {x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64},
    /// Closes the outline currently being drawn
    Close,
}


/// The outline of one glyph
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Outline {
    pub segment: Vec<Segment>,
}


/// The box around an outline, as (left, bottom, right, top). All 0 if there is no segment
pub fn bounds(outline: &Outline) -> (f64, f64, f64, f64) {
    let mut left = f64::INFINITY;
    let mut bottom = f64::INFINITY;
    let mut right = f64::NEG_INFINITY;
    let mut top = f64::NEG_INFINITY;
    let mut current = (0.0, 0.0);
    let mut start = (0.0, 0.0);
    let mut touched = false;

    let mut take = |x: f64, y: f64| {
        left = left.min(x);
        bottom = bottom.min(y);
        right = right.max(x);
        top = top.max(y);
    };

    for segment in &outline.segment {
        match segment {
            Segment::Move {x, y} => {
                current = (*x, *y);
                start = current;
            }
            Segment::Line {x, y} => {
                take(current.0, current.1);
                take(*x, *y);
                current = (*x, *y);
                touched = true;
            }
            Segment::Quadratic {x1, y1, x, y} => {
                take(current.0, current.1);
                take(*x, *y);
                take_quadratic((current.0, *x1, *x), (current.1, *y1, *y), &mut take);
                current = (*x, *y);
                touched = true;
            }
            Segment::Cubic {x1, y1, x2, y2, x, y} => {
                take(current.0, current.1);
                take(*x, *y);
                take_cubic(
                    (current.0, *x1, *x2, *x),
                    (current.1, *y1, *y2, *y),
                    &mut take,
                );
                current = (*x, *y);
                touched = true;
            }
            Segment::Close => current = start,
        }
    }

    if !touched {
        return (0.0, 0.0, 0.0, 0.0);
    }

    (left, bottom, right, top)
}


/// Takes the extrema of a quadratic Bezier curve that are not at its ends
fn take_quadratic(x: (f64, f64, f64), y: (f64, f64, f64), take: &mut impl FnMut(f64, f64)) {
    let mut at = |t: f64| {
        take(quadratic_at(x.0, x.1, x.2, t), quadratic_at(y.0, y.1, y.2, t));
    };

    if let Some(t) = quadratic_turn(x.0, x.1, x.2) {
        at(t);
    }

    if let Some(t) = quadratic_turn(y.0, y.1, y.2) {
        at(t);
    }
}


/// Takes the extrema of a cubic Bezier curve that are not at its ends
fn take_cubic(x: (f64, f64, f64, f64), y: (f64, f64, f64, f64), take: &mut impl FnMut(f64, f64)) {
    for t in cubic_turn(x.0, x.1, x.2, x.3) {
        take(cubic_at(x.0, x.1, x.2, x.3, t), cubic_at(y.0, y.1, y.2, y.3, t));
    }

    for t in cubic_turn(y.0, y.1, y.2, y.3) {
        take(cubic_at(x.0, x.1, x.2, x.3, t), cubic_at(y.0, y.1, y.2, y.3, t));
    }
}


fn quadratic_at(p0: f64, p1: f64, p2: f64, t: f64) -> f64 {
    let s = 1.0 - t;

    s * s * p0 + 2.0 * s * t * p1 + t * t * p2
}


fn cubic_at(p0: f64, p1: f64, p2: f64, p3: f64, t: f64) -> f64 {
    let s = 1.0 - t;

    s * s * s * p0 + 3.0 * s * s * t * p1 + 3.0 * s * t * t * p2 + t * t * t * p3
}


/// The t at which the derivative of a quadratic Bezier curve is 0. Returns it only if it is
/// between 0 and 1
fn quadratic_turn(p0: f64, p1: f64, p2: f64) -> Option<f64> {
    let bottom = p0 - 2.0 * p1 + p2;

    if bottom.abs() < 1e-12 {
        return None;
    }

    let t = (p0 - p1) / bottom;

    if t > 0.0 && t < 1.0 {Some(t)} else {None}
}


/// The values of t at which the derivative of a cubic Bezier curve is 0. Returns only those
/// between 0 and 1
fn cubic_turn(p0: f64, p1: f64, p2: f64, p3: f64) -> Vec<f64> {
    let a = 3.0 * (-p0 + 3.0 * p1 - 3.0 * p2 + p3);
    let b = 6.0 * (p0 - 2.0 * p1 + p2);
    let c = 3.0 * (p1 - p0);
    let mut found = Vec::new();

    if a.abs() < 1e-12 {
        if b.abs() > 1e-12 {
            found.push(-c / b);
        }

    } else {
        let discriminant = b * b - 4.0 * a * c;

        if discriminant >= 0.0 {
            let root = discriminant.sqrt();
            found.push((-b + root) / (2.0 * a));
            found.push((-b - root) / (2.0 * a));
        }
    }

    found.retain(|t| *t > 0.0 && *t < 1.0);
    found
}


/// Writes an outline as an SVG d attribute. Multiplies by scale, flips y, and places it at
/// (offsetX, offsetY)
pub fn path_data(outline: &Outline, scale: f64, offset_x: f64, offset_y: f64) -> String {
    let mut out = String::new();
    let place = |x: f64, y: f64| {
        format!("{} {}", number(offset_x + x * scale), number(offset_y - y * scale))
    };

    for segment in &outline.segment {
        match segment {
            Segment::Move {x, y} => {
                out.push('M');
                out.push_str(&place(*x, *y));
            }
            Segment::Line {x, y} => {
                out.push('L');
                out.push_str(&place(*x, *y));
            }
            Segment::Quadratic {x1, y1, x, y} => {
                out.push('Q');
                out.push_str(&place(*x1, *y1));
                out.push(' ');
                out.push_str(&place(*x, *y));
            }
            Segment::Cubic {x1, y1, x2, y2, x, y} => {
                out.push('C');
                out.push_str(&place(*x1, *y1));
                out.push(' ');
                out.push_str(&place(*x2, *y2));
                out.push(' ');
                out.push_str(&place(*x, *y));
            }
            Segment::Close => out.push('Z'),
        }
    }

    out
}


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

    #[test]
    fn a_straight_box() {
        let outline = Outline {
            segment: vec![
                Segment::Move {x: 10.0, y: 20.0},
                Segment::Line {x: 100.0, y: 20.0},
                Segment::Line {x: 100.0, y: 200.0},
                Segment::Close,
            ],
        };
        assert_eq!(bounds(&outline), (10.0, 20.0, 100.0, 200.0));
    }

    #[test]
    fn a_curve_reaches_past_its_control_points() {
        // Returns the box of the curve itself, not the box of its control points
        let outline = Outline {
            segment: vec![
                Segment::Move {x: 0.0, y: 0.0},
                Segment::Quadratic {x1: 50.0, y1: 100.0, x: 100.0, y: 0.0},
            ],
        };
        let (left, bottom, right, top) = bounds(&outline);
        assert_eq!((left, bottom, right), (0.0, 0.0, 100.0));
        assert!((top - 50.0).abs() < 1e-9);
    }

    #[test]
    fn a_cubic_curve_too() {
        let outline = Outline {
            segment: vec![
                Segment::Move {x: 0.0, y: 0.0},
                Segment::Cubic {x1: 0.0, y1: 100.0, x2: 100.0, y2: 100.0, x: 100.0, y: 0.0},
            ],
        };
        let (_, bottom, _, top) = bounds(&outline);
        assert_eq!(bottom, 0.0);
        assert!((top - 75.0).abs() < 1e-9);
    }

    #[test]
    fn an_empty_outline_has_no_box() {
        assert_eq!(bounds(&Outline::default()), (0.0, 0.0, 0.0, 0.0));
    }

    #[test]
    fn writes_a_path_with_the_y_flipped() {
        let outline = Outline {
            segment: vec![
                Segment::Move {x: 0.0, y: 500.0},
                Segment::Line {x: 1000.0, y: 0.0},
                Segment::Close,
            ],
        };
        assert_eq!(path_data(&outline, 0.001, 0.0, 0.0), "M0 -0.5L1 0Z");
    }
}