From b9143455f75a43d3a775f8cabb965a22d40e1d8e Mon Sep 17 00:00:00 2001 From: Andrej Peterka Date: Tue, 16 Sep 2025 11:14:43 +0200 Subject: [PATCH] Center align subtitles by default --- pycaption/scenarist.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/pycaption/scenarist.py b/pycaption/scenarist.py index cfb44ec4..1414d1ef 100644 --- a/pycaption/scenarist.py +++ b/pycaption/scenarist.py @@ -88,18 +88,16 @@ class ScenaristDVDWriter(BaseWriter): palette_image.putpalette([*paColor, *e1Color, *e2Color, *bgColor] + [0, 0, 0] * 252) font_langs = { - Language.get('en'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansDisplay-Regular-Note-Math.ttf", - 'align': 'left'}, - Language.get('ru'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansDisplay-Regular-Note-Math.ttf", - 'align': 'left'}, + Language.get('en'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansDisplay-Regular-Note-Math.ttf"}, + Language.get('ru'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansDisplay-Regular-Note-Math.ttf"}, Language.get('ar'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansDisplay-RegularAndArabic.ttf", 'align': 'right'}, Language.get('he'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansHebrew-Regular.ttf", 'align': 'right'}, - Language.get('hi'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansDevanagari-Regular.ttf", 'align': 'left'}, - Language.get('ja-JP'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansJP+Math-Regular.ttf", 'align': 'left'}, - Language.get('zh-TW'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansTC+Math-Regular.ttf", 'align': 'left'}, - Language.get('zh-CN'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansSC+Math-Regular.ttf", 'align': 'left'}, - Language.get('ko-KR'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansKR+Math-Regular.ttf", 'align': 'left'}, - Language.get('th'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansThai-Regular.ttf", 'align': 'left'}, + Language.get('hi'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansDevanagari-Regular.ttf"}, + Language.get('ja-JP'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansJP+Math-Regular.ttf"}, + Language.get('zh-TW'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansTC+Math-Regular.ttf"}, + Language.get('zh-CN'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansSC+Math-Regular.ttf"}, + Language.get('ko-KR'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansKR+Math-Regular.ttf"}, + Language.get('th'): {'fontfile': f"{os.path.dirname(__file__)}/NotoSansThai-Regular.ttf"}, } def __init__(self, relativize=True, video_width=720, video_height=480, fit_to_screen=True, tape_type='NON_DROP', @@ -223,6 +221,7 @@ def write( position='bottom', avoid_same_next_start_prev_end=False, tiff_compression='tiff_deflate', + align='center', ): if tiff_compression not in ['tiff_deflate', 'raw']: raise ValueError('Unknown tiff_compression. Supported: {}'.format('tiff_deflate, raw')) @@ -261,7 +260,7 @@ def write( raise ValueError('Cannot find appropriate font for selected language') fnt = distances[0][1]['fontfile'] - align = distances[0][1]['align'] + align = distances[0][1].get('align') or align missing_glyphs = self.get_missing_glyphs(fnt, self.get_characters(caps_final)) if missing_glyphs: @@ -317,7 +316,7 @@ def format_ts(self, value): str_value = str_value + ':%02d' % (int((int(value / 1000) % 1000) / int(1000 / self.frame_rate))) return str_value - def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, position: str = 'bottom', align: str = 'left'): + def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, position: str = 'bottom', align: str = 'center'): ascender, descender = fnt.getmetrics() line_spacing = ascender + abs(descender) # Basic line height without extra padding lines_written = 0