Debian Codegraph @

third_party/rust/wpf-gpu-raster/src/bezier.rs

raw
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 //+-----------------------------------------------------------------------------
6 //
7 // class Bezier32
8 //
9 // Bezier cracker.
10 //
11 // A hybrid cubic Bezier curve flattener based on KirkO's error factor.
12 // Generates line segments fast without using the stack. Used to flatten a
13 // path.
14 //
15 // For an understanding of the methods used, see:
16 //
17 // Kirk Olynyk, "..."
18 // Goossen and Olynyk, "System and Method of Hybrid Forward
19 // Differencing to Render Bezier Splines"
20 // Lien, Shantz and Vaughan Pratt, "Adaptive Forward Differencing for
21 // Rendering Curves and Surfaces", Computer Graphics, July 1987
22 // Chang and Shantz, "Rendering Trimmed NURBS with Adaptive Forward
23 // Differencing", Computer Graphics, August 1988
24 // Foley and Van Dam, "Fundamentals of Interactive Computer Graphics"
25 //
26 // Public Interface:
27 // bInit(pptfx) - pptfx points to 4 control points of
28 // Bezier. Current point is set to the first
29 // point after the start-point.
30 // Bezier32(pptfx) - Constructor with initialization.
31 // vGetCurrent(pptfx) - Returns current polyline point.
32 // bCurrentIsEndPoint() - TRUE if current point is end-point.
33 // vNext() - Moves to next polyline point.
34 //
35
36
37 #![allow(unused_parens)]
38 #![allow(non_upper_case_globals)]
39 //+-----------------------------------------------------------------------------
40 //
41
42 //
43 // $TAG ENGR
44
45 // $Module: win_mil_graphics_geometry
46 // $Keywords:
47 //
48 // $Description:
49 // Class for flattening a bezier.
50 //
51 // $ENDTAG
52 //
53 //------------------------------------------------------------------------------
54
55 // First conversion from original 28.4 to 18.14 format
56 const HFD32_INITIAL_SHIFT: i32 = 10;
57
58 // Second conversion to 15.17 format
59 const HFD32_ADDITIONAL_SHIFT: i32 = 3;
60
61
62 // BEZIER_FLATTEN_GDI_COMPATIBLE:
63 //
64 // Don't turn on this switch without testing carefully. It's more for
65 // documentation's sake - to show the values that GDI used - for an error
66 // tolerance of 2/3.
67
68 // It turns out that 2/3 produces very noticable artifacts on antialiased lines,
69 // so we want to use 1/4 instead.
70 /*
71 #ifdef BEZIER_FLATTEN_GDI_COMPATIBLE
72
73 // Flatten to an error of 2/3. During initial phase, use 18.14 format.
74
75 #define TEST_MAGNITUDE_INITIAL (6 * 0x00002aa0L)
76
77 // Error of 2/3. During normal phase, use 15.17 format.
78
79 #define TEST_MAGNITUDE_NORMAL (TEST_MAGNITUDE_INITIAL << 3)
80
81 #else
82 */
83 use crate::types::*;
84 /*
85 // Flatten to an error of 1/4. During initial phase, use 18.14 format.
86
87 const TEST_MAGNITUDE_INITIAL: i32 = (6 * 0x00001000);
88
89 // Error of 1/4. During normal phase, use 15.17 format.
90
91 const TEST_MAGNITUDE_NORMAL: i32 = (TEST_MAGNITUDE_INITIAL << 3);
92 */
93
94 // I have modified the constants for HFD32 as part of fixing accuracy errors
95 // (Bug 816015). Something similar could be done for the 64 bit hfd, but it ain't
96 // broke so I'd rather not fix it.
97
98 // The shift to the steady state 15.17 format
99 const HFD32_SHIFT: LONG = HFD32_INITIAL_SHIFT + HFD32_ADDITIONAL_SHIFT;
100
101 // Added to output numbers before rounding back to original representation
102 const HFD32_ROUND: LONG = 1 << (HFD32_SHIFT - 1);
103
104 // The error is tested on max(|e2|, |e3|), which represent 6 times the actual error.
105 // The flattening tolerance is hard coded to 1/4 in the original geometry space,
106 // which translates to 4 in 28.4 format. So 6 times that is:
107
108 const HFD32_TOLERANCE: LONGLONG = 24;
109
110 // During the initial phase, while working in 18.14 format
111 const HFD32_INITIAL_TEST_MAGNITUDE: LONGLONG = HFD32_TOLERANCE << HFD32_INITIAL_SHIFT;
112
113 // During the steady state, while working in 15.17 format
114 const HFD32_TEST_MAGNITUDE: LONGLONG = HFD32_INITIAL_TEST_MAGNITUDE << HFD32_ADDITIONAL_SHIFT;
115
116 // We will stop halving the segment with basis e1, e2, e3, e4 when max(|e2|, |e3|)
117 // is less than HFD32_TOLERANCE. The operation e2 = (e2 + e3) >> 3 in vHalveStepSize() may
118 // eat up 3 bits of accuracy. HfdBasis32 starts off with a pad of HFD32_SHIFT zeros, so
119 // we can stay exact up to HFD32_SHIFT/3 subdivisions. Since every subdivision is guaranteed
120 // to shift max(|e2|, |e3|) at least by 2, we will subdivide no more than n times if the
121 // initial max(|e2|, |e3|) is less than than HFD32_TOLERANCE << 2n. But if the initial
122 // max(|e2|, |e3|) is greater than HFD32_TOLERANCE >> (HFD32_SHIFT / 3) then we may not be
123 // able to flatten with the 32 bit hfd, so we need to resort to the 64 bit hfd.
124
125 const HFD32_MAX_ERROR: INT = (HFD32_TOLERANCE as i32) << ((2 * HFD32_INITIAL_SHIFT) / 3);
126
127 // The maximum size of coefficients that can be handled by HfdBasis32.
128 const HFD32_MAX_SIZE: LONGLONG = 0xffffc000;
129
130 // Michka 9/12/03: I found this number in the the body of the code witout any explanation.
131 // My analysis suggests that we could get away with larger numbers, but if I'm wrong we
132 // could be in big trouble, so let us stay conservative.
133 //
134 // In bInit() we subtract Min(Bezier coeffients) from the original coefficients, so after
135 // that 0 <= coefficients <= Bound, and the test will be Bound < HFD32_MAX_SIZE. When
136 // switching to the HFD basis in bInit():
137 // * e0 is the first Bezier coeffient, so abs(e0) <= Bound.
138 // * e1 is a difference of non-negative coefficients so abs(e1) <= Bound.
139 // * e2 and e3 can be written as 12*(p - (q + r)/2) where p,q and r are coefficients.
140 // 0 <=(q + r)/2 <= Bound, so abs(p - (q + r)/2) <= 2*Bound, hence
141 // abs(e2), abs(e3) <= 12*Bound.
142 //
143 // During vLazyHalveStepSize we add e2 + e3, resulting in absolute value <= 24*Bound.
144 // Initially HfdBasis32 shifts the numbers by HFD32_INITIAL_SHIFT, so we need to handle
145 // 24*bounds*(2^HFD32_SHIFT), and that needs to be less than 2^31. So the bounds need to
146 // be less than 2^(31-HFD32_INITIAL_SHIFT)/24).
147 //
148 // For speed, the algorithm uses & rather than < for comparison. To facilitate that we
149 // replace 24 by 32=2^5, and then the binary representation of the number is of the form
150 // 0...010...0 with HFD32_SHIFT+5 trailing zeros. By subtracting that from 2^32 = 0xffffffff+1
151 // we get a number that is 1..110...0 with the same number of trailing zeros, and that can be
152 // used with an & for comparison. So the number should be:
153 //
154 // 0xffffffffL - (1L << (31 - HFD32_INITIAL_SHIFT - 5)) + 1 = (1L << 16) + 1 = 0xffff0000
155 //
156 // For the current values of HFD32_INITIAL_SHIFT=10 and HFD32_ADDITIONAL_SHIFT=3, the steady
157 // state doesn't pose additional requirements, as shown below.
158 //
159 // For some reason the current code uses 0xfffc0000 = (1L << 14) + 1.
160 //
161 // Here is why the steady state doesn't pose additional requirements:
162 //
163 // In vSteadyState we multiply e0 and e1 by 8, so the requirement is Bounds*2^13 < 2^31,
164 // or Bounds < 2^18, less stringent than the above.
165 //
166 // In vLazyHalveStepSize we cut the error down by subdivision, making abs(e2) and abs(e3)
167 // less than HFD32_TEST_MAGNITUDE = 24*2^13, well below 2^31.
168 //
169 // During all the steady-state operations - vTakeStep, vHalveStepSize and vDoubleStepSize,
170 // e0 is on the curve and e1 is a difference of 2 points on the curve, so
171 // abs(e0), abs(e1) < Bounds * 2^13, which requires Bound < 2^(31-13) = 2^18. e2 and e3
172 // are errors, kept below 6*HFD32_TEST_MAGNITUDE = 216*2^13. Details:
173 //
174 // In vTakeStep e2 = 2e2 - e3 keeps abs(e2) < 3*HFD32_TEST_MAGNITUDE = 72*2^13,
175 // well below 2^31
176 //
177 // In vHalveStepSize we add e2 + e3 when their absolute is < 3*HFD32_TEST_MAGNITUDE (because
178 // this comes after a step), so that keeps the result below 6*HFD32_TEST_MAGNITUDE = 216*2^13.
179 //
180 // In vDoubleStepSize we know that abs(e2), abs(e3) < HFD32_TEST_MAGNITUDE/4, otherwise we
181 // would not have doubled the step.
182
183 #[derive(Default)]
184 struct HfdBasis32
185 {
186 e0: LONG,
187 e1: LONG,
188 e2: LONG,
189 e3: LONG,
190 }
191
192 impl HfdBasis32 {
193 fn lParentErrorDividedBy4(&self) -> LONG {
194 self.e3.abs().max((self.e2 + self.e2 - self.e3).abs())
195 }
196
197 fn lError(&self) -> LONG
198 {
199 self.e2.abs().max(self.e3.abs())
200 }
201
202 fn fxValue(&self) -> INT
203 {
204 return((self.e0 + HFD32_ROUND) >> HFD32_SHIFT);
205 }
206
207 fn bInit(&mut self, p1: INT, p2: INT, p3: INT, p4: INT) -> bool
208 {
209 // Change basis and convert from 28.4 to 18.14 format:
210
211 self.e0 = (p1 ) << HFD32_INITIAL_SHIFT;
212 self.e1 = (p4 - p1 ) << HFD32_INITIAL_SHIFT;
213
214 self.e2 = 6 * (p2 - p3 - p3 + p4);
215 self.e3 = 6 * (p1 - p2 - p2 + p3);
216
217 if (self.lError() >= HFD32_MAX_ERROR)
218 {
219 // Large error, will require too many subdivision for this 32 bit hfd
220 return false;
221 }
222
223 self.e2 <<= HFD32_INITIAL_SHIFT;
224 self.e3 <<= HFD32_INITIAL_SHIFT;
225
226 return true;
227 }
228
229 fn vLazyHalveStepSize(&mut self, cShift: LONG)
230 {
231 self.e2 = self.ExactShiftRight(self.e2 + self.e3, 1);
232 self.e1 = self.ExactShiftRight(self.e1 - self.ExactShiftRight(self.e2, cShift), 1);
233 }
234
235 fn vSteadyState(&mut self, cShift: LONG)
236 {
237 // We now convert from 18.14 fixed format to 15.17:
238
239 self.e0 <<= HFD32_ADDITIONAL_SHIFT;
240 self.e1 <<= HFD32_ADDITIONAL_SHIFT;
241
242 let mut lShift = cShift - HFD32_ADDITIONAL_SHIFT;
243
244 if (lShift < 0)
245 {
246 lShift = -lShift;
247 self.e2 <<= lShift;
248 self.e3 <<= lShift;
249 }
250 else
251 {
252 self.e2 >>= lShift;
253 self.e3 >>= lShift;
254 }
255 }
256
257 fn vHalveStepSize(&mut self)
258 {
259 self.e2 = self.ExactShiftRight(self.e2 + self.e3, 3);
260 self.e1 = self.ExactShiftRight(self.e1 - self.e2, 1);
261 self.e3 = self.ExactShiftRight(self.e3, 2);
262 }
263
264 fn vDoubleStepSize(&mut self)
265 {
266 self.e1 += self.e1 + self.e2;
267 self.e3 <<= 2;
268 self.e2 = (self.e2 << 3) - self.e3;
269 }
270
271 fn vTakeStep(&mut self)
272 {
273 self.e0 += self.e1;
274 let lTemp = self.e2;
275 self.e1 += lTemp;
276 self.e2 += lTemp - self.e3;
277 self.e3 = lTemp;
278 }
279
280 fn ExactShiftRight(&self, num: i32, shift: i32) -> i32
281 {
282 // Performs a shift to the right while asserting that we're not
283 // losing significant bits
284
285 assert!(num == (num >> shift) << shift);
286 return num >> shift;
287 }
288 }
289
290 fn vBoundBox(
291 aptfx: &[POINT; 4]) -> RECT
292 {
293 let mut left = aptfx[0].x;
294 let mut right = aptfx[0].x;
295 let mut top = aptfx[0].y;
296 let mut bottom = aptfx[0].y;
297
298 for i in 1..4
299 {
300 left = left.min(aptfx[i].x);
301 top = top.min(aptfx[i].y);
302 right = right.max(aptfx[i].x);
303 bottom = bottom.max(aptfx[i].y);
304 }
305
306 // We make the bounds one pixel loose for the nominal width
307 // stroke case, which increases the bounds by half a pixel
308 // in every dimension:
309
310 RECT { left: left - 16, top: top - 16, right: right + 16, bottom: bottom + 16}
311 }
312
313
314
315 fn bIntersect(
316 a: &RECT,
317 b: &RECT) -> bool
318 {
319 return((a.left < b.right) &&
320 (a.top < b.bottom) &&
321 (a.right > b.left) &&
322 (a.bottom > b.top));
323 }
324
325 #[derive(Default)]
326 pub struct Bezier32
327 {
328 cSteps: LONG,
329 x: HfdBasis32,
330 y: HfdBasis32,
331 rcfxBound: RECT
332 }
333 impl Bezier32 {
334
335 fn bInit(&mut self,
336 aptfxBez: &[POINT; 4],
337 // Pointer to 4 control points
338 prcfxClip: Option<&RECT>) -> bool
339 // Bound box of visible region (optional)
340 {
341 let mut aptfx;
342 let mut cShift = 0; // Keeps track of 'lazy' shifts
343
344 self.cSteps = 1; // Number of steps to do before reach end of curve
345
346 self.rcfxBound = vBoundBox(aptfxBez);
347
348 aptfx = aptfxBez.clone();
349
350 {
351 let mut fxOr;
352 let mut fxOffset;
353
354 // find out if the coordinates minus the bounding box
355 // exceed 10 bits
356 fxOffset = self.rcfxBound.left;
357 fxOr = {aptfx[0].x -= fxOffset; aptfx[0].x};
358 fxOr |= {aptfx[1].x -= fxOffset; aptfx[1].x};
359 fxOr |= {aptfx[2].x -= fxOffset; aptfx[2].x};
360 fxOr |= {aptfx[3].x -= fxOffset; aptfx[3].x};
361
362 fxOffset = self.rcfxBound.top;
363 fxOr |= {aptfx[0].y -= fxOffset; aptfx[0].y};
364 fxOr |= {aptfx[1].y -= fxOffset; aptfx[1].y};
365 fxOr |= {aptfx[2].y -= fxOffset; aptfx[2].y};
366 fxOr |= {aptfx[3].y -= fxOffset; aptfx[3].y};
367
368 // This 32 bit cracker can only handle points in a 10 bit space:
369
370 if ((fxOr as i64 & HFD32_MAX_SIZE) != 0) {
371 return false;
372 }
373 }
374
375 if (!self.x.bInit(aptfx[0].x, aptfx[1].x, aptfx[2].x, aptfx[3].x))
376 {
377 return false;
378 }
379 if (!self.y.bInit(aptfx[0].y, aptfx[1].y, aptfx[2].y, aptfx[3].y))
380 {
381 return false;
382 }
383
384
385 if (match prcfxClip { None => true, Some(clip) => bIntersect(&self.rcfxBound, clip)})
386 {
387
388 loop {
389 let lTestMagnitude = (HFD32_INITIAL_TEST_MAGNITUDE << cShift) as LONG;
390
391 if (self.x.lError() <= lTestMagnitude && self.y.lError() <= lTestMagnitude) {
392 break;
393 }
394
395 cShift += 2;
396 self.x.vLazyHalveStepSize(cShift);
397 self.y.vLazyHalveStepSize(cShift);
398 self.cSteps <<= 1;
399 }
400 }
401
402 self.x.vSteadyState(cShift);
403 self.y.vSteadyState(cShift);
404
405 // Note that this handles the case where the initial error for
406 // the Bezier is already less than HFD32_TEST_MAGNITUDE:
407
408 self.x.vTakeStep();
409 self.y.vTakeStep();
410 self.cSteps-=1;
411
412 return true;
413 }
414
415
416 fn cFlatten(&mut self,
417 mut pptfx: &mut [POINT],
418 pbMore: &mut bool) -> i32
419 {
420 let mut cptfx = pptfx.len();
421 assert!(cptfx > 0);
422
423 let cptfxOriginal = cptfx;
424
425 while {
426 // Return current point:
427
428 pptfx[0].x = self.x.fxValue() + self.rcfxBound.left;
429 pptfx[0].y = self.y.fxValue() + self.rcfxBound.top;
430 pptfx = &mut pptfx[1..];
431
432 // If cSteps == 0, that was the end point in the curve!
433
434 if (self.cSteps == 0)
435 {
436 *pbMore = false;
437
438 // '+1' because we haven't decremented 'cptfx' yet:
439
440 return(cptfxOriginal - cptfx + 1) as i32;
441 }
442
443 // Okay, we have to step:
444
445 if (self.x.lError().max(self.y.lError()) > HFD32_TEST_MAGNITUDE as LONG)
446 {
447 self.x.vHalveStepSize();
448 self.y.vHalveStepSize();
449 self.cSteps <<= 1;
450 }
451
452 // We are here after vTakeStep. Before that the error max(|e2|,|e3|) was less
453 // than HFD32_TEST_MAGNITUDE. vTakeStep changed e2 to 2e2-e3. Since
454 // |2e2-e3| < max(|e2|,|e3|) << 2 and vHalveStepSize is guaranteed to reduce
455 // max(|e2|,|e3|) by >> 2, no more than one subdivision should be required to
456 // bring the new max(|e2|,|e3|) back to within HFD32_TEST_MAGNITUDE, so:
457 assert!(self.x.lError().max(self.y.lError()) <= HFD32_TEST_MAGNITUDE as LONG);
458
459 while (!(self.cSteps & 1 != 0) &&
460 self.x.lParentErrorDividedBy4() <= (HFD32_TEST_MAGNITUDE as LONG >> 2) &&
461 self.y.lParentErrorDividedBy4() <= (HFD32_TEST_MAGNITUDE as LONG >> 2))
462 {
463 self.x.vDoubleStepSize();
464 self.y.vDoubleStepSize();
465 self.cSteps >>= 1;
466 }
467
468 self.cSteps -=1 ;
469 self.x.vTakeStep();
470 self.y.vTakeStep();
471 cptfx -= 1;
472 cptfx != 0
473 } {}
474
475 *pbMore = true;
476 return cptfxOriginal as i32;
477 }
478 }
479
480
481 ///////////////////////////////////////////////////////////////////////////
482 // Bezier64
483 //
484 // All math is done using 64 bit fixed numbers in a 36.28 format.
485 //
486 // All drawing is done in a 31 bit space, then a 31 bit window offset
487 // is applied. In the initial transform where we change to the HFD
488 // basis, e2 and e3 require the most bits precision: e2 = 6(p2 - 2p3 + p4).
489 // This requires an additional 4 bits precision -- hence we require 36 bits
490 // for the integer part, and the remaining 28 bits is given to the fraction.
491 //
492 // In rendering a Bezier, every 'subdivide' requires an extra 3 bits of
493 // fractional precision. In order to be reversible, we can allow no
494 // error to creep in. Since a INT coordinate is 32 bits, and we
495 // require an additional 4 bits as mentioned above, that leaves us
496 // 28 bits fractional precision -- meaning we can do a maximum of
497 // 9 subdivides. Now, the maximum absolute error of a Bezier curve in 27
498 // bit integer space is 2^29 - 1. But 9 subdivides reduces the error by a
499 // guaranteed factor of 2^18, meaning we can subdivide down only to an error
500 // of 2^11 before we overflow, when in fact we want to reduce error to less
501 // than 1.
502 //
503 // So what we do is HFD until we hit an error less than 2^11, reverse our
504 // basis transform to get the four control points of this smaller curve
505 // (rounding in the process to 32 bits), then invoke another copy of HFD
506 // on the reduced Bezier curve. We again have enough precision, but since
507 // its starting error is less than 2^11, we can reduce error to 2^-7 before
508 // overflowing! We'll start a low HFD after every step of the high HFD.
509 ////////////////////////////////////////////////////////////////////////////
510 #[derive(Default)]
511 struct HfdBasis64
512 {
513 e0: LONGLONG,
514 e1: LONGLONG,
515 e2: LONGLONG,
516 e3: LONGLONG,
517 }
518
519 impl HfdBasis64 {
520 fn vParentError(&self) -> LONGLONG
521 {
522 (self.e3 << 2).abs().max(((self.e2 << 3) - (self.e3 << 2)).abs())
523 }
524
525 fn vError(&self) -> LONGLONG
526 {
527 self.e2.abs().max(self.e3.abs())
528 }
529
530 fn fxValue(&self) -> INT
531 {
532 // Convert from 36.28 and round:
533
534 let mut eq = self.e0;
535 eq += (1 << (BEZIER64_FRACTION - 1));
536 eq >>= BEZIER64_FRACTION;
537 return eq as LONG as INT;
538 }
539
540 fn vInit(&mut self, p1: INT, p2: INT, p3: INT, p4: INT)
541 {
542 let mut eqTmp;
543 let eqP2 = p2 as LONGLONG;
544 let eqP3 = p3 as LONGLONG;
545
546 // e0 = p1
547 // e1 = p4 - p1
548 // e2 = 6(p2 - 2p3 + p4)
549 // e3 = 6(p1 - 2p2 + p3)
550
551 // Change basis:
552
553 self.e0 = p1 as LONGLONG; // e0 = p1
554 self.e1 = p4 as LONGLONG;
555 self.e2 = eqP2; self.e2 -= eqP3; self.e2 -= eqP3; self.e2 += self.e1; // e2 = p2 - 2*p3 + p4
556 self.e3 = self.e0; self.e3 -= eqP2; self.e3 -= eqP2; self.e3 += eqP3; // e3 = p1 - 2*p2 + p3
557 self.e1 -= self.e0; // e1 = p4 - p1
558
559 // Convert to 36.28 format and multiply e2 and e3 by six:
560
561 self.e0 <<= BEZIER64_FRACTION;
562 self.e1 <<= BEZIER64_FRACTION;
563 eqTmp = self.e2; self.e2 += eqTmp; self.e2 += eqTmp; self.e2 <<= (BEZIER64_FRACTION + 1);
564 eqTmp = self.e3; self.e3 += eqTmp; self.e3 += eqTmp; self.e3 <<= (BEZIER64_FRACTION + 1);
565 }
566
567 fn vUntransform<F: Fn(&mut POINT) -> &mut LONG>(&self,
568 afx: &mut [POINT; 4], field: F)
569 {
570 // Declare some temps to hold our operations, since we can't modify e0..e3.
571
572 let mut eqP0;
573 let mut eqP1;
574 let mut eqP2;
575 let mut eqP3;
576
577 // p0 = e0
578 // p1 = e0 + (6e1 - e2 - 2e3)/18
579 // p2 = e0 + (12e1 - 2e2 - e3)/18
580 // p3 = e0 + e1
581
582 eqP0 = self.e0;
583
584 // NOTE PERF: Convert this to a multiply by 6: [andrewgo]
585
586 eqP2 = self.e1;
587 eqP2 += self.e1;
588 eqP2 += self.e1;
589 eqP1 = eqP2;
590 eqP1 += eqP2; // 6e1
591 eqP1 -= self.e2; // 6e1 - e2
592 eqP2 = eqP1;
593 eqP2 += eqP1; // 12e1 - 2e2
594 eqP2 -= self.e3; // 12e1 - 2e2 - e3
595 eqP1 -= self.e3;
596 eqP1 -= self.e3; // 6e1 - e2 - 2e3
597
598 // NOTE: May just want to approximate these divides! [andrewgo]
599 // Or can do a 64 bit divide by 32 bit to get 32 bits right here.
600
601 eqP1 /= 18;
602 eqP2 /= 18;
603 eqP1 += self.e0;
604 eqP2 += self.e0;
605
606 eqP3 = self.e0;
607 eqP3 += self.e1;
608
609 // Convert from 36.28 format with rounding:
610
611 eqP0 += (1 << (BEZIER64_FRACTION - 1)); eqP0 >>= BEZIER64_FRACTION; *field(&mut afx[0]) = eqP0 as LONG;
612 eqP1 += (1 << (BEZIER64_FRACTION - 1)); eqP1 >>= BEZIER64_FRACTION; *field(&mut afx[1]) = eqP1 as LONG;
613 eqP2 += (1 << (BEZIER64_FRACTION - 1)); eqP2 >>= BEZIER64_FRACTION; *field(&mut afx[2]) = eqP2 as LONG;
614 eqP3 += (1 << (BEZIER64_FRACTION - 1)); eqP3 >>= BEZIER64_FRACTION; *field(&mut afx[3]) = eqP3 as LONG;
615 }
616
617 fn vHalveStepSize(&mut self)
618 {
619 // e2 = (e2 + e3) >> 3
620 // e1 = (e1 - e2) >> 1
621 // e3 >>= 2
622
623 self.e2 += self.e3; self.e2 >>= 3;
624 self.e1 -= self.e2; self.e1 >>= 1;
625 self.e3 >>= 2;
626 }
627
628 fn vDoubleStepSize(&mut self)
629 {
630 // e1 = 2e1 + e2
631 // e3 = 4e3;
632 // e2 = 8e2 - e3
633
634 self.e1 <<= 1; self.e1 += self.e2;
635 self.e3 <<= 2;
636 self.e2 <<= 3; self.e2 -= self.e3;
637 }
638
639 fn vTakeStep(&mut self)
640 {
641 self.e0 += self.e1;
642 let eqTmp = self.e2;
643 self.e1 += self.e2;
644 self.e2 += eqTmp; self.e2 -= self.e3;
645 self.e3 = eqTmp;
646 }
647 }
648
649 const BEZIER64_FRACTION: LONG = 28;
650
651 // The following is our 2^11 target error encoded as a 36.28 number
652 // (don't forget the additional 4 bits of fractional precision!) and
653 // the 6 times error multiplier:
654
655 const geqErrorHigh: LONGLONG = (6 * (1 << 15) >> (32 - BEZIER64_FRACTION)) << 32;
656
657 /*#ifdef BEZIER_FLATTEN_GDI_COMPATIBLE
658
659 // The following is the default 2/3 error encoded as a 36.28 number,
660 // multiplied by 6, and leaving 4 bits for fraction:
661
662 const LONGLONG geqErrorLow = (LONGLONG)(4) << 32;
663
664 #else*/
665
666 // The following is the default 1/4 error encoded as a 36.28 number,
667 // multiplied by 6, and leaving 4 bits for fraction:
668
669 use crate::types::POINT;
670
671 const geqErrorLow: LONGLONG = (3) << 31;
672
673 //#endif
674 #[derive(Default)]
675 pub struct Bezier64
676 {
677 xLow: HfdBasis64,
678 yLow: HfdBasis64,
679 xHigh: HfdBasis64,
680 yHigh: HfdBasis64,
681
682 eqErrorLow: LONGLONG,
683 rcfxClip: Option<RECT>,
684
685 cStepsHigh: LONG,
686 cStepsLow: LONG
687 }
688
689 impl Bezier64 {
690
691 fn vInit(&mut self,
692 aptfx: &[POINT; 4],
693 // Pointer to 4 control points
694 prcfxVis: Option<&RECT>,
695 // Pointer to bound box of visible area (may be NULL)
696 eqError: LONGLONG)
697 // Fractional maximum error (32.32 format)
698 {
699 self.cStepsHigh = 1;
700 self.cStepsLow = 0;
701
702 self.xHigh.vInit(aptfx[0].x, aptfx[1].x, aptfx[2].x, aptfx[3].x);
703 self.yHigh.vInit(aptfx[0].y, aptfx[1].y, aptfx[2].y, aptfx[3].y);
704
705 // Initialize error:
706
707 self.eqErrorLow = eqError;
708
709 self.rcfxClip = prcfxVis.cloned();
710
711 while (((self.xHigh.vError()) > geqErrorHigh) ||
712 ((self.yHigh.vError()) > geqErrorHigh))
713 {
714 self.cStepsHigh <<= 1;
715 self.xHigh.vHalveStepSize();
716 self.yHigh.vHalveStepSize();
717 }
718 }
719
720 fn cFlatten(
721 &mut self,
722 mut pptfx: &mut [POINT],
723 pbMore: &mut bool) -> INT
724 {
725 let mut aptfx: [POINT; 4] = Default::default();
726 let mut cptfx = pptfx.len();
727 let mut rcfxBound: RECT;
728 let cptfxOriginal = cptfx;
729
730 assert!(cptfx > 0);
731
732 while {
733 if (self.cStepsLow == 0)
734 {
735 // Optimization that if the bound box of the control points doesn't
736 // intersect with the bound box of the visible area, render entire
737 // curve as a single line:
738
739 self.xHigh.vUntransform(&mut aptfx, |p| &mut p.x);
740 self.yHigh.vUntransform(&mut aptfx, |p| &mut p.y);
741
742 self.xLow.vInit(aptfx[0].x, aptfx[1].x, aptfx[2].x, aptfx[3].x);
743 self.yLow.vInit(aptfx[0].y, aptfx[1].y, aptfx[2].y, aptfx[3].y);
744 self.cStepsLow = 1;
745
746 if (match &self.rcfxClip { None => true, Some(clip) => {rcfxBound = vBoundBox(&aptfx); bIntersect(&rcfxBound, &clip)}})
747 {
748 while (((self.xLow.vError()) > self.eqErrorLow) ||
749 ((self.yLow.vError()) > self.eqErrorLow))
750 {
751 self.cStepsLow <<= 1;
752 self.xLow.vHalveStepSize();
753 self.yLow.vHalveStepSize();
754 }
755 }
756
757 // This 'if' handles the case where the initial error for the Bezier
758 // is already less than the target error:
759
760 if ({self.cStepsHigh -= 1; self.cStepsHigh} != 0)
761 {
762 self.xHigh.vTakeStep();
763 self.yHigh.vTakeStep();
764
765 if (((self.xHigh.vError()) > geqErrorHigh) ||
766 ((self.yHigh.vError()) > geqErrorHigh))
767 {
768 self.cStepsHigh <<= 1;
769 self.xHigh.vHalveStepSize();
770 self.yHigh.vHalveStepSize();
771 }
772
773 while (!(self.cStepsHigh & 1 != 0) &&
774 ((self.xHigh.vParentError()) <= geqErrorHigh) &&
775 ((self.yHigh.vParentError()) <= geqErrorHigh))
776 {
777 self.xHigh.vDoubleStepSize();
778 self.yHigh.vDoubleStepSize();
779 self.cStepsHigh >>= 1;
780 }
781 }
782 }
783
784 self.xLow.vTakeStep();
785 self.yLow.vTakeStep();
786
787 pptfx[0].x = self.xLow.fxValue();
788 pptfx[0].y = self.yLow.fxValue();
789 pptfx = &mut pptfx[1..];
790
791 self.cStepsLow-=1;
792 if (self.cStepsLow == 0 && self.cStepsHigh == 0)
793 {
794 *pbMore = false;
795
796 // '+1' because we haven't decremented 'cptfx' yet:
797
798 return(cptfxOriginal - cptfx + 1) as INT;
799 }
800
801 if ((self.xLow.vError() > self.eqErrorLow) ||
802 (self.yLow.vError() > self.eqErrorLow))
803 {
804 self.cStepsLow <<= 1;
805 self.xLow.vHalveStepSize();
806 self.yLow.vHalveStepSize();
807 }
808
809 while (!(self.cStepsLow & 1 != 0) &&
810 ((self.xLow.vParentError()) <= self.eqErrorLow) &&
811 ((self.yLow.vParentError()) <= self.eqErrorLow))
812 {
813 self.xLow.vDoubleStepSize();
814 self.yLow.vDoubleStepSize();
815 self.cStepsLow >>= 1;
816 }
817 cptfx -= 1;
818 cptfx != 0
819 } {};
820
821 *pbMore = true;
822 return(cptfxOriginal) as INT;
823 }
824 }
825
826 //+-----------------------------------------------------------------------------
827 //
828 // class CMILBezier
829 //
830 // Bezier cracker. Flattens any Bezier in our 28.4 device space down to a
831 // smallest 'error' of 2^-7 = 0.0078. Will use fast 32 bit cracker for small
832 // curves and slower 64 bit cracker for big curves.
833 //
834 // Public Interface:
835 // vInit(aptfx, prcfxClip, peqError)
836 // - pptfx points to 4 control points of Bezier. The first point
837 // retrieved by bNext() is the the first point in the approximation
838 // after the start-point.
839 //
840 // - prcfxClip is an optional pointer to the bound box of the visible
841 // region. This is used to optimize clipping of Bezier curves that
842 // won't be seen. Note that this value should account for the pen's
843 // width!
844 //
845 // - optional maximum error in 32.32 format, corresponding to Kirko's
846 // error factor.
847 //
848 // bNext(pptfx)
849 // - pptfx points to where next point in approximation will be
850 // returned. Returns FALSE if the point is the end-point of the
851 // curve.
852 //
853 pub (crate) enum CMILBezier
854 {
855 Bezier64(Bezier64),
856 Bezier32(Bezier32)
857 }
858
859 impl CMILBezier {
860 // All coordinates must be in 28.4 format:
861 pub fn new(aptfxBez: &[POINT; 4], prcfxClip: Option<&RECT>) -> Self {
862 let mut bez32 = Bezier32::default();
863 let bBez32 = bez32.bInit(aptfxBez, prcfxClip);
864 if bBez32 {
865 CMILBezier::Bezier32(bez32)
866 } else {
867 let mut bez64 = Bezier64::default();
868 bez64.vInit(aptfxBez, prcfxClip, geqErrorLow);
869 CMILBezier::Bezier64(bez64)
870 }
871 }
872
873 // Returns the number of points filled in. This will never be zero.
874 //
875 // The last point returned may not be exactly the last control
876 // point. The workaround is for calling code to add an extra
877 // point if this is the case.
878 pub fn Flatten( &mut self,
879 pptfx: &mut [POINT],
880 pbMore: &mut bool) -> INT {
881 match self {
882 CMILBezier::Bezier32(bez) => bez.cFlatten(pptfx, pbMore),
883 CMILBezier::Bezier64(bez) => bez.cFlatten(pptfx, pbMore)
884 }
885 }
886 }
887
888 #[test]
889 fn flatten() {
890 let curve: [POINT; 4] = [
891 POINT{x: 1715, y: 6506},
892 POINT{x: 1692, y: 6506},
893 POINT{x: 1227, y: 5148},
894 POINT{x: 647, y: 5211}];
895 let mut bez = CMILBezier::new(&curve, None);
896 let mut result: [POINT; 32] = Default::default();
897 let mut more: bool = false;
898 let count = bez.Flatten(&mut result, &mut more);
899 assert_eq!(count, 21);
900 assert_eq!(more, false);
901 }
902
903 #[test]
904 fn split_flatten32() {
905 // make sure that flattening a curve into two small buffers matches
906 // doing it into a large buffer
907 let curve: [POINT; 4] = [
908 POINT{x: 1795, y: 8445},
909 POINT{x: 1795, y: 8445},
910 POINT{x: 1908, y: 8683},
911 POINT{x: 2043, y: 8705}];
912
913 let mut bez = CMILBezier::new(&curve, None);
914 let mut result: [POINT; 8] = Default::default();
915 let mut more: bool = false;
916 let count = bez.Flatten(&mut result[..5], &mut more);
917 assert_eq!(count, 5);
918 assert_eq!(more, true);
919 let count = bez.Flatten(&mut result[5..], &mut more);
920 assert_eq!(count, 3);
921 assert_eq!(more, false);
922
923 let mut bez = CMILBezier::new(&curve, None);
924 let mut full_result: [POINT; 8] = Default::default();
925 let mut more: bool = false;
926 let count = bez.Flatten(&mut full_result, &mut more);
927 assert_eq!(count, 8);
928 assert_eq!(more, false);
929 assert!(result == full_result);
930 }
931
932 #[test]
933 fn flatten32() {
934 let curve: [POINT; 4] = [
935 POINT{x: 100, y: 100},
936 POINT{x: 110, y: 100},
937 POINT{x: 110, y: 110},
938 POINT{x: 110, y: 100}];
939 let mut bez = CMILBezier::new(&curve, None);
940 let mut result: [POINT; 32] = Default::default();
941 let mut more: bool = false;
942 let count = bez.Flatten(&mut result, &mut more);
943 assert_eq!(count, 3);
944 assert_eq!(more, false);
945 }
946
947 #[test]
948 fn flatten32_double_step_size() {
949 let curve: [POINT; 4] = [
950 POINT{x: 1761, y: 8152},
951 POINT{x: 1761, y: 8152},
952 POINT{x: 1750, y: 8355},
953 POINT{x: 1795, y: 8445}];
954 let mut bez = CMILBezier::new(&curve, None);
955 let mut result: [POINT; 32] = Default::default();
956 let mut more: bool = false;
957 let count = bez.Flatten(&mut result, &mut more);
958 assert_eq!(count, 7);
959 assert_eq!(more, false);
960 }
961
962 #[test]
963 fn bezier64_init_high_num_steps() {
964 let curve: [POINT; 4] = [
965 POINT{x: 33, y: -1},
966 POINT{x: -1, y: -1},
967 POINT{x: -1, y: -16385},
968 POINT{x: -226, y: 10}];
969 let mut bez = CMILBezier::new(&curve, None);
970 let mut result: [POINT; 32] = Default::default();
971 let mut more: bool = false;
972 let count = bez.Flatten(&mut result, &mut more);
973 assert_eq!(count, 32);
974 assert_eq!(more, true);
975 }
976
977 #[test]
978 fn bezier64_high_error() {
979 let curve: [POINT; 4] = [
980 POINT{x: -1, y: -1},
981 POINT{x: -4097, y: -1},
982 POINT{x: 65471, y: -256},
983 POINT{x: -1, y: 0}];
984 let mut bez = CMILBezier::new(&curve, None);
985 let mut result: [POINT; 32] = Default::default();
986 let mut more: bool = false;
987 let count = bez.Flatten(&mut result, &mut more);
988 assert_eq!(count, 32);
989 assert_eq!(more, true);
990 }