caj2pdf-core 0.5.0

Platform-neutral CAJ-family conversion core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// SPDX-License-Identifier: MIT

//! Original calculations and caller-input checks; no document bytes or tables.

use super::*;
use crate::ErrorKind;

fn close(actual: f64, expected: f64) {
    assert!(
        (actual - expected).abs() <= 0.000_000_000_001,
        "{actual} differs from {expected}"
    );
}

fn page() -> EmpiricalPageGeometry {
    empirical_page_from_pixels(100, 250, [-3.25, 1.125]).unwrap()
}

#[test]
fn documented_factors_and_comparison_precision_are_explicit() {
    assert_eq!(EMPIRICAL_COORDINATE_POINTS_PER_UNIT, 240.0 / 2473.0);
    assert_eq!(EMPIRICAL_PLACEMENT_TOLERANCE_POINTS, 0.00005);
}

#[test]
fn image_dimensions_round_the_exact_point_ratio_once() {
    let page = empirical_page_from_pixels(2071, 153, [0.0; 2]).unwrap();
    // Predetermined decimal physical dimensions catch the extra rounding
    // from multiplying the binary approximation of 0.24. That older result
    // serializes as 497.03999999999996 and changes full-page edge pixels.
    assert_eq!(page.size.width_points, 497.04);
    assert_eq!(page.size.height_points, 36.72);
    assert_ne!(page.size.width_points, 2071.0 * 0.24);
    let transform = empirical_image_transform(page, 2071, 153, point(0, 0)).unwrap();
    assert_eq!(transform, [497.04, 0.0, 0.0, -36.72, 0.0, 36.72]);
    // The whole public unsigned dimension range keeps the numerator exact.
    let largest = empirical_page_from_pixels(u32::MAX, u32::MAX, [0.0; 2]).unwrap();
    assert_eq!(largest.size.width_points, 1_030_792_150.8);
    assert_eq!(largest.size.height_points, 1_030_792_150.8);
}

#[test]
fn fractional_negative_pdf_origin_and_off_page_translation_are_preserved() {
    let page = page();
    assert_eq!(
        page.size,
        PageSpec {
            width_points: 24.0,
            height_points: 60.0
        }
    );
    assert_eq!(page.media_box().unwrap(), [-3.25, 1.125, 20.75, 61.125]);
    let ctm = empirical_image_transform(page, 3, 7, point(2473, 2473)).unwrap();
    close(ctm[0], 0.72);
    assert_eq!(&ctm[1..3], &[0.0, 0.0]);
    close(ctm[3], -1.68);
    assert_eq!(&ctm[4..], &[236.75, -178.875]);
    // The image is outside both page axes. Its source origin is not clipped.
    assert!(ctm[4] > page.media_box().unwrap()[2]);
    assert!(ctm[5] < page.media_box().unwrap()[1]);
}

#[test]
fn zero_origin_is_top_left_and_positive_y_moves_downward() {
    let page = empirical_page_from_pixels(50, 100, [0.0, 0.0]).unwrap();
    let top = empirical_image_transform(page, 50, 100, point(0, 0)).unwrap();
    assert_eq!(top, [12.0, 0.0, 0.0, -24.0, 0.0, 24.0]);
    let moved = empirical_image_transform(page, 50, 100, point(1, 1)).unwrap();
    close(moved[4], 240.0 / 2473.0);
    close(moved[5], 24.0 - 240.0 / 2473.0);
    assert_eq!(&top[..4], &moved[..4]);
    // Coordinates remain unrounded; four-decimal precision is a comparison
    // policy, rather than a loss of precision during field evaluation.
    assert_ne!(moved[4], (moved[4] * 10_000.0).round() / 10_000.0);
}

#[test]
fn source_order_repeats_and_pixel_dimensions_do_not_change_coordinate_roles() {
    let page = page();
    let coordinates = [point(2, 4), point(7, 3), point(2, 4)];
    let transforms =
        coordinates.map(|coordinate| empirical_image_transform(page, 3, 7, coordinate).unwrap());
    assert_eq!(transforms[0], transforms[2]);
    assert!(transforms[1][4] > transforms[0][4]);
    assert!(transforms[1][5] > transforms[0][5]);
    let resized = empirical_image_transform(page, 11, 2, coordinates[0]).unwrap();
    // Compare returned binary64 values, not x87 extended intermediates.
    // Independently rounded exact coordinates are -13/4 + 480/2473 and
    // 489/8 - 960/2473. Pixel dimensions must not change either translation.
    for transform in [transforms[0], transforms[2], resized] {
        assert_eq!(transform[4].to_bits(), 0xc008727dabbc819b);
        assert_eq!(transform[5].to_bits(), 0x404e5e4fb5779033);
    }
    close(resized[0], 2.64);
    close(resized[3], -0.48);
}

