omnipython-pygraphics 0.1.7.dev3__cp314-cp314-android_21_arm64_v8a.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1645 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Cross-platform 2D drawing for MicroPython, CircuitPython and CPython.
5
+
6
+ Draw shapes, text and images into a framebuffer. Every drawing call returns
7
+ the `Area` it changed, so you can send just that part to a screen.
8
+
9
+ The compiled module and the pure-Python package have the same names, except
10
+ four that write or read Python source and exist only in the pure-Python
11
+ package; each says so. `implementation()` tells you which one you have.
12
+
13
+ Example::
14
+
15
+ import pygraphics
16
+
17
+ fb = pygraphics.FrameBuffer(bytearray(16 * 16 * 2), 16, 16, pygraphics.RGB565)
18
+ fb.fill(0)
19
+ area = fb.fill_rect(1, 1, 6, 6, 0xFFFF)
20
+ pygraphics.text8(fb, "Hi", 0, 0, 0xFFFF)
21
+ """
22
+
23
+ from __future__ import annotations
24
+
25
+ from typing import Any, Iterator
26
+
27
+ __revision__: str
28
+ """Which source this build came from, from `git describe`: a release tag, or
29
+ the nearest one and the commit. Only the compiled module has it."""
30
+
31
+ # -- pixel formats ------------------------------------------------------------
32
+
33
+ MONO_VLSB: int
34
+ """1 bit per pixel; each byte is 8 pixels down a column, low bit at the top."""
35
+ MONO_HLSB: int
36
+ """1 bit per pixel; bytes run across a row, high bit on the left."""
37
+ MONO_HMSB: int
38
+ """1 bit per pixel; bytes run across a row, low bit on the left."""
39
+ GS2_HMSB: int
40
+ """2 bits per pixel: four-level greyscale."""
41
+ GS4_HMSB: int
42
+ """4 bits per pixel: sixteen-level greyscale."""
43
+ GS8: int
44
+ """8 bits per pixel: 256-level greyscale."""
45
+ RGB565: int
46
+ """16 bits per pixel, the format of most small colour screens."""
47
+ RGB888: int
48
+ """24 bits per pixel, stored red, green, blue. pygraphics' own format, not
49
+ `framebuf`'s."""
50
+
51
+ class Area:
52
+ """Rectangular geometry helper used for dirty-rectangle updates, hit testing,
53
+ and clipping.
54
+
55
+ `Area` values are commonly returned by drawing helpers such as
56
+ `fill_rect` and `circle` and can be used to refresh only the
57
+ pixels that changed. They are also useful for widget hit testing, button
58
+ bounds, and trimming a draw operation to a sub-region of the screen.
59
+
60
+ The constructor accepts either separate ``(x, y, w, h)`` values or a single
61
+ tuple containing those four values.
62
+
63
+ Attributes:
64
+ x (int | float): The x-coordinate of the top-left corner of the area.
65
+ y (int | float): The y-coordinate of the top-left corner of the area.
66
+ w (int | float): The width of the area.
67
+ h (int | float): The height of the area.
68
+ """
69
+
70
+ x: int | float
71
+ y: int | float
72
+ w: int | float
73
+ h: int | float
74
+
75
+ def __init__(self, x: int | float | tuple[int | float, int | float, int | float, int | float], y: int | float | None = None, w: int | float | None = None, h: int | float | None = None) -> None:
76
+ """Create an area from explicit coordinates or from a single ``(x, y, w, h)`` tuple.
77
+
78
+ Args:
79
+ x (int | float | tuple): The left edge, or a tuple containing
80
+ ``(x, y, w, h)``.
81
+ y (int | float): The top edge.
82
+ w (int | float): The width of the area.
83
+ h (int | float): The height of the area.
84
+ """
85
+
86
+ def contains(self, x: int | tuple, y: int | None = None) -> bool:
87
+ """Checks if the specified point is contained within the area.
88
+
89
+ Args:
90
+ x (int | tuple): The x-coordinate of the point to check
91
+ or a tuple containing the x and y coordinates of the point.
92
+ y (int): The y-coordinate of the point to check.
93
+
94
+ Returns:
95
+ (bool): True if the point is contained within the area, False otherwise.
96
+ """
97
+
98
+ def contains_area(self, other: Area) -> bool:
99
+ """Checks if the specified area is contained within the area.
100
+
101
+ Args:
102
+ other (Area): The other area to check.
103
+
104
+ Returns:
105
+ (bool): True if the other area is contained within the area, False otherwise.
106
+ """
107
+
108
+ def intersects(self, other: Area) -> bool:
109
+ """Checks if the current Area object intersects with another Area object.
110
+
111
+ Args:
112
+ other (Area): The other Area object to check for overlap.
113
+
114
+ Returns:
115
+ (bool): True if the two Area objects intersect, False otherwise.
116
+ """
117
+
118
+ def touches_or_intersects(self, other: Area) -> bool:
119
+ """Checks if the current Area object touches or intersects with another Area object.
120
+
121
+ Args:
122
+ other (Area): The other Area object to check for overlap or touch.
123
+
124
+ Returns:
125
+ (bool): True if the two Area objects touch or intersect, False otherwise.
126
+ """
127
+
128
+ def shift(self, dx: int | float = 0, dy: int | float = 0) -> Area:
129
+ """Returns a new Area shifted by the specified amount in the x and y directions.
130
+
131
+ Args:
132
+ dx (int | float): The amount to shift the area in the x direction.
133
+ dy (int | float): The amount to shift the area in the y direction.
134
+
135
+ Returns:
136
+ (Area): A new Area object shift by the specified amount in the x and y directions.
137
+ """
138
+
139
+ def clip(self, other: Area) -> Area:
140
+ """Clips the current Area object to the specified Area object.
141
+
142
+ Args:
143
+ other (Area): The other Area object to clip to.
144
+
145
+ Returns:
146
+ (Area): A new Area object representing the clipped area.
147
+ """
148
+
149
+ def offset(self, d1: int | float, d2: int | float | None = None, d3: int | float | None = None, d4: int | float | None = None) -> Area:
150
+ """Returns a new Area offset by the specified amount(s).
151
+
152
+ If only one argument is provided, it is used as the offset in all 4 directions.
153
+ If two arguments are provided, the first is used as the offset in the x direction and the second as the offset in the y direction.
154
+ If three arguments are provided, they are used as the offsets in the left, top/bottom, and right directions, respectively.
155
+ If four arguments are provided, they are used as the offsets in the left, top, right, and bottom directions, respectively.
156
+
157
+ Args:
158
+ d1 (int | float): The offset in the x direction or the offset in all 4 directions.
159
+ d2 (int | float): The offset in the y direction or the offset in the top/bottom direction.
160
+ d3 (int | float): The offset in the right direction.
161
+ d4 (int | float): The offset in the bottom direction.
162
+
163
+ Returns:
164
+ (Area): A new Area object offset by the specified amount(s).
165
+ """
166
+
167
+ def inset(self, d1: int | float, d2: int | float | None = None, d3: int | float | None = None, d4: int | float | None = None) -> Area:
168
+ """Returns a new Area inset by the specified amount(s).
169
+
170
+ If only one argument is provided, it is used as the inset in all 4 directions.
171
+ If two arguments are provided, the first is used as the inset in the x direction and the second as the inset in the y direction.
172
+ If three arguments are provided, they are used as the insets in the left, top/bottom, and right directions, respectively.
173
+ If four arguments are provided, they are used as the insets in the left, top, right, and bottom directions, respectively.
174
+
175
+ Args:
176
+ d1 (int | float): The inset in the x direction or the inset in all 4 directions.
177
+ d2 (int | float): The inset in the y direction or the inset in the top/bottom direction.
178
+ d3 (int | float): The inset in the right direction.
179
+ d4 (int | float): The inset in the bottom direction.
180
+
181
+ Returns:
182
+ (Area): A new Area object inset by the specified amount(s).
183
+ """
184
+
185
+ def __eq__(self, other: object) -> bool:
186
+ """Checks if the current Area object is equal to another Area object.
187
+
188
+ Args:
189
+ other (Area): The other Area object to compare with.
190
+
191
+ Returns:
192
+ (bool): True if the two Area objects are equal, False otherwise.
193
+ """
194
+
195
+ def __ne__(self, other: object) -> bool:
196
+ """Checks if the current Area object is not equal to another Area object.
197
+
198
+ Args:
199
+ other (Area): The other Area object to compare with.
200
+
201
+ Returns:
202
+ (bool): True if the two Area objects are not equal, False otherwise.
203
+ """
204
+
205
+ def __add__(self, other: Area) -> Area:
206
+ """Computes the union of the current Area object and another Area object.
207
+
208
+ Args:
209
+ other (Area): The other Area object to compute the union with.
210
+
211
+ Returns:
212
+ (Area): A new Area object representing the union of the two areas.
213
+ """
214
+
215
+ def __bool__(self) -> bool:
216
+ """True when the area has positive width and height."""
217
+
218
+ def __iter__(self) -> Iterator[int | float]:
219
+ """Returns an iterator over the elements of the Area object.
220
+
221
+ Returns:
222
+ (iterator): An iterator over the elements of the Area object.
223
+ """
224
+
225
+ class FrameBuffer:
226
+ """Framebuffer surface for drawing either to the display itself or to an
227
+ off-screen buffer.
228
+
229
+ This class preserves the standard ``framebuf`` API while adding helpers for
230
+ shapes, text, and image I/O. Each draw operation returns an `Area`
231
+ describing the pixels that changed, which is ideal for dirty-rectangle
232
+ updates and for compositing a pre-rendered scene before pushing it to the
233
+ display.
234
+
235
+ Typical usage is to create a ``bytearray`` large enough for the target size,
236
+ draw a frame with `fill`, `fill_rect`, `circle`, or
237
+ `text8`, then copy that buffer to a display driver or use the returned
238
+ ``Area`` to refresh only the changed region.
239
+
240
+ Args:
241
+ buffer (bytearray): Framebuffer buffer.
242
+ width (int): Width in pixels.
243
+ height (int): Height in pixels.
244
+ format (int): Framebuffer format constant such as ``RGB565`` or ``RGB888``.
245
+ """
246
+
247
+ def __init__(self, buffer: bytearray, width: int, height: int, format: int, *args: Any, **kwargs: Any) -> None:
248
+ """Create a framebuffer wrapping ``buffer``.
249
+
250
+ Args:
251
+ buffer (bytearray): Pixel storage (writable).
252
+ width (int): Width in pixels.
253
+ height (int): Height in pixels.
254
+ format (int): Pixel format constant (``RGB565``, ``RGB888``, etc.).
255
+ *args (Any): Forwarded to the base ``framebuf.FrameBuffer`` (e.g. stride).
256
+ **kwargs (Any): Forwarded to the base constructor (e.g. ``stride=``).
257
+
258
+ Raises:
259
+ ValueError: If ``format`` is not a supported constant.
260
+ """
261
+
262
+ @property
263
+ def color_depth(self) -> int:
264
+ """Bits per pixel for the current format (1, 2, 4, 8, 16, or 24)."""
265
+
266
+ @property
267
+ def width(self) -> int:
268
+ """Framebuffer width in pixels."""
269
+
270
+ @property
271
+ def height(self) -> int:
272
+ """Framebuffer height in pixels."""
273
+
274
+ @property
275
+ def buffer(self) -> bytearray | memoryview:
276
+ """Underlying pixel buffer passed to the constructor."""
277
+
278
+ @property
279
+ def format(self) -> int:
280
+ """Pixel format constant (``RGB565``, ``RGB888``, ``MONO_HLSB``, …)."""
281
+
282
+ def fill_rect(self, x: int, y: int, w: int, h: int, c: int) -> Area:
283
+ """Fill a rectangle with a solid color.
284
+
285
+ This is the usual primitive for clearing a region, drawing panels, or
286
+ staging a background before more detailed shapes or text are drawn.
287
+
288
+ Args:
289
+ x (int): X coordinate of the top-left corner.
290
+ y (int): Y coordinate of the top-left corner.
291
+ w (int): Width in pixels.
292
+ h (int): Height in pixels.
293
+ c (int): Color to write.
294
+
295
+ Returns:
296
+ Area: Bounding box of the filled rectangle.
297
+ """
298
+
299
+ def pixel(self, x: int, y: int, c: int | None = None) -> Area | int:
300
+ """Read or draw a single pixel.
301
+
302
+ With ``c`` it draws the pixel; without, it reads the pixel's color.
303
+
304
+ Args:
305
+ x (int): x coordinate
306
+ y (int): y coordinate
307
+ c (int): color to draw (default: None, to read the pixel)
308
+
309
+ Returns:
310
+ (Area | int): The bounding box of the pixel when drawing, or the
311
+ pixel's color when reading.
312
+ """
313
+
314
+ def fill(self, c: int) -> Area:
315
+ """Fill the buffer with the given color.
316
+
317
+ Args:
318
+ c (int): color
319
+
320
+ Returns:
321
+ (Area): Bounding box of the filled buffer
322
+ """
323
+
324
+ def ellipse(self, x: int, y: int, rx: int, ry: int, c: int, f: bool = False, m: int = 15) -> Area:
325
+ """Draw an ellipse at the given location, radii and color.
326
+
327
+ Args:
328
+ x (int): Center x coordinate
329
+ y (int): Center y coordinate
330
+ rx (int): X radius
331
+ ry (int): Y radius
332
+ c (int): color
333
+ f (bool): Fill the ellipse (default: False)
334
+ m (int): Bitmask to determine which quadrants to draw (default: 0b1111)
335
+
336
+ Returns:
337
+ (Area): Bounding box of the ellipse
338
+ """
339
+
340
+ def hline(self, x: int, y: int, w: int, c: int) -> Area:
341
+ """Draw a horizontal line at the given location, width and color.
342
+
343
+ Args:
344
+ x (int): x coordinate
345
+ y (int): y coordinate
346
+ w (int): Width in pixels
347
+ c (int): color
348
+
349
+ Returns:
350
+ (Area): Bounding box of the horizontal line
351
+ """
352
+
353
+ def line(self, x1: int, y1: int, x2: int, y2: int, c: int) -> Area:
354
+ """Draw a line between the given start and end points and color.
355
+
356
+ Args:
357
+ x1 (int): Start x coordinate
358
+ y1 (int): Start y coordinate
359
+ x2 (int): End x coordinate
360
+ y2 (int): End y coordinate
361
+ c (int): color
362
+
363
+ Returns:
364
+ (Area): Bounding box of the line
365
+ """
366
+
367
+ def poly(self, x: int, y: int, coords: list[int] | tuple[int, ...], c: int, f: bool = False) -> Area:
368
+ """Draw a polygon at the given location, coordinates and color.
369
+
370
+ Args:
371
+ x (int): x coordinate
372
+ y (int): y coordinate
373
+ coords (array): Array of x, y coordinate tuples
374
+ c (int): color
375
+ f (bool): Fill the polygon (default: False)
376
+
377
+ Raises:
378
+ MemoryError: In the compiled module, a filled polygon of more than 256 vertices needs one int of
379
+ scratch memory per vertex, and there wasn't enough. Nothing is drawn. A polygon of any
380
+ size fills; 256 vertices or fewer allocates nothing.
381
+
382
+ Returns:
383
+ (Area): Bounding box of the polygon
384
+ """
385
+
386
+ def rect(self, x: int, y: int, w: int, h: int, c: int, f: bool = False) -> Area:
387
+ """Draw a rectangle at the given location, size and color.
388
+
389
+ Args:
390
+ x (int): Top left corner x coordinate
391
+ y (int): Top left corner y coordinate
392
+ w (int): Width in pixels
393
+ h (int): Height in pixels
394
+ c (int): color
395
+ f (bool): Fill the rectangle (default: False)
396
+
397
+ Returns:
398
+ (Area): Bounding box of the rectangle
399
+ """
400
+
401
+ def vline(self, x: int, y: int, h: int, c: int) -> Area:
402
+ """Draw a vertical line at the given location, height and color.
403
+
404
+ Args:
405
+ x (int): x coordinate
406
+ y (int): y coordinate
407
+ h (int): Height in pixels
408
+ c (int): color
409
+
410
+ Returns:
411
+ (Area): Bounding box of the vertical line
412
+ """
413
+
414
+ def text(self, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data: str | None = None, height: int = 8) -> Area:
415
+ """Draw text at the given location, using the given font and color.
416
+
417
+ Args:
418
+ s (str): Text to draw
419
+ x (int): x coordinate
420
+ y (int): y coordinate
421
+ c (int): color
422
+ scale (int): Scale factor (default: 1)
423
+ inverted (bool): Invert the text (default: False)
424
+ font_data (str): Path to the font file (default: None)
425
+ height (int): Height of the font (default: 8)
426
+
427
+ Returns:
428
+ (Area): Bounding box of the text
429
+ """
430
+
431
+ def blit(self, source, x: int, y: int, key: int = -1, palette: Any = None) -> Area:
432
+ """Blit the given buffer at the given location.
433
+
434
+ Args:
435
+ source (FrameBuffer): FrameBuffer to blit
436
+ x (int): x coordinate
437
+ y (int): y coordinate
438
+ key (int): Color key (default: -1)
439
+ palette (FrameBuffer): Palette (default: None)
440
+
441
+ Returns:
442
+ (Area): Bounding box of the blitted buffer
443
+ """
444
+
445
+ def arc(self, x: int, y: int, r: int, a0: float, a1: float, c: int) -> Area:
446
+ """Arc drawing function. Will draw a single pixel wide arc with a radius r
447
+ centered at x, y from a0 to a1.
448
+
449
+ Args:
450
+ x (int): X-coordinate of the arc's center.
451
+ y (int): Y-coordinate of the arc's center.
452
+ r (int): Radius of the arc.
453
+ a0 (float): Starting angle in degrees.
454
+ a1 (float): Ending angle in degrees.
455
+ c (int): color.
456
+
457
+ Returns:
458
+ (Area): The bounding box of the arc.
459
+ """
460
+
461
+ def blit_rect(self, buf, x: int, y: int, w: int, h: int) -> Area:
462
+ """Blit a rectangular area from a buffer to the canvas. Uses the canvas's blit_rect method if available,
463
+ otherwise writes directly to the buffer.
464
+
465
+ Args:
466
+ buf (memoryview): Buffer to blit. Must already be byte-swapped if necessary.
467
+ x (int): X-coordinate to blit to.
468
+ y (int): Y-coordinate to blit to.
469
+ w (int): Width of the area to blit.
470
+ h (int): Height of the area to blit.
471
+
472
+ Returns:
473
+ (Area): The bounding box of the blitted area.
474
+ """
475
+
476
+ def blit_transparent(self, buf, x: int, y: int, w: int, h: int, key: int) -> Area:
477
+ """Blit a buffer with transparency.
478
+
479
+ Args:
480
+ buf (memoryview): Buffer to blit.
481
+ x (int): X-coordinate to blit to.
482
+ y (int): Y-coordinate to blit to.
483
+ w (int): Width of the area to blit.
484
+ h (int): Height of the area to blit.
485
+ key (int): Key value for transparency.
486
+
487
+ Returns:
488
+ (Area): The bounding box of the blitted area.
489
+ """
490
+
491
+ def blit_alpha(self, buf, x: int, y: int, w: int, h: int, color: int) -> Area:
492
+ """Tint ``color`` onto the framebuffer through an 8-bit alpha mask.
493
+
494
+ ``buf`` holds ``w * h`` alpha values, one byte each, row by row. Each
495
+ pixel becomes ``(color * a + old * (255 - a) + 127) // 255``, worked out
496
+ per channel (5, 6 and 5 bits for ``RGB565``, 8 each for ``RGB888``, one
497
+ for ``GS8``), so alpha 0 leaves the pixel alone and 255 writes ``color``.
498
+ ``color`` is a pixel value in the framebuffer's own format, the same
499
+ number ``pixel`` takes. The mask is read where it lives and never
500
+ copied, so a ``memoryview`` over a frozen ``bytes`` literal stays in
501
+ flash. Parts of the mask outside the framebuffer are clipped.
502
+
503
+ Args:
504
+ buf (bytes): Alpha mask: ``bytes``, ``bytearray`` or ``memoryview``.
505
+ x (int): X-coordinate of the mask's top-left corner.
506
+ y (int): Y-coordinate of the mask's top-left corner.
507
+ w (int): Mask width in pixels.
508
+ h (int): Mask height in pixels.
509
+ color (int): Tint, in the framebuffer's pixel format.
510
+
511
+ Returns:
512
+ (Area): The bounding box of the mask.
513
+
514
+ Raises:
515
+ ValueError: If the format is not ``RGB565``, ``RGB888`` or ``GS8``,
516
+ if ``w`` or ``h`` is negative, or if ``buf`` is shorter than
517
+ ``w * h``.
518
+ """
519
+
520
+ def circle(self, x0: int, y0: int, r: int, c: int, f: bool = False) -> Area:
521
+ """Circle drawing function. Will draw a single pixel wide circle
522
+ centered at x0, y0 and the specified r.
523
+
524
+ Args:
525
+ x0 (int): Center x coordinate
526
+ y0 (int): Center y coordinate
527
+ r (int): Radius
528
+ c (int): Color
529
+ f (bool): Fill the circle (default: False)
530
+
531
+ Returns:
532
+ (Area): The bounding box of the circle.
533
+ """
534
+
535
+ def gradient_rect(self, x: int, y: int, w: int, h: int, c1: int, c2: int | None = None, vertical: bool = True) -> Area:
536
+ """Fill a rectangle with a gradient.
537
+
538
+ Args:
539
+ x (int): X-coordinate of the top-left corner of the rectangle.
540
+ y (int): Y-coordinate of the top-left corner of the rectangle.
541
+ w (int): Width of the rectangle.
542
+ h (int): Height of the rectangle.
543
+ c1 (int): 565 encoded color for the top or left edge.
544
+ c2 (int): 565 encoded color for the bottom or right edge. If None or the same as c1,
545
+ fill_rect will be called instead.
546
+ vertical (bool): If True, the gradient will be vertical. If False, the gradient will be horizontal.
547
+
548
+ Returns:
549
+ (Area): The bounding box of the filled area.
550
+ """
551
+
552
+ def polygon(self, points: list, x: int, y: int, color: int, angle: float = 0, center_x: int = 0, center_y: int = 0) -> Area:
553
+ """Draw a polygon on the canvas.
554
+
555
+ Args:
556
+ points (list): List of points to draw.
557
+ x (int): X-coordinate of the polygon's position.
558
+ y (int): Y-coordinate of the polygon's position.
559
+ color (int): color.
560
+ angle (float): Rotation angle in radians (default: 0).
561
+ center_x (int): X-coordinate of the rotation center (default: 0).
562
+ center_y (int): Y-coordinate of the rotation center (default: 0).
563
+
564
+ Raises:
565
+ ValueError: If the polygon has less than 3 points.
566
+
567
+ Returns:
568
+ (Area): The bounding box of the polygon.
569
+ """
570
+
571
+ def round_rect(self, x0: int, y0: int, w: int, h: int, r: int, c: int, f: bool = False) -> Area:
572
+ """Rounded rectangle drawing function. Will draw a single pixel wide rounded rectangle starting at
573
+ x0, y0 and extending w, h pixels with the specified radius.
574
+
575
+ Args:
576
+ x0 (int): X-coordinate of the top-left corner of the rectangle.
577
+ y0 (int): Y-coordinate of the top-left corner of the rectangle.
578
+ w (int): Width of the rectangle.
579
+ h (int): Height of the rectangle.
580
+ r (int): Radius of the corners.
581
+ c (int): color.
582
+ f (bool): Fill the rectangle (default: False).
583
+
584
+ Returns:
585
+ (Area): The bounding box of the rectangle.
586
+ """
587
+
588
+ def triangle(self, x0: int, y0: int, x1: int, y1: int, x2: int, y2: int, c: int, f: bool = False) -> Area:
589
+ """Triangle drawing function. Draws a single pixel wide triangle with vertices at
590
+ (x0, y0), (x1, y1), and (x2, y2).
591
+
592
+ Args:
593
+ x0 (int): X-coordinate of the first vertex.
594
+ y0 (int): Y-coordinate of the first vertex.
595
+ x1 (int): X-coordinate of the second vertex.
596
+ y1 (int): Y-coordinate of the second vertex.
597
+ x2 (int): X-coordinate of the third vertex.
598
+ y2 (int): Y-coordinate of the third vertex.
599
+ c (int): color.
600
+ f (bool): Fill the triangle (default: False).
601
+
602
+ Returns:
603
+ (Area): The bounding box of the triangle.
604
+ """
605
+
606
+ def text8(self, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data: str | None = None) -> Area:
607
+ """Place text on the canvas with an 8 pixel high font.
608
+ Breaks on ``\\n`` to next line. Does not break on line going off canvas.
609
+
610
+ Args:
611
+ s (str): The text to draw.
612
+ x (int): The x position to start drawing the text.
613
+ y (int): The y position to start drawing the text.
614
+ c (int): The color to draw the text in. Default is 1.
615
+ scale (int): The scale factor to draw the text at. Default is 1.
616
+ inverted (bool): If True, draw the text inverted. Default is False.
617
+ font_data (str): The path to the font file to use. Default is None.
618
+
619
+ Returns:
620
+ Area: The area that was drawn to.
621
+ """
622
+
623
+ def text14(self, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data: str | None = None) -> Area:
624
+ """Place text on the canvas with a 14 pixel high font.
625
+ Breaks on ``\\n`` to next line. Does not break on line going off canvas.
626
+
627
+ Args:
628
+ s (str): The text to draw.
629
+ x (int): The x position to start drawing the text.
630
+ y (int): The y position to start drawing the text.
631
+ c (int): The color to draw the text in. Default is 1.
632
+ scale (int): The scale factor to draw the text at. Default is 1.
633
+ inverted (bool): If True, draw the text inverted. Default is False.
634
+ font_data (str): The path to the font file to use. Default is None.
635
+
636
+ Returns:
637
+ Area: The area that was drawn to.
638
+ """
639
+
640
+ def text16(self, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data: str | None = None) -> Area:
641
+ """Place text on the canvas with a 16 pixel high font.
642
+ Breaks on ``\\n`` to next line. Does not break on line going off canvas.
643
+
644
+ Args:
645
+ s (str): The text to draw.
646
+ x (int): The x position to start drawing the text.
647
+ y (int): The y position to start drawing the text.
648
+ c (int): The color to draw the text in. Default is 1.
649
+ scale (int): The scale factor to draw the text at. Default is 1.
650
+ inverted (bool): If True, draw the text inverted. Default is False.
651
+ font_data (str): The path to the font file to use. Default is None.
652
+
653
+ Returns:
654
+ Area: The area that was drawn to.
655
+ """
656
+
657
+ def scroll(self, xstep: int, ystep: int) -> Area:
658
+ """Scroll buffer contents. Returns the full buffer bounds."""
659
+
660
+ def save(self, filename: str | None = None) -> str:
661
+ """Save the framebuffer to a file. The file extension must match the format, otherwise
662
+ the extension will be appended to the filename.
663
+
664
+ Saves 1-bit formats as PBM, 2-bit formats as PGM with max value 3, 4-bit formats as PGM with max value 15,
665
+ 8-bit formats as PGM with max value 255, and 16-bit formats as BMP.
666
+
667
+ Args:
668
+ filename (str): Filename to save to
669
+ """
670
+
671
+ def export(self, filename: str) -> str:
672
+ """Export this framebuffer as an importable ``.py`` bitmap module.
673
+
674
+ Only the pure-Python package has this.
675
+
676
+ See `pygraphics._files.export_framebuffer`. Ships ``BITMAP`` as a
677
+ ``bytearray`` for zero-copy `from_bitmap` on MicroPython.
678
+ """
679
+
680
+ @staticmethod
681
+ def from_file(filename: str) -> FrameBuffer:
682
+ """Load a framebuffer from a file.
683
+
684
+ Args:
685
+ filename (str): Filename to load from
686
+
687
+ Returns:
688
+ FrameBuffer: Image loaded as a framebuffer.
689
+ """
690
+
691
+ @staticmethod
692
+ def from_bitmap(buffer: bytearray | memoryview | bytes, width: int, height: int, format: int) -> FrameBuffer:
693
+ """Build a ``FrameBuffer`` from a bitmap buffer (export-module style).
694
+
695
+ If ``buffer`` is already a writable ``bytearray`` (or a writable
696
+ ``memoryview``), it is used without copying. Legacy ``bytes`` /
697
+ ``memoryview(bytes)`` modules get one ``bytearray`` copy so MicroPython
698
+ can own a writable pixel buffer.
699
+
700
+ Only the pure-Python package has this.
701
+ """
702
+
703
+ @staticmethod
704
+ def from_module(mod: Any) -> FrameBuffer:
705
+ """Load from a module with ``WIDTH``, ``HEIGHT``, ``FORMAT``, ``BITMAP``.
706
+
707
+ Only the pure-Python package has this.
708
+ """
709
+
710
+ def arc(canvas, x: int, y: int, r: int, a0: float, a1: float, c: int) -> Area:
711
+ """Arc drawing function. Will draw a single pixel wide arc with a radius r
712
+ centered at x, y from a0 to a1.
713
+
714
+ Args:
715
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
716
+ x (int): X-coordinate of the arc's center.
717
+ y (int): Y-coordinate of the arc's center.
718
+ r (int): Radius of the arc.
719
+ a0 (float): Starting angle in degrees.
720
+ a1 (float): Ending angle in degrees.
721
+ c (int): color.
722
+
723
+ Returns:
724
+ (Area): The bounding box of the arc.
725
+ """
726
+
727
+ def blit(canvas, source, x: int, y: int, key: int = -1, palette: Any = None) -> Area:
728
+ """Copy a source buffer or sprite onto a canvas.
729
+
730
+ This is useful for compositing an off-screen frame into the live display,
731
+ for scrolling text and sprite animations, and for drawing pre-rendered tiles
732
+ or bitmaps. The returned area is the region that was written so that it can
733
+ be treated as a dirty rectangle.
734
+
735
+ Args:
736
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
737
+ source (FrameBuffer): Source framebuffer or canvas-like object.
738
+ x (int): X-coordinate to blit to.
739
+ y (int): Y-coordinate to blit to.
740
+ key (int): Key value for transparency (default: -1).
741
+ palette (Palette): Palette object for color translation (default: None).
742
+
743
+ Returns:
744
+ Area: The bounding box of the blitted area.
745
+ """
746
+
747
+ def blit_rect(canvas, buf, x: int, y: int, w: int, h: int) -> Area:
748
+ """Blit a rectangular area from a buffer to the canvas. Uses the canvas's blit_rect method if available,
749
+ otherwise writes directly to the buffer.
750
+
751
+ Args:
752
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
753
+ buf (memoryview): Buffer to blit. Must already be byte-swapped if necessary.
754
+ x (int): X-coordinate to blit to.
755
+ y (int): Y-coordinate to blit to.
756
+ w (int): Width of the area to blit.
757
+ h (int): Height of the area to blit.
758
+
759
+ Returns:
760
+ (Area): The bounding box of the blitted area.
761
+ """
762
+
763
+ def blit_transparent(canvas, buf, x: int, y: int, w: int, h: int, key: int) -> Area:
764
+ """Blit a buffer with transparency.
765
+
766
+ Args:
767
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
768
+ buf (memoryview): Buffer to blit.
769
+ x (int): X-coordinate to blit to.
770
+ y (int): Y-coordinate to blit to.
771
+ w (int): Width of the area to blit.
772
+ h (int): Height of the area to blit.
773
+ key (int): Key value for transparency.
774
+
775
+ Returns:
776
+ (Area): The bounding box of the blitted area.
777
+ """
778
+
779
+ def circle(canvas, x0: int, y0: int, r: int, c: int, f: bool = False) -> Area:
780
+ """Draw a circle or filled disk centered at ``(x0, y0)``.
781
+
782
+ This is a common primitive for buttons, status indicators, animated balls,
783
+ and other UI ornaments. Pass ``f=True`` for a filled circle.
784
+
785
+ Args:
786
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
787
+ x0 (int): Center x coordinate.
788
+ y0 (int): Center y coordinate.
789
+ r (int): Radius.
790
+ c (int): Color.
791
+ f (bool): Fill the circle (default: ``False``).
792
+
793
+ Returns:
794
+ Area: The bounding box of the circle.
795
+ """
796
+
797
+ def ellipse(canvas, x0: int, y0: int, r1: int, r2: int, c: int, f: bool = False, m: int = 15, w: int | None = None, h: int | None = None) -> Area:
798
+ """Midpoint ellipse algorithm
799
+ Draw an ellipse at the given location. Radii r1 and r2 define the geometry; equal values cause a
800
+ circle to be drawn. The c parameter defines the color.
801
+
802
+ The optional f parameter can be set to True to fill the ellipse. Otherwise just a one pixel outline
803
+ is drawn.
804
+
805
+ The optional m parameter enables drawing to be restricted to certain quadrants of the ellipse.
806
+ The LS four bits determine which quadrants are to be drawn, with bit 0 specifying Q1, b1 Q2,
807
+ b2 Q3 and b3 Q4. Quadrants are numbered counterclockwise with Q1 being top right.
808
+
809
+ Args:
810
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
811
+ x0 (int): Center x coordinate
812
+ y0 (int): Center y coordinate
813
+ r1 (int): x radius
814
+ r2 (int): y radius
815
+ c (int): color
816
+ f (bool): Fill the ellipse (default: False)
817
+ m (int): Bitmask to determine which quadrants to draw (default: 0b1111)
818
+ w (int): Width of the ellipse (default: None)
819
+ h (int): Height of the ellipse (default: None)
820
+
821
+ Returns:
822
+ (Area): The bounding box of the ellipse.
823
+ """
824
+
825
+ def fill(canvas, c: int) -> Area:
826
+ """Fill the entire canvas with a solid color.
827
+
828
+ This is the typical first step for each frame: clear the previous scene,
829
+ then draw the next frame on top of it.
830
+
831
+ Args:
832
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
833
+ c (int): Color.
834
+
835
+ Returns:
836
+ Area: The bounding box of the filled area.
837
+ """
838
+
839
+ def fill_rect(canvas, x: int, y: int, w: int, h: int, c: int) -> Area:
840
+ """Draw a filled rectangle.
841
+
842
+ Use this for backgrounds, panels, status bars, and other simple UI blocks.
843
+ It is also the workhorse for clipping and partial redraw operations.
844
+
845
+ Args:
846
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
847
+ x (int): X-coordinate of the top-left corner.
848
+ y (int): Y-coordinate of the top-left corner.
849
+ w (int): Width of the rectangle.
850
+ h (int): Height of the rectangle.
851
+ c (int): Color.
852
+
853
+ Returns:
854
+ Area: The bounding box of the filled area.
855
+ """
856
+
857
+ def gradient_rect(canvas, x: int, y: int, w: int, h: int, c1: int, c2: int | None = None, vertical: bool = True) -> Area:
858
+ """Fill a rectangle with a vertical or horizontal color ramp.
859
+
860
+ This is useful for scenery, backgrounds, and other effects where a smooth
861
+ transition looks better than a flat fill. The gallery applications use it for
862
+ animated skies and other stylized backdrops.
863
+
864
+ Args:
865
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
866
+ x (int): X-coordinate of the top-left corner.
867
+ y (int): Y-coordinate of the top-left corner.
868
+ w (int): Width of the rectangle.
869
+ h (int): Height of the rectangle.
870
+ c1 (int): Color for the top or left edge (format-native).
871
+ c2 (int): Color for the bottom or right edge. If omitted or equal to
872
+ ``c1``, the helper falls back to `fill_rect`.
873
+ vertical (bool): If ``True``, draw a vertical gradient; otherwise a horizontal one.
874
+
875
+ Returns:
876
+ Area: The bounding box of the filled area.
877
+ """
878
+
879
+ def hline(canvas, x0: int, y0: int, w: int, c: int) -> Area:
880
+ """Horizontal line drawing function. Will draw a single pixel wide line.
881
+
882
+ Args:
883
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
884
+ x0 (int): X-coordinate of the start of the line.
885
+ y0 (int): Y-coordinate of the start of the line.
886
+ w (int): Width of the line.
887
+ c (int): color.
888
+
889
+ Returns:
890
+ (Area): The bounding box of the line.
891
+ """
892
+
893
+ def line(canvas, x0: int, y0: int, x1: int, y1: int, c: int) -> Area:
894
+ """Draw a single-pixel line from ``(x0, y0)`` to ``(x1, y1)``.
895
+
896
+ Use this for outlines, rulers, charts, and small UI connectors where a solid
897
+ stroke is more appropriate than a filled shape.
898
+
899
+ Args:
900
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
901
+ x0 (int): X-coordinate of the start of the line.
902
+ y0 (int): Y-coordinate of the start of the line.
903
+ x1 (int): X-coordinate of the end of the line.
904
+ y1 (int): Y-coordinate of the end of the line.
905
+ c (int): Color.
906
+
907
+ Returns:
908
+ Area: The bounding box of the line.
909
+ """
910
+
911
+ def pixel(canvas, x: int, y: int, c: int) -> Area:
912
+ """Draw a single pixel at the specified x, y location. Uses the canvas's pixel method if available,
913
+ otherwise writes directly to the buffer.
914
+
915
+ Args:
916
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
917
+ x (int): X-coordinate of the pixel.
918
+ y (int): Y-coordinate of the pixel.
919
+ c (int): color.
920
+
921
+ Returns:
922
+ (Area): The bounding box of the pixel.
923
+ """
924
+
925
+ def poly(canvas, x: int, y: int, coords: list, c: int, f: bool = False) -> Area:
926
+ """Given a list of coordinates, draw an arbitrary (convex or concave) closed polygon at the given x, y location
927
+ using the given color.
928
+
929
+ The coords must be specified as an array of integers, e.g. array('h', [x0, y0, x1, y1, ... xn, yn]) or a
930
+ list or tuple of points, e.g. [(x0, y0), (x1, y1), ... (xn, yn)].
931
+
932
+ The optional f parameter can be set to True to fill the polygon. Otherwise, just a one-pixel outline is drawn.
933
+
934
+ Args:
935
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
936
+ x (int): X-coordinate of the polygon's position.
937
+ y (int): Y-coordinate of the polygon's position.
938
+ coords (list): List of coordinates.
939
+ c (int): color.
940
+ f (bool): Fill the polygon (default: False).
941
+
942
+ Raises:
943
+ MemoryError: In the compiled module, a filled polygon of more than 256 vertices needs one int of
944
+ scratch memory per vertex, and there wasn't enough. Nothing is drawn. A polygon of any
945
+ size fills; 256 vertices or fewer allocates nothing.
946
+
947
+ Returns:
948
+ (Area): The bounding box of the polygon.
949
+ """
950
+
951
+ def polygon(canvas, points: list, x: int, y: int, color: int, angle: float = 0, center_x: int = 0, center_y: int = 0) -> Area:
952
+ """Draw a polygon on the canvas.
953
+
954
+ Args:
955
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
956
+ points (list): List of points to draw.
957
+ x (int): X-coordinate of the polygon's position.
958
+ y (int): Y-coordinate of the polygon's position.
959
+ color (int): color.
960
+ angle (float): Rotation angle in radians (default: 0).
961
+ center_x (int): X-coordinate of the rotation center (default: 0).
962
+ center_y (int): Y-coordinate of the rotation center (default: 0).
963
+
964
+ Raises:
965
+ ValueError: If the polygon has less than 3 points.
966
+
967
+ Returns:
968
+ (Area): The bounding box of the polygon.
969
+ """
970
+
971
+ def rect(canvas, x0: int, y0: int, w: int, h: int, c: int, f: bool = False) -> Area:
972
+ """Rectangle drawing function. Will draw a single pixel wide rectangle starting at
973
+ x0, y0 and extending w, h pixels.
974
+
975
+ Args:
976
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
977
+ x0 (int): X-coordinate of the top-left corner of the rectangle.
978
+ y0 (int): Y-coordinate of the top-left corner of the rectangle.
979
+ w (int): Width of the rectangle.
980
+ h (int): Height of the rectangle.
981
+ c (int): color.
982
+ f (bool): Fill the rectangle (default: False).
983
+
984
+ Returns:
985
+ (Area): The bounding box of the rectangle.
986
+ """
987
+
988
+ def round_rect(canvas, x0: int, y0: int, w: int, h: int, r: int, c: int, f: bool = False) -> Area:
989
+ """Rounded rectangle drawing function. Will draw a single pixel wide rounded rectangle starting at
990
+ x0, y0 and extending w, h pixels with the specified radius.
991
+
992
+ Args:
993
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
994
+ x0 (int): X-coordinate of the top-left corner of the rectangle.
995
+ y0 (int): Y-coordinate of the top-left corner of the rectangle.
996
+ w (int): Width of the rectangle.
997
+ h (int): Height of the rectangle.
998
+ r (int): Radius of the corners.
999
+ c (int): color.
1000
+ f (bool): Fill the rectangle (default: False).
1001
+
1002
+ Returns:
1003
+ (Area): The bounding box of the rectangle.
1004
+ """
1005
+
1006
+ def triangle(canvas, x0: int, y0: int, x1: int, y1: int, x2: int, y2: int, c: int, f: bool = False) -> Area:
1007
+ """Triangle drawing function. Draws a single pixel wide triangle with vertices at
1008
+ (x0, y0), (x1, y1), and (x2, y2).
1009
+
1010
+ Args:
1011
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
1012
+ x0 (int): X-coordinate of the first vertex.
1013
+ y0 (int): Y-coordinate of the first vertex.
1014
+ x1 (int): X-coordinate of the second vertex.
1015
+ y1 (int): Y-coordinate of the second vertex.
1016
+ x2 (int): X-coordinate of the third vertex.
1017
+ y2 (int): Y-coordinate of the third vertex.
1018
+ c (int): color.
1019
+ f (bool): Fill the triangle (default: False).
1020
+
1021
+ Returns:
1022
+ (Area): The bounding box of the triangle.
1023
+ """
1024
+
1025
+ def vline(canvas, x0: int, y0: int, h: int, c: int) -> Area:
1026
+ """Horizontal line drawing function. Will draw a single pixel wide line.
1027
+
1028
+ Args:
1029
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
1030
+ x0 (int): X-coordinate of the start of the line.
1031
+ y0 (int): Y-coordinate of the start of the line.
1032
+ h (int): Height of the line.
1033
+ c (int): color.
1034
+
1035
+ Returns:
1036
+ (Area): The bounding box of the line.
1037
+ """
1038
+
1039
+ def text(*args, height: int = 8, **kwargs) -> Area:
1040
+ """Selector to call the correct text function based on the height of the font.
1041
+ See text8, text14, and text16 for more information.
1042
+
1043
+ Args:
1044
+ height (int): The height of the font to use. Default is 8.
1045
+ """
1046
+
1047
+ def text8(canvas, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data=None) -> Area:
1048
+ """Draw a single line of text with the built-in 8-pixel font.
1049
+
1050
+ This helper suits labels, status text, and scrolling captions. The text is written into the given canvas at
1051
+ ``(x, y)`` and returns the region that was updated so that it can be used
1052
+ as a dirty rectangle.
1053
+
1054
+ Args:
1055
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
1056
+ s (str): The text to draw.
1057
+ x (int): The x position to start drawing the text.
1058
+ y (int): The y position to start drawing the text.
1059
+ c (int): The color to draw the text in. Default is ``1``.
1060
+ scale (int): The scale factor to draw the text at. Default is ``1``.
1061
+ inverted (bool): If ``True``, draw the text inverted. Default is ``False``.
1062
+ font_data (str | bytearray): Path to a font file or memoryview. Default is ``None``.
1063
+
1064
+ Returns:
1065
+ Area: The area that was drawn to.
1066
+ """
1067
+
1068
+ def text14(canvas, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data=None) -> Area:
1069
+ """Place text on the canvas with a 14 pixel high font.
1070
+ Breaks on ``\\n`` to next line. Does not break on line going off canvas.
1071
+
1072
+ Args:
1073
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
1074
+ s (str): The text to draw.
1075
+ x (int): The x position to start drawing the text.
1076
+ y (int): The y position to start drawing the text.
1077
+ c (int): The color to draw the text in. Default is 1.
1078
+ scale (int): The scale factor to draw the text at. Default is 1.
1079
+ inverted (bool): If True, draw the text inverted. Default is False.
1080
+ font_data (str | bytearray): The path to the font .bin file or memoryview. Default is None.
1081
+
1082
+ Returns:
1083
+ Area: The area that was drawn to.
1084
+ """
1085
+
1086
+ def text16(canvas, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data=None) -> Area:
1087
+ """Place text on the canvas with a 16 pixel high font.
1088
+ Breaks on ``\\n`` to next line. Does not break on line going off canvas.
1089
+
1090
+ Args:
1091
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
1092
+ s (str): The text to draw.
1093
+ x (int): The x position to start drawing the text.
1094
+ y (int): The y position to start drawing the text.
1095
+ c (int): The color to draw the text in. Default is 1.
1096
+ scale (int): The scale factor to draw the text at. Default is 1.
1097
+ inverted (bool): If True, draw the text inverted. Default is False.
1098
+ font_data (str | bytearray): The path to the font .bin file or memoryview. Default is None.
1099
+
1100
+ Returns:
1101
+ Area: The area that was drawn to.
1102
+ """
1103
+
1104
+ class Font:
1105
+ """Load a bundled romfont and reuse it for multiple text draws.
1106
+
1107
+ `Font` is useful when the same UI needs to draw several labels, scores, or
1108
+ status strings with the same glyph set. It can load either an embedded font
1109
+ or a custom ``.bin`` file from disk or from memory.
1110
+
1111
+ Args:
1112
+ font_data (str | bytearray): Path to a font file or memoryview. Default is ``None``.
1113
+ height (int): The height of the font. Default is ``None``.
1114
+ cached (bool): If ``True`` (default), read the font into memory on init.
1115
+ If ``False``, read it from disk each time it is needed.
1116
+ """
1117
+
1118
+ font_name: str
1119
+ """What the font is called: ``"default"``, the file's base name, or ``"memoryview"``."""
1120
+
1121
+ def __init__(self, font_data: str | memoryview | bytes | None = None, height: int | None = None, cached: bool = True) -> None:
1122
+ """Load a romfont from a path, ``memoryview``, or embedded default.
1123
+
1124
+ Binary font layout: 256 glyphs in ASCII order, one byte per pixel row
1125
+ per glyph (fonts up to 8 pixels wide). If ``height`` is omitted and
1126
+ ``font_data`` is a path like ``font_8x14.bin``, height is taken from the
1127
+ name; for a ``memoryview``, height is ``len(data) // 256``.
1128
+
1129
+ Args:
1130
+ font_data (str | memoryview): Path to a ``.bin`` font file, or a
1131
+ ``memoryview`` of glyph bytes. Default uses the embedded font
1132
+ for ``height`` (or 8×8).
1133
+ height (int): Glyph height in pixels. Overrides height inferred from
1134
+ the file name when both are set.
1135
+ cached (bool): If True (default), read the whole file into memory.
1136
+ If False, seek the open file for each glyph row.
1137
+
1138
+ Raises:
1139
+ FileNotFoundError: If ``font_data`` is a path that cannot be opened.
1140
+ RuntimeError: If the file size does not match the expected font size.
1141
+ """
1142
+
1143
+ @property
1144
+ def width(self) -> int:
1145
+ """Return the width of the font in pixels."""
1146
+
1147
+ @property
1148
+ def height(self) -> int:
1149
+ """Return the height of the font in pixels."""
1150
+
1151
+ def deinit(self) -> None:
1152
+ """Close the font file as cleanup."""
1153
+
1154
+ def __enter__(self) -> Font:
1155
+ """Open the font for a ``with`` block.
1156
+
1157
+ Only the pure-Python package has this.
1158
+ """
1159
+
1160
+ def __exit__(self, exception_type, exception_value, traceback) -> None:
1161
+ """Close the font at the end of a ``with`` block.
1162
+
1163
+ Only the pure-Python package has this.
1164
+ """
1165
+
1166
+ def draw_char(self, char: str, x: int, y: int, canvas, color: int, scale: int = 1, inverted: bool = False) -> Area:
1167
+ """Draw one character at position (x,y).
1168
+
1169
+ Args:
1170
+ char (str): The character to draw.
1171
+ x (int): The x position to draw the character.
1172
+ y (int): The y position to draw the character.
1173
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
1174
+ color (int): The color to draw the character in.
1175
+ scale (int): The scale factor to draw the character at. Default is 1.
1176
+ inverted (bool): If True, draw the character inverted. Default is False.
1177
+
1178
+ Returns:
1179
+ (Area): The area that was drawn to.
1180
+ """
1181
+
1182
+ def text(self, canvas, string: str, x: int, y: int, color: int, scale: int = 1, inverted: bool = False) -> Area:
1183
+ """Draw text to the canvas.
1184
+
1185
+ Args:
1186
+ canvas (FrameBuffer): The surface to draw on; any object with the same drawing methods works.
1187
+ string (str): The text to draw.
1188
+ x (int): The x position to start drawing the text.
1189
+ y (int): The y position to start drawing the text.
1190
+ color (int): The color to draw the text in.
1191
+ scale (int): The scale factor to draw the text at. Default is 1.
1192
+ inverted (bool): If True, draw the text inverted. Default is False.
1193
+
1194
+ Returns:
1195
+ (Area): The area that was drawn to.
1196
+ """
1197
+
1198
+ def text_width(self, text: str, scale: int = 1) -> int:
1199
+ """Return the pixel width of the specified text message.
1200
+ Takes into account the scale factor, but not any newlines.
1201
+
1202
+ Args:
1203
+ text (str): The text to measure.
1204
+ scale (int): The scale factor to measure the text at. Default is 1.
1205
+
1206
+ Returns:
1207
+ int: Width in pixels.
1208
+ """
1209
+
1210
+ def export(self, filename: str) -> None:
1211
+ """Export the font data in self._cache to a .py file that can be imported.
1212
+ The format is a single bytes object named _FONT. There are 256 lines, one for each character.
1213
+ The last line is `FONT = memoryview(_FONT)`.
1214
+
1215
+ Args:
1216
+ filename (str): The path to save the file to.
1217
+ """
1218
+
1219
+ class Draw:
1220
+ """Draw shapes on a canvas (display, FrameBuffer, or compatible object).
1221
+
1222
+ Each method delegates to ``pygraphics._shapes`` and returns an ``Area`` bounding box.
1223
+
1224
+ Args:
1225
+ canvas: Object with framebuf-compatible drawing methods.
1226
+
1227
+ Example::
1228
+
1229
+ draw = Draw(display)
1230
+ draw.fill(0x0000)
1231
+ draw.rect(10, 10, 100, 100, 0xFFFF)
1232
+
1233
+ with draw.clip(5, 5, 50, 30):
1234
+ draw.fill_rect(0, 0, 200, 200, 0xF800) # clipped to the 50x30 region
1235
+ """
1236
+
1237
+ canvas: Any
1238
+ """The canvas this object draws on."""
1239
+
1240
+ def __init__(self, canvas: Any) -> None:
1241
+ """Attach drawing helpers to ``canvas``.
1242
+
1243
+ Args:
1244
+ canvas: Object with framebuf-compatible drawing methods (e.g.
1245
+ `FrameBuffer <pygraphics.FrameBuffer>` or a display driver).
1246
+ """
1247
+
1248
+ def clip(self, x: int, y: int | None = None, w: int | None = None, h: int | None = None) -> ClipContext:
1249
+ """Return a context manager that clips drawing to a rectangle.
1250
+
1251
+ Accepts ``clip(x, y, w, h)`` or ``clip(Area(...))``. Nested clips intersect.
1252
+ """
1253
+
1254
+ def arc(self, x: int, y: int, r: int, a0: float, a1: float, c: int) -> Area:
1255
+ """Draw an arc. Returns Area bounds."""
1256
+
1257
+ def blit(self, source, x: int, y: int, key: int = -1, palette: Any = None) -> Area:
1258
+ """Blit a source buffer. Returns Area bounds."""
1259
+
1260
+ def blit_rect(self, buf, x: int, y: int, w: int, h: int) -> Area:
1261
+ """Blit a raw rectangle buffer. Returns Area bounds."""
1262
+
1263
+ def blit_transparent(self, buf, x: int, y: int, w: int, h: int, key: int | None = None) -> Area:
1264
+ """Blit with transparency. Returns Area bounds."""
1265
+
1266
+ def circle(self, x: int, y: int, r: int, c: int, f: bool = False) -> Area:
1267
+ """Draw a circle. Returns Area bounds."""
1268
+
1269
+ def ellipse(self, x: int, y: int, r1: int, r2: int, c: int, f: bool = False, m: int = 15, w: int | None = None, h: int | None = None) -> Area:
1270
+ """Draw an ellipse. Returns Area bounds."""
1271
+
1272
+ def fill(self, c: int) -> Area:
1273
+ """Fill the canvas. Returns Area bounds."""
1274
+
1275
+ def fill_rect(self, x: int, y: int, w: int, h: int, c: int) -> Area:
1276
+ """Fill a rectangle. Returns Area bounds."""
1277
+
1278
+ def gradient_rect(self, x: int, y: int, w: int, h: int, c1: int, c2: int | None = None, vertical: bool = True) -> Area:
1279
+ """Fill a rectangle with a gradient. Returns Area bounds."""
1280
+
1281
+ def hline(self, x: int, y: int, w: int, c: int) -> Area:
1282
+ """Draw a horizontal line. Returns Area bounds."""
1283
+
1284
+ def line(self, x1: int, y1: int, x2: int, y2: int, c: int) -> Area:
1285
+ """Draw a line. Returns Area bounds."""
1286
+
1287
+ def pixel(self, x: int, y: int, c: int) -> Area:
1288
+ """Draw a pixel. Returns Area bounds."""
1289
+
1290
+ def poly(self, x: int, y: int, coords: list[int] | tuple[int, ...], c: int, f: bool = False) -> Area:
1291
+ """Draw a polygon from flat coordinates. Returns Area bounds."""
1292
+
1293
+ def polygon(self, points: list[tuple[int, int]], x: int, y: int, c: int, angle: int = 0, center_x: int = 0, center_y: int = 0) -> Area:
1294
+ """Draw a polygon from point list. Returns Area bounds."""
1295
+
1296
+ def rect(self, x: int, y: int, w: int, h: int, c: int, f: bool = False) -> Area:
1297
+ """Draw a rectangle. Returns Area bounds."""
1298
+
1299
+ def round_rect(self, x: int, y: int, w: int, h: int, r: int, c: int, f: bool = False) -> Area:
1300
+ """Draw a rounded rectangle. Returns Area bounds."""
1301
+
1302
+ def triangle(self, x1: int, y1: int, x2: int, y2: int, x3: int, y3: int, c: int, f: bool = False) -> Area:
1303
+ """Draw a triangle. Returns Area bounds."""
1304
+
1305
+ def vline(self, x: int, y: int, h: int, c: int) -> Area:
1306
+ """Draw a vertical line. Returns Area bounds."""
1307
+
1308
+ def text(self, *args, **kwargs) -> Area:
1309
+ """Draw text using height from kwargs. Returns Area bounds."""
1310
+
1311
+ def text8(self, *args, **kwargs) -> Area:
1312
+ """Draw 8px-high text. Returns Area bounds."""
1313
+
1314
+ def text14(self, *args, **kwargs) -> Area:
1315
+ """Draw 14px-high text. Returns Area bounds."""
1316
+
1317
+ def text16(self, *args, **kwargs) -> Area:
1318
+ """Draw 16px-high text. Returns Area bounds."""
1319
+
1320
+ class ClippedCanvas:
1321
+ """Proxy that restricts drawing on ``canvas`` to ``clip``.
1322
+
1323
+ Drawing methods that intersect the clip region are forwarded to the
1324
+ underlying canvas; pixels outside the clip are ignored. Unknown attributes
1325
+ fall through to ``canvas`` via ``__getattr__``.
1326
+ """
1327
+
1328
+ def __init__(self, canvas, clip: Area) -> None:
1329
+ """Wrap ``canvas`` so drawing is limited to ``clip``.
1330
+
1331
+ Args:
1332
+ canvas: Framebuf-compatible draw target.
1333
+ clip (Area): Inclusive rectangle in canvas coordinates.
1334
+ """
1335
+
1336
+ @property
1337
+ def width(self) -> int:
1338
+ """Underlying canvas width in pixels."""
1339
+
1340
+ @property
1341
+ def height(self) -> int:
1342
+ """Underlying canvas height in pixels."""
1343
+
1344
+ def pixel(self, x: int, y: int, c: int | None = None) -> Area | int:
1345
+ """Get or set a pixel if ``(x, y)`` is inside the clip.
1346
+
1347
+ Args:
1348
+ x: X coordinate.
1349
+ y: Y coordinate.
1350
+ c: Color to set. If ``None``, read the existing pixel color.
1351
+
1352
+ Returns:
1353
+ On set: ``Area`` of the pixel, or ``None`` if outside the clip.
1354
+ On get: pixel color, or ``None`` if outside the clip.
1355
+ """
1356
+
1357
+ def fill(self, c: int) -> Area:
1358
+ """Fill the entire clip rectangle with color ``c``.
1359
+
1360
+ Args:
1361
+ c: Fill color.
1362
+
1363
+ Returns:
1364
+ ``Area`` of the filled clip, or ``None`` if empty.
1365
+ """
1366
+
1367
+ def fill_rect(self, x: int, y: int, w: int, h: int, c: int) -> Area:
1368
+ """Fill the intersection of the given rectangle with the clip.
1369
+
1370
+ Args:
1371
+ x: Left edge.
1372
+ y: Top edge.
1373
+ w: Width.
1374
+ h: Height.
1375
+ c: Fill color.
1376
+
1377
+ Returns:
1378
+ Clipped ``Area`` that was filled, or ``None`` if no overlap.
1379
+ """
1380
+
1381
+ def hline(self, x: int, y: int, w: int, c: int) -> Area:
1382
+ """Draw a horizontal line clipped to this region.
1383
+
1384
+ Args:
1385
+ x: Start x.
1386
+ y: Y coordinate.
1387
+ w: Width in pixels.
1388
+ c: Color.
1389
+
1390
+ Returns:
1391
+ Clipped ``Area``, or ``None`` if no overlap.
1392
+ """
1393
+
1394
+ def vline(self, x: int, y: int, h: int, c: int) -> Area:
1395
+ """Draw a vertical line clipped to this region.
1396
+
1397
+ Args:
1398
+ x: X coordinate.
1399
+ y: Start y.
1400
+ h: Height in pixels.
1401
+ c: Color.
1402
+
1403
+ Returns:
1404
+ Clipped ``Area``, or ``None`` if no overlap.
1405
+ """
1406
+
1407
+ def blit_rect(self, buf, x: int, y: int, w: int, h: int) -> Area:
1408
+ """Blit a packed buffer, cropping to the clip region.
1409
+
1410
+ Args:
1411
+ buf: Source bytes for a ``w`` x ``h`` rectangle (canvas bpp).
1412
+ x: Destination x.
1413
+ y: Destination y.
1414
+ w: Source width.
1415
+ h: Source height.
1416
+
1417
+ Returns:
1418
+ Clipped destination ``Area``, or ``None`` if no overlap.
1419
+ """
1420
+
1421
+ def blit_transparent(self, buf, x: int, y: int, w: int, h: int, key: int) -> Area:
1422
+ """Blit a packed buffer with transparency, cropped to the clip.
1423
+
1424
+ Args:
1425
+ buf: Source bytes for a ``w`` x ``h`` rectangle (canvas bpp).
1426
+ x: Destination x.
1427
+ y: Destination y.
1428
+ w: Source width.
1429
+ h: Source height.
1430
+ key: Transparent color key.
1431
+
1432
+ Returns:
1433
+ Clipped destination ``Area``, or ``None`` if no overlap.
1434
+ """
1435
+
1436
+ def text(self, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data=None, height=8) -> Area:
1437
+ """Draw romfont text clipped to this region.
1438
+
1439
+ Glyphs are rendered via `fill_rect` on ``self``, so pixels
1440
+ outside the clip are discarded (unlike falling through to the
1441
+ underlying canvas ``text``, which draws unclipped).
1442
+
1443
+ Args:
1444
+ s: Text to draw.
1445
+ x: Left edge.
1446
+ y: Top edge.
1447
+ c: Foreground color.
1448
+ scale: Glyph scale factor.
1449
+ inverted: Mirror glyphs when True.
1450
+ font_data: Optional romfont bytes / path.
1451
+ height: Romfont height (8, 14, or 16).
1452
+
1453
+ Returns:
1454
+ Unclipped text ``Area`` (same contract as ``FrameBuffer.text``).
1455
+ """
1456
+
1457
+ def text8(self, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data=None) -> Area:
1458
+ """Draw 8px-high romfont text clipped to this region."""
1459
+
1460
+ def text14(self, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data=None) -> Area:
1461
+ """Draw 14px-high romfont text clipped to this region."""
1462
+
1463
+ def text16(self, s: str, x: int, y: int, c: int = 1, scale: int = 1, inverted: bool = False, font_data=None) -> Area:
1464
+ """Draw 16px-high romfont text clipped to this region."""
1465
+
1466
+ class ClipContext:
1467
+ """Context manager that pushes a clip rectangle onto a `Draw` stack.
1468
+
1469
+ Entering returns the effective (intersected) clip ``Area``. Nested
1470
+ ``with draw.clip(...)`` blocks intersect.
1471
+ """
1472
+
1473
+ def __init__(self, draw, area: Area) -> None:
1474
+ """Bind this context to ``draw`` and the clip ``area``.
1475
+
1476
+ Args:
1477
+ draw (Draw): Draw instance whose clip stack is updated.
1478
+ area (Area): Clip rectangle to push on enter.
1479
+ """
1480
+
1481
+ def __enter__(self) -> ClipContext:
1482
+ """Push the clip and return the effective clip ``Area``."""
1483
+
1484
+ def __exit__(self, exc_type, exc, tb) -> None:
1485
+ """Pop the clip rectangle from the draw stack."""
1486
+
1487
+ class BMP565:
1488
+ """Read a 16-bit RGB565 BMP as a sliceable, optionally streamed asset.
1489
+
1490
+ Provide either ``filename`` (path to a BMP file) or ``source`` (an in-memory
1491
+ RGB565 buffer with ``width`` and ``height``).
1492
+ """
1493
+
1494
+ width: int
1495
+ """Image width in pixels."""
1496
+ height: int
1497
+ """Image height in pixels."""
1498
+ bpp: int
1499
+ """Bits per pixel: 16."""
1500
+ BPP: int
1501
+ """Bytes per pixel: 2."""
1502
+
1503
+ def __init__(self, filename: str | None = None, source: bytearray | memoryview | None = None, streamed: bool = False, mirrored: bool = False, width: int | None = None, height: int | None = None) -> None:
1504
+ """Load from a file path or an existing RGB565 buffer.
1505
+
1506
+ Args:
1507
+ filename: Path to an RGB565 BMP file. Mutually exclusive with ``source``.
1508
+ source: Existing RGB565 pixel buffer. Requires ``width`` and ``height``.
1509
+ streamed: If True with ``filename``, keep the file open and read rows
1510
+ on demand instead of loading the full image into memory.
1511
+ mirrored: When streaming, reverse each row read (rarely needed).
1512
+ width: Pixel width when constructing from ``source``.
1513
+ height: Pixel height when constructing from ``source``.
1514
+
1515
+ Raises:
1516
+ ValueError: If neither ``filename`` nor ``source`` is given, or the
1517
+ file is not a valid RGB565 BMP.
1518
+ """
1519
+
1520
+ def __call__(self, x: int, y: int, w: int, h: int) -> bytes | bytearray | memoryview:
1521
+ """Return the RGB565 bytes for the rectangle at ``(x, y)`` size ``w`` × ``h``.
1522
+
1523
+ Equivalent to ``self[x : x + w, y : y + h]``.
1524
+
1525
+ Args:
1526
+ x: Left edge in pixels.
1527
+ y: Top edge in pixels.
1528
+ w: Width in pixels.
1529
+ h: Height in pixels.
1530
+
1531
+ Returns:
1532
+ Raw RGB565 bytes for the requested region.
1533
+ """
1534
+
1535
+ @property
1536
+ def buffer(self) -> memoryview | None:
1537
+ """Writable ``memoryview`` of the full RGB565 pixel buffer, or ``None`` if streamed."""
1538
+
1539
+ def save(self, filename: str | None = None) -> str:
1540
+ """Write this image as an RGB565 BMP file.
1541
+
1542
+ If ``filename`` already exists, a numeric suffix is appended
1543
+ (``image.bmp`` → ``image_1.bmp``, etc.).
1544
+
1545
+ Args:
1546
+ filename: Output path. Defaults to the source filename or ``image.bmp``.
1547
+
1548
+ Returns:
1549
+ The path actually written.
1550
+ """
1551
+
1552
+ def __getitem__(self, key: int | slice | tuple[int | slice, int | slice]) -> int | bytes | bytearray | memoryview:
1553
+ """Index or slice pixel data.
1554
+
1555
+ Args:
1556
+ key: One of:
1557
+
1558
+ * ``(x, y)`` — single pixel as an ``int`` (RGB565)
1559
+ * ``(x_slice, y_slice)`` — rectangular region as ``bytearray``
1560
+ * ``int`` — flat pixel index as an ``int``
1561
+ * ``slice`` — full-width row range as ``bytearray``
1562
+
1563
+ Returns:
1564
+ Pixel value(s) for the requested key.
1565
+
1566
+ Raises:
1567
+ ValueError: If ``key`` is not a supported index form.
1568
+ """
1569
+
1570
+ def deinit(self) -> None:
1571
+ """Close the open file handle when constructed with ``streamed=True``."""
1572
+
1573
+ def __exit__(self, exception_type, exception_value, traceback) -> None:
1574
+ """Close the image at the end of a ``with`` block, as `deinit` does.
1575
+
1576
+ Only the pure-Python package has this.
1577
+ """
1578
+
1579
+ def load_image(filename: str) -> FrameBuffer:
1580
+ """Load a ``FrameBuffer`` from a PBM, PGM, or RGB565 BMP file."""
1581
+
1582
+ def save_image(fb: FrameBuffer, filename: str | None = None) -> str:
1583
+ """Save a ``FrameBuffer`` to PBM, PGM, BMP, or PNG based on format or filename.
1584
+
1585
+ MONO_HLSB → PBM, GS2/GS4/GS8 → PGM, RGB565 → BMP (or PNG if filename ends in .png),
1586
+ RGB888 → PNG. Other formats raise ``ValueError``.
1587
+ """
1588
+
1589
+ def pbm_to_framebuffer(filename: str) -> FrameBuffer:
1590
+ """Convert a PBM file to a MONO_HLSB FrameBuffer.
1591
+
1592
+ Allocates one pixel buffer and reads pixels with ``readinto`` (peak ~1x).
1593
+
1594
+ Args:
1595
+ filename (str): Filename of the PBM file
1596
+
1597
+ Returns:
1598
+ FrameBuffer: Loaded monochrome framebuffer.
1599
+ """
1600
+
1601
+ def pgm_to_framebuffer(filename: str) -> FrameBuffer:
1602
+ """Convert a PGM file to a GS2_HMSB, GS4_HMSB or GS8 FrameBuffer.
1603
+
1604
+ Allocates one pixel buffer and reads pixels with ``readinto`` (peak ~1x).
1605
+
1606
+ Args:
1607
+ filename (str): Filename of the PGM file
1608
+
1609
+ Returns:
1610
+ FrameBuffer: Loaded greyscale framebuffer (format from max value).
1611
+ """
1612
+
1613
+ def bmp_to_framebuffer(filename: str) -> FrameBuffer:
1614
+ """Convert an RGB565 BMP file to a FrameBuffer.
1615
+
1616
+ Args:
1617
+ filename (str): Path to the BMP file.
1618
+
1619
+ Returns:
1620
+ FrameBuffer: Loaded RGB565 framebuffer.
1621
+ """
1622
+
1623
+ def export_framebuffer(fb: FrameBuffer, filename: str) -> str:
1624
+ """Export a ``FrameBuffer`` as an importable ``.py`` bitmap module.
1625
+
1626
+ Only the pure-Python package has this.
1627
+
1628
+ Format::
1629
+
1630
+ WIDTH = …
1631
+ HEIGHT = …
1632
+ FORMAT = …
1633
+ BITMAP = bytearray(
1634
+ b'…'\
1635
+ b'…'
1636
+ )
1637
+
1638
+ ``BITMAP`` is a writable ``bytearray`` so MicroPython can wrap it in a
1639
+ ``FrameBuffer`` without copying. Prefer `FrameBuffer.from_bitmap` to
1640
+ load. Differs from `Font.export`, which keeps a read-only
1641
+ ``memoryview`` for glyph indexing.
1642
+ """
1643
+
1644
+ def implementation() -> str:
1645
+ """Say which package this is: ``"native_cmod"`` for the compiled module, ``"pygraphics_python"`` for the pure-Python one."""