Skip to content

Commit

Permalink
Add compare command to glyphsets CLI tool
Browse files Browse the repository at this point in the history
  • Loading branch information
yanone committed Apr 10, 2024
1 parent 30404b7 commit 4d1ecbd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
46 changes: 46 additions & 0 deletions Lib/glyphsets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,52 @@ def describe_glyphset(glyph_names):
return md


def compare_glyphsets(glyphsets):
"""
Compare the contents of glyphsets to each other,
with the first glyphset being the reference.
Each consecutive glyphset gets compares to each former.
"""

if len(list(set(glyphsets))) < 2:
raise ValueError("Please provide at least two glyphsets to compare.")

if len(list(set(glyphsets))) != len(glyphsets):
raise ValueError("Please provide unique glyphsets to compare.")

reference_glyphset = glyphsets[0]
reference_glyphs = set(glyphs_in_glyphset(reference_glyphset))

print(f"{reference_glyphset}:\n{'=' * len(reference_glyphset)}\n")
print(f"Total glyphs: {len(reference_glyphs)}\n")
print(describe_glyphset(reference_glyphs))

for i, glyphset in enumerate(glyphsets):
if i == 0:
continue

this_glyphs = set(glyphs_in_glyphset(glyphsets[i]))
previous_glyphs = set(glyphs_in_glyphset(glyphsets[i - 1]))

print(f"\n{glyphset}:\n{'=' * len(glyphset)}\n")
print(f"Total glyphs: {len(this_glyphs)}\n")

missing = previous_glyphs.difference(this_glyphs)
extra = this_glyphs.difference(previous_glyphs)

if missing:
print(
f"{glyphset} has {len(missing)} **missing** glyphs compared to {glyphsets[i - 1]}:\n"
)
print(describe_glyphset(missing))

if extra:
print(
f"{glyphset} has {len(extra)} **extra** glyphs compared to {glyphsets[i - 1]}:\n"
)
print(describe_glyphset(extra))


def add_country(code):
if code in regions:
return f"{regions[code].name} ({code})"
Expand Down
11 changes: 10 additions & 1 deletion Lib/glyphsets/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import argparse
from glyphsets import build_glyphsapp_filter_list
from glyphsets import build_glyphsapp_filter_list, compare_glyphsets


def main():
Expand Down Expand Up @@ -28,6 +28,12 @@ def main():
filter_lists_parser.add_argument("--prod-names", action="store_true", default=False)
filter_lists_parser.add_argument("-o", "--out", required=True, help="output path")

filter_lists_parser = subparsers.add_parser(
"compare",
help="Compare two or more glyhsets to each other; with later glyphsets being compared to the former.",
)
filter_lists_parser.add_argument("glyphsets", nargs="+")

# nam_file_parser = subparsers.add_parser(
# "nam-file", help="Ouput .nam file from given glyphset(s)."
# )
Expand All @@ -52,6 +58,9 @@ def main():
if args.command == "filter-list":
build_glyphsapp_filter_list(args.glyphsets, args.out, args.prod_names)

if args.command == "compare":
compare_glyphsets(args.glyphsets)

# elif args.command == "update-srcs":
# srcs = [load_source(src) for src in args.srcs]
# for src in srcs:
Expand Down
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ RESULTS: │
Glyphsets Tool
==============

> [!NOTE]
> Previously existing commands of the `glyphsets` tool are currently deactivated after the transition to the new database. These are: `update-srcs`, `nam-file`, `missing-in-font`. Please report if you need to use these.
## Custom Filters

You can create your own Glyphs.app _Custom Filters_ using the `glyphsets` tool.

Install or update the tool with pip, if you haven’t already:
Expand All @@ -121,10 +126,42 @@ glyphsets filter-list -o myfilter.plist GF_Latin_Core GF_Latin_Plus
```
Add this `.plist` file next to your Glyphs file and (after restart) you would be able to see it in the filters sidebar.

> [!NOTE]
> Previously existing commands of the `glyphsets` tool are currently deactivated after the transition to the new database. These are: `update-srcs`, `nam-file`, `missing-in-font`. Please report if you need to use these.
## Compare Glyphsets

You can compare the contents of two or more glyphsets against each other. Each consecutive glyphset will be compared to the previous one.

This command lists the complete contents of `GF_Latin_Kernel` first, and then lists only extra (or missing) glyphs for `GF_Latin_Core` when compared to `GF_Latin_Kernel`:
```
glyphsets compare GF_Latin_Kernel GF_Latin_Core
```

Output:

```
GF_Latin_Kernel:
===============
Total glyphs: 116
Letter (52 glyphs):
`A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z`
...
GF_Latin_Core:
=============
Total glyphs: 324
GF_Latin_Core has 208 **extra** glyphs compared to GF_Latin_Kernel:
Letter (168 glyphs):
`ª º À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö Ø Ù Ú Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ø ù ú û ü ý þ ÿ Ā ā Ă ă Ą ą Ć ć Ċ ċ Č č Ď ď Đ đ Ē ē Ė ė Ę ę Ě ě Ğ ğ Ġ ġ Ģ ģ Ħ ħ Ī ī Į į İ ı Ķ ķ Ĺ ĺ Ļ ļ Ľ ľ Ł ł Ń ń Ņ ņ Ň ň Ő ő Œ œ Ŕ ŕ Ř ř Ś ś Ş ş Š š Ť ť Ū ū Ů ů Ű ű Ų ų Ŵ ŵ Ŷ ŷ Ÿ Ź ź Ż ż Ž ž Ș ș Ț ț ȷ Ẁ ẁ Ẃ ẃ Ẅ ẅ ẞ Ỳ ỳ /idotaccent`
...
```
Acknowledgements
================

Expand Down
5 changes: 5 additions & 0 deletions tests/test_glyphsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
languages_per_glyphset,
get_glyphsets_fulfilled,
defined_glyphsets,
compare_glyphsets,
)

DATA_FP = os.path.join(os.path.dirname(__file__), "data")
Expand Down Expand Up @@ -31,3 +32,7 @@ def test_coverage():

ttFont = TTFont(FONT_PATH)
assert get_glyphsets_fulfilled(ttFont)["GF_Latin_Core"]["percentage"] > 0.99


def test_compare():
compare_glyphsets(["GF_Latin_Kernel", "GF_Latin_Core", "GF_Latin_Plus"])

0 comments on commit 4d1ecbd

Please sign in to comment.