#[test]
fn positive_pixel_range_has_no_integer_product_or_float_overflow() {
    let page = empirical_page_from_pixels(u32::MAX, u32::MAX, [0.0; 2]).unwrap();
    assert_eq!(page.size.width_points, f64::from(u32::MAX) * 0.24);
    let ctm = empirical_image_transform(page, u32::MAX, u32::MAX, point(1, 2)).unwrap();
    assert!(ctm.into_iter().all(f64::is_finite));
    for (width, height) in [(0, 1), (1, 0), (0, 0)] {
        assert!(matches!(
            empirical_page_from_pixels(width, height, [0.0; 2]),
            Err(Error {
                kind: ErrorKind::Malformed,
                ..
            })
        ));
        assert!(matches!(
            empirical_image_transform(page, width, height, point(0, 0)),
            Err(Error {
                kind: ErrorKind::Malformed,
                ..
            })
        ));
    }
}

#[test]
fn forged_page_origins_sizes_overflow_and_precision_collapse_are_rejected() {
    let valid = page();
    for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
        for origin in [[value, 0.0], [0.0, value]] {
            assert!(matches!(
                empirical_page_from_pixels(1, 1, origin),
                Err(Error {
                    kind: ErrorKind::Malformed,
                    ..
                })
            ));
        }
    }
    for value in [0.0, -1.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
        for size in [
            PageSpec {
                width_points: value,
                ..valid.size
            },
            PageSpec {
                height_points: value,
                ..valid.size
            },
        ] {
            let bad = EmpiricalPageGeometry { size, ..valid };
            assert!(matches!(
                bad.media_box(),
                Err(Error {
                    kind: ErrorKind::Malformed,
                    ..
                })
            ));
            assert!(matches!(
                empirical_image_transform(bad, 1, 1, point(0, 0)),
                Err(Error {
                    kind: ErrorKind::Malformed,
                    ..
                })
            ));
        }
    }
    for (origin_points, size) in [
        (
            [f64::MAX, 0.0],
            PageSpec {
                width_points: f64::MAX,
                height_points: 1.0,
            },
        ),
        (
            [0.0, f64::MAX],
            PageSpec {
                width_points: 1.0,
                height_points: f64::MAX,
            },
        ),
        (
            [f64::MAX, 0.0],
            PageSpec {
                width_points: 1.0,
                height_points: 1.0,
            },
        ),
        (
            [0.0, f64::MAX],
            PageSpec {
                width_points: 1.0,
                height_points: 1.0,
            },
        ),
    ] {
        let bad = EmpiricalPageGeometry {
            origin_points,
            size,
        };
        assert!(matches!(
            bad.media_box(),
            Err(Error {
                kind: ErrorKind::Malformed,
                ..
            })
        ));
    }
}

#[test]
fn noncollapsed_extreme_origins_must_preserve_dimension_and_coordinate_precision() {
    for page in [
        EmpiricalPageGeometry {
            origin_points: [1.0e18, 0.0],
            size: PageSpec {
                width_points: 1000.0,
                height_points: 24.0,
            },
        },
        EmpiricalPageGeometry {
            origin_points: [0.0, 1.0e18],
            size: PageSpec {
                width_points: 24.0,
                height_points: 1000.0,
            },
        },
    ] {
        assert!(matches!(
            page.media_box(),
            Err(Error {
                kind: ErrorKind::Malformed,
                reason: "empirical PDF origin cannot preserve the page dimension precision",
                ..
            })
        ));
    }
    for (page, coordinate) in [
        (
            EmpiricalPageGeometry {
                origin_points: [1.0e18, 0.0],
                size: PageSpec {
                    width_points: 1024.0,
                    height_points: 24.0,
                },
            },
            point(1, 0),
        ),
        (
            EmpiricalPageGeometry {
                origin_points: [0.0, 1.0e18],
                size: PageSpec {
                    width_points: 24.0,
                    height_points: 1024.0,
                },
            },
            point(0, 1),
        ),
    ] {
        assert!(page.media_box().is_ok());
        assert!(matches!(
            empirical_image_transform(page, 1, 1, coordinate),
            Err(Error {
                kind: ErrorKind::Malformed,
                reason: "empirical PDF origin cannot preserve the selected coordinate precision",
                ..
            })
        ));
        // An exact zero offset still has its intended meaning at that origin.
        assert!(empirical_image_transform(page, 1, 1, point(0, 0)).is_ok());
    }
}

