summaryrefslogtreecommitdiffstats
path: root/fonts/noto-fonts/gen_noto.py
blob: 829d5a34040cf541219ca6bba8a77971066eb6b1 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
"""Regenerate noto-fonts.mk for the release named by PKG_VERSION.

Usage: gen_noto.py [DOWNLOAD_DIR]

Needs the gh CLI (authenticated) and curl. The font files are fetched
to DOWNLOAD_DIR (default: a temporary directory) to compute their
sha256 sums. To bump the package, edit PKG_VERSION in the Makefile to
the new noto-monthly-release-<version> tag, run this script, reset
PKG_RELEASE and build. A family that is new upstream has to be added
to one group in GROUPS first.
"""

import collections
import hashlib
import json
import os
import re
import subprocess
import sys
import tempfile
import urllib.parse
from concurrent.futures import ThreadPoolExecutor

REPO = 'notofonts/notofonts.github.io'
SKIP_FAMILIES = ('NotoSansTest', 'NotoSerifTest')
HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, 'noto-fonts.mk')

# A script without a living user community is in "historic" whatever its
# region.
GROUPS = (
    ('core', 'Noto Sans, Serif and Sans Mono for Latin, Greek and Cyrillic', '''
        NotoSans NotoSansMono NotoSerif
    '''),
    ('extra', 'Noto Serif Display, SignWriting, Duployan and Shavian', '''
        NotoSansDuployan NotoSansShavian NotoSansSignWriting
        NotoSerifDisplay
    '''),
    ('symbols', 'Noto symbols, mathematics and music notation', '''
        NotoMusic NotoSansMath NotoSansSymbols NotoSansSymbols2
        NotoZnamennyMusicalNotation
    '''),
    ('west-asia', 'Noto fonts for West Asian scripts, including Arabic', '''
        NotoKufiArabic NotoNaskhArabic NotoNaskhArabicUI NotoRashiHebrew
        NotoSansArabic NotoSansArabicUI NotoSansArmenian NotoSansGeorgian
        NotoSansHebrew NotoSansMandaic NotoSansSamaritan NotoSansSyriac
        NotoSansSyriacEastern NotoSansSyriacWestern NotoSerifArmenian
        NotoSerifGeorgian NotoSerifHebrew NotoSerifYezidi
    '''),
    ('africa-americas', 'Noto fonts for African and American scripts', '''
        NotoSansAdlam NotoSansAdlamUnjoined NotoSansBamum NotoSansBassaVah
        NotoSansCanadianAboriginal NotoSansCherokee NotoSansCoptic
        NotoSansEthiopic NotoSansMedefaidrin NotoSansMendeKikakui
        NotoSansNKo NotoSansNKoUnjoined NotoSansOsage NotoSansOsmanya
        NotoSansTifinagh NotoSansVai NotoSerifEthiopic
    '''),
    ('south-asia', 'Noto fonts for South and Central Asian scripts', '''
        NotoNastaliqUrdu NotoSansBengali NotoSansChakma NotoSansDevanagari NotoSansGujarati
        NotoSansGunjalaGondi NotoSansGurmukhi NotoSansKannada
        NotoSansLepcha NotoSansLimbu NotoSansMalayalam NotoSansMasaramGondi
        NotoSansMeeteiMayek NotoSansMongolian NotoSansMro
        NotoSansNagMundari NotoSansNewa NotoSansOlChiki NotoSansOriya
        NotoSansSaurashtra NotoSansSinhala NotoSansSoraSompeng
        NotoSansSunuwar NotoSansSylotiNagri NotoSansTamil
        NotoSansTamilSupplement NotoSansTelugu NotoSansThaana NotoSansWancho
        NotoSansWarangCiti NotoSerifBengali NotoSerifDevanagari
        NotoSerifGujarati NotoSerifGurmukhi NotoSerifKannada
        NotoSerifMalayalam NotoSerifOriya NotoSerifSinhala NotoSerifTamil
        NotoSerifTelugu NotoSerifTibetan NotoSerifToto
    '''),
    ('southeast-asia',
     'Noto fonts for South-East Asian, Yi, Lisu and Miao scripts', '''
        NotoSansBalinese NotoSansBatak NotoSansBuginese NotoSansBuhid
        NotoSansCham NotoSansHanifiRohingya NotoSansHanunoo
        NotoSansJavanese NotoSansKayahLi NotoSansKhmer NotoSansLao
        NotoSansLaoLooped NotoSansLisu NotoSansMiao NotoSansMyanmar
        NotoSansNewTaiLue NotoSansPahawhHmong NotoSansPauCinHau
        NotoSansRejang NotoSansSundanese NotoSansTagalog NotoSansTagbanwa
        NotoSansTaiLe NotoSansTaiTham NotoSansTaiViet NotoSansTangsa
        NotoSansThai NotoSansThaiLooped NotoSansYi NotoSerifBalinese
        NotoSerifKhmer NotoSerifLao NotoSerifMyanmar NotoSerifNPHmong
        NotoSerifThai
    '''),
    ('historic', 'Noto fonts for historic scripts and numerals', '''
        NotoFangsongKSSRotated NotoFangsongKSSVertical
        NotoSansAnatolianHieroglyphs NotoSansAvestan NotoSansBhaiksuki
        NotoSansBrahmi NotoSansCarian NotoSansCaucasianAlbanian
        NotoSansChorasmian NotoSansCuneiform NotoSansCypriot
        NotoSansCyproMinoan NotoSansDeseret NotoSansEgyptianHieroglyphs
        NotoSansElbasan NotoSansElymaic NotoSansGlagolitic NotoSansGothic
        NotoSansGrantha NotoSansHatran NotoSansImperialAramaic
        NotoSansIndicSiyaqNumbers NotoSansInscriptionalPahlavi
        NotoSansInscriptionalParthian NotoSansKaithi NotoSansKawi
        NotoSansKharoshthi NotoSansKhojki NotoSansKhudawadi NotoSansLinearA
        NotoSansLinearB NotoSansLycian NotoSansLydian NotoSansMahajani
        NotoSansManichaean NotoSansMarchen NotoSansMayanNumerals
        NotoSansMeroitic NotoSansModi NotoSansMultani NotoSansNabataean
        NotoSansNandinagari NotoSansNushu NotoSansOgham
        NotoSansOldHungarian NotoSansOldItalic NotoSansOldNorthArabian
        NotoSansOldPermic NotoSansOldPersian NotoSansOldSogdian
        NotoSansOldSouthArabian NotoSansOldTurkic NotoSansPalmyrene
        NotoSansPhagsPa NotoSansPhoenician NotoSansPsalterPahlavi
        NotoSansRunic NotoSansSharada NotoSansSiddham NotoSansSogdian
        NotoSansSoyombo NotoSansTakri NotoSansTirhuta NotoSansUgaritic
        NotoSansVithkuqi NotoSansZanabazarSquare NotoSerifAhom
        NotoSerifDivesAkuru NotoSerifDogra NotoSerifGrantha
        NotoSerifHentaigana NotoSerifKhitanSmallScript NotoSerifKhojki
        NotoSerifMakasar NotoSerifOldUyghur NotoSerifOttomanSiyaq
        NotoSerifTangut NotoSerifTodhri NotoSerifVithkuqi
        NotoTraditionalNushu
    '''),
)


