From 3cb898593c67ccfa13a1f0f5b37601b9bcea806e Mon Sep 17 00:00:00 2001 From: Pierre-Anthony Lemieux Date: Thu, 21 Dec 2023 21:45:51 -0800 Subject: [PATCH] Added support for preserve textAlign --- src/main/python/ttconv/filters/doc/lcd.py | 14 ++++++++--- src/test/python/test_lcd_filter.py | 29 +++++++++++++++++++++++ src/test/python/test_tt.py | 2 +- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/python/ttconv/filters/doc/lcd.py b/src/main/python/ttconv/filters/doc/lcd.py index d33dd527..d591efd8 100644 --- a/src/main/python/ttconv/filters/doc/lcd.py +++ b/src/main/python/ttconv/filters/doc/lcd.py @@ -37,7 +37,7 @@ from ttconv.filters.supported_style_properties import SupportedStylePropertiesFilter from ttconv.isd import StyleProcessors from ttconv.model import ContentDocument, ContentElement, Region, P -from ttconv.style_properties import ColorType, CoordinateType, DisplayAlignType, ExtentType, LengthType, StyleProperties, WritingModeType, NamedColors +from ttconv.style_properties import TextAlignType, ColorType, CoordinateType, DisplayAlignType, ExtentType, LengthType, StyleProperties, WritingModeType, NamedColors import ttconv.utils LOGGER = logging.getLogger(__name__) @@ -73,6 +73,9 @@ def name(cls): # specifies the safe area as an integer percentage safe_area: typing.Optional[int] = field(default=10, metadata={"decoder": int}) + # preserve text alignment the text color + preserve_text_align: typing.Optional[bool] = field(default=False, metadata={"decoder": bool}) + # overrides the text color color: typing.Optional[ColorType] = field(default=None, metadata={"decoder": ttconv.utils.parse_color}) @@ -100,6 +103,9 @@ def process(self, doc: ContentDocument) -> ContentDocument: StyleProperties.Position: [] } + if self.config.preserve_text_align: + supported_styles.update({StyleProperties.TextAlign: []}) + if self.config.color is None: supported_styles.update({StyleProperties.Color: []}) @@ -189,7 +195,6 @@ def process(self, doc: ContentDocument) -> ContentDocument: region.set_style(StyleProperties.DisplayAlign, new_display_align) # reposition region - # TODO: allow configurable safe area region.set_style( StyleProperties.Origin, CoordinateType( @@ -236,4 +241,7 @@ def process(self, doc: ContentDocument) -> ContentDocument: # apply text color if doc.get_body() is not None and self.config.color is not None: - doc.get_body().set_style(StyleProperties.Color, self.config.color) \ No newline at end of file + doc.get_body().set_style(StyleProperties.Color, self.config.color) + + if doc.get_body() is not None and not self.config.preserve_text_align: + doc.get_body().set_style(StyleProperties.TextAlign, TextAlignType.center) diff --git a/src/test/python/test_lcd_filter.py b/src/test/python/test_lcd_filter.py index cef25221..ad6f7ef9 100644 --- a/src/test/python/test_lcd_filter.py +++ b/src/test/python/test_lcd_filter.py @@ -293,6 +293,35 @@ def test_region_resizing(self): styles.DisplayAlignType.after ) + def test_text_align(self): + doc = model.ContentDocument() + + # r1: origin=10,10 extent=80,20 + r1 = model.Region("r1", doc) + doc.put_region(r1) + + # body + body = model.Body(doc) + doc.set_body(body) + + # div + div = model.Div(doc) + body.push_child(div) + + # p1: r1 + p1 = model.P(doc) + p1.set_id("p1") + p1.set_region(r1) + p1.set_style(styles.StyleProperties.TextAlign, styles.TextAlignType.end) + div.push_child(p1) + + # apply filter + filter = LCDFilter(LCDFilterConfig()) + + filter.process(doc) + + self.assertIsNone(p1.get_style(styles.StyleProperties.TextAlign)) + self.assertEqual(body.get_style(styles.StyleProperties.TextAlign), styles.TextAlignType.center) if __name__ == '__main__': unittest.main() diff --git a/src/test/python/test_tt.py b/src/test/python/test_tt.py index e1c36a0f..39bcb95b 100644 --- a/src/test/python/test_tt.py +++ b/src/test/python/test_tt.py @@ -221,7 +221,7 @@ def test_lcd_filter(self): '-i', in_path, '-o', out_path, '--filter', 'lcd', - '--config', '{"lcd": {"bg_color": "blue", "safe_area": 0, "color": "red"}}' + '--config', '{"lcd": {"bg_color": "blue", "safe_area": 0, "color": "red", "preserve_text_align": true}}' ]) if __name__ == '__main__':