#[test]
fn page_and_offset_rounding_must_not_accumulate_beyond_translation_tolerance() {
    let page = empirical_page_from_pixels(1, 9, [0.0, 274_877_906_944.0]).unwrap();
    let [_, bottom, _, top] = page.media_box().unwrap();
    let offset = 14.0 * EMPIRICAL_COORDINATE_POINTS_PER_UNIT;
    let rounded_y = top - offset;
    assert!(
        ((top - bottom) - page.size.height_points).abs() <= EMPIRICAL_PLACEMENT_TOLERANCE_POINTS
    );
    assert!(((top - rounded_y) - offset).abs() <= EMPIRICAL_PLACEMENT_TOLERANCE_POINTS);
    assert!(
        ((rounded_y - bottom) - (page.size.height_points - offset)).abs()
            > EMPIRICAL_PLACEMENT_TOLERANCE_POINTS
    );
    match empirical_image_transform(page, 1, 1, point(0, 14)) {
        // x87 may retain enough intermediate precision to avoid the two
        // accumulated binary64 rounding errors demonstrated above. Accept
        // success only for the one binary64 y within the existing tolerance
        // of the exact result: 2^38 + 54/25 - 3360/2473. Its neighbors miss by
        // -0.0000567930524666 and +0.0000652772600334 points respectively.
        Ok(ctm) => assert_eq!(ctm[5].to_bits(), 0x4250_0000_0000_3349),
        Err(error) => assert!(matches!(
            error,
            Error {
                kind: ErrorKind::Malformed,
                reason: "empirical PDF origin cannot preserve the selected coordinate precision",
                ..
            }
        )),
    }
}

#[test]
fn full_raw_word_domain_is_unsigned_and_preserves_off_page_positions() {
    let page = page();
    for coordinate in [
        point(0, 0),
        point(32767, 32767),
        point(32768, 0),
        point(0, 32768),
        point(u16::MAX, u16::MAX),
    ] {
        let ctm = empirical_image_transform(page, 1, 1, coordinate).unwrap();
        close(ctm[4], -3.25 + f64::from(coordinate.x) * 240.0 / 2473.0);
        close(ctm[5], 61.125 - f64::from(coordinate.y) * 240.0 / 2473.0);
        if coordinate.x >= 32768 {
            assert!(ctm[4] > page.media_box().unwrap()[2]);
        }
        if coordinate.y >= 32768 {
            assert!(ctm[5] < page.media_box().unwrap()[1]);
        }
    }
}

fn point(x: u16, y: u16) -> RawTextCoordinate {
    RawTextCoordinate {
        x,
        y,
        ..Default::default()
    }
}

#[test]
fn c8_glyph_sizes_predict_independent_original_controls() {
    let origin = [4652, 4274];
    for (field, expected_pixels) in [
        (2, 318),
        (3, 352),
        (4, 397),
        (5, 477),
        (6, 545),
        (7, 636),
        (8, 715),
    ] {
        let style = 0x1000 | field << 5 | field;
        let cjk = native_glyph_transform(
            page(),
            origin,
            [4672, 4294],
            style,
            C8GlyphClass::Cjk,
            [None; 2],
        )
        .unwrap();
        let latin = native_glyph_transform(
            page(),
            origin,
            [4672, 4294],
            style,
            C8GlyphClass::Latin,
            [None; 2],
        )
        .unwrap();
        // Original square fonts, 96 DPI, displayed 3420%. Field 7 was held out
        // from model calibration. These are measurements, not size-table copies.
        assert_eq!(
            (cjk[3] * 96.0 / 72.0 * 34.2).floor(),
            f64::from(expected_pixels)
        );
        assert_eq!(cjk[..4], latin[..4]);
        assert!(latin[4] > cjk[4]);
        assert!(latin[5] < cjk[5]);
        let shifted = native_glyph_transform(
            page(),
            [4672, 4294],
            [4692, 4314],
            style,
            C8GlyphClass::Cjk,
            [None; 2],
        )
        .unwrap();
        assert_eq!(shifted, cjk);
    }
    // Independent horizontal/vertical fields preserve each axis's size.
    let matrix = native_glyph_transform(
        page(),
        origin,
        [4672, 4294],
        0x1065,
        C8GlyphClass::Cjk,
        [None; 2],
    )
    .unwrap();
    assert!(matrix[0] < matrix[3]);
    close(matrix[0], 7.724252491694352);
    close(matrix[3], 10.465116279069768);
}

