def handle_starttag(self, tag, attrs): span = model.Span(self.parent.get_doc()) self.parent.push_child(span) self.parent = span if tag.lower() in ("b", "bold"): span.set_style(styles.StyleProperties.FontWeight, styles.FontWeightType.bold) elif tag.lower() in ("i", "italic"): span.set_style(styles.StyleProperties.FontStyle, styles.FontStyleType.italic) elif tag.lower() in ("u", "underline"): span.set_style(styles.StyleProperties.TextDecoration, styles.TextDecorationType(underline=True)) elif tag.lower() == "font": for attr in attrs: if attr[0] == "color": color = parse_color(attr[1]) break else: LOGGER.warning("Font tag without a color attribute at line %s", self.line_num) return if color is None: LOGGER.warning("Unknown color %s at line %s", attrs["color"], self.line_num) return span.set_style(styles.StyleProperties.Color, color) else: LOGGER.warning("Unknown tag %s at line %s", tag, self.line_num) return
def extract( cls, context: StyleParsingContext, xml_attrib: str) -> typing.Union[str, styles.TextOutlineType]: if xml_attrib == "none": return styles.SpecialValues.none shadows = [] for shadow in xml_attrib.split(","): cs = shadow.split(" ") if len(cs) < 1 or len(cs) > 4: raise ValueError("Invalid Syntax") x_offset = StyleProperties.ttml_length_to_model(context, cs[0]) y_offset = StyleProperties.ttml_length_to_model(context, cs[1]) blur_radius = None color = None if len(cs) == 3: try: blur_radius = StyleProperties.ttml_length_to_model( context, cs[2]) except ValueError: color = utils.parse_color(cs[2]) else: # len(cs) == 4 blur_radius = StyleProperties.ttml_length_to_model( context, cs[2]) color = utils.parse_color(cs[3]) shadows.append( styles.TextShadowType.Shadow(x_offset=x_offset, y_offset=y_offset, blur_radius=blur_radius, color=color)) return styles.TextShadowType(shadows)
def extract(cls, context: StyleParsingContext, xml_attrib: str): style = None style_style = None style_symbol = None color = None position = None for c in xml_attrib.split(" "): if c == "none": return styles.SpecialValues.none if c == "auto": style = styles.TextEmphasisType.Style.auto elif c in ("filled", "open"): style_style = c elif c in ("circle", "dot", "sesame"): style_symbol = c elif c in styles.TextEmphasisType.Position.__members__: position = styles.TextEmphasisType.Position[c] elif c == "current": color = None else: color = utils.parse_color(c) if style_style is None and style_symbol is None: style = styles.TextEmphasisType.Style.auto else: style_style = style_style if style_style is not None else "circle" style_symbol = style_symbol if style_symbol is not None else "filled" style = styles.TextEmphasisType.Style(f"{style_style} {style_symbol}") position = position if position is not None else styles.TextEmphasisType.Position.outside return styles.TextEmphasisType( style=style, color=color, position=position )
def extract( cls, context: StyleParsingContext, xml_attrib: str) -> typing.Union[str, styles.TextOutlineType]: if xml_attrib == "none": return styles.SpecialValues.none s = xml_attrib.split(" ") if len(s) == 0 or len(s) > 2: raise ValueError("Bad syntax") thickness = StyleProperties.ttml_length_to_model(context, s[-1]) color = utils.parse_color(s[0]) if len(s) == 2 else None return styles.TextOutlineType(color=color, thickness=thickness)
def extract(cls, context: StyleParsingContext, xml_attrib: str): style = None symbol = None color = None position = None for c in xml_attrib.split(" "): if c in styles.TextEmphasisType.Style.__members__: style = styles.TextEmphasisType.Style[c] elif c in styles.TextEmphasisType.Symbol.__members__: symbol = styles.TextEmphasisType.Symbol[c] elif c in styles.TextEmphasisType.Position.__members__: position = styles.TextEmphasisType.Position[c] elif c == "current": color = None else: color = utils.parse_color(c) if style is None and symbol is None: style = styles.TextEmphasisType.Style.auto else: symbol = symbol or styles.TextEmphasisType.Symbol.circle style = style or styles.TextEmphasisType.Style.filled position = position or styles.TextEmphasisType.Position.outside return styles.TextEmphasisType(style=style, color=color, position=position, symbol=symbol)
def extract(cls, context: StyleParsingContext, xml_attrib: str): return utils.parse_color(xml_attrib)
def test_colors(self): for test in self.tests: with self.subTest(test[0]): c = parse_color(test[0]) self.assertEqual(c, test[1])
def test_bad_color(self): with self.assertRaises(ValueError): parse_color("#red")