Skip to content

Commit 78993e0

Browse files
authored
Merge pull request #35 from castlabs/HDCP-1267
get multiline right!
2 parents ee4d3ad + a7c08b2 commit 78993e0

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

pycaption/subtitler_image_based.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,22 @@ def write_images(
227227
def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, position: str = 'bottom',
228228
align: str = 'left'):
229229
ascender, descender = fnt.getmetrics()
230-
line_spacing = (ascender + abs(descender)) * 0.75 # Basic line height without extra padding
230+
line_spacing = (ascender + abs(descender)) * 0.80 # Basic line height without extra padding
231+
232+
# Split captions containing \n into separate single-line captions
233+
flat_captions = []
234+
for caption in caption_list:
235+
text = caption.get_text()
236+
if '\n' in text:
237+
for line in text.split('\n'):
238+
flat_captions.append(Caption(caption.start, caption.end,
239+
[CaptionNode.create_text(line)],
240+
layout_info=caption.layout_info))
241+
else:
242+
flat_captions.append(caption)
243+
231244
lines_written = 0
232-
for caption in caption_list[::-1]:
245+
for caption in flat_captions[::-1]:
233246
text = caption.get_text()
234247
l, t, r, b = draw.textbbox((0, 0), text, font=fnt, align=align)
235248

@@ -266,7 +279,8 @@ def printLine(self, draw: ImageDraw, caption_list: Caption, fnt: ImageFont, posi
266279
if position != 'source':
267280
x = self.video_width / 2 - r / 2
268281
if position == 'bottom':
269-
# Place baseline at 5% from the bottom; descender runs below
282+
# Place baseline at 8% from the bottom; descender runs below
283+
# Additional lines grow upward from the same anchor
270284
y = self.video_height * 0.92 - ascender - lines_written * line_spacing
271285
elif position == 'top':
272286
y = 10 + lines_written * line_spacing

tests/test_subtitler_image_based.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11

22
import os
3+
from unittest import skip
34

45
import pytest
56
from PIL import Image, ImageDraw, ImageFont
67

8+
from pycaption import SRTReader
79
from pycaption.base import Caption, CaptionNode
810
from pycaption.exceptions import CaptionRendererError
11+
from pycaption.filtergraph import FiltergraphWriter
912
from pycaption.geometry import Layout, Point, Size, UnitEnum
1013
from pycaption.subtitler_image_based import SubtitleImageBasedWriter
1114

@@ -112,12 +115,20 @@ class TestBaselineAlignment:
112115

113116
NO_DESCENDER = "AHLEN" # no descenders
114117
WITH_DESCENDER = "gypsy" # descenders: g, y, p
118+
WITH_DESCENDER_TOP = "gypsy\nAHLEN" # descenders: g, y, p
119+
WITH_DESCENDER_BOTTOM = "AHLEN\ngypsy" # descenders: g, y, p
120+
115121

116122
COMBOS = [
117123
("no_desc_x2", [NO_DESCENDER, NO_DESCENDER]),
118124
("desc_x2", [WITH_DESCENDER, WITH_DESCENDER]),
119125
("top_no_bottom_yes", [NO_DESCENDER, WITH_DESCENDER]),
120126
("top_yes_bottom_no", [WITH_DESCENDER, NO_DESCENDER]),
127+
("one_line-no", [NO_DESCENDER]),
128+
("one_line-yes", [WITH_DESCENDER]),
129+
("one_line-yes", [WITH_DESCENDER]),
130+
("two-in-one-a", [WITH_DESCENDER_TOP]),
131+
("tow-in-one-b", [WITH_DESCENDER_BOTTOM]),
121132
]
122133

123134
@pytest.fixture(params=COMBOS, ids=[c[0] for c in COMBOS])
@@ -140,5 +151,7 @@ def test_baseline_visual(self, combo, tmp_path):
140151
guide.line([(0, baseline_y), (width, baseline_y)], fill=(255, 0, 0, 200), width=1)
141152

142153
out = tmp_path / f"baseline_{name}.png"
154+
out = f"tests/baseline_samples/baseline_{name}.png"
143155
img.save(str(out))
144-
print(f"\nSaved: {out}")
156+
print(f"\nSaved: {out}")
157+

0 commit comments

Comments
 (0)