@@ -78,31 +78,32 @@ def text_bounding_box(
78
78
font_height = ascender_max + descender_max
79
79
font_yoffset = ascender_max
80
80
81
-
82
81
lines = 1
83
82
84
83
font_height = font .get_glyph (ord ("M" )).height
85
84
86
-
87
85
xposition = x_start = 0 # starting x position (left margin)
88
86
yposition = y_start = 0
89
87
90
88
left = right = x_start
91
89
top = bottom = y_start
92
90
93
-
94
- y_offset_tight = int (( font .get_glyph (ord ("M" )).height - text .count ("\n " ) * line_spacing_ypixels (font , line_spacing )
91
+ y_offset_tight = int (
92
+ (
93
+ font .get_glyph (ord ("M" )).height
94
+ - text .count ("\n " ) * line_spacing_ypixels (font , line_spacing )
95
+ )
96
+ / 2
95
97
)
96
- / 2 )
97
- # this needs to be reviewed (also in label.py), since it doesn't respond
98
+ # this needs to be reviewed (also in label.py), since it doesn't respond
98
99
# properly to the number of newlines.
99
100
100
- newline = False
101
+ newline = False
101
102
102
103
for char in text :
103
104
104
105
if char == "\n " : # newline
105
- newline = True
106
+ newline = True
106
107
107
108
else :
108
109
@@ -112,49 +113,43 @@ def text_bounding_box(
112
113
print ("Glyph not found: {}" .format (repr (char )))
113
114
else :
114
115
if newline :
115
- newline = False
116
+ newline = False
116
117
xposition = x_start # reset to left column
117
118
yposition = yposition + line_spacing_ypixels (
118
- font , line_spacing
119
- ) # Add a newline
119
+ font , line_spacing
120
+ ) # Add a newline
120
121
lines += 1
121
122
xposition += my_glyph .shift_x
122
123
right = max (right , xposition )
123
124
124
125
if yposition == y_start : # first line, find the Ascender height
125
- top = min (top , - my_glyph .height - my_glyph .dy + y_offset_tight )
126
+ top = min (top , - my_glyph .height - my_glyph .dy + y_offset_tight )
126
127
bottom = max (bottom , yposition - my_glyph .dy + y_offset_tight )
127
128
128
- # width = my_glyph.width
129
- # height = my_glyph.height
130
- # dx = my_glyph.dx
131
- # dy = my_glyph.dy
132
- # shift_x = my_glyph.shift_x
133
- # shift_y = my_glyph.shift_x
134
-
135
-
136
- loose_height = (lines - 1 ) * line_spacing_ypixels (font , line_spacing ) + (ascender_max + descender_max )
137
-
138
-
139
-
140
- label_calibration_offset = int (( font .get_glyph (ord ("M" )).height - text .count ("\n " ) * line_spacing_ypixels (font , line_spacing )
129
+ loose_height = (lines - 1 ) * line_spacing_ypixels (font , line_spacing ) + (
130
+ ascender_max + descender_max
141
131
)
142
- / 2 )
143
-
144
- y_offset_tight = - top + label_calibration_offset #
145
132
133
+ label_calibration_offset = int (
134
+ (
135
+ font .get_glyph (ord ("M" )).height
136
+ - text .count ("\n " ) * line_spacing_ypixels (font , line_spacing )
137
+ )
138
+ / 2
139
+ )
146
140
141
+ y_offset_tight = - top + label_calibration_offset
147
142
148
- final_box_width = right - left
143
+ final_box_width = right - left
149
144
if background_tight :
150
- final_box_height = bottom - top
151
- final_y_offset = y_offset_tight
152
-
145
+ final_box_height = bottom - top
146
+ final_y_offset = y_offset_tight
147
+
153
148
else :
154
- final_box_height = loose_height
155
- final_y_offset = ascender_max
149
+ final_box_height = loose_height
150
+ final_y_offset = ascender_max
156
151
157
- return (final_box_width , final_box_height , 0 , final_y_offset ) # -x1_min is the x_offset
152
+ return (final_box_width , final_box_height , 0 , final_y_offset )
158
153
159
154
160
155
def place_text (
@@ -256,13 +251,13 @@ def place_text(
256
251
257
252
xposition = xposition + shift_x
258
253
259
- return (left , top , right - left , bottom - top ) # bounding_box
254
+ return (left , top , right - left , bottom - top ) # bounding_box
260
255
261
256
262
257
class Label (displayio .Group ):
263
258
# Class variable
264
259
# To save memory, set Label._memory_saver=True and avoid storing the text string in the class.
265
- # If set to False, the class saves the text string for future reference. *** use getter
260
+ # If set to False, the class saves the text string for future reference.
266
261
_memory_saver = True
267
262
268
263
def __init__ (
@@ -302,7 +297,7 @@ def __init__(
302
297
else :
303
298
self ._text = text # text to be displayed
304
299
305
- # limit padding to >= 0 *** raise an error if negative padding is requested
300
+ # limit padding to >= 0
306
301
padding_top = max (0 , padding_top )
307
302
padding_bottom = max (0 , padding_bottom )
308
303
padding_left = max (0 , padding_left )
@@ -367,7 +362,7 @@ def __init__(
367
362
tile_height = box_y ,
368
363
default_tile = 0 ,
369
364
x = - padding_left ,
370
- y = label_position_yoffset - y_offset - padding_top ,
365
+ y = label_position_yoffset - y_offset - padding_top ,
371
366
)
372
367
373
368
# instance the Group with super... super().__init__(
@@ -377,32 +372,16 @@ def __init__(
377
372
) # this will include any arguments, including scale
378
373
self .append (self .tilegrid ) # add the bitmap's tilegrid to the group
379
374
380
- ####### *******
381
- # Set the tileGrid position in the parent based upon anchor_point and anchor_position
382
- # **** Should scale affect the placement of anchor_position?
375
+ # Update bounding_box values. Note: To be consistent with label.py,
376
+ # this is the bounding box for the text only, not including the background.
383
377
384
378
self .bounding_box = (
385
379
self .tilegrid .x ,
386
- #self.tilegrid.y + (y_offset - tight_y_offset),
387
- self .tilegrid .y , #+ (y_offset - tight_y_offset),
380
+ self .tilegrid .y ,
388
381
tight_box_x ,
389
382
tight_box_y ,
390
383
)
391
384
392
- # self.bounding_box = (
393
- # self.tilegrid.x,
394
- # self.tilegrid.y + (y_offset),
395
- # tight_box_x,
396
- # tight_box_y,
397
- # )
398
-
399
-
400
-
401
- # Update bounding_box values. Note: To be consistent with label.py,
402
- # this is the bounding box for the text only, not including the background.
403
- # ******** Need repair
404
- # Create the TileGrid to hold the single Bitmap (self.bitmap)
405
-
406
385
self ._anchored_position = anchored_position
407
386
self .anchor_point = anchor_point
408
387
self .anchored_position = (
0 commit comments