def version_get():
    with open(os.path.join(HERE, 'Makefile')) as f:
        m = re.search(r'^PKG_VERSION:=(\S+)$', f.read(), re.M)
    return m.group(1)


def tree_get(tag):
    out = subprocess.check_output(
        ['gh', 'api', 'repos/%s/git/trees/%s?recursive=1' % (REPO, tag)])
    tree = json.loads(out)
    assert not tree['truncated']
    return tree['tree']


def families_get(entries):
    fam = collections.defaultdict(lambda: collections.defaultdict(list))
    for e in entries:
        if e['type'] != 'blob':
            continue
        p = e['path'].split('/')
        if len(p) != 5 or p[0] != 'fonts' or p[2] != 'googlefonts':
            continue
        if p[3] not in ('ttf', 'variable-ttf') or p[1] in SKIP_FAMILIES:
            continue
        fam[p[1]][p[3]].append((p[4], e['size']))
    return fam


def groups_check(fam):
    grouped = [f for _, _, families in GROUPS for f in families.split()]
    dup = sorted(f for f, n in collections.Counter(grouped).items() if n > 1)
    assert not dup, 'in more than one group: %s' % ' '.join(dup)
    new = sorted(set(fam) - set(grouped))
    assert not new, 'not in any group: %s' % ' '.join(new)
    gone = sorted(set(grouped) - set(fam))
    assert not gone, 'not in the release: %s' % ' '.join(gone)


def family_title(family):
    s = re.sub(r'^Noto', '', family)
    return re.sub(r'(?<=[a-z0-9])(?=[A-Z])', ' ', s)


def name_local(filename):
    return filename.replace('[', '_').replace(']', '').replace(',', '_')


def sha256_get(path):
    with open(path, 'rb') as f:
        return hashlib.sha256(f.read()).hexdigest()


def main():
    version = version_get()
    tag = 'noto-monthly-release-%s' % version
    raw = 'https://raw.githubusercontent.com/%s/%s/' % (REPO, tag)
    dl = sys.argv[1] if len(sys.argv) > 1 else tempfile.mkdtemp()

    fam = families_get(tree_get(tag))
    groups_check(fam)

    jobs = []
    rows = collections.OrderedDict()
    for group, _, families in GROUPS:
        for f in sorted(families.split()):
            kind = 'variable-ttf' if 'variable-ttf' in fam[f] else 'ttf'
            for fn, size in sorted(fam[f][kind]):
                dst = os.path.join(dl, f, kind, name_local(fn))
                jobs.append(('fonts/%s/googlefonts/%s/%s' % (f, kind, fn),
                             dst, size))
                rows.setdefault(group, []).append(
                    (f, kind, urllib.parse.quote(fn, safe=''),
                     name_local(fn), dst))
    license_dst = os.path.join(dl, 'fonts', 'LICENSE')
    jobs.append(('fonts/LICENSE', license_dst, None))

    def fetch(job):
        path, dst, size = job
        os.makedirs(os.path.dirname(dst), exist_ok=True)
        url = raw + urllib.parse.quote(path, safe='/')
        subprocess.check_call(['curl', '-sSfL', '--retry', '3', '-o', dst, url])
        if size is not None:
            assert os.path.getsize(dst) == size, (path, size)

    with ThreadPoolExecutor(8) as ex:
        list(ex.map(fetch, jobs))

    with open(OUT, 'w') as o:
        o.write('# Generated by gen_noto.py for %s, one NotoFile per font file.\n' % tag)
        o.write('# Per family the variable fonts are used where upstream ships them,\n')
        o.write('# otherwise the static fonts, both from googlefonts/.\n\n')
        o.write('NOTO_LICENSE_HASH:=%s\n' % sha256_get(license_dst))
        for group, title, families in GROUPS:
            o.write('\nNOTO_GROUPS+=%s\n' % group)
            o.write('NOTO_TITLE_%s:=%s\n' % (group, title))
            o.write('NOTO_FAMILIES_%s:=%s\n' % (group, ', '.join(
                family_title(f) for f in sorted(families.split()))))
            for f, kind, urlfn, loc, dst in rows[group]:
                o.write('$(eval $(call NotoFile,%s,%s,%s,%s,%s,%s))\n'
                        % (group, f, kind, urlfn, loc, sha256_get(dst)))
    print(len(jobs) - 1, 'files', len(fam), 'families', len(GROUPS), 'packages')


main()