Add package names to install on Trisquel
[card-game.git] / card-generation.py
1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # This program is distributed in the hope that it will be useful, but
7 # WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <https://www.gnu.org/licenses/>.
13
14 from reportlab.lib.pagesizes import letter
15 from reportlab.pdfgen import canvas
16 from reportlab.lib.units import inch
17 from reportlab.lib.colors import black, white
18 from textwrap import wrap
19
20 # Function to read lines from a file
21 def read_lines(filename):
22     with open(filename, 'r', encoding='utf-8') as file:
23         return [line.strip() for line in file.readlines() if line.strip()]
24
25 # Paper size (example: letter size)
26 paper_width, paper_height = letter
27
28 # Card size in inches
29 card_width, card_height = 3.5 * inch, 2.5 * inch
30
31 # Function to add cards to a PDF
32 def create_pdf(cards, filename, is_question):
33     c = canvas.Canvas(filename, pagesize=(paper_width, paper_height))
34     c.setFont("Helvetica", 10)  # Set default font
35
36     cards_per_row = int(paper_width // card_width)
37     cards_per_column = int(paper_height // card_height)
38     cards_per_page = cards_per_row * cards_per_column
39
40     def add_card(text, x, y):
41         fill_color = black if is_question else white
42         text_color = white if is_question else black
43         c.setFillColor(fill_color)
44         c.rect(x, y, card_width, card_height, stroke=1, fill=1)
45         c.setFillColor(text_color)
46
47         # Wrap text to fit within card
48         max_width = card_width - 20  # Adjust for some padding
49         wrapped_text = wrap(text, width=40)  # Wrap text (adjust width as needed)
50
51         for line_number, line in enumerate(wrapped_text, start=1):
52             c.drawString(x + 10, y + card_height - 20 - (line_number * 10), line)
53
54     for index, card_text in enumerate(cards):
55         page_index = index // cards_per_page
56         card_index = index % cards_per_page
57         row_index = card_index // cards_per_row
58         column_index = card_index % cards_per_row
59         x = column_index * card_width
60         y = paper_height - ((row_index + 1) * card_height)
61
62         # New page if necessary
63         if card_index == 0 and index != 0:
64             c.showPage()
65
66         add_card(card_text, x, y)
67
68     c.save()
69
70 # Load questions and answers
71 questions = read_lines('questions')
72 answers = read_lines('answers')
73
74 # Create PDFs for questions and answers
75 create_pdf(questions, "question_cards.pdf", is_question=True)
76 create_pdf(answers, "answer_cards.pdf", is_question=False)