#[test]
fn c8_glyph_origins_are_signed_and_unknown_styles_are_errors() {
    let origin = [4652, 4274];
    let a = native_glyph_transform(
        page(),
        origin,
        [4652, 4274],
        0x1084,
        C8GlyphClass::Cjk,
        [None; 2],
    )
    .unwrap();
    let b = native_glyph_transform(
        page(),
        origin,
        [4632, 4254],
        0x1084,
        C8GlyphClass::Cjk,
        [None; 2],
    )
    .unwrap();
    close(b[4] - a[4], -20.0 * EMPIRICAL_COORDINATE_POINTS_PER_UNIT);
    close(b[5] - a[5], 20.0 * EMPIRICAL_COORDINATE_POINTS_PER_UNIT);
    for style in [
        0x0485, 0x9c85, 0x1485, 0x04c5, 0x14c5, 0x14a4, 0x9084, 0x1004, 0x1080, 0x1024, 0x108b,
    ] {
        assert!(
            native_glyph_transform(page(), origin, origin, style, C8GlyphClass::Cjk, [None; 2])
                .is_err()
        );
    }
    let mut invalid = page();
    invalid.size.height_points = f64::NAN;
    assert!(
        native_glyph_transform(
            invalid,
            origin,
            origin,
            0x1084,
            C8GlyphClass::Cjk,
            [None; 2]
        )
        .is_err()
    );
}

#[test]
fn c8_horizontal_decoration_preserves_partial_marks_and_independent_axes() {
    let page = source_page_geometry([600, 600]).unwrap();
    let origin = [4652, 4274];
    for (length, count) in [(10, 1), (50, 1), (89, 1), (91, 2), (180, 3), (430, 5)] {
        let d = empirical_c8_horizontal_decoration(
            page,
            origin,
            [[4712, 4334], [4712 + length, 4334]],
            0x1084,
            [None; 2],
        )
        .unwrap();
        // Counts independently observed at 486% and 993%, including partial tails.
        assert_eq!(d.glyph_count, count);
        close(d.first_glyph[4], 5.822887181560857);
        close(d.first_glyph[5], 48.045519517768646);
        close(
            d.clip[2],
            f64::from(length) * EMPIRICAL_COORDINATE_POINTS_PER_UNIT,
        );
        assert_eq!(d.clip[1], 0.0);
        assert_eq!(d.clip[3], page.size.height_points);
    }
    let a = empirical_c8_horizontal_decoration(
        page,
        origin,
        [[4712, 4334], [5142, 4334]],
        0x1048,
        [None; 2],
    )
    .unwrap();
    let b = empirical_c8_horizontal_decoration(
        page,
        origin,
        [[4712, 4334], [5142, 4334]],
        0x1102,
        [None; 2],
    )
    .unwrap();
    assert!(a.first_glyph[0] < b.first_glyph[0]);
    assert!(a.first_glyph[3] > b.first_glyph[3]);
    assert!(a.glyph_count > b.glyph_count);
    assert_eq!(a.clip, b.clip);
    let shifted = empirical_c8_horizontal_decoration(
        page,
        [4672, 4294],
        [[4732, 4354], [5162, 4354]],
        0x1048,
        [None; 2],
    )
    .unwrap();
    assert_eq!(a, shifted);
    let maximum = empirical_c8_horizontal_decoration(
        page,
        origin,
        [[0, 0], [u16::MAX, 0]],
        0x1042,
        [None; 2],
    )
    .unwrap();
    assert!(maximum.first_glyph[4] < 0.0);
    assert!(maximum.glyph_count < 1000);
}

