Ejemplo n.º 1
0
    def ordered_styles(self, dataset):
        """
        This function figures out the chain of styles.
        WARNING: Do not call this before the semantic validation of tt/head/styling is finished. Otherwise your style
        may not have been found yet!
        :param dataset: Semantic dataset
        :return: a list of styles applicable in order
        """

        if self._styling_lock.locked():
            raise SemanticValidationError(ERR_SEMANTIC_STYLE_CIRCLE.format(
                style=self.id
            ))

        with self._styling_lock:
            if self._ordered_styles is not None:
                return self._ordered_styles
            ordered_styles = [self]
            if self.style is not None:
                for style_id in self.style:
                    try:
                        style_elem = dataset['tt_element'].get_element_by_id(elem_id=style_id, elem_type=style_type)
                        cascading_styles = style_elem.ordered_styles(dataset=dataset)
                        for style_elem in cascading_styles:
                            if style_elem in ordered_styles:
                                continue
                            ordered_styles.append(style_elem)
                    except LookupError:
                        raise SemanticValidationError(ERR_SEMANTIC_STYLE_MISSING.format(
                            style=style_id
                        ))

            self._ordered_styles = ordered_styles
            return ordered_styles
Ejemplo n.º 2
0
    def _semantic_collect_applicable_styles(self, dataset, style_type):
        referenced_styles = []
        inherited_styles = []
        region_styles = []
        if self.style is not None:
            # Styles cascade
            for style_id in self.style:
                try:
                    style = dataset['tt_element'].get_element_by_id(elem_id=style_id, elem_type=style_type)
                    for style_binding in style.ordered_styles(dataset=dataset):
                        if style_binding not in referenced_styles:
                            referenced_styles.append(style_binding)
                except LookupError:
                    raise SemanticValidationError(ERR_SEMANTIC_STYLE_MISSING.format(style=style_id))

            # Push this validated set onto the stack for children to use

        for style_list in dataset['styles_stack']:
            # Traverse all the styles encountered at our parent elements
            for inh_style in style_list:
                if inh_style not in referenced_styles and inh_style not in inherited_styles:
                    inherited_styles.append(inh_style)

        region = dataset.get('region', None)
        self._inherited_region = region

        if region is not None:
            # At last apply any region styles we may find
            for region_style in region.validated_styles:
                if region_style not in referenced_styles and region_style not in inherited_styles:
                    region_styles.append(region_style)

        self._referenced_styles = referenced_styles
        self._validated_styles = referenced_styles + inherited_styles + region_styles
Ejemplo n.º 3
0
    def ordered_styles(self, dataset):
        """
        This function figures out the chain of styles.
        WARNING: Do not call this before the semantic validation of tt/head/styling is finished. Otherwise your style
        may not have been found yet!
        :param dataset: Semantic dataset
        :return: a list of styles applicable in order
        """

        if self._styling_lock.locked():
            raise SemanticValidationError(ERR_SEMANTIC_STYLE_CIRCLE.format(
                style=self.id
            ))

        with self._styling_lock:
            if self._ordered_styles is not None:
                return self._ordered_styles
            ordered_styles = [self]
            if self.style is not None:
                for style_id in self.style:
                    try:
                        style_elem = dataset['tt_element'].get_element_by_id(elem_id=style_id, elem_type=style_type)
                        cascading_styles = style_elem.ordered_styles(dataset=dataset)
                        for style_elem in cascading_styles:
                            if style_elem in ordered_styles:
                                continue
                            ordered_styles.append(style_elem)
                    except LookupError:
                        raise SemanticValidationError(ERR_SEMANTIC_STYLE_MISSING.format(
                            style=style_id
                        ))

            self._ordered_styles = ordered_styles
            return ordered_styles
    def _semantic_collect_applicable_styles(self, dataset, style_type):
        referenced_styles = []
        inherited_styles = []
        region_styles = []
        if self.style is not None:
            # Styles cascade
            for style_id in self.style:
                try:
                    style = dataset['tt_element'].get_element_by_id(
                        elem_id=style_id, elem_type=style_type)
                    for style_binding in style.ordered_styles(dataset=dataset):
                        if style_binding not in referenced_styles:
                            referenced_styles.append(style_binding)
                except LookupError:
                    raise SemanticValidationError(
                        ERR_SEMANTIC_STYLE_MISSING.format(style=style_id))

            # Push this validated set onto the stack for children to use

        for style_list in dataset['styles_stack']:
            # Traverse all the styles encountered at our parent elements
            for inh_style in style_list:
                if inh_style not in referenced_styles and inh_style not in inherited_styles:
                    inherited_styles.append(inh_style)

        region = dataset.get('region', None)
        self._inherited_region = region

        if region is not None:
            # At last apply any region styles we may find
            for region_style in region.validated_styles:
                if region_style not in referenced_styles and region_style not in inherited_styles:
                    region_styles.append(region_style)

        self._referenced_styles = referenced_styles
        self._validated_styles = referenced_styles + inherited_styles + region_styles
