Skip to content

Commit ace3e78

Browse files
author
Margaret Matocha
committed
Cleaned up the text_bounding_box function to be similar to label.py
1 parent d0c7d38 commit ace3e78

File tree

1 file changed

+81
-117
lines changed

1 file changed

+81
-117
lines changed

adafruit_display_text/bitmap_label.py

Lines changed: 81 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -78,109 +78,83 @@ def text_bounding_box(
7878
font_height = ascender_max + descender_max
7979
font_yoffset = ascender_max
8080

81-
base_point = (0, 0) # entry point for the next glyph
8281

83-
x1_min = (
84-
y1_min
85-
) = (
86-
x2_max
87-
) = (
88-
y2_max
89-
) = None # initialize the bounding box corners (1:lower left, 2: upper right)
82+
lines = 1
9083

91-
y_offset = None # Provide the baseline y_offset for the bitmap
84+
font_height = font.get_glyph(ord("M")).height
85+
86+
87+
xposition = x_start = 0 # starting x position (left margin)
88+
yposition = y_start = 0
89+
90+
left = right = x_start
91+
top = bottom = y_start
9292

93-
box_height = 0
94-
box_width = 0
95-
box_height_adder = 0
9693

97-
firstline = True # handles special cases for first line
94+
y_offset_tight = int(( font.get_glyph(ord("M")).height - text.count("\n") *line_spacing_ypixels(font, line_spacing)
95+
)
96+
/ 2)
97+
# this needs to be reviewed (also in label.py), since it doesn't respond
98+
# properly to the number of newlines.
99+
100+
newline=False
98101

99102
for char in text:
103+
100104
if char == "\n": # newline
101-
if firstline: # This the first line
102-
firstline = False
103-
if background_tight:
104-
if (
105-
y1_min is None
106-
): # A newline was the first character of the first line
107-
y_offset = 0 #label_position_yoffset
108-
box_height_adder = (
109-
line_spacing_ypixels(font, line_spacing - 1) + y_offset
110-
)
111-
# for a leading newline, this adds to the box_height
112-
113-
else:
114-
y_offset = (
115-
-y1_min
116-
) # The bitmap y-offset is the max y-position of the first line
117-
118-
else: # background is "loose"
119-
y_offset = font_yoffset
120-
if y1_min is None:
121-
box_height_adder = line_spacing_ypixels(font, line_spacing)
122-
123-
base_point = (
124-
0,
125-
base_point[1] + line_spacing_ypixels(font, line_spacing),
126-
) # baseline point for the next glyph
105+
newline=True
127106

128107
else:
108+
129109
my_glyph = font.get_glyph(ord(char))
110+
130111
if my_glyph == None: # Error checking: no glyph found
131112
print("Glyph not found: {}".format(repr(char)))
132113
else:
133-
x1 = base_point[0] # x1,y1 = upper left of glyph
134-
x2 = x1 + my_glyph.shift_x # x2,y2 = lower right of glyph
135-
if background_tight:
136-
y1 = base_point[1] - (
137-
my_glyph.height + my_glyph.dy
138-
) # Upper left corner Note: Positive Y is down
139-
y2 = y1 + my_glyph.height
140-
else: # background is "loose"
141-
y1 = base_point[1] - font_yoffset
142-
y2 = y1 + font_height
143-
base_point = (
144-
base_point[0] + my_glyph.shift_x,
145-
base_point[1] + my_glyph.shift_y,
146-
) # update the next baseline point location
147-
148-
# find the min bounding box size incorporating this glyph's bounding box
149-
if x1_min is not None:
150-
x1_min = min(x1, x1_min)
151-
else:
152-
x1_min = min(0, x1) # ****
153-
if y1_min is not None:
154-
y1_min = min(y1, y1_min)
155-
else:
156-
y1_min = y1
157-
if x2_max is not None:
158-
x2_max = max(x2, x2_max)
159-
else:
160-
x2_max = x2
161-
if y2_max is not None:
162-
y2_max = max(y2, y2_max)
163-
else:
164-
y2_max = y2
165-
166-
if x1_min is not None and x2_max is not None:
167-
box_width = max(0, x2_max - x1_min)
168-
if y1_min is not None and y2_max is not None:
169-
box_height = y2_max - y1_min
170-
171-
if firstline: # This the first line
172-
if background_tight:
173-
y_offset = (
174-
-y1_min
175-
) # The bitmap y-offset is the max y-position of the first line
176-
else: # background is "loose"
177-
y_offset = font_yoffset
178-
179-
box_height = max(
180-
0, box_height + box_height_adder
181-
) # to add any additional height for leading newlines
182-
183-
return (box_width, box_height, -x1_min, y_offset) # -x1_min is the x_offset
114+
if newline:
115+
newline=False
116+
xposition = x_start # reset to left column
117+
yposition = yposition + line_spacing_ypixels(
118+
font, line_spacing
119+
) # Add a newline
120+
lines += 1
121+
xposition += my_glyph.shift_x
122+
right = max(right, xposition)
123+
124+
if yposition == y_start: # first line, find the Ascender height
125+
top = min(top, - my_glyph.height - my_glyph.dy + y_offset_tight)
126+
bottom = max(bottom, yposition - my_glyph.dy + y_offset_tight)
127+
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)
141+
)
142+
/ 2)
143+
144+
y_offset_tight = -top +label_calibration_offset #
145+
146+
147+
148+
final_box_width=right-left
149+
if background_tight:
150+
final_box_height=bottom-top
151+
final_y_offset=y_offset_tight
152+
153+
else:
154+
final_box_height=loose_height
155+
final_y_offset=ascender_max
156+
157+
return (final_box_width, final_box_height, 0, final_y_offset) # -x1_min is the x_offset
184158

185159

186160
def place_text(
@@ -212,26 +186,6 @@ def place_text(
212186
x_start = xposition # starting x position (left margin)
213187
y_start = yposition
214188

215-
if (
216-
background_palette_index != 0
217-
): # the textbackground is different from the bitmap background
218-
# draw a bounding box where the text will go
219-
220-
(ignore, font_line_height) = bounding_box(
221-
"M g", font, line_spacing, scale
222-
) # check height with ascender and descender.
223-
(box_x, box_y) = bounding_box(text, font, line_spacing, scale)
224-
box_y = max(font_line_height, box_y)
225-
226-
for y in range(box_y):
227-
for x in range(box_x):
228-
if (xposition + x < bitmap_width) and (
229-
yposition + y < bitmap_height
230-
): # check boundaries
231-
bitmap[
232-
(yposition + y) * bitmap_width + (xposition + x)
233-
] = background_palette_index
234-
235189
left = right = x_start
236190
top = bottom = y_start
237191

@@ -302,7 +256,7 @@ def place_text(
302256

303257
xposition = xposition + shift_x
304258

305-
return (left, top, left + right, bottom - top) # bounding_box
259+
return (left, top, right-left, bottom - top) # bounding_box
306260

307261

308262
class Label(displayio.Group):
@@ -357,15 +311,15 @@ def __init__(
357311
# Calculate the text bounding box
358312

359313
# Calculate tight box to provide bounding box dimensions to match label for anchor_position calculations
360-
(tight_box_x, tight_box_y, dummy_x_offset, tight_y_offset) = text_bounding_box(
314+
(tight_box_x, tight_box_y, x_offset, tight_y_offset) = text_bounding_box(
361315
text, font, self._line_spacing, background_tight=True,
362316
)
363317

364318
if background_tight:
365319
box_x = tight_box_x
366320
box_y = tight_box_y
367-
x_offset = dummy_x_offset
368321
y_offset = tight_y_offset
322+
369323
else:
370324
(box_x, box_y, x_offset, y_offset) = text_bounding_box(
371325
text, font, self._line_spacing, background_tight=background_tight,
@@ -399,7 +353,7 @@ def __init__(
399353
label_position_yoffset = int( # To calibrate with label.py positioning
400354
(
401355
font.get_glyph(ord("M")).height
402-
- font.get_bounding_box()[1] * self._line_spacing
356+
- text.count("\n") * font.get_bounding_box()[1] * self._line_spacing
403357
)
404358
/ 2
405359
)
@@ -413,8 +367,7 @@ def __init__(
413367
tile_height=box_y,
414368
default_tile=0,
415369
x=-padding_left,
416-
#y=label_position_yoffset - y_offset - padding_left,
417-
y= - y_offset - padding_left,
370+
y= label_position_yoffset - y_offset - padding_top,
418371
)
419372

420373
# instance the Group with super... super().__init__(
@@ -430,10 +383,21 @@ def __init__(
430383

431384
self.bounding_box = (
432385
self.tilegrid.x,
433-
self.tilegrid.y + (y_offset - tight_y_offset),
386+
#self.tilegrid.y + (y_offset - tight_y_offset),
387+
self.tilegrid.y, #+ (y_offset - tight_y_offset),
434388
tight_box_x,
435389
tight_box_y,
436390
)
391+
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+
437401
# Update bounding_box values. Note: To be consistent with label.py,
438402
# this is the bounding box for the text only, not including the background.
439403
# ******** Need repair

0 commit comments

Comments
 (0)