Skip to content

Commit

Permalink
Merge pull request #104 from googlefonts/test_strings
Browse files Browse the repository at this point in the history
Add test strings (attempt 2)
  • Loading branch information
simoncozens authored Mar 21, 2023
2 parents 16ee184 + 7251ce3 commit 33be7a7
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 72 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ jobs:
pip install pytest
- name: Run Tests
run: |
pytest ./tests/testglyphdata.py
pytest ./tests/testusage.py
pytest ./tests
43 changes: 40 additions & 3 deletions Lib/glyphsets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging
import unicodedata2 as uni
from glyphsLib.glyphdata import get_glyph
from copy import deepcopy as copy

try:
from ._version import version as __version__ # type: ignore
Expand All @@ -18,12 +19,13 @@


DATA_FP = os.path.join(os.path.dirname(__file__), "data.json")
TEST_STRINGS_DATA = os.path.join(os.path.dirname(__file__), "test_strings.json")
log = logging.getLogger(__file__)


class _GFGlyphData:
def __init__(self, data=json.load(open(DATA_FP))):
self._data = data
def __init__(self, data=json.load(open(DATA_FP, encoding="utf8"))):
self._data = copy(data)
self._in_use = set(g["nice_name"] for g in self._data["glyphs"])

def save(self, fp=DATA_FP):
Expand All @@ -39,7 +41,7 @@ def from_json(cls, fp):
def __getitem__(self, k):
return self._data[k]

def missing_glyphsets_in_font(self, ttFont, threshold=0.8):
def glyphsets_in_font(self, ttFont):
glyphs_in_font = set(ttFont.getGlyphOrder())
unicodes_in_font = set(ttFont.getBestCmap().keys())
res = {}
Expand All @@ -57,7 +59,17 @@ def missing_glyphsets_in_font(self, ttFont, threshold=0.8):
res[glyphset]["has"].append(g)
else:
res[glyphset]["missing"].append(g)
return res

def glyphsets_fulfilled(self, ttFont):
res = self.glyphsets_in_font(ttFont)
return {
k: len(v["has"]) / (len(v["has"]) + len(v["missing"]))
for k, v in res.items()
}

