| 1 | //! Multi-contour path modifiers (`sExpand`, `sJitter`, `sOffset`, `LineType` dashing) |
| 2 | //! and shared contour offset/orientation utilities. |
| 3 | |
| 4 | use super::PathModifier; |
| 5 | |
| 6 | #[derive(Clone, Debug)] |
| 7 | pub(super) struct Contour { |
| 8 | pub(super) points: Vec<[f32; 2]>, |
| 9 | pub(super) closed: bool, |
| 10 | } |
| 11 | |
| 12 | pub(super) fn dedup_contour_points(pts: Vec<[f32; 2]>, closed: bool) -> Vec<[f32; 2]> { |
| 13 | let mut out: Vec<[f32; 2]> = Vec::with_capacity(pts.len()); |
| 14 | for p in pts { |
| 15 | if !p[0].is_finite() || !p[1].is_finite() { |
| 16 | continue; |
| 17 | } |
| 18 | if let Some(&prev) = out.last() { |
| 19 | if (p[0] - prev[0]).hypot(p[1] - prev[1]) <= 1e-6 { |
| 20 | continue; |
| 21 | } |
| 22 | } |
| 23 | out.push(p); |
| 24 | } |
| 25 | if closed && out.len() >= 2 { |
| 26 | if let (Some(&first), Some(&last)) = (out.first(), out.last()) { |
| 27 | if (last[0] - first[0]).hypot(last[1] - first[1]) <= 1e-6 { |
| 28 | out.pop(); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | out |
| 33 | } |
| 34 | |
| 35 | pub(super) fn signed_area(pts: &[[f32; 2]]) -> f32 { |
| 36 | let n = pts.len(); |
| 37 | if n < 3 { |
| 38 | return 0.0; |
| 39 | } |
| 40 | let mut sum = 0.0_f64; |
| 41 | for i in 0..n { |
| 42 | let a = pts[i]; |
| 43 | let b = pts[(i + 1) % n]; |
| 44 | sum += (a[0] as f64) * (b[1] as f64) - (b[0] as f64) * (a[1] as f64); |
| 45 | } |
| 46 | (0.5 * sum) as f32 |
| 47 | } |
| 48 | |
| 49 | pub(super) fn apply_path_modifier_contours( |
| 50 | contours: Vec<Contour>, |
| 51 | op: PathModifier, |
| 52 | shape_idx: u32, |
| 53 | mod_idx: u32, |
| 54 | ) -> Vec<Contour> { |
| 55 | match op { |
| 56 | PathModifier::Expand { amount, join_style, miter_limit } => { |
| 57 | expand_contours(contours, amount, join_style, miter_limit) |
| 58 | } |
| 59 | PathModifier::Jitter { x_amount, y_amount, seed, smoothness, per_shape } => { |
| 60 | if per_shape { |
| 61 | let dx = jitter_hash(seed, shape_idx, mod_idx, 0, 0) * x_amount; |
| 62 | let dy = jitter_hash(seed, shape_idx, mod_idx, 0, 1) * y_amount; |
| 63 | return contours |
| 64 | .into_iter() |
| 65 | .map(|c| Contour { |
| 66 | points: c.points.into_iter().map(|p| [p[0] + dx, p[1] + dy]).collect(), |
| 67 | closed: c.closed, |
| 68 | }) |
| 69 | .collect(); |
| 70 | } |
| 71 | contours |
| 72 | .into_iter() |
| 73 | .enumerate() |
| 74 | .map(|(ci, c)| { |
| 75 | jitter_contour( |
| 76 | c, |
| 77 | x_amount, |
| 78 | y_amount, |
| 79 | seed, |
| 80 | smoothness, |
| 81 | false, |
| 82 | shape_idx.wrapping_mul(131).wrapping_add(ci as u32), |
| 83 | mod_idx, |
| 84 | ) |
| 85 | }) |
| 86 | .collect() |
| 87 | } |
| 88 | PathModifier::Offset { |
| 89 | offset_x, |
| 90 | offset_y, |
| 91 | contour_offset, |
| 92 | position, |
| 93 | join_style, |
| 94 | miter_limit, |
| 95 | } => { |
| 96 | let mut cs = if contour_offset.abs() > 1e-6 { |
| 97 | expand_contours(contours, contour_offset, join_style, miter_limit) |
| 98 | } else { |
| 99 | contours |
| 100 | }; |
| 101 | if position.abs() > 1e-6 { |
| 102 | cs = cs.into_iter().map(|c| shift_contour_position(c, position)).collect(); |
| 103 | } |
| 104 | if offset_x.abs() > 1e-6 || offset_y.abs() > 1e-6 { |
| 105 | for c in &mut cs { |
| 106 | for p in &mut c.points { |
| 107 | p[0] += offset_x; |
| 108 | p[1] += offset_y; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | cs |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | fn expand_contours( |
| 118 | contours: Vec<Contour>, |
| 119 | amount: f32, |
| 120 | join_style: u8, |
| 121 | miter_limit: f32, |
| 122 | ) -> Vec<Contour> { |
| 123 | if amount.abs() <= 1e-6 { |
| 124 | return contours; |
| 125 | } |
| 126 | // Determine dominant orientation from the largest closed contour so outer boundaries |
| 127 | // and opposite-winding holes offset consistently (e.g., `sText` glyphs). |
| 128 | let mut best_abs = 0.0_f32; |
| 129 | let mut dom_sign = 0.0_f32; |
| 130 | for c in &contours { |
| 131 | if c.closed && c.points.len() >= 3 { |
| 132 | let a = signed_area(&c.points); |
| 133 | if a.abs() > best_abs { |
| 134 | best_abs = a.abs(); |
| 135 | dom_sign = if a > 0.0 { 1.0 } else { -1.0 }; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | let dom = (best_abs > 1e-10).then_some(dom_sign); |
| 140 | contours |
| 141 | .into_iter() |
| 142 | .filter_map(|c| { |
| 143 | let out = expand_contour(c, amount, join_style, miter_limit, dom); |
| 144 | if out.points.len() >= 2 && (!out.closed || out.points.len() >= 3) { |
| 145 | Some(out) |
| 146 | } else { |
| 147 | None |
| 148 | } |
| 149 | }) |
| 150 | .collect() |
| 151 | } |
| 152 | |
| 153 | /// Offsets a contour outward (`amount > 0`) or inward (`amount < 0`) using miter/bevel/round joins. |
| 154 | pub(super) fn expand_contour( |
| 155 | contour: Contour, |
| 156 | amount: f32, |
| 157 | join_style: u8, |
| 158 | miter_limit: f32, |
| 159 | orient_override: Option<f32>, |
| 160 | ) -> Contour { |
| 161 | if amount.abs() <= 1e-6 { |
| 162 | return contour; |
| 163 | } |
| 164 | let pts = dedup_contour_points(contour.points, contour.closed); |
| 165 | if !contour.closed { |
| 166 | if pts.len() < 2 { |
| 167 | return Contour { points: Vec::new(), closed: false }; |
| 168 | } |
| 169 | let n = pts.len(); |
| 170 | let mut out = Vec::with_capacity(n); |
| 171 | for i in 0..n { |
| 172 | let n0 = if i > 0 { |
| 173 | let dx = pts[i][0] - pts[i - 1][0]; |
| 174 | let dy = pts[i][1] - pts[i - 1][1]; |
| 175 | let l = dx.hypot(dy).max(1e-6); |
| 176 | [dy / l, -dx / l] |
| 177 | } else { |
| 178 | [0.0, 0.0] |
| 179 | }; |
| 180 | let n1 = if i + 1 < n { |
| 181 | let dx = pts[i + 1][0] - pts[i][0]; |
| 182 | let dy = pts[i + 1][1] - pts[i][1]; |
| 183 | let l = dx.hypot(dy).max(1e-6); |
| 184 | [dy / l, -dx / l] |
| 185 | } else { |
| 186 | [0.0, 0.0] |
| 187 | }; |
| 188 | let avg = if i == 0 { |
| 189 | n1 |
| 190 | } else if i + 1 == n { |
| 191 | n0 |
| 192 | } else { |
| 193 | let bx = n0[0] + n1[0]; |
| 194 | let by = n0[1] + n1[1]; |
| 195 | let denom = (1.0 + n0[0] * n1[0] + n0[1] * n1[1]).max(1e-4); |
| 196 | let scale = (1.0 / denom).min(miter_limit * 0.5); |
| 197 | [bx * scale, by * scale] |
| 198 | }; |
| 199 | out.push([pts[i][0] + avg[0] * amount, pts[i][1] + avg[1] * amount]); |
| 200 | } |
| 201 | return Contour { points: out, closed: false }; |
| 202 | } |
| 203 | |
| 204 | let n = pts.len(); |
| 205 | if n < 3 { |
| 206 | return Contour { points: Vec::new(), closed: true }; |
| 207 | } |
| 208 | let orig_area = signed_area(&pts); |
| 209 | if orig_area.abs() <= 1e-10 { |
| 210 | return Contour { points: Vec::new(), closed: true }; |
| 211 | } |
| 212 | // For CCW (orig_area > 0) in Y-up shape space, outward normal of edge (dx, dy) is (dy, -dx)/len. |
| 213 | let self_orient = if orig_area > 0.0 { 1.0_f32 } else { -1.0_f32 }; |
| 214 | let orient = orient_override.unwrap_or(self_orient); |
| 215 | let eff_amount = amount * (self_orient * orient); |
| 216 | |
| 217 | // Compute per-edge outward unit normals relative to the contour's own winding, |
| 218 | // and let `eff_amount` flip inward for opposite-winding holes. |
| 219 | let mut normals = Vec::with_capacity(n); |
| 220 | for i in 0..n { |
| 221 | let a = pts[i]; |
| 222 | let b = pts[(i + 1) % n]; |
| 223 | let dx = b[0] - a[0]; |
| 224 | let dy = b[1] - a[1]; |
| 225 | let len = dx.hypot(dy).max(1e-6); |
| 226 | normals.push([self_orient * dy / len, -self_orient * dx / len]); |
| 227 | } |
| 228 | |
| 229 | let mut out = Vec::with_capacity(n * 2); |
| 230 | for i in 0..n { |
| 231 | let n0 = normals[(i + n - 1) % n]; |
| 232 | let n1 = normals[i]; |
| 233 | let p = pts[i]; |
| 234 | |
| 235 | let dot = (n0[0] * n1[0] + n0[1] * n1[1]).clamp(-1.0, 1.0); |
| 236 | let cross = n0[0] * n1[1] - n0[1] * n1[0]; |
| 237 | if dot > 1.0 - 1e-5 { |
| 238 | out.push([p[0] + n0[0] * eff_amount, p[1] + n0[1] * eff_amount]); |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | let is_outer = (cross * self_orient * eff_amount) > 0.0; |
| 243 | let q0 = [p[0] + n0[0] * eff_amount, p[1] + n0[1] * eff_amount]; |
| 244 | let q1 = [p[0] + n1[0] * eff_amount, p[1] + n1[1] * eff_amount]; |
| 245 | let denom = 1.0 + dot; |
| 246 | |
| 247 | if is_outer { |
| 248 | match join_style { |
| 249 | 0 => { |
| 250 | out.push(q0); |
| 251 | out.push(q1); |
| 252 | } |
| 253 | 1 => { |
| 254 | let a0 = (n0[1] * eff_amount).atan2(n0[0] * eff_amount); |
| 255 | let a1 = (n1[1] * eff_amount).atan2(n1[0] * eff_amount); |
| 256 | let mut delta = a1 - a0; |
| 257 | while delta > std::f32::consts::PI { |
| 258 | delta -= std::f32::consts::TAU; |
| 259 | } |
| 260 | while delta < -std::f32::consts::PI { |
| 261 | delta += std::f32::consts::TAU; |
| 262 | } |
| 263 | let steps = ((delta.abs() / 0.25).ceil() as usize).clamp(1, 16); |
| 264 | let rad = eff_amount.abs(); |
| 265 | for s in 0..=steps { |
| 266 | let a = a0 + delta * (s as f32 / steps as f32); |
| 267 | out.push([p[0] + rad * a.cos(), p[1] + rad * a.sin()]); |
| 268 | } |
| 269 | } |
| 270 | _ => { |
| 271 | if denom > 1e-4 { |
| 272 | let miter_ratio = (2.0 / denom).sqrt(); |
| 273 | if miter_ratio <= miter_limit { |
| 274 | let inv = eff_amount / denom; |
| 275 | out.push([p[0] + (n0[0] + n1[0]) * inv, p[1] + (n0[1] + n1[1]) * inv]); |
| 276 | } else { |
| 277 | out.push(q0); |
| 278 | out.push(q1); |
| 279 | } |
| 280 | } else { |
| 281 | out.push(q0); |
| 282 | out.push(q1); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } else if denom > 1e-4 { |
| 287 | let miter_ratio = (2.0 / denom).sqrt(); |
| 288 | let clamped_inv = if miter_ratio <= miter_limit { |
| 289 | eff_amount / denom |
| 290 | } else { |
| 291 | eff_amount * (miter_limit / (2.0 * denom).sqrt().max(1e-4)) |
| 292 | }; |
| 293 | out.push([ |
| 294 | p[0] + (n0[0] + n1[0]) * clamped_inv, |
| 295 | p[1] + (n0[1] + n1[1]) * clamped_inv, |
| 296 | ]); |
| 297 | } else { |
| 298 | out.push([(q0[0] + q1[0]) * 0.5, (q0[1] + q1[1]) * 0.5]); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if eff_amount < 0.0 { |
| 303 | let mut min_x = f32::INFINITY; |
| 304 | let mut max_x = f32::NEG_INFINITY; |
| 305 | let mut min_y = f32::INFINITY; |
| 306 | let mut max_y = f32::NEG_INFINITY; |
| 307 | for &p in &pts { |
| 308 | min_x = min_x.min(p[0]); |
| 309 | max_x = max_x.max(p[0]); |
| 310 | min_y = min_y.min(p[1]); |
| 311 | max_y = max_y.max(p[1]); |
| 312 | } |
| 313 | let half_min = 0.5 * (max_x - min_x).min(max_y - min_y); |
| 314 | let new_area = signed_area(&out); |
| 315 | if (-eff_amount) >= half_min - 1e-5 || new_area * orig_area <= 1e-10 { |
| 316 | return Contour { points: Vec::new(), closed: true }; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | Contour { points: dedup_contour_points(out, true), closed: true } |
| 321 | } |
| 322 | |
| 323 | /// Deterministic signed float in `[-1.0, 1.0]` from `(seed, shape_idx, mod_idx, vertex_idx, axis)`. |
| 324 | #[inline] |
| 325 | fn jitter_hash(seed: i64, shape_idx: u32, mod_idx: u32, vertex_idx: u32, axis: u32) -> f32 { |
| 326 | let mut h = (seed as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15) |
| 327 | ^ (shape_idx as u64).wrapping_mul(0xBF58_476D_1CE4_E5B9) |
| 328 | ^ (mod_idx as u64).wrapping_mul(0x94D0_49BB_1331_11EB) |
| 329 | ^ (vertex_idx as u64).wrapping_mul(0xD1B5_4A32_D192_ED03) |
| 330 | ^ (axis as u64).wrapping_mul(0x517C_C1B7_2722_0A95); |
| 331 | h = (h ^ (h >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); |
| 332 | h = (h ^ (h >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); |
| 333 | h ^= h >> 31; |
| 334 | let u = ((h >> 40) as f32) * (1.0 / ((1u32 << 24) as f32)); |
| 335 | u * 2.0 - 1.0 |
| 336 | } |
| 337 | |
| 338 | fn subdivide_for_jitter(pts: &[[f32; 2]], closed: bool) -> Vec<[f32; 2]> { |
| 339 | let n = pts.len(); |
| 340 | if n < 2 || n >= 48 { |
| 341 | return pts.to_vec(); |
| 342 | } |
| 343 | let seg_count = if closed { n } else { n - 1 }; |
| 344 | let mut total_len = 0.0_f32; |
| 345 | for i in 0..seg_count { |
| 346 | let a = pts[i]; |
| 347 | let b = pts[(i + 1) % n]; |
| 348 | total_len += (b[0] - a[0]).hypot(b[1] - a[1]); |
| 349 | } |
| 350 | if total_len <= 1e-6 { |
| 351 | return pts.to_vec(); |
| 352 | } |
| 353 | let target_step = (total_len / 48.0).max(1e-4); |
| 354 | let mut out = Vec::with_capacity(64); |
| 355 | for i in 0..seg_count { |
| 356 | let a = pts[i]; |
| 357 | let b = pts[(i + 1) % n]; |
| 358 | let len = (b[0] - a[0]).hypot(b[1] - a[1]); |
| 359 | let subs = ((len / target_step).ceil() as usize).clamp(1, 32); |
| 360 | for s in 0..subs { |
| 361 | let t = s as f32 / subs as f32; |
| 362 | out.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]); |
| 363 | } |
| 364 | } |
| 365 | if !closed { |
| 366 | out.push(pts[n - 1]); |
| 367 | } |
| 368 | out |
| 369 | } |
| 370 | |
| 371 | #[allow(clippy::too_many_arguments)] |
| 372 | fn jitter_contour( |
| 373 | contour: Contour, |
| 374 | x_amount: f32, |
| 375 | y_amount: f32, |
| 376 | seed: i64, |
| 377 | smoothness: f32, |
| 378 | per_shape: bool, |
| 379 | shape_idx: u32, |
| 380 | mod_idx: u32, |
| 381 | ) -> Contour { |
| 382 | if per_shape { |
| 383 | let dx = jitter_hash(seed, shape_idx, mod_idx, 0, 0) * x_amount; |
| 384 | let dy = jitter_hash(seed, shape_idx, mod_idx, 0, 1) * y_amount; |
| 385 | let points = contour.points.into_iter().map(|p| [p[0] + dx, p[1] + dy]).collect(); |
| 386 | return Contour { points, closed: contour.closed }; |
| 387 | } |
| 388 | |
| 389 | let pts = subdivide_for_jitter(&contour.points, contour.closed); |
| 390 | let n = pts.len(); |
| 391 | if n < 2 { |
| 392 | return Contour { points: pts, closed: contour.closed }; |
| 393 | } |
| 394 | |
| 395 | let mut offsets: Vec<[f32; 2]> = (0..n) |
| 396 | .map(|i| { |
| 397 | [ |
| 398 | jitter_hash(seed, shape_idx, mod_idx, i as u32, 0) * x_amount, |
| 399 | jitter_hash(seed, shape_idx, mod_idx, i as u32, 1) * y_amount, |
| 400 | ] |
| 401 | }) |
| 402 | .collect(); |
| 403 | |
| 404 | if smoothness > 1e-4 { |
| 405 | let sigma = (smoothness * 2.5).min((n as f32) * 0.5); |
| 406 | let radius = ((sigma * 2.5).ceil() as usize).clamp(1, n / 2); |
| 407 | let inv_two_sig_sq = 0.5 / (sigma * sigma).max(1e-4); |
| 408 | let mut smoothed = vec![[0.0_f32; 2]; n]; |
| 409 | for i in 0..n { |
| 410 | let mut sx = 0.0_f32; |
| 411 | let mut sy = 0.0_f32; |
| 412 | let mut wsum = 0.0_f32; |
| 413 | for d in -(radius as isize)..=(radius as isize) { |
| 414 | let w = (-((d * d) as f32) * inv_two_sig_sq).exp(); |
| 415 | let j = if contour.closed { |
| 416 | (i as isize + d).rem_euclid(n as isize) as usize |
| 417 | } else { |
| 418 | (i as isize + d).clamp(0, (n - 1) as isize) as usize |
| 419 | }; |
| 420 | sx += offsets[j][0] * w; |
| 421 | sy += offsets[j][1] * w; |
| 422 | wsum += w; |
| 423 | } |
| 424 | if wsum > 1e-6 { |
| 425 | smoothed[i] = [sx / wsum, sy / wsum]; |
| 426 | } |
| 427 | } |
| 428 | let boost = (1.0 + sigma.sqrt()).min(3.0); |
| 429 | for s in &mut smoothed { |
| 430 | s[0] = (s[0] * boost).clamp(-x_amount.abs(), x_amount.abs()); |
| 431 | s[1] = (s[1] * boost).clamp(-y_amount.abs(), y_amount.abs()); |
| 432 | } |
| 433 | offsets = smoothed; |
| 434 | } |
| 435 | |
| 436 | let points = pts |
| 437 | .into_iter() |
| 438 | .zip(offsets) |
| 439 | .map(|(p, o)| [p[0] + o[0], p[1] + o[1]]) |
| 440 | .collect(); |
| 441 | Contour { points, closed: contour.closed } |
| 442 | } |
| 443 | |
| 444 | fn shift_contour_position(contour: Contour, position: f32) -> Contour { |
| 445 | let pts = dedup_contour_points(contour.points, contour.closed); |
| 446 | let n = pts.len(); |
| 447 | if n < 2 { |
| 448 | return Contour { points: pts, closed: contour.closed }; |
| 449 | } |
| 450 | |
| 451 | if !contour.closed { |
| 452 | let mut total = 0.0_f32; |
| 453 | for i in 0..n - 1 { |
| 454 | total += (pts[i + 1][0] - pts[i][0]).hypot(pts[i + 1][1] - pts[i][1]); |
| 455 | } |
| 456 | let cdx = pts[n - 1][0] - pts[0][0]; |
| 457 | let cdy = pts[n - 1][1] - pts[0][1]; |
| 458 | let clen = cdx.hypot(cdy); |
| 459 | if clen <= 1e-6 || total <= 1e-6 { |
| 460 | return Contour { points: pts, closed: false }; |
| 461 | } |
| 462 | let shift = position * total; |
| 463 | let tx = (cdx / clen) * shift; |
| 464 | let ty = (cdy / clen) * shift; |
| 465 | return Contour { |
| 466 | points: pts.into_iter().map(|p| [p[0] + tx, p[1] + ty]).collect(), |
| 467 | closed: false, |
| 468 | }; |
| 469 | } |
| 470 | |
| 471 | let frac = position.rem_euclid(1.0); |
| 472 | if frac <= 1e-6 || frac >= 1.0 - 1e-6 { |
| 473 | return Contour { points: pts, closed: true }; |
| 474 | } |
| 475 | |
| 476 | let mut seg_lens = Vec::with_capacity(n); |
| 477 | let mut total = 0.0_f32; |
| 478 | for i in 0..n { |
| 479 | let a = pts[i]; |
| 480 | let b = pts[(i + 1) % n]; |
| 481 | let l = (b[0] - a[0]).hypot(b[1] - a[1]); |
| 482 | seg_lens.push(l); |
| 483 | total += l; |
| 484 | } |
| 485 | if total <= 1e-6 { |
| 486 | return Contour { points: pts, closed: true }; |
| 487 | } |
| 488 | |
| 489 | let target = frac * total; |
| 490 | let mut acc = 0.0_f32; |
| 491 | let mut split_idx = 0usize; |
| 492 | let mut split_t = 0.0_f32; |
| 493 | for (i, &l) in seg_lens.iter().enumerate() { |
| 494 | if acc + l >= target || i + 1 == n { |
| 495 | split_idx = i; |
| 496 | split_t = if l > 1e-6 { ((target - acc) / l).clamp(0.0, 1.0) } else { 0.0 }; |
| 497 | break; |
| 498 | } |
| 499 | acc += l; |
| 500 | } |
| 501 | |
| 502 | let a = pts[split_idx]; |
| 503 | let b = pts[(split_idx + 1) % n]; |
| 504 | let start_pt = [a[0] + (b[0] - a[0]) * split_t, a[1] + (b[1] - a[1]) * split_t]; |
| 505 | let mut out = Vec::with_capacity(n + 1); |
| 506 | out.push(start_pt); |
| 507 | for k in 1..=n { |
| 508 | let idx = (split_idx + k) % n; |
| 509 | let p = pts[idx]; |
| 510 | if (p[0] - out.last().unwrap()[0]).hypot(p[1] - out.last().unwrap()[1]) > 1e-6 { |
| 511 | out.push(p); |
| 512 | } |
| 513 | } |
| 514 | Contour { points: dedup_contour_points(out, true), closed: true } |
| 515 | } |
| 516 | |
| 517 | /// Splits pixel-space `contours` into dash/gap open `zeno::Command` segments for `line_type > 0`. |
| 518 | pub(super) fn dash_contours( |
| 519 | contours: &[Contour], |
| 520 | line_type: u8, |
| 521 | stroke_px: f32, |
| 522 | ) -> Vec<zeno::Command> { |
| 523 | let w = stroke_px.max(1.0); |
| 524 | let pattern: &[f32] = match line_type { |
| 525 | 1 => &[3.0 * w, 2.0 * w], // Dash |
| 526 | 2 => &[0.5 * w, 1.5 * w], // Dot |
| 527 | 3 => &[3.0 * w, 1.5 * w, 0.5 * w, 1.5 * w], // DashDot |
| 528 | 4 => &[3.0 * w, 1.5 * w, 0.5 * w, 1.5 * w, 0.5 * w, 1.5 * w], // DashDotDot |
| 529 | _ => return Vec::new(), |
| 530 | }; |
| 531 | let mut out = Vec::new(); |
| 532 | for c in contours { |
| 533 | let n = c.points.len(); |
| 534 | if n < 2 { |
| 535 | continue; |
| 536 | } |
| 537 | let seg_count = if c.closed { n } else { n - 1 }; |
| 538 | let mut pat_idx = 0usize; |
| 539 | let mut pat_rem = pattern[0]; |
| 540 | let mut draw_on = true; |
| 541 | let mut pen_active = false; |
| 542 | for s in 0..seg_count { |
| 543 | let a = c.points[s]; |
| 544 | let b = c.points[(s + 1) % n]; |
| 545 | let dx = b[0] - a[0]; |
| 546 | let dy = b[1] - a[1]; |
| 547 | let len = dx.hypot(dy); |
| 548 | if len <= 1e-6 { |
| 549 | continue; |
| 550 | } |
| 551 | let mut pos = 0.0_f32; |
| 552 | while pos < len - 1e-5 { |
| 553 | let step = pat_rem.min(len - pos); |
| 554 | let t0 = pos / len; |
| 555 | let t1 = (pos + step) / len; |
| 556 | let p0 = [a[0] + dx * t0, a[1] + dy * t0]; |
| 557 | let p1 = [a[0] + dx * t1, a[1] + dy * t1]; |
| 558 | if draw_on { |
| 559 | if !pen_active { |
| 560 | out.push(zeno::Command::MoveTo(zeno::Vector::new(p0[0], p0[1]))); |
| 561 | pen_active = true; |
| 562 | } |
| 563 | out.push(zeno::Command::LineTo(zeno::Vector::new(p1[0], p1[1]))); |
| 564 | } else { |
| 565 | pen_active = false; |
| 566 | } |
| 567 | pos += step; |
| 568 | pat_rem -= step; |
| 569 | if pat_rem <= 1e-5 { |
| 570 | pat_idx = (pat_idx + 1) % pattern.len(); |
| 571 | pat_rem = pattern[pat_idx]; |
| 572 | draw_on = pat_idx % 2 == 0; |
| 573 | if !draw_on { |
| 574 | pen_active = false; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | out |
| 581 | } |