-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlabyrinth.py
executable file
·78 lines (70 loc) · 2.12 KB
/
labyrinth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
import PIL
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import sys
DPI=75
def in2px(inches):
return (int)(inches*DPI)
def px2in(pixels):
return (int)(pixels/DPI)
A1=(in2px(8.5), in2px(11))
font=ImageFont.load_default()
em=font.getsize("m")[0]
curs=(0, 0)
off=(0, 0)
rot=0
pgsz=A1
page=PIL.Image.new("L", pgsz, color=255)
rotations=0
rotated=False
for line in sys.stdin.readlines():
for w in line.split():
word=w+" "
size=font.getsize(word)
mask=Image.new("L", size, color=255)
mask.paste(font.getmask(word), (0, 0, size[0], size[1]))
print(word)
if(not
((rot==0 and curs[0]+size[0]+off[0]<pgsz[0]) or
( rot==270 and curs[1]+size[0]+off[1]<pgsz[1]) or
( rot==180 and curs[0]-size[0]>off[0] ) or
( rot==90 and curs[1]-size[0]>off[1] ))):
if rot==90: # rotate from left to top
off=(off[0], off[1]+curs[1]-em)
curs=(curs[0], 0)
elif rot==270: # rotate from right to bottom
pgsz=(pgsz[0], (off[1]+curs[1])-em)
curs=(curs[0], curs[1]-em*2)
elif rot==180: # rotate from bottom to left
off=(off[0]+curs[0]-em, off[1])
curs=(0, curs[1])
else: # rot=0 from top to right
pgsz=((off[0]+curs[0])-em, pgsz[1])
rot=(rot+270)%360
if(rotated):
page.save("page.png")
sys.exit()
rotated=True
rotations+=1
print("rot:", rot, "curs:", curs, "off:", off, "pgsz:", pgsz, "size:", size)
draw=ImageDraw.Draw(page)
draw.line((off[0], off[1], off[0], pgsz[1]))
draw.line((off[0], pgsz[1], pgsz[0], pgsz[1]))
draw.line((pgsz[0], pgsz[1], pgsz[0], off[1]))
draw.line((pgsz[0], off[1], off[0], off[1]))
print("rotating ", rot)
page.save("page.png")
else:
rotated=False
if rot==180:
curs=(curs[0]-size[0], curs[1])
elif rot==90:
curs=(curs[0], curs[1]-size[0])
page.paste(mask.rotate(rot, expand=1), (curs[0]+off[0], curs[1]+off[1]))
if rot==0:
curs=(curs[0]+size[0], curs[1])
elif rot==270:
curs=(curs[0], curs[1]+size[0])
page.save("page.png")