コード例 #1
0
    def test_layout_is_relative(self):
        empty_layout = Layout()

        self.assertTrue(empty_layout.is_relative())

        size_px = Size(30, UnitEnum.PIXEL)
        size_px2 = Size(30, UnitEnum.PIXEL)

        size_percent = Size(30, UnitEnum.PERCENT)
        size_percent2 = Size(30, UnitEnum.PERCENT)

        point_abs = Point(size_px, size_px2)
        point_rel = Point(size_percent, size_percent2)

        stretch_abs = Stretch(size_px, size_px2)
        stretch_rel = Stretch(size_percent, size_percent2)

        layout_abs = Layout(origin=point_abs, extent=stretch_abs, padding=None)

        layout_mix = Layout(origin=point_abs, extent=stretch_rel, padding=None)

        layout_rel = Layout(origin=point_rel, extent=stretch_rel, padding=None)

        self.assertFalse(layout_abs.is_relative())
        self.assertFalse(layout_mix.is_relative())
        self.assertTrue(layout_rel.is_relative())
コード例 #2
0
    def test_only_the_custom_region_is_created(self):
        caption_set = DFXPReader().read(
            SAMPLE_DFXP_TO_RENDER_WITH_ONLY_DEFAULT_POSITIONING_INPUT)

        new_region = Layout(alignment=Alignment(HorizontalAlignmentEnum.LEFT,
                                                VerticalAlignmentEnum.TOP))

        dfxp = SinglePositioningDFXPWriter(new_region).write(caption_set)
        # Using a different parser, because this preserves letter case
        # The output file is ok, but when parsing it, the "regular" parses
        # loses letter case.
        layout = BeautifulSoup(dfxp, features='xml').findChild('layout')

        self.assertEqual(len(layout.findChildren('region')), 1)

        region = layout.findChild('region')
        text_align = region['tts:textAlign']
        display_align = region['tts:displayAlign']

        internal_alignment = _create_internal_alignment(
            text_align, display_align)  # noqa
        self.assertEqual(internal_alignment.horizontal,
                         HorizontalAlignmentEnum.LEFT)  # noqa
        self.assertEqual(internal_alignment.vertical,
                         VerticalAlignmentEnum.TOP)  # noqa
コード例 #3
0
ファイル: dfxp.py プロジェクト: jpenney/pycaption
    def _extract_positioning_information(self, region_id, element):
        """Returns a Layout object that describes the element's positioning
        information

        :param region_id: the id of the region to which the element is
            associated
        :type region_id: unicode
        :param element: BeautifulSoup Tag or NavigableString; this only comes
            into action (at the moment) if the
        :rtype: Layout
        """
        region_tag = None

        if region_id is not None:
            region_tag = self.find(u'region', {u'xml:id': region_id})

        region_scraper = LayoutInfoScraper(self, region_tag)

        layout_info = region_scraper.scrape_positioning_info(
            element, self.read_invalid_positioning)

        if layout_info and any(layout_info):
            # layout_info contains information?
            return Layout(*layout_info)
        else:
            # layout_info doesn't contain any information
            return self.NO_POSITIONING_INFO
コード例 #4
0
    def test_only_the_specified_custom_attributes_are_created_for_the_region(
            self):  # noqa
        caption_set = DFXPReader().read(
            SAMPLE_DFXP_TO_RENDER_WITH_ONLY_DEFAULT_POSITIONING_INPUT)

        new_region = Layout(alignment=Alignment(HorizontalAlignmentEnum.LEFT,
                                                VerticalAlignmentEnum.TOP))

        dfxp = SinglePositioningDFXPWriter(new_region).write(caption_set)

        region = BeautifulSoup(dfxp).find('region')
        self.assertTrue('xml:id' in region.attrs)
        self.assertNotEqual(region.attrs['xml:id'], DFXP_DEFAULT_REGION_ID)
        self.assertEqual(len(region.attrs), 3)
コード例 #5
0
    def test_only_the_specified_custom_attributes_are_created_for_the_region(
            self, sample_dfxp_to_render_with_only_default_positioning_input):
        caption_set = DFXPReader().read(
            sample_dfxp_to_render_with_only_default_positioning_input)

        new_region = Layout(alignment=Alignment(HorizontalAlignmentEnum.LEFT,
                                                VerticalAlignmentEnum.TOP))

        dfxp = SinglePositioningDFXPWriter(new_region).write(caption_set)

        region = BeautifulSoup(dfxp).find('region')

        assert 'xml:id' in region.attrs
        assert region.attrs['xml:id'] != DFXP_DEFAULT_REGION_ID
        assert len(region.attrs) == 3
コード例 #6
0
    def test_only_the_custom_region_is_created(
            self, sample_dfxp_to_render_with_only_default_positioning_input):
        caption_set = DFXPReader().read(
            sample_dfxp_to_render_with_only_default_positioning_input)

        new_region = Layout(alignment=Alignment(HorizontalAlignmentEnum.LEFT,
                                                VerticalAlignmentEnum.TOP))

        dfxp = SinglePositioningDFXPWriter(new_region).write(caption_set)
        # Using a different parser, because this preserves letter case
        # The output file is ok, but when parsing it, the "regular" parses
        # loses letter case.
        layout = BeautifulSoup(dfxp, features='xml').findChild('layout')

        region = layout.findChild('region')
        text_align = region['tts:textAlign']
        display_align = region['tts:displayAlign']

        internal_alignment = _create_internal_alignment(
            text_align, display_align)

        assert len(layout.findChildren('region')) == 1
        assert internal_alignment.horizontal == HorizontalAlignmentEnum.LEFT
        assert internal_alignment.vertical == VerticalAlignmentEnum.TOP
コード例 #7
0
    def test_layout_is_relative(self):
        empty_layout = Layout()

        size_px = Size(30, UnitEnum.PIXEL)
        size_px2 = Size(30, UnitEnum.PIXEL)

        size_percent = Size(30, UnitEnum.PERCENT)
        size_percent2 = Size(30, UnitEnum.PERCENT)

        point_abs = Point(size_px, size_px2)
        point_rel = Point(size_percent, size_percent2)

        stretch_abs = Stretch(size_px, size_px2)
        stretch_rel = Stretch(size_percent, size_percent2)

        layout_abs = Layout(
            origin=point_abs,
            extent=stretch_abs,
            padding=None
        )

        layout_mix = Layout(
            origin=point_abs,
            extent=stretch_rel,
            padding=None
        )

        layout_rel = Layout(
            origin=point_rel,
            extent=stretch_rel,
            padding=None
        )

        assert empty_layout.is_relative()
        assert not layout_abs.is_relative()
        assert not layout_mix.is_relative()
        assert layout_rel.is_relative()
コード例 #8
0
ファイル: dfxp.py プロジェクト: jpenney/pycaption
    xmlns:tts="http://www.w3.org/ns/ttml#styling">
    <head>
        <styling/>
        <layout/>
    </head>
    <body/>
</tt>
'''

DFXP_DEFAULT_STYLE = {
    u'color': u'white',
    u'font-family': u'monospace',
    u'font-size': u'1c',
}

DFXP_DEFAULT_REGION = Layout(alignment=Alignment(
    HorizontalAlignmentEnum.CENTER, VerticalAlignmentEnum.BOTTOM))

DFXP_DEFAULT_STYLE_ID = u'default'
DFXP_DEFAULT_REGION_ID = u'bottom'


class DFXPReader(BaseReader):
    def __init__(self, *args, **kw):
        self.read_invalid_positioning = (kw.get('read_invalid_positioning',
                                                False))
        self.nodes = []

    def detect(self, content):
        if u'</tt>' in content.lower():
            return True
        else: