Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/domdiv/config_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
"centre",
"full",
]
TEXT_CHOICES = ["card", "rules", "blank"]
TEXT_CHOICES = [
"card",
"rules",
"blank",
"image",
"wiki-text",
]
LINE_CHOICES = ["line", "dot", "cropmarks", "line-cropmarks", "dot-cropmarks"]

HEAD_CHOICES = ["tab", "strap", "cover", "none"]
Expand Down
20 changes: 20 additions & 0 deletions src/domdiv/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ def read_card_data(options) -> list[Card]:
)
assert cards, "Could not load any cards from database"

with open(
os.path.join("src", "domdiv", "tools", "merged_cards_en_us.json"),
"r",
encoding="utf-8",
) as wiki_json:
wiki_cards = json.load(wiki_json)

for c in cards:
if c.name in wiki_cards:
c.wiki_text = wiki_cards[c.name].get("raw_wikitext")
else:
c.wiki_text = ""
print("Could not find card %r" % c.name)

for c in cards:
if not hasattr(c, "wiki_text"):
print(f"{c.name} has no wiki_text")
elif not c.wiki_text:
print(f"{c.name} has falsey wiki_text: [{c.wiki_text}]")
Comment on lines +186 to +190
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is debugging code that will go away.


set_db_filepath = os.path.join("card_db", "sets_db.json.gz")
with resource_handling.get_resource_stream(set_db_filepath) as setfile:
Card.sets = json.loads(setfile.read().decode("utf-8"))
Expand Down
95 changes: 59 additions & 36 deletions src/domdiv/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
from reportlab.pdfbase.pdfmetrics import stringWidth
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Paragraph, XPreformatted

from . import resource_handling
from .cards import Card
from .inline_images import IMAGES, expand_wiki_nowrap_template, replace_wiki_templates


