예제 #1
0
    def test_offsets_and_position(self):
        caption_text = SccCaptionText()

        caption_text.set_x_offset(None)
        caption_text.set_y_offset(None)

        expected = PositionType(LengthType(value=0, units=LengthType.Units.c),
                                LengthType(value=0, units=LengthType.Units.c))
        self.assertEqual(expected, caption_text.get_position())

        caption_text.set_x_offset(12)
        caption_text.set_y_offset(8)

        expected = PositionType(LengthType(value=12, units=LengthType.Units.c),
                                LengthType(value=8, units=LengthType.Units.c))
        self.assertEqual(expected, caption_text.get_position())

        other_caption_text = SccCaptionText()
        other_caption_text.set_x_offset(12)
        other_caption_text.set_y_offset(8)

        self.assertTrue(caption_text.has_same_origin(other_caption_text))
        self.assertFalse(caption_text.is_contiguous(other_caption_text))

        caption_text.set_y_offset(9)
        self.assertFalse(caption_text.has_same_origin(other_caption_text))
        self.assertTrue(caption_text.is_contiguous(other_caption_text))
예제 #2
0
def get_extent_from_dimensions(width: Union[int, Number],
                               height: Union[int, Number],
                               units=LengthType.Units.c) -> ExtentType:
    """Converts dimensions into extent"""
    height = LengthType(value=height, units=units)
    width = LengthType(value=width, units=units)

    return ExtentType(height, width)
예제 #3
0
def get_position_from_offsets(x_offset: int,
                              y_offset: int,
                              units=LengthType.Units.c) -> CoordinateType:
    """Converts offsets into position"""
    x_position = LengthType(value=x_offset, units=units)
    y_position = LengthType(value=y_offset, units=units)

    return CoordinateType(x_position, y_position)
예제 #4
0
 def check_element_extent(self,
                          elem: ContentElement,
                          width: Union[int, float, Number],
                          height: Union[int, float, Number],
                          unit=LengthType.Units.c):
     expected_extent = ExtentType(width=LengthType(value=width, units=unit),
                                  height=LengthType(value=height,
                                                    units=unit))
     self.check_element_style(elem, StyleProperties.Extent, expected_extent)
예제 #5
0
 def check_element_origin(self,
                          elem: ContentElement,
                          expected_x_origin: Union[int, float, Number],
                          expected_y_origin: Union[int, float, Number],
                          unit=LengthType.Units.c):
     expected_origin = CoordinateType(x=LengthType(value=expected_x_origin,
                                                   units=unit),
                                      y=LengthType(value=expected_y_origin,
                                                   units=unit))
     self.check_element_style(elem, StyleProperties.Origin, expected_origin)
예제 #6
0
파일: reader.py 프로젝트: sandflow/ttconv
def to_model(scc_content: str,
             config: Optional[SccReaderConfiguration] = None,
             progress_callback=lambda _: None):
    """Converts a SCC document to the data model"""

    context = _SccContext(config)
    document = ContentDocument()

    # Safe area must be a 32x15 grid, that represents 80% of the root area
    root_cell_resolution = CellResolutionType(
        rows=SCC_ROOT_CELL_RESOLUTION_ROWS,
        columns=SCC_ROOT_CELL_RESOLUTION_COLUMNS)
    document.set_cell_resolution(root_cell_resolution)

    context.set_safe_area(
        int((root_cell_resolution.columns -
             SCC_SAFE_AREA_CELL_RESOLUTION_COLUMNS) / 2),
        int((root_cell_resolution.rows - SCC_SAFE_AREA_CELL_RESOLUTION_ROWS) /
            2))

    # The active area is equivalent to the safe area
    active_area = ActiveAreaType(
        left_offset=context.safe_area_x_offset / root_cell_resolution.columns,
        top_offset=context.safe_area_y_offset / root_cell_resolution.rows,
        width=(root_cell_resolution.columns -
               (context.safe_area_x_offset * 2)) /
        root_cell_resolution.columns,
        height=(root_cell_resolution.rows -
                (context.safe_area_y_offset * 2)) / root_cell_resolution.rows,
    )
    document.set_active_area(active_area)

    body = Body()
    body.set_doc(document)
    document.set_body(body)

    # the default value of LineHeight ("normal") typically translates to 125% of the font size, which causes regions to overflow.
    body.set_style(StyleProperties.LineHeight,
                   LengthType(value=100, units=LengthType.Units.pct))

    # use a more readable font than the default Courier
    body.set_style(StyleProperties.FontFamily,
                   ("Consolas", "Monaco", GenericFontFamilyType.monospace))

    # add line padding
    body.set_style(StyleProperties.LinePadding,
                   LengthType(value=0.25, units=LengthType.Units.c))

    context.div = Div()
    context.div.set_doc(document)
    body.push_child(context.div)

    lines = scc_content.splitlines()
    nb_lines = len(lines)

    for (index, line) in enumerate(lines):
        LOGGER.debug(line)
        scc_line = SccLine.from_str(line)

        progress_callback((index + 1) / nb_lines)

        if scc_line is None:
            continue

        context.process_line(scc_line)

    context.flush()

    return document
예제 #7
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))