root/sgf2pdf/goboard.py

Revision 4, 3.2 kB (checked in by ug, 2 years ago)

Fixed typo.

  • Property svn:executable set to
Line 
1 #!/usr/bin/python
2 # print a go board using reportlab
3
4 import os
5 import sys
6 from reportlab.lib.units import inch
7 from reportlab.lib.colors import black, white
8 from reportlab.pdfgen import canvas
9
10 boardsize = 19
11 unit = .3 * inch
12 coordinates = True
13
14 def placeStone(c, x, y, color, text = None):
15     '''Place a stone on the board, uses coordinates between 1 and 19,
16     (1,1) corresponds to [aa] in SGF.
17     If color is neither black nor white, then the text is put as a label.'''
18
19     if color == black:
20         strokecolor = black
21         invcolor = white
22     elif color == white:
23         invcolor = black
24         strokecolor = black
25     else:
26         color = white
27         strokecolor = white
28         invcolor = black
29     y = 20-y
30     c.saveState()
31     c.setStrokeColor(strokecolor)
32     c.setFillColor(color)
33     if strokecolor == black:
34         c.circle(inch + (x-1)*unit, inch + (y-1)*unit, .48*unit, 1, 1)
35     else:
36         c.circle(inch + (x-1)*unit, inch + (y-1)*unit, .3*unit, 1, 1)
37    
38     if not text is None:
39         c.setStrokeColor(invcolor)
40         c.setFillColor(invcolor)
41         c.setFont('Helvetica', 12)
42         c.drawCentredString(inch + (x-1)*unit, inch + y*unit-1.18*unit, text)
43     c.restoreState()
44
45 def getCanvas(filename):
46     c = canvas.Canvas(filename + '.pdf')
47     c.translate(0, 5*inch)
48
49     l1 = [ inch + i * unit for i in range(boardsize) ]
50     c.grid(l1, l1)
51
52     if boardsize == 19:
53         if coordinates:
54             for i in range(19):
55                 c.drawCentredString(inch + i*unit, inch - unit , 'ABCDEFGHJKLMNOPQRST'[i])
56             for i in range(19):
57                 c.drawCentredString(inch - unit, inch + i*unit-.1*unit , str(i+1))
58         for i in range(3):
59             for j in range(3):
60                 c.circle(inch + (i*6 + 3) * unit, inch + (j*6 + 3) * unit, .1 * unit, 1, 1)
61     elif boardsize == 13:
62         for i in range(3):
63             for j in range(3):
64                 c.circle(inch + (i*3 + 3) * unit, inch + (j*3 + 3) * unit, .1 * unit, 1, 1)
65     elif boardsize == 9:
66         for i in range(2):
67             for j in range(2):
68                 c.circle(inch + (i*4 + 2) * unit, inch + (j*4 + 2) * unit, .1* unit, 1, 1)
69         c.circle(inch + 4 * unit, inch + 4 * unit, .075 * unit, 1, 1)
70     return c
71
72 if len(sys.argv) <= 1:
73     print 'Usage: goboard.py POSITION'
74     print 'where POSITION is the name of a file containing a go board position in wiki'
75     print "markup (as used by Sensei's Library)."
76
77 file = open(sys.argv[1])
78 lines = file.readlines()
79 file.close()
80
81 c = getCanvas(sys.argv[1])
82 y = 0
83 for line in lines:
84     if not line.startswith('$$ | '): continue
85     y += 1
86     l = line[5:].split(' ')
87     for i in range(19):
88         x = i+1
89         if l[i] == 'X': placeStone(c,x,y,black)
90         elif l[i] == 'O': placeStone(c,x,y,white)
91         elif l[i] in ['1','3','5','7','9']: placeStone(c,x,y,black, l[i])
92         elif l[i] in ['2','4','6','8']: placeStone(c,x,y,white, l[i])
93         elif l[i] == '0': placeStone(c,x,y,white, '10')
94         elif not l[i] in ['.',',']: placeStone(c,x,y,None, l[i])
95 c.showPage()
96 c.save()
97
98 # if ImageMagick is installed you can uncomment the lines below in order to produce a png file
99 # from the pdf.
100
101 # os.system('convert -density 400x400 %s.pdf -resize 17%% %s.png' % (sys.argv[1], sys.argv[1]))
102
Note: See TracBrowser for help on using the browser.