Skip to content

Reference

Canvas

Bases: Widget

A widget that renders a 2D canvas.

Source code in src/textual_hires_canvas/canvas.py
  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
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
class Canvas(Widget):
    """A widget that renders a 2D canvas."""

    @dataclass
    class Resize(Message):
        canvas: "Canvas"
        size: Size

    default_hires_mode: HiResMode

    _canvas_size: Size
    _canvas_region: Region
    _buffer: list[list[str]]
    _styles: list[list[str]]

    # Style cache to avoid reparsing identical style strings
    _style_cache: dict[str, Style] = {}

    def __init__(
        self,
        width: int = 40,
        height: int = 20,
        default_hires_mode: HiResMode | None = HiResMode.BRAILLE,
        name: str | None = None,
        id: str | None = None,
        classes: str | None = None,
        disabled: bool = False,
    ):
        """Initialize the Canvas widget.

        Args:
            width: The width of the canvas. Defaults to 40.
            height: The height of the canvas. Defaults to 20.
            default_hires_mode: The default high-resolution mode. Defaults to
                HiresMode.BRAILLE.
            name: The name of the widget. Defaults to None.
            id: The ID of the widget. Defaults to None.
            classes: The CSS classes of the widget. Defaults to None.
            disabled: Whether the widget is disabled. Defaults to False.
        """
        super().__init__(name=name, id=id, classes=classes, disabled=disabled)

        self._refreshes_pending: int = 0
        # reference count batch refreshes

        self._buffer = []
        self._styles = []
        self._canvas_size = Size(0, 0)
        self._canvas_region = Region()

        self.default_hires_mode = default_hires_mode or HiResMode.BRAILLE

        self.reset(size=Size(width, height), refresh=False)

    @contextmanager
    def batch_refresh(self) -> Iterator[None]:
        """Context manager that defers call to refresh until exiting the context.

        This is useful when making multiple changes to the canvas and only wanting
        to trigger refresh once at the end.

        Example:
            ```python
            with canvas.batch_changes():
                canvas.set_pixel(0, 0)
                canvas.set_pixel(1, 1)
                canvas.set_pixel(2, 2)
            # Refresh called
            ```

        Yields:
            Iterator[None]: A context manager.
        """
        self._refreshes_pending += 1
        try:
            yield
        finally:
            self._refreshes_pending -= 1
            if self._refreshes_pending == 0:
                self.refresh()

    @asynccontextmanager
    async def async_batch_refresh(self) -> AsyncIterator[None]:
        """Async context manager that defers call to refresh until exiting the context.

        This is useful when making multiple asynchronous changes to the canvas and only wanting
        to trigger refresh once at the end.

        Example:
                Yields:
        AsyncIterator[None]: An async context manager.
        """
        self._refreshes_pending += 1
        try:
            yield
        finally:
            self._refreshes_pending -= 1
            if self._refreshes_pending == 0:
                self.refresh()

    def refresh(
        self,
        *regions: Region,
        repaint: bool = True,
        layout: bool = False,
        recompose: bool = False,
    ) -> Self:
        if self._refreshes_pending:
            return self
        super().refresh(*regions, repaint=repaint, layout=layout, recompose=recompose)
        return self

    def _on_resize(self, event: Resize) -> None:
        self.post_message(self.Resize(canvas=self, size=event.size))

    def reset(self, size: Size | None = None, refresh: bool = True) -> None:
        """Resets the canvas to the specified size or to the current size if no size is provided.
        Clears buffers,styles and dirty cache, and resets the canvas size.

        Args:
            size: The new size for the canvas.
            refresh: Whether to refresh the canvas after resetting.
        Returns:
            self for chaining.
        """
        # Update size and regions if provided
        if size:
            self._canvas_size = size
            self._canvas_region = Region(0, 0, size.width, size.height)

        # Initialize buffers if we have a valid size
        if self._canvas_size:
            width = self._canvas_size.width
            height = self._canvas_size.height

            # More efficient buffer creation using list comprehension with multiplication
            # This is significantly faster than nested loops for large buffers
            self._buffer = [[" "] * width for _ in range(height)]
            self._styles = [[""] * width for _ in range(height)]

        # Only refresh if requested
        if refresh:
            self.refresh()

    def render_line(self, y: int) -> Strip:
        """Renders a single line of the canvas at the given y-coordinate.

        Args:
            y: The y-coordinate of the line.
        Returns:
            A Strip representing the line.
        """
        # Fast path for out-of-bounds or uninitialized
        if not self._canvas_size.area or y >= self._canvas_size.height:
            return Strip.blank(cell_length=0)

        buffer_line = self._buffer[y]
        styles_line = self._styles[y]

        # Fast path for blank lines
        if all(char == " " for char in buffer_line):
            return Strip.blank(cell_length=len(buffer_line))

        # Create segments with batching by style
        segments: list[Segment] = []
        append = segments.append  # Local reference for faster calls

        # Batch processing for same style segments
        current_style_str = None
        current_style_obj = None
        current_text = ""

        for char, style_str in zip(buffer_line, styles_line):
            # When style changes, add current batch and start new one
            if style_str != current_style_str:
                # Add current batch if it exists
                if current_text:
                    append(Segment(current_text, style=current_style_obj))

                # Start new batch
                current_style_str = style_str
                current_text = char

                # Get style object from cache or create new one
                if style_str:
                    if style_str not in self._style_cache:
                        self._style_cache[style_str] = Style.parse(style_str)
                    current_style_obj = self._style_cache[style_str]
                else:
                    current_style_obj = None
            else:
                # Add to current batch
                current_text += char

        # Add the final batch
        if current_text:
            append(Segment(current_text, style=current_style_obj))

        return Strip(segments).simplify()

    def get_pixel(self, x: int, y: int) -> tuple[str, str]:
        """Retrieves the character and style of a single pixel at the given coordinates.

        Args:
            x: The x-coordinate of the pixel.
            y: The y-coordinate of the pixel.
        Returns:
            A tuple containing the character and style of the pixel.
        """
        return self._buffer[y][x], self._styles[y][x]

    def set_pixel(self, x: int, y: int, char: str = "█", style: str = "white") -> None:
        """Sets a single pixel at the given coordinates.
        Also marks it dirty for refreshing.

        Args:
            x: The x-coordinate of the pixel.
            y: The y-coordinate of the pixel.
            char: The character to draw.
            style: The style to apply to the character.
        """
        # Fast rejection path without assert for performance
        if not (
            0 <= x < self._canvas_region.width and 0 <= y < self._canvas_region.height
        ):
            return

        self._buffer[y][x] = char
        self._styles[y][x] = style
        self.refresh()

    def set_pixels(
        self,
        coordinates: Iterable[tuple[int, int]],
        char: str = "█",
        style: str = "white",
    ) -> None:
        """Sets multiple pixels at the given coordinates.

        Args:
            coordinates: An iterable of tuples representing the coordinates of the pixels.
            char: The character to draw.
            style: The style to apply to the character.
        """
        # Check if we have coordinates
        coord_list = list(coordinates)
        if not coord_list:
            return

        # Batch updates to avoid calling refresh for each pixel
        # Cache properties for faster access in the loop
        buffer = self._buffer
        styles = self._styles
        width = self._canvas_region.width
        height = self._canvas_region.height

        # Process all pixels first, then refresh once
        for x, y in coord_list:
            if 0 <= x < width and 0 <= y < height:
                buffer[y][x] = char
                styles[y][x] = style

        # Only refresh once after all updates
        self.refresh()

    def set_hires_pixels(
        self,
        coordinates: Iterable[tuple[FloatScalar, FloatScalar]],
        hires_mode: HiResMode | None = None,
        style: str = "white",
    ) -> None:
        """Sets multiple pixels at the given coordinates using the specified Hi-Res mode.

        Args:
            coordinates: An iterable of tuples representing the coordinates of the pixels.
            hires_mode: The Hi-Res mode to use.
            style: The style to apply to the character.
        """
        # Use default mode if none provided
        hires_mode = hires_mode or self.default_hires_mode
        pixel_size = hires_sizes[hires_mode]
        pixel_info = pixels.get(hires_mode)
        assert pixel_info is not None

        # Group coordinates by their cell position to minimize buffer operations
        cells_to_update: dict[tuple[int, int], set[tuple[int, int]]] = {}

        # Pre-compute these values outside the loop for better performance
        w_factor = pixel_size.width
        h_factor = pixel_size.height

        # Process all coordinates and group them by their target cell
        for x, y in coordinates:
            # Early rejection for out-of-bounds
            if not self._canvas_region.contains(floor(x), floor(y)):
                continue

            # Calculate high-res coordinates
            hx = floor(x * w_factor)
            hy = floor(y * h_factor)

            # Calculate which cell this belongs to and offset within cell
            cell_x = hx // w_factor
            cell_y = hy // h_factor

            # Get or create the set for this cell
            cell_key = (cell_x, cell_y)
            if cell_key not in cells_to_update:
                cells_to_update[cell_key] = set()

            # Add this point to the cell's set
            offset_x = hx % w_factor
            offset_y = hy % h_factor
            cells_to_update[cell_key].add((offset_x, offset_y))

        # Process each cell that needs updating
        for (cell_x, cell_y), points in cells_to_update.items():
            # Create a small buffer just for this cell
            cell_buffer = np.zeros((pixel_size.height, pixel_size.width), dtype=bool)

            # Mark each point in the buffer
            for offset_x, offset_y in points:
                cell_buffer[offset_y, offset_x] = True

            # Convert to subpixels and look up the character
            subpixels = tuple(int(v) for v in cell_buffer.flat)
            if char := pixel_info[subpixels]:
                self.set_pixel(
                    cell_x,
                    cell_y,
                    char=char,
                    style=style,
                )

    def draw_line(
        self, x0: int, y0: int, x1: int, y1: int, char: str = "█", style: str = "white"
    ) -> None:
        """Draws a line from (x0, y0) to (x1, y1) using the specified character and style.

        Args:
            x0: The x-coordinate of the start of the line.
            y0: The y-coordinate of the start of the line.
            x1: The x-coordinate of the end of the line.
            y1: The y-coordinate of the end of the line.
            char: The character to draw.
            style: The style to apply to the character.
        """
        if not self._canvas_region.contains(
            x0, y0
        ) and not self._canvas_region.contains(x1, y1):
            return
        self.set_pixels(self._get_line_coordinates(x0, y0, x1, y1), char, style)

    def draw_lines(
        self,
        coordinates: Iterable[tuple[int, int, int, int]],
        char: str = "█",
        style: str = "white",
    ) -> None:
        """Draws multiple lines from given coordinates using the specified character and style.

        Args:
            coordinates: An iterable of tuples representing the coordinates of the lines.
            char: The character to draw.
            style: The style to apply to the character.
        """
        # Convert to list for multiple passes
        coord_list = list(coordinates)
        if not coord_list:
            return

        # Collect all pixels from all lines before rendering
        all_pixels = []

        for x0, y0, x1, y1 in coord_list:
            # Skip if both endpoints are outside the canvas
            if not self._canvas_region.contains(
                x0, y0
            ) and not self._canvas_region.contains(x1, y1):
                continue

            # Get coordinates for this line and extend the pixel collection
            line_pixels = self._get_line_coordinates(x0, y0, x1, y1)
            all_pixels.extend(line_pixels)

        # Draw all pixels at once with a single refresh
        if all_pixels:
            self.set_pixels(all_pixels, char, style)

    def draw_hires_line(
        self,
        x0: float,
        y0: float,
        x1: float,
        y1: float,
        hires_mode: HiResMode | None = None,
        style: str = "white",
    ) -> None:
        """Draws a high-resolution line from (x0, y0) to (x1, y1) using the specified character and style.

        Args:
            x0: The x-coordinate of the start of the line.
            y0: The y-coordinate of the start of the line.
            x1: The x-coordinate of the end of the line.
            y1: The y-coordinate of the end of the line.
            hires_mode: The high-resolution mode to use.
            style: The style to apply to the character.
        """
        self.draw_hires_lines([(x0, y0, x1, y1)], hires_mode, style)

    def draw_hires_lines(
        self,
        coordinates: Iterable[
            tuple[FloatScalar, FloatScalar, FloatScalar, FloatScalar]
        ],
        hires_mode: HiResMode | None = None,
        style: str = "white",
    ) -> None:
        """Draws multiple high-resolution lines from given coordinates using the specified character and style.

        Args:
            coordinates: An iterable of tuples representing the coordinates of the lines.
            hires_mode: The high-resolution mode to use.
            style: The style to apply to the character.
        """
        # Early out if no coordinates
        if not coordinates:
            return

        # Convert to list if not already for multiple passes
        coord_list = list(coordinates)

        hires_mode = hires_mode or self.default_hires_mode
        pixel_size = hires_sizes[hires_mode]

        # Pre-compute multiplication factors once
        w_factor = pixel_size.width
        h_factor = pixel_size.height
        inv_w_factor = 1.0 / w_factor
        inv_h_factor = 1.0 / h_factor

        # Initialize an empty list for collecting pixel coordinates
        pixels: list[tuple[float, float]] = []
        pixels_append = pixels.append  # Local reference for faster calls

        # Process each line
        for x0, y0, x1, y1 in coord_list:
            # Skip if both endpoints are outside canvas (optimization)
            if not self._canvas_region.contains(
                floor(x0), floor(y0)
            ) and not self._canvas_region.contains(floor(x1), floor(y1)):
                continue

            # Convert to high-res grid coordinates
            hx0 = floor(x0 * w_factor)
            hy0 = floor(y0 * h_factor)
            hx1 = floor(x1 * w_factor)
            hy1 = floor(y1 * h_factor)

            # Get line coordinates
            coords = self._get_line_coordinates(hx0, hy0, hx1, hy1)

            # Convert back to canvas space and add to pixel array
            # Use direct append and precalculated factors for better performance
            for x, y in coords:
                pixels_append((x * inv_w_factor, y * inv_h_factor))

        # Only make one call to set_hires_pixels with all points
        if pixels:
            self.set_hires_pixels(pixels, hires_mode, style)

    def draw_triangle(
        self,
        x0: int,
        y0: int,
        x1: int,
        y1: int,
        x2: int,
        y2: int,
        style: str = "white",
    ) -> None:
        """Draw a triangle outline using the specified style.

        Args:
            x0: The x-coordinate of the first vertex.
            y0: The y-coordinate of the first vertex.
            x1: The x-coordinate of the second vertex.
            y1: The y-coordinate of the second vertex.
            x2: The x-coordinate of the third vertex.
            y2: The y-coordinate of the third vertex.
            style: The style to apply to the characters.
        """

        # Draw the three sides of the triangle
        self.draw_lines(
            [(x0, y0, x1, y1), (x1, y1, x2, y2), (x2, y2, x0, y0)], "█", style
        )

    def draw_hires_triangle(
        self,
        x0: float,
        y0: float,
        x1: float,
        y1: float,
        x2: float,
        y2: float,
        hires_mode: HiResMode | None = None,
        style: str = "white",
    ) -> None:
        """Draw a high-resolution triangle outline using the specified style.

        Args:
            x0: The x-coordinate of the first vertex.
            y0: The y-coordinate of the first vertex.
            x1: The x-coordinate of the second vertex.
            y1: The y-coordinate of the second vertex.
            x2: The x-coordinate of the third vertex.
            y2: The y-coordinate of the third vertex.
            hires_mode: The high-resolution mode to use.
            style: The style to apply to the characters.
        """

        # Draw the three sides of the triangle with high-resolution
        self.draw_hires_lines(
            [(x0, y0, x1, y1), (x1, y1, x2, y2), (x2, y2, x0, y0)], hires_mode, style
        )

    def draw_filled_triangle(
        self,
        x0: int,
        y0: int,
        x1: int,
        y1: int,
        x2: int,
        y2: int,
        style: str = "white",
    ) -> None:
        """Draw a filled triangle using the specified style.
        Uses a scanline algorithm for efficient filling.

        Args:
            x0: The x-coordinate of the first vertex.
            y0: The y-coordinate of the first vertex.
            x1: The x-coordinate of the second vertex.
            y1: The y-coordinate of the second vertex.
            x2: The x-coordinate of the third vertex.
            y2: The y-coordinate of the third vertex.
            style: The style to apply to the characters.
        """
        # Sort vertices by y-coordinate (y0 <= y1 <= y2)
        if y0 > y1:
            x0, y0, x1, y1 = x1, y1, x0, y0
        if y1 > y2:
            x1, y1, x2, y2 = x2, y2, x1, y1
        if y0 > y1:
            x0, y0, x1, y1 = x1, y1, x0, y0

        # Skip if all points are the same or triangle has no height
        if (y0 == y1 == y2) or (y0 == y2):
            return

        # Initialize empty list for all pixel coordinates
        pixels: list[tuple[int, int]] = []
        pixels_append = pixels.append

        # Calculate interpolation factor for the middle point
        inv_slope02 = (x2 - x0) / (y2 - y0) if y2 != y0 else 0

        # First half of the triangle (bottom flat or top)
        if y1 == y0:  # Flat top triangle
            self._fill_flat_top_triangle(x0, y0, x1, y1, x2, y2, pixels_append)
        elif y1 == y2:  # Flat bottom triangle
            self._fill_flat_bottom_triangle(x0, y0, x1, y1, x2, y2, pixels_append)
        else:  # General triangle - split into flat-top and flat-bottom
            # Calculate the x-coordinate of the point on the long edge that has y = y1
            x3 = int(x0 + inv_slope02 * (y1 - y0))

            # Fill the flat bottom part
            self._fill_flat_bottom_triangle(x0, y0, x1, y1, x3, y1, pixels_append)

            # Fill the flat top part
            self._fill_flat_top_triangle(x1, y1, x3, y1, x2, y2, pixels_append)

        # Draw all pixels at once
        if pixels:
            self.set_pixels(pixels, "█", style)

    def _fill_flat_bottom_triangle(
        self,
        x0: int,
        y0: int,
        x1: int,
        y1: int,
        x2: int,
        y1_dup: int,
        pixels_append: Callable[[tuple[int, int]], None],
    ) -> None:
        """Helper method to fill a flat-bottom triangle (private method)."""
        dx1 = (x1 - x0) / (y1 - y0) if y1 != y0 else 0
        dx2 = (x2 - x0) / (y1 - y0) if y1 != y0 else 0

        # Initialize scanline coordinates
        x_start = x_end = float(x0)

        # Scan from top to bottom
        for y in range(y0, y1 + 1):
            # Add all pixels in this scanline
            for x in range(int(x_start), int(x_end) + 1):
                pixels_append((x, y))

            # Update scanline endpoints
            x_start += dx1
            x_end += dx2

    def _fill_flat_top_triangle(
        self,
        x0: int,
        y0: int,
        x1: int,
        y0_dup: int,
        x2: int,
        y2: int,
        pixels_append: Callable[[tuple[int, int]], None],
    ) -> None:
        """Helper method to fill a flat-top triangle (private method)."""
        dx1 = (x2 - x0) / (y2 - y0) if y2 != y0 else 0
        dx2 = (x2 - x1) / (y2 - y0) if y2 != y0 else 0

        # Initialize scanline coordinates
        x_start = float(x0)
        x_end = float(x1)

        # Scan from top to bottom
        for y in range(y0, y2 + 1):
            # Add all pixels in this scanline
            for x in range(int(x_start), int(x_end) + 1):
                pixels_append((x, y))

            # Update scanline endpoints
            x_start += dx1
            x_end += dx2

    def draw_filled_hires_triangle(
        self,
        x0: float,
        y0: float,
        x1: float,
        y1: float,
        x2: float,
        y2: float,
        hires_mode: HiResMode | None = None,
        style: str = "white",
    ) -> None:
        """Draw a filled high-resolution triangle using the specified style.
        Uses a scanline algorithm with subpixel precision.

        Args:
            x0: The x-coordinate of the first vertex.
            y0: The y-coordinate of the first vertex.
            x1: The x-coordinate of the second vertex.
            y1: The y-coordinate of the second vertex.
            x2: The x-coordinate of the third vertex.
            y2: The y-coordinate of the third vertex.
            hires_mode: The high-resolution mode to use.
            style: The style to apply to the characters.
        """
        # Use default hires mode if none provided
        hires_mode = hires_mode or self.default_hires_mode

        # Sort vertices by y-coordinate (y0 <= y1 <= y2)
        if y0 > y1:
            x0, y0, x1, y1 = x1, y1, x0, y0
        if y1 > y2:
            x1, y1, x2, y2 = x2, y2, x1, y1
        if y0 > y1:
            x0, y0, x1, y1 = x1, y1, x0, y0

        # Skip if all points are the same or triangle has no height
        if abs(y0 - y2) < 1e-6:
            return

        # Initialize pixel collection
        pixels: list[tuple[float, float]] = []
        pixels_append = pixels.append

        # Define helper function to add a hi-res pixel
        def add_hires_pixel(x: float, y: float) -> None:
            pixels_append((x, y))

        # No edge parameters needed

        # Process based on triangle shape
        if abs(y1 - y0) < 1e-6:  # Flat top triangle
            self._fill_flat_top_hires_triangle(x0, y0, x1, y1, x2, y2, add_hires_pixel)
        elif abs(y1 - y2) < 1e-6:  # Flat bottom triangle
            self._fill_flat_bottom_hires_triangle(
                x0, y0, x1, y1, x2, y2, add_hires_pixel
            )
        else:  # General triangle - split into flat-top and flat-bottom
            # Calculate the interpolation factor for the split point
            t = (y1 - y0) / (y2 - y0)

            # Calculate the x-coordinate of the point on the long edge that has y = y1
            x3 = x0 + t * (x2 - x0)

            # Fill the flat bottom part
            self._fill_flat_bottom_hires_triangle(
                x0, y0, x1, y1, x3, y1, add_hires_pixel
            )

            # Fill the flat top part
            self._fill_flat_top_hires_triangle(x1, y1, x3, y1, x2, y2, add_hires_pixel)

        # Draw all hi-res pixels at once
        if pixels:
            self.set_hires_pixels(pixels, hires_mode, style)

    def _fill_flat_bottom_hires_triangle(
        self,
        x0: float,
        y0: float,
        x1: float,
        y1: float,
        x2: float,
        y1_dup: float,
        add_pixel: Callable[[float, float], None],
    ) -> None:
        """Helper method to fill a flat-bottom triangle with hi-res precision (private method)."""
        # Calculate slopes
        height = y1 - y0
        if abs(height) < 1e-6:
            return

        dx_left = (x1 - x0) / height
        dx_right = (x2 - x0) / height

        # Initialize scanline endpoints
        x_left = x0
        x_right = x0

        # Calculate steps for smoother rendering (use more y steps for better quality)
        y_steps = max(100, int(height * 10))
        y_step = height / y_steps

        # Scan from top to bottom
        for i in range(y_steps + 1):
            y = y0 + i * y_step

            # Calculate scanline width
            width = x_right - x_left

            # Calculate steps for this scanline
            x_steps = max(20, int(width * 10))
            x_step = width / max(1, x_steps)

            # Add pixels along scanline
            for j in range(x_steps + 1):
                x = x_left + j * x_step
                add_pixel(x, y)

            # Update scanline endpoints for next row
            x_left += dx_left * y_step
            x_right += dx_right * y_step

    def _fill_flat_top_hires_triangle(
        self,
        x0: float,
        y0: float,
        x1: float,
        y0_dup: float,
        x2: float,
        y2: float,
        add_pixel: Callable[[float, float], None],
    ) -> None:
        """Helper method to fill a flat-top triangle with hi-res precision (private method)."""
        # Calculate slopes
        height = y2 - y0
        if abs(height) < 1e-6:
            return

        dx_left = (x2 - x0) / height
        dx_right = (x2 - x1) / height

        # Initialize scanline endpoints
        x_left = x0
        x_right = x1

        # Calculate steps for smoother rendering (use more y steps for better quality)
        y_steps = max(100, int(height * 10))
        y_step = height / y_steps

        # Scan from top to bottom
        for i in range(y_steps + 1):
            y = y0 + i * y_step

            # Calculate scanline width
            width = x_right - x_left

            # Calculate steps for this scanline
            x_steps = max(20, int(width * 10))
            x_step = width / max(1, x_steps)

            # Add pixels along scanline
            for j in range(x_steps + 1):
                x = x_left + j * x_step
                add_pixel(x, y)

            # Update scanline endpoints for next row
            x_left += dx_left * y_step
            x_right += dx_right * y_step

    def draw_quad(
        self,
        x0: int,
        y0: int,
        x1: int,
        y1: int,
        x2: int,
        y2: int,
        x3: int,
        y3: int,
        style: str = "white",
    ) -> None:
        """Draw a quadrilateral outline using the specified style.

        Args:
            x0: The x-coordinate of the first vertex.
            y0: The y-coordinate of the first vertex.
            x1: The x-coordinate of the second vertex.
            y1: The y-coordinate of the second vertex.
            x2: The x-coordinate of the third vertex.
            y2: The y-coordinate of the third vertex.
            x3: The x-coordinate of the fourth vertex.
            y3: The y-coordinate of the fourth vertex.
            style: The style to apply to the characters.
        """
        # Draw the four sides of the quadrilateral
        self.draw_lines(
            [(x0, y0, x1, y1), (x1, y1, x2, y2), (x2, y2, x3, y3), (x3, y3, x0, y0)],
            "█",
            style,
        )

    def draw_hires_quad(
        self,
        x0: float,
        y0: float,
        x1: float,
        y1: float,
        x2: float,
        y2: float,
        x3: float,
        y3: float,
        hires_mode: HiResMode | None = None,
        style: str = "white",
    ) -> None:
        """Draw a high-resolution quadrilateral outline using the specified style.

        Args:
            x0: The x-coordinate of the first vertex.
            y0: The y-coordinate of the first vertex.
            x1: The x-coordinate of the second vertex.
            y1: The y-coordinate of the second vertex.
            x2: The x-coordinate of the third vertex.
            y2: The y-coordinate of the third vertex.
            x3: The x-coordinate of the fourth vertex.
            y3: The y-coordinate of the fourth vertex.
            hires_mode: The high-resolution mode to use.
            style: The style to apply to the characters.
        """
        # Draw the four sides of the quadrilateral with high-resolution
        self.draw_hires_lines(
            [(x0, y0, x1, y1), (x1, y1, x2, y2), (x2, y2, x3, y3), (x3, y3, x0, y0)],
            hires_mode,
            style,
        )

    def draw_filled_quad(
        self,
        x0: int,
        y0: int,
        x1: int,
        y1: int,
        x2: int,
        y2: int,
        x3: int,
        y3: int,
        style: str = "white",
    ) -> None:
        """Draw a filled quadrilateral using the specified style.
        Splits the quad into two triangles for filling.

        Args:
            x0: The x-coordinate of the first vertex.
            y0: The y-coordinate of the first vertex.
            x1: The x-coordinate of the second vertex.
            y1: The y-coordinate of the second vertex.
            x2: The x-coordinate of the third vertex.
            y2: The y-coordinate of the third vertex.
            x3: The x-coordinate of the fourth vertex.
            y3: The y-coordinate of the fourth vertex.
            style: The style to apply to the characters.
        """
        # Draw the quad as two filled triangles
        # First triangle (0, 1, 2)
        self.draw_filled_triangle(x0, y0, x1, y1, x2, y2, style)
        # Second triangle (0, 2, 3)
        self.draw_filled_triangle(x0, y0, x2, y2, x3, y3, style)

    def draw_filled_hires_quad(
        self,
        x0: float,
        y0: float,
        x1: float,
        y1: float,
        x2: float,
        y2: float,
        x3: float,
        y3: float,
        hires_mode: HiResMode | None = None,
        style: str = "white",
    ) -> None:
        """Draw a filled high-resolution quadrilateral using the specified style.
        Splits the quad into two triangles for filling.

        Args:
            x0: The x-coordinate of the first vertex.
            y0: The y-coordinate of the first vertex.
            x1: The x-coordinate of the second vertex.
            y1: The y-coordinate of the second vertex.
            x2: The x-coordinate of the third vertex.
            y2: The y-coordinate of the third vertex.
            x3: The x-coordinate of the fourth vertex.
            y3: The y-coordinate of the fourth vertex.
            hires_mode: The high-resolution mode to use.
            style: The style to apply to the characters.
        """
        # Draw the quad as two filled high-resolution triangles
        # First triangle (0, 1, 2)
        self.draw_filled_hires_triangle(x0, y0, x1, y1, x2, y2, hires_mode, style)
        # Second triangle (0, 2, 3)
        self.draw_filled_hires_triangle(x0, y0, x2, y2, x3, y3, hires_mode, style)

    def draw_rectangle_box(
        self,
        x0: int,
        y0: int,
        x1: int,
        y1: int,
        thickness: int = 1,
        style: str = "white",
    ) -> None:
        """Draw a rectangle box with the specified thickness and style.

        Args:
            x0: The x-coordinate of the top-left corner.
            y0: The y-coordinate of the top-left corner.
            x1: The x-coordinate of the bottom-right corner.
            y1: The y-coordinate of the bottom-right corner.
            thickness: The thickness of the box.
            style: The style to apply to the characters.
        """
        # (x0, y0)     (x1, y0)
        #    ┌────────────┐
        #    │            │
        #    │            │
        #    │            │
        #    │            │
        #    └────────────┘
        # (x0, y1)     (x1, y1)

        # NOTE: A difference of 0 between coordinates results in a
        # width or height of 1 cell inside Textual.

        T = thickness
        x0, x1 = sorted((x0, x1))
        y0, y1 = sorted((y0, y1))

        # Both width and height are 1. This would just be a dot, so
        # we don't draw anything.
        if (x1 - x0 == 0) and (y1 - y0 == 0):
            return

        # We now know either the width or height must be higher than 2.
        # Height is 1, place two horizontal line enders.
        if y1 - y0 == 0:
            self.set_pixel(x0, y0, char=get_box((0, T, 0, 0)), style=style)
            self.set_pixel(x1, y0, char=get_box((0, 0, 0, T)), style=style)
            if x1 - x0 >= 2:
                # Width is greater than or equal to 3, draw a horizontal line
                # between the line enders.
                self.draw_line(
                    x0 + 1, y0, x1 - 1, y1, char=get_box((0, T, 0, T)), style=style
                )
            return

        # Width is 1, place two vertical line enders.
        if x1 - x0 == 0:
            self.set_pixel(x0, y0, char=get_box((0, 0, T, 0)), style=style)
            self.set_pixel(x0, y1, char=get_box((T, 0, 0, 0)), style=style)
            if y1 - y0 >= 2:
                # Height is greater than or equal to 3, draw a horizontal line
                # between the line enders.
                self.draw_line(
                    x0, y0 + 1, x1, y1 - 1, char=get_box((T, 0, T, 0)), style=style
                )
            return

        # The remaining conditions require all the corner pieces to be drawn.
        self.set_pixel(x0, y0, char=get_box((0, T, T, 0)), style=style)
        self.set_pixel(x1, y0, char=get_box((0, 0, T, T)), style=style)
        self.set_pixel(x1, y1, char=get_box((T, 0, 0, T)), style=style)
        self.set_pixel(x0, y1, char=get_box((T, T, 0, 0)), style=style)

        # If width and height are both 2, we don't need any lines. Only corners.
        if (x1 - x0 == 1) and (y1 - y0 == 1):
            return

        # Width is greater than or equal to 3, draw horizontal lines.
        if x1 - x0 >= 2:
            for y in y0, y1:
                self.draw_line(
                    x0 + 1, y, x1 - 1, y, char=get_box((0, T, 0, T)), style=style
                )
        # Height is greater than or equal to 3, draw vertical lines.
        if y1 - y0 >= 2:
            for x in x0, x1:
                self.draw_line(
                    x, y0 + 1, x, y1 - 1, char=get_box((T, 0, T, 0)), style=style
                )

    def draw_filled_circle(
        self, cx: int, cy: int, radius: int, style: str = "white"
    ) -> None:
        """Draw a filled circle using Bresenham's algorithm. Compensates for 2:1 aspect ratio.

        Args:
            cx (int): X-coordinate of the center of the circle.
            cy (int): Y-coordinate of the center of the circle.
            radius (int): Radius of the circle.
            style (str): Style of the pixels to be drawn.
        """
        # Early rejection for invalid inputs
        if radius <= 0:
            return

        # Initialize buffer to collect all pixels
        pixels: list[tuple[int, int]] = []
        pixels_append = pixels.append  # Local reference for faster calls

        x = 0
        y = radius
        d = 3 - 2 * radius

        # Pre-compute aspect ratio adjustments
        max_iterations = radius * 2  # Safety limit
        iteration = 0

        while y >= x and iteration < max_iterations:
            # Adjust y-coordinates to account for 2:1 aspect ratio
            y1 = cy + (y + 1) // 2
            y2 = cy + (x + 1) // 2
            y3 = cy - x // 2
            y4 = cy - y // 2

            # Add horizontal lines of pixels
            for xpos in range(cx - x, cx + x + 1):
                pixels_append((xpos, y1))
                pixels_append((xpos, y4))

            for xpos in range(cx - y, cx + y + 1):
                pixels_append((xpos, y2))
                pixels_append((xpos, y3))

            x += 1
            if d > 0:
                y -= 1
                d = d + 4 * (x - y) + 10
            else:
                d = d + 4 * x + 6

            iteration += 1

        # Draw all pixels at once
        if pixels:
            self.set_pixels(pixels, "█", style)

    def draw_filled_hires_circle(
        self,
        cx: float,
        cy: float,
        radius: float,
        hires_mode: HiResMode | None = None,
        style: str = "white",
    ) -> None:
        """Draw a filled circle, with high-resolution support.

        Args:
            cx (float): X-coordinate of the center of the circle.
            cy (float): Y-coordinate of the center of the circle.
            radius (float): Radius of the circle.
            hires_mode (HiResMode): The high-resolution mode to use.
            style (str): Style of the pixels to be drawn.
        """
        # Early rejection for invalid inputs
        if radius <= 0:
            return

        # Constants and scaling factors
        scale_x = 1
        scale_y = 2
        aspect_ratio = scale_x / scale_y

        # Pre-compute values used in the inner loop
        radius_squared = radius**2
        inv_scale_x = 1.0 / scale_x
        inv_scale_y_aspect = 1.0 / (scale_y * aspect_ratio)

        # Determine the bounding box for the circle
        y_min = int(-radius * scale_y)
        y_max = int(radius * scale_y) + 1
        x_min = int(-radius * scale_x)
        x_max = int(radius * scale_x) + 1

        # Initialize an empty list for collecting pixel coordinates
        # (We calculate estimated_pixels only for the safety check below)
        estimated_pixels = int(3.2 * radius * radius)  # 3.2 instead of π for safety
        pixels: list[tuple[float, float]] = []
        pixels_append = pixels.append  # Local reference for faster calls

        # Use a more efficient scanning algorithm
        # For each y, compute the range of valid x values directly
        for y in range(y_min, y_max):
            # Solve circle equation for x: (x/sx)² + (y/(sy*ar))² <= r²
            y_term = (y * inv_scale_y_aspect) ** 2
            if y_term > radius_squared:
                continue  # Skip this row if y is outside the circle

            x_term_max = radius_squared - y_term
            if x_term_max < 0:
                continue  # Skip if no valid x values

            # Find the range of valid x values
            x_radius = int((x_term_max**0.5) * scale_x)
            x_start = max(x_min, -x_radius)
            x_end = min(x_max, x_radius + 1)

            # Add all points in this row in one go
            for x in range(x_start, x_end):
                pixels_append((cx + x * inv_scale_x, cy + y / scale_y))

            # Safety check
            if len(pixels) > estimated_pixels * 2:
                break

        # Render all pixels at once
        self.set_hires_pixels(pixels, hires_mode, style)

    def draw_circle(self, cx: int, cy: int, radius: int, style: str = "white") -> None:
        """Draw a circle using Bresenham's algorithm. Compensates for 2:1 aspect ratio.

        Args:
            cx (int): X-coordinate of the center of the circle.
            cy (int): Y-coordinate of the center of the circle.
            radius (int): Radius of the circle.
            style (str): Style of the pixels to be drawn.
        """
        # Early rejection for invalid inputs
        if radius <= 0:
            return

        # Initialize buffer to collect all pixels
        pixels: list[tuple[int, int]] = []
        pixels_append = pixels.append  # Local reference for faster calls

        x = radius
        y = 0
        decision = 1 - radius

        # Pre-compute aspect ratio adjustments
        max_iterations = radius * 2  # Safety limit
        iteration = 0

        while y <= x and iteration < max_iterations:
            y_half = y // 2
            x_half = x // 2

            # Add all 8 points in each octant
            pixels_append((cx + x, cy + y_half))
            pixels_append((cx - x, cy + y_half))
            pixels_append((cx + x, cy - y_half))
            pixels_append((cx - x, cy - y_half))
            pixels_append((cx + y, cy + x_half))
            pixels_append((cx - y, cy + x_half))
            pixels_append((cx + y, cy - x_half))
            pixels_append((cx - y, cy - x_half))

            y += 1
            if decision <= 0:
                decision += 2 * y + 1
            else:
                x -= 1
                decision += 2 * (y - x) + 1

            iteration += 1

        # Draw all pixels at once
        if pixels:
            self.set_pixels(pixels, "█", style)

    def draw_hires_circle(
        self,
        cx: float,
        cy: float,
        radius: float,
        hires_mode: HiResMode | None = None,
        style: str = "white",
    ) -> None:
        """Draw a circle with high-resolution support using Bresenham's algorithm. Compensates for 2:1 aspect ratio.

        Args:
            cx (float): X-coordinate of the center of the circle.
            cy (float): Y-coordinate of the center of the circle.
            radius (float): Radius of the circle.
            hires_mode (HiResMode): The high-resolution mode to use.
            style (str): Style of the pixels to be drawn.
        """
        # Early rejection for invalid inputs
        if radius <= 0:
            return

        # Initialize an empty list for collecting pixel coordinates
        # (We calculate estimated_size only for the safety check below)
        estimated_size = int(
            radius * 16
        )  # Each step adds 8 pixels, estimate 4*radius steps
        pixels: list[tuple[float, float]] = []
        pixels_extend = pixels.extend  # Local reference for faster calls

        # Pre-compute constants
        scale_x = 1
        scale_y = 2
        aspect_ratio = scale_x / scale_y

        # Midpoint circle algorithm with floating point precision
        x: float = radius
        y: float = 0
        decision: float = 1 - radius

        # Pre-compute points for each octant to avoid repeated calculations
        def get_circle_points(x: float, y: float) -> list[tuple[float, float]]:
            y_scaled = y * aspect_ratio
            x_scaled = x * aspect_ratio
            return [
                (cx + x, cy + y_scaled),
                (cx - x, cy + y_scaled),
                (cx + x, cy - y_scaled),
                (cx - x, cy - y_scaled),
                (cx + y, cy + x_scaled),
                (cx - y, cy + x_scaled),
                (cx + y, cy - x_scaled),
                (cx - y, cy - x_scaled),
            ]

        # Generate points for the circle
        while y <= x:
            # Add all 8 points at once
            pixels_extend(get_circle_points(x, y))

            # Update position
            y += 0.5
            if decision <= 0:
                decision += 2 * y + 1
            else:
                x -= 0.5
                decision += 2 * (y - x) + 1

            # Safety check to avoid infinite loops
            if len(pixels) > estimated_size:
                break

        # Render all pixels at once
        self.set_hires_pixels(pixels, hires_mode, style)

    def write_text(
        self,
        x: int,
        y: int,
        text: str,
        align: TextAlign = TextAlign.LEFT,
    ) -> None:
        """Write text to the canvas at the specified position, with support for markup.

        Args:
            x (int): X-coordinate of the left edge of the text.
            y (int): Y-coordinate of the baseline of the text.
            text (str): Text to be written.
            align (TextAlign): The alignment of the text within the canvas.
        """
        if y < 0 or y >= self._canvas_size.height:
            return

        # parse markup
        rich_text = Text.from_markup(text)
        # store plain text
        if (plain_text := rich_text.plain) == "":
            return
        # store styles for each individual character
        rich_styles = []
        for c in rich_text.divide(range(1, len(plain_text))):
            style = Style()
            for span in c._spans:
                style += Style.parse(span.style)
            rich_styles.append(style)

        if align == TextAlign.RIGHT:
            x -= len(plain_text) - 1
        elif align == TextAlign.CENTER:
            div, mod = divmod(len(plain_text), 2)
            x -= div
            if mod == 0:
                # even number of characters, shift one to the right since I just
                # like that better -- DF
                x += 1

        if x <= -len(plain_text) or x >= self._canvas_size.width:
            # no part of text falls inside the canvas
            return

        overflow_left = -x
        overflow_right = x + len(plain_text) - self._canvas_size.width
        if overflow_left > 0:
            buffer_left = 0
            text_left = overflow_left
        else:
            buffer_left = x
            text_left = 0
        if overflow_right > 0:
            buffer_right = None
            text_right = -overflow_right
        else:
            buffer_right = x + len(plain_text)
            text_right = None

        self._buffer[y][buffer_left:buffer_right] = plain_text[text_left:text_right]
        self._styles[y][buffer_left:buffer_right] = [
            str(s) for s in rich_styles[text_left:text_right]
        ]
        assert len(self._buffer[y]) == self._canvas_size.width
        assert len(self._styles[y]) == self._canvas_size.width
        self.refresh()

    def _get_line_coordinates(
        self, x0: int, y0: int, x1: int, y1: int
    ) -> list[tuple[int, int]]:
        """Get all pixel coordinates on the line between two points.

        Fast implementation of Bresenham's line algorithm.
        Returns a list of coordinates instead of a generator for better performance.

        Args:
            x0: starting point x coordinate
            y0: starting point y coordinate
            x1: end point x coordinate
            y1: end point y coordinate

        Returns:
            List of (x, y) coordinate tuples that make up the line.
        """
        # Early rejection if endpoints are identical
        if x0 == x1 and y0 == y1:
            return [(x0, y0)]

        # Fast path for horizontal lines
        if y0 == y1:
            if x0 < x1:
                return [(x, y0) for x in range(x0, x1 + 1)]
            else:
                return [(x, y0) for x in range(x1, x0 + 1)]

        # Fast path for vertical lines
        if x0 == x1:
            if y0 < y1:
                return [(x0, y) for y in range(y0, y1 + 1)]
            else:
                return [(x0, y) for y in range(y1, y0 + 1)]

        # Initialize algorithm variables
        dx = abs(x1 - x0)
        sx = 1 if x0 < x1 else -1
        dy = -abs(y1 - y0)
        sy = 1 if y0 < y1 else -1
        error = dx + dy

        # Pre-allocate result list with known line length for efficiency
        # The +1 ensures we have enough space for all points
        max_points = max(abs(x1 - x0), abs(y1 - y0)) + 1
        points = []
        points.append((x0, y0))

        # x and y are mutable copies so we can modify them
        x, y = x0, y0

        # Main loop - more efficient without generator overhead
        while not (x == x1 and y == y1):
            e2 = 2 * error
            if e2 >= dy:
                if x == x1:
                    break
                error += dy
                x += sx
            if e2 <= dx:
                if y == y1:
                    break
                error += dx
                y += sy
            points.append((x, y))

            # Safety check to avoid infinite loops
            if len(points) > max_points:
                break

        return points

__init__(width=40, height=20, default_hires_mode=HiResMode.BRAILLE, name=None, id=None, classes=None, disabled=False)

Initialize the Canvas widget.

Parameters:

Name Type Description Default
width int

The width of the canvas. Defaults to 40.

40
height int

The height of the canvas. Defaults to 20.

20
default_hires_mode HiResMode | None

The default high-resolution mode. Defaults to HiresMode.BRAILLE.

BRAILLE
name str | None

The name of the widget. Defaults to None.

None
id str | None

The ID of the widget. Defaults to None.

None
classes str | None

The CSS classes of the widget. Defaults to None.

None
disabled bool

Whether the widget is disabled. Defaults to False.

False
Source code in src/textual_hires_canvas/canvas.py
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
def __init__(
    self,
    width: int = 40,
    height: int = 20,
    default_hires_mode: HiResMode | None = HiResMode.BRAILLE,
    name: str | None = None,
    id: str | None = None,
    classes: str | None = None,
    disabled: bool = False,
):
    """Initialize the Canvas widget.

    Args:
        width: The width of the canvas. Defaults to 40.
        height: The height of the canvas. Defaults to 20.
        default_hires_mode: The default high-resolution mode. Defaults to
            HiresMode.BRAILLE.
        name: The name of the widget. Defaults to None.
        id: The ID of the widget. Defaults to None.
        classes: The CSS classes of the widget. Defaults to None.
        disabled: Whether the widget is disabled. Defaults to False.
    """
    super().__init__(name=name, id=id, classes=classes, disabled=disabled)

    self._refreshes_pending: int = 0
    # reference count batch refreshes

    self._buffer = []
    self._styles = []
    self._canvas_size = Size(0, 0)
    self._canvas_region = Region()

    self.default_hires_mode = default_hires_mode or HiResMode.BRAILLE

    self.reset(size=Size(width, height), refresh=False)

