Skip to content

Commit

Permalink
Added support for preserve textAlign
Browse files Browse the repository at this point in the history
  • Loading branch information
palemieux committed Dec 22, 2023
1 parent f877ac5 commit 3cb8985
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/main/python/ttconv/filters/doc/lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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: []})

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
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)
29 changes: 29 additions & 0 deletions src/test/python/test_lcd_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion src/test/python/test_tt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit 3cb8985

Please sign in to comment.