| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
import sys |
|---|
| 4 |
from string import join |
|---|
| 5 |
|
|---|
| 6 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Flowable, Frame |
|---|
| 7 |
from reportlab.lib.styles import getSampleStyleSheet |
|---|
| 8 |
from reportlab.rl_config import defaultPageSize |
|---|
| 9 |
from reportlab.lib.units import inch |
|---|
| 10 |
from reportlab.lib.colors import black, white |
|---|
| 11 |
from reportlab.pdfgen import canvas |
|---|
| 12 |
|
|---|
| 13 |
PAGE_HEIGHT = defaultPageSize[1] |
|---|
| 14 |
PAGE_WIDTH = defaultPageSize[0] |
|---|
| 15 |
styles = getSampleStyleSheet() |
|---|
| 16 |
|
|---|
| 17 |
from sgfparser import * |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
class boardPDF(Flowable): |
|---|
| 21 |
|
|---|
| 22 |
def __init__(self, filename, pos = (0,0), boardsize = 19, unit = .25, region=(1,1,19,19)): |
|---|
| 23 |
self.boardsize = boardsize |
|---|
| 24 |
self.unit = unit * inch |
|---|
| 25 |
self.pos = pos |
|---|
| 26 |
self.region = region |
|---|
| 27 |
|
|---|
| 28 |
self.blackList = [] |
|---|
| 29 |
self.whiteList = [] |
|---|
| 30 |
|
|---|
| 31 |
def initBoard(self): |
|---|
| 32 |
self.canv.translate(-(self.region[0]-1)*self.unit, -(self.region[1]-1)*self.unit) |
|---|
| 33 |
self.p = self.canv.beginPath() |
|---|
| 34 |
self.p.rect(.6*self.unit+(self.region[0]-1.5)*self.unit, .6*self.unit+(self.region[1]-1.5)*self.unit, |
|---|
| 35 |
(self.region[2])*self.unit, (self.region[3])*self.unit) |
|---|
| 36 |
self.canv.clipPath(self.p,0) |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
l1 = [ self.unit*.6 + i * self.unit for i in range(self.boardsize) ] |
|---|
| 40 |
self.canv.grid(l1, l1) |
|---|
| 41 |
|
|---|
| 42 |
self.canv.saveState() |
|---|
| 43 |
self.canv.setLineWidth(2.5) |
|---|
| 44 |
self.canv.setLineCap(2) |
|---|
| 45 |
l1 = [self.unit*.6, self.unit*.6 + (self.boardsize-1)*self.unit] |
|---|
| 46 |
self.canv.grid(l1, l1) |
|---|
| 47 |
self.canv.restoreState() |
|---|
| 48 |
|
|---|
| 49 |
if self.boardsize == 19: |
|---|
| 50 |
for i in range(3): |
|---|
| 51 |
for j in range(3): |
|---|
| 52 |
self.canv.circle(self.unit*.6 + (i*6 + 3) * self.unit, self.unit*.6 + (j*6 + 3) * self.unit, |
|---|
| 53 |
.075 * self.unit, 1, 1) |
|---|
| 54 |
elif self.boardsize == 13: |
|---|
| 55 |
for i in range(3): |
|---|
| 56 |
for j in range(3): |
|---|
| 57 |
self.canv.circle(self.unit*.6 + (i*3 + 3) * self.unit, self.unit*.6 + (j*3 + 3) * self.unit, |
|---|
| 58 |
.075 * self.unit, 1, 1) |
|---|
| 59 |
elif self.boardsize == 9: |
|---|
| 60 |
for i in range(2): |
|---|
| 61 |
for j in range(2): |
|---|
| 62 |
self.canv.circle(self.unit*.6 + (i*4 + 2) * self.unit, self.unit*.6 + (j*4 + 2) * self.unit, |
|---|
| 63 |
.075 * self.unit, 1, 1) |
|---|
| 64 |
self.canv.circle(self.unit*.6 + 4 * self.unit, self.unit*.6 + 4 * self.unit, .075 * self.unit, 1, 1) |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
def draw(self): |
|---|
| 71 |
self.initBoard() |
|---|
| 72 |
for x, y, text in self.blackList: self.drawBlackStone(x, y, text) |
|---|
| 73 |
for x, y, text in self.whiteList: self.drawWhiteStone(x, y, text) |
|---|
| 74 |
|
|---|
| 75 |
def wrap(self, availWidth, availHeight): |
|---|
| 76 |
return (self.region[2]) * self.unit, (self.region[3]) * self.unit |
|---|
| 77 |
|
|---|
| 78 |
def split(self, availWidth, availHeight): |
|---|
| 79 |
return [] |
|---|
| 80 |
|
|---|
| 81 |
def getSpaceAfter(self): |
|---|
| 82 |
return 0 |
|---|
| 83 |
|
|---|
| 84 |
def getSpaceBefore(self): |
|---|
| 85 |
return 0 |
|---|
| 86 |
|
|---|
| 87 |
def blackStone(self, x, y, text = None): |
|---|
| 88 |
self.blackList.append((x, y, text)) |
|---|
| 89 |
|
|---|
| 90 |
def drawBlackStone(self, x, y, text=None): |
|---|
| 91 |
self.canv.saveState() |
|---|
| 92 |
self.canv.setStrokeColor(black) |
|---|
| 93 |
self.canv.setFillColor(black) |
|---|
| 94 |
self.canv.circle(self.unit*.6 + (x-1)*self.unit, self.unit*.6 + (y-1)*self.unit, .472*self.unit, 1, 1) |
|---|
| 95 |
|
|---|
| 96 |
if not text is None: |
|---|
| 97 |
self.canv.setStrokeColor(white) |
|---|
| 98 |
self.canv.setFillColor(white) |
|---|
| 99 |
self.canv.setFont('Helvetica', self.unit*.5) |
|---|
| 100 |
self.canv.drawCentredString(self.unit*.6 + (x-1)*self.unit, self.unit*.6 + y*self.unit-1.18*self.unit, text) |
|---|
| 101 |
|
|---|
| 102 |
self.canv.restoreState() |
|---|
| 103 |
|
|---|
| 104 |
def whiteStone(self, x, y, text = None): |
|---|
| 105 |
self.whiteList.append((x, y, text)) |
|---|
| 106 |
|
|---|
| 107 |
def drawWhiteStone(self, x, y, text=None): |
|---|
| 108 |
self.canv.saveState() |
|---|
| 109 |
self.canv.setStrokeColor(black) |
|---|
| 110 |
self.canv.setFillColor(white) |
|---|
| 111 |
self.canv.circle(self.unit*.6 + (x-1)*self.unit, self.unit*.6 + (y-1)*self.unit, .472*self.unit, 1, 1) |
|---|
| 112 |
|
|---|
| 113 |
if not text is None: |
|---|
| 114 |
self.canv.setStrokeColor(black) |
|---|
| 115 |
self.canv.setFillColor(black) |
|---|
| 116 |
self.canv.setFont('Helvetica', self.unit*.5) |
|---|
| 117 |
self.canv.drawCentredString(self.unit*.6 + (x-1)*self.unit, self.unit*.6 + y*self.unit-1.18*self.unit, text) |
|---|
| 118 |
|
|---|
| 119 |
self.canv.restoreState() |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
def outputSGF(cursor, b, max=50, startWith = 1, firstMoveNum=1): |
|---|
| 123 |
|
|---|
| 124 |
d = {} |
|---|
| 125 |
superL = [] |
|---|
| 126 |
counter = 1 |
|---|
| 127 |
|
|---|
| 128 |
while counter < startWith: |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
if cursor.currentNode().has_key('B'): |
|---|
| 134 |
x = ord(cursor.currentNode()['B'][0][0]) - ord('a') + 1 |
|---|
| 135 |
y = b.boardsize - (ord(cursor.currentNode()['B'][0][1]) - ord('a')) |
|---|
| 136 |
b.blackStone(x, y, '') |
|---|
| 137 |
d[(x,y)] = ('B', None) |
|---|
| 138 |
counter += 1 |
|---|
| 139 |
elif cursor.currentNode().has_key('W'): |
|---|
| 140 |
x = ord(cursor.currentNode()['W'][0][0]) - ord('a') + 1 |
|---|
| 141 |
y = b.boardsize - (ord(cursor.currentNode()['W'][0][1]) - ord('a')) |
|---|
| 142 |
b.whiteStone(x, y, '') |
|---|
| 143 |
d[(x,y)] = ('W', None) |
|---|
| 144 |
counter += 1 |
|---|
| 145 |
|
|---|
| 146 |
if cursor.noChildren(): cursor.next() |
|---|
| 147 |
else: return |
|---|
| 148 |
|
|---|
| 149 |
counter = 0 |
|---|
| 150 |
while 1 and counter < max: |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
if cursor.currentNode().has_key('B'): |
|---|
| 157 |
x = ord(cursor.currentNode()['B'][0][0]) - ord('a') + 1 |
|---|
| 158 |
y = b.boardsize - (ord(cursor.currentNode()['B'][0][1]) - ord('a')) |
|---|
| 159 |
if not d.has_key((x,y)): |
|---|
| 160 |
b.blackStone(x, y, `counter+firstMoveNum`) |
|---|
| 161 |
d[(x,y)] = ('B', counter+firstMoveNum) |
|---|
| 162 |
else: |
|---|
| 163 |
if d[(x,y)][1]: |
|---|
| 164 |
superL.append(`counter+firstMoveNum` + ' at ' + `d[(x,y)][1]`) |
|---|
| 165 |
else: |
|---|
| 166 |
superL.append(`counter+firstMoveNum` + ' at ???') |
|---|
| 167 |
counter += 1 |
|---|
| 168 |
elif cursor.currentNode().has_key('W'): |
|---|
| 169 |
x = ord(cursor.currentNode()['W'][0][0]) - ord('a') + 1 |
|---|
| 170 |
y = b.boardsize - (ord(cursor.currentNode()['W'][0][1]) - ord('a')) |
|---|
| 171 |
if not d.has_key((x,y)): |
|---|
| 172 |
b.whiteStone(x, y, `counter+firstMoveNum`) |
|---|
| 173 |
d[(x,y)] = ('W', counter+firstMoveNum) |
|---|
| 174 |
else: |
|---|
| 175 |
if d[(x,y)][1]: |
|---|
| 176 |
superL.append(`counter+firstMoveNum` + ' at ' + `d[(x,y)][1]`) |
|---|
| 177 |
else: |
|---|
| 178 |
superL.append(`counter+firstMoveNum` + ' at ???') |
|---|
| 179 |
counter += 1 |
|---|
| 180 |
|
|---|
| 181 |
if cursor.noChildren(): cursor.next() |
|---|
| 182 |
else: break |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
def myFirstPage(canvas, doc): |
|---|
| 187 |
canvas.saveState() |
|---|
| 188 |
canvas.setFont('Times-Bold', 16) |
|---|
| 189 |
canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title) |
|---|
| 190 |
canvas.setFont('Times-Roman', 9) |
|---|
| 191 |
canvas.drawString(inch, 0.75*inch, 'First page / %s' % pageinfo) |
|---|
| 192 |
canvas.restoreState() |
|---|
| 193 |
|
|---|
| 194 |
def myLaterPages(canvas, doc): |
|---|
| 195 |
canvas.saveState() |
|---|
| 196 |
canvas.setFont('Times-Roman', 9) |
|---|
| 197 |
canvas.drawString(inch, 0.75*inch, 'Page %d %s' % (doc.page, pageinfo)) |
|---|
| 198 |
canvas.restoreState() |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
if __name__ == '__main__': |
|---|
| 202 |
|
|---|
| 203 |
if len(sys.argv) <= 1: |
|---|
| 204 |
print 'usage' |
|---|
| 205 |
|
|---|
| 206 |
file = open(sys.argv[1]) |
|---|
| 207 |
sgf = file.read() |
|---|
| 208 |
file.close() |
|---|
| 209 |
|
|---|
| 210 |
c = Cursor(sgf, 1) |
|---|
| 211 |
|
|---|
| 212 |
b = boardPDF('goboard2', (0,3), 19, .25, (4, 4, 8, 8)) |
|---|
| 213 |
outputSGF(c, b, 50, 50, 50) |
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
styles = getSampleStyleSheet() |
|---|
| 238 |
style = styles['Normal'] |
|---|
| 239 |
Story = [] |
|---|
| 240 |
|
|---|
| 241 |
for i in range(2): |
|---|
| 242 |
bogustext = ('This is paragraph number %s. ' % `i`) * 15 |
|---|
| 243 |
p = Paragraph(bogustext, style) |
|---|
| 244 |
Story.append(p) |
|---|
| 245 |
Story.append(Spacer(1, 0.2*inch)) |
|---|
| 246 |
|
|---|
| 247 |
bogustext = ('This is an extra paragraph without spacer') * 15 |
|---|
| 248 |
p = Paragraph(bogustext, style) |
|---|
| 249 |
Story.append(p) |
|---|
| 250 |
|
|---|
| 251 |
for i in range(5,10): |
|---|
| 252 |
bogustext = ('This is paragraph number %s. ' % `i`) * 10 |
|---|
| 253 |
p = Paragraph(bogustext, style) |
|---|
| 254 |
Story.append(p) |
|---|
| 255 |
Story.append(Spacer(1, 0.2*inch)) |
|---|
| 256 |
|
|---|
| 257 |
ca = canvas.Canvas('goboard4.pdf') |
|---|
| 258 |
|
|---|
| 259 |
ht = 0 |
|---|
| 260 |
for i in range(6): |
|---|
| 261 |
ht += Story[i].wrap(5.8*inch, 4*inch)[1] |
|---|
| 262 |
|
|---|
| 263 |
ht += 16 |
|---|
| 264 |
f1 = Frame(inch, 9*inch-ht, 6*inch, ht, showBoundary=1) |
|---|
| 265 |
f1.addFromList(Story, ca) |
|---|
| 266 |
|
|---|
| 267 |
f2 = Frame(inch, 2.5*inch, 3*inch, 6.5*inch-ht, showBoundary=1) |
|---|
| 268 |
f2.addFromList([b], ca) |
|---|
| 269 |
|
|---|
| 270 |
f3 = Frame(4*inch, 2.5*inch, 3*inch, 6.5*inch-ht, showBoundary=1) |
|---|
| 271 |
f3.addFromList(Story, ca) |
|---|
| 272 |
|
|---|
| 273 |
f4 = Frame(inch, inch, 6*inch, 1.5*inch, showBoundary=1) |
|---|
| 274 |
f4.addFromList(Story, ca) |
|---|
| 275 |
|
|---|
| 276 |
ca.save() |
|---|