async_batch_refresh() async

Async context manager that defers call to refresh until exiting the context.

This is useful when making multiple asynchronous changes to the canvas and only wanting to trigger refresh once at the end.

Example

Yields:

AsyncIterator[None]: An async context manager.

Source code in src/textual_hires_canvas/canvas.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
@asynccontextmanager
async def async_batch_refresh(self) -> AsyncIterator[None]:
    """Async context manager that defers call to refresh until exiting the context.

    This is useful when making multiple asynchronous changes to the canvas and only wanting
    to trigger refresh once at the end.

    Example:
            Yields:
    AsyncIterator[None]: An async context manager.
    """
    self._refreshes_pending += 1
    try:
        yield
    finally:
        self._refreshes_pending -= 1
        if self._refreshes_pending == 0:
            self.refresh()

batch_refresh()

Context manager that defers call to refresh until exiting the context.

This is useful when making multiple changes to the canvas and only wanting to trigger refresh once at the end.

Example
with canvas.batch_changes():
    canvas.set_pixel(0, 0)
    canvas.set_pixel(1, 1)
    canvas.set_pixel(2, 2)
# Refresh called

Yields:

Type Description
None

Iterator[None]: A context manager.

Source code in src/textual_hires_canvas/canvas.py
 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
@contextmanager
def batch_refresh(self) -> Iterator[None]:
    """Context manager that defers call to refresh until exiting the context.

    This is useful when making multiple changes to the canvas and only wanting
    to trigger refresh once at the end.

    Example:
        ```python
        with canvas.batch_changes():
            canvas.set_pixel(0, 0)
            canvas.set_pixel(1, 1)
            canvas.set_pixel(2, 2)
        # Refresh called
        ```

    Yields:
        Iterator[None]: A context manager.
    """
    self._refreshes_pending += 1
    try:
        yield
    finally:
        self._refreshes_pending -= 1
        if self._refreshes_pending == 0:
            self.refresh()

draw_circle(cx, cy, radius, style='white')

Draw a circle using Bresenham's algorithm. Compensates for 2:1 aspect ratio.

Parameters:

Name Type Description Default
cx int

X-coordinate of the center of the circle.

required
cy int

Y-coordinate of the center of the circle.

required
radius int

Radius of the circle.

required
style str

Style of the pixels to be drawn.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_circle(self, cx: int, cy: int, radius: int, style: str = "white") -> None:
    """Draw a circle using Bresenham's algorithm. Compensates for 2:1 aspect ratio.

    Args:
        cx (int): X-coordinate of the center of the circle.
        cy (int): Y-coordinate of the center of the circle.
        radius (int): Radius of the circle.
        style (str): Style of the pixels to be drawn.
    """
    # Early rejection for invalid inputs
    if radius <= 0:
        return

    # Initialize buffer to collect all pixels
    pixels: list[tuple[int, int]] = []
    pixels_append = pixels.append  # Local reference for faster calls

    x = radius
    y = 0
    decision = 1 - radius

    # Pre-compute aspect ratio adjustments
    max_iterations = radius * 2  # Safety limit
    iteration = 0

    while y <= x and iteration < max_iterations:
        y_half = y // 2
        x_half = x // 2

        # Add all 8 points in each octant
        pixels_append((cx + x, cy + y_half))
        pixels_append((cx - x, cy + y_half))
        pixels_append((cx + x, cy - y_half))
        pixels_append((cx - x, cy - y_half))
        pixels_append((cx + y, cy + x_half))
        pixels_append((cx - y, cy + x_half))
        pixels_append((cx + y, cy - x_half))
        pixels_append((cx - y, cy - x_half))

        y += 1
        if decision <= 0:
            decision += 2 * y + 1
        else:
            x -= 1
            decision += 2 * (y - x) + 1

        iteration += 1

    # Draw all pixels at once
    if pixels:
        self.set_pixels(pixels, "█", style)

