def test_do_not_continue_stroke_automatically(self): result = [] word = self.make_text(tl.Stroke.UNDERLINE, result) space = tl.Space(width=0.5) tl.render_text_strokes([word, space, word]) assert len(result) == 2 assert result[0] == "STROKE(UNDERLINE, 3.0)", "do not continue stroke"
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 random_sized_content(count: int) -> Content: """Create content with randomized text size.""" def size(): return random.choice([0, 1, 1, 1, 1, 1, 2, 3]) for word in tl.lorem_ipsum(count): font = fix_sized_fonts[size()] yield Word(word, font) yield tl.Space(font.space)
def test_continue_stroke_across_one_space(self): result = [] word = self.make_text(tl.Stroke.UNDERLINE + tl.Stroke.CONTINUE, result) space = tl.Space(width=0.5) tl.render_text_strokes([word, space, word]) assert len(result) == 2 assert result[ 0] == "STROKE(UNDERLINE, 3.5)", "space should be included" assert result[1] == "STROKE(UNDERLINE, 3.0)", "no following space"
def test_continue_stroke_across_multiple_spaces(self): result = [] word = self.make_text(tl.Stroke.UNDERLINE + tl.Stroke.CONTINUE, result) space = tl.Space(width=0.5) nbsp = tl.NonBreakingSpace(width=0.5) tl.render_text_strokes([word, space, nbsp, space, word]) assert len(result) == 2 assert (result[0] == "STROKE(UNDERLINE, 4.5)" ), "3 spaces should be included" assert result[1] == "STROKE(UNDERLINE, 3.0)", "no following spaces"
def stroked_content(count: int, size: int = 1) -> Content: """Create content with one text size and groups of words with or without strokes. """ font = fix_sized_fonts[size] groups = stroke_groups(tl.lorem_ipsum(count)) for group, stroke in groups: # strokes should span across spaces in between words: # Spaces between words are bound to the preceding content box renderer, # MText is more flexible, but this implementation is easy and good # enough, otherwise spaces would need a distinct height and a rendering # object, both are not implemented for glue objects. continue_stroke = stroke + 8 if stroke else 0 for word in group[:-1]: yield Word(word, font=font, stroke=continue_stroke) yield tl.Space(font.space) # strokes end at the last word, without continue stroke: yield Word(group[-1], font=font, stroke=stroke) yield tl.Space(font.space)
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 fraction_content() -> Content: """Create content with one text size and place random fractions between words. """ words = list(uniform_content(120)) for word in words: word.valign = tl.CellAlignment.BOTTOM stacking_options = list(tl.Stacking) font = SizedFont(0.25) # fraction font for _ in range(10): stacking = random.choice(stacking_options) top = str(random.randint(1, 1000)) bottom = str(random.randint(1, 1000)) pos = random.randint(0, len(words) - 1) if isinstance(words[pos], tl.Space): pos += 1 words.insert(pos, Fraction(top, bottom, stacking, font)) words.insert(pos + 1, tl.Space(font.space)) return words
def test_total_height_is_zero(self): assert tl.Space(1).total_height == 0
def test_expand_unrestricted_space(self): space = tl.Space(1) space.resize(1.5) assert space.total_width == 1.5 space.resize(30) assert space.total_width == 30
def test_expand_restricted_space(self): space = tl.Space(1, max_width=2) space.resize(1.5) assert space.total_width == 1.5 space.resize(3) assert space.total_width == 2
def test_default_min_width(self): space = tl.Space(1) space.resize(0.5) assert space.total_width == 1.0
def test_shrink_space(self): space = tl.Space(1, min_width=0.1) space.resize(0.5) assert space.total_width == 0.5 space.resize(0) assert space.total_width == 0.1
def space(self, ctx: MTextContext): return tl.Space(width=self.space_width(ctx))
def test_can_shrink(self): assert tl.Space(1).can_shrink is False assert tl.Space(1, min_width=0.5).can_shrink is True
def test_can_grow(self): assert tl.Space(1).can_grow is True assert tl.Space(1, max_width=1.0).can_grow is False
def uniform_content(count: int, size: int = 1) -> Content: """Create content with one text size.""" font = fix_sized_fonts[size] for word in tl.lorem_ipsum(count): yield Word(word, font) yield tl.Space(font.space)