-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathglyph_cache.py
56 lines (39 loc) · 1.39 KB
/
glyph_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# SPDX-FileCopyrightText: 2019 Scott Shawcroft for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_bitmap_font.glyph_cache`
====================================================
Displays text using CircuitPython's displayio.
* Author(s): Scott Shawcroft
Implementation Notes
--------------------
**Hardware:**
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
try:
from typing import Union, Iterable
from fontio import Glyph
except ImportError:
pass
import gc
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Bitmap_Font.git"
class GlyphCache:
"""Caches glyphs loaded by a subclass."""
def __init__(self) -> None:
self._glyphs = {}
def load_glyphs(self, code_points: Union[int, str, Iterable[int]]) -> None:
"""Loads displayio.Glyph objects into the GlyphCache from the font."""
def get_glyph(self, code_point: int) -> Glyph:
"""Returns a displayio.Glyph for the given code point or None is unsupported."""
if code_point in self._glyphs:
return self._glyphs[code_point]
code_points = set()
code_points.add(code_point)
self._glyphs[code_point] = None
self.load_glyphs(code_points)
gc.collect()
return self._glyphs[code_point]