Skip to content

Commit ce892e4

Browse files
committed
improved readability
added unit tests
1 parent aaf19d8 commit ce892e4

2 files changed

Lines changed: 81 additions & 5 deletions

File tree

src/main/python/ttconv/filters/doc/imsc11text.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,18 @@ def _regions_overlap(r1: ISD.Region, r2: ISD.Region) -> bool:
255255
return False
256256

257257
# r1 bounding box
258-
x1, y1 = o1.x.value, o1.y.value
259-
x2, y2 = x1 + e1.width.value, y1 + e1.height.value
258+
left1 = o1.x.value
259+
top1 = o1.y.value
260+
right1 = left1 + e1.width.value
261+
bottom1 = top1 + e1.height.value
260262

261263
# r2 bounding box
262-
ax1, ay1 = o2.x.value, o2.y.value
263-
ax2, ay2 = ax1 + e2.width.value, ay1 + e2.height.value
264+
left2 = o2.x.value
265+
top2 = o2.y.value
266+
right2 = left2 + e2.width.value
267+
bottom2 = top2 + e2.height.value
264268

265-
return not (x2 <= ax1 or ax2 <= x1 or y2 <= ay1 or ay2 <= y1)
269+
return not (right1 <= left2 or right2 <= left1 or bottom1 <= top2 or bottom2 <= top1)
266270

267271

268272
def _validate_isd_element(element):

src/test/python/test_imsc11text_filter.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,78 @@ def test_line_padding_rw_raises(self):
598598
with self.assertRaises(ValueError):
599599
IMSC11TextFilter().process(doc)
600600

601+
# Spatial overlap checks
602+
603+
def _make_two_region_doc(self, ox1, oy1, ew1, eh1, ox2, oy2, ew2, eh2):
604+
"""Builds a doc with two regions each containing active content.
605+
606+
All coordinates and sizes are in percent units.
607+
"""
608+
doc = model.ContentDocument()
609+
body = model.Body(doc)
610+
doc.set_body(body)
611+
div = model.Div(doc)
612+
body.push_child(div)
613+
614+
for idx, (ox, oy, ew, eh) in enumerate(
615+
[(ox1, oy1, ew1, eh1), (ox2, oy2, ew2, eh2)]
616+
):
617+
region = model.Region(f"r{idx}", doc)
618+
region.set_style(
619+
styles.StyleProperties.Origin,
620+
styles.CoordinateType(
621+
x=styles.LengthType(ox, styles.LengthType.Units.pct),
622+
y=styles.LengthType(oy, styles.LengthType.Units.pct),
623+
),
624+
)
625+
region.set_style(
626+
styles.StyleProperties.Extent,
627+
styles.ExtentType(
628+
width=styles.LengthType(ew, styles.LengthType.Units.pct),
629+
height=styles.LengthType(eh, styles.LengthType.Units.pct),
630+
),
631+
)
632+
doc.put_region(region)
633+
634+
p = model.P(doc)
635+
p.set_begin(Fraction(0))
636+
p.set_end(Fraction(5))
637+
p.set_region(region)
638+
div.push_child(p)
639+
span = model.Span(doc)
640+
p.push_child(span)
641+
text = model.Text(doc, "Hello")
642+
span.push_child(text)
643+
644+
return doc
645+
646+
def test_overlapping_regions_raises(self):
647+
# r0: x=[0,60], y=[0,50] r1: x=[40,100], y=[0,50] → overlap at x=[40,60]
648+
doc = self._make_two_region_doc(0, 0, 60, 50, 40, 0, 60, 50)
649+
with self.assertRaises(ValueError):
650+
IMSC11TextFilter().process(doc)
651+
652+
def test_abutting_regions_horizontal_passes(self):
653+
# r0: x=[0,50], y=[0,100] r1: x=[50,100], y=[0,100] → share edge, no overlap
654+
doc = self._make_two_region_doc(0, 0, 50, 100, 50, 0, 50, 100)
655+
filt = IMSC11TextFilter()
656+
filt.process(doc)
657+
self.assertIn(IMSC_11_TEXT_PROFILE_DESIGNATOR, doc.get_content_profiles())
658+
659+
def test_abutting_regions_vertical_passes(self):
660+
# r0: x=[0,100], y=[0,50] r1: x=[0,100], y=[50,100] → share edge, no overlap
661+
doc = self._make_two_region_doc(0, 0, 100, 50, 0, 50, 100, 50)
662+
filt = IMSC11TextFilter()
663+
filt.process(doc)
664+
self.assertIn(IMSC_11_TEXT_PROFILE_DESIGNATOR, doc.get_content_profiles())
665+
666+
def test_non_overlapping_regions_with_gap_passes(self):
667+
# r0: x=[0,40], y=[0,100] r1: x=[60,100], y=[0,100] → gap at x=[40,60]
668+
doc = self._make_two_region_doc(0, 0, 40, 100, 60, 0, 40, 100)
669+
filt = IMSC11TextFilter()
670+
filt.process(doc)
671+
self.assertIn(IMSC_11_TEXT_PROFILE_DESIGNATOR, doc.get_content_profiles())
672+
601673
# Filter auto-registration
602674
def test_filter_registered_by_name(self):
603675
filt_cls = DocumentFilter.get_filter_by_name("imsc11text")

0 commit comments

Comments
 (0)