1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2019 Scott Shawcroft for Adafruit Industries LLC
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+ """
23
+ `adafruit_bitmap_font.bdf`
24
+ ====================================================
25
+
26
+ Loads BDF format fonts.
27
+
28
+ * Author(s): Scott Shawcroft
29
+
30
+ Implementation Notes
31
+ --------------------
32
+
33
+ **Hardware:**
34
+
35
+ **Software and Dependencies:**
36
+
37
+ * Adafruit CircuitPython firmware for the supported boards:
38
+ https://github.com/adafruit/circuitpython/releases
39
+
40
+ """
41
+
1
42
import gc
2
- from .glyph_cache import GlyphCache
3
43
from displayio import Glyph
44
+ from .glyph_cache import GlyphCache
45
+
46
+ __version__ = "0.0.0-auto.0"
47
+ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Bitmap_Font.git"
4
48
5
49
class BDF (GlyphCache ):
50
+ """Loads glyphs from a BDF file in the given bitmap_class."""
6
51
def __init__ (self , f , bitmap_class ):
7
52
super ().__init__ ()
8
53
self .file = f
@@ -13,8 +58,12 @@ def __init__(self, f, bitmap_class):
13
58
line = str (line , "utf-8" )
14
59
if not line or not line .startswith ("STARTFONT 2.1" ):
15
60
raise ValueError ("Unsupported file version" )
61
+ self .point_size = None
62
+ self .x_resolution = None
63
+ self .y_resolution = None
16
64
17
65
def get_bounding_box (self ):
66
+ """Return the maximum glyph size as a 4-tuple of: width, height, x_offset, y_offset"""
18
67
self .file .seek (0 )
19
68
while True :
20
69
line = self .file .readline ()
@@ -23,17 +72,18 @@ def get_bounding_box(self):
23
72
break
24
73
25
74
if line .startswith ("FONTBOUNDINGBOX " ):
26
- _ , x , y , dx , dy = line .split ()
27
- return (int (x ), int (y ), int (dx ), int (dy ))
75
+ _ , x , y , x_offset , y_offset = line .split ()
76
+ return (int (x ), int (y ), int (x_offset ), int (y_offset ))
28
77
return None
29
78
30
79
def load_glyphs (self , code_points ):
80
+ # pylint: disable=too-many-statements,too-many-branches,too-many-nested-blocks,too-many-locals
31
81
metadata = True
32
82
character = False
33
83
code_point = None
34
84
bytes_per_row = 1
35
85
desired_character = False
36
- current_info = None
86
+ current_info = {}
37
87
current_y = 0
38
88
rounded_x = 1
39
89
total_remaining = len (code_points )
@@ -74,12 +124,12 @@ def load_glyphs(self, code_points):
74
124
desired_character = False
75
125
elif line .startswith (b"BBX" ):
76
126
if desired_character :
77
- _ , x , y , dx , dy = line .split ()
127
+ _ , x , y , x_offset , y_offset = line .split ()
78
128
x = int (x )
79
129
y = int (y )
80
- dx = int (dx )
81
- dy = int (dy )
82
- current_info ["bounds" ] = (x , y , dx , dy )
130
+ x_offset = int (x_offset )
131
+ y_offset = int (y_offset )
132
+ current_info ["bounds" ] = (x , y , x_offset , y_offset )
83
133
current_info ["bitmap" ] = self .bitmap_class (x , y , 2 )
84
134
elif line .startswith (b"BITMAP" ):
85
135
if desired_character :
@@ -114,7 +164,7 @@ def load_glyphs(self, code_points):
114
164
x = 0
115
165
for i in range (rounded_x ):
116
166
val = (bits >> ((rounded_x - i - 1 )* 8 )) & 0xFF
117
- for j in range (7 ,- 1 ,- 1 ):
167
+ for j in range (7 , - 1 , - 1 ):
118
168
if x >= width :
119
169
break
120
170
bit = 0
0 commit comments