How to use the pycaption.geometry.UnitEnum function in pycaption

To help you get started, we’ve selected a few pycaption examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pbs / pycaption / tests / test_dfxp.py View on Github external
def test_individual_layouts_of_captions_with_matching_timespec_are_kept(self):  # noqa
        captionset = DFXPReader().read(
            SAMPLE_DFXP_MULTIPLE_CAPTIONS_WITH_THE_SAME_TIMING
        )
        expected_layouts = [
            (((10, UnitEnum.PERCENT), (10, UnitEnum.PERCENT)), None, None, (HorizontalAlignmentEnum.CENTER, VerticalAlignmentEnum.BOTTOM)),
            (((40, UnitEnum.PERCENT), (40, UnitEnum.PERCENT)), None, None, (HorizontalAlignmentEnum.CENTER, VerticalAlignmentEnum.BOTTOM)),
            (((10, UnitEnum.PERCENT), (70, UnitEnum.PERCENT)), None, None, (HorizontalAlignmentEnum.CENTER, VerticalAlignmentEnum.BOTTOM))]
        actual_layouts = [c_.layout_info.serialized() for c_ in
                          captionset.get_captions('en-US')]

        self.assertEqual(expected_layouts, actual_layouts)
github pbs / pycaption / tests / test_geometry.py View on Github external
def test_stretch_is_relative(self):
        size_px = Size(30, UnitEnum.PIXEL)
        size_px2 = Size(30, UnitEnum.PIXEL)

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

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

        self.assertFalse(stretch_abs.is_relative())
        self.assertFalse(stretch_mix.is_relative())
        self.assertTrue(stretch_rel.is_relative())
github pbs / pycaption / pycaption / geometry.py View on Github external
def from_string(cls, string):
        """Given a string of the form "46px" or "5%" etc., returns the proper
        size object

        :param string: a number concatenated to any of the UnitEnum members.
        :type string: unicode
        :rtype: Size
        """

        raw_number = string
        for unit in list(UnitEnum):
            if raw_number.endswith(unit.value):
                raw_number = raw_number.rstrip(unit.value)
                break
        else:
            unit = None

        if unit is not None:
            value = None
            try:
                value = float(raw_number)
                value = int(raw_number)
            except ValueError:
                pass

            if value is None:
                raise ValueError(
github pbs / pycaption / pycaption / geometry.py View on Github external
"""
        value = self.value
        unit = self.unit

        if unit == UnitEnum.PERCENT:
            return self  # Nothing to do here

        # The input must be valid so that any conversion can be done
        if not (video_width or video_height):
            raise RelativizationError(
                "Either video width or height must be given as a reference")
        elif video_width and video_height:
            raise RelativizationError(
                "Only video width or height can be given as reference")

        if unit == UnitEnum.EM:
            # TODO: Implement proper conversion of em in function of font-size
            # The em unit is relative to the font-size, to which we currently
            # have no access. As a workaround, we presume the font-size is 16px,
            # which is a common default value but not guaranteed.
            value *= 16
            unit = UnitEnum.PIXEL

        if unit == UnitEnum.PT:
            # XXX: we will convert first to "px" and from "px" this will be
            # converted to percent. we don't take into consideration the
            # font-size
            value = value / 72.0 * 96.0
            unit = UnitEnum.PIXEL

        if unit == UnitEnum.PIXEL:
            value = value * 100.0 / (video_width or video_height)
github pbs / pycaption / pycaption / geometry.py View on Github external
def __init__(self, value, unit):
        """
        :param value: A number (float or int will do)
        :param unit: A UnitEnum member
        """
        if value is None:
            raise ValueError("Size must be initialized with a value.")
        if not isinstance(unit,UnitEnum):
            raise ValueError("Size must be initialized with a valid unit.")

        self.value = float(value)
        self.unit = unit
github pbs / pycaption / pycaption / geometry.py View on Github external
# which is a common default value but not guaranteed.
            value *= 16
            unit = UnitEnum.PIXEL

        if unit == UnitEnum.PT:
            # XXX: we will convert first to "px" and from "px" this will be
            # converted to percent. we don't take into consideration the
            # font-size
            value = value / 72.0 * 96.0
            unit = UnitEnum.PIXEL

        if unit == UnitEnum.PIXEL:
            value = value * 100.0 / (video_width or video_height)
            unit = UnitEnum.PERCENT

        if unit == UnitEnum.CELL:
            # TODO: Implement proper cell resolution
            # (w3.org/TR/ttaf1-dfxp/#parameter-attribute-cellResolution)
            # For now we will use the default values (32 columns and 15 rows)
            cell_reference = 32 if video_width else 15
            value = value * 100.0 / cell_reference
            unit = UnitEnum.PERCENT

        return Size(value, unit)
github pbs / pycaption / pycaption / dfxp / base.py View on Github external
the region, its styles, or can be inherited from the root <tt> element.
        For the latter case, it must be specified in the unit 'pixel'.

        :rtype: Stretch
        """
        extent = None

        # Does the root 'tt' element have it?
        if extent is None:
            root = self.root_element
            extent = _get_object_from_attribute(
                root, 'tts:extent', Stretch.from_xml_attribute
            )

            if extent is not None:
                if not extent.is_measured_in(UnitEnum.PIXEL):
                    raise CaptionReadSyntaxError(
                        "The base <tt> element attribute 'tts:extent' should "
                        "only be specified in pixels. Check the docs: "
                        "http://www.w3.org/TR/ttaf1-dfxp/"
                        "#style-attribute-extent"
                    )
        return extent
</tt></tt>
github pbs / pycaption / pycaption / scc / specialized_collections.py View on Github external
"""Create a Layout object from the positioning information given

    The row can have a value from 1 to 15 inclusive. (vertical positioning)
    The column can have a value from 0 to 31 inclusive. (horizontal)

    :param position_tuple: a tuple of ints (row, col)
    :type position_tuple: tuple
    :rtype: Layout
    """
    if not position_tuple:
        return None

    row, column = position_tuple

    horizontal = Size(100 * column / 32.0, UnitEnum.PERCENT)
    vertical = Size(100 * (row - 1) / 15.0, UnitEnum.PERCENT)
    return Layout(origin=Point(horizontal, vertical),
                  alignment=Alignment(HorizontalAlignmentEnum.LEFT,
                                      VerticalAlignmentEnum.TOP)
                  )
github pbs / pycaption / pycaption / geometry.py View on Github external
diff_horizontal = Size(100 - self.origin.x.value, UnitEnum.PERCENT)
            diff_vertical = Size(100 - self.origin.y.value, UnitEnum.PERCENT)
            if not self.extent:
                # Extent is not set, use the calculated values
                new_extent = Stretch(diff_horizontal, diff_vertical)
            else:
                # Extent is set but may have inconsistent values,
                # e.g. origin="35% 25%" extent="80% 80%", which would cause
                # captions to end horizontally at 115% and vertically at 105%,
                # which would result in them being cut out of the screen.
                # In this case, the horizontal and vertical values are
                # corrected so that origin + extent = 100%.
                bottom_right = self.origin.add_stretch(self.extent)

                found_absolute_unit = False
                if bottom_right.x.unit != UnitEnum.PERCENT:
                    found_absolute_unit = True
                elif bottom_right.x.unit != UnitEnum.PERCENT:
                    found_absolute_unit = True

                if found_absolute_unit:
                    raise ValueError("Units must be relativized before extent "
                                     "can be calculated based on origin.")

                new_horizontal = self.extent.horizontal
                new_vertical = self.extent.vertical
                # If extent is set but it's inconsistent, replace with
                # calculated values
                if bottom_right.x.value > 100:
                    new_horizontal = diff_horizontal
                if bottom_right.y.value > 100:
                    new_vertical = diff_vertical
github pbs / pycaption / pycaption / geometry.py View on Github external
except ValueError:
                pass

            if value is None:
                raise ValueError(
                    """Couldn't recognize the value "{value}" as a number"""
                    .format(value=raw_number)
                )
            instance = cls(value, unit)
            return instance
        else:
            raise ValueError(
                "The specified value is not valid because its unit "
                "is not recognized: {value}. "
                "The only supported units are: {supported}"
                .format(value=raw_number, supported=', '.join(UnitEnum._member_map_))
            )