Registry / RepositoryTypeScriptRustPython
Rollpie
ReadmeFiles
Versions
Info
Download
0.2.0579.9 KB2026-09-150.1.1576.6 KB2026-09-12
Version
0.2.0
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-09-15
Size
579.9 KB
Downloads
2
Checksum
58312d92e63a92784e82a974317afd849e59d143a6320c3cc62faa0980834a7b
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
//! 字形の輪郭
//!
//! 座標はフォントの単位のままで、y は上向きである。書き出すときに大きさを掛け、y を反転させる。

use crate::text::push_number;


/// 輪郭を作る 1 本の線
#[derive(Debug, Clone, PartialEq)]
pub enum Segment {
    /// 筆を上げて移す
    Move {x: f64, y: f64},
    /// 直線
    Line {x: f64, y: f64},
    /// 2 次ベジエ。TrueType の字形が使う
    Quadratic {x1: f64, y1: f64, x: f64, y: f64},
    /// 3 次ベジエ。CFF の字形が使う
    Cubic {x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64},
    /// いま描いている輪郭を閉じる
    Close,
}


/// 1 つの字形の輪郭
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Outline {
    pub segment: Vec<Segment>,
}


/// 輪郭を囲む箱。(左, 下, 右, 上)。線が 1 本もなければすべて 0
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)
}


/// 2 次ベジエの、両端でない極値を拾う
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);
    }
}


/// 3 次ベジエの、両端でない極値を拾う
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
}


/// 2 次ベジエの微分が 0 になる t。0 と 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}
}


/// 3 次ベジエの微分が 0 になる t。0 と 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
}


/// SVG の d 属性にする。scale を掛けて y を反転し、(offsetX, offsetY) へ置く
pub fn path_data(outline: &Outline, scale: f64, offset_x: f64, offset_y: f64) -> String {
    // 座標 1 つにつき 12 字ほど書く。あとから伸ばさずに済むだけの場所を先に取る
    let mut out = String::with_capacity(outline.segment.len() * 24);
    let place = |out: &mut String, x: f64, y: f64| {
        push_number(out, offset_x + x * scale);
        out.push(' ');
        push_number(out, offset_y - y * scale);
    };

    for segment in &outline.segment {
        match segment {
            Segment::Move {x, y} => {
                out.push('M');
                place(&mut out, *x, *y);
            }
            Segment::Line {x, y} => {
                out.push('L');
                place(&mut out, *x, *y);
            }
            Segment::Quadratic {x1, y1, x, y} => {
                out.push('Q');
                place(&mut out, *x1, *y1);
                out.push(' ');
                place(&mut out, *x, *y);
            }
            Segment::Cubic {x1, y1, x2, y2, x, y} => {
                out.push('C');
                place(&mut out, *x1, *y1);
                out.push(' ');
                place(&mut out, *x2, *y2);
                out.push(' ');
                place(&mut out, *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() {
        // 制御点を結んだ箱ではなく、曲線そのものの箱を返す
        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");
    }
}