def build_line(align): big0 = tl.Text(width=3, height=3) small = tl.Text(width=1, height=1, valign=align, renderer=Rect('CELL')) big1 = tl.Text(width=3, height=3) line = tl.HCellGroup([big0, small, big1]) line.place(0, 0) return line
def str2cells(s: str, content=3, space=0.5): # t ... text cell # f ... fraction cell # space is space # ~ ... non breaking space (nbsp) # # ... tabulator for c in s.lower(): if c == "t": yield tl.Text(width=content, height=1, renderer=Rect("Text")) elif c == "f": cell = tl.Text(content / 2, 1) yield tl.Fraction( top=cell, bottom=cell, stacking=tl.Stacking.SLANTED, renderer=Rect("Fraction"), ) elif c == " ": yield tl.Space(width=space) elif c == "~": yield tl.NonBreakingSpace(width=space) elif c == "#": yield tl.Tabulator(width=0) # Tabulators do not need a width else: raise ValueError(f'unknown cell type "{c}"')
def fraction(stacking, x, y): result = [] a = tl.Text(1, 1, renderer=Rect("A", result)) b = tl.Text(1, 1, renderer=Rect("B", result)) fr = tl.Fraction(a, b, stacking, renderer=Rect("Fraction", result)) fr.place(x, y) fr.render() return result
def test_line_total_width_is_defined_by_content(self): line = tl.LeftLine(10) line.append(tl.Text(1, 1)) assert line.total_width == 1 line.append(tl.Text(1, 1)) assert line.total_width == 2 line.append(tl.Text(1, 1)) assert line.total_width == 3
def test_line_height_is_defined_by_max_content_height(self): line = tl.LeftLine(10) line.append(tl.Text(1, 1)) assert line.total_height == 1 line.append(tl.Text(1, 2)) assert line.total_height == 2 line.append(tl.Text(1, 3)) assert line.total_height == 3
def build_line(align): line = tl.LeftLine(width=7) big0 = tl.Text(width=3, height=3) small = tl.Text(width=1, height=1, valign=align, renderer=Rect("CELL")) big1 = tl.Text(width=3, height=3) line.append(big0) line.append(small) line.append(big1) line.place(0, 0) return line
def test_mixed_alignment(self): big0 = tl.Text(width=3, height=3) bottom = tl.Text(width=1, height=1, valign=tl.CellAlignment.BOTTOM) center = tl.Text(width=1, height=1, valign=tl.CellAlignment.CENTER) top = tl.Text(width=1, height=1, valign=tl.CellAlignment.TOP) big1 = tl.Text(width=3, height=3) line = tl.HCellGroup([big0, top, center, bottom, big1]) line.place(0, 0) # final location is always the top/left corner of the cell: assert bottom.final_location() == (5, -2) assert center.final_location() == (4, -1) assert top.final_location() == (3, 0)
def test_mixed_alignment(self): big0 = tl.Text(width=3, height=3) bottom = tl.Text(width=1, height=1, valign=tl.CellAlignment.BOTTOM) center = tl.Text(width=1, height=1, valign=tl.CellAlignment.CENTER) top = tl.Text(width=1, height=1, valign=tl.CellAlignment.TOP) big1 = tl.Text(width=3, height=3) line = tl.LeftLine(width=9) for cell in [big0, top, center, bottom, big1]: line.append(cell) line.place(0, 0) assert bottom.final_location() == (5, -2) assert center.final_location() == (4, -1) assert top.final_location() == (3, 0)
def make_text(stroke, result): text = tl.Text(width=3, height=1, stroke=stroke, renderer=StrokeRender("STROKE", result)) text.place(0, 0) return text
def render_text(stroke, result): text = tl.Text(width=3, height=1, stroke=stroke, renderer=StrokeRender("STROKE", result)) text.place(0, 0) tl.render_text_strokes([text])
def word(self, text: str, ctx: MTextContext) -> tl.ContentCell: return tl.Text( # The first call to get_font() is very slow! width=self.get_font(ctx).text_width(text), height=ctx.cap_height, valign=tl.CellAlignment(ctx.align), renderer=self.renderer, )
def str2cells(s: str, content=3, space=0.5): # t ... text cell # f ... fraction cell # space is space # ~ ... non breaking space (nbsp) for c in s.lower(): if c == 't': yield tl.Text(width=content, height=1, renderer=Rect('Text')) elif c == 'f': cell = tl.Text(content / 2, 1) yield tl.Fraction(top=cell, bottom=cell, stacking=tl.Stacking.SLANTED, renderer=Rect('Fraction')) elif c == ' ': yield tl.Space(width=space) elif c == '~': yield tl.NonBreakingSpace(width=space) else: raise ValueError(f'unknown cell type "{c}"')
def word(self, text: str, ctx: MTextContext) -> tl.ContentCell: line_attribs = dict(self.current_base_attribs or {}) line_attribs.update(get_color_attribs(ctx)) text_attribs = dict(line_attribs) text_attribs.update(self.get_text_attribs(ctx)) return tl.Text( width=self.get_font(ctx).text_width(text), height=ctx.cap_height, valign=tl.CellAlignment(ctx.align), stroke=self.get_stroke(ctx), renderer=TextRenderer(text, text_attribs, line_attribs, self.layout), )
def word(self, text: str, ctx: MTextContext) -> tl.ContentCell: return tl.Text(width=self.get_font(ctx).text_width(text), height=ctx.cap_height, valign=tl.CellAlignment(ctx.align), stroke=self.get_stroke(ctx), renderer=TextRenderer( text, ctx.cap_height, ctx.width_factor, ctx.oblique, self.new_text_properties(self._properties, ctx), self._backend, ))
def test_fill_until_line_is_full(self): line = tl.LeftLine(10) assert line.append(tl.Text(5, 1)) == tl.AppendType.SUCCESS assert line.append(tl.Text(5, 1)) == tl.AppendType.SUCCESS assert line.append(tl.Text(5, 1)) == tl.AppendType.FAIL assert line.total_width <= line.line_width