Spaces:
Runtime error
Runtime error
File size: 3,597 Bytes
6ee449a |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
from fpdf import FPDF
import tempfile
import os
import bridgebots
from pbn_util import merge_pbn, parse_pbn
DEALER_LIST = ['N', 'E', 'S', 'W']
VULNERABILITY_LIST = ["None","NS","EW","All","NS","EW","All","None","EW","All","None","NS","All","None","NS","EW"]
SUITS = [bridgebots.Suit.SPADES, bridgebots.Suit.HEARTS, bridgebots.Suit.DIAMONDS, bridgebots.Suit.CLUBS]
SUIT_SYMBOLS = ['♠','♥','♦','♣']
def create_hand_record_pdf(pbn_paths):
filepath_merged_pbn = merge_pbn(pbn_paths)
results = parse_pbn(filepath_merged_pbn)
fd,fn = tempfile.mkstemp(".pdf")
pdf = FPDF()
pdf.add_page()
pdf.add_font('times2', style='', fname='times.ttf')
pdf.set_font("times2", "", 8)
pdf.c_margin = 0.1
table_config = {
'borders_layout':'NONE',
'col_widths':3,
'line_height':pdf.font_size + 0.5,
'align': 'L',
'text_align': 'L',
'first_row_as_headings': False,
}
start_x,start_y = 10,10
table_size = 45, 48
table_margin = 2
for i, result in enumerate(results):
deal = result[0]
board_no = int(result[1][0]['Board'])
page_i = i % 20
if (i % 20 == 0) and (i != 0):
pdf.add_page()
row_idx = page_i // 4
col_idx = page_i % 4
x = start_x + (table_size[0] + 2 * table_margin + 1) * col_idx
y = start_y + (table_size[1] + 2 * table_margin + 1) * row_idx
top_left = x - table_margin, y - table_margin
top_right = x + table_size[0] + table_margin, y - table_margin
bottom_left = x - table_margin, y + table_size[1] + table_margin
bottom_right = x + table_size[0] + table_margin, y + table_size[1] + table_margin
pdf.set_xy(x,y)
pdf.line(*top_left, *bottom_left)
pdf.line(*top_left, * top_right)
pdf.line(*top_right, *bottom_right)
pdf.line(*bottom_left, *bottom_right)
dealer = DEALER_LIST[(board_no-1) % 4]
vul = VULNERABILITY_LIST[(board_no-1) % 16]
with pdf.table(**table_config) as table:
row = table.row()
row.cell(f'{board_no}\n{dealer}/{vul}', colspan=6, rowspan=4, align='C', v_align='C')
for i, (suit, values) in enumerate(zip(SUIT_SYMBOLS, get_values(deal, 'N'))):
if i!=0:
row = table.row()
print_suit_values(pdf,row,suit,values)
row = table.row()
# row = table.row()
for i, (suit, values1, values2) in enumerate(zip(SUIT_SYMBOLS, get_values(deal, 'W'), get_values(deal, 'E'))):
row = table.row()
print_suit_values(pdf, row, suit, values1)
row.cell('',colspan=5)
print_suit_values(pdf, row, suit, values2)
row = table.row()
row = table.row()
row.cell('', colspan=6, rowspan=4)
for i, (suit, values) in enumerate(zip(SUIT_SYMBOLS, get_values(deal, 'S'))):
if i!=0:
row = table.row()
print_suit_values(pdf, row, suit, values)
pdf.output(fn)
return fn
def get_values(result, direction):
d = bridgebots.Direction.from_str(direction)
hand = result.hands[d]
return [''.join([value.abbreviation()
for value in hand.suits[suit]])
for suit in SUITS]
def print_suit_values(pdf, row, suit, values):
if (suit=='♥') or (suit=='♦'): # Hearts or Diamonds
pdf.set_text_color(255,0,0)
row.cell(suit, colspan=1)
pdf.set_text_color(0,0,0)
row.cell(values, colspan=4)
|