- add trafoat method
[PyX/mjg.git] / test / experimental / textboxes.py
blob873a2304bd177e675f37bbb5f384ea11c5ac708c
1 import sys; sys.path[:0] = ["../.."]
2 import random, string
3 from pyx import *
5 text.set(texipc=1)
6 text.set(texdebug="debug.tex", usefiles=["debug.dvi", "debug.log"])
8 def randpar():
9 return " ".join(["".join([random.choice(string.lowercase)
10 for j in range(random.randint(1, 3))])
11 for i in range(random.randint(100, 300))])
13 def randparwithdisplay():
14 #par = "" # TODO allow for formulas at the beginning -- to be checked
15 par = randpar()
16 for i in range(random.randint(0, 3)):
17 par += r"\display{}xxx\enddisplay{}"
18 for j in range(random.randint(0, 1)):
19 par += randpar()
20 #return par # TODO allow for formulas at the end -- to be checked
21 return par + randpar()
23 def randtext():
24 return r"\par{}".join([randparwithdisplay() for i in range(random.randint(1, 10))])
26 def output(boxes, shapes):
27 c = canvas.canvas()
28 y = 0
29 for i in range(len(boxes)):
30 if i < len(shapes):
31 shape = shapes[i]
32 c.stroke(path.rect(0, y, shape[0], -shape[1]))
33 c.stroke(boxes[i].bbox().path(), [trafo.translate(0, y), color.rgb.red])
34 c.insert(boxes[i], [trafo.translate(0, y)])
35 for mark in boxes[i].markers.keys():
36 mx, my = boxes[i].markers[mark]
37 if mark[:5] == "start":
38 c.fill(path.circle(mx, my+y, 0.05), [color.rgb.red])
39 elif mark[:3] == "end":
40 c.fill(path.circle(mx, my+y, 0.05), [color.rgb.green])
41 else:
42 raise "other marks in there!"
43 y -= shape[1] + 3
44 c.writeEPSfile("textboxes")
46 random.seed(0)
47 shapes = [(10,7), (8,5)]*50
48 n = 0
49 only = 28
50 while only is None or n <= only:
51 print n
52 n += 1
53 thistext = randtext()
54 if only is not None and n <= only:
55 continue
56 boxes = text.defaulttexrunner.textboxes(thistext, shapes)
57 #output(boxes, shapes)
58 for i in range(len(boxes)):
59 if i < len(shapes):
60 shape = shapes[i]
61 if abs(unit.topt(boxes[i].bbox().right()) - unit.topt(shape[0])) > 1:
62 print ("right boundary differs:",
63 unit.topt(boxes[i].bbox().bottom()), -unit.topt(shape[1]),
64 unit.topt(boxes[i].bbox().right()), unit.topt(shape[0]),
65 i+1, len(boxes))