コード例 #1
0
ファイル: paragraph.py プロジェクト: RufaelDev/ttconv
  def to_paragraph(self, doc: ContentDocument) -> P:
    """Converts and returns current caption paragraph into P instance"""

    # Set up a new paragraph
    p = P()
    p.set_doc(doc)
    p.set_id(self._caption_id)

    if self._begin is not None:
      p.set_begin(self._begin.to_temporal_offset())
    if self._end is not None:
      p.set_end(self._end.to_temporal_offset())

    # Set the region to current caption
    region = _SccParagraphRegion(self, doc)
    p.set_region(region.get_region())

    # Set the paragraph style
    for (prop, value) in self.get_style_properties().items():
      p.set_style(prop, value)

    # Add caption content (text and line-breaks)
    for caption_content in self._caption_contents:

      if isinstance(caption_content, SccCaptionLineBreak):
        p.push_child(Br(doc))
        continue

      if isinstance(caption_content, SccCaptionText):
        span = Span(doc)

        if caption_content.get_begin() is not None:

          begin = caption_content.get_begin().to_temporal_offset()

          if self.get_caption_style() is SccCaptionStyle.PaintOn:
            # Compute paragraph-relative begin time
            begin -= self._begin.to_temporal_offset()

          span.set_begin(begin)

        if caption_content.get_end() is not None:

          end = caption_content.get_end().to_temporal_offset()

          if self.get_caption_style() is SccCaptionStyle.PaintOn:
            # Compute paragraph-relative end time
            end -= self._end.to_temporal_offset()

          span.set_end(end)

        for (prop, value) in caption_content.get_style_properties().items():
          span.set_style(prop, value)

        span.push_child(Text(doc, caption_content.get_text()))

        p.push_child(span)

    return p
コード例 #2
0
ファイル: paragraph.py プロジェクト: sandflow/ttconv
    def to_paragraph(self, doc: ContentDocument) -> P:
        """Converts and returns current caption paragraph into P instance"""

        # Set up a new paragraph
        p = P()
        p.set_doc(doc)
        p.set_id(self._caption_id)

        if self._begin is not None:
            p.set_begin(self._begin.to_temporal_offset())
        if self._end is not None:
            p.set_end(self._end.to_temporal_offset())

        # Set the region to current caption
        region = _SccParagraphRegion(self, doc)
        p.set_region(region.get_region())

        # Set the paragraph style
        for (prop, value) in self.get_style_properties().items():
            p.set_style(prop, value)

        # Add caption content (text and line-breaks)
        last_row: Optional[int] = None
        for row, caption_line in sorted(self._caption_lines.items()):

            if last_row is not None:
                for _ in range(0, abs(last_row - row)):
                    p.push_child(Br(doc))

            last_row = row

            for caption_text in caption_line.get_texts():

                # Skip empty texts
                if caption_text.is_empty():
                    continue

                span = Span(doc)

                if caption_text.get_begin() is not None:

                    begin = caption_text.get_begin().to_temporal_offset()

                    if self.get_caption_style() is SccCaptionStyle.PaintOn:
                        # Compute paragraph-relative begin time
                        begin -= self._begin.to_temporal_offset()

                    span.set_begin(begin)

                if caption_text.get_end() is not None:

                    end = caption_text.get_end().to_temporal_offset()

                    if self.get_caption_style() is SccCaptionStyle.PaintOn:
                        # Compute paragraph-relative end time
                        end -= self._end.to_temporal_offset()

                    span.set_end(end)

                for (prop,
                     value) in caption_text.get_style_properties().items():
                    span.set_style(prop, value)

                if StyleProperties.BackgroundColor not in caption_text.get_style_properties(
                ):
                    span.set_style(StyleProperties.BackgroundColor,
                                   NamedColors.black.value)

                span.push_child(Text(doc, caption_text.get_text()))

                p.push_child(span)

        return p