Skip to content

Instantly share code, notes, and snippets.

@khaledhosny
Last active May 8, 2020 15:49
Show Gist options
  • Save khaledhosny/7c120b9795edb11dca9e12287cdb3b10 to your computer and use it in GitHub Desktop.
Save khaledhosny/7c120b9795edb11dca9e12287cdb3b10 to your computer and use it in GitHub Desktop.
Calculate font script and language coverage based on ICU exemplar data
# Copyright 2020 Khaled Hosny
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from icu import Locale, LocaleData
def coverage(font, threshold=10):
cmap = set(chr(c) for c in font.getBestCmap())
languages = set()
scripts = set()
partial = {}
for locale in Locale.getAvailableLocales():
data = LocaleData(locale)
examplar = set("".join(data.getExemplarSet()))
if not cmap.isdisjoint(examplar):
locale = Locale(locale)
locale.addLikelySubtags()
diff = examplar - cmap
if not diff:
scripts.add(locale.getDisplayScript())
languages.add(locale.getDisplayLanguage())
elif len(diff) <= threshold:
partial[locale.getDisplayLanguage()] = diff
return scripts, languages, partial
def report(scripts, languages, partial):
text = []
if scripts or languages:
text.append("# Full")
if scripts:
scripts = ", ".join(sorted(scripts))
text.append(f"* Scripts: {scripts}")
if languages:
languages = ", ".join(sorted(languages))
text.append(f"* Languages: {languages}")
if partial:
import unicodedata as ucd
text.append("# Partial")
for language, missing in partial.items():
missing = ", ".join((ucd.combining(c) and f"\u25CC{c}" or c) for c in sorted(missing))
text.append(f"* {language}: {missing}")
return "\n".join(text)
if __name__ == "__main__":
import sys
from fontTools.ttLib import TTFont
font = TTFont(sys.argv[1])
print(report(*coverage(font)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment