def test_style_property_text_outline_has_px(self): prop = styles.SpecialValues.none self.assertEqual(imsc_styles.StyleProperties.TextOutline.has_px(prop), False) prop = styles.TextOutlineType(color=styles.NamedColors.red.value, thickness=styles.LengthType( value=5, units=styles.LengthType.Units.em)) self.assertEqual(imsc_styles.StyleProperties.TextOutline.has_px(prop), False) prop = styles.TextOutlineType(color=styles.NamedColors.red.value, thickness=styles.LengthType( value=5, units=styles.LengthType.Units.px)) self.assertEqual(imsc_styles.StyleProperties.TextOutline.has_px(prop), True)
def test_tts_text_outline(self): to1 = styles.SpecialValues.none self.assertEqual( _get_set_style(imsc_styles.StyleProperties.TextOutline, to1), "none") to2 = styles.TextOutlineType(color=styles.NamedColors.red.value, thickness=styles.LengthType(value=5)) self.assertEqual( _get_set_style(imsc_styles.StyleProperties.TextOutline, to2), "#ff0000 5%")
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 to_model(data_file: typing.IO, _config = None, progress_callback=lambda _: None): """Converts an SRT document to the data model""" doc = model.ContentDocument() region = model.Region(_DEFAULT_REGION_ID, doc) region.set_style( styles.StyleProperties.Origin, styles.CoordinateType( x=styles.LengthType(5, styles.LengthType.Units.pct), y=styles.LengthType(5, styles.LengthType.Units.pct) ) ) region.set_style( styles.StyleProperties.Extent, styles.ExtentType( height=styles.LengthType(90, styles.LengthType.Units.pct), width=styles.LengthType(90, styles.LengthType.Units.pct) ) ) region.set_style( styles.StyleProperties.DisplayAlign, styles.DisplayAlignType.after ) region.set_style( styles.StyleProperties.TextAlign, styles.TextAlignType.center ) region.set_style( styles.StyleProperties.LineHeight, _DEFAULT_LINE_HEIGHT ) region.set_style( styles.StyleProperties.FontFamily, _DEFAULT_FONT_STACK ) region.set_style( styles.StyleProperties.FontSize, _DEFAULT_FONT_SIZE ) region.set_style( styles.StyleProperties.Color, _DEFAULT_TEXT_COLOR ) region.set_style( styles.StyleProperties.TextOutline, styles.TextOutlineType( _DEFAULT_OUTLINE_THICKNESS, _DEFAULT_OUTLINE_COLOR ) ) doc.put_region(region) body = model.Body(doc) body.set_region(region) doc.set_body(body) div = model.Div(doc) body.push_child(div) lines : str = data_file.readlines() state = _State.COUNTER current_p = None for line_index, line in enumerate(_none_terminated(lines)): if state is _State.COUNTER: if line is None: break if _EMPTY_RE.fullmatch(line): continue if _COUNTER_RE.search(line) is None: LOGGER.fatal("Missing subtitle counter at line %s", line_index) return None progress_callback(line_index/len(lines)) state = _State.TC continue if state is _State.TC: if line is None: break m = _TIMECODE_RE.search(line) if m is None: LOGGER.fatal("Missing timecode at line %s", line_index) return None current_p = model.P(doc) current_p.set_begin( int(m.group('begin_h')) * 3600 + int(m.group('begin_m')) * 60 + int(m.group('begin_s')) + int(m.group('begin_ms')) / 1000 ) current_p.set_end( int(m.group('end_h')) * 3600 + int(m.group('end_m')) * 60 + int(m.group('end_s')) + int(m.group('end_ms')) / 1000 ) state = _State.TEXT continue if state in (_State.TEXT, _State.TEXT_MORE): if line is None or _EMPTY_RE.fullmatch(line): subtitle_text = subtitle_text.strip('\r\n')\ .replace(r"\n\r", "\n")\ .replace(r"{bold}", r"<bold>")\ .replace(r"{/bold}", r"</bold>")\ .replace(r"{italic}", r"<italic>")\ .replace(r"{/italic}", r"</italic>")\ .replace(r"{underline}", r"<underline>")\ .replace(r"{/underline}", r"</underline>") parser = _TextParser(current_p, line_index) parser.feed(subtitle_text) parser.close() state = _State.COUNTER continue if state is _State.TEXT: div.push_child(current_p) subtitle_text = "" if state is _State.TEXT_MORE: current_p.push_child(model.Br(current_p.get_doc())) subtitle_text += line state = _State.TEXT_MORE continue return doc