draw_filled_circle(cx, cy, radius, style='white')

Draw a filled circle using Bresenham's algorithm. Compensates for 2:1 aspect ratio.

Parameters:

Name Type Description Default
cx int

X-coordinate of the center of the circle.

required
cy int

Y-coordinate of the center of the circle.

required
radius int

Radius of the circle.

required
style str

Style of the pixels to be drawn.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_filled_circle(
    self, cx: int, cy: int, radius: int, style: str = "white"
) -> None:
    """Draw a filled circle using Bresenham's algorithm. Compensates for 2:1 aspect ratio.

    Args:
        cx (int): X-coordinate of the center of the circle.
        cy (int): Y-coordinate of the center of the circle.
        radius (int): Radius of the circle.
        style (str): Style of the pixels to be drawn.
    """
    # Early rejection for invalid inputs
    if radius <= 0:
        return

    # Initialize buffer to collect all pixels
    pixels: list[tuple[int, int]] = []
    pixels_append = pixels.append  # Local reference for faster calls

    x = 0
    y = radius
    d = 3 - 2 * radius

    # Pre-compute aspect ratio adjustments
    max_iterations = radius * 2  # Safety limit
    iteration = 0

    while y >= x and iteration < max_iterations:
        # Adjust y-coordinates to account for 2:1 aspect ratio
        y1 = cy + (y + 1) // 2
        y2 = cy + (x + 1) // 2
        y3 = cy - x // 2
        y4 = cy - y // 2

        # Add horizontal lines of pixels
        for xpos in range(cx - x, cx + x + 1):
            pixels_append((xpos, y1))
            pixels_append((xpos, y4))

        for xpos in range(cx - y, cx + y + 1):
            pixels_append((xpos, y2))
            pixels_append((xpos, y3))

        x += 1
        if d > 0:
            y -= 1
            d = d + 4 * (x - y) + 10
        else:
            d = d + 4 * x + 6

        iteration += 1

    # Draw all pixels at once
    if pixels:
        self.set_pixels(pixels, "█", style)

draw_filled_hires_circle(cx, cy, radius, hires_mode=None, style='white')

Draw a filled circle, with high-resolution support.

Parameters:

Name Type Description Default
cx float

X-coordinate of the center of the circle.

required
cy float

Y-coordinate of the center of the circle.

required
radius float

Radius of the circle.

required
hires_mode HiResMode

The high-resolution mode to use.

None
style str

Style of the pixels to be drawn.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_filled_hires_circle(
    self,
    cx: float,
    cy: float,
    radius: float,
    hires_mode: HiResMode | None = None,
    style: str = "white",
) -> None:
    """Draw a filled circle, with high-resolution support.

    Args:
        cx (float): X-coordinate of the center of the circle.
        cy (float): Y-coordinate of the center of the circle.
        radius (float): Radius of the circle.
        hires_mode (HiResMode): The high-resolution mode to use.
        style (str): Style of the pixels to be drawn.
    """
    # Early rejection for invalid inputs
    if radius <= 0:
        return

    # Constants and scaling factors
    scale_x = 1
    scale_y = 2
    aspect_ratio = scale_x / scale_y

    # Pre-compute values used in the inner loop
    radius_squared = radius**2
    inv_scale_x = 1.0 / scale_x
    inv_scale_y_aspect = 1.0 / (scale_y * aspect_ratio)

    # Determine the bounding box for the circle
    y_min = int(-radius * scale_y)
    y_max = int(radius * scale_y) + 1
    x_min = int(-radius * scale_x)
    x_max = int(radius * scale_x) + 1

    # Initialize an empty list for collecting pixel coordinates
    # (We calculate estimated_pixels only for the safety check below)
    estimated_pixels = int(3.2 * radius * radius)  # 3.2 instead of π for safety
    pixels: list[tuple[float, float]] = []
    pixels_append = pixels.append  # Local reference for faster calls

    # Use a more efficient scanning algorithm
    # For each y, compute the range of valid x values directly
    for y in range(y_min, y_max):
        # Solve circle equation for x: (x/sx)² + (y/(sy*ar))² <= r²
        y_term = (y * inv_scale_y_aspect) ** 2
        if y_term > radius_squared:
            continue  # Skip this row if y is outside the circle

        x_term_max = radius_squared - y_term
        if x_term_max < 0:
            continue  # Skip if no valid x values

        # Find the range of valid x values
        x_radius = int((x_term_max**0.5) * scale_x)
        x_start = max(x_min, -x_radius)
        x_end = min(x_max, x_radius + 1)

        # Add all points in this row in one go
        for x in range(x_start, x_end):
            pixels_append((cx + x * inv_scale_x, cy + y / scale_y))

        # Safety check
        if len(pixels) > estimated_pixels * 2:
            break

    # Render all pixels at once
    self.set_hires_pixels(pixels, hires_mode, style)

draw_filled_hires_quad(x0, y0, x1, y1, x2, y2, x3, y3, hires_mode=None, style='white')

Draw a filled high-resolution quadrilateral using the specified style. Splits the quad into two triangles for filling.

Parameters:

Name Type Description Default
x0 float

The x-coordinate of the first vertex.

required
y0 float

The y-coordinate of the first vertex.

required
x1 float

The x-coordinate of the second vertex.

required
y1 float

The y-coordinate of the second vertex.

required
x2 float

The x-coordinate of the third vertex.

required
y2 float

The y-coordinate of the third vertex.

required
x3 float

The x-coordinate of the fourth vertex.

required
y3 float

The y-coordinate of the fourth vertex.

required
hires_mode HiResMode | None

The high-resolution mode to use.

None
style str

The style to apply to the characters.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_filled_hires_quad(
    self,
    x0: float,
    y0: float,
    x1: float,
    y1: float,
    x2: float,
    y2: float,
    x3: float,
    y3: float,
    hires_mode: HiResMode | None = None,
    style: str = "white",
) -> None:
    """Draw a filled high-resolution quadrilateral using the specified style.
    Splits the quad into two triangles for filling.

    Args:
        x0: The x-coordinate of the first vertex.
        y0: The y-coordinate of the first vertex.
        x1: The x-coordinate of the second vertex.
        y1: The y-coordinate of the second vertex.
        x2: The x-coordinate of the third vertex.
        y2: The y-coordinate of the third vertex.
        x3: The x-coordinate of the fourth vertex.
        y3: The y-coordinate of the fourth vertex.
        hires_mode: The high-resolution mode to use.
        style: The style to apply to the characters.
    """
    # Draw the quad as two filled high-resolution triangles
    # First triangle (0, 1, 2)
    self.draw_filled_hires_triangle(x0, y0, x1, y1, x2, y2, hires_mode, style)
    # Second triangle (0, 2, 3)
    self.draw_filled_hires_triangle(x0, y0, x2, y2, x3, y3, hires_mode, style)

draw_filled_hires_triangle(x0, y0, x1, y1, x2, y2, hires_mode=None, style='white')

Draw a filled high-resolution triangle using the specified style. Uses a scanline algorithm with subpixel precision.

Parameters:

Name Type Description Default
x0 float

The x-coordinate of the first vertex.

required
y0 float

The y-coordinate of the first vertex.

required
x1 float

The x-coordinate of the second vertex.

required
y1 float

The y-coordinate of the second vertex.

required
x2 float

The x-coordinate of the third vertex.

required
y2 float

The y-coordinate of the third vertex.

required
hires_mode HiResMode | None

The high-resolution mode to use.

None
style str

The style to apply to the characters.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_filled_hires_triangle(
    self,
    x0: float,
    y0: float,
    x1: float,
    y1: float,
    x2: float,
    y2: float,
    hires_mode: HiResMode | None = None,
    style: str = "white",
) -> None:
    """Draw a filled high-resolution triangle using the specified style.
    Uses a scanline algorithm with subpixel precision.

    Args:
        x0: The x-coordinate of the first vertex.
        y0: The y-coordinate of the first vertex.
        x1: The x-coordinate of the second vertex.
        y1: The y-coordinate of the second vertex.
        x2: The x-coordinate of the third vertex.
        y2: The y-coordinate of the third vertex.
        hires_mode: The high-resolution mode to use.
        style: The style to apply to the characters.
    """
    # Use default hires mode if none provided
    hires_mode = hires_mode or self.default_hires_mode

    # Sort vertices by y-coordinate (y0 <= y1 <= y2)
    if y0 > y1:
        x0, y0, x1, y1 = x1, y1, x0, y0
    if y1 > y2:
        x1, y1, x2, y2 = x2, y2, x1, y1
    if y0 > y1:
        x0, y0, x1, y1 = x1, y1, x0, y0

    # Skip if all points are the same or triangle has no height
    if abs(y0 - y2) < 1e-6:
        return

    # Initialize pixel collection
    pixels: list[tuple[float, float]] = []
    pixels_append = pixels.append

    # Define helper function to add a hi-res pixel
    def add_hires_pixel(x: float, y: float) -> None:
        pixels_append((x, y))

    # No edge parameters needed

    # Process based on triangle shape
    if abs(y1 - y0) < 1e-6:  # Flat top triangle
        self._fill_flat_top_hires_triangle(x0, y0, x1, y1, x2, y2, add_hires_pixel)
    elif abs(y1 - y2) < 1e-6:  # Flat bottom triangle
        self._fill_flat_bottom_hires_triangle(
            x0, y0, x1, y1, x2, y2, add_hires_pixel
        )
    else:  # General triangle - split into flat-top and flat-bottom
        # Calculate the interpolation factor for the split point
        t = (y1 - y0) / (y2 - y0)

        # Calculate the x-coordinate of the point on the long edge that has y = y1
        x3 = x0 + t * (x2 - x0)

        # Fill the flat bottom part
        self._fill_flat_bottom_hires_triangle(
            x0, y0, x1, y1, x3, y1, add_hires_pixel
        )

        # Fill the flat top part
        self._fill_flat_top_hires_triangle(x1, y1, x3, y1, x2, y2, add_hires_pixel)

    # Draw all hi-res pixels at once
    if pixels:
        self.set_hires_pixels(pixels, hires_mode, style)

draw_filled_quad(x0, y0, x1, y1, x2, y2, x3, y3, style='white')

Draw a filled quadrilateral using the specified style. Splits the quad into two triangles for filling.

Parameters:

Name Type Description Default
x0 int

The x-coordinate of the first vertex.

required
y0 int

The y-coordinate of the first vertex.

required
x1 int

The x-coordinate of the second vertex.

required
y1 int

The y-coordinate of the second vertex.

required
x2 int

The x-coordinate of the third vertex.

required
y2 int

The y-coordinate of the third vertex.

required
x3 int

The x-coordinate of the fourth vertex.

required
y3 int

The y-coordinate of the fourth vertex.

required
style str

The style to apply to the characters.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_filled_quad(
    self,
    x0: int,
    y0: int,
    x1: int,
    y1: int,
    x2: int,
    y2: int,
    x3: int,
    y3: int,
    style: str = "white",
) -> None:
    """Draw a filled quadrilateral using the specified style.
    Splits the quad into two triangles for filling.

    Args:
        x0: The x-coordinate of the first vertex.
        y0: The y-coordinate of the first vertex.
        x1: The x-coordinate of the second vertex.
        y1: The y-coordinate of the second vertex.
        x2: The x-coordinate of the third vertex.
        y2: The y-coordinate of the third vertex.
        x3: The x-coordinate of the fourth vertex.
        y3: The y-coordinate of the fourth vertex.
        style: The style to apply to the characters.
    """
    # Draw the quad as two filled triangles
    # First triangle (0, 1, 2)
    self.draw_filled_triangle(x0, y0, x1, y1, x2, y2, style)
    # Second triangle (0, 2, 3)
    self.draw_filled_triangle(x0, y0, x2, y2, x3, y3, style)

draw_filled_triangle(x0, y0, x1, y1, x2, y2, style='white')

Draw a filled triangle using the specified style. Uses a scanline algorithm for efficient filling.

Parameters:

Name Type Description Default
x0 int

The x-coordinate of the first vertex.

required
y0 int

The y-coordinate of the first vertex.

required
x1 int

The x-coordinate of the second vertex.

required
y1 int

The y-coordinate of the second vertex.

required
x2 int

The x-coordinate of the third vertex.

required
y2 int

The y-coordinate of the third vertex.

required
style str

The style to apply to the characters.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_filled_triangle(
    self,
    x0: int,
    y0: int,
    x1: int,
    y1: int,
    x2: int,
    y2: int,
    style: str = "white",
) -> None:
    """Draw a filled triangle using the specified style.
    Uses a scanline algorithm for efficient filling.

    Args:
        x0: The x-coordinate of the first vertex.
        y0: The y-coordinate of the first vertex.
        x1: The x-coordinate of the second vertex.
        y1: The y-coordinate of the second vertex.
        x2: The x-coordinate of the third vertex.
        y2: The y-coordinate of the third vertex.
        style: The style to apply to the characters.
    """
    # Sort vertices by y-coordinate (y0 <= y1 <= y2)
    if y0 > y1:
        x0, y0, x1, y1 = x1, y1, x0, y0
    if y1 > y2:
        x1, y1, x2, y2 = x2, y2, x1, y1
    if y0 > y1:
        x0, y0, x1, y1 = x1, y1, x0, y0

    # Skip if all points are the same or triangle has no height
    if (y0 == y1 == y2) or (y0 == y2):
        return

    # Initialize empty list for all pixel coordinates
    pixels: list[tuple[int, int]] = []
    pixels_append = pixels.append

    # Calculate interpolation factor for the middle point
    inv_slope02 = (x2 - x0) / (y2 - y0) if y2 != y0 else 0

    # First half of the triangle (bottom flat or top)
    if y1 == y0:  # Flat top triangle
        self._fill_flat_top_triangle(x0, y0, x1, y1, x2, y2, pixels_append)
    elif y1 == y2:  # Flat bottom triangle
        self._fill_flat_bottom_triangle(x0, y0, x1, y1, x2, y2, pixels_append)
    else:  # General triangle - split into flat-top and flat-bottom
        # Calculate the x-coordinate of the point on the long edge that has y = y1
        x3 = int(x0 + inv_slope02 * (y1 - y0))

        # Fill the flat bottom part
        self._fill_flat_bottom_triangle(x0, y0, x1, y1, x3, y1, pixels_append)

        # Fill the flat top part
        self._fill_flat_top_triangle(x1, y1, x3, y1, x2, y2, pixels_append)

    # Draw all pixels at once
    if pixels:
        self.set_pixels(pixels, "█", style)

draw_hires_circle(cx, cy, radius, hires_mode=None, style='white')

Draw a circle with high-resolution support using Bresenham's algorithm. Compensates for 2:1 aspect ratio.

Parameters:

Name Type Description Default
cx float

X-coordinate of the center of the circle.

required
cy float

Y-coordinate of the center of the circle.

required
radius float

Radius of the circle.

required
hires_mode HiResMode

The high-resolution mode to use.

None
style str

Style of the pixels to be drawn.

'white'
Source code in src/textual_hires_canvas/canvas.py
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
def draw_hires_circle(
    self,
    cx: float,
    cy: float,
    radius: float,
    hires_mode: HiResMode | None = None,
    style: str = "white",
) -> None:
    """Draw a circle with high-resolution support using Bresenham's algorithm. Compensates for 2:1 aspect ratio.

    Args:
        cx (float): X-coordinate of the center of the circle.
        cy (float): Y-coordinate of the center of the circle.
        radius (float): Radius of the circle.
        hires_mode (HiResMode): The high-resolution mode to use.
        style (str): Style of the pixels to be drawn.
    """
    # Early rejection for invalid inputs
    if radius <= 0:
        return

    # Initialize an empty list for collecting pixel coordinates
    # (We calculate estimated_size only for the safety check below)
    estimated_size = int(
        radius * 16
    )  # Each step adds 8 pixels, estimate 4*radius steps
    pixels: list[tuple[float, float]] = []
    pixels_extend = pixels.extend  # Local reference for faster calls

    # Pre-compute constants
    scale_x = 1
    scale_y = 2
    aspect_ratio = scale_x / scale_y

    # Midpoint circle algorithm with floating point precision
    x: float = radius
    y: float = 0
    decision: float = 1 - radius

    # Pre-compute points for each octant to avoid repeated calculations
    def get_circle_points(x: float, y: float) -> list[tuple[float, float]]:
        y_scaled = y * aspect_ratio
        x_scaled = x * aspect_ratio
        return [
            (cx + x, cy + y_scaled),
            (cx - x, cy + y_scaled),
            (cx + x, cy - y_scaled),
            (cx - x, cy - y_scaled),
            (cx + y, cy + x_scaled),
            (cx - y, cy + x_scaled),
            (cx + y, cy - x_scaled),
            (cx - y, cy - x_scaled),
        ]

    # Generate points for the circle
    while y <= x:
        # Add all 8 points at once
        pixels_extend(get_circle_points(x, y))

        # Update position
        y += 0.5
        if decision <= 0:
            decision += 2 * y + 1
        else:
            x -= 0.5
            decision += 2 * (y - x) + 1

        # Safety check to avoid infinite loops
        if len(pixels) > estimated_size:
            break

    # Render all pixels at once
    self.set_hires_pixels(pixels, hires_mode, style)

draw_hires_line(x0, y0, x1, y1, hires_mode=None, style='white')

Draws a high-resolution line from (x0, y0) to (x1, y1) using the specified character and style.

Parameters:

Name Type Description Default
x0 float

The x-coordinate of the start of the line.

required
y0 float

The y-coordinate of the start of the line.

required
x1 float

The x-coordinate of the end of the line.

required
y1 float

The y-coordinate of the end of the line.

required
hires_mode HiResMode | None

The high-resolution mode to use.

None
style str

The style to apply to the character.

'white'
Source code in src/textual_hires_canvas/canvas.py
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
def draw_hires_line(
    self,
    x0: float,
    y0: float,
    x1: float,
    y1: float,
    hires_mode: HiResMode | None = None,
    style: str = "white",
) -> None:
    """Draws a high-resolution line from (x0, y0) to (x1, y1) using the specified character and style.

    Args:
        x0: The x-coordinate of the start of the line.
        y0: The y-coordinate of the start of the line.
        x1: The x-coordinate of the end of the line.
        y1: The y-coordinate of the end of the line.
        hires_mode: The high-resolution mode to use.
        style: The style to apply to the character.
    """
    self.draw_hires_lines([(x0, y0, x1, y1)], hires_mode, style)

draw_hires_lines(coordinates, hires_mode=None, style='white')

Draws multiple high-resolution lines from given coordinates using the specified character and style.

Parameters:

Name Type Description Default
coordinates Iterable[tuple[FloatScalar, FloatScalar, FloatScalar, FloatScalar]]

An iterable of tuples representing the coordinates of the lines.

required
hires_mode HiResMode | None

The high-resolution mode to use.

None
style str

The style to apply to the character.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_hires_lines(
    self,
    coordinates: Iterable[
        tuple[FloatScalar, FloatScalar, FloatScalar, FloatScalar]
    ],
    hires_mode: HiResMode | None = None,
    style: str = "white",
) -> None:
    """Draws multiple high-resolution lines from given coordinates using the specified character and style.

    Args:
        coordinates: An iterable of tuples representing the coordinates of the lines.
        hires_mode: The high-resolution mode to use.
        style: The style to apply to the character.
    """
    # Early out if no coordinates
    if not coordinates:
        return

    # Convert to list if not already for multiple passes
    coord_list = list(coordinates)

    hires_mode = hires_mode or self.default_hires_mode
    pixel_size = hires_sizes[hires_mode]

    # Pre-compute multiplication factors once
    w_factor = pixel_size.width
    h_factor = pixel_size.height
    inv_w_factor = 1.0 / w_factor
    inv_h_factor = 1.0 / h_factor

    # Initialize an empty list for collecting pixel coordinates
    pixels: list[tuple[float, float]] = []
    pixels_append = pixels.append  # Local reference for faster calls

    # Process each line
    for x0, y0, x1, y1 in coord_list:
        # Skip if both endpoints are outside canvas (optimization)
        if not self._canvas_region.contains(
            floor(x0), floor(y0)
        ) and not self._canvas_region.contains(floor(x1), floor(y1)):
            continue

        # Convert to high-res grid coordinates
        hx0 = floor(x0 * w_factor)
        hy0 = floor(y0 * h_factor)
        hx1 = floor(x1 * w_factor)
        hy1 = floor(y1 * h_factor)

        # Get line coordinates
        coords = self._get_line_coordinates(hx0, hy0, hx1, hy1)

        # Convert back to canvas space and add to pixel array
        # Use direct append and precalculated factors for better performance
        for x, y in coords:
            pixels_append((x * inv_w_factor, y * inv_h_factor))

    # Only make one call to set_hires_pixels with all points
    if pixels:
        self.set_hires_pixels(pixels, hires_mode, style)