#[test]
fn c8_horizontal_decoration_rejects_unverified_geometry_and_styles() {
    for points in [
        [[1, 1], [1, 1]],
        [[2, 1], [1, 1]],
        [[1, 1], [1, 2]],
        [[1, 1], [2, 2]],
    ] {
        assert!(
            empirical_c8_horizontal_decoration(page(), [0, 0], points, 0x1084, [None; 2]).is_err()
        );
    }
    assert!(
        empirical_c8_horizontal_decoration(page(), [0, 0], [[0, 0], [1, 0]], 0x1080, [None; 2])
            .is_err()
    );
    let mut invalid = page();
    invalid.size.width_points = f64::NAN;
    assert!(
        empirical_c8_horizontal_decoration(invalid, [0, 0], [[0, 0], [1, 0]], 0x1084, [None; 2])
            .is_err()
    );
}

#[test]
fn c8_segments_reproduce_independent_axes_and_preserve_endpoint_order() {
    let page = source_page_geometry([600, 600]).unwrap();
    for style in [0xa380, 0xa381, 0xa382, 0xa383, 0xa38b] {
        let horizontal =
            empirical_c8_segment(page, [4652, 4274], [[4682, 4304], [4832, 4304]], style).unwrap();
        // Original segment-axes control: relative endpoints (30,30)/(180,30).
        close(horizontal[0][0], 12000.0 / 2473.0);
        close(horizontal[1][0], 48000.0 / 2473.0);
        close(horizontal[0][1], 132000.0 / 2473.0);
        assert_eq!(horizontal[0][1], horizontal[1][1]);
        let reverse =
            empirical_c8_segment(page, [4652, 4274], [[4832, 4304], [4682, 4304]], style).unwrap();
        assert_eq!(reverse, [horizontal[1], horizontal[0]]);
        let vertical =
            empirical_c8_segment(page, [4652, 4274], [[4902, 4524], [4902, 4704]], style).unwrap();
        assert_eq!(vertical[0][0], vertical[1][0]);
        assert!(vertical[0][1] > vertical[1][1]);
        let shifted =
            empirical_c8_segment(page, [4672, 4294], [[4702, 4324], [4852, 4324]], style).unwrap();
        assert_eq!(shifted, horizontal);
    }
    let off_page =
        empirical_c8_segment(page, [100, 100], [[0, 0], [u16::MAX, u16::MAX]], 0xa381).unwrap();
    assert!(off_page[0][0] < 0.0);
    assert!(off_page[1][1] < 0.0);
    for style in [0xa384, 0xa386, 1] {
        assert!(empirical_c8_segment(page, [0, 0], [[0, 0], [1, 1]], style).is_err());
    }
    let mut invalid = page;
    invalid.origin_points[0] = f64::INFINITY;
    assert!(empirical_c8_segment(invalid, [0, 0], [[0, 0], [1, 1]], 0xa381).is_err());
}

#[test]
fn observed_glyph_style_prefixes_share_geometry_without_admitting_other_records() {
    for field in [2, 8] {
        for class in [C8GlyphClass::Cjk, C8GlyphClass::Latin] {
            let style = 0x1000 | field << 5 | field;
            let expected =
                native_glyph_transform(page(), [4652, 4274], [4672, 4294], style, class, [None; 2])
                    .unwrap();
            for flags in [0x0800, 0x0c00] {
                let actual = native_glyph_transform(
                    page(),
                    [4652, 4274],
                    [4672, 4294],
                    flags | field << 5 | field,
                    class,
                    [None; 2],
                )
                .unwrap();
                // i586 may retain x87 intermediate precision across the two
                // evaluations. Compare arithmetic with the existing point-scale
                // tolerance; this is not a source-raster fidelity tolerance.
                for (actual, expected) in actual.into_iter().zip(expected) {
                    close(actual, expected);
                }
            }
        }
    }
    // The independent glyph controls do not admit these decoration states.
    assert!(
        empirical_c8_horizontal_decoration(page(), [0, 0], [[0, 0], [100, 0]], 0x0884, [None; 2])
            .is_err()
    );
}

