Skip to content

Commit 0414edd

Browse files
committed
Line breaks when forced to top or bottom
1 parent 4685a6a commit 0414edd

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

pycaption/scenarist.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ def format_ts(self, value):
317317
return str_value
318318

319319
def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, position: str = 'bottom', align: str = 'left'):
320+
ascender, descender = fnt.getmetrics()
321+
line_spacing = ascender + abs(descender) # Basic line height without extra padding
322+
lines_written = 0
320323
for caption in caption_list[::-1]:
321324
text = caption.get_text()
322325
l, t, r, b = draw.textbbox((0, 0), text, font=fnt, align=align)
@@ -354,9 +357,9 @@ def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, posi
354357
if position != 'source':
355358
x = self.video_width / 2 - r / 2
356359
if position == 'bottom':
357-
y = self.video_height - b - 10 # padding for readability
360+
y = self.video_height - b - 10 - lines_written * line_spacing # padding for readability
358361
elif position == 'top':
359-
y = 10
362+
y = 10 + lines_written * line_spacing
360363
else:
361364
raise ValueError('Unknown "position": {}'.format(position))
362365

@@ -381,3 +384,4 @@ def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, posi
381384
draw.text((x + adj, y - adj), text, font=fnt, fill=borderColor, align=align)
382385

383386
draw.text((x, y), text, font=fnt, fill=fontColor, align=align)
387+
lines_written += 1

0 commit comments

Comments
 (0)