rename filename to file and adjust documentation
[PyX/mjg.git] / test / experimental / textboxes.py
blobb7fd7fb3cf2ba107aee688c4e9309634c0893aef
1 import sys; sys.path[:0] = ["../.."]
2 import random, string
3 from pyx import *
5 text.set(texipc=1)
6 text.set(mode="tex", 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 tr = text.texrunner()
30 for i in range(len(boxes)):
31 if i < len(shapes):
32 shape = shapes[i]
33 c.stroke(path.rect(0, y, shape[0], -shape[1]))
34 c.stroke(boxes[i].bbox().path(), [trafo.translate(0, y), color.rgb.red])
35 c.insert(boxes[i], [trafo.translate(0, y)])
36 for mark in boxes[i].markers.keys():
37 mx, my = boxes[i].markers[mark]
38 c.insert(tr.text(mx,my+y, mark+"~", [text.size.tiny, text.halign.right]))
39 if mark[:5] == "start":
40 c.fill(path.circle(mx, my+y, 0.05), [color.rgb.red])
41 elif mark[:3] == "end":
42 c.fill(path.circle(mx, my+y, 0.05), [color.rgb.green])
43 else:
44 raise "other marks in there!"
45 y -= shape[1] + 3
46 c.writeEPSfile("textboxes")
48 random.seed(0)
49 shapes = [(10,7), (8,5)]*50
50 n = 0
51 only = 28
52 while only is None or n <= only:
53 print n
54 n += 1
55 if 1:
56 thistext = randtext()
57 thistextfile = open("debug.thistext", "w")
58 thistextfile.write(thistext)
59 thistextfile.close()
60 else:
61 thistextfile = open("debug.thistext", "r")
62 thistext = thistextfile.read()
63 thistextfile.close()
64 if only is not None and n <= only:
65 continue
66 boxes = text.defaulttexrunner.textboxes(thistext, shapes)
67 output(boxes, shapes)
68 for i in range(len(boxes)):
69 if i < len(shapes):
70 shape = shapes[i]
71 if abs(unit.topt(boxes[i].bbox().right()) - unit.topt(shape[0])) > 1:
72 print ("right boundary differs:",
73 unit.topt(boxes[i].bbox().bottom()), -unit.topt(shape[1]),
74 unit.topt(boxes[i].bbox().right()), unit.topt(shape[0]),
75 i+1, len(boxes))