-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# This workflow will upload a Python Package using Twine when a release is created | ||
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries | ||
|
||
# This workflow uses actions that are not certified by GitHub. | ||
# They are provided by a third-party and are governed by | ||
# separate terms of service, privacy policy, and support | ||
# documentation. | ||
|
||
name: Upload Python Package | ||
|
||
on: | ||
push: | ||
tags: | ||
- "v*" | ||
|
||
permissions: write-all | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.9' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip build | ||
- name: Build package | ||
run: | | ||
python3 -m build --sdist --wheel --outdir dist/ . | ||
- name: Publish package | ||
uses: pypa/gh-action-pypi-publish@release/v1 | ||
- name: Create GitHub release | ||
uses: marvinpinto/action-automatic-releases@latest | ||
with: | ||
repo_token: "${{ secrets.GITHUB_TOKEN }}" | ||
prerelease: false | ||
files: | | ||
dist/* | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
""" | ||
some small work with fonts. | ||
""" | ||
|
||
import os | ||
import sys | ||
from pathlib import Path | ||
from typing import cast | ||
|
||
import freetype | ||
from typing_extensions import NamedTuple, TypeAlias | ||
|
||
FontFamilyName: TypeAlias = str | ||
StyleName: TypeAlias = str | ||
|
||
FONTDIRS_SYSTEM: list[Path] = [] | ||
FONTDIRS_CUSTOM: list[Path] = [] | ||
SUPPORTED_EXT: tuple[str, ...] = ( | ||
".ttf", ".ttc", ".otf", ".otc", ".cff", ".woff", ".woff2", | ||
".pfa", ".pfb", ".pcf", ".fnt", ".bdf", ".pfr" | ||
) | ||
SUPPORTED_EXT += tuple(f.upper() for f in SUPPORTED_EXT) | ||
_indexed_fontfiles: set[Path] = set() | ||
|
||
|
||
class FontRef(NamedTuple): | ||
"""Data for locating font style.""" | ||
path: Path | ||
bank: int | ||
|
||
|
||
_indexed_fontrefs: dict[FontFamilyName, dict[StyleName, FontRef]] = {} | ||
_indexed_langnames: dict[FontFamilyName, FontFamilyName] = {} | ||
|
||
|
||
def update_system_fontdirs() -> None: | ||
"""Update system font directories (in `FONTDIRS_SYSTEM`).""" | ||
FONTDIRS_SYSTEM.clear() | ||
if sys.platform == "win32": | ||
if localappdata := os.getenv("LOCALAPPDATA"): | ||
# include user-site font directory | ||
if ( | ||
_localfontdir := Path(localappdata) / "Microsoft" / "Windows" / "Fonts" | ||
).is_dir(): | ||
FONTDIRS_SYSTEM.append(_localfontdir) | ||
if windir := os.getenv("WINDIR"): | ||
# include system-site font directory | ||
FONTDIRS_SYSTEM.append(Path(windir) / "fonts") | ||
elif sys.platform in ("linux", "linux2"): | ||
if not (xdgdata := os.getenv("XDG_DATA_DIRS")): | ||
# thank you xdg | ||
# (some weird platforms does not have `/usr/share`) | ||
xdgdata = "/usr/share" if os.path.exists("/usr/share") else "" | ||
FONTDIRS_SYSTEM.extend( | ||
Path(datadir) / "fonts" | ||
for datadir in xdgdata.split(":") if datadir | ||
) | ||
elif sys.platform == "darwin": | ||
FONTDIRS_SYSTEM.extend( | ||
( | ||
Path("~/Library/Fonts").expanduser(), | ||
Path("/Library/Fonts"), Path("/System/Library/Fonts") | ||
) | ||
) | ||
|
||
|
||
def get_fontdirs() -> list[Path]: | ||
"""Get all font directories.""" | ||
return FONTDIRS_CUSTOM + FONTDIRS_SYSTEM | ||
|
||
|
||
def update_fontfiles_index() -> None: | ||
"""Update font files index.""" | ||
for directory in FONTDIRS_SYSTEM: | ||
for r, _, fs in os.walk(directory): | ||
_indexed_fontfiles.update( | ||
{Path(r) / fn for fn in fs if fn.endswith(SUPPORTED_EXT)} | ||
) | ||
for directory in FONTDIRS_CUSTOM: | ||
for r, _, fs in os.walk(directory): | ||
_indexed_fontfiles.update( | ||
{Path(r) / fn for fn in fs if fn.endswith(SUPPORTED_EXT)} | ||
) | ||
|
||
|
||
def _update_fontref_index(fn: Path, face: freetype.Face) -> None: | ||
family = cast(bytes, face.family_name).decode() | ||
style = cast(bytes, face.style_name).decode() | ||
_indexed_fontrefs.setdefault(family, {}) | ||
_indexed_fontrefs[family][style] = FontRef(fn, face.face_index) | ||
_indexed_langnames.update( | ||
{ | ||
( | ||
sfo.string.decode("utf-16-be" if sfo.platform_id != 1 else "gbk") | ||
# here actually I should use platform encoding dictionaries instead | ||
# of single `gbk`, but it'll take some time. | ||
): family | ||
for sfo in ( | ||
face.get_sfnt_name(i) for i in range(face.sfnt_name_count) | ||
) if sfo.name_id in (1, 16) | ||
} | ||
) | ||
|
||
|
||
def update_fontrefs_index(): | ||
"""Update font references index.""" | ||
for fn in _indexed_fontfiles: | ||
face = freetype.Face(str(fn)) | ||
_update_fontref_index(fn, face) | ||
for i in range(1, face.num_faces): | ||
face = freetype.Face(str(fn), i) | ||
_update_fontref_index(fn, face) | ||
|
||
|
||
update_system_fontdirs() | ||
update_fontfiles_index() | ||
update_fontrefs_index() | ||
|
||
if __name__ == "__main__": | ||
from pprint import pprint | ||
print("_indexed_fontrefs:") | ||
pprint(_indexed_fontrefs, indent=4) | ||
print("_indexed_langnames:") | ||
pprint(_indexed_langnames, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[project] | ||
name = "fontra" | ||
version = "0.1.0" | ||
description = "Some small work with fonts." | ||
authors = [ | ||
{name = "HivertMoZara", email = "[email protected]"}, | ||
] | ||
license = {text = "MIT"} | ||
dependencies = ["freetype-py>=0.3.2,<1", "typing_extensions>=4.6.0,<5"] | ||
requires-python = ">=3.9" | ||
readme = "README.md" | ||
|
||
[project.urls] | ||
Homepage = "https://github.com/NCBM/fontra" | ||
Repository = "https://github.com/NCBM/fontra" | ||
|
||
[tool.pyright] | ||
pythonVersion = "3.9" |