@@ -317,6 +317,9 @@ def format_ts(self, value):
317
317
return str_value
318
318
319
319
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
320
323
for caption in caption_list [::- 1 ]:
321
324
text = caption .get_text ()
322
325
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
354
357
if position != 'source' :
355
358
x = self .video_width / 2 - r / 2
356
359
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
358
361
elif position == 'top' :
359
- y = 10
362
+ y = 10 + lines_written * line_spacing
360
363
else :
361
364
raise ValueError ('Unknown "position": {}' .format (position ))
362
365
@@ -381,3 +384,4 @@ def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, posi
381
384
draw .text ((x + adj , y - adj ), text , font = fnt , fill = borderColor , align = align )
382
385
383
386
draw .text ((x , y ), text , font = fnt , fill = fontColor , align = align )
387
+ lines_written += 1
0 commit comments