draw_hires_quad(x0, y0, x1, y1, x2, y2, x3, y3, hires_mode=None, style='white')

Draw a high-resolution quadrilateral outline using the specified style.

Parameters:

Name Type Description Default
x0 float

The x-coordinate of the first vertex.

required
y0 float

The y-coordinate of the first vertex.

required
x1 float

The x-coordinate of the second vertex.

required
y1 float

The y-coordinate of the second vertex.

required
x2 float

The x-coordinate of the third vertex.

required
y2 float

The y-coordinate of the third vertex.

required
x3 float

The x-coordinate of the fourth vertex.

required
y3 float

The y-coordinate of the fourth vertex.

required
hires_mode HiResMode | None

The high-resolution mode to use.

None
style str

The style to apply to the characters.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_hires_quad(
    self,
    x0: float,
    y0: float,
    x1: float,
    y1: float,
    x2: float,
    y2: float,
    x3: float,
    y3: float,
    hires_mode: HiResMode | None = None,
    style: str = "white",
) -> None:
    """Draw a high-resolution quadrilateral outline using the specified style.

    Args:
        x0: The x-coordinate of the first vertex.
        y0: The y-coordinate of the first vertex.
        x1: The x-coordinate of the second vertex.
        y1: The y-coordinate of the second vertex.
        x2: The x-coordinate of the third vertex.
        y2: The y-coordinate of the third vertex.
        x3: The x-coordinate of the fourth vertex.
        y3: The y-coordinate of the fourth vertex.
        hires_mode: The high-resolution mode to use.
        style: The style to apply to the characters.
    """
    # Draw the four sides of the quadrilateral with high-resolution
    self.draw_hires_lines(
        [(x0, y0, x1, y1), (x1, y1, x2, y2), (x2, y2, x3, y3), (x3, y3, x0, y0)],
        hires_mode,
        style,
    )

draw_hires_triangle(x0, y0, x1, y1, x2, y2, hires_mode=None, style='white')

Draw a high-resolution triangle outline using the specified style.

Parameters:

Name Type Description Default
x0 float

The x-coordinate of the first vertex.

required
y0 float

The y-coordinate of the first vertex.

required
x1 float

The x-coordinate of the second vertex.

required
y1 float

The y-coordinate of the second vertex.

required
x2 float

The x-coordinate of the third vertex.

required
y2 float

The y-coordinate of the third vertex.

required
hires_mode HiResMode | None

The high-resolution mode to use.

None
style str

The style to apply to the characters.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_hires_triangle(
    self,
    x0: float,
    y0: float,
    x1: float,
    y1: float,
    x2: float,
    y2: float,
    hires_mode: HiResMode | None = None,
    style: str = "white",
) -> None:
    """Draw a high-resolution triangle outline using the specified style.

    Args:
        x0: The x-coordinate of the first vertex.
        y0: The y-coordinate of the first vertex.
        x1: The x-coordinate of the second vertex.
        y1: The y-coordinate of the second vertex.
        x2: The x-coordinate of the third vertex.
        y2: The y-coordinate of the third vertex.
        hires_mode: The high-resolution mode to use.
        style: The style to apply to the characters.
    """

    # Draw the three sides of the triangle with high-resolution
    self.draw_hires_lines(
        [(x0, y0, x1, y1), (x1, y1, x2, y2), (x2, y2, x0, y0)], hires_mode, style
    )

draw_line(x0, y0, x1, y1, char='█', style='white')

Draws a line from (x0, y0) to (x1, y1) using the specified character and style.

Parameters:

Name Type Description Default
x0 int

The x-coordinate of the start of the line.

required
y0 int

The y-coordinate of the start of the line.

required
x1 int

The x-coordinate of the end of the line.

required
y1 int

The y-coordinate of the end of the line.

required
char str

The character to draw.

'█'
style str

The style to apply to the character.

'white'
Source code in src/textual_hires_canvas/canvas.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
def draw_line(
    self, x0: int, y0: int, x1: int, y1: int, char: str = "█", style: str = "white"
) -> None:
    """Draws a line from (x0, y0) to (x1, y1) using the specified character and style.

    Args:
        x0: The x-coordinate of the start of the line.
        y0: The y-coordinate of the start of the line.
        x1: The x-coordinate of the end of the line.
        y1: The y-coordinate of the end of the line.
        char: The character to draw.
        style: The style to apply to the character.
    """
    if not self._canvas_region.contains(
        x0, y0
    ) and not self._canvas_region.contains(x1, y1):
        return
    self.set_pixels(self._get_line_coordinates(x0, y0, x1, y1), char, style)

draw_lines(coordinates, char='█', style='white')

Draws multiple lines from given coordinates using the specified character and style.

Parameters:

Name Type Description Default
coordinates Iterable[tuple[int, int, int, int]]

An iterable of tuples representing the coordinates of the lines.

required
char str

The character to draw.

'█'
style str

The style to apply to the character.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_lines(
    self,
    coordinates: Iterable[tuple[int, int, int, int]],
    char: str = "█",
    style: str = "white",
) -> None:
    """Draws multiple lines from given coordinates using the specified character and style.

    Args:
        coordinates: An iterable of tuples representing the coordinates of the lines.
        char: The character to draw.
        style: The style to apply to the character.
    """
    # Convert to list for multiple passes
    coord_list = list(coordinates)
    if not coord_list:
        return

    # Collect all pixels from all lines before rendering
    all_pixels = []

    for x0, y0, x1, y1 in coord_list:
        # Skip if both endpoints are outside the canvas
        if not self._canvas_region.contains(
            x0, y0
        ) and not self._canvas_region.contains(x1, y1):
            continue

        # Get coordinates for this line and extend the pixel collection
        line_pixels = self._get_line_coordinates(x0, y0, x1, y1)
        all_pixels.extend(line_pixels)

    # Draw all pixels at once with a single refresh
    if all_pixels:
        self.set_pixels(all_pixels, char, style)

draw_quad(x0, y0, x1, y1, x2, y2, x3, y3, style='white')

Draw a quadrilateral outline using the specified style.

Parameters:

Name Type Description Default
x0 int

The x-coordinate of the first vertex.

required
y0 int

The y-coordinate of the first vertex.

required
x1 int

The x-coordinate of the second vertex.

required
y1 int

The y-coordinate of the second vertex.

required
x2 int

The x-coordinate of the third vertex.

required
y2 int

The y-coordinate of the third vertex.

required
x3 int

The x-coordinate of the fourth vertex.

required
y3 int

The y-coordinate of the fourth vertex.

required
style str

The style to apply to the characters.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_quad(
    self,
    x0: int,
    y0: int,
    x1: int,
    y1: int,
    x2: int,
    y2: int,
    x3: int,
    y3: int,
    style: str = "white",
) -> None:
    """Draw a quadrilateral outline using the specified style.

    Args:
        x0: The x-coordinate of the first vertex.
        y0: The y-coordinate of the first vertex.
        x1: The x-coordinate of the second vertex.
        y1: The y-coordinate of the second vertex.
        x2: The x-coordinate of the third vertex.
        y2: The y-coordinate of the third vertex.
        x3: The x-coordinate of the fourth vertex.
        y3: The y-coordinate of the fourth vertex.
        style: The style to apply to the characters.
    """
    # Draw the four sides of the quadrilateral
    self.draw_lines(
        [(x0, y0, x1, y1), (x1, y1, x2, y2), (x2, y2, x3, y3), (x3, y3, x0, y0)],
        "█",
        style,
    )

draw_rectangle_box(x0, y0, x1, y1, thickness=1, style='white')

Draw a rectangle box with the specified thickness and style.

Parameters:

Name Type Description Default
x0 int

The x-coordinate of the top-left corner.

required
y0 int

The y-coordinate of the top-left corner.

required
x1 int

The x-coordinate of the bottom-right corner.

required
y1 int

The y-coordinate of the bottom-right corner.

required
thickness int

The thickness of the box.

1
style str

The style to apply to the characters.

'white'
Source code in src/textual_hires_canvas/canvas.py
 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
def draw_rectangle_box(
    self,
    x0: int,
    y0: int,
    x1: int,
    y1: int,
    thickness: int = 1,
    style: str = "white",
) -> None:
    """Draw a rectangle box with the specified thickness and style.

    Args:
        x0: The x-coordinate of the top-left corner.
        y0: The y-coordinate of the top-left corner.
        x1: The x-coordinate of the bottom-right corner.
        y1: The y-coordinate of the bottom-right corner.
        thickness: The thickness of the box.
        style: The style to apply to the characters.
    """
    # (x0, y0)     (x1, y0)
    #    ┌────────────┐
    #    │            │
    #    │            │
    #    │            │
    #    │            │
    #    └────────────┘
    # (x0, y1)     (x1, y1)

    # NOTE: A difference of 0 between coordinates results in a
    # width or height of 1 cell inside Textual.

    T = thickness
    x0, x1 = sorted((x0, x1))
    y0, y1 = sorted((y0, y1))

    # Both width and height are 1. This would just be a dot, so
    # we don't draw anything.
    if (x1 - x0 == 0) and (y1 - y0 == 0):
        return

    # We now know either the width or height must be higher than 2.
    # Height is 1, place two horizontal line enders.
    if y1 - y0 == 0:
        self.set_pixel(x0, y0, char=get_box((0, T, 0, 0)), style=style)
        self.set_pixel(x1, y0, char=get_box((0, 0, 0, T)), style=style)
        if x1 - x0 >= 2:
            # Width is greater than or equal to 3, draw a horizontal line
            # between the line enders.
            self.draw_line(
                x0 + 1, y0, x1 - 1, y1, char=get_box((0, T, 0, T)), style=style
            )
        return

    # Width is 1, place two vertical line enders.
    if x1 - x0 == 0:
        self.set_pixel(x0, y0, char=get_box((0, 0, T, 0)), style=style)
        self.set_pixel(x0, y1, char=get_box((T, 0, 0, 0)), style=style)
        if y1 - y0 >= 2:
            # Height is greater than or equal to 3, draw a horizontal line
            # between the line enders.
            self.draw_line(
                x0, y0 + 1, x1, y1 - 1, char=get_box((T, 0, T, 0)), style=style
            )
        return

    # The remaining conditions require all the corner pieces to be drawn.
    self.set_pixel(x0, y0, char=get_box((0, T, T, 0)), style=style)
    self.set_pixel(x1, y0, char=get_box((0, 0, T, T)), style=style)
    self.set_pixel(x1, y1, char=get_box((T, 0, 0, T)), style=style)
    self.set_pixel(x0, y1, char=get_box((T, T, 0, 0)), style=style)

    # If width and height are both 2, we don't need any lines. Only corners.
    if (x1 - x0 == 1) and (y1 - y0 == 1):
        return

    # Width is greater than or equal to 3, draw horizontal lines.
    if x1 - x0 >= 2:
        for y in y0, y1:
            self.draw_line(
                x0 + 1, y, x1 - 1, y, char=get_box((0, T, 0, T)), style=style
            )
    # Height is greater than or equal to 3, draw vertical lines.
    if y1 - y0 >= 2:
        for x in x0, x1:
            self.draw_line(
                x, y0 + 1, x, y1 - 1, char=get_box((T, 0, T, 0)), style=style
            )

draw_triangle(x0, y0, x1, y1, x2, y2, style='white')

Draw a triangle outline using the specified style.

Parameters:

Name Type Description Default
x0 int

The x-coordinate of the first vertex.

required
y0 int

The y-coordinate of the first vertex.

required
x1 int

The x-coordinate of the second vertex.

required
y1 int

The y-coordinate of the second vertex.

required
x2 int

The x-coordinate of the third vertex.

required
y2 int

