def test_02(): "ParagraphStack" factory = Factory() texel = TextModel("123\n567\n").texel boxes = factory.create_boxes(texel) assert listtools.calc_length(boxes) == 8 paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert check_box(stack, texel) texel = TextModel("123\n\n5\n67\n").texel boxes = factory.create_boxes(texel) assert listtools.calc_length(boxes) == 10 paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert check_box(stack, texel) texel = TextModel("123\n").texel boxes = factory.create_boxes(texel) paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) par = stack.childs[-1] assert len(par) == 4 assert len(stack) == 4 if 0: for p in paragraphs: p.dump_boxes(0, 0, 0) assert stack.get_info(3, 0, 0)[-2:] == (3, 0) texel = TextModel("").texel boxes = factory.create_boxes(texel) paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs)
def test_01(): factory = Factory() boxes = factory.create_boxes(TextModel("123").texel) assert listtools.calc_length(boxes) == 3 boxes = factory.create_boxes(TextModel("123\n567").get_xtexel()) assert listtools.calc_length(boxes) == 8 paragraphs = create_paragraphs(boxes) assert len(paragraphs) == 2 assert len(paragraphs[0]) == 4 assert len(paragraphs[1]) == 4 assert listtools.calc_length(paragraphs) == 8
def create_paragraphs(textboxes, maxw=0, Paragraph=Paragraph, \ device=TESTDEVICE): r = [] l = [] for box in textboxes: l.append(box) if isinstance(box, NewlineBox) or isinstance(box, EndBox): if maxw > 0: rows = simple_linewrap(l, maxw, tabstops=(), device=device) else: rows = [Row(l, device)] r.append(Paragraph(rows, device)) l = [] assert not l # There is always one final box which is either a # NewLine or an EndBox. Therefore there is no rest in # l. assert listtools.calc_length(r) == listtools.calc_length(textboxes) return r
def create_boxes(self, texel, i1=None, i2=None): if i1 is None: assert i2 is None i1 = 0 i2 = len(texel) else: assert i1 <= i2 i1 = max(0, i1) i2 = min(len(texel), i2) if i1 == i2: return () name = texel.__class__.__name__ + '_handler' handler = getattr(self, name) boxes = handler(texel, i1, i2) if not i2 - i1 == listtools.calc_length(boxes): print i1, i2, texel print boxes assert i2 - i1 == listtools.calc_length(boxes) return tuple(boxes)
def _replaced(self, element, i1, i2, stuff): # replaces everything between $i1$ and $i2$ in $element$ by # $stuff$ grouped = self._grouped n = len(element) assert i2 >= i1 assert i2 <= len(element) if i2 > i1: rest = treebase.remove(element, i1, i2) element = grouped(rest) r = treebase.insert(element, i1, stuff) from textmodel.listtools import calc_length assert len(grouped(r)) == n - (i2 - i1) + calc_length(stuff) return grouped(r)
def create_boxes(self, texel, i1=None, i2=None): if i1 is None: assert i2 is None i1 = 0 i2 = len(texel) else: assert i1 <= i2 i1 = max(0, i1) i2 = min(len(texel), i2) if i1 == i2: return () name = texel.__class__.__name__ + '_handler' handler = getattr(self, name) boxes = handler(texel, i1, i2) if not i2 - i1 == listtools.calc_length(boxes): print i1, i2, texel k1 = 0 for box in boxes: k2 = k1 + len(box) print k1, k2, box.dump_boxes(k1, 0, 0) k1 = k2 assert i2 - i1 == listtools.calc_length(boxes) return tuple(boxes)
def test_02(): "ParagraphStack" factory = Factory() texel = TextModel("123\n567").texel boxes = factory.create_boxes(texel) assert listtools.calc_length(boxes) == 7 paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert check_box(stack, texel) texel = TextModel("123\n\n5\n67").texel boxes = factory.create_boxes(texel) assert listtools.calc_length(boxes) == 9 paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert check_box(stack, texel) assert stack.extra_height == 0 texel = TextModel("123\n").texel boxes = factory.create_boxes(texel) paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert stack.childs[-1].has_newline() assert stack.extra_height == 1.0 par = stack.childs[-1] assert len(par) == 4 assert len(stack) == 4 assert stack.get_info(3, 0, 0)[-2:] == (3, 0) assert stack.get_info(4, 0, 0)[-2:] == (0, 1) texel = TextModel("").texel boxes = factory.create_boxes(texel) paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert stack.extra_height > 0
def create_paragraphs(textboxes, maxw=0, Paragraph=Paragraph, device=TESTDEVICE): # Erzeugt eine Liste von Paragraphen r = [] l = [] for box in textboxes: l.append(box) if isinstance(box, NewlineBox): if maxw > 0: rows = simple_linewrap(l, maxw, device) else: rows = [Row(l, device)] r.append(Paragraph(rows, device)) l = [] if l: if maxw > 0: rows = simple_linewrap(l, maxw, device) else: rows = [Row(l, device)] r.append(Paragraph(rows, device)) assert listtools.calc_length(r) == listtools.calc_length(textboxes) return r
def __init__(self, childs, device=None): self.childs = tuple(childs) if device is not None: self.device = device self.length = listtools.calc_length(self.childs) self.layout()
def __init__(self, rows, device=None): length = listtools.calc_length(rows) self.weights = (0, length) self.childs = rows _ParagraphBox.__init__(self, device)