def split(seq, n):
Expand Down Expand Up @@ -1184,6 +1186,7 @@ def replace_image_tag(
replace_specs = [
# Coins
# TODO: coin text baseline should align with surrounding text
# TODONE: fixed in the version that uses wiki markup
(r"(\d+)\s\<\*COIN\*\>", "coin_small_\\1.png", 2.4, 200),
(r"(\d+)\s(c|C)oin(s)?", "coin_small_\\1.png", 1.2, 100),
(r"([Xx])\s(c|C)oin(s)?", "coin_small_x.png", 1.2, 100),
Expand All @@ -1210,38 +1213,43 @@ def add_inline_text(self, card, text, emWidth):
# Bonuses
text = card.getBonusBoldText(text)

# wiki markup for bold and italics
text = re.sub(r"'''([^']+?)'''", r"<b>\1</b>", text)
text = re.sub(r"''([^']+?)''", r"<i>\1</i>", text)

# <line>: 11 em dashes, but not wider than the text box
line = "<center>{}</center>\n".format("&mdash;" * min(11, int(emWidth)))
text = re.sub(r"\<line\>", line, text)
line = f'<center>{"&mdash;" * min(11, int(emWidth))}</center>\n'
text = re.sub(r"<line>", line, text)
text = re.sub(r"{{[dD]ivline}}", line, text)
# <tab> and \t
text = re.sub(r"\<tab\>", "\t", text)
text = re.sub(r"\<t\>", "\t", text)
text = re.sub(r"<tab>", "\t", text)
text = re.sub(r"<t>", "\t", text)
text = re.sub(r"\t", "&nbsp;" * 4, text)

# various breaks
text = re.sub(r"\<br\>", "<br />", text)
text = re.sub(r"\<n\>", "\n", text)
text = re.sub(r"<br>", "<br />", text)
text = re.sub(r"<n>", "\n", text)

# alignments
text = re.sub(r"\<c\>", "<center>", text)
text = re.sub(r"\<center\>", "\n<para alignment='center'>", text)
text = re.sub(r"\</c\>", "</center>", text)
text = re.sub(r"\</center\>", "</para>", text)

text = re.sub(r"\<l\>", "<left>", text)
text = re.sub(r"\<left\>", "\n<para alignment='left'>", text)
text = re.sub(r"\</l\>", "</left>", text)
text = re.sub(r"\</left\>", "</para>", text)

text = re.sub(r"\<r\>", "<right>", text)
text = re.sub(r"\<right\>", "\n<para alignment='right'>", text)
text = re.sub(r"\</r\>", "</right>", text)
text = re.sub(r"\</right\>", "</para>", text)

text = re.sub(r"\<j\>", "<justify>", text)
text = re.sub(r"\<justify\>", "\n<para alignment='justify'>", text)
text = re.sub(r"\</j\>", "</justify>", text)
text = re.sub(r"\</justify\>", "</para>", text)
text = re.sub(r"<c>", "<center>", text)
text = re.sub(r"<center>", "\n<para alignment='center'>", text)
text = re.sub(r"</c>", "</center>", text)
text = re.sub(r"</center>", "</para>", text)

text = re.sub(r"<l>", "<left>", text)
text = re.sub(r"<left>", "\n<para alignment='left'>", text)
text = re.sub(r"</l>", "</left>", text)
text = re.sub(r"</left>", "</para>", text)

text = re.sub(r"<r>", "<right>", text)
text = re.sub(r"<right>", "\n<para alignment='right'>", text)
text = re.sub(r"</r>", "</right>", text)
text = re.sub(r"</right>", "</para>", text)

text = re.sub(r"<j>", "<justify>", text)
text = re.sub(r"<justify>", "\n<para alignment='justify'>", text)
text = re.sub(r"</j>", "</justify>", text)
text = re.sub(r"</justify>", "</para>", text)

return text.strip().strip("\n")

Expand Down Expand Up @@ -1286,11 +1294,10 @@ def drawCardCount(self, card, x, y, offset=-1):

return width + 1

def drawCost(self, card, x, y, costOffset=-1, scale=1):
def drawCost(self, card, x, y, scale=1):
# card = subject card
# x = left side of coin
# y = baseline height for text
# (costOffset is no longer used)

# Measured card metrics:
# coin and debt icons are 17 pt with a 1 pt drop shadow (18 pt total)
Expand Down Expand Up @@ -1979,12 +1986,20 @@ def drawText(self, item, panel, divider_text="card"):

# Figure out what text is to be printed on this divider
descriptions = None
do_legacy_substitutions = True
if divider_text == "card" and card.description:
# Add the card text to the divider
descriptions = card.description
elif divider_text == "rules" and card.extra:
# Add the extra rules text to the divider
descriptions = card.extra
elif (
divider_text == "wiki-text"
and hasattr(card, "wiki_text")
and card.wiki_text
):
do_legacy_substitutions = False
descriptions = card.wiki_text

if descriptions is None:
# No text to print, so exit early and cleanly
Expand All @@ -1993,12 +2008,16 @@ def drawText(self, item, panel, divider_text="card"):

s = getSampleStyleSheet()["BodyText"]
s.fontName = self.fontStyle["Rules"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was surprised that this wasn't using the Minion font. Any objections to using self.fontStyle["Regular"] instead? It's a little bit of extra work to switch back to Times for the <dash> rendering, since in Minion the dashes have whitespace between them, and to register the font family so bold and italics work, but I've got code locally that does all of that.

if divider_text == "card" and not card.isExpansion():
if divider_text == "wiki-text":
s.fontSize = 11 # Dominion cards seem to be printed in 11 point font
s.leading = 13 # This is the space between adjacent lines
s.autoLeading = "max"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should leave a comment explaining this one too

if divider_text in {"card", "wiki-text"} and not card.isExpansion():
s.alignment = TA_CENTER
else:
s.alignment = TA_JUSTIFY

textHorizontalMargin = 0.5 * cm
textHorizontalMargin = 0.4 * cm
textVerticalMargin = 0.3 * cm
textBoxWidth = item.cardWidth - 2 * textHorizontalMargin
textBoxHeight = totalHeight - usedHeight - 2 * textVerticalMargin
Expand All @@ -2008,21 +2027,25 @@ def drawText(self, item, panel, divider_text="card"):
if not card.isExpansion():
emWidth = textBoxWidth / s.fontSize
descriptions = self.add_inline_text(card, descriptions, emWidth)
descriptions = re.split("\n", descriptions)
descriptions = re.split("\n|</?p>", descriptions)
# Remove the last item if it's empty
if descriptions[-1] == "":
descriptions = descriptions[:-1]
while True:
paragraphs = []
# this accounts for the spacers we insert between paragraphs
h = (len(descriptions) - 1) * spacerHeight
for d in descriptions:
if card.isExpansion():
dmod = d
else:
dmod = self.add_inline_images(d, s.fontSize)
if not card.isExpansion():
if do_legacy_substitutions:
d = self.add_inline_images(d, s.fontSize)
d = replace_wiki_templates(d, s.fontSize)
d = expand_wiki_nowrap_template(d)
try:
p = Paragraph(dmod, s)
p = Paragraph(d, s)
except ValueError as e:
raise ValueError(
f'Error rendering text from "{card.name}": {e} ("{dmod}")'
f'Error rendering text from "{card.name}": {e} ("{d}")'
)
h += p.wrap(textBoxWidth, textBoxHeight)[1]
paragraphs.append(p)
Expand Down
Loading