Exemplo n.º 1
0
    def from_model(model_doc: model.ContentDocument):

        layout_element = et.Element(LayoutElement.qn)

        for r in model_doc.iter_regions():
            region_element = RegionElement.from_model(r)
            if region_element is not None:
                layout_element.append(region_element)

        return layout_element
Exemplo n.º 2
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
Exemplo n.º 3
0
    def from_model(model_doc: model.ContentDocument) -> et.Element:

        tt_element = et.Element(TTElement.qn)

        imsc_attr.XMLLangAttribute.set(tt_element, model_doc.get_lang())

        if model_doc.get_cell_resolution() != model.CellResolutionType(
                rows=15, columns=32):
            imsc_attr.CellResolutionAttribute.set(
                tt_element, model_doc.get_cell_resolution())

        has_px = False

        all_elements = list(model_doc.iter_regions())

        if model_doc.get_body() is not None:
            all_elements.extend(model_doc.get_body().dfs_iterator())

        for element in all_elements:
            for model_style_prop in element.iter_styles():
                if StyleProperties.BY_MODEL_PROP[model_style_prop].has_px(
                        element.get_style(model_style_prop)):
                    has_px = True
                    break
            if has_px:
                break

        if model_doc.get_px_resolution() is not None and has_px:
            imsc_attr.ExtentAttribute.set(tt_element,
                                          model_doc.get_px_resolution())

        if model_doc.get_active_area() is not None:
            imsc_attr.ActiveAreaAttribute.set(tt_element,
                                              model_doc.get_active_area())

        # Write the <head> section first
        head_element = HeadElement.from_model(model_doc)

        if head_element is not None:
            tt_element.append(head_element)

        model_body = model_doc.get_body()

        if model_body is not None:

            body_element = BodyElement.from_model(model_body)

            if body_element is not None:
                tt_element.append(body_element)

        return tt_element
Exemplo n.º 4
0
  def from_model(
    ctx: TTMLElement.WritingContext,
    model_doc: model.ContentDocument,
  ) -> typing.Optional[et.Element]:
    '''Returns a TTML `layout` element (an XML element) using the information in the ContentDocument `model_doc`.
    `ctx` contains state information used in the process.
    '''

    layout_element = None
    
    for r in model_doc.iter_regions():
      region_element = RegionElement.from_model(ctx, r)
      if region_element is not None:
        if layout_element is None:
          layout_element = et.Element(LayoutElement.qn)
        layout_element.append(region_element)

    return layout_element
Exemplo n.º 5
0
  def from_model(
    model_doc: model.ContentDocument,
    frame_rate: typing.Optional[Fraction],
    time_expression_syntax: imsc_attr.TimeExpressionSyntaxEnum,
    progress_callback: typing.Callable[[numbers.Real], typing.NoReturn]
  ) -> et.Element:
    '''Converts the data model to an IMSC document contained in an ElementTree Element'''

    ctx = TTMLElement.WritingContext(frame_rate, time_expression_syntax)

    tt_element = et.Element(TTElement.qn)

    imsc_attr.XMLLangAttribute.set(tt_element, model_doc.get_lang())
    
    if model_doc.get_cell_resolution() != model.CellResolutionType(rows=15, columns=32):
      imsc_attr.CellResolutionAttribute.set(tt_element, model_doc.get_cell_resolution())

    has_px = False

    all_elements = list(model_doc.iter_regions())

    if model_doc.get_body() is not None:
      all_elements.extend(model_doc.get_body().dfs_iterator())

    for element in all_elements:
      for model_style_prop in element.iter_styles():
        if StyleProperties.BY_MODEL_PROP[model_style_prop].has_px(element.get_style(model_style_prop)):
          has_px = True
          break
      for animation_step in element.iter_animation_steps():
        if StyleProperties.BY_MODEL_PROP[animation_step.style_property].has_px(animation_step.value):
          has_px = True
          break      
      if has_px:
        break

    if model_doc.get_px_resolution() is not None and has_px:
      imsc_attr.ExtentAttribute.set(tt_element, model_doc.get_px_resolution())

    if model_doc.get_active_area() is not None:
      imsc_attr.ActiveAreaAttribute.set(tt_element, model_doc.get_active_area())

    if model_doc.get_display_aspect_ratio() is not None:
      imsc_attr.DisplayAspectRatioAttribute.set(tt_element, model_doc.get_display_aspect_ratio())

    if frame_rate is not None:
      imsc_attr.FrameRateAttribute.set(tt_element, frame_rate)

    # Write the <head> section first
    head_element = HeadElement.from_model(ctx, model_doc)

    progress_callback(0.5)

    if head_element is not None:
      tt_element.append(head_element)

    model_body = model_doc.get_body()

    if model_body is not None:

      body_element = BodyElement.from_model(ctx, model_body)

      if body_element is not None:
        tt_element.append(body_element)

    progress_callback(1.0)

    return tt_element