Skip to content

Instantly share code, notes, and snippets.

@simoncozens
Last active July 15, 2022 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simoncozens/173c9d35e28c1c6e43e58405d0c4695b to your computer and use it in GitHub Desktop.
Save simoncozens/173c9d35e28c1c6e43e58405d0c4695b to your computer and use it in GitHub Desktop.
import json
import urllib.request
import subprocess
import psutil
import tempfile
import os
import glob
import yaml
import re
from fontTools import designspaceLib
from glyphsLib import GSFont
from ufoLib2 import Font
import argparse
parser = argparse.ArgumentParser(description="Build all of Noto and time it.")
parser.add_argument(
"--statics",
action="store_true",
help="build statics (default: build variable fonts)",
)
args = parser.parse_args()
with urllib.request.urlopen("http://notofonts.github.io/noto.json") as url:
noto = json.loads(url.read().decode())
results = []
def get_font_stats(font_path):
if font_path.endswith(".designspace"):
ds = designspaceLib.DesignSpaceDocument.fromfile(font_path)
glyphs = len(Font.open(ds.sources[0].path))
masters = len(ds.sources)
instances = len(ds.instances)
elif font_path.endswith(".glyphs"):
gs = GSFont(font_path)
glyphs = len(gs.glyphs)
masters = len(gs.masters)
instances = len(gs.instances)
return (masters, instances, glyphs)
def build_one_target(name, path):
os.chdir(os.path.dirname(path))
config = os.path.basename(path)
config = yaml.load(open(config), Loader=yaml.FullLoader)
sources = config["sources"]
masters, instances, glyphs = get_font_stats(sources[0])
# Just build the first
if args.statics:
fontmake_args = ["-o", "ttf", "-i"]
else:
fontmake_args = ["-o", "variable"]
rc = subprocess.run(
["/usr/bin/time", "-o", "/tmp/time", "fontmake"]
+ fontmake_args
+ [
"--",
sources[0],
],
capture_output=True,
)
with open("/tmp/time") as f:
timings = f.read()
r = re.search(r"(\S+) real\s*(\S+) user\s*(\S+) sys", timings)
rv = {
"name": name,
"real": float(r[1]),
"user": float(r[2]),
"sys": float(r[3]),
"succeeded": rc.returncode == 0,
"format": os.path.splitext(sources[0])[-1][1:],
"masters": masters,
"instances": instances,
"glyphs": glyphs,
}
return rv
if os.path.exists("state.json"):
state = json.load(open("state.json"))
else:
state = {}
for repo, v in noto.items():
print(f"{repo}")
url = v["repo_url"]
print(f" ..cloning")
with tempfile.TemporaryDirectory() as td:
subprocess.run(
["git", "clone", "--depth", "1", url, td], capture_output=True, check=True
)
for font in glob.glob(td + "/sources/config*.yaml"):
name = os.path.basename(font).replace("config-", "").replace(".yaml", "")
print(f" ..building {name}")
if name in state:
print(f" ..already built")
continue
before = os.getcwd()
rv = build_one_target(name, font)
os.chdir(before)
if rv:
results.append(rv)
state[name] = rv
json.dump(state, open("state.json", "w"), indent=4)
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment