Skip to content

Commit

Permalink
add fontra as alternative to fontconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
FriedrichFroebel authored Sep 22, 2024
1 parent 794df28 commit e68f159
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 15 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ jobs:
run:
python -m pip install --upgrade wheel
- name: install package
run: |
run:
python -m pip install .[dev,mypy]
# Bottle still depends on the old `cgi` module removed in Python 3.13,
# thus provide it here.
# Avoid the additional error:
# RuntimeError: 'cgi' was slated for removal after Python 3.13 alpha
- name: prepare cgi module (Python 3.13)
run: |
wget https://github.com/python/cpython/raw/3.12/Lib/cgi.py
sed --in-place 's/warnings._deprecated(__name__, remove=(3,13))//' cgi.py
if: ${{ matrix.python == '3.13-dev' }}
- name: run tests
- name: run tests with fontconfig
run:
python -m unittest discover --verbose --start-directory tests
- name: install fontra
run:
python -m pip install .[fontra]
if: ${{ matrix.python != '3.8' }}
- name: run tests with fontra
run: |
python -c "from brother_ql_web.font_helpers import _has_fontra; assert _has_fontra()"
python -m unittest discover --verbose --start-directory tests
if: ${{ matrix.python != '3.8' }}
- name: run flake8
run:
python -m flake8 --extend-exclude "cgi.py" .
python -m flake8
- name: run black
run:
black --check --diff --extend-exclude "cgi.py" .
black --check --diff .
- name: run mypy
run:
mypy brother_ql_web/ tests/
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Development version

* Add optional dependency on *fontra* for cases where *fontconfig* is not available by providing the *fontra* extra. (Many thanks to *chrismaster* for the initial implementation/recommendation and *NCBM* as the maintainer of *fontra* for the quick enhancement of adding the missing bits for better *fontconfig* compatibility.)

# Version 0.3.0 - 2024-08-19

* Add API for printing images.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ It's also possible to install it from source for the current interpreter with:

In addition to the Python package requirements itself, `fontconfig` should be installed on your system. It's used to identify and inspect fonts on your machine. This package is pre-installed on many Linux distributions. If you're using a Mac, you might want to use [Homebrew](https://brew.sh) to install fontconfig using [`brew install fontconfig`](https://formulae.brew.sh/formula/fontconfig).

If you are not able to install or use `fontconfig` for some reasons, `fontra` is available as a fallback. You can install if using the `fontra` extra.

### Configuration file

Grab a copy of the [example configuration file](https://github.com/FriedrichFroebel/brother_ql_web/blob/master/config.example.json) and adjust it to your needs. You can store this file on your device wherever you want - just make sure to remember the full path as you will have to pass it to the CLI.
Expand Down
36 changes: 36 additions & 0 deletions brother_ql_web/font_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def get_fonts(folder: str | None = None) -> dict[str, dict[str, str]]:
Scan a folder (or the system) for .ttf / .otf fonts and
return a dictionary of the structure family -> style -> file path
"""
if _has_fontra():
return _get_fonts_using_fontra(folder)
return _get_fonts_using_fontconfig(folder)


def _get_fonts_using_fontconfig(folder: str | None = None) -> dict[str, dict[str, str]]:
fonts: dict[str, dict[str, str]] = defaultdict(dict)
if folder:
cmd = ["fc-scan", "--format", "%{file}:%{family}:style=%{style}\n", folder]
Expand Down Expand Up @@ -44,3 +50,33 @@ def get_fonts(folder: str | None = None) -> dict[str, dict[str, str]]:
fonts[families[i]][styles[i]] = path
# logger.debug("Added this font: %s", (families[i], styles[i], path))
return dict(fonts)


def _has_fontra() -> bool:
from importlib.util import find_spec

return find_spec("fontra") is not None


def _get_fonts_using_fontra(folder: str | None = None) -> dict[str, dict[str, str]]:
from pathlib import Path
import fontra

if folder:
fontra.FONTDIRS_CUSTOM.append(Path(folder))
fontra.update_custom_fontfiles_index()
fontra.update_fontrefs_index()
else:
fontra.init_fontdb()
fonts: dict[str, dict[str, str]] = defaultdict(dict)
families = fontra.all_fonts(classical=True)
for family in families:
styles = fontra.get_font_styles(family, classical=True)
for style in styles:
path: str = (
fontra.get_font(family, style, classical=True)
.path.absolute()
.as_posix()
)
fonts[family][style] = path
return dict(fonts)
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Issues = "https://github.com/FriedrichFroebel/brother_ql_web//issues"
Changelog = "https://github.com/FriedrichFroebel/brother_ql_web/blob/master/CHANGELOG.md"

[project.optional-dependencies]
fontra = [
"fontra>=0.5.2",
]
dev = [
"black",
"codespell",
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample-out.png
Binary file removed tests/sample-out.png
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/test_font_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def assert_font_dictionary_subset(
not_in_name: str = "",
) -> None:
for font_name, font_styles in expected.items():
self.assertIn(font_name, actual)
self.assertIn(font_name, actual.keys())
actual_styles = actual[font_name]
with self.subTest(font_name=font_name):
for font_style, path in font_styles.items():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def run() -> None:

self.thread = Thread(target=run)
self.thread.start()
sleep(0.5)
sleep(2.0)


class GetConfigTestCase(TestCase):
Expand Down

0 comments on commit e68f159

Please sign in to comment.