sugarloaf 0.2.36

Sugarloaf is Rio rendering engine, designed to be multiplatform. It is based on WebGPU, Rust library for Desktops and WebAssembly for Web (JavaScript). This project is created and maintained for Rio terminal purposes but feel free to use it.
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
// Copyright (c) 2023-present, Raphael Amorim.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//
// CJK Font Metrics Integration Tests
//
// These tests verify that the consistent font metrics approach
// correctly handles CJK (Chinese, Japanese, Korean) fonts alongside
// Latin fonts, ensuring consistent terminal grid behavior.

#[cfg(test)]
mod tests {
    use crate::font::metrics::{FaceMetrics, Metrics};

    /// Test data representing typical font metrics for different font types
    struct TestFontData {
        name: &'static str,
        face: FaceMetrics,
    }

    impl TestFontData {
        fn cascadia_code() -> Self {
            Self {
                name: "Cascadia Code",
                face: FaceMetrics {
                    cell_width: 9.6,
                    ascent: 11.5,
                    descent: 2.5,
                    line_gap: 0.8,
                    underline_position: Some(-1.2),
                    underline_thickness: Some(1.0),
                    strikethrough_position: Some(5.8),
                    strikethrough_thickness: Some(1.0),
                    cap_height: Some(8.7),
                    ex_height: Some(5.8),
                    ic_width: None,
                },
            }
        }

        fn noto_sans_cjk() -> Self {
            Self {
                name: "Noto Sans CJK",
                face: FaceMetrics {
                    cell_width: 19.2, // Double width for CJK
                    ascent: 13.8,
                    descent: 3.2,
                    line_gap: 1.2,
                    underline_position: Some(-1.8),
                    underline_thickness: Some(1.1),
                    strikethrough_position: Some(6.9),
                    strikethrough_thickness: Some(1.1),
                    cap_height: Some(10.4),
                    ex_height: Some(6.9),
                    ic_width: None,
                },
            }
        }

        fn source_han_sans() -> Self {
            Self {
                name: "Source Han Sans",
                face: FaceMetrics {
                    cell_width: 18.8,
                    ascent: 14.2,
                    descent: 3.8,
                    line_gap: 1.0,
                    underline_position: Some(-2.0),
                    underline_thickness: Some(1.2),
                    strikethrough_position: Some(7.1),
                    strikethrough_thickness: Some(1.2),
                    cap_height: Some(10.7),
                    ex_height: Some(7.1),
                    ic_width: None,
                },
            }
        }

        fn wenquanyi_micro_hei() -> Self {
            Self {
                name: "WenQuanYi Micro Hei",
                face: FaceMetrics {
                    cell_width: 20.0,
                    ascent: 15.0,
                    descent: 4.0,
                    line_gap: 1.5,
                    underline_position: Some(-1.5),
                    underline_thickness: Some(1.0),
                    strikethrough_position: Some(7.5),
                    strikethrough_thickness: Some(1.0),
                    cap_height: Some(11.25),
                    ex_height: Some(7.5),
                    ic_width: None,
                },
            }
        }

        fn dejavu_sans_mono() -> Self {
            Self {
                name: "DejaVu Sans Mono",
                face: FaceMetrics {
                    cell_width: 8.4,
                    ascent: 10.8,
                    descent: 2.2,
                    line_gap: 0.6,
                    underline_position: Some(-1.0),
                    underline_thickness: Some(0.8),
                    strikethrough_position: Some(5.4),
                    strikethrough_thickness: Some(0.8),
                    cap_height: Some(8.1),
                    ex_height: Some(5.4),
                    ic_width: None,
                },
            }
        }

        fn noto_color_emoji() -> Self {
            Self {
                name: "Noto Color Emoji",
                face: FaceMetrics {
                    cell_width: 20.0, // Emoji are typically double-width
                    ascent: 14.0,
                    descent: 3.5,
                    line_gap: 1.0,
                    underline_position: Some(-2.0),
                    underline_thickness: Some(1.2),
                    strikethrough_position: Some(7.0),
                    strikethrough_thickness: Some(1.2),
                    cap_height: Some(10.5),
                    ex_height: Some(7.0),
                    ic_width: None,
                },
            }
        }
    }

    #[test]
    fn test_cjk_font_consistency_with_latin_primary() {
        let latin_font = TestFontData::cascadia_code();
        let cjk_fonts = vec![
            TestFontData::noto_sans_cjk(),
            TestFontData::source_han_sans(),
            TestFontData::wenquanyi_micro_hei(),
        ];

        let primary_metrics = Metrics::calc(latin_font.face);

        for cjk_font in cjk_fonts {
            let cjk_metrics = Metrics::calc_with_primary_cell_dimensions(
                cjk_font.face,
                &primary_metrics,
            );

            // All fonts should use the same cell dimensions
            assert_eq!(
                cjk_metrics.cell_width, primary_metrics.cell_width,
                "{} should use primary cell width",
                cjk_font.name
            );
            assert_eq!(
                cjk_metrics.cell_height, primary_metrics.cell_height,
                "{} should use primary cell height",
                cjk_font.name
            );
            assert_eq!(
                cjk_metrics.cell_baseline, primary_metrics.cell_baseline,
                "{} should use primary baseline",
                cjk_font.name
            );
            assert_eq!(
                cjk_metrics.cursor_height, primary_metrics.cursor_height,
                "{} should use primary cursor height",
                cjk_font.name
            );

            // Verify metrics are reasonable
            assert!(
                cjk_metrics.cell_width > 0,
                "{} cell width should be positive",
                cjk_font.name
            );
            assert!(
                cjk_metrics.cell_height > 0,
                "{} cell height should be positive",
                cjk_font.name
            );
            assert!(
                cjk_metrics.cell_baseline < cjk_metrics.cell_height,
                "{} baseline should be within cell height",
                cjk_font.name
            );
        }
    }

    #[test]
    fn test_multiple_latin_fonts_consistency() {
        let fonts = vec![
            TestFontData::cascadia_code(),
            TestFontData::dejavu_sans_mono(),
        ];

        let primary_metrics = Metrics::calc(fonts[0].face);

        for font in fonts.iter().skip(1) {
            let font_metrics =
                Metrics::calc_with_primary_cell_dimensions(font.face, &primary_metrics);

            assert_eq!(
                font_metrics.cell_width, primary_metrics.cell_width,
                "{} should use primary cell width",
                font.name
            );
            assert_eq!(
                font_metrics.cell_height, primary_metrics.cell_height,
                "{} should use primary cell height",
                font.name
            );
            assert_eq!(
                font_metrics.cell_baseline, primary_metrics.cell_baseline,
                "{} should use primary baseline",
                font.name
            );
        }
    }

    #[test]
    fn test_cjk_primary_font_scenario() {
        // Test scenario where CJK font is the primary font
        let cjk_font = TestFontData::noto_sans_cjk();
        let latin_font = TestFontData::cascadia_code();

        let primary_metrics = Metrics::calc(cjk_font.face);
        let latin_metrics =
            Metrics::calc_with_primary_cell_dimensions(latin_font.face, &primary_metrics);

        // Latin font should adapt to CJK dimensions
        assert_eq!(latin_metrics.cell_width, primary_metrics.cell_width);
        assert_eq!(latin_metrics.cell_height, primary_metrics.cell_height);
        assert_eq!(latin_metrics.cell_baseline, primary_metrics.cell_baseline);

        // Verify the CJK font's natural dimensions are used
        let expected_height = (cjk_font.face.ascent
            + cjk_font.face.descent
            + (cjk_font.face.line_gap * 2.0))
            .ceil() as u32;
        assert_eq!(primary_metrics.cell_height, expected_height);
    }

    #[test]
    fn test_rich_text_format_consistency() {
        let latin_font = TestFontData::cascadia_code();
        let cjk_font = TestFontData::noto_sans_cjk();

        let primary_metrics = Metrics::calc(latin_font.face);
        let cjk_metrics =
            Metrics::calc_with_primary_cell_dimensions(cjk_font.face, &primary_metrics);

        let (latin_ascent, latin_descent, latin_leading) =
            primary_metrics.for_rich_text();
        let (cjk_ascent, cjk_descent, cjk_leading) = cjk_metrics.for_rich_text();

        // Rich text format should be consistent
        assert_eq!(
            latin_ascent, cjk_ascent,
            "Rich text ascent should be consistent"
        );
        assert_eq!(
            latin_descent, cjk_descent,
            "Rich text descent should be consistent"
        );
        assert_eq!(
            latin_leading, cjk_leading,
            "Rich text leading should be consistent"
        );

        // Verify the values make sense
        assert!(latin_ascent > 0.0, "Ascent should be positive");
        assert!(latin_descent > 0.0, "Descent should be positive");
        assert_eq!(
            latin_leading, 0.0,
            "Leading should be 0 (incorporated into height)"
        );

        // Verify ascent + descent equals cell height
        assert_eq!(
            latin_ascent + latin_descent,
            primary_metrics.cell_height as f32
        );
    }

    #[test]
    fn test_underline_positioning_across_fonts() {
        let latin_font = TestFontData::cascadia_code();
        let cjk_font = TestFontData::noto_sans_cjk();

        let primary_metrics = Metrics::calc(latin_font.face);
        let cjk_metrics =
            Metrics::calc_with_primary_cell_dimensions(cjk_font.face, &primary_metrics);

        // Both should have reasonable underline positioning
        assert!(primary_metrics.underline_position <= primary_metrics.cell_height);
        assert!(primary_metrics.underline_thickness > 0);
        assert!(cjk_metrics.underline_thickness > 0);

        // Note: CJK font's underline position might be outside cell bounds when using
        // primary font's cell dimensions. This is expected behavior - the underline
        // calculation is font-specific but constrained by primary font's cell size.
        // In practice, the renderer would clamp this to reasonable bounds.

        // Verify that both fonts use the same cell dimensions (the key requirement)
        assert_eq!(cjk_metrics.cell_width, primary_metrics.cell_width);
        assert_eq!(cjk_metrics.cell_height, primary_metrics.cell_height);
        assert_eq!(cjk_metrics.cell_baseline, primary_metrics.cell_baseline);
    }

    #[test]
    fn test_strikethrough_positioning_across_fonts() {
        let latin_font = TestFontData::cascadia_code();
        let cjk_font = TestFontData::noto_sans_cjk();

        let primary_metrics = Metrics::calc(latin_font.face);
        let cjk_metrics =
            Metrics::calc_with_primary_cell_dimensions(cjk_font.face, &primary_metrics);

        // Both should have reasonable strikethrough positioning
        assert!(primary_metrics.strikethrough_position < primary_metrics.cell_height);
        assert!(cjk_metrics.strikethrough_position < cjk_metrics.cell_height);
        assert!(primary_metrics.strikethrough_thickness > 0);
        assert!(cjk_metrics.strikethrough_thickness > 0);

        // Strikethrough should be above underline
        assert!(
            primary_metrics.strikethrough_position < primary_metrics.underline_position
        );
        assert!(cjk_metrics.strikethrough_position < cjk_metrics.underline_position);
    }

    #[test]
    fn test_extreme_font_metrics() {
        // Test with extreme font metrics to ensure robustness
        let tiny_font = FaceMetrics {
            cell_width: 1.0,
            ascent: 2.0,
            descent: 0.5,
            line_gap: 0.1,
            underline_position: Some(-0.1),
            underline_thickness: Some(0.1),
            strikethrough_position: Some(1.0),
            strikethrough_thickness: Some(0.1),
            cap_height: Some(1.5),
            ex_height: Some(1.0),
            ic_width: None,
        };

        let huge_font = FaceMetrics {
            cell_width: 100.0,
            ascent: 120.0,
            descent: 30.0,
            line_gap: 10.0,
            underline_position: Some(-5.0),
            underline_thickness: Some(3.0),
            strikethrough_position: Some(60.0),
            strikethrough_thickness: Some(3.0),
            cap_height: Some(90.0),
            ex_height: Some(60.0),
            ic_width: None,
        };

        let tiny_metrics = Metrics::calc(tiny_font);
        let huge_metrics =
            Metrics::calc_with_primary_cell_dimensions(huge_font, &tiny_metrics);

        // Huge font should adapt to tiny font's dimensions
        assert_eq!(huge_metrics.cell_width, tiny_metrics.cell_width);
        assert_eq!(huge_metrics.cell_height, tiny_metrics.cell_height);
        assert_eq!(huge_metrics.cell_baseline, tiny_metrics.cell_baseline);

        // All values should be reasonable (no panics, no zero values where inappropriate)
        assert!(tiny_metrics.cell_width > 0);
        assert!(tiny_metrics.cell_height > 0);
        assert!(huge_metrics.cell_width > 0);
        assert!(huge_metrics.cell_height > 0);
    }

    #[test]
    fn test_line_height_calculation_precision() {
        // Test that line height calculation maintains precision correctly
        let font = FaceMetrics {
            cell_width: 9.6,
            ascent: 11.7,
            descent: 2.3,
            line_gap: 0.9,
            underline_position: None,
            underline_thickness: None,
            strikethrough_position: None,
            strikethrough_thickness: None,
            cap_height: None,
            ex_height: None,
            ic_width: None,
        };

        let metrics = Metrics::calc(font);

        // Line height: 11.7 + 2.3 + (0.9 * 2.0) = 16.8, ceiled to 17
        // But let's check the actual calculation
        let expected_height =
            (font.ascent + font.descent + (font.line_gap * 2.0)).ceil() as u32;
        assert_eq!(metrics.cell_height, expected_height);

        // Cell width: 9.6 ceiled to 10
        assert_eq!(metrics.cell_width, 10);
    }

    #[test]
    fn test_baseline_consistency_across_font_combinations() {
        let fonts = vec![
            TestFontData::cascadia_code(),
            TestFontData::dejavu_sans_mono(),
            TestFontData::noto_sans_cjk(),
            TestFontData::source_han_sans(),
            TestFontData::wenquanyi_micro_hei(),
        ];

        // Test each font as primary with others as secondary
        for (i, primary_font) in fonts.iter().enumerate() {
            let primary_metrics = Metrics::calc(primary_font.face);

            for (j, secondary_font) in fonts.iter().enumerate() {
                if i == j {
                    continue;
                } // Skip self

                let secondary_metrics = Metrics::calc_with_primary_cell_dimensions(
                    secondary_font.face,
                    &primary_metrics,
                );

                // Baseline should be consistent
                assert_eq!(
                    secondary_metrics.cell_baseline, primary_metrics.cell_baseline,
                    "Baseline inconsistent: {} primary, {} secondary",
                    primary_font.name, secondary_font.name
                );

                // Cell dimensions should be consistent
                assert_eq!(
                    secondary_metrics.cell_width, primary_metrics.cell_width,
                    "Cell width inconsistent: {} primary, {} secondary",
                    primary_font.name, secondary_font.name
                );

                assert_eq!(
                    secondary_metrics.cell_height, primary_metrics.cell_height,
                    "Cell height inconsistent: {} primary, {} secondary",
                    primary_font.name, secondary_font.name
                );
            }
        }
    }

    // Tests specifically for Issue #1071: CJK characters display "higher" than Latins,
    // so the terminal scroll to wrong place after showing long CJK text
    //
    // These tests verify that the fix correctly handles the scrolling issues
    // caused by inconsistent line heights between Latin and CJK characters.

    /// Test that reproduces the original issue #1071
    /// Before the fix: CJK fonts would have different line heights than Latin fonts,
    /// causing incorrect scrolling calculations
    #[test]
    fn test_issue_1071_cjk_latin_line_height_consistency() {
        // Simulate the scenario described in issue #1071
        // Latin font (like the user's primary font)
        let latin_font = FaceMetrics {
            cell_width: 9.0,
            ascent: 11.0,
            descent: 3.0,
            line_gap: 1.0,
            underline_position: Some(-1.0),
            underline_thickness: Some(1.0),
            strikethrough_position: Some(5.5),
            strikethrough_thickness: Some(1.0),
            cap_height: Some(8.5),
            ex_height: Some(5.5),
            ic_width: None,
        };

        // CJK font (fallback font that was causing issues)
        let cjk_font = FaceMetrics {
            cell_width: 18.0, // Double width
            ascent: 14.0,     // Taller ascent
            descent: 4.0,     // Deeper descent
            line_gap: 2.0,    // More line spacing
            underline_position: Some(-1.5),
            underline_thickness: Some(1.2),
            strikethrough_position: Some(7.0),
            strikethrough_thickness: Some(1.2),
            cap_height: Some(10.5),
            ex_height: Some(7.0),
            ic_width: Some(36.0), // Measured CJK character width
        };

        // Calculate metrics using the consistent approach (the fix)
        let latin_metrics = Metrics::calc(latin_font);
        let cjk_metrics =
            Metrics::calc_with_primary_cell_dimensions(cjk_font, &latin_metrics);

        // CRITICAL: Both fonts must have identical cell dimensions
        // This is what fixes the scrolling issue
        assert_eq!(
            cjk_metrics.cell_height,
            latin_metrics.cell_height,
            "CJK and Latin fonts must have identical cell heights to prevent scrolling issues"
        );

        assert_eq!(
            cjk_metrics.cell_width, latin_metrics.cell_width,
            "CJK and Latin fonts must have identical cell widths for consistent grid"
        );

        assert_eq!(
            cjk_metrics.cell_baseline,
            latin_metrics.cell_baseline,
            "CJK and Latin fonts must have identical baselines to prevent text misalignment"
        );

        // Verify that the terminal can calculate scroll distances correctly
        // Before the fix: terminal would assume 4 Latin lines = 4 * latin_height
        // But actual content height would be different due to CJK line heights
        let lines_to_scroll = 4;
        let expected_scroll_distance = lines_to_scroll * latin_metrics.cell_height;
        let actual_scroll_distance = lines_to_scroll * cjk_metrics.cell_height;

        assert_eq!(
            expected_scroll_distance,
            actual_scroll_distance,
            "Scroll distance calculation must be consistent between Latin and CJK content"
        );
    }

    /// Test the specific scenario mentioned in issue #1071:
    /// "after a long CJK text was output in rio terminal, it run into a strange status"
    #[test]
    fn test_issue_1071_long_cjk_text_scrolling() {
        // Simulate fonts similar to those that would cause the issue
        let primary_font = FaceMetrics {
            cell_width: 10.0,
            ascent: 12.0,
            descent: 3.0,
            line_gap: 1.0,
            underline_position: Some(-1.0),
            underline_thickness: Some(1.0),
            strikethrough_position: Some(6.0),
            strikethrough_thickness: Some(1.0),
            cap_height: Some(9.0),
            ex_height: Some(6.0),
            ic_width: None,
        };

        // CJK font that would previously cause scrolling issues
        let cjk_font = FaceMetrics {
            cell_width: 20.0,
            ascent: 15.0,
            descent: 4.0,
            line_gap: 2.0,
            underline_position: Some(-2.0),
            underline_thickness: Some(1.5),
            strikethrough_position: Some(7.5),
            strikethrough_thickness: Some(1.5),
            cap_height: Some(11.0),
            ex_height: Some(7.5),
            ic_width: Some(40.0),
        };

        let primary_metrics = Metrics::calc(primary_font);
        let cjk_metrics =
            Metrics::calc_with_primary_cell_dimensions(cjk_font, &primary_metrics);

        // Simulate the scenario: terminal displays long CJK text and needs to scroll
        // The issue was that terminal would calculate scroll based on Latin line height
        // but actual content would have different height due to CJK metrics

        let terminal_rows = 24; // Typical terminal height
        let content_lines = 30; // More lines than can fit on screen
        let lines_to_scroll = content_lines - terminal_rows; // 6 lines need to scroll

        // Before fix: these would be different, causing wrong scroll position
        let latin_based_scroll = lines_to_scroll * primary_metrics.cell_height;
        let cjk_based_scroll = lines_to_scroll * cjk_metrics.cell_height;

        assert_eq!(
            latin_based_scroll, cjk_based_scroll,
            "Scroll calculations must be identical for Latin and CJK content"
        );

        // Verify that the input line remains visible after scrolling
        // The issue mentioned: "I cannot even see the input line!"
        let total_content_height = content_lines * cjk_metrics.cell_height;
        let visible_area_height = terminal_rows * cjk_metrics.cell_height;
        let scroll_position = total_content_height - visible_area_height;

        // Input line should be at the bottom of visible area
        let input_line_position = total_content_height - cjk_metrics.cell_height;
        let input_line_visible = input_line_position >= scroll_position;

        assert!(
            input_line_visible,
            "Input line must remain visible after scrolling CJK content"
        );
    }

    /// Test the printf scenario specifically mentioned in the issue
    #[test]
    fn test_issue_1071_printf_command_scrolling() {
        // The issue mentioned: "terminal scrolls less than 4 lines down after the printf command executed"
        let latin_font = FaceMetrics {
            cell_width: 8.0,
            ascent: 10.0,
            descent: 2.0,
            line_gap: 0.5,
            underline_position: Some(-1.0),
            underline_thickness: Some(1.0),
            strikethrough_position: Some(5.0),
            strikethrough_thickness: Some(1.0),
            cap_height: Some(7.5),
            ex_height: Some(5.0),
            ic_width: None,
        };

        let cjk_font = FaceMetrics {
            cell_width: 16.0,
            ascent: 13.0,
            descent: 3.5,
            line_gap: 1.5,
            underline_position: Some(-1.5),
            underline_thickness: Some(1.2),
            strikethrough_position: Some(6.5),
            strikethrough_thickness: Some(1.2),
            cap_height: Some(9.5),
            ex_height: Some(6.5),
            ic_width: Some(32.0),
        };

        let latin_metrics = Metrics::calc(latin_font);
        let cjk_metrics =
            Metrics::calc_with_primary_cell_dimensions(cjk_font, &latin_metrics);

        // Simulate printf outputting 4 lines of content
        let printf_output_lines = 4;

        // Before fix: terminal would calculate scroll distance based on Latin metrics
        // but actual content height would be based on CJK metrics, causing mismatch
        let expected_scroll_distance = printf_output_lines * latin_metrics.cell_height;
        let actual_content_height = printf_output_lines * cjk_metrics.cell_height;

        assert_eq!(
            expected_scroll_distance, actual_content_height,
            "printf output height calculation must be consistent between font types"
        );

        // The fix ensures that both calculations use the same cell_height
        assert_eq!(
            latin_metrics.cell_height, cjk_metrics.cell_height,
            "Both fonts must use same cell height to prevent printf scrolling issues"
        );
    }

    /// Test baseline adjustment functionality that helps with the scrolling issue
    #[test]
    fn test_issue_1071_baseline_adjustment_consistency() {
        let font = FaceMetrics {
            cell_width: 10.0,
            ascent: 12.0,
            descent: 3.0,
            line_gap: 1.0,
            underline_position: Some(-1.0),
            underline_thickness: Some(1.0),
            strikethrough_position: Some(6.0),
            strikethrough_thickness: Some(1.0),
            cap_height: Some(9.0),
            ex_height: Some(6.0),
            ic_width: None,
        };

        let mut metrics = Metrics::calc(font);
        let original_baseline = metrics.get_baseline_adjustment();

        // Test that baseline adjustment works correctly when cell height changes
        // This is important for maintaining consistent text positioning
        let new_height = metrics.cell_height + 4;
        metrics.apply_cell_height_adjustment(new_height);

        let adjusted_baseline = metrics.get_baseline_adjustment();

        // Baseline should be adjusted to keep text centered
        assert_eq!(adjusted_baseline, original_baseline + 2.0);

        // Verify that text positioning remains consistent
        let (ascent, descent, _) = metrics.for_rich_text();
        assert_eq!(ascent + descent, new_height as f32);
    }

    /// Test CJK font size adjustment that prevents the scrolling issue
    #[test]
    fn test_issue_1071_cjk_font_size_normalization() {
        // Test the font size adjustment that normalizes CJK fonts with Latin fonts
        let latin_font = FaceMetrics {
            cell_width: 9.0,
            ascent: 11.0,
            descent: 2.5,
            line_gap: 0.8,
            underline_position: Some(-1.0),
            underline_thickness: Some(1.0),
            strikethrough_position: Some(5.5),
            strikethrough_thickness: Some(1.0),
            cap_height: Some(8.5),
            ex_height: Some(5.5),
            ic_width: Some(18.0), // Measured CJK width in Latin font
        };

        let cjk_font = FaceMetrics {
            cell_width: 18.0,
            ascent: 14.0,
            descent: 3.5,
            line_gap: 1.2,
            underline_position: Some(-1.5),
            underline_thickness: Some(1.2),
            strikethrough_position: Some(7.0),
            strikethrough_thickness: Some(1.2),
            cap_height: Some(10.5),
            ex_height: Some(7.0),
            ic_width: Some(36.0), // Measured CJK width in CJK font
        };

        // Calculate font size adjustment
        let size_adjustment =
            Metrics::calculate_cjk_font_size_adjustment(&latin_font, &cjk_font);

        assert!(
            size_adjustment.is_some(),
            "Font size adjustment should be calculated"
        );

        let adjustment_ratio = size_adjustment.unwrap();
        // Should use ic_width ratio: 18.0 / 36.0 = 0.5
        assert!((adjustment_ratio - 0.5).abs() < 0.001);

        // This adjustment helps normalize the fonts so they work together consistently
        // preventing the line height mismatches that caused the scrolling issue
    }

    /// Integration test that verifies the complete fix for issue #1071
    #[test]
    fn test_issue_1071_complete_fix_integration() {
        // This test simulates the complete scenario described in issue #1071

        // Primary Latin font (user's main font)
        let latin_font = FaceMetrics {
            cell_width: 10.0,
            ascent: 12.0,
            descent: 3.0,
            line_gap: 1.0,
            underline_position: Some(-1.0),
            underline_thickness: Some(1.0),
            strikethrough_position: Some(6.0),
            strikethrough_thickness: Some(1.0),
            cap_height: Some(9.0),
            ex_height: Some(6.0),
            ic_width: None,
        };

        // CJK fallback font (causing the original issue)
        let cjk_font = FaceMetrics {
            cell_width: 20.0,
            ascent: 15.0,
            descent: 4.0,
            line_gap: 2.0,
            underline_position: Some(-2.0),
            underline_thickness: Some(1.5),
            strikethrough_position: Some(7.5),
            strikethrough_thickness: Some(1.5),
            cap_height: Some(11.0),
            ex_height: Some(7.5),
            ic_width: Some(40.0),
        };

        // Apply the fix: use consistent metrics approach
        let primary_metrics = Metrics::calc(latin_font);
        let cjk_metrics =
            Metrics::calc_with_primary_cell_dimensions(cjk_font, &primary_metrics);

        // Verify all the key aspects of the fix:

        // 1. Consistent cell dimensions (prevents scrolling calculation errors)
        assert_eq!(primary_metrics.cell_height, cjk_metrics.cell_height);
        assert_eq!(primary_metrics.cell_width, cjk_metrics.cell_width);
        assert_eq!(primary_metrics.cell_baseline, cjk_metrics.cell_baseline);

        // 2. Consistent rich text format (prevents rendering height mismatches)
        let (latin_ascent, latin_descent, latin_leading) =
            primary_metrics.for_rich_text();
        let (cjk_ascent, cjk_descent, cjk_leading) = cjk_metrics.for_rich_text();

        assert_eq!(latin_ascent, cjk_ascent);
        assert_eq!(latin_descent, cjk_descent);
        assert_eq!(latin_leading, cjk_leading);

        // 3. Baseline adjustment works correctly
        assert_eq!(
            primary_metrics.get_baseline_adjustment(),
            cjk_metrics.get_baseline_adjustment()
        );

        // 4. Terminal grid calculations will be consistent
        let terminal_rows = 24;
        let content_lines = 30;
        let scroll_lines = content_lines - terminal_rows;

        let latin_scroll_height = scroll_lines * primary_metrics.cell_height;
        let cjk_scroll_height = scroll_lines * cjk_metrics.cell_height;

        assert_eq!(latin_scroll_height, cjk_scroll_height);

        // 5. Font-specific positioning is preserved (underline, strikethrough)
        // CJK font can have different positioning while maintaining grid consistency
        // This allows proper rendering while preventing scrolling issues
        assert_eq!(cjk_metrics.cell_height, primary_metrics.cell_height);
        // But positioning details can differ (this is intentional and correct)
    }

    /// Test edge case: mixed content with alternating Latin and CJK characters
    #[test]
    fn test_issue_1071_mixed_content_consistency() {
        let latin_font = FaceMetrics {
            cell_width: 8.0,
            ascent: 10.0,
            descent: 2.0,
            line_gap: 0.5,
            underline_position: Some(-1.0),
            underline_thickness: Some(1.0),
            strikethrough_position: Some(5.0),
            strikethrough_thickness: Some(1.0),
            cap_height: Some(7.5),
            ex_height: Some(5.0),
            ic_width: None,
        };

        let cjk_font = FaceMetrics {
            cell_width: 16.0,
            ascent: 12.0,
            descent: 3.0,
            line_gap: 1.0,
            underline_position: Some(-1.5),
            underline_thickness: Some(1.2),
            strikethrough_position: Some(6.0),
            strikethrough_thickness: Some(1.2),
            cap_height: Some(9.0),
            ex_height: Some(6.0),
            ic_width: Some(32.0),
        };

        let latin_metrics = Metrics::calc(latin_font);
        let cjk_metrics =
            Metrics::calc_with_primary_cell_dimensions(cjk_font, &latin_metrics);

        // Simulate a line with mixed content: "Hello 世界 World"
        // Each character should occupy the same cell height regardless of font
        let mixed_line_height = latin_metrics.cell_height; // Latin chars
        let cjk_line_height = cjk_metrics.cell_height; // CJK chars

        assert_eq!(
            mixed_line_height, cjk_line_height,
            "Mixed content lines must have consistent height"
        );

        // Verify that a terminal line containing mixed content
        // has predictable and consistent height
        let line_count = 5;
        let total_height_latin = line_count * latin_metrics.cell_height;
        let total_height_mixed = line_count * cjk_metrics.cell_height;

        assert_eq!(
            total_height_latin, total_height_mixed,
            "Mixed content must not affect total height calculations"
        );
    }

