EPS: arc and arcn with huge angles no longer hang the import
arc added 2π to its sweep in a loop until the sweep was no longer negative, and arcn subtracted 2π the same way. For an angle of 10^18 degrees the loop took about 3e15 passes, and from 2^56 radians (about 4.1e18 degrees) up, adding 2π left the sweep unchanged and the loop never ended. A sweep that needed no loop, as in 0 10^18 arc, was passed unchanged to kurbo, which divides it into more than 1e16 curves (about four per turn at a radius of 10), and arc appended each curve to the path. Importing an EPS file probes it with effectcraft_pdf::parse, which has no time limit, so importing such a file did not finish. As in storytold/vectorcraft#1062, every arc now starts at the first angle's remainder after whole turns, and when the second angle is below the first (arc) or above it (arcn), the sweep is worked out from the two angles' remainders. A sweep of up to 1,024 turns is drawn whole. For a longer one, the import records a skip note ("arc (too many turns)") and leaves out whole pairs of turns, so that between 1,022 and 1,024 turns remain. The arc still ends at the second angle, and an even-odd fill covers the same area as the full sweep. Arcs of up to 1,024 turns whose angles are within about 10^6 degrees are unchanged apart from rounding, except some whose angles are a whole number of turns apart or within rounding of that. For larger angles, the old conversion to radians lost precision: at 10^15 degrees, a quarter-turn arc swept 1.5723 radians instead of 1.5708. When raising (arc) or lowering (arcn) the second angle by whole turns makes it equal to the first, as in 7 -353 arc or 2 362 arcn, rounding sometimes made the old loop add a turn and draw a full circle. Those arcs are now empty, as the PostScript Language Reference defines them. When the raised or lowered angle falls just short of the first instead, the reference defines a full circle, and each version leaves some of these arcs empty: the old code drew nothing for 125.00000000000001 125 arc, which the new code draws as a full circle, and the new code draws nothing for 1.0000000000000002 -719 arc, which the old code drew as a full circle. arcs_with_huge_angles_end_at_their_second_angle parses each program on a thread of its own and fails when no result arrives within 20 seconds.
This commit is contained in:
+33
-11
@@ -343,6 +343,15 @@ fn matrix_val(m: Affine) -> V {
|
||||
array(m.as_coeffs().iter().map(|c| V::Num(*c)).collect())
|
||||
}
|
||||
|
||||
/// The angle from `a` counterclockwise to `b` (degrees, 0 to 360), worked out from what is left
|
||||
/// of each after whole turns.
|
||||
fn ccw_turn(a: f64, b: f64) -> f64 {
|
||||
(b % 360.0 - a % 360.0).rem_euclid(360.0)
|
||||
}
|
||||
|
||||
/// The most turns one `arc` or `arcn` draws.
|
||||
const MAX_ARC_TURNS: f64 = 1024.0;
|
||||
|
||||
/// The deepest procedure nesting run (deeper recursion stops the program).
|
||||
const MAX_CALL_DEPTH: usize = 256;
|
||||
|
||||
@@ -502,20 +511,33 @@ impl Interp {
|
||||
}
|
||||
|
||||
fn arc(&mut self, neg: bool) {
|
||||
let a2 = self.popn().to_radians();
|
||||
let a1 = self.popn().to_radians();
|
||||
let a2 = self.popn();
|
||||
let a1 = self.popn();
|
||||
let r = self.popn();
|
||||
let c = self.user_point();
|
||||
let mut sweep = a2 - a1;
|
||||
if neg {
|
||||
while sweep > 0.0 {
|
||||
sweep -= std::f64::consts::TAU;
|
||||
}
|
||||
// `arc` raises `a2` by multiples of 360 until it is at least `a1`, and `arcn` lowers it
|
||||
// until it is at most `a1` (PostScript Language Reference §8.2, `arc` and `arcn`). Those
|
||||
// sweeps, and the first endpoint, are worked out from what is left of each angle after
|
||||
// whole turns.
|
||||
let sweep = if !neg && a2 < a1 {
|
||||
ccw_turn(a1, a2)
|
||||
} else if neg && a2 > a1 {
|
||||
-ccw_turn(a2, a1)
|
||||
} else {
|
||||
while sweep < 0.0 {
|
||||
sweep += std::f64::consts::TAU;
|
||||
}
|
||||
}
|
||||
a2 - a1
|
||||
};
|
||||
// A sweep of more than `MAX_ARC_TURNS` turns is shortened by whole pairs of turns to
|
||||
// between `MAX_ARC_TURNS - 2` and `MAX_ARC_TURNS` turns, with a skip note. The arc still
|
||||
// ends at `a2`, and an even-odd fill or clip of it covers the same area as the full sweep.
|
||||
let max = MAX_ARC_TURNS * 360.0;
|
||||
let sweep = if sweep.abs() > max {
|
||||
self.b.skip("arc (too many turns)");
|
||||
let s = sweep.signum();
|
||||
s * (max - 720.0 + (s * (a2 % 720.0 - a1 % 720.0)).rem_euclid(720.0))
|
||||
} else {
|
||||
sweep
|
||||
};
|
||||
let (a1, sweep) = ((a1 % 360.0).to_radians(), sweep.to_radians());
|
||||
let start = Point::new(c.x + r * a1.cos(), c.y + r * a1.sin());
|
||||
if self.gs.cur.is_some() {
|
||||
self.line_to(start);
|
||||
|
||||
Reference in New Issue
Block a user