diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..947f120 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.png binary diff --git a/README.md b/README.md index 2065318..90855ff 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/cool.png b/examples/cool.png new file mode 100644 index 0000000..398b5a4 Binary files /dev/null and b/examples/cool.png differ diff --git a/main.py b/main.py index 24757f7..dd07ae3 100644 --- a/main.py +++ b/main.py @@ -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: @@ -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: