retroglyph-core 0.6.0

A 2D pseudographic terminal library -- core types, no backend
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
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
//! Cross-grid copies: [`Grid::blit`], [`Grid::blit_alpha`], and [`Grid::blit_cross_layer`], built
//! on the shared [`Grid::blit_with`] copy loop.
//!
//! The [`BlendMode`] blend math backing [`Grid::blit_alpha`] lives here too, next to its only
//! caller.

#[cfg(test)]
use super::super::Pos;
use super::super::{BlendMode, Grid, Rect, TileExtra};
use crate::color::Color;
#[cfg(test)]
use crate::color::{Style, Tint};
use crate::tile::{Tile, TileFlags};
use alloc::vec::Vec;
use alpha_blend::BlendMode as SeparableBlendMode;
use alpha_blend::channel::Channel;

impl Grid {
    /// Copies tiles from `src` within `src_rect` to `self` at `(dst_x, dst_y)`
    /// on `layer`. Empty tiles (nothing written; see [`Tile::is_empty`]) are
    /// treated as transparent and skipped. An explicit space is copied and
    /// overwrites the destination.
    ///
    /// Multi-cell spans (see [`write_span`](Self::write_span)) do **not** survive a blit: copied
    /// tiles keep their glyphs but lose [`TileFlags::SPAN_ANCHOR`]/[`TileFlags::SPAN_COVERED`],
    /// so a span degrades to exactly its text fallback. `src_rect` can clip a span in half, and
    /// half a span is not a thing the grid can represent; degrading to the fallback glyphs is
    /// both representable and the same content a cell backend would have drawn anyway.
    ///
    /// The same is true of wide-character pairs: `src_rect` clipping a lead from its spacer, or
    /// the copy landing on only one half of a destination pair, both leave half a pair, which is
    /// equally unrepresentable. Either case strips [`TileFlags::WIDE_CHAR`]/
    /// [`TileFlags::WIDE_CHAR_SPACER`] from the surviving half (or clears the destination half
    /// the copy overwrites), so a blit can never leave a dangling lead or an orphaned spacer
    /// behind (retroglyph#1013).
    ///
    /// Walks `src`'s and `self`'s layer buffers directly by flat index instead of going through
    /// [`tile`](Self::tile)/[`put_tile`](Self::put_tile) per cell (see retroglyph#263):
    /// each of those recomputes a coordinate conversion and a bounds check per cell, which this
    /// does once per row instead. The destination layer is allocated once, up front, rather than
    /// as a side effect of the first written cell, but only if `src_rect` (clamped to `src`'s
    /// bounds) contains at least one non-empty tile, matching `put_tile`'s original
    /// allocate-on-first-write behavior for a `src_rect` that is entirely transparent.
    pub fn blit(&mut self, layer: u8, src: &Self, src_rect: Rect, dst_x: u16, dst_y: u16) {
        self.blit_with(
            layer,
            src,
            layer,
            src_rect,
            dst_x,
            dst_y,
            |tile, _dst_tile| *tile,
        );
    }

    /// Same as [`blit`](Self::blit) but blends foreground and background
    /// colors with the given alpha factors, using `mode` to compute the
    /// blended color. `fg_alpha` and `bg_alpha` are in 0.0-1.0 range where
    /// 0.0 = keep destination, 1.0 = replace with src; for a non-
    /// [`Linear`](BlendMode::Linear) `mode`, "replace with src" instead means
    /// "replace with `mode`'s fully blended color" (see [`BlendMode`]).
    ///
    /// Blending operates on packed RGB values; [`Color::Default`] preserves
    /// the destination. Non-RGB color variants (Ansi/Indexed) are passed
    /// through unblended, regardless of `mode`.
    ///
    /// [`BlendMode::Linear`]'s per-channel color lerp is delegated to [`gem::Mix`]. The other
    /// modes delegate to [`alpha_blend::BlendMode`] (imported in this module as
    /// `SeparableBlendMode` to avoid colliding with this crate's own [`BlendMode`]).
    ///
    /// Like [`blit`](Self::blit) (see retroglyph#262/#263), walks `src`'s and `self`'s layer
    /// buffers directly by flat index instead of per-cell [`tile`](Self::tile)/
    /// [`put_tile`](Self::put_tile), and allocates the destination layer once, up front, rather
    /// than as a side effect of the first written cell.
    #[allow(clippy::too_many_arguments, clippy::float_cmp)]
    pub fn blit_alpha(
        &mut self,
        layer: u8,
        src: &Self,
        src_rect: Rect,
        dst_x: u16,
        dst_y: u16,
        mode: BlendMode,
        fg_alpha: f32,
        bg_alpha: f32,
    ) {
        self.blit_with(
            layer,
            src,
            layer,
            src_rect,
            dst_x,
            dst_y,
            |tile, dst_tile| {
                let mut blended = *tile;
                // `fg_alpha == 1.0` only lets `Linear` skip the call: `Linear` at `t ==
                // 1.0` is `src` by definition, but a `Screen`/`Dodge`/`Burn`/`Overlay`
                // mix at full alpha still needs to run the mode's formula: it isn't
                // equivalent to the raw source color (see `blend_color`'s matching guard).
                if mode != BlendMode::Linear || fg_alpha != 1.0 {
                    blended.style.fg =
                        blend_color(mode, tile.style.fg, dst_tile.style.fg, fg_alpha);
                }
                if mode != BlendMode::Linear || bg_alpha != 1.0 {
                    blended.style.bg =
                        blend_color(mode, tile.style.bg, dst_tile.style.bg, bg_alpha);
                }
                blended
            },
        );
    }

    /// Same as [`blit`](Self::blit), except the source tiles are read from `src_layer` on `src`
    /// rather than from `dst_layer` (the layer this writes to on `self`).
    ///
    /// [`blit`](Self::blit) uses one `layer` for both sides, which is exactly right for two
    /// grids sharing the same layer scheme (e.g. [`Surface::on_layer`](crate::surface::Surface::on_layer)
    /// copying within itself), but wrong for [`Surface::blit`](crate::surface::Surface::blit)'s case: a
    /// `src` that is a standalone, layer-0-only `Grid` (composed content like `BoxStyle::render`'s
    /// output), stamped onto a destination surface that may currently be on any layer. Calling
    /// [`blit`](Self::blit) with the destination's layer there looks up that same layer on `src`,
    /// finds nothing (`src` only ever populated layer 0), and silently copies nothing
    /// (retroglyph#824). This method exists so a caller in that position can pin `src_layer` to
    /// `0` independently of `dst_layer`.
    pub(crate) fn blit_cross_layer(
        &mut self,
        dst_layer: u8,
        src: &Self,
        src_layer: u8,
        src_rect: Rect,
        dst_x: u16,
        dst_y: u16,
    ) {
        self.blit_with(
            dst_layer,
            src,
            src_layer,
            src_rect,
            dst_x,
            dst_y,
            |tile, _dst_tile| *tile,
        );
    }

    /// Shared copy loop behind [`blit`](Self::blit), [`blit_alpha`](Self::blit_alpha), and
    /// [`blit_cross_layer`](Self::blit_cross_layer): clamps `src_rect` to `src`'s bounds, skips
    /// the whole call if nothing in it is visible, clears any destination span or wide-character
    /// pair the copy is about to partially overwrite (retroglyph#710, retroglyph#1013), and walks
    /// matching `src`/destination cells by
    /// flat index (retroglyph#262/#263), applying `transform` to each non-empty source tile
    /// (given the source tile and, for context, the destination tile it's about to replace)
    /// before writing it and fixing up grapheme extras. `dst_x`/`dst_y` saturate on overflow
    /// (retroglyph#268) rather than wrapping; the bounds checks below always catch a saturated
    /// `u16::MAX` origin.
    ///
    /// `dst_layer` and `src_layer` are separate parameters (rather than the one `layer` [`blit`]
    /// and [`blit_alpha`] expose) so [`blit_cross_layer`](Self::blit_cross_layer) can read a
    /// different source layer than the one it writes: see that method's own doc for why (this is
    /// the retroglyph#824 fix).
    ///
    /// ```text
    ///   src (read from src_layer)              self (written at dst_layer)
    ///   +-----------------------+              +-----------------------+
    ///   |     src_rect          |              |                       |
    ///   |     +--------+        |  translate   |    (dst_x,dst_y)      |
    ///   |     |  A  B  |        |  by          |    +--------+         |
    ///   |     |  C  D..|........|. (dst_x -    |    |  A  B  |         |
    ///   |     +-----|--+   src  |   src_rect   |    |  C  D  |         |
    ///   |           |   bounds  |   origin)    |    +-----|--+  self   |
    ///   +-----------|-----------+              +----------|---bounds--+
    ///               clipped to src's edge                 clipped again to self's edge
    ///
    ///   Copied region = src_rect ∩ src bounds ∩ (self bounds shifted back by the offset).
    ///   Cells outside any of the three are skipped, never wrapped (dst_x/dst_y saturate).
    /// ```
    #[allow(clippy::too_many_arguments)]
    fn blit_with(
        &mut self,
        dst_layer: u8,
        src: &Self,
        src_layer: u8,
        src_rect: Rect,
        dst_x: u16,
        dst_y: u16,
        transform: impl Fn(&Tile, &Tile) -> Tile,
    ) {
        let Some(src_lb) = src.layer(src_layer) else {
            return;
        };
        let src_width = usize::from(src.width);
        let sx0 = src_rect.left().min(src.width);
        let sx1 = src_rect.right().min(src.width);
        let sy0 = src_rect.top().min(src.height);
        let sy1 = src_rect.bottom().min(src.height);
        if sx0 >= sx1 || sy0 >= sy1 {
            return;
        }

        // Matches the original's implicit allocate-on-first-write: only touch the destination
        // layer at all if there's at least one visible (non-empty) source tile to copy.
        let has_visible = (sy0..sy1).any(|sy| {
            let start = usize::from(sy) * src_width + usize::from(sx0);
            let end = usize::from(sy) * src_width + usize::from(sx1);
            src_lb.buf.as_ref()[start..end]
                .iter()
                .any(|t| !t.flags.contains(TileFlags::EMPTY))
        });
        if !has_visible {
            return;
        }

        let dst_width = usize::from(self.width);
        let dst_height = usize::from(self.height);

        // A blit writes straight into the destination buffer below, bypassing `put_tile`, so it
        // has to do `put_tile`'s `clear_span_overlap`/`clear_overlap` calls itself, or a cell that
        // used to anchor (or be covered by) a multi-cell span, or half of a wide-character pair,
        // would keep claiming cells this blit just overwrote (retroglyph#710, retroglyph#1013).
        // Only the cells actually being overwritten (in bounds, non-empty source tile) are
        // cleared: an empty source tile is transparent and leaves the destination untouched, so
        // clearing a whole row's footprint up front would wipe out spans/pairs the blit never
        // actually touches. `clear_span_overlap` is gated on `has_spans` so a grid that never uses
        // spans pays only the one `bool` check; `clear_overlap` has no such gate because `put_tile`
        // itself never gates it (`WIDE_CHAR`/`WIDE_CHAR_SPACER` are set on every feature
        // combination, not just under `egc`).
        for sy in sy0..sy1 {
            let dy = dst_y.saturating_add(sy - src_rect.top());
            if usize::from(dy) >= dst_height {
                continue;
            }
            for sx in sx0..sx1 {
                let dx = dst_x.saturating_add(sx - src_rect.left());
                if usize::from(dx) >= dst_width {
                    continue;
                }
                let src_idx = usize::from(sy) * src_width + usize::from(sx);
                if src_lb.buf.as_ref()[src_idx]
                    .flags
                    .contains(TileFlags::EMPTY)
                {
                    continue;
                }
                if self.has_spans {
                    self.clear_span_overlap(dst_layer, dx, dy, 1);
                }
                self.clear_overlap(dst_layer, dx, dy, 1);
            }
        }

        let dst_lb = self.layer_or_alloc(dst_layer);
        let mut pending_extras: Vec<(usize, TileExtra)> = Vec::new();

        for sy in sy0..sy1 {
            let dy = dst_y.saturating_add(sy - src_rect.top());
            if usize::from(dy) >= dst_height {
                continue;
            }
            for sx in sx0..sx1 {
                let dx = dst_x.saturating_add(sx - src_rect.left());
                if usize::from(dx) >= dst_width {
                    continue;
                }
                let src_idx = usize::from(sy) * src_width + usize::from(sx);
                let tile = &src_lb.buf.as_ref()[src_idx];
                if tile.flags.contains(TileFlags::EMPTY) {
                    continue;
                }
                let dst_idx = usize::from(dy) * dst_width + usize::from(dx);
                let dst_tile = dst_lb.buf.as_ref()[dst_idx];
                let mut out_tile = transform(tile, &dst_tile);
                out_tile.flags.remove(TileFlags::HAS_EXTRA);
                out_tile.clear_span();

                // Half a wide-character pair is as unrepresentable as half a span (see
                // `clear_span` above): `src_rect` or the destination clip can separate a lead
                // from its spacer, so drop the flag on whichever half survives the copy alone
                // rather than leave a dangling lead (no spacer to its right) or an orphaned
                // spacer (no lead to its left) (retroglyph#1013).
                if out_tile.flags.contains(TileFlags::WIDE_CHAR) {
                    let partner_survived = sx + 1 < sx1 && usize::from(dx) + 1 < dst_width;
                    if !partner_survived {
                        out_tile.clear_wide();
                    }
                } else if out_tile.flags.contains(TileFlags::WIDE_CHAR_SPACER) {
                    let partner_survived = sx > sx0 && dx > 0;
                    if !partner_survived {
                        out_tile.clear_wide();
                    }
                }
                dst_lb.buf.as_mut()[dst_idx] = out_tile;
                if tile.flags.contains(TileFlags::HAS_EXTRA) {
                    if let Some(extra) = src_lb.extra_entry_for(src_idx, tile) {
                        pending_extras.push((dst_idx, extra));
                    }
                } else {
                    dst_lb.extras.remove(&dst_idx);
                }
            }
        }

        for (idx, extra) in pending_extras {
            dst_lb.buf.as_mut()[idx].flags.insert(TileFlags::HAS_EXTRA);
            dst_lb.extras.insert(idx, extra);
        }
    }
}

/// Blends two [`Color`] values using `mode`. [`Color::Default`] preserves the
/// destination. Non-RGB source colors are returned as-is (no resolution).
///
/// [`BlendMode::Linear`] is a per-channel sRGB-domain lerp (dst -> src by `t`) delegated to
/// [`gem::Mix`], which is `no_std`-safe (round-half-away via `floor(x + 0.5)`, no `std`/`libm`
/// float intrinsics). The other modes evaluate [`SeparableBlendMode::mix`] per channel in
/// `0.0..=1.0` (converting u8 <-> f32 at the boundary; see [`blend_separable_channel`]), then lerp
/// that fully mixed color against the destination by `t`, same as `Linear`.
#[allow(clippy::float_cmp)]
fn blend_color(mode: BlendMode, src: Color, dst: Color, t: f32) -> Color {
    use gem::Mix as _;
    use gem::rgb::{HasBlue as _, HasGreen as _, HasRed as _, Rgb888};
    match (src, dst) {
        (Color::Default, _) => Color::Default,
        (
            Color::Rgb {
                r: sr,
                g: sg,
                b: sb,
            },
            Color::Rgb {
                r: dr,
                g: dg,
                b: db,
            },
        ) if mode != BlendMode::Linear || t != 1.0 => {
            // `Linear` at `t == 1.0` is `src` by definition (skip to the catch-all arm below);
            // the other modes must still run their mix formula at `t == 1.0`: see `blit_alpha`.
            let (r, g, b) = mode.separable().map_or_else(
                || {
                    // `dst.mix(src, t)`, not `src.mix(dst, t)`: at `t == 0.0` this must return
                    // `dst` ("keep destination", per `blit_alpha`'s doc comment) and only reach
                    // `src` at `t == 1.0`: the same `0.0 == dst, 1.0 == fully blended` contract
                    // every other `BlendMode` follows (see `blend_separable_channel`).
                    let out = Rgb888::from_rgb(dr, dg, db).mix(Rgb888::from_rgb(sr, sg, sb), t);
                    (out.red(), out.green(), out.blue())
                },
                |sep| {
                    (
                        blend_separable_channel(sep, sr, dr, t),
                        blend_separable_channel(sep, sg, dg, t),
                        blend_separable_channel(sep, sb, db, t),
                    )
                },
            );
            Color::Rgb { r, g, b }
        }
        (src, _) => src,
    }
}

/// Evaluates `sep`'s per-channel mixing function for one RGB channel (`src`/`dst` are u8, `sep`
/// operates in `0.0..=1.0` f32), then lerps that mixed value against `dst` by `t`: `0.0` keeps
/// `dst`, `1.0` uses the fully mixed color. Clamps before converting back to u8 via
/// `Channel::from_f32`, since `ColorDodge`/`ColorBurn`'s `min(1.0, ...)` branches can round a
/// hair outside `0.0..=1.0` at the float boundary.
fn blend_separable_channel(sep: SeparableBlendMode, src: u8, dst: u8, t: f32) -> u8 {
    let cs = Channel::to_f32(src);
    let cb = Channel::to_f32(dst);
    let mixed = sep.mix(cb, cs);
    // A plain multiply-add measurably disagrees with a fused one (`crate::math::mul_add`) by
    // ±1 LSB on some inputs.
    let blended = crate::math::mul_add(mixed - cb, t, cb);
    Channel::from_f32(blended.clamp(0.0, 1.0))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "egc")]
    #[test]
    fn blit_carries_a_tint_across_grids() {
        let mut src = Grid::new(4, 4);
        src.write_grapheme(0, 1, 1, "@", Style::default());
        src.set_tint(0, 1, 1, Tint::multiply(64, 128, 192));

        let mut dst = Grid::new(4, 4);
        // Pre-existing tint on the destination cell, to prove the copy replaces rather than
        // merges with whatever was there.
        dst.set_tint(0, 1, 1, Tint::mix(9, 9, 9, 9));
        dst.blit(0, &src, Rect::new(0, 0, 4, 4), 0, 0);

        assert_eq!(dst.tint(0, 1, 1), Tint::multiply(64, 128, 192));
        assert_eq!(dst.tint(0, 0, 0), Tint::None);
    }

    #[cfg(feature = "egc")]
    #[test]
    fn blit_clears_a_destination_tint_where_the_source_has_none() {
        let mut src = Grid::new(2, 2);
        src.write_grapheme(0, 0, 0, "@", Style::default());

        let mut dst = Grid::new(2, 2);
        dst.set_tint(0, 0, 0, Tint::multiply(1, 2, 3));
        dst.blit(0, &src, Rect::new(0, 0, 2, 2), 0, 0);

        assert_eq!(dst.tint(0, 0, 0), Tint::None);
    }

    #[cfg(feature = "egc")]
    #[test]
    fn blit_preserves_extra() {
        let mut src = Grid::new(2, 2);
        src.write_grapheme(0, 0, 0, "e\u{0301}", Style::default());

        let mut dst = Grid::new(2, 2);
        dst.blit(0, &src, Rect::new(0, 0, 2, 2), 0, 0);
        assert_eq!(dst[Pos::new(0, 0)].glyph, 'e');
        assert_eq!(crate::grid::grapheme_at(&dst, 0, 0, 0), Some("e\u{0301}"));
    }

    #[test]
    fn blit_empty_rect_is_a_no_op() {
        // A zero-area `src_rect` has no cells at all: `sx0 >= sx1` should short-circuit before
        // touching the destination.
        let src = Grid::new(2, 2);
        let mut dst = Grid::new(2, 2);
        dst.put_tile(0, (0, 0), Tile::new('x', Style::default()));
        dst.blit(0, &src, Rect::new(0, 0, 0, 0), 0, 0);
        assert_eq!(dst[Pos::new(0, 0)].glyph(), 'x');
        assert_eq!(dst.max_layer(), 0);
    }

    #[test]
    fn blit_fully_transparent_source_does_not_allocate_dst_layer() {
        // Perf refactor (#263): the destination layer is allocated up front, but only after
        // confirming the (clamped) source region has at least one non-empty tile, matching
        // `put_tile`'s original allocate-on-first-write behavior for an all-transparent blit.
        let src = Grid::new(2, 2);
        let mut dst = Grid::new(2, 2);
        dst.blit(3, &src, Rect::new(0, 0, 2, 2), 0, 0);
        assert_eq!(dst.max_layer(), 0);
    }

    #[test]
    fn blit_skips_out_of_bounds_source_and_dest_regions() {
        let mut src = Grid::new(4, 4);
        for y in 0..4 {
            for x in 0..4 {
                src.put_tile(0, (x, y), Tile::new('#', Style::default()));
            }
        }

        let mut dst = Grid::new(2, 2);
        // `src_rect` extends past `src`'s bounds and the destination offset pushes part of the
        // copied region past `dst`'s bounds too; both should be silently clamped, not panic.
        dst.blit(0, &src, Rect::new(2, 2, 10, 10), 1, 1);
        assert_eq!(dst[Pos::new(1, 1)].glyph(), '#');
        assert_eq!(dst[Pos::new(0, 0)].glyph(), ' ');
        assert_eq!(dst[Pos::new(0, 1)].glyph(), ' ');
        assert_eq!(dst[Pos::new(1, 0)].glyph(), ' ');
    }

    #[test]
    fn blit_sub_cell_offset_and_transparency() {
        let mut src = Grid::new(2, 2);
        src.put_tile(0, (0, 0), Tile::new('A', Style::default()));
        // (1, 0) and (1, 1) stay at their default (empty) tile: transparent, should not
        // overwrite the destination.
        src.put_tile(0, (0, 1), Tile::new('B', Style::default()));

        let mut dst = Grid::new(3, 3);
        dst.put_tile(0, (2, 2), Tile::new('Z', Style::default()));
        dst.blit(0, &src, Rect::new(0, 0, 2, 2), 1, 1);

        assert_eq!(dst[Pos::new(1, 1)].glyph(), 'A');
        assert_eq!(dst[Pos::new(1, 2)].glyph(), 'B');
        // Untouched by the (transparent) source cells at (1, 0) and (1, 1).
        assert_eq!(dst[Pos::new(2, 1)].glyph(), ' ');
        assert_eq!(dst[Pos::new(2, 2)].glyph(), 'Z');
    }

    #[test]
    fn blit_multi_layer_independent() {
        let mut src = Grid::new(2, 2);
        src.put_tile(0, (0, 0), Tile::new('a', Style::default()));
        src.put_tile(2, (0, 0), Tile::new('b', Style::default()));

        let mut dst = Grid::new(2, 2);
        dst.blit(0, &src, Rect::new(0, 0, 2, 2), 0, 0);
        dst.blit(2, &src, Rect::new(0, 0, 2, 2), 0, 0);

        assert_eq!(dst.tile(0, (0, 0)).map(Tile::glyph), Some('a'));
        assert_eq!(dst.tile(2, (0, 0)).map(Tile::glyph), Some('b'));
        // Layer 1 was never written by either blit call.
        assert!(dst.tile(1, (0, 0)).is_none());
    }

    #[test]
    fn blit_dest_origin_near_u16_max_does_not_wrap() {
        // retroglyph#268: with a plain (non-saturating) `dst_x + (sx - src_rect.left())`, an
        // origin this close to `u16::MAX` overflows and wraps back into a small, in-bounds
        // value: silently corrupting an unrelated cell instead of being clamped out. Picked so
        // that `dst_x + 3` overflows `u16` and wraps to `1`, which *is* in-bounds for this small
        // `dst` grid: `65534u16.wrapping_add(3) == 1`.
        let mut src = Grid::new(4, 1);
        src.put_tile(0, (3, 0), Tile::new('Q', Style::default()));

        let mut dst = Grid::new(4, 1);
        dst.blit(0, &src, Rect::new(0, 0, 4, 1), u16::MAX - 1, 0);

        // The would-be-wrapped cell (index 1) must not have been touched.
        assert_eq!(dst[Pos::new(1, 0)].glyph(), ' ');
        // No other cell was touched either: the whole row's writes overflowed and were
        // skipped (dst_x saturates to u16::MAX for every column in this row).
        for x in 0..4 {
            assert_eq!(
                dst[Pos::new(x, 0)].glyph(),
                ' ',
                "cell ({x}, 0) unexpectedly written"
            );
        }
    }

    #[test]
    fn blit_normal_offset_unaffected_by_overflow_fix() {
        // A typical, non-overflowing blit must still work exactly as before.
        let mut src = Grid::new(2, 2);
        src.put_tile(0, (0, 0), Tile::new('A', Style::default()));
        src.put_tile(0, (1, 1), Tile::new('B', Style::default()));

        let mut dst = Grid::new(4, 4);
        dst.blit(0, &src, Rect::new(0, 0, 2, 2), 1, 1);

        assert_eq!(dst[Pos::new(1, 1)].glyph(), 'A');
        assert_eq!(dst[Pos::new(2, 2)].glyph(), 'B');
    }

    // --- `BlendMode` / `blit_alpha` ---
    #[test]
    fn blend_separable_channel_screen_lightens_toward_the_combined_color() {
        // cb = 102 (0.4), cs = 204 (0.8): screen = cb + cs - cb*cs = 0.88.
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::Screen, 204, 102, 1.0),
            224
        );
        // t = 0.5 lerps the destination halfway to that fully mixed color.
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::Screen, 204, 102, 0.5),
            163
        );
    }

    #[test]
    fn blend_separable_channel_dodge_brightens_toward_white() {
        // cb = 51 (0.2), cs = 204 (0.8): min(1, 0.2 / 0.2) saturates to 1.0.
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::ColorDodge, 204, 51, 1.0),
            255
        );
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::ColorDodge, 204, 51, 0.5),
            153
        );
    }

    #[test]
    fn blend_separable_channel_burn_darkens_toward_black() {
        // cb = 204 (0.8), cs = 51 (0.2): 1 - min(1, 0.2 / 0.2) bottoms out at 0.0.
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::ColorBurn, 51, 204, 1.0),
            0
        );
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::ColorBurn, 51, 204, 0.5),
            102
        );
    }

    #[test]
    fn blend_separable_channel_overlay_switches_between_multiply_and_screen() {
        // cb = 51 (0.2, the <= 0.5 branch): 2 * cb * cs.
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::Overlay, 204, 51, 1.0),
            82
        );
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::Overlay, 204, 51, 0.5),
            66
        );
        // cb = 204 (0.8, the > 0.5 branch): 1 - 2 * (1 - cb) * (1 - cs).
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::Overlay, 51, 204, 1.0),
            173
        );
    }

    #[test]
    fn blend_separable_channel_multiply_darkens_toward_the_product() {
        // cb = 204 (0.8), cs = 51 (0.2): multiply = cb * cs = 0.16.
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::Multiply, 204, 51, 1.0),
            41
        );
        // t = 0.5 lerps the destination halfway to that fully mixed color.
        assert_eq!(
            blend_separable_channel(SeparableBlendMode::Multiply, 204, 51, 0.5),
            46
        );
    }

    /// End-to-end through `blit_alpha`, not just the per-channel helper: proves `BlendMode`
    /// actually reaches `blend_color` and lands on the destination tile's style.
    #[test]
    fn blit_alpha_screen_blends_fg() {
        let mut src = Grid::new(1, 1);
        src.put_tile(
            0,
            (0, 0),
            Tile::default()
                .with_glyph('X')
                .with_style(Style::new().fg(Color::Rgb {
                    r: 204,
                    g: 204,
                    b: 204,
                })),
        );

        let mut dst = Grid::new(1, 1);
        dst.put_tile(
            0,
            (0, 0),
            Tile::default()
                .with_glyph('_')
                .with_style(Style::new().fg(Color::Rgb {
                    r: 102,
                    g: 102,
                    b: 102,
                })),
        );

        dst.blit_alpha(
            0,
            &src,
            Rect::new(0, 0, 1, 1),
            0,
            0,
            BlendMode::Screen,
            1.0,
            1.0,
        );
        assert_eq!(
            dst[Pos::new(0, 0)].style.fg,
            Color::Rgb {
                r: 224,
                g: 224,
                b: 224
            }
        );
    }

    /// Same as `blit_alpha_screen_blends_fg`, but for `style.bg` and `bg_alpha`: both
    /// alpha factors are independent, so `fg_alpha == 1.0` (fully mixed) and `bg_alpha == 0.5`
    /// (half-lerped toward the mix) must land different results on the two channels.
    #[test]
    fn blit_alpha_screen_blends_bg() {
        let mut src = Grid::new(1, 1);
        src.put_tile(
            0,
            (0, 0),
            Tile::default().with_glyph('X').with_style(
                Style::new()
                    .fg(Color::Rgb {
                        r: 204,
                        g: 204,
                        b: 204,
                    })
                    .bg(Color::Rgb {
                        r: 204,
                        g: 204,
                        b: 204,
                    }),
            ),
        );

        let mut dst = Grid::new(1, 1);
        dst.put_tile(
            0,
            (0, 0),
            Tile::default().with_glyph('_').with_style(
                Style::new()
                    .fg(Color::Rgb {
                        r: 102,
                        g: 102,
                        b: 102,
                    })
                    .bg(Color::Rgb {
                        r: 102,
                        g: 102,
                        b: 102,
                    }),
            ),
        );

        dst.blit_alpha(
            0,
            &src,
            Rect::new(0, 0, 1, 1),
            0,
            0,
            BlendMode::Screen,
            1.0,
            0.5,
        );
        // `fg_alpha == 1.0`: fully mixed, same as `blit_alpha_screen_blends_fg`.
        assert_eq!(
            dst[Pos::new(0, 0)].style.fg,
            Color::Rgb {
                r: 224,
                g: 224,
                b: 224
            }
        );
        // `bg_alpha == 0.5`: only half-lerped from the destination toward that same mix.
        assert_eq!(
            dst[Pos::new(0, 0)].style.bg,
            Color::Rgb {
                r: 163,
                g: 163,
                b: 163
            }
        );
    }

    /// retroglyph#268: same wraparound guard as `blit`'s
    /// `blit_dest_origin_near_u16_max_does_not_wrap`, but through `blit_alpha`'s
    /// separate `dst_x`/`dst_y` computation.
    #[test]
    fn blit_alpha_dest_origin_near_u16_max_does_not_wrap() {
        let mut src = Grid::new(4, 1);
        src.put_tile(0, (3, 0), Tile::new('Q', Style::default()));

        let mut dst = Grid::new(4, 1);
        dst.blit_alpha(
            0,
            &src,
            Rect::new(0, 0, 4, 1),
            u16::MAX - 1,
            0,
            BlendMode::Linear,
            1.0,
            1.0,
        );

        for x in 0..4 {
            assert_eq!(
                dst[Pos::new(x, 0)].glyph(),
                ' ',
                "cell ({x}, 0) unexpectedly written"
            );
        }
    }

    /// `BlendMode::Linear` at `t == 0.0` keeps the destination and at `t == 1.0` uses the source,
    /// matching `blit_alpha`'s doc comment. The underlying `gem::Mix` call takes its arguments in
    /// the opposite order, so this pins the direction against a silent `src`/`dst` swap.
    #[test]
    fn blit_alpha_linear_direction() {
        let mut src = Grid::new(1, 1);
        src.put_tile(
            0,
            (0, 0),
            Tile::default()
                .with_glyph('X')
                .with_style(Style::new().fg(Color::Rgb {
                    r: 255,
                    g: 255,
                    b: 255,
                })),
        );

        let dst_color = Color::Rgb { r: 0, g: 0, b: 0 };
        let at = |t: f32| {
            let mut dst = Grid::new(1, 1);
            dst.put_tile(
                0,
                (0, 0),
                Tile::default()
                    .with_glyph('_')
                    .with_style(Style::new().fg(dst_color)),
            );
            dst.blit_alpha(
                0,
                &src,
                Rect::new(0, 0, 1, 1),
                0,
                0,
                BlendMode::Linear,
                t,
                1.0,
            );
            dst[Pos::new(0, 0)].style.fg
        };

        assert_eq!(at(0.0), dst_color);
        assert_eq!(
            at(1.0),
            Color::Rgb {
                r: 255,
                g: 255,
                b: 255
            }
        );
        let Color::Rgb { r, g, b } = at(0.5) else {
            panic!("expected Color::Rgb");
        };
        assert!(r > 0 && r < 255, "expected a mid-gray, got {r}");
        assert_eq!(r, g);
        assert_eq!(g, b);
    }

    /// Every `BlendMode` preserves `Color::Default` and passes non-RGB colors through unblended,
    /// same as the pre-existing `Linear` behavior.
    #[test]
    fn blend_color_non_rgb_passthrough_all_modes() {
        for mode in [
            BlendMode::Linear,
            BlendMode::Screen,
            BlendMode::Dodge,
            BlendMode::Burn,
            BlendMode::Overlay,
            BlendMode::Multiply,
        ] {
            assert_eq!(
                blend_color(mode, Color::Default, Color::Rgb { r: 1, g: 2, b: 3 }, 0.5),
                Color::Default
            );
            assert_eq!(
                blend_color(mode, Color::BLACK, Color::WHITE, 0.5),
                Color::BLACK
            );
        }
    }

    #[test]
    fn blit_degrades_a_span_to_its_fallback_glyphs() {
        // `src_rect` can clip a footprint in half, and half a span is not representable, so
        // `blit` drops the span role and keeps the glyphs (which are the text fallback anyway).
        let mut src = Grid::new(4, 4);
        src.write_span(0, 0, 0, &["C=", "[]"], Style::default())
            .unwrap();

        let mut dst = Grid::new(4, 4);
        dst.blit(0, &src, Rect::new(0, 0, 2, 2), 0, 0);

        assert_eq!(dst[Pos::new(0, 0)].glyph(), 'C');
        assert_eq!(dst[Pos::new(1, 1)].glyph(), ']');
        assert_eq!(dst[Pos::new(0, 0)].span(), (1, 1));
        assert_eq!(dst.span_owner(0, 1, 1), None);
        for (x, y) in [(0, 0), (1, 0), (0, 1), (1, 1)] {
            let flags = dst[Pos::new(x, y)].flags();
            assert!(!flags.contains(TileFlags::SPAN_ANCHOR), "({x}, {y})");
            assert!(!flags.contains(TileFlags::SPAN_COVERED), "({x}, {y})");
        }
    }

    #[test]
    fn blit_leaves_a_dangling_span_anchor_in_the_destination() {
        // retroglyph#710: `blit` writes straight into the destination buffer, bypassing
        // `put_tile`'s `clear_span_overlap` call, so overwriting a span's covered cell used to
        // leave the anchor still claiming a cell the blit had just replaced.
        let mut dst = Grid::new(4, 1);
        dst.write_span(0, 0, 0, &["ab"], Style::default()).unwrap();

        let mut src = Grid::new(4, 1);
        src.put_tile(0, (1, 0), Tile::new('X', Style::default()));
        dst.blit(0, &src, Rect::new(1, 0, 1, 1), 1, 0);

        assert_eq!(dst[Pos::new(1, 0)].glyph(), 'X');
        assert_eq!(dst.tile(0, Pos::new(0, 0)).map(Tile::span), Some((1, 1)));
        assert!(!dst[Pos::new(0, 0)].flags().contains(TileFlags::SPAN_ANCHOR));
        assert!(
            !dst[Pos::new(1, 0)]
                .flags()
                .contains(TileFlags::SPAN_COVERED)
        );
    }

    #[test]
    fn blit_alpha_leaves_a_dangling_span_anchor_in_the_destination() {
        // Same bug as `blit_leaves_a_dangling_span_anchor_in_the_destination`, but through
        // `blit_alpha`'s separate copy path.
        let mut dst = Grid::new(4, 1);
        dst.write_span(0, 0, 0, &["ab"], Style::default()).unwrap();

        let mut src = Grid::new(4, 1);
        src.put_tile(0, (1, 0), Tile::new('X', Style::default()));
        dst.blit_alpha(
            0,
            &src,
            Rect::new(1, 0, 1, 1),
            1,
            0,
            BlendMode::Linear,
            1.0,
            1.0,
        );

        assert_eq!(dst[Pos::new(1, 0)].glyph(), 'X');
        assert_eq!(dst.tile(0, Pos::new(0, 0)).map(Tile::span), Some((1, 1)));
    }

    /// `blit_cross_layer` (used by `Surface::blit` for the retroglyph#824 fix) reads a fixed
    /// `src_layer` regardless of the layer it writes to, unlike `blit`'s single shared `layer`.
    #[test]
    fn blit_cross_layer_reads_a_different_source_layer_than_it_writes() {
        let mut src = Grid::new(2, 2);
        // Only layer 0 is ever populated on `src` (a standalone, layer-0-only composed grid).
        src.put_tile(0, (0, 0), Tile::new('S', Style::default()));

        let mut dst = Grid::new(2, 2);
        dst.blit_cross_layer(3, &src, 0, Rect::new(0, 0, 2, 2), 0, 0);

        // Written to layer 3 on `dst`, even though it was read from layer 0 on `src`.
        assert_eq!(dst.tile(3, (0, 0)).map(Tile::glyph), Some('S'));
        // Layer 0 is always allocated but untouched by this call: still its default tile.
        assert_eq!(dst[Pos::new(0, 0)].glyph(), ' ');
    }

    #[test]
    fn blit_leaves_a_dangling_wide_char_lead_in_the_destination() {
        // retroglyph#1013: `blit` writes straight into the destination buffer, bypassing
        // `put_tile`'s `clear_overlap` call, so overwriting a wide-character pair's spacer used
        // to leave the lead cell still claiming a spacer the blit had just replaced.
        let mut dst = Grid::new(4, 1);
        dst.put_tile(0, (0, 0), Tile::new('\u{4e2d}', Style::default()));

        let mut src = Grid::new(4, 1);
        src.put_tile(0, (1, 0), Tile::new('X', Style::default()));
        dst.blit(0, &src, Rect::new(1, 0, 1, 1), 1, 0);

        assert_eq!(dst[Pos::new(1, 0)].glyph(), 'X');
        assert!(!dst[Pos::new(0, 0)].flags().contains(TileFlags::WIDE_CHAR));
    }

    #[test]
    fn blit_alpha_leaves_a_dangling_wide_char_lead_in_the_destination() {
        // Same bug as `blit_leaves_a_dangling_wide_char_lead_in_the_destination`, but through
        // `blit_alpha`'s separate copy path.
        let mut dst = Grid::new(4, 1);
        dst.put_tile(0, (0, 0), Tile::new('\u{4e2d}', Style::default()));

        let mut src = Grid::new(4, 1);
        src.put_tile(0, (1, 0), Tile::new('X', Style::default()));
        dst.blit_alpha(
            0,
            &src,
            Rect::new(1, 0, 1, 1),
            1,
            0,
            BlendMode::Linear,
            1.0,
            1.0,
        );

        assert_eq!(dst[Pos::new(1, 0)].glyph(), 'X');
        assert!(!dst[Pos::new(0, 0)].flags().contains(TileFlags::WIDE_CHAR));
    }

    #[test]
    fn blit_degrades_a_wide_char_pair_clipped_by_src_rect() {
        // `src_rect` can clip a wide-character pair in half, and half a pair is not
        // representable, so `blit` drops the `WIDE_CHAR` flag on the lead it does copy, the same
        // way it already degrades a clipped span (retroglyph#1013).
        let mut src = Grid::new(4, 1);
        src.put_tile(0, (0, 0), Tile::new('\u{4e2d}', Style::default()));

        let mut dst = Grid::new(4, 1);
        dst.blit(0, &src, Rect::new(0, 0, 1, 1), 0, 0);

        assert!(!dst[Pos::new(0, 0)].flags().contains(TileFlags::WIDE_CHAR));
    }

    #[test]
    fn blit_alpha_degrades_a_wide_char_pair_clipped_by_src_rect() {
        // Same bug as `blit_degrades_a_wide_char_pair_clipped_by_src_rect`, but through
        // `blit_alpha`'s separate copy path.
        let mut src = Grid::new(4, 1);
        src.put_tile(0, (0, 0), Tile::new('\u{4e2d}', Style::default()));

        let mut dst = Grid::new(4, 1);
        dst.blit_alpha(
            0,
            &src,
            Rect::new(0, 0, 1, 1),
            0,
            0,
            BlendMode::Linear,
            1.0,
            1.0,
        );

        assert!(!dst[Pos::new(0, 0)].flags().contains(TileFlags::WIDE_CHAR));
    }

    #[test]
    fn blit_copies_a_whole_wide_char_pair_intact() {
        // The lead-clip and spacer-clip tests above both exercise the `!partner_survived` half of
        // `blit_with`'s wide-pair check; this covers the other half, where `src_rect` includes
        // both halves and neither flag should be stripped.
        let mut src = Grid::new(4, 1);
        src.put_tile(0, (0, 0), Tile::new('\u{4e2d}', Style::default()));

        let mut dst = Grid::new(4, 1);
        dst.blit(0, &src, Rect::new(0, 0, 2, 1), 0, 0);

        assert!(dst[Pos::new(0, 0)].flags().contains(TileFlags::WIDE_CHAR));
        assert!(
            dst[Pos::new(1, 0)]
                .flags()
                .contains(TileFlags::WIDE_CHAR_SPACER)
        );
    }

    #[test]
    fn blit_degrades_a_bare_wide_char_spacer_clipped_by_src_rect() {
        // The spacer twin of `blit_degrades_a_wide_char_pair_clipped_by_src_rect`: `src_rect` can
        // just as easily clip out the lead and leave the spacer, which is equally unrepresentable
        // on its own, so `blit` drops `WIDE_CHAR_SPACER` on the spacer it does copy.
        let mut src = Grid::new(4, 1);
        src.put_tile(0, (0, 0), Tile::new('\u{4e2d}', Style::default()));

        let mut dst = Grid::new(4, 1);
        dst.blit(0, &src, Rect::new(1, 0, 1, 1), 1, 0);

        assert!(
            !dst[Pos::new(1, 0)]
                .flags()
                .contains(TileFlags::WIDE_CHAR_SPACER)
        );
    }

    #[test]
    fn blit_degrades_a_wide_char_pair_clipped_by_the_destinations_edge() {
        // retroglyph#1087: unlike the `_clipped_by_src_rect` tests above, `src_rect` here covers
        // the *whole* pair (both halves are present and in-bounds on the source side); it's the
        // destination that's too narrow to hold both, so the lead lands in the last dst column
        // with no room for a spacer beside it. `blit_with`'s `partner_survived` check for the
        // `WIDE_CHAR` half includes `usize::from(dx) + 1 < dst_width` precisely so this case
        // degrades the same way as a source-side clip, instead of writing a dangling lead with no
        // spacer.
        let mut src = Grid::new(4, 1);
        src.put_tile(0, (0, 0), Tile::new('\u{4e2d}', Style::default()));

        let mut dst = Grid::new(1, 1);
        dst.blit(0, &src, Rect::new(0, 0, 2, 1), 0, 0);

        assert!(!dst[Pos::new(0, 0)].flags().contains(TileFlags::WIDE_CHAR));
    }

    #[test]
    fn blit_alpha_degrades_a_wide_char_pair_clipped_by_the_destinations_edge() {
        // Same as `blit_degrades_a_wide_char_pair_clipped_by_the_destinations_edge`, but through
        // `blit_alpha`'s separate copy path.
        let mut src = Grid::new(4, 1);
        src.put_tile(0, (0, 0), Tile::new('\u{4e2d}', Style::default()));

        let mut dst = Grid::new(1, 1);
        dst.blit_alpha(
            0,
            &src,
            Rect::new(0, 0, 2, 1),
            0,
            0,
            BlendMode::Linear,
            1.0,
            1.0,
        );

        assert!(!dst[Pos::new(0, 0)].flags().contains(TileFlags::WIDE_CHAR));
    }

    /// A single `blit`-vs-`copy_rect_clamped` comparison case for
    /// `blit_clamp_matches_grixys_copy_rect_clamped_on_shared_clipped_rect_cases`.
    struct BlitClampCase {
        name: &'static str,
        src_w: u16,
        src_h: u16,
        dst_w: u16,
        dst_h: u16,
        src_rect: Rect,
        dst_x: u16,
        dst_y: u16,
    }

    /// Every source cell gets a unique glyph derived from its position, so a mismatch in the
    /// clamp/translate math (an off-by-one, a row misaligned after clipping, ...) shows up as the
    /// wrong letter landing in the wrong destination cell, not just a wrong cell count.
    fn blit_clamp_case_glyph_at(x: u16, y: u16, width: u16) -> char {
        let idx = u32::from(y) * u32::from(width) + u32::from(x);
        char::from_u32(u32::from(b'A') + idx).expect("case grids stay within 'A'..='Z'")
    }

    /// Runs one [`BlitClampCase`] through both `Grid::blit` and `grixy::ops::copy_rect_clamped`
    /// on an equivalent pair of plain `grixy::buf::GridBuf`s, and asserts the copied region
    /// agrees cell-for-cell.
    fn assert_blit_clamp_case(case: &BlitClampCase) {
        use grixy::buf::GridBuf;
        use grixy::ops::GridWrite as _;
        use grixy::transform::GridConvertExt as _;

        let mut rg_src = Grid::new(case.src_w, case.src_h);
        for y in 0..case.src_h {
            for x in 0..case.src_w {
                let glyph = blit_clamp_case_glyph_at(x, y, case.src_w);
                rg_src.put_tile(0, (x, y), Tile::default().with_glyph(glyph));
            }
        }
        let mut rg_dst = Grid::new(case.dst_w, case.dst_h);
        rg_dst.blit(0, &rg_src, case.src_rect, case.dst_x, case.dst_y);
        let rg_result: Vec<char> = (0..case.dst_h)
            .flat_map(|y| (0..case.dst_w).map(move |x| (x, y)))
            .map(|(x, y)| rg_dst.tile(0, (x, y)).map_or(' ', Tile::glyph))
            .collect();

        let mut gx_src = GridBuf::<char, _, _>::new_filled(
            usize::from(case.src_w),
            usize::from(case.src_h),
            ' ',
        );
        for y in 0..case.src_h {
            for x in 0..case.src_w {
                let glyph = blit_clamp_case_glyph_at(x, y, case.src_w);
                gx_src
                    .set(grixy::core::Pos::new(usize::from(x), usize::from(y)), glyph)
                    .unwrap();
            }
        }
        let mut gx_dst = GridBuf::<char, _, _>::new_filled(
            usize::from(case.dst_w),
            usize::from(case.dst_h),
            ' ',
        );
        grixy::ops::copy_rect_clamped(
            &gx_src.copied(),
            &mut gx_dst,
            grixy::core::Rect::from_ltwh(
                usize::from(case.src_rect.left()),
                usize::from(case.src_rect.top()),
                usize::from(case.src_rect.width()),
                usize::from(case.src_rect.height()),
            ),
            grixy::core::Pos::new(usize::from(case.dst_x), usize::from(case.dst_y)),
        );
        let (gx_result, _, _) = gx_dst.into_inner();

        assert_eq!(rg_result, gx_result, "case: {}", case.name);
    }

    /// `blit_with`'s clamp math (clamp `src_rect` to `src`'s bounds, translate into destination
    /// space, clamp again to `dst`'s bounds) is a hand-written copy of the algorithm
    /// `grixy::ops::copy_rect_clamped` generalizes (retroglyph#831). This walks a shared set of
    /// clipped-rect cases through both `Grid::blit` and `copy_rect_clamped` on an equivalent pair
    /// of plain `grixy::buf::GridBuf`s, and asserts the copied region agrees cell-for-cell, so the
    /// two can't silently drift apart. `Grid` can't implement `GridRead`/`GridWrite` itself (its
    /// span/extras bookkeeping has no equivalent there), so this compares outcomes rather than
    /// sharing code.
    #[test]
    fn blit_clamp_matches_grixys_copy_rect_clamped_on_shared_clipped_rect_cases() {
        let cases = [
            BlitClampCase {
                name: "fully inside both grids",
                src_w: 4,
                src_h: 4,
                dst_w: 6,
                dst_h: 6,
                src_rect: Rect::new(0, 0, 4, 4),
                dst_x: 1,
                dst_y: 1,
            },
            BlitClampCase {
                name: "src_rect wider than src (source-side clip)",
                src_w: 3,
                src_h: 3,
                dst_w: 5,
                dst_h: 5,
                src_rect: Rect::new(0, 0, 10, 10),
                dst_x: 0,
                dst_y: 0,
            },
            BlitClampCase {
                name: "destination-side clip",
                src_w: 3,
                src_h: 3,
                dst_w: 5,
                dst_h: 5,
                src_rect: Rect::new(0, 0, 3, 3),
                dst_x: 3,
                dst_y: 3,
            },
            BlitClampCase {
                name: "both sides clip, tighter bound wins",
                src_w: 4,
                src_h: 4,
                dst_w: 6,
                dst_h: 6,
                src_rect: Rect::new(0, 0, 10, 10),
                dst_x: 3,
                dst_y: 3,
            },
            BlitClampCase {
                name: "src_rect offset, clipped on src's right/bottom",
                src_w: 4,
                src_h: 4,
                dst_w: 6,
                dst_h: 6,
                src_rect: Rect::new(2, 2, 5, 5),
                dst_x: 0,
                dst_y: 0,
            },
            BlitClampCase {
                name: "source completely out of bounds",
                src_w: 3,
                src_h: 3,
                dst_w: 5,
                dst_h: 5,
                src_rect: Rect::new(5, 5, 2, 2),
                dst_x: 0,
                dst_y: 0,
            },
            BlitClampCase {
                name: "destination completely out of bounds",
                src_w: 3,
                src_h: 3,
                dst_w: 5,
                dst_h: 5,
                src_rect: Rect::new(0, 0, 3, 3),
                dst_x: 10,
                dst_y: 10,
            },
            BlitClampCase {
                name: "zero-size src_rect",
                src_w: 3,
                src_h: 3,
                dst_w: 5,
                dst_h: 5,
                src_rect: Rect::new(0, 0, 0, 0),
                dst_x: 0,
                dst_y: 0,
            },
        ];

        for case in &cases {
            assert_blit_clamp_case(case);
        }
    }
}