fix(anatomy): V1-2.2 model arcs as browsers store them
Second security review: endpoints equal in f32 draw no arc, as in browsers; an arc carries the spread of its f32 variants (endpoint or radius moved one ulp) in its deviation and their extremes in its bounds. Probe and mutants extended.
This commit is contained in:
1 parent
03be75beed
commit
86dce16b09
5 files changed
+141
-19
No files matched your search
@@ -75,9 +75,12 @@ pub const MAX_CURVE_CHORDS: usize = 64;
|
||||
/// The smallest arc radius read, zero aside: `2⁻²⁰`, the reciprocal of the largest coordinate,
|
||||
/// so the radii scaled up to reach the endpoint (at most `2⁸²` squared) stay finite in `f32`.
|
||||
pub const MIN_ARC_RADIUS: f64 = 1.0 / 1_048_576.0;
|
||||
/// The relative `f32` error of a browser's arc arithmetic, added to an arc's chord deviation
|
||||
/// times its scaled radius: a bound, not a measure (the centre may lie far outside the frame).
|
||||
const ARC_F32_DRIFT: f64 = 1.0 / 8_388_608.0;
|
||||
/// One `f32` ulp, relative (`2⁻²³`): browsers store path points in `f32` and compute arcs in it.
|
||||
const F32_ULP: f64 = 1.0 / 8_388_608.0;
|
||||
/// The relative error of a browser's arc arithmetic (about five chained `f32` operations),
|
||||
/// added to an arc's deviation times its scaled radius: an estimate, the centre lying perhaps
|
||||
/// far outside the frame.
|
||||
const ARC_F32_DRIFT: f64 = 4.0 * F32_ULP;
|
||||
|
||||
/// A fill outline: its subpaths as polylines (each closed by the fill), the largest distance
|
||||
/// between a chord and the curve it stands for, and whether more than the asked number of
|
||||
@@ -285,6 +288,7 @@ impl<'a> Cursor<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Arc {
|
||||
rx: f64,
|
||||
ry: f64,
|
||||
@@ -523,7 +527,9 @@ impl Pen {
|
||||
if tiny(arc.rx) || tiny(arc.ry) {
|
||||
return None;
|
||||
}
|
||||
if arc.to == self.at {
|
||||
// Browsers compare the endpoints as stored, in `f32`: equal there, no arc is drawn.
|
||||
let stored = |(x, y): (f64, f64)| (x as f32, y as f32);
|
||||
if stored(arc.to) == stored(self.at) {
|
||||
return Some(());
|
||||
}
|
||||
if arc.rx == 0.0 || arc.ry == 0.0 {
|
||||
@@ -539,16 +545,62 @@ impl Pen {
|
||||
// Over the sweep as `t` goes from 0 to 1 the second derivative stays within the larger
|
||||
// radius times the sweep squared.
|
||||
let radius = ellipse.rx.max(ellipse.ry);
|
||||
let spread = self.f32_spread(arc, &ellipse);
|
||||
let count = self.chords(
|
||||
radius * ellipse.sweep * ellipse.sweep,
|
||||
radius * ARC_F32_DRIFT,
|
||||
radius * ARC_F32_DRIFT + spread,
|
||||
);
|
||||
self.trace(count, |t| ellipse.at(ellipse.start + ellipse.sweep * t));
|
||||
self.line(arc.to);
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// How far the arc a browser draws may run from this one: the same arc with an endpoint or
|
||||
/// a radius moved by one `f32` ulp, compared at the same parameters (an ill-conditioned arc,
|
||||
/// a flat ellipse along its chord, moves a lot). Each variant's extremes join the bounds.
|
||||
fn f32_spread(&mut self, arc: &Arc, ellipse: &Ellipse) -> f64 {
|
||||
let from = self.at;
|
||||
let ulp = [from.0, from.1, arc.to.0, arc.to.1]
|
||||
.iter()
|
||||
.fold(1.0_f64, |m, v| m.max(v.abs()))
|
||||
* F32_ULP;
|
||||
let moved = |dx: f64, dy: f64, grow: (f64, f64)| Arc {
|
||||
rx: arc.rx * grow.0,
|
||||
ry: arc.ry * grow.1,
|
||||
to: (arc.to.0 + dx, arc.to.1 + dy),
|
||||
..*arc
|
||||
};
|
||||
let (more, same) = (1.0 + F32_ULP, 1.0);
|
||||
let variants = [
|
||||
moved(ulp, 0.0, (same, same)),
|
||||
moved(-ulp, 0.0, (same, same)),
|
||||
moved(0.0, ulp, (same, same)),
|
||||
moved(0.0, -ulp, (same, same)),
|
||||
moved(0.0, 0.0, (more, same)),
|
||||
moved(0.0, 0.0, (same, more)),
|
||||
];
|
||||
let mut spread = 0.0_f64;
|
||||
for variant in &variants {
|
||||
let other = Ellipse::through(from, variant);
|
||||
for angle in other.extreme_angles() {
|
||||
if other.sweeps(angle) {
|
||||
self.add(other.at(angle));
|
||||
}
|
||||
}
|
||||
for i in 0..=SPREAD_SAMPLES {
|
||||
let t = f64::from(i) / f64::from(SPREAD_SAMPLES);
|
||||
let here = ellipse.at(ellipse.start + ellipse.sweep * t);
|
||||
let there = other.at(other.start + other.sweep * t);
|
||||
spread = spread.max(distance((here.0 - there.0, here.1 - there.1)));
|
||||
}
|
||||
}
|
||||
spread
|
||||
}
|
||||
}
|
||||
|
||||
/// The parameters an arc and its `f32` variants are compared at.
|
||||
const SPREAD_SAMPLES: u32 = 32;
|
||||
|
||||
/// `a − 2b + c`, the curve's second difference over three control points.
|
||||
fn second_difference(a: (f64, f64), b: (f64, f64), c: (f64, f64)) -> (f64, f64) {
|
||||
(a.0 - 2.0 * b.0 + c.0, a.1 - 2.0 * b.1 + c.1)
|
||||
@@ -813,8 +865,8 @@ mod tests {
|
||||
}
|
||||
|
||||
/// Every point of the curve lies within the declared deviation of its chords, and the
|
||||
/// deviation within the tolerance asked unless the curve took `MAX_CURVE_CHORDS` (an arc
|
||||
/// whose radii the endpoints scale up).
|
||||
/// deviation within the tolerance asked (an arc's `f32` spread aside, well under 1e-3 here)
|
||||
/// unless the curve took `MAX_CURVE_CHORDS` (an arc whose radii the endpoints scale up).
|
||||
#[test]
|
||||
fn outline_chords_stay_within_the_deviation() {
|
||||
let mut rng = Lcg(13);
|
||||
@@ -876,7 +928,7 @@ mod tests {
|
||||
let line = &outline.subpaths[0];
|
||||
let capped = line.len() == MAX_CURVE_CHORDS + 1;
|
||||
assert!(
|
||||
outline.deviation <= tolerance || capped,
|
||||
outline.deviation <= tolerance + 1e-3 || capped,
|
||||
"case {case} {d}: {}",
|
||||
outline.deviation
|
||||
);
|
||||
@@ -936,6 +988,40 @@ mod tests {
|
||||
assert!(small.deviation <= 0.05 + 1e-6, "{}", small.deviation);
|
||||
}
|
||||
|
||||
/// Endpoints a browser stores as one `f32` point draw no arc, as in the browser.
|
||||
#[test]
|
||||
fn an_arc_between_endpoints_equal_in_f32_draws_nothing() {
|
||||
let outline = path_outline("M40 50A20 20 0 1 1 40.000001 50Z", 0.05, 4096).unwrap();
|
||||
assert!(outline.subpaths.iter().all(|s| s.len() <= 2), "{outline:?}");
|
||||
let bounds = path_bounds("M40 50A20 20 0 1 1 40.000001 50Z").unwrap();
|
||||
assert!(bounds.max_y - bounds.min_y < 1e-3, "{bounds:?}");
|
||||
assert!(path_bounds("M40 50A20 20 0 1 1 40.01 50Z").unwrap().min_y < 20.0);
|
||||
}
|
||||
|
||||
/// A flat ellipse along its chord moves far for one `f32` ulp of its endpoint (the browser
|
||||
/// rounds it there): the spread joins the deviation and the variants' extremes the bounds.
|
||||
/// A semicircle keeps a spread small against its size.
|
||||
#[test]
|
||||
fn an_ill_conditioned_arc_carries_its_f32_spread() {
|
||||
let d = "M40.000003 72.000003A35 0.00000095367431640625 45 0 1 90 122L90 150L40 150Z";
|
||||
let outline = path_outline(d, 0.05, 4096).unwrap();
|
||||
assert!(outline.deviation > 0.5, "{}", outline.deviation);
|
||||
assert!(
|
||||
path_bounds(d).unwrap().max_x > 100.0,
|
||||
"the needle is inside the bounds"
|
||||
);
|
||||
let large = path_outline(
|
||||
"M0 1000a1000 1000 0 1 0 2000 0a1000 1000 0 1 0 -2000 0Z",
|
||||
0.05,
|
||||
4096,
|
||||
)
|
||||
.unwrap();
|
||||
// Two semicircles of radius 1 000: the endpoints sit on a diameter, where the centre
|
||||
// moves as the square root of an endpoint's ulp (0.84 units, 0.12 px on the equine
|
||||
// scale): under a thousandth of the radius.
|
||||
assert!(large.deviation < 1.0, "{}", large.deviation);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_outline_stops_at_its_vertex_cap() {
|
||||
let d = format!("M0 0{}", "l1 1".repeat(9));
|
||||
@@ -951,7 +1037,12 @@ mod tests {
|
||||
assert_eq!(read("M1,2,3,4"), Some((1.0, 2.0, 3.0, 4.0)));
|
||||
assert_eq!(read("M.5.5L1-1"), Some((0.5, -1.0, 1.0, 0.5)));
|
||||
assert_eq!(read("M0 0h5v-2H1V9z"), Some((0.0, -2.0, 5.0, 9.0)));
|
||||
assert_eq!(read("M0 0a1 1 0 1010 0"), Some((0.0, 0.0, 10.0, 5.0)));
|
||||
// An arc's bounds hold its `f32` variants too: a few ulps around the exact half circle.
|
||||
let arc = read("M0 0a1 1 0 1010 0").unwrap();
|
||||
let exact = [0.0, 0.0, 10.0, 5.0];
|
||||
for (got, want) in [arc.0, arc.1, arc.2, arc.3].iter().zip(exact) {
|
||||
assert!((got - want).abs() < 1e-5, "{arc:?}");
|
||||
}
|
||||
assert_eq!(read("M0 0 1e1 1E-1"), Some((0.0, 0.0, 10.0, 0.1)));
|
||||
assert_eq!(read(" M0 0 Z m1 1 l1 1 "), Some((0.0, 0.0, 2.0, 2.0)));
|
||||
for broken in [
|
||||
|
||||
Reference in new issue
Block a user