1use alloc::vec::Vec;
10
11use super::raster::Raster;
12use super::{Dc, Surface};
13use rt::fx::{maxss, sqrt};
14
15const KAPPA_FX: i32 = 141; fn kappa(r: i32, k: i32) -> i32 {
19 ((r as i64 * k as i64) >> 8) as i32
20}
21
22#[derive(Clone, Copy)]
23pub(crate) struct Sub {
24 pub start: usize,
25 pub count: usize,
26 pub closed: bool,
27}
28
29#[derive(Default)]
30pub struct Path {
31 pub(crate) pts: Vec<(f32, f32)>,
32 pub(crate) subs: Vec<Sub>,
33 open: bool,
34 start: (f32, f32),
35 cur: (f32, f32),
36 ox: i32,
37 oy: i32,
38 tolerance: f32,
39}
40
41#[inline]
43pub(crate) fn zf(a: f32, b: f32) -> bool {
44 !(a < b || a > b)
45}
46
47fn seg_count(m: f32, k: f32) -> i32 {
49 let v = sqrt(m * k);
50 let mut n = rt::fx::cvtt(v);
51 if !((n as f32) >= v) {
52 n += 1;
53 }
54 n.clamp(1, 128)
55}
56
57fn vlen(x: f32, y: f32) -> f32 {
58 sqrt(x * x + y * y)
59}
60
61impl Path {
62 pub fn set_tolerance(&mut self, tolerance: f32) -> f32 {
65 let old = if self.tolerance > 0.0 { self.tolerance } else { 0.2 };
66 if tolerance.is_finite() && tolerance > 0.0 {
67 self.tolerance = tolerance.clamp(0.001, 10.0);
68 }
69 old
70 }
71
72 fn curve_factor(&self) -> f32 {
73 if self.tolerance > 0.0 { 0.2 / self.tolerance } else { 1.0 }
74 }
75 pub fn begin(&mut self, dc: &Dc) {
77 self.pts.clear();
78 self.subs.clear();
79 self.open = false;
80 self.ox = dc.ox;
81 self.oy = dc.oy;
82 }
83
84 pub fn reset(&mut self) {
86 self.pts.clear();
87 self.subs.clear();
88 self.open = false;
89 }
90
91 pub fn set_origin(&mut self, ox: i32, oy: i32) {
92 self.ox = ox;
93 self.oy = oy;
94 }
95
96 pub fn is_empty(&self) -> bool {
97 self.subs.is_empty()
98 }
99
100 pub fn pts_mut(&mut self) -> &mut [(f32, f32)] {
103 &mut self.pts
104 }
105
106 fn to_xy(&self, x: i32, y: i32) -> (f32, f32) {
107 (x as f32 * (1.0 / 256.0) + self.ox as f32, y as f32 * (1.0 / 256.0) + self.oy as f32)
108 }
109
110 fn new_sub(&mut self, p: (f32, f32)) {
111 self.subs.push(Sub { start: self.pts.len(), count: 0, closed: false });
112 self.open = true;
113 self.start = p;
114 }
115
116 pub fn add_pt(&mut self, p: (f32, f32)) {
119 if !self.open {
120 if !self.subs.is_empty() {
121 let c = self.cur;
122 self.new_sub(c);
123 self.add_pt(c);
124 } else {
125 self.new_sub(p);
126 }
127 }
128 let si = self.subs.len() - 1;
129 if self.subs[si].count != 0 {
130 let last = self.pts[self.pts.len() - 1];
131 if zf(p.0, last.0) && zf(p.1, last.1) {
132 return;
133 }
134 }
135 self.pts.push(p);
136 self.subs[si].count += 1;
137 self.cur = p;
138 }
139
140 pub fn move_fx(&mut self, x: i32, y: i32) {
141 let p = self.to_xy(x, y);
142 self.new_sub(p);
143 self.add_pt(p);
144 }
145
146 pub fn move_f(&mut self, x: f32, y: f32) {
147 self.new_sub((x, y));
148 self.add_pt((x, y));
149 }
150
151 pub fn line_fx(&mut self, x: i32, y: i32) {
152 let p = self.to_xy(x, y);
153 self.add_pt(p);
154 }
155
156 pub fn line_f(&mut self, x: f32, y: f32) {
157 self.add_pt((x, y));
158 }
159
160 pub fn close(&mut self) {
161 if self.open {
162 let si = self.subs.len() - 1;
163 self.subs[si].closed = true;
164 self.open = false;
165 self.cur = self.start;
166 }
167 }
168
169 pub fn quad_fx(&mut self, cx: i32, cy: i32, x: i32, y: i32) {
171 let c = self.to_xy(cx, cy);
172 let p = self.to_xy(x, y);
173 self.quad_f(c.0, c.1, p.0, p.1);
174 }
175
176 pub fn quad_f(&mut self, cx: f32, cy: f32, x: f32, y: f32) {
178 if self.subs.is_empty() {
179 self.new_sub((cx, cy));
180 self.add_pt((cx, cy));
181 }
182 let (p0x, p0y) = self.cur;
183 let mx = p0x - (cx + cx) + x;
184 let my = p0y - (cy + cy) + y;
185 let n = seg_count(vlen(mx, my), 1.25 * self.curve_factor());
186 for i in 1..=n {
187 let t = i as f32 / n as f32;
188 let u = 1.0 - t;
189 let b0 = u * u;
190 let mut b1 = t * u;
191 b1 = b1 + b1;
192 let b2 = t * t;
193 let mut px = p0x * b0 + cx * b1 + x * b2;
194 let mut py = p0y * b0 + cy * b1 + y * b2;
195 if i == n {
196 px = x;
197 py = y;
198 }
199 self.add_pt((px, py));
200 }
201 }
202
203 pub fn cubic_fx(&mut self, c1x: i32, c1y: i32, c2x: i32, c2y: i32, x: i32, y: i32) {
205 let p1 = self.to_xy(c1x, c1y);
206 let p2 = self.to_xy(c2x, c2y);
207 let p3 = self.to_xy(x, y);
208 self.cubic_f(p1, p2, p3);
209 }
210
211 pub fn cubic_f(&mut self, p1: (f32, f32), p2: (f32, f32), p3: (f32, f32)) {
213 if self.subs.is_empty() {
214 self.new_sub(p1);
215 self.add_pt(p1);
216 }
217 let p0 = self.cur;
218 let m1 = vlen(p0.0 - (p1.0 + p1.0) + p2.0, p0.1 - (p1.1 + p1.1) + p2.1);
219 let m2 = vlen(p1.0 - (p2.0 + p2.0) + p3.0, p1.1 - (p2.1 + p2.1) + p3.1);
220 let n = seg_count(maxss(m2, m1), 3.75 * self.curve_factor());
221 for i in 1..=n {
222 if i == n {
223 self.add_pt(p3);
224 break;
225 }
226 let t = i as f32 / n as f32;
227 let u = 1.0 - t;
228 let uu = u * u;
229 let b0 = uu * u;
230 let b1 = uu * t * 3.0;
231 let tt = t * t;
232 let b2 = tt * u * 3.0;
233 let b3 = tt * t;
234 let px = p0.0 * b0 + p1.0 * b1 + p2.0 * b2 + p3.0 * b3;
235 let py = p0.1 * b0 + p1.1 * b1 + p2.1 * b2 + p3.1 * b3;
236 self.add_pt((px, py));
237 }
238 }
239
240 pub fn rect_fx(&mut self, x: i32, y: i32, w: i32, h: i32) {
241 let x1 = x + w;
242 let y1 = y + h;
243 self.move_fx(x, y);
244 self.line_fx(x1, y);
245 self.line_fx(x1, y1);
246 self.line_fx(x, y1);
247 self.close();
248 }
249
250 pub fn rrect_fx(&mut self, x: i32, y: i32, w: i32, h: i32, r: i32) {
252 let (x0, y0, x1, y1) = (x, y, x + w, y + h);
253 let r = r.min(w.min(h) >> 1);
254 if r <= 0 {
255 self.rect_fx(x, y, w, y1 - y0);
256 return;
257 }
258 let k = kappa(r, 256 - KAPPA_FX);
259 self.move_fx(x0 + r, y0);
260 self.line_fx(x1 - r, y0);
261 self.cubic_fx(x1 - k, y0, x1, y0 + k, x1, y0 + r);
262 self.line_fx(x1, y1 - r);
263 self.cubic_fx(x1, y1 - k, x1 - k, y1, x1 - r, y1);
264 self.line_fx(x0 + r, y1);
265 self.cubic_fx(x0 + k, y1, x0, y1 - k, x0, y1 - r);
266 self.line_fx(x0, y0 + r);
267 self.cubic_fx(x0, y0 + k, x0 + k, y0, x0 + r, y0);
268 self.close();
269 }
270
271 pub fn ellipse_fx(&mut self, cx: i32, cy: i32, rx: i32, ry: i32) {
273 let kx = kappa(rx, KAPPA_FX);
274 let ky = kappa(ry, KAPPA_FX);
275 self.move_fx(cx + rx, cy);
276 self.cubic_fx(cx + rx, cy + ky, cx + kx, cy + ry, cx, cy + ry);
277 self.cubic_fx(cx - kx, cy + ry, cx - rx, cy + ky, cx - rx, cy);
278 self.cubic_fx(cx - rx, cy - ky, cx - kx, cy - ry, cx, cy - ry);
279 self.cubic_fx(cx + kx, cy - ry, cx + rx, cy - ky, cx + rx, cy);
280 self.close();
281 }
282
283 pub fn fill_lines(&self, ras: &mut Raster) {
285 ras.lines_begin();
286 for s in &self.subs {
287 if s.count < 2 {
288 continue;
289 }
290 for k in 0..s.count {
291 let a = self.pts[s.start + k];
292 let b = if k + 1 < s.count { self.pts[s.start + k + 1] } else { self.pts[s.start] };
293 ras.emit_line(a.0, a.1, b.0, b.1);
294 }
295 }
296 }
297
298 pub fn fill(&self, ras: &mut Raster, dc: &Dc, sf: &mut Surface, rule: u32) {
300 self.fill_lines(ras);
301 ras.fill(dc, sf, rule);
302 }
303}