#[test]
fn independently_controlled_variants_preserve_both_glyph_classes() {
    for (reference, styles) in [
        (0x10e7, &[0x04e7, 0x14e7][..]),
        (0x1084, &[0x0484, 0x1484, 0x9c84][..]),
        (0x10c6, &[0x04c6, 0x14c6][..]),
        (0x10a5, &[0x14a5][..]),
    ] {
        for class in [C8GlyphClass::Cjk, C8GlyphClass::Latin] {
            let expected = native_glyph_transform(
                page(),
                [4652, 4274],
                [5200, 4700],
                reference,
                class,
                [None; 2],
            )
            .unwrap();
            for &style in styles {
                let actual = native_glyph_transform(
                    page(),
                    [4652, 4274],
                    [5200, 4700],
                    style,
                    class,
                    [None; 2],
                )
                .unwrap();
                for (actual, expected) in actual.into_iter().zip(expected) {
                    close(actual, expected);
                }
                assert!(
                    empirical_c8_horizontal_decoration(
                        page(),
                        [0, 0],
                        [[0, 0], [100, 0]],
                        style,
                        [None; 2]
                    )
                    .is_err()
                );
            }
        }
    }
}

#[test]
fn large_cjk_control_uses_verified_em_and_existing_signed_origin() {
    for (style, expected_em) in [(0xe58c, 27.159468438538206), (0x154a, 20.930232558139537)] {
        let actual = native_glyph_transform(
            page(),
            [4652, 4274],
            [4672, 4294],
            style,
            C8GlyphClass::Cjk,
            [None; 2],
        )
        .unwrap();
        close(actual[0], expected_em);
        close(actual[3], expected_em);
        close(
            actual[4],
            page().origin_points[0] + 40.0 * EMPIRICAL_COORDINATE_POINTS_PER_UNIT,
        );
        let shifted = native_glyph_transform(
            page(),
            [4672, 4294],
            [4692, 4314],
            style,
            C8GlyphClass::Cjk,
            [None; 2],
        )
        .unwrap();
        assert_eq!(actual, shifted);
        assert!(
            native_glyph_transform(
                page(),
                [0, 0],
                [0, 0],
                style,
                C8GlyphClass::Latin,
                [None; 2]
            )
            .is_err()
        );
    }
    for style in [0x118c, 0xe58b, 0xe56c] {
        assert!(
            native_glyph_transform(page(), [0, 0], [0, 0], style, C8GlyphClass::Cjk, [None; 2])
                .is_err()
        );
    }
}

#[test]
fn a385_line_marker_preserves_independently_checked_endpoints() {
    for points in [[[4757, 4800], [6300, 4800]], [[4690, 4350], [4900, 4500]]] {
        let expected = empirical_c8_segment(page(), [4652, 4274], points, 0xa381).unwrap();
        let mut marked = points;
        marked[0][0] |= 0xc000;
        // x87 targets may retain different intermediate precision. Use the
        // existing point tolerance, as for the other geometry controls.
        for input in [points, marked] {
            let actual = empirical_c8_segment(page(), [4652, 4274], input, 0xa385).unwrap();
            for (actual, expected) in actual
                .into_iter()
                .flatten()
                .zip(expected.into_iter().flatten())
            {
                close(actual, expected);
            }
        }
        assert_ne!(
            empirical_c8_segment(page(), [4652, 4274], marked, 0xa381).unwrap(),
            expected
        );
    }
}

#[test]
fn mode_zero_geometry_uses_its_measured_origins_and_size_zero() {
    let page = source_page_geometry([700, 500]).unwrap();
    let unit = EMPIRICAL_COORDINATE_POINTS_PER_UNIT;
    for (style, class, size, baseline) in [
        (0x1084, C8GlyphClass::Cjk, 35.0, 0.0),
        (0x1084, C8GlyphClass::Latin, 35.0, 10.0),
        (0, C8GlyphClass::Cjk, 21.0, 0.0),
        (0x1000, C8GlyphClass::Latin, 21.0, 10.0),
        (0x154a, C8GlyphClass::Cjk, 84.0, 0.0),
    ] {
        let m =
            mode_zero_glyph_transform(page, [30, 41], [100, 140], style, class, [None; 2]).unwrap();
        close(m[0], size * 75.0 / 301.0);
        close(m[3], size * 75.0 / 301.0);
        close(m[4], 90.0 * unit);
        close(m[5], (401.0 - baseline) * unit - size * 75.0 / 301.0);
    }
    let bad_page = EmpiricalPageGeometry {
        origin_points: [f64::NAN, 0.0],
        ..page
    };
    assert!(
        mode_zero_glyph_transform(bad_page, [0; 2], [100; 2], 0, C8GlyphClass::Cjk, [None; 2])
            .is_err()
    );
    assert!(
        mode_zero_glyph_transform(
            page,
            [0; 2],
            [100; 2],
            0x154a,
            C8GlyphClass::Latin,
            [None; 2]
        )
        .is_err()
    );
}