The y-coordinate of the third vertex.

required
style str

The style to apply to the characters.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def draw_triangle(
    self,
    x0: int,
    y0: int,
    x1: int,
    y1: int,
    x2: int,
    y2: int,
    style: str = "white",
) -> None:
    """Draw a triangle outline using the specified style.

    Args:
        x0: The x-coordinate of the first vertex.
        y0: The y-coordinate of the first vertex.
        x1: The x-coordinate of the second vertex.
        y1: The y-coordinate of the second vertex.
        x2: The x-coordinate of the third vertex.
        y2: The y-coordinate of the third vertex.
        style: The style to apply to the characters.
    """

    # Draw the three sides of the triangle
    self.draw_lines(
        [(x0, y0, x1, y1), (x1, y1, x2, y2), (x2, y2, x0, y0)], "█", style
    )

get_pixel(x, y)

Retrieves the character and style of a single pixel at the given coordinates.

Parameters:

Name Type Description Default
x int

The x-coordinate of the pixel.

required
y int

The y-coordinate of the pixel.

required

Returns: A tuple containing the character and style of the pixel.

Source code in src/textual_hires_canvas/canvas.py
240
241
242
243
244
245
246
247
248
249
def get_pixel(self, x: int, y: int) -> tuple[str, str]:
    """Retrieves the character and style of a single pixel at the given coordinates.

    Args:
        x: The x-coordinate of the pixel.
        y: The y-coordinate of the pixel.
    Returns:
        A tuple containing the character and style of the pixel.
    """
    return self._buffer[y][x], self._styles[y][x]

render_line(y)

Renders a single line of the canvas at the given y-coordinate.

Parameters:

Name Type Description Default
y int

The y-coordinate of the line.

required

Returns: A Strip representing the line.

Source code in src/textual_hires_canvas/canvas.py
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
def render_line(self, y: int) -> Strip:
    """Renders a single line of the canvas at the given y-coordinate.

    Args:
        y: The y-coordinate of the line.
    Returns:
        A Strip representing the line.
    """
    # Fast path for out-of-bounds or uninitialized
    if not self._canvas_size.area or y >= self._canvas_size.height:
        return Strip.blank(cell_length=0)

    buffer_line = self._buffer[y]
    styles_line = self._styles[y]

    # Fast path for blank lines
    if all(char == " " for char in buffer_line):
        return Strip.blank(cell_length=len(buffer_line))

    # Create segments with batching by style
    segments: list[Segment] = []
    append = segments.append  # Local reference for faster calls

    # Batch processing for same style segments
    current_style_str = None
    current_style_obj = None
    current_text = ""

    for char, style_str in zip(buffer_line, styles_line):
        # When style changes, add current batch and start new one
        if style_str != current_style_str:
            # Add current batch if it exists
            if current_text:
                append(Segment(current_text, style=current_style_obj))

            # Start new batch
            current_style_str = style_str
            current_text = char

            # Get style object from cache or create new one
            if style_str:
                if style_str not in self._style_cache:
                    self._style_cache[style_str] = Style.parse(style_str)
                current_style_obj = self._style_cache[style_str]
            else:
                current_style_obj = None
        else:
            # Add to current batch
            current_text += char

    # Add the final batch
    if current_text:
        append(Segment(current_text, style=current_style_obj))

    return Strip(segments).simplify()

reset(size=None, refresh=True)

Resets the canvas to the specified size or to the current size if no size is provided. Clears buffers,styles and dirty cache, and resets the canvas size.

Parameters:

Name Type Description Default
size Size | None

The new size for the canvas.

None
refresh bool

Whether to refresh the canvas after resetting.

True

Returns: self for chaining.

Source code in src/textual_hires_canvas/canvas.py
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
def reset(self, size: Size | None = None, refresh: bool = True) -> None:
    """Resets the canvas to the specified size or to the current size if no size is provided.
    Clears buffers,styles and dirty cache, and resets the canvas size.

    Args:
        size: The new size for the canvas.
        refresh: Whether to refresh the canvas after resetting.
    Returns:
        self for chaining.
    """
    # Update size and regions if provided
    if size:
        self._canvas_size = size
        self._canvas_region = Region(0, 0, size.width, size.height)

    # Initialize buffers if we have a valid size
    if self._canvas_size:
        width = self._canvas_size.width
        height = self._canvas_size.height

        # More efficient buffer creation using list comprehension with multiplication
        # This is significantly faster than nested loops for large buffers
        self._buffer = [[" "] * width for _ in range(height)]
        self._styles = [[""] * width for _ in range(height)]

    # Only refresh if requested
    if refresh:
        self.refresh()

set_hires_pixels(coordinates, hires_mode=None, style='white')

Sets multiple pixels at the given coordinates using the specified Hi-Res mode.

Parameters:

Name Type Description Default
coordinates Iterable[tuple[FloatScalar, FloatScalar]]

An iterable of tuples representing the coordinates of the pixels.

required
hires_mode HiResMode | None

The Hi-Res mode to use.

None
style str

The style to apply to the character.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def set_hires_pixels(
    self,
    coordinates: Iterable[tuple[FloatScalar, FloatScalar]],
    hires_mode: HiResMode | None = None,
    style: str = "white",
) -> None:
    """Sets multiple pixels at the given coordinates using the specified Hi-Res mode.

    Args:
        coordinates: An iterable of tuples representing the coordinates of the pixels.
        hires_mode: The Hi-Res mode to use.
        style: The style to apply to the character.
    """
    # Use default mode if none provided
    hires_mode = hires_mode or self.default_hires_mode
    pixel_size = hires_sizes[hires_mode]
    pixel_info = pixels.get(hires_mode)
    assert pixel_info is not None

    # Group coordinates by their cell position to minimize buffer operations
    cells_to_update: dict[tuple[int, int], set[tuple[int, int]]] = {}

    # Pre-compute these values outside the loop for better performance
    w_factor = pixel_size.width
    h_factor = pixel_size.height

    # Process all coordinates and group them by their target cell
    for x, y in coordinates:
        # Early rejection for out-of-bounds
        if not self._canvas_region.contains(floor(x), floor(y)):
            continue

        # Calculate high-res coordinates
        hx = floor(x * w_factor)
        hy = floor(y * h_factor)

        # Calculate which cell this belongs to and offset within cell
        cell_x = hx // w_factor
        cell_y = hy // h_factor

        # Get or create the set for this cell
        cell_key = (cell_x, cell_y)
        if cell_key not in cells_to_update:
            cells_to_update[cell_key] = set()

        # Add this point to the cell's set
        offset_x = hx % w_factor
        offset_y = hy % h_factor
        cells_to_update[cell_key].add((offset_x, offset_y))

    # Process each cell that needs updating
    for (cell_x, cell_y), points in cells_to_update.items():
        # Create a small buffer just for this cell
        cell_buffer = np.zeros((pixel_size.height, pixel_size.width), dtype=bool)

        # Mark each point in the buffer
        for offset_x, offset_y in points:
            cell_buffer[offset_y, offset_x] = True

        # Convert to subpixels and look up the character
        subpixels = tuple(int(v) for v in cell_buffer.flat)
        if char := pixel_info[subpixels]:
            self.set_pixel(
                cell_x,
                cell_y,
                char=char,
                style=style,
            )

set_pixel(x, y, char='█', style='white')

Sets a single pixel at the given coordinates. Also marks it dirty for refreshing.

Parameters:

Name Type Description Default
x int

The x-coordinate of the pixel.

required
y int

The y-coordinate of the pixel.

required
char str

The character to draw.

'█'
style str

The style to apply to the character.

'white'
Source code in src/textual_hires_canvas/canvas.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
def set_pixel(self, x: int, y: int, char: str = "█", style: str = "white") -> None:
    """Sets a single pixel at the given coordinates.
    Also marks it dirty for refreshing.

    Args:
        x: The x-coordinate of the pixel.
        y: The y-coordinate of the pixel.
        char: The character to draw.
        style: The style to apply to the character.
    """
    # Fast rejection path without assert for performance
    if not (
        0 <= x < self._canvas_region.width and 0 <= y < self._canvas_region.height
    ):
        return

    self._buffer[y][x] = char
    self._styles[y][x] = style
    self.refresh()

set_pixels(coordinates, char='█', style='white')

Sets multiple pixels at the given coordinates.

Parameters:

Name Type Description Default
coordinates Iterable[tuple[int, int]]

An iterable of tuples representing the coordinates of the pixels.

required
char str

The character to draw.

'█'
style str

The style to apply to the character.

'white'
Source code in src/textual_hires_canvas/canvas.py
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
def set_pixels(
    self,
    coordinates: Iterable[tuple[int, int]],
    char: str = "█",
    style: str = "white",
) -> None:
    """Sets multiple pixels at the given coordinates.

    Args:
        coordinates: An iterable of tuples representing the coordinates of the pixels.
        char: The character to draw.
        style: The style to apply to the character.
    """
    # Check if we have coordinates
    coord_list = list(coordinates)
    if not coord_list:
        return

    # Batch updates to avoid calling refresh for each pixel
    # Cache properties for faster access in the loop
    buffer = self._buffer
    styles = self._styles
    width = self._canvas_region.width
    height = self._canvas_region.height

    # Process all pixels first, then refresh once
    for x, y in coord_list:
        if 0 <= x < width and 0 <= y < height:
            buffer[y][x] = char
            styles[y][x] = style

    # Only refresh once after all updates
    self.refresh()

write_text(x, y, text, align=TextAlign.LEFT)

Write text to the canvas at the specified position, with support for markup.

Parameters:

Name Type Description Default
x int

X-coordinate of the left edge of the text.

required
y int

Y-coordinate of the baseline of the text.

required
text str

Text to be written.

required
align TextAlign

The alignment of the text within the canvas.

LEFT
Source code in src/textual_hires_canvas/canvas.py
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
def write_text(
    self,
    x: int,
    y: int,
    text: str,
    align: TextAlign = TextAlign.LEFT,
) -> None:
    """Write text to the canvas at the specified position, with support for markup.

    Args:
        x (int): X-coordinate of the left edge of the text.
        y (int): Y-coordinate of the baseline of the text.
        text (str): Text to be written.
        align (TextAlign): The alignment of the text within the canvas.
    """
    if y < 0 or y >= self._canvas_size.height:
        return

    # parse markup
    rich_text = Text.from_markup(text)
    # store plain text
    if (plain_text := rich_text.plain) == "":
        return
    # store styles for each individual character
    rich_styles = []
    for c in rich_text.divide(range(1, len(plain_text))):
        style = Style()
        for span in c._spans:
            style += Style.parse(span.style)
        rich_styles.append(style)

    if align == TextAlign.RIGHT:
        x -= len(plain_text) - 1
    elif align == TextAlign.CENTER:
        div, mod = divmod(len(plain_text), 2)
        x -= div
        if mod == 0:
            # even number of characters, shift one to the right since I just
            # like that better -- DF
            x += 1

    if x <= -len(plain_text) or x >= self._canvas_size.width:
        # no part of text falls inside the canvas
        return

    overflow_left = -x
    overflow_right = x + len(plain_text) - self._canvas_size.width
    if overflow_left > 0:
        buffer_left = 0
        text_left = overflow_left
    else:
        buffer_left = x
        text_left = 0
    if overflow_right > 0:
        buffer_right = None
        text_right = -overflow_right
    else:
        buffer_right = x + len(plain_text)
        text_right = None

    self._buffer[y][buffer_left:buffer_right] = plain_text[text_left:text_right]
    self._styles[y][buffer_left:buffer_right] = [
        str(s) for s in rich_styles[text_left:text_right]
    ]
    assert len(self._buffer[y]) == self._canvas_size.width
    assert len(self._styles[y]) == self._canvas_size.width
    self.refresh()