Beispiel #1
0
def _get_region_from_model(doc: model.ContentDocument, x_origin: Number,
                           y_origin: Number, width: Number, height: Number,
                           display_align: styles.DisplayAlignType):
    """Returns a matching region from `doc` or creates one
  """

    found_region = None

    regions = list(doc.iter_regions())

    for r in regions:
        r_origin: styles.CoordinateType = r.get_style(
            styles.StyleProperties.Origin)
        assert r_origin is not None
        assert r_origin.x.units is styles.LengthType.Units.pct
        assert r_origin.y.units is styles.LengthType.Units.pct
        if r_origin.x.value != x_origin or r_origin.y.value != y_origin:
            continue

        r_extent: styles.ExtentType = r.get_style(
            styles.StyleProperties.Extent)
        assert r_extent is not None
        assert r_extent.height.units is styles.LengthType.Units.pct
        assert r_extent.width.units is styles.LengthType.Units.pct
        if r_extent.height.value != height or r_extent.width.value != width:
            continue

        r_display_align: styles.DisplayAlignType = r.get_style(
            styles.StyleProperties.DisplayAlign)
        assert r_display_align is not None
        if r_display_align != display_align:
            continue

        found_region = r
        break

    if found_region is None:
        found_region = model.Region(f"r{len(regions)}", doc)
        found_region.set_style(
            styles.StyleProperties.Extent,
            styles.ExtentType(
                height=styles.LengthType(height, styles.LengthType.Units.pct),
                width=styles.LengthType(width, styles.LengthType.Units.pct),
            ))
        found_region.set_style(
            styles.StyleProperties.Origin,
            styles.CoordinateType(
                x=styles.LengthType(x_origin, styles.LengthType.Units.pct),
                y=styles.LengthType(y_origin, styles.LengthType.Units.pct)))
        found_region.set_style(styles.StyleProperties.DisplayAlign,
                               display_align)
        doc.put_region(found_region)

    return found_region
Beispiel #2
0
    def test_srt_writer(self):
        doc = ContentDocument()

        r1 = Region("r1", doc)
        doc.put_region(r1)

        r2 = Region("r2", doc)
        r2.set_begin(Fraction(2))
        r2.set_end(Fraction(4))
        doc.put_region(r2)

        body = Body(doc)
        doc.set_body(body)

        div = Div(doc)
        body.push_child(div)

        p = P(doc)
        p.set_region(r1)
        p.set_end(Fraction(2))
        div.push_child(p)

        span = Span(doc)
        span.push_child(Text(doc, "Lorem ipsum dolor sit amet,"))
        p.push_child(span)

        p = P(doc)
        p.set_region(r2)
        div.push_child(p)

        span = Span(doc)
        span.push_child(Text(doc, "consectetur adipiscing elit."))
        p.push_child(span)

        p = P(doc)
        p.set_region(r1)
        p.set_begin(Fraction(4))
        p.set_end(Fraction(6))
        div.push_child(p)

        span = Span(doc)
        span.push_child(
            Text(doc, "Pellentesque interdum lacinia sollicitudin."))
        p.push_child(span)

        expected_srt = """1
00:00:00,000 --> 00:00:02,000
Lorem ipsum dolor sit amet,

2
00:00:02,000 --> 00:00:04,000
consectetur adipiscing elit.

3
00:00:04,000 --> 00:00:06,000
Pellentesque interdum lacinia sollicitudin.
"""

        srt_from_model = srt_writer.from_model(doc)

        self.assertEqual(expected_srt, srt_from_model)
Beispiel #3
0
    def test_process_isd(self):
        supported_style_properties = SupportedStylePropertiesFilter({
            StyleProperties.BackgroundColor: [NamedColors.red.value],
            StyleProperties.Extent: []
        })

        doc = ContentDocument()

        r1 = Region("r1", doc)
        r1.set_style(StyleProperties.BackgroundColor, NamedColors.red.value)
        r1.set_style(StyleProperties.LuminanceGain, 2.0)
        doc.put_region(r1)

        b = Body(doc)
        b.set_begin(Fraction(1))
        b.set_end(Fraction(10))
        doc.set_body(b)

        div1 = Div(doc)
        div1.set_region(r1)
        b.push_child(div1)

        p1 = P(doc)
        p1.set_style(StyleProperties.BackgroundColor, NamedColors.white.value)
        p1.set_style(StyleProperties.Direction, DirectionType.rtl)
        div1.push_child(p1)

        span1 = Span(doc)
        span1.set_style(StyleProperties.BackgroundColor, NamedColors.red.value)
        span1.set_style(StyleProperties.FontStyle, FontStyleType.italic)
        span1.set_style(StyleProperties.Direction, DirectionType.ltr)
        p1.push_child(span1)

        t1 = Text(doc, "hello")
        span1.push_child(t1)

        significant_times = sorted(ISD.significant_times(doc))
        self.assertEqual(3, len(significant_times))

        isd = ISD.from_model(doc, significant_times[1])

        r1 = isd.get_region("r1")

        self.assertEqual(len(Region._applicableStyles), len(r1._styles))
        self.assertEqual(NamedColors.red.value,
                         r1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(2.0, r1.get_style(StyleProperties.LuminanceGain))

        body1 = list(r1)[0]
        div1 = list(body1)[0]
        p1 = list(div1)[0]
        span1 = list(p1)[0]

        self.assertEqual(len(P._applicableStyles), len(p1._styles))
        self.assertEqual(NamedColors.white.value,
                         p1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(DirectionType.rtl,
                         p1.get_style(StyleProperties.Direction))

        self.assertEqual(len(Span._applicableStyles), len(span1._styles))
        self.assertEqual(NamedColors.red.value,
                         span1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(FontStyleType.italic,
                         span1.get_style(StyleProperties.FontStyle))
        self.assertEqual(DirectionType.ltr,
                         span1.get_style(StyleProperties.Direction))

        supported_style_properties.process(isd)

        self.assertEqual(2, len(r1._styles))
        self.assertEqual(NamedColors.red.value,
                         r1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(
            ExtentType(height=LengthType(value=0.0, units=LengthType.Units.rh),
                       width=LengthType(value=0.0, units=LengthType.Units.rw)),
            r1.get_style(StyleProperties.Extent))

        self.assertEqual(0, len(p1._styles))
        self.assertIsNone(p1.get_style(StyleProperties.BackgroundColor))
        self.assertIsNone(p1.get_style(StyleProperties.Direction))

        self.assertEqual(1, len(span1._styles))
        self.assertEqual(NamedColors.red.value,
                         span1.get_style(StyleProperties.BackgroundColor))
        self.assertIsNone(span1.get_style(StyleProperties.FontStyle))
        self.assertIsNone(span1.get_style(StyleProperties.Direction))
Beispiel #4
0
    def test_process_isd(self):
        default_style_value_filter = DefaultStylePropertyValuesFilter({
            StyleProperties.BackgroundColor:
            NamedColors.red.value,
            StyleProperties.Direction:
            DirectionType.ltr
        })

        doc = ContentDocument()

        r1 = Region("r1", doc)
        r1.set_style(StyleProperties.BackgroundColor, NamedColors.red.value)
        r1.set_style(StyleProperties.LuminanceGain, 2.0)
        doc.put_region(r1)

        b = Body(doc)
        b.set_begin(Fraction(1))
        b.set_end(Fraction(10))
        doc.set_body(b)

        div1 = Div(doc)
        div1.set_region(r1)
        b.push_child(div1)

        p1 = P(doc)
        p1.set_style(StyleProperties.BackgroundColor, NamedColors.white.value)
        p1.set_style(StyleProperties.Direction, DirectionType.rtl)
        div1.push_child(p1)

        span1 = Span(doc)
        span1.set_style(StyleProperties.BackgroundColor, NamedColors.red.value)
        span1.set_style(StyleProperties.FontStyle, FontStyleType.italic)
        span1.set_style(StyleProperties.Direction, DirectionType.ltr)
        p1.push_child(span1)

        t1 = Text(doc, "hello")
        span1.push_child(t1)

        significant_times = sorted(ISD.significant_times(doc))
        self.assertEqual(3, len(significant_times))

        isd = ISD.from_model(doc, significant_times[1])

        r1 = isd.get_region("r1")

        self.assertEqual(len(Region._applicableStyles), len(r1._styles))
        self.assertEqual(NamedColors.red.value,
                         r1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(2.0, r1.get_style(StyleProperties.LuminanceGain))

        body1 = list(r1)[0]
        div1 = list(body1)[0]
        p1 = list(div1)[0]
        span1 = list(p1)[0]

        self.assertEqual(len(P._applicableStyles), len(p1._styles))
        self.assertEqual(NamedColors.white.value,
                         p1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(DirectionType.rtl,
                         p1.get_style(StyleProperties.Direction))

        self.assertEqual(len(Span._applicableStyles), len(span1._styles))
        self.assertEqual(NamedColors.red.value,
                         span1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(FontStyleType.italic,
                         span1.get_style(StyleProperties.FontStyle))
        self.assertEqual(DirectionType.ltr,
                         span1.get_style(StyleProperties.Direction))

        default_style_value_filter.process(isd)

        self.assertEqual(len(Region._applicableStyles) - 1, len(r1._styles))
        self.assertIsNone(r1.get_style(StyleProperties.BackgroundColor))

        self.assertEqual(len(P._applicableStyles), len(p1._styles))
        self.assertEqual(NamedColors.white.value,
                         p1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(DirectionType.rtl,
                         p1.get_style(StyleProperties.Direction))

        self.assertEqual(len(Span._applicableStyles) - 1, len(span1._styles))
        self.assertIsNone(span1.get_style(StyleProperties.BackgroundColor))
        self.assertEqual(FontStyleType.italic,
                         span1.get_style(StyleProperties.FontStyle))
        self.assertEqual(DirectionType.ltr,
                         span1.get_style(StyleProperties.Direction))