def missing_glyphsets_in_font(self, ttFont, threshold=0.8):
res = self.glyphsets_in_font(ttFont)
fulfilled = {
k: len(v["has"]) / (len(v["has"]) + len(v["missing"]))
for k, v in res.items()
Expand Down Expand Up @@ -233,3 +245,28 @@ def build_fea(self, glyphset):


GFGlyphData = _GFGlyphData()


class _TestDocData:
def __init__(
self,
data=json.load(open(TEST_STRINGS_DATA, encoding="utf8")),
glyphsets=GFGlyphData,
):
self._data = data
self._glyphsets = glyphsets

def test_strings_in_font(self, ttFont, threshold=0.95):
res = {}
glyphsets_in_font = self._glyphsets.glyphsets_fulfilled(ttFont)
for glyphset, coverage in glyphsets_in_font.items():
if coverage < threshold:
continue
test_strings = self._data.get(glyphset)
if not test_strings:
continue
res[glyphset] = test_strings
return res


GFTestData = _TestDocData()
35 changes: 35 additions & 0 deletions Lib/glyphsets/test_strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"GF_Latin_Kernel": {
"Letter Groups": [
"EFHIJLTU",
"AVWXZ",
"KMNY",
"COQS",
"BPRDG",
"fijltr",
"kvwxyz",
"hmnu",
"ceogs",
"abdpq"
],
"Spacing": [
"HHOHHOOHOO HIHOIO",
"uuonouonoo ninoioolonlniuo",
"010 080 030 020 050 060 070"
],
"Kerning": [
"HHAVAHH WAFAPATAUAYALYTJĿLVÆ HH",
"TiTíTîTïTìTĭTīTĩT Vă Tä Tü Tõ Tŭ Tř",
"HHTan oTo yTy vAv oVo eYe Ly Ky Ac",
"nin non nvn ovo oxo ayn fl fi fj ft gj ko rento",
"ďá ďu ŀl gj ľk włw yły ółr fħ ílïlîl",
"H.Y-Y.T,F.P,V:Y„V-T-A*A’A“A’A-AL–L—L-",
"n.r.r,y.y,(o)(i)(d)(j)(f)[j]{f}",
"080 076 474 973 94 7078",
"P4T47A .47,9.7-»4",
"42°21′29″N 71°03′49″W",
"d² m³ 15°C/26°F",
"0/0\\0"
]
}
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
package_data={'glyphsets': [
"encodings/*.nam",
"encodings/GF Glyph Sets/*.nam",
"data.json",
"*.json",
]
},
entry_points={"console_scripts": ["glyphsets = glyphsets.__main__:main"]},
Expand Down
64 changes: 64 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from glyphsets import _GFGlyphData, _TestDocData
from fontTools.ttLib import TTFont
import os
import pytest

DATA_FP = os.path.join(os.path.dirname(__file__), "data")

glyphs_data = {
"glyphs": [
{
"nice_name": "A",
"production_name": "A",
"character": "A",
"unicode": 65,
"glyphsets": [
"GFLatinIPA",
"GFLatinAfrican",
"GFLatinCore",
"GFLatinKernel",
"GFLatinMinorities",
"GFLatinPlus",
"GFLatinVietnamese",
],
},
{
"nice_name": "brevecomb_acutecomb",
"production_name": "uni03060301",
"character": None,
"unicode": None,
"glyphsets": ["GFLatinVietnamese"],
},
{
"nice_name": "fdotaccent",
"production_name": "uni1E1F",
"character": "\u1e1f",
"unicode": 7711,
"glyphsets": ["GFLatinAfrican"],
},
]
}


@pytest.fixture
def glyph_data():
return _GFGlyphData(glyphs_data)


@pytest.fixture
def test_string_data():
data = {
"GFLatinKernel": {
"Spacing": ["AAA"],
},
"GFLatinAfrican": {
"Spacing": ["BBB"],
},
}
return _TestDocData(data, _GFGlyphData(glyphs_data))


@pytest.fixture
def ttFont():
fp = os.path.join(DATA_FP, "MavenPro[wght].ttf")
return TTFont(fp)
84 changes: 18 additions & 66 deletions tests/testglyphdata.py → tests/test_glyphdata.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,7 @@
import pytest
from fontTools.ttLib import TTFont
from glyphsets import _GFGlyphData
from glyphsLib import GSFont, GSGlyph
from defcon import Font
import os

DATA_FP = os.path.join(os.path.dirname(__file__), "data")


@pytest.fixture
def db():
data = {
"glyphs": [
{
"nice_name": "A",
"production_name": "A",
"character": "A",
"unicode": 65,
"glyphsets": [
"GFLatinIPA",
"GFLatinAfrican",
"GFLatinCore",
"GFLatinKernel",
"GFLatinMinorities",
"GFLatinPlus",
"GFLatinVietnamese",
],
},
{
"nice_name": "brevecomb_acutecomb",
"production_name": "uni03060301",
"character": None,
"unicode": None,
"glyphsets": ["GFLatinVietnamese"],
},
{
"nice_name": "fdotaccent",
"production_name": "uni1E1F",
"character": "\u1e1f",
"unicode": 7711,
"glyphsets": ["GFLatinAfrican"],
},
]
}
return _GFGlyphData(data)


@pytest.fixture
def ttFont():
fp = os.path.join(DATA_FP, "MavenPro[wght].ttf")
return TTFont(fp)
from . import *


def glyphs_src1():
Expand Down Expand Up @@ -85,8 +37,8 @@ def ufo_src1():
(["foobar"], []),
],
)
def test_build_name_file(db, test_input, expected):
nam_data = db.build_nam_file(test_input)
def test_build_name_file(glyph_data, test_input, expected):
nam_data = glyph_data.build_nam_file(test_input)
assert nam_data == expected


Expand All @@ -101,8 +53,8 @@ def test_build_name_file(db, test_input, expected):
(["foobar"], []),
],
)
def test_glyphsapp_filter_lists(db, test_input, expected):
filter_list = db.build_glyphsapp_filter_list(test_input)
def test_glyphsapp_filter_lists(glyph_data, test_input, expected):
filter_list = glyph_data.build_glyphsapp_filter_list(test_input)
assert filter_list == expected


Expand Down Expand Up @@ -130,8 +82,8 @@ def test_glyphsapp_filter_lists(db, test_input, expected):
(ufo_src1(), ["GFLatinCore"], ["A"]),
],
)
def test_update_source_glyphset(db, src, test_input, expected):
db.update_source_glyphset(src, test_input)
def test_update_source_glyphset(glyph_data, src, test_input, expected):
glyph_data.update_source_glyphset(src, test_input)

if isinstance(src, GSFont):
assert [g.name for g in src.glyphs] == expected
Expand All @@ -141,41 +93,41 @@ def test_update_source_glyphset(db, src, test_input, expected):
assert set(g.name for g in src) == set(expected)


def test_update_db(db):
glyphs = [g["nice_name"] for g in db._data["glyphs"]]
def test_update_glyph_data(glyph_data):
glyphs = [g["nice_name"] for g in glyph_data._data["glyphs"]]
assert glyphs == ["A", "brevecomb_acutecomb", "fdotaccent"]

# Add a new glyphset to an already existing glyph
src1 = GSFont()
src1.filepath = "NewGlyphSet.glyphs"
src1.glyphs.append(GSGlyph("A"))
db.update_db_from_sources([src1])
assert "NewGlyphSet" in db["glyphs"][0]["glyphsets"]
glyph_data.update_db_from_sources([src1])
assert "NewGlyphSet" in glyph_data["glyphs"][0]["glyphsets"]

# Add duplicate glyphs
db_size = len(db["glyphs"])
glyph_data_size = len(glyph_data["glyphs"])
src2 = GSFont()
src2.filepath = "GFLatinCore.glyphs"
src2.glyphs.append(GSGlyph("A"))
src2.glyphs.append(GSGlyph("brevecomb_acutecomb"))
src2.glyphs.append(GSGlyph("fdotaccent"))
db.update_db_from_sources([src2])
assert len(db["glyphs"]) == db_size
glyph_data.update_db_from_sources([src2])
assert len(glyph_data["glyphs"]) == glyph_data_size

# Add new glyphs
src3 = GSFont()
src3.filepath = "Greek.glyphs"
src3.glyphs.append(GSGlyph("Alpha"))
src3.glyphs.append(GSGlyph("Beta"))
db.update_db_from_sources([src3])
assert len(db["glyphs"]) == db_size + 2
glyph_data.update_db_from_sources([src3])
assert len(glyph_data["glyphs"]) == glyph_data_size + 2

# TODO ufo sources


def test_glyphsets_missing_in_font(db, ttFont):
def test_glyphsets_missing_in_font(glyph_data, ttFont):
# TODO use fonttools fontbuilder and make a font from scratch
missing = db.missing_glyphsets_in_font(ttFont, threshold=0)
missing = glyph_data.missing_glyphsets_in_font(ttFont, threshold=0)
assert missing == {
"GFLatinAfrican": [
{
Expand Down
11 changes: 11 additions & 0 deletions tests/test_teststrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest
from . import *


def test_test_strings_in_font(test_string_data, ttFont):
res = test_string_data.test_strings_in_font(ttFont)
assert "GFLatinKernel" in res

# Drop threshold so we pick up GFLatinAfrican
res = test_string_data.test_strings_in_font(ttFont, 0.4)
assert "GFLatinAfrican" in res
File renamed without changes.

0 comments on commit 33be7a7

Please sign in to comment.