notebookvb
This commit is contained in:
@@ -157,6 +157,52 @@ def draw_calcudoku(c: Canvas, x0: float, y0: float, cell: float,
|
||||
c.line(lx, ly1, lx, ly2)
|
||||
|
||||
|
||||
def generate_pdf(puzzles: list[dict], output_path: Path):
|
||||
"""
|
||||
Vygeneruje PDF ze seznamu puzzle.
|
||||
Každý dict musí mít: difficulty, cages_str, solution_str, grid_size, puzzle_date.
|
||||
"""
|
||||
BOARD_CM = 11
|
||||
SOL_CM = 6
|
||||
GAP = 1.5 * cm
|
||||
page_w, page_h = A4
|
||||
|
||||
prepped = []
|
||||
for p in puzzles:
|
||||
gs = p["grid_size"]
|
||||
cell = BOARD_CM * cm / gs
|
||||
cages = parse_cages(p["cages_str"])
|
||||
cage_map = build_cage_map(cages, gs)
|
||||
solution = parse_solution(p["solution_str"], gs)
|
||||
prepped.append((p, gs, cell, cages, cage_map, solution))
|
||||
|
||||
c = Canvas(str(output_path), pagesize=A4)
|
||||
|
||||
# Zadání — 2 puzzle nad sebou na stránku
|
||||
for i in range(0, len(prepped), 2):
|
||||
for j, (p, gs, cell, cages, cage_map, _) in enumerate(prepped[i:i + 2]):
|
||||
board = gs * cell
|
||||
x0 = (page_w - board) / 2
|
||||
y0 = page_h - 2 * cm - j * (BOARD_CM * cm + 3 * cm)
|
||||
draw_calcudoku(c, x0, y0, cell, cages, cage_map, gs,
|
||||
f"Calcudoku {p['difficulty']} — {p['puzzle_date']}")
|
||||
c.showPage()
|
||||
|
||||
# Řešení
|
||||
c.setFont("ArialBold", 14)
|
||||
c.drawCentredString(page_w / 2, page_h - 2 * cm, "Řešení")
|
||||
y_cursor = page_h - 3.5 * cm
|
||||
for p, gs, _, cages, cage_map, solution in prepped:
|
||||
sol_cell = SOL_CM * cm / gs
|
||||
sol_board = gs * sol_cell
|
||||
x0 = (page_w - sol_board) / 2
|
||||
draw_calcudoku(c, x0, y_cursor, sol_cell, cages, cage_map, gs,
|
||||
p["difficulty"], solution=solution)
|
||||
y_cursor -= sol_board + GAP
|
||||
c.showPage()
|
||||
c.save()
|
||||
|
||||
|
||||
def main():
|
||||
conn = connect_mysql(database="puzzle")
|
||||
cur = conn.cursor()
|
||||
@@ -176,31 +222,14 @@ def main():
|
||||
|
||||
difficulty, cages_str, solution_str, extra_json = row
|
||||
extra = json.loads(extra_json)
|
||||
grid_size = extra["grid_size"]
|
||||
|
||||
cages = parse_cages(cages_str)
|
||||
cage_map = build_cage_map(cages, grid_size)
|
||||
solution = parse_solution(solution_str, grid_size)
|
||||
|
||||
page_w, page_h = A4
|
||||
board_cm = 11
|
||||
cell = board_cm * cm / grid_size
|
||||
board = grid_size * cell
|
||||
|
||||
c = Canvas(str(OUTPUT), pagesize=A4)
|
||||
|
||||
# Zadání
|
||||
x0 = (page_w - board) / 2
|
||||
y0 = page_h - 2 * cm
|
||||
draw_calcudoku(c, x0, y0, cell, cages, cage_map, grid_size,
|
||||
f"Calcudoku {difficulty} — 2026-05-08")
|
||||
|
||||
# Řešení
|
||||
y0_sol = y0 - board - 3 * cm
|
||||
draw_calcudoku(c, x0, y0_sol, cell, cages, cage_map, grid_size,
|
||||
"Řešení", solution=solution)
|
||||
|
||||
c.save()
|
||||
puzzles = [{
|
||||
"difficulty": difficulty,
|
||||
"cages_str": cages_str,
|
||||
"solution_str": solution_str,
|
||||
"grid_size": extra["grid_size"],
|
||||
"puzzle_date": "2026-05-08",
|
||||
}]
|
||||
generate_pdf(puzzles, OUTPUT)
|
||||
print(f"PDF uloženo: {OUTPUT}")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user