Skip to content

Commit

Permalink
Line breaks when forced to top or bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
sannies committed Jan 15, 2025
1 parent 4685a6a commit 0414edd
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pycaption/scenarist.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ def format_ts(self, value):
return str_value

def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, position: str = 'bottom', align: str = 'left'):
ascender, descender = fnt.getmetrics()
line_spacing = ascender + abs(descender) # Basic line height without extra padding
lines_written = 0
for caption in caption_list[::-1]:
text = caption.get_text()
l, t, r, b = draw.textbbox((0, 0), text, font=fnt, align=align)
Expand Down Expand Up @@ -354,9 +357,9 @@ def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, posi
if position != 'source':
x = self.video_width / 2 - r / 2
if position == 'bottom':
y = self.video_height - b - 10 # padding for readability
y = self.video_height - b - 10 - lines_written * line_spacing # padding for readability
elif position == 'top':
y = 10
y = 10 + lines_written * line_spacing
else:
raise ValueError('Unknown "position": {}'.format(position))

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

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

0 comments on commit 0414edd

Please sign in to comment.