    /// Test edge cases for font metrics calculations
    #[test]
    fn test_edge_cases() {
        // Test extremely small font size
        let tiny_font = FaceMetrics {
            cell_width: 0.1,
            ascent: 0.1,
            descent: 0.05,
            line_gap: 0.01,
            underline_position: Some(-0.01),
            underline_thickness: Some(0.01),
            strikethrough_position: Some(0.05),
            strikethrough_thickness: Some(0.01),
            cap_height: Some(0.08),
            ex_height: Some(0.05),
            ic_width: None,
        };

        let tiny_metrics = Metrics::calc(tiny_font);
        assert!(
            tiny_metrics.cell_width >= 1,
            "Cell width must be at least 1 pixel"
        );
        assert!(
            tiny_metrics.cell_height >= 1,
            "Cell height must be at least 1 pixel"
        );
        assert!(
            tiny_metrics.underline_thickness >= 1,
            "Line thickness must be at least 1 pixel"
        );

        // Test extremely large font size
        let huge_font = FaceMetrics {
            cell_width: 1000.0,
            ascent: 1200.0,
            descent: 300.0,
            line_gap: 100.0,
            underline_position: Some(-100.0),
            underline_thickness: Some(100.0),
            strikethrough_position: Some(600.0),
            strikethrough_thickness: Some(100.0),
            cap_height: Some(900.0),
            ex_height: Some(600.0),
            ic_width: None,
        };

        let huge_metrics = Metrics::calc(huge_font);
        assert_eq!(
            huge_metrics.cell_width, 1000,
            "Large font metrics should be preserved"
        );
        assert_eq!(
            huge_metrics.cell_height, 1700,
            "Large font height calculation"
        );

        // Test font with missing optional metrics
        let minimal_font = FaceMetrics {
            cell_width: 10.0,
            ascent: 12.0,
            descent: 3.0,
            line_gap: 1.0,
            underline_position: None,
            underline_thickness: None,
            strikethrough_position: None,
            strikethrough_thickness: None,
            cap_height: None,
            ex_height: None,
            ic_width: None,
        };

        let minimal_metrics = Metrics::calc(minimal_font);
        assert!(
            minimal_metrics.underline_thickness >= 1,
            "Default underline thickness"
        );
        assert!(
            minimal_metrics.strikethrough_thickness >= 1,
            "Default strikethrough thickness"
        );
        assert!(
            minimal_metrics.underline_position > 0,
            "Default underline position"
        );
        assert!(
            minimal_metrics.strikethrough_position > 0,
            "Default strikethrough position"
        );
    }

    /// Test mixed script rendering (Latin + CJK + Emoji)
    #[test]
    fn test_mixed_script_rendering() {
        let latin_font = TestFontData::cascadia_code();
        let cjk_font = TestFontData::noto_sans_cjk();
        let emoji_font = TestFontData::noto_color_emoji();

        let latin_metrics = Metrics::calc(latin_font.face);
        let cjk_metrics =
            Metrics::calc_with_primary_cell_dimensions(cjk_font.face, &latin_metrics);
        let emoji_metrics =
            Metrics::calc_with_primary_cell_dimensions(emoji_font.face, &latin_metrics);

        // All fonts should have the same cell height for consistent rendering
        assert_eq!(latin_metrics.cell_height, cjk_metrics.cell_height);
        assert_eq!(latin_metrics.cell_height, emoji_metrics.cell_height);

        // All fonts should have the same baseline
        assert_eq!(latin_metrics.cell_baseline, cjk_metrics.cell_baseline);
        assert_eq!(latin_metrics.cell_baseline, emoji_metrics.cell_baseline);

        // Verify that a line containing all three scripts renders consistently
        let mixed_line_height = latin_metrics.cell_height;
        let latin_only_height = latin_metrics.cell_height;
        let cjk_only_height = cjk_metrics.cell_height;
        let emoji_only_height = emoji_metrics.cell_height;

        assert_eq!(
            mixed_line_height, latin_only_height,
            "Mixed script lines must have same height as single script lines"
        );
        assert_eq!(mixed_line_height, cjk_only_height);
        assert_eq!(mixed_line_height, emoji_only_height);
    }
}