#[test]
fn mode_zero_digits_have_measured_height_specific_offsets() {
    let page = source_page_geometry([300, 300]).unwrap();
    let unit = EMPIRICAL_COORDINATE_POINTS_PER_UNIT;
    for (style, axes, em, raw_x, raw_y) in [
        (0x1000, [None; 2], 21.0, 19.0, 64.0),
        (0x1084, [None; 2], 35.0, 18.0, 67.0),
        (0x10a5, [None; 2], 42.0, 18.0, 69.0),
        (0, [Some(36); 2], 36.0, 18.0, 67.0),
    ] {
        let m = mode_zero_digit_transform(page, [0, 1], [20, 50], style, axes).unwrap();
        close(m[0], em * 75.0 / 301.0);
        close(m[3], em * 75.0 / 301.0);
        close(m[4], raw_x * unit);
        close(m[5], (300.0 - raw_y) * unit - em * 75.0 / 301.0);
    }
    for (style, axes) in [
        (0x154a, [None; 2]),
        (0x1084, [Some(36), None]),
        (0xe58c, [Some(36); 2]),
    ] {
        assert!(mode_zero_digit_transform(page, [0; 2], [20, 50], style, axes).is_err());
    }
}

#[test]
fn four_unit_axes_preserve_measured_em_and_latin_baseline() {
    let cjk = native_glyph_transform(
        page(),
        [4652, 4274],
        [4672, 4334],
        0x1084,
        C8GlyphClass::Cjk,
        [Some(4); 2],
    )
    .unwrap();
    let latin = native_glyph_transform(
        page(),
        [4652, 4274],
        [4672, 4334],
        0x10a5,
        C8GlyphClass::Latin,
        [Some(4); 2],
    )
    .unwrap();
    close(cjk[0], 4.0 * 75.0 / 301.0);
    close(cjk[3], cjk[0]);
    close(latin[0], cjk[0]);
    close(latin[3], cjk[3]);
    close(latin[4] - cjk[4], cjk[0] / 8.0);
    close(
        cjk[5] - latin[5],
        15.0 * EMPIRICAL_COORDINATE_POINTS_PER_UNIT,
    );
}

#[test]
fn small_field_glyph_controls_preserve_independent_axes_and_latin_baseline() {
    let page = empirical_page_from_pixels(200, 200, [0.0; 2]).unwrap();
    for (style, width, height, offset) in [
        (0x1000, 21.0, 21.0, 11.0),
        (0x1001, 21.0, 24.0, 10.0),
        (0x1020, 24.0, 21.0, 11.0),
        (0x1021, 24.0, 24.0, 10.0),
        (0x1129, 72.0, 72.0, -1.0),
        (0x1022, 24.0, 28.0, 9.0),
        (0x1041, 28.0, 24.0, 10.0),
    ] {
        let cjk = native_glyph_transform(
            page,
            [4652, 4274],
            [4672, 4334],
            style,
            C8GlyphClass::Cjk,
            [None; 2],
        )
        .unwrap();
        let latin = native_glyph_transform(
            page,
            [4652, 4274],
            [4672, 4334],
            style,
            C8GlyphClass::Latin,
            [None; 2],
        )
        .unwrap();
        close(cjk[0], width * 75.0 / 301.0);
        close(cjk[3], height * 75.0 / 301.0);
        close(latin[4] - cjk[4], cjk[0] / 8.0);
        close(
            cjk[5] - latin[5],
            offset * EMPIRICAL_COORDINATE_POINTS_PER_UNIT,
        );
        assert!(
            empirical_c8_horizontal_decoration(
                page,
                [4652, 4274],
                [[4672, 4334], [4752, 4334]],
                style,
                [None; 2]
            )
            .is_err()
        );
    }
}