#!/usr/bin/python
# print a go board using reportlab

import os
import sys
from reportlab.lib.units import inch
from reportlab.lib.colors import black, white
from reportlab.pdfgen import canvas

boardsize = 19
unit = .3 * inch
coordinates = True

def placeStone(c, x, y, color, text = None):
    '''Place a stone on the board, uses coordinates between 1 and 19,
    (1,1) corresponds to [aa] in SGF.
    If color is neither black nor white, then the text is put as a label.'''

    if color == black: 
        strokecolor = black
        invcolor = white
    elif color == white: 
        invcolor = black
        strokecolor = black
    else:
        color = white
        strokecolor = white
        invcolor = black
    y = 20-y
    c.saveState()
    c.setStrokeColor(strokecolor)
    c.setFillColor(color)
    if strokecolor == black:
        c.circle(inch + (x-1)*unit, inch + (y-1)*unit, .48*unit, 1, 1)
    else:
        c.circle(inch + (x-1)*unit, inch + (y-1)*unit, .3*unit, 1, 1)
    
    if not text is None:
        c.setStrokeColor(invcolor)
        c.setFillColor(invcolor)
        c.setFont('Helvetica', 12)
        c.drawCentredString(inch + (x-1)*unit, inch + y*unit-1.18*unit, text)
    c.restoreState()

def getCanvas(filename):
    c = canvas.Canvas(filename + '.pdf')
    c.translate(0, 5*inch)

    l1 = [ inch + i * unit for i in range(boardsize) ]
    c.grid(l1, l1)

    if boardsize == 19:
        if coordinates:
            for i in range(19):
                c.drawCentredString(inch + i*unit, inch - unit , 'ABCDEFGHJKLMNOPQRST'[i])
            for i in range(19):
                c.drawCentredString(inch - unit, inch + i*unit-.1*unit , str(i+1))
        for i in range(3):
            for j in range(3):
                c.circle(inch + (i*6 + 3) * unit, inch + (j*6 + 3) * unit, .1 * unit, 1, 1)
    elif boardsize == 13:
        for i in range(3):
            for j in range(3):
                c.circle(inch + (i*3 + 3) * unit, inch + (j*3 + 3) * unit, .1 * unit, 1, 1)
    elif boardsize == 9:
        for i in range(2):
            for j in range(2):
                c.circle(inch + (i*4 + 2) * unit, inch + (j*4 + 2) * unit, .1* unit, 1, 1)
        c.circle(inch + 4 * unit, inch + 4 * unit, .075 * unit, 1, 1)
    return c

if len(sys.argv) <= 1:
    print 'Usage: goboard.py POSITION'
    print 'where POSITION is the name of a file containing a go board position in wiki'
    print "markup (as used by Sensei's Library)."

file = open(sys.argv[1])
lines = file.readlines()
file.close()

c = getCanvas(sys.argv[1])
y = 0
for line in lines:
    if not line.startswith('$$ | '): continue
    y += 1
    l = line[5:].split(' ')
    for i in range(19):
        x = i+1
        if l[i] == 'X': placeStone(c,x,y,black)
        elif l[i] == 'O': placeStone(c,x,y,white)
        elif l[i] in ['1','3','5','7','9']: placeStone(c,x,y,black, l[i])
        elif l[i] in ['2','4','6','8']: placeStone(c,x,y,white, l[i])
        elif l[i] == '0': placeStone(c,x,y,white, '10')
        elif not l[i] in ['.',',']: placeStone(c,x,y,None, l[i])
c.showPage()
c.save()

# if ImageMagick is installed you can uncomment the lines below in order to produce a png file
# from the pdf.

# os.system('convert -density 400x400 %s.pdf -resize 17%% %s.png' % (sys.argv[1], sys.argv[1]))

