Пример #1
0
def to_model(scc_content: str):
  """Converts a SCC document to the data model"""

  context = _SccContext()
  document = ContentDocument()

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

  context.set_safe_area(int((root_cell_resolution.columns - 32) / 2), int((root_cell_resolution.rows - 15) / 2))

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

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

  time_code = None
  for line in scc_content.splitlines():
    LOGGER.debug(line)
    scc_line = SccLine.from_str(line)

    if scc_line is None:
      continue

    time_code = scc_line.to_model(context)

  context.flush(time_code)

  return document
Пример #2
0
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
Пример #3
0
    def test_matching_region(self):
        doc = ContentDocument()
        doc_columns = 40
        doc_rows = 19
        doc.set_cell_resolution(
            CellResolutionType(rows=doc_rows, columns=doc_columns))

        safe_area_x_offset = 4
        safe_area_y_offset = 2

        caption_paragraph = SccCaptionParagraph(safe_area_x_offset,
                                                safe_area_y_offset)
        caption_paragraph.set_cursor_at(4, 7)
        caption_paragraph.new_caption_text()

        caption_paragraph.get_current_text().append("A 20-char long line.")

        origin = caption_paragraph.get_origin()
        self.assertEqual(11, origin.x.value)
        self.assertEqual(5, origin.y.value)

        extent = caption_paragraph.get_extent()
        self.assertEqual(20, extent.width.value)
        self.assertEqual(1, extent.height.value)

        paragraph_region = _SccParagraphRegion(caption_paragraph, doc)

        self.assertIsNone(paragraph_region._find_matching_region())
        region = paragraph_region._create_matching_region()

        self.assertEqual("region1", region.get_id())
        self.assertTrue(paragraph_region._has_same_origin_as_region(region))
        self.assertEqual(region, paragraph_region._find_matching_region())

        self.assertEqual(ShowBackgroundType.whenActive,
                         region.get_style(StyleProperties.ShowBackground))

        region_extent = region.get_style(StyleProperties.Extent)

        self.assertEqual(50, region_extent.width.value)
        self.assertEqual(63, region_extent.height.value)

        caption_paragraph.set_cursor_at(5, 7)
        caption_paragraph.new_caption_text()
        caption_paragraph.get_current_text().append(
            "This is another 34-char long line.")

        origin = caption_paragraph.get_origin()
        self.assertEqual(11, origin.x.value)
        self.assertEqual(5, origin.y.value)

        extent = caption_paragraph.get_extent()
        self.assertEqual(34, extent.width.value)
        self.assertEqual(2, extent.height.value)

        paragraph_region = _SccParagraphRegion(caption_paragraph, doc)

        self.assertEqual(region, paragraph_region._find_matching_region())
        paragraph_region._extend_region_to_paragraph(region)
        self.assertTrue(paragraph_region._has_same_origin_as_region(region))

        region_extent = region.get_style(StyleProperties.Extent)

        self.assertEqual(62, region_extent.width.value)
        self.assertEqual(63, region_extent.height.value)