Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.png binary
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

Injects a PNG logo into a TTF font as a ligature. Type a trigger sequence
in any ligature-aware tool and the logo renders in place of the text. The
replacement is context aware and will only trigger when the sequence is
surrounded by whitespace.
replacement is context aware and will only trigger when the sequence is not
part of another number or string. Punctuation and whitespace may precede or
follow the sequence.

```
uv run font-lig --font input.ttf --logo cool.png --out output.ttf --sequence "cool" --family-name "Font + Cool"

echo "Fonts are cool!"
```

![example](./examples/fonts_are_cool.png)

## Existing Tools

There are lots of ways to make your own font. I didn't see anything obvious and single-purpose to achieve this
Expand Down
Binary file added examples/cool.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 5 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,13 @@ def get_or_create_marker_glyph(font: TTFont, base_glyph: str) -> str:
return marker


def non_whitespace_glyphs(font: TTFont) -> list[str]:
def alphanumeric_glyphs(font: TTFont) -> list[str]:
"""
Return glyph names for all non-whitespace characters in the font.
The ligature fires only when surrounded by whitespace or at a run boundary.

TODO: Are there other boundaries that may "trick" us?
Return glyph names for all alphanumeric characters in the font.
The ligature is suppressed when preceded/followed by a letter or digit.
"""
cmap = font.getBestCmap() or {}
return [name for c, name in cmap.items() if not chr(c).isspace()]
return [name for c, name in cmap.items() if chr(c).isalnum()]


def make_lig(glyph_name: str, components: list[str]) -> ot.Ligature:
Expand Down Expand Up @@ -393,7 +391,7 @@ def add_ligature(
first_glyph = glyph_seq[0]

if context:
exclusion_glyphs = non_whitespace_glyphs(font)
exclusion_glyphs = alphanumeric_glyphs(font)
log.debug("context exclusion glyphs: %s", exclusion_glyphs)

if exclusion_glyphs:
Expand Down
Loading