@@ -137,8 +137,6 @@ def __init__(
137
137
x = x ,
138
138
y = y ,
139
139
text = text ,
140
- # color=color,
141
- # background_color=background_color,
142
140
line_spacing = line_spacing ,
143
141
background_tight = background_tight ,
144
142
padding_top = padding_top ,
@@ -170,21 +168,13 @@ def _reset_text(
170
168
** kwargs
171
169
):
172
170
173
- # store the instance variables
174
-
175
- print ('_reset_text Text string to print: {}' .format (text ))
176
-
177
171
# Store all the instance variables
178
172
if font is not None :
179
173
self ._font = font
180
174
if x is not None :
181
175
self .x = x
182
176
if y is not None :
183
177
self .y = y
184
- # if color is not None:
185
- # self.color = color
186
- # if background_color is not None:
187
- # self.background_color = background_color
188
178
if line_spacing is not None :
189
179
self ._line_spacing = line_spacing
190
180
if background_tight is not None :
@@ -236,27 +226,27 @@ def _reset_text(
236
226
237
227
# Calculate the text bounding box
238
228
239
- # Calculate tight box to provide bounding box dimensions to match label for
229
+ # Calculate both " tight" and "loose" bounding box dimensions to match label for
240
230
# anchor_position calculations
241
231
(
242
- tight_box_x ,
232
+ box_x ,
243
233
tight_box_y ,
244
- tight_x_offset ,
234
+ x_offset ,
245
235
tight_y_offset ,
236
+ loose_box_y ,
237
+ loose_y_offset
246
238
) = self ._text_bounding_box (
247
- text , self ._font , self ._line_spacing , background_tight = True ,
248
- ) # calculate the box size for a tight background
239
+ text , self ._font , self ._line_spacing ,
240
+ ) # calculate the box size for a tight and loose backgrounds
249
241
250
242
if self ._background_tight :
251
- box_x = tight_box_x
252
243
box_y = tight_box_y
253
244
y_offset = tight_y_offset
254
- x_offset = tight_x_offset
255
245
256
246
else : # calculate the box size for a loose background
257
- ( box_x , box_y , x_offset , y_offset ) = self . _text_bounding_box (
258
- text , self . _font , self . _line_spacing , background_tight = self . _background_tight ,
259
- )
247
+ box_y = loose_box_y
248
+ y_offset = loose_y_offset
249
+
260
250
# Calculate the background size including padding
261
251
box_x = box_x + self ._padding_left + self ._padding_right
262
252
box_y = box_y + self ._padding_top + self ._padding_bottom
@@ -300,7 +290,7 @@ def _reset_text(
300
290
self ._bounding_box = (
301
291
self .tilegrid .x ,
302
292
self .tilegrid .y ,
303
- tight_box_x ,
293
+ box_x ,
304
294
tight_box_y ,
305
295
)
306
296
@@ -310,11 +300,11 @@ def _reset_text(
310
300
311
301
@staticmethod
312
302
def _line_spacing_ypixels (font , line_spacing ):
313
- # Note: Scale is not implemented at this time, any scaling is pushed up to the Group level
303
+ # Note: Scaling is provided at the Group level
314
304
return_value = int (line_spacing * font .get_bounding_box ()[1 ])
315
305
return return_value
316
306
317
- def _text_bounding_box (self , text , font , line_spacing , background_tight = False ):
307
+ def _text_bounding_box (self , text , font , line_spacing ):
318
308
319
309
# This empirical approach checks several glyphs for maximum ascender and descender height
320
310
# (consistent with label.py)
@@ -345,8 +335,6 @@ def _text_bounding_box(self, text, font, line_spacing, background_tight=False):
345
335
top = bottom = y_start
346
336
347
337
y_offset_tight = int ((font .get_glyph (ord ("M" )).height ) / 2 )
348
- # this needs to be reviewed (also in label.py), since it doesn't respond
349
- # properly to the number of newlines.
350
338
351
339
newline = False
352
340
@@ -387,17 +375,19 @@ def _text_bounding_box(self, text, font, line_spacing, background_tight=False):
387
375
left = 0
388
376
389
377
final_box_width = right - left
390
- if background_tight :
391
- final_box_height = bottom - top
392
- final_y_offset = - top + y_offset_tight
393
378
394
- else :
395
- final_box_height = (lines - 1 ) * self ._line_spacing_ypixels (
379
+
380
+ final_box_height_tight = bottom - top
381
+ final_y_offset_tight = - top + y_offset_tight
382
+
383
+ final_box_height_loose = (lines - 1 ) * self ._line_spacing_ypixels (
396
384
font , line_spacing
397
- ) + (ascender_max + descender_max )
398
- final_y_offset = ascender_max
385
+ ) + (ascender_max + descender_max )
386
+ final_y_offset_loose = ascender_max
387
+
388
+ # return (final_box_width, final_box_height, left, final_y_offset)
399
389
400
- return (final_box_width , final_box_height , left , final_y_offset )
390
+ return (final_box_width , final_box_height_tight , left , final_y_offset_tight , final_box_height_loose , final_y_offset_loose )
401
391
402
392
# pylint: disable=too-many-nested-blocks
403
393
def _place_text (
@@ -410,17 +400,10 @@ def _place_text(
410
400
yposition ,
411
401
text_palette_index = 1 ,
412
402
background_palette_index = 0 ,
413
- print_only_pixels = True , # print_only_pixels = True: only update the bitmap where the glyph
414
- # pixel color is > 0. This is especially useful for script fonts where glyph
415
- # bounding boxes overlap
416
- # Set `print_only_pixels=False` to write all pixels
417
403
):
418
404
# placeText - Writes text into a bitmap at the specified location.
419
405
#
420
- # Verify paletteIndex is working properly with * operator, especially
421
- # if accommodating multicolored fonts
422
- #
423
- # Note: Scale is not implemented at this time, is pushed up to Group level
406
+ # Note: scale is pushed up to Group level
424
407
425
408
bitmap_width = bitmap .width
426
409
bitmap_height = bitmap .height
@@ -465,38 +448,18 @@ def _place_text(
465
448
glyph_offset_x = (
466
449
my_glyph .tile_index * my_glyph .width
467
450
) # for type BuiltinFont, this creates the x-offset in the glyph bitmap.
468
- # for BDF loaded fonts, this should equal 0
469
-
470
- for y in range (my_glyph .height ):
471
- for x in range (my_glyph .width ):
472
- x_placement = x + xposition + my_glyph .dx
473
- y_placement = y + yposition - my_glyph .height - my_glyph .dy
474
-
475
- if (bitmap_width > x_placement >= 0 ) and (
476
- bitmap_height > y_placement >= 0
477
- ):
478
-
479
- # Allows for remapping the bitmap indexes using paletteIndex
480
- # for background and text.
481
- palette_indexes = (
482
- background_palette_index ,
483
- text_palette_index ,
484
- )
485
-
486
- this_pixel_color = palette_indexes [
487
- my_glyph .bitmap [
488
- y * my_glyph .bitmap .width + x + glyph_offset_x
489
- ]
490
- ]
491
-
492
- if not print_only_pixels or this_pixel_color > 0 :
493
- # write all characters if printOnlyPixels = False,
494
- # or if thisPixelColor is > 0
495
- bitmap [
496
- y_placement * bitmap_width + x_placement
497
- ] = this_pixel_color
498
- elif y_placement > bitmap_height :
499
- break
451
+ # for BDF loaded fonts, this should equal 0
452
+
453
+ bitmap .blit (
454
+ xposition + my_glyph .dx ,
455
+ yposition - my_glyph .height - my_glyph .dy ,
456
+ my_glyph .bitmap ,
457
+ x1 = glyph_offset_x ,
458
+ y1 = 0 ,
459
+ x2 = glyph_offset_x + my_glyph .width - 1 ,
460
+ y2 = 0 + my_glyph .height - 1 ,
461
+ skip_index = 0 , # do not copy over any 0 background pixels
462
+ )
500
463
501
464
xposition = xposition + my_glyph .shift_x
502
465
@@ -508,6 +471,22 @@ def bounding_box(self):
508
471
first two numbers are offset from the x, y origin of this group"""
509
472
return self ._bounding_box
510
473
474
+ # @property
475
+ # def scale(self):
476
+ # return self._scale
477
+
478
+ # @scale.setter
479
+ # def scale(self, new_scale):
480
+ # self._scale=new_scale
481
+ # #super(displayio.Group, self).scale.fset(self, new_scale)
482
+ # anchored_position=self._anchored_position # update the anchored_position
483
+
484
+ def set_scale (self , new_scale ):
485
+ """Set the scaling of the label"""
486
+ self ._scale = int (round (new_scale ))
487
+ self .scale = self ._scale
488
+ self .anchored_position = self ._anchored_position
489
+
511
490
@property
512
491
def line_spacing (self ):
513
492
"""The amount of space between lines of text, in multiples of the font's
@@ -557,7 +536,6 @@ def text(self):
557
536
@text .setter # Cannot set color or background color with text setter, use separate setter
558
537
def text (self , new_text ):
559
538
self ._reset_text (text = new_text )
560
- print ('updated text: {}' .format (new_text ))
561
539
562
540
@property
563
541
def font (self ):
0 commit comments