| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
from gflanguages import LoadLanguages |
| 26 |
from hyperglot import parse as hyperglot_parse |
| 27 |
|
| 28 |
|
| 29 |
def _ParseFontChars(path): |
| 30 |
""" |
| 31 |
Open the provided font path and extract the codepoints encoded in the font |
| 32 |
@return list of characters |
| 33 |
""" |
| 34 |
from fontTools.ttLib import TTFont |
| 35 |
font = TTFont(path, lazy=True) |
| 36 |
cmap = font["cmap"].getBestCmap() |
| 37 |
font.close() |
| 38 |
|
| 39 |
|
| 40 |
return [chr(c) for c in cmap.keys()] |
| 41 |
|
| 42 |
|
| 43 |
def SupportedLanguages(font_path, languages=None): |
| 44 |
""" |
| 45 |
Get languages supported by given font file. |
| 46 |
|
| 47 |
Languages are pulled from the given set. Based on whether exemplar character |
| 48 |
sets are present in the given font. |
| 49 |
|
| 50 |
Logic based on Hyperglot: |
| 51 |
https://github.com/rosettatype/hyperglot/blob/3172061ca05a62c0ff330eb802a17d4fad8b1a4d/lib/hyperglot/language.py#L273-L301 |
| 52 |
""" |
| 53 |
if languages is None: |
| 54 |
languages = LoadLanguages() |
| 55 |
|
| 56 |
chars = _ParseFontChars(font_path) |
| 57 |
|
| 58 |
supported = [] |
| 59 |
for lang in languages.values(): |
| 60 |
if not lang.HasField('exemplar_chars') or \ |
| 61 |
not lang.exemplar_chars.HasField('base'): |
| 62 |
continue |
| 63 |
|
| 64 |
base = hyperglot_parse.parse_chars(lang.exemplar_chars.base, |
| 65 |
decompose=False, |
| 66 |
retainDecomposed=False) |
| 67 |
if set(base).issubset(chars): |
| 68 |
supported.append(lang) |
| 69 |
|
| 70 |
return supported |
| 71 |
|
| 72 |
|
| 73 |
def portable_path(p): |
| 74 |
import os |
| 75 |
return os.path.join(*p.split('/')) |
| 76 |
|
| 77 |
|
| 78 |
def TEST_FILE(f): |
| 79 |
return portable_path("data/test/" + f) |
| 80 |
|
| 81 |
|
| 82 |
def test_SupportedLanguages(): |
| 83 |
font = TEST_FILE('nunito/Nunito-Regular.ttf') |
| 84 |
supported = SupportedLanguages(font) |
| 85 |
langs = [supported[i].name for i, _ in enumerate(supported)] |
| 86 |
assert len(langs) == 225 |
| 87 |
assert 'Lithuanian' in langs |