Ejemplo n.º 5
0
    def _semantic_collect_applicable_styles(self, dataset, style_type, parent_binding, defer_font_size=False,
                                            extra_referenced_styles=None):
        """
        This function identifies the styling dependency chain for the styled element in question.

        :param dataset: Semantic dataset
        :param style_type: the style_type to be used in the process (there are different style types for EBU-TT D and live).
        :param parent_binding: The immediate parent of the styled element in the document structure
        :param defer_font_size: If True then fontsize can stay percentage in case it could not be calculated
        :param extra_referenced_styles: Used by region to inject its extra style attributes

        :return:
        """

        self._specified_style = None
        self._computed_style = None
        self._parent_computed_style = None
        referenced_styles = []
        inherited_styles = []
        region_styles = []
        if extra_referenced_styles is None:
            extra_referenced_styles = []

        if self.style is not None:
            # Styles cascade
            for style_id in self.style:
                try:
                    style = dataset['tt_element'].get_element_by_id(elem_id=style_id, elem_type=style_type)
                    for style_binding in style.ordered_styles(dataset=dataset):
                        if style_binding not in referenced_styles:
                            referenced_styles.append(style_binding)
                except LookupError:
                    raise SemanticValidationError(ERR_SEMANTIC_STYLE_MISSING.format(style=style_id))

            # Push this validated set onto the stack for children to use

        for style_list in dataset['styles_stack']:
            # Traverse all the styles encountered at our parent elements
            for inh_style in style_list:
                if inh_style not in referenced_styles and inh_style not in inherited_styles:
                    inherited_styles.append(inh_style)

        region = dataset.get('region', None)
        self._inherited_region = region

        if region is not None:
            # At last apply any region styles we may find
            for region_style in region.validated_styles:
                if region_style not in referenced_styles and region_style not in inherited_styles:
                    region_styles.append(region_style)

        self._referenced_styles = referenced_styles
        self._inherited_styles = inherited_styles
        self._region_styles = region_styles
        self._validated_styles = referenced_styles + inherited_styles + region_styles

        if parent_binding is not None and hasattr(parent_binding, 'computed_style'):
            parent_computed_style = parent_binding.computed_style
        else:
            parent_computed_style = None

        if region is not None and hasattr(region, 'computed_style'):
            region_computed_style = region.computed_style
        else:
            region_computed_style = None

        # Let's resolve the specified styles
        # Make sure the extra style attributes supersede the rest of the referenced styles
        self._specified_style = self._compatible_style_type.resolve_styles(extra_referenced_styles + referenced_styles)

        # Let's generate the computed style of the element
        self._computed_style = self._compatible_style_type.compute_style(
            self._specified_style,
            parent_computed_style,
            region_computed_style,
            dataset,
            defer_font_size
        )