def test_parse_attributes(): result = parse_text("This line is ~emph{nice}.\nSecond line") assert result == [("text", "This line is "), ("begin", "emph"), ("text", "nice"), ("end", None), ("text", "."), ("newline", 1), ("text", "Second line")] result = parse_text("This line is ^emph|nice|.\nSecond line", '^', '|', '|') assert result == [("text", "This line is "), ("begin", "emph"), ("text", "nice"), ("end", None), ("text", "."), ("newline", 1), ("text", "Second line")]
def test_svg_text(): r = RendererSVG() r.begin(300, 300) style = TextStyle() style.font = "Ubuntu" style.line_spacing = 1.2 sizes = (10, 20, 30) colors = ("red", "green", "#0000FF") emph = TextStyle() emph.color = "orange" tt = TextStyle() tt.font = "Ubuntu Mono" styles = { "emph": emph, "tt": tt } text = parse_text("This ~emph{is\nWide}~tt{ Window~emph{!}}") for i, (s, c) in enumerate(zip(sizes, colors)): st = style.copy() st.size = s st.color = c r.draw_text(5, 70 * i + 20, text, st, styles) r.end() r.write(utils.output_name("text.svg"))
def test_parse_lines(): result = parse_text("ABC XYZ\nX\n\nY\n") assert result == [("text", "ABC XYZ"), ("newline", 1), ("text", "X"), ("newline", 2), ("text", "Y")]
def test_svg_text(): r = RendererSVG() r.begin(300, 300) style = TextStyle() style.font = "Ubuntu" style.line_spacing = 1.2 sizes = (10, 20, 30) colors = ("red", "green", "#0000FF") emph = TextStyle() emph.color = "orange" tt = TextStyle() tt.font = "Ubuntu Mono" styles = {"emph": emph, "tt": tt} text = parse_text("This ~emph{is\nWide}~tt{ Window~emph{!}}") for i, (s, c) in enumerate(zip(sizes, colors)): st = style.copy() st.size = s st.color = c r.draw_text(5, 70 * i + 20, text, st, styles) r.end() r.write(utils.output_name("text.svg"))
def _get_text_size(self, ctx, text, role): if isinstance(text, str): text = parse_text(text) style = self._get_text_style(ctx, role) key = ctx.renderer.get_text_size_query_key(style, ctx.text_styles, text) width, height = ctx.get_query(key) offset_x, offset_y = self.get_text_offset(style) return width + offset_x * 2, height + offset_y * 2
def draw_text(self, ctx, x, y, text, style): if isinstance(text, str): text = parse_text(text) offset_x, offset_y = self.get_text_offset(style) if style.align == "left": x += offset_x elif style.align == "right": x -= offset_x ctx.renderer.draw_text(x, y + style.size + offset_y, text, style, ctx.text_styles)
def test_parser_simple(): result = parse_text("ABC") assert result == [("text", "ABC")]
def _get_text_size_query(self, ctx, text, role): if isinstance(text, str): text = parse_text(text) style = self._get_text_style(ctx, role) return ctx.renderer.get_text_size_query(style, ctx.text_styles, text)
def __init__(self, text, show): text = text.strip() super().__init__(show, text) self.content = parse_text(text) self.show = show self.size_cache = None
def __init__(self, text, role, show): text = text.strip() super().__init__(show, text) self.content = parse_text(text) self.role = role self.show = show