From cfaadb5b5164398d3a5267024d004d4d7ee87497 Mon Sep 17 00:00:00 2001 From: Pierre-Anthony Lemieux Date: Mon, 16 Jan 2023 10:21:27 +1100 Subject: [PATCH] Disallow 'c' units for all styles with the exception of ebutts:linePadding --- src/main/python/ttconv/style_properties.py | 70 ++++++++++++++++++---- src/test/python/test_isd.py | 1 + 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/main/python/ttconv/style_properties.py b/src/main/python/ttconv/style_properties.py index 1ae4bb10..5364bab0 100644 --- a/src/main/python/ttconv/style_properties.py +++ b/src/main/python/ttconv/style_properties.py @@ -135,6 +135,10 @@ class ExtentType: height: LengthType = LengthType() width: LengthType = LengthType() + def __post_init__(self): + if not isinstance(self.height, LengthType) or not isinstance(self.width, LengthType): + raise ValueError("Dimensions must be a length") + class GenericFontFamilyType(Enum): '''\\ ''' @@ -186,6 +190,10 @@ class PaddingType: after: LengthType = LengthType() start: LengthType = LengthType() + def __post_init__(self): + if not isinstance(self.before, LengthType) or not isinstance(self.end, LengthType) \ + or not isinstance(self.after, LengthType) or not isinstance(self.start, LengthType): + raise ValueError("Dimensions must be a length") class RubyAlignType(Enum): '''tts:rubyAlign value @@ -209,6 +217,10 @@ class Position(Enum): position: Position = Position.outside length: LengthType = None + def __post_init__(self): + if self.length is not None and not isinstance(self.length, LengthType): + raise ValueError("Length must be a length") + class ShowBackgroundType(Enum): '''tts:showBackground values ''' @@ -313,8 +325,11 @@ class Shadow: blur_radius: typing.Optional[LengthType] = None color: typing.Optional[ColorType] = None - shadows: typing.Tuple[TextShadowType.Shadow] + def __post_init__(self): + if self.blur_radius is not None and not isinstance(self.blur_radius, LengthType): + raise ValueError("The blur_radius value must be a length") + shadows: typing.Tuple[TextShadowType.Shadow] class UnicodeBidiType(Enum): '''tts:unicodeBidi values @@ -354,6 +369,9 @@ class CoordinateType: x: LengthType y: LengthType + def __post_init__(self): + if not isinstance(self.x, LengthType) or not isinstance(self.y, LengthType): + raise ValueError("Coordinates must be a length") @dataclass(frozen=True) class PositionType: @@ -372,6 +390,13 @@ class VEdge(Enum): h_edge: HEdge = HEdge.left v_edge: VEdge = VEdge.top + def __post_init__(self): + if not isinstance(self.h_offset, LengthType) or not isinstance(self.v_offset, LengthType): + raise ValueError("Offset must be a length") + + if LengthType.Units.c in (self.h_offset.units, self.v_offset.units): + raise ValueError("Offset must not be in 'c' units") + # # Style properties # @@ -465,7 +490,7 @@ def make_initial_value(): @staticmethod def validate(value): - return isinstance(value, LengthType) + return isinstance(value, LengthType) and value.units != LengthType.Units.c class Display(StyleProperty): '''Corresponds to tts:display.''' @@ -514,8 +539,8 @@ def make_initial_value(): @staticmethod def validate(value: ExtentType): return isinstance(value, ExtentType) \ - and value.width.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.c, LengthType.Units.rw) \ - and value.height.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.c, LengthType.Units.rh) + and value.width.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.rw) \ + and value.height.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.rh) class FillLineGap(StyleProperty): @@ -605,7 +630,7 @@ def make_initial_value(): @staticmethod def validate(value): - return value == SpecialValues.normal or isinstance(value, LengthType) + return value == SpecialValues.normal or (isinstance(value, LengthType) and value.units != LengthType.Units.c) class LinePadding(StyleProperty): @@ -685,8 +710,8 @@ def make_initial_value(): @staticmethod def validate(value: CoordinateType): return isinstance(value, CoordinateType) \ - and value.x.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.c, LengthType.Units.rw) \ - and value.y.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.c, LengthType.Units.rh) + and value.x.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.rw) \ + and value.y.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.rh) class Overflow(StyleProperty): @@ -716,8 +741,10 @@ def make_initial_value(): @staticmethod def validate(value): - return isinstance(value, PaddingType) + if not isinstance(value, PaddingType): + return False + return LengthType.Units.c not in (value.before.units, value.end.units, value.after.units, value.start.units) class Position(StyleProperty): '''Corresponds to tts:position.''' @@ -735,8 +762,8 @@ def make_initial_value(): @staticmethod def validate(value: PositionType): return isinstance(value, PositionType) \ - and value.h_offset.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.c, LengthType.Units.rw) \ - and value.v_offset.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.c, LengthType.Units.rh) + and value.h_offset.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.rw) \ + and value.v_offset.units in (LengthType.Units.pct, LengthType.Units.px, LengthType.Units.rh) class RubyAlign(StyleProperty): '''Corresponds to tts:rubyAlign.''' @@ -780,8 +807,13 @@ def make_initial_value(): @staticmethod def validate(value): - return value == SpecialValues.none or isinstance(value, RubyReserveType) + if value == SpecialValues.none: + return True + + if not isinstance(value, RubyReserveType): + return False + return value.length is None or value.length.units != LengthType.Units.c class Shear(StyleProperty): '''Corresponds to tts:shear.''' @@ -892,7 +924,13 @@ def make_initial_value(): @staticmethod def validate(value): - return value == SpecialValues.none or isinstance(value, TextOutlineType) + if value == SpecialValues.none: + return True + + if not isinstance(value, TextOutlineType): + return False + + return value.thickness.units != LengthType.Units.c class TextShadow(StyleProperty): @@ -908,7 +946,13 @@ def make_initial_value(): @staticmethod def validate(value): - return value == SpecialValues.none or isinstance(value, TextShadowType) + if value == SpecialValues.none: + return True + + if not isinstance(value, TextShadowType): + return False + + return all(s.blur_radius is None or s.blur_radius.units != LengthType.Units.c for s in value.shadows) class UnicodeBidi(StyleProperty): diff --git a/src/test/python/test_isd.py b/src/test/python/test_isd.py index d52ae1f9..8407092a 100644 --- a/src/test/python/test_isd.py +++ b/src/test/python/test_isd.py @@ -326,6 +326,7 @@ def test_compute_extent_px(self): self.assertAlmostEqual(extent.height.value, 100*25/doc.get_px_resolution().height) self.assertEqual(extent.height.units, styles.LengthType.Units.rh) + @unittest.skip("Removing support for 'c' units other with ebutts:linePadding") def test_compute_extent_c(self): doc = model.ContentDocument()