@@ -66,10 +66,7 @@ class Label(LabelBase):
66
66
This is helpful when two or more labels need to be aligned to the same baseline
67
67
:param: (int,str) tab_replacement: tuple with tab character replace information. When
68
68
(4, " ") will indicate a tab replacement of 4 spaces, defaults to 4 spaces by
69
- tab character
70
- :param: str label_direction: string defining the label text orientation. There are 5
71
- configurations possibles ``LTR``:Left-To-Right ``RTL``:Right-To-Left
72
- ``TTB``:TTB Top-To-Bottom ``UPR``:Upwards ``DWR``:Downwards. It defaults to ``STR``"""
69
+ tab character"""
73
70
74
71
# pylint: disable=too-many-instance-attributes, too-many-locals
75
72
# This has a lot of getters/setters, maybe it needs cleanup.
@@ -125,7 +122,6 @@ def __init__(self, font, **kwargs) -> None:
125
122
self ._padding_left = kwargs .get ("padding_left" , 0 )
126
123
self ._padding_right = kwargs .get ("padding_right" , 0 )
127
124
self .base_alignment = kwargs .get ("base_alignment" , False )
128
- self .label_type = kwargs .get ("label_direction" , "LTR" )
129
125
130
126
if text is not None :
131
127
self ._update_text (str (text ))
@@ -140,6 +136,7 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
140
136
:param y_offset: int y pixel bottom coordinate for the background_box"""
141
137
142
138
left = self ._bounding_box [0 ]
139
+
143
140
if self ._background_tight : # draw a tight bounding box
144
141
box_width = self ._bounding_box [2 ]
145
142
box_height = self ._bounding_box [3 ]
@@ -149,33 +146,14 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
149
146
else : # draw a "loose" bounding box to include any ascenders/descenders.
150
147
ascent , descent = self ._get_ascent_descent ()
151
148
152
- if (
153
- self .label_type == "UPR"
154
- or self .label_type == "DWR"
155
- or self .label_type == "TTB"
156
- ):
157
- box_height = (
158
- self ._bounding_box [3 ] + self ._padding_top + self ._padding_bottom
159
- )
160
- x_box_offset = - self ._padding_bottom
161
- box_width = (
162
- (ascent + descent )
163
- + int ((lines - 1 ) * self .width * self ._line_spacing )
164
- + self ._padding_left
165
- + self ._padding_right
166
- )
167
- else :
168
- box_width = (
169
- self ._bounding_box [2 ] + self ._padding_left + self ._padding_right
170
- )
171
- x_box_offset = - self ._padding_left
172
- box_height = (
173
- (ascent + descent )
174
- + int ((lines - 1 ) * self .height * self ._line_spacing )
175
- + self ._padding_top
176
- + self ._padding_bottom
177
- )
178
-
149
+ box_width = self ._bounding_box [2 ] + self ._padding_left + self ._padding_right
150
+ x_box_offset = - self ._padding_left
151
+ box_height = (
152
+ (ascent + descent )
153
+ + int ((lines - 1 ) * self .height * self ._line_spacing )
154
+ + self ._padding_top
155
+ + self ._padding_bottom
156
+ )
179
157
if self .base_alignment :
180
158
y_box_offset = - ascent - self ._padding_top
181
159
else :
@@ -184,30 +162,17 @@ def _create_background_box(self, lines: int, y_offset: int) -> None:
184
162
box_width = max (0 , box_width ) # remove any negative values
185
163
box_height = max (0 , box_height ) # remove any negative values
186
164
187
- if self .label_type == "UPR" :
188
- movx = left + x_box_offset
189
- movy = - box_height - x_box_offset
190
- elif self .label_type == "DWR" :
191
- movx = left + x_box_offset
192
- movy = x_box_offset
193
- elif self .label_type == "TTB" :
194
- movx = left + x_box_offset
195
- movy = x_box_offset
196
- else :
197
- movx = left + x_box_offset
198
- movy = y_box_offset
199
-
200
165
background_bitmap = displayio .Bitmap (box_width , box_height , 1 )
201
166
tile_grid = displayio .TileGrid (
202
167
background_bitmap ,
203
168
pixel_shader = self ._background_palette ,
204
- x = movx ,
205
- y = movy ,
169
+ x = left + x_box_offset ,
170
+ y = y_box_offset ,
206
171
)
207
172
208
173
return tile_grid
209
174
210
- def _update_background_color (self , new_color : int ) -> None :
175
+ def _update_background_color (self , new_color ) :
211
176
"""Private class function that allows updating the font box background color
212
177
:param new_color: int color as an RGB hex number."""
213
178
@@ -262,10 +227,9 @@ def _update_background_color(self, new_color: int) -> None:
262
227
self .local_group .pop (0 )
263
228
self ._added_background_tilegrid = False
264
229
265
- # pylint: disable = too-many-branches, too-many-statements
266
230
def _update_text (
267
- self , new_text : str
268
- ) -> None : # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
231
+ self , new_text
232
+ ): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
269
233
x = 0
270
234
y = 0
271
235
if self ._added_background_tilegrid :
@@ -274,19 +238,12 @@ def _update_text(
274
238
i = 0
275
239
tilegrid_count = i
276
240
if self .base_alignment :
277
- y_offset = 0
241
+ self . _y_offset = 0
278
242
else :
279
- y_offset = self ._get_ascent () // 2
280
-
281
- if self .label_type == "RTL" :
282
- left = top = bottom = 0
283
- right = None
284
- elif self .label_type == "LTR" :
285
- right = top = bottom = 0
286
- left = None
287
- else :
288
- top = right = left = 0
289
- bottom = 0
243
+ self ._y_offset = self ._get_ascent () // 2
244
+
245
+ right = top = bottom = 0
246
+ left = None
290
247
291
248
for character in new_text :
292
249
if character == "\n " :
@@ -296,74 +253,17 @@ def _update_text(
296
253
glyph = self ._font .get_glyph (ord (character ))
297
254
if not glyph :
298
255
continue
299
-
300
- if self .label_type == "LTR" or self .label_type == "RTL" :
301
- bottom = max (bottom , y - glyph .dy + y_offset )
302
- if y == 0 : # first line, find the Ascender height
303
- top = min (top , - glyph .height - glyph .dy + y_offset )
304
- position_y = y - glyph .height - glyph .dy + y_offset
305
-
306
- if self .label_type == "LTR" :
307
- right = max (right , x + glyph .shift_x , x + glyph .width + glyph .dx )
308
- if x == 0 :
309
- if left is None :
310
- left = glyph .dx
311
- else :
312
- left = min (left , glyph .dx )
313
- position_x = x + glyph .dx
256
+ right = max (right , x + glyph .shift_x , x + glyph .width + glyph .dx )
257
+ if x == 0 :
258
+ if left is None :
259
+ left = glyph .dx
314
260
else :
315
- left = max (
316
- left , abs (x ) + glyph .shift_x , abs (x ) + glyph .width + glyph .dx
317
- )
318
- if x == 0 :
319
- if right is None :
320
- right = glyph .dx
321
- else :
322
- right = max (right , glyph .dx )
323
- position_x = x - glyph .width
324
-
325
- if self .label_type == "TTB" :
326
- if x == 0 :
327
- if left is None :
328
- left = glyph .dx
329
- else :
330
- left = min (left , glyph .dx )
331
- if y == 0 :
332
- top = min (top , - glyph .dy )
333
-
334
- bottom = max (bottom , y + glyph .height , y + glyph .height + glyph .dy )
335
- right = max (
336
- right , x + glyph .width + glyph .dx , x + glyph .shift_x + glyph .dx
337
- )
338
- position_y = y + glyph .dy
339
- position_x = x - glyph .width // 2 + y_offset
340
-
341
- if self .label_type == "UPR" :
342
- if x == 0 :
343
- if bottom is None :
344
- bottom = - glyph .dx
345
-
346
- if y == 0 : # first line, find the Ascender height
347
- bottom = min (bottom , - glyph .dy )
348
- left = min (left , x - glyph .height + y_offset )
349
- top = min (top , y - glyph .width - glyph .dx , y - glyph .shift_x )
350
- right = max (right , x + glyph .height , x + glyph .height - glyph .dy )
351
- position_y = y - glyph .width - glyph .dx
352
- position_x = x - glyph .height - glyph .dy + y_offset
353
-
354
- if self .label_type == "DWR" :
355
- if y == 0 :
356
- if top is None :
357
- top = - glyph .dx
358
- top = min (top , - glyph .dx )
359
- if x == 0 :
360
- left = min (left , - glyph .dy )
361
- left = min (left , x , x - glyph .dy - y_offset )
362
- bottom = max (bottom , y + glyph .width + glyph .dx , y + glyph .shift_x )
363
- right = max (right , x + glyph .height )
364
- position_y = y + glyph .dx
365
- position_x = x + glyph .dy - y_offset
366
-
261
+ left = min (left , glyph .dx )
262
+ if y == 0 : # first line, find the Ascender height
263
+ top = min (top , - glyph .height - glyph .dy + self ._y_offset )
264
+ bottom = max (bottom , y - glyph .dy + self ._y_offset )
265
+ position_y = y - glyph .height - glyph .dy + self ._y_offset
266
+ position_x = x + glyph .dx
367
267
if glyph .width > 0 and glyph .height > 0 :
368
268
try :
369
269
# pylint: disable=unexpected-keyword-arg
@@ -385,60 +285,27 @@ def _update_text(
385
285
x = position_x ,
386
286
y = position_y ,
387
287
)
388
-
389
- if self .label_type == "UPR" :
390
- face .transpose_xy = True
391
- face .flip_x = True
392
- if self .label_type == "DWR" :
393
- face .transpose_xy = True
394
- face .flip_y = True
395
-
396
288
if tilegrid_count < len (self .local_group ):
397
289
self .local_group [tilegrid_count ] = face
398
290
else :
399
291
self .local_group .append (face )
400
292
tilegrid_count += 1
401
- if self .label_type == "RTL" :
402
- x = x - glyph .shift_x
403
- if self .label_type == "TTB" :
404
- if glyph .height < 2 :
405
- y = y + glyph .shift_x
406
- else :
407
- y = y + glyph .height + 1
408
- if self .label_type == "UPR" :
409
- y = y - glyph .shift_x
410
- if self .label_type == "DWR" :
411
- y = y + glyph .shift_x
412
- if self .label_type == "LTR" :
413
- x = x + glyph .shift_x
293
+ x += glyph .shift_x
414
294
i += 1
295
+ # Remove the rest
415
296
416
- if self . label_type == "LTR" and left is None :
297
+ if left is None :
417
298
left = 0
418
- if self .label_type == "RTL" and right is None :
419
- right = 0
420
- if self .label_type == "TTB" and top is None :
421
- top = 0
422
299
423
300
while len (self .local_group ) > tilegrid_count : # i:
424
301
self .local_group .pop ()
425
- # pylint: disable=invalid-unary-operand-type
426
- if self .label_type == "RTL" :
427
- self ._bounding_box = (- left , top , left - right , bottom - top )
428
- if self .label_type == "TTB" :
429
- self ._bounding_box = (left , top , right - left , bottom - top )
430
- if self .label_type == "UPR" :
431
- self ._bounding_box = (left , top , right , bottom - top )
432
- if self .label_type == "DWR" :
433
- self ._bounding_box = (left , top , right , bottom - top )
434
- if self .label_type == "LTR" :
435
- self ._bounding_box = (left , top , right - left , bottom - top )
436
-
437
302
self ._text = new_text
303
+ self ._bounding_box = (left , top , right - left , bottom - top )
304
+
438
305
if self .background_color is not None :
439
306
self ._update_background_color (self ._background_color )
440
307
441
- def _reset_text (self , new_text : str ) -> None :
308
+ def _reset_text (self , new_text ) :
442
309
new_text = self ._tab_text .join (new_text .split ("\t " ))
443
310
try :
444
311
current_anchored_position = self .anchored_position
@@ -447,7 +314,7 @@ def _reset_text(self, new_text: str) -> None:
447
314
except RuntimeError as run_error :
448
315
raise RuntimeError ("Text length exceeds max_glyphs" ) from run_error
449
316
450
- def _set_font (self , new_font ) -> None :
317
+ def _set_font (self , new_font ):
451
318
old_text = self ._text
452
319
current_anchored_position = self .anchored_position
453
320
self ._text = ""
@@ -456,9 +323,9 @@ def _set_font(self, new_font) -> None:
456
323
self ._update_text (str (old_text ))
457
324
self .anchored_position = current_anchored_position
458
325
459
- def _set_line_spacing (self , new_line_spacing : float ) -> None :
326
+ def _set_line_spacing (self , new_line_spacing ) :
460
327
self ._line_spacing = new_line_spacing
461
328
self .text = self ._text # redraw the box
462
329
463
- def _set_text (self , new_text : str , scale : int ) -> None :
330
+ def _set_text (self , new_text , scale ) :
464
331
self ._reset_text (new_text )
0 commit comments