Skip to content

Commit

Permalink
Improve logging; simplify font handling (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
sumpfork authored Apr 11, 2024
1 parent 9d0fc0f commit 811de70
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 139 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "domdiv"
dynamic = ["version"]
dependencies = ["reportlab", "Pillow", "configargparse"]
dependencies = ["reportlab", "Pillow", "configargparse", "loguru"]
description = "Divider Generation for the Dominion Card Game"
keywords = ["boardgame", "cardgame", "dividers"]
authors = [{ name = "Peter Gorniak", email = "[email protected]" }]
Expand Down
13 changes: 3 additions & 10 deletions src/domdiv/cards.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import re

from loguru import logger
from reportlab.lib.units import cm


Expand Down Expand Up @@ -208,11 +209,7 @@ def setImage(self, use_set_icon=False):
setImage = Card.sets[self.cardset_tag]["image"]

if setImage is None and self.cardset_tag != "base":
print(
'warning, no set image for set "{}", card "{}"'.format(
self.cardset, self.name
)
)
logger.warning(f'no set image for set "{self.cardset}", card "{self.name}"')
return setImage

def setTextIcon(self):
Expand All @@ -225,11 +222,7 @@ def setTextIcon(self):
setTextIcon = Card.sets[self.cardset_tag]["text_icon"]

if setTextIcon is None and self.cardset != "base":
print(
'warning, no set text for set "{}", card "{}"'.format(
self.cardset, self.name
)
)
logger.warning(f'no set text for set "{self.cardset}", card "{self.name}"')
return setTextIcon

def isBlank(self):
Expand Down
99 changes: 43 additions & 56 deletions src/domdiv/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import numbers
import os
import re
import sys

import pkg_resources
from loguru import logger
from PIL import Image, ImageEnhance
from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY, TA_LEFT
from reportlab.lib.styles import getSampleStyleSheet
Expand Down Expand Up @@ -586,100 +586,90 @@ def registerFonts(self):
if pkg_resources.resource_exists("domdiv", fpath):
fontpaths[font] = (fpath, False)
break
logger.trace(fontpaths)
# Mark the built-in files as pre-registered
registered = {
font: None for font, fontpath in fontpaths.items() if fontpath is None
}

# Check if *all three* Times Roman TTF fonts have been found and register them. If not -> remove all three
# from the paths list
timesTTFfound = (
"Times-Roman-TTF" in fontpaths
and "Times-Bold-TTF" in fontpaths
and "Times-Italic-TTF" in fontpaths
)
if not langlatin1 and timesTTFfound:
pdfmetrics.registerFont(
TTFont(
"Times-Roman-TTF",
pkg_resources.resource_filename(
"domdiv", fontpaths.get("Times-Roman-TTF")[0]
),
)
)
pdfmetrics.registerFont(
TTFont(
"Times-Bold-TTF",
pkg_resources.resource_filename(
"domdiv", fontpaths.get("Times-Bold-TTF")[0]
),
)
)
pdfmetrics.registerFont(
TTFont(
"Times-Italic-TTF",
pkg_resources.resource_filename(
"domdiv", fontpaths.get("Times-Italic-TTF")[0]
),
)
)
timesTTFs = set(["Times-Roman-TTF", "Times-Bold-TTF", "Times-Italic-TTF"])
timesTTF_not_found = timesTTFs - set(fontpaths)

if not langlatin1 and not timesTTF_not_found:
# Register Times Roman TTF as font family. Necessary for <b> and <i> attributes to work in Platypus!
pdfmetrics.registerFontFamily(
"Times-Roman-TTF",
normal="Times-Roman-TTF",
bold="Times-Bold-TTF",
italic="Times-Italic-TTF",
)

# Since we registered them already, mark the Times Roman TTF as pre-registered
registered.update({"Times-Roman-TTF": None})
registered.update({"Times-Bold-TTF": None})
registered.update({"Times-Italic-TTF": None})
else:
fontpaths.pop("Times-Roman-TTF", None)
fontpaths.pop("Times-Bold-TTF", None)
fontpaths.pop("Times-Italic-TTF", None)
if not langlatin1:
logger.warning(
f"Non-Latin1 language requested ({self.options.language}) "
"but Times TTF font files not provided (black boxes may appear). "
f"missing fonts: {timesTTF_not_found}"
)
for fontname in timesTTFs:
fontpaths.pop(fontname, None)

# Determine the best matching fonts for each font type.
fontprefs = {
"Name": [ # card names & types
"TrajanPro-Bold",
"MinionPro-Regular",
"Times-Roman" if langlatin1 or not timesTTFfound else "Times-Roman-TTF",
(
"Times-Roman"
if langlatin1 or timesTTF_not_found
else "Times-Roman-TTF"
),
],
"Expansion": [ # expansion names
"CharlemagneStd-Bold",
"TrajanPro-Bold",
"MinionPro-Regular",
"Times-Roman" if langlatin1 or not timesTTFfound else "Times-Roman-TTF",
(
"Times-Roman"
if langlatin1 or timesTTF_not_found
else "Times-Roman-TTF"
),
],
"Cost": [ # card costs (coins, debt, etc)
"MinionStd-Black",
"MinionPro-Bold",
"Times-Bold" if langlatin1 or not timesTTFfound else "Times-Bold-TTF",
"Times-Bold" if langlatin1 or timesTTF_not_found else "Times-Bold-TTF",
],
"PlusCost": [ # card cost superscript "+" modifiers
"Helvetica-Bold",
],
"Regular": [ # regular text
"MinionPro-Regular",
"Times-Roman" if langlatin1 or not timesTTFfound else "Times-Roman-TTF",
(
"Times-Roman"
if langlatin1 or timesTTF_not_found
else "Times-Roman-TTF"
),
],
"Bold": [ # miscellaneous bold text
"MinionPro-Bold",
"Times-Bold" if langlatin1 or not timesTTFfound else "Times-Bold-TTF",
"Times-Bold" if langlatin1 or timesTTF_not_found else "Times-Bold-TTF",
],
"Italic": [ # for --use-set-text-icon
"MinionPro-Italic",
(
"Times-Italic"
if langlatin1 or not timesTTFfound
if langlatin1 or timesTTF_not_found
else "Times-Italic-TTF"
),
],
"Rules": [
"Times-Roman" if langlatin1 or not timesTTFfound else "Times-Roman-TTF",
(
"Times-Roman"
if langlatin1 or timesTTF_not_found
else "Times-Roman-TTF"
),
],
"Monospaced": [
"Courier",
Expand All @@ -693,15 +683,13 @@ def registerFonts(self):
for style, font in self.fontStyle.items():
best = fontprefs[style][0]
if font != best:
print(
"Warning, {} missing from font dirs; "
"using {} instead.".format(best, font),
file=sys.stderr,
logger.warning(
f"Font {best} missing from font dirs; using {font} instead."
)
if font in registered:
continue
fontpath, is_local = fontpaths[font]
# print("Registering {} = {}".format(font, fontpath))
logger.trace(f"Registering {font} = {fontpath}")
pdfmetrics.registerFont(
TTFont(
font,
Expand Down Expand Up @@ -1725,10 +1713,9 @@ def drawTab(self, item, panel=None, backside=False):
else:
name_lines = lname, (lmid + rmid + " " + rname).rstrip()
else: # nowhere to break
print(
f"Could not break up long card name: {name}",
round(width / cm, 2),
round(textWidth / cm, 2),
logger.debug(
f"Could not break up long card name: {name} "
f"{round(width / cm, 2)} {round(textWidth / cm, 2)}",
)
name_lines = (name,)
else:
Expand Down Expand Up @@ -2291,7 +2278,7 @@ def calculatePages(self, cards):
if options.wrapper:
# Adjust height for wrapper. Use the maximum thickness of any divider so we know anything will fit.
maxStackHeight = max(c.getStackHeight(options.thickness) for c in cards)
print("Max Card Stack Height: {:.2f}cm ".format(maxStackHeight / cm))
logger.info(f"Max Card Stack Height: {maxStackHeight / cm:.2f}cm ")
options.dividerHeightReserved = totalHeight(options, maxStackHeight)

# Adjust for rotation
Expand Down
14 changes: 4 additions & 10 deletions src/domdiv/fonts/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
## Fonts

I believe I cannot distribute one font (Minion Pro) domdiv uses as they are owned by Adobe with a License that I understand to disallow redistribution. However, you can download the [3 font files from a third party here](https://www.dropbox.com/s/tsqk69mayoa3pfz/MinionPro-ForDominionTabs.zip?dl=1).
I believe I cannot distribute some fonts (Minion Pro, Trajan, Charlgemagne) domdiv uses as they are owned by Adobe with a License that I understand to disallow redistribution. You can look for them on fontsgeek.com or similar sites.

Other sources for the font files (included for historical record but probably unneeded as long as long as the download above works:
Alternatively, many font files are included with every install of the free Adobe Reader. For example, on Windows 7 they are in `C:\Program Files (x86)\Adobe\Reader 9.0\Resource\Font`.

- http://fontsgeek.com/fonts/Minion-Pro-Regular
- http://fontsgeek.com/fonts/Minion-Pro-Italic
- http://fontsgeek.com/fonts/Minion-Pro-Bold
Sadly, all these fonts use features that are not support by the reportlab package. Thus, they need to first be converted to ttf (TrueType) format. I used the open source package fontforge to do the conversion. Included as 'tools/convert.ff' is a script for fontforge to do the conversion, on Mac OS X with fontforge installed through homebrew you can just run, for example, `./tools/convert.ff MinionPro-Regular.otf`.

Alternatively, the font files are included with every install of the free Adobe Reader. For example, on Windows 7 they are in C:\Program Files (x86)\Adobe\Reader 9.0\Resource\Font called `MinionPro-Regular.otf`, `MinionPro-Bold.otf` and `MinionPro-It.otf`.

Sadly, all these fonts use features that are not support by the reportlab package. Thus, they need to first be converted to ttf (TrueType) format. I used the open source package fontforge to do the conversion. Included as 'convert.ff' is a script for fontforge to do the conversion, on Mac OS X with fontforge installed through macports or homebrew you can just run `./convert.ff MinionPro-Regular.otf`, `./convert.ff MinionPro-Bold.otf` and `./convert.ff MinionPro-It.otf`. With other fontforge installations, you'll need to change the first line of convert.ff to point to your fontforge executable. I have not done this step under Windows - I imagine it may be possible with a cygwin install of fontforge or some such method.

Copy the converted `.ttf` files to the `fonts` directory in the `domdiv` package/directory, then perform the package install below.
Copy the converted `.ttf` files to the `fonts` directory in the `domdiv` package/directory, then perform the package install below. Or, you can put them into any directory and provide the `--font-dir` option to point to it when you call the script, even if it's installed from pypi.

If you select language in `domdiv` options which is not supported in [ISO/IEC 8859-1:1998 (Latin1)](https://en.wikipedia.org/wiki/ISO/IEC_8859-1#Modern_languages_with_complete_coverage) (e.g. Czech), you will have to obtain Times Roman TTF fonts as well:

Expand Down
Loading

0 comments on commit 811de70

Please sign in to comment.