mirror of
https://github.com/Shik3i/KoalaSync.git
synced 2026-07-26 12:08:15 +00:00
af4ac943a9
- Flag font: ship a committed fontTools subset (43 KB, -45%) of TwemojiCountryFlags.woff2 with only the 15 flags the site uses, under a content-hashed name with a preload on every flag-bearing page. The build validates the subset against a manifest and fails when a new flag appears without regenerating (npm run subset-flags). JS subsetters (subset-font/harfbuzzjs) were rejected: their wasm builds silently drop the COLR/CPAL color tables, rendering all flags invisible. - Responsive mascots: legal/join/404 pages served 434-500px sources for 175-180px slots; the build now emits 180w/360w variants (AVIF 1x: ~7 KB vs ~22 KB) and the pages use srcset. Small logo spots (14-42px) now use the existing 64/128px variants instead of the 256px file. - injectAvifPictures copies the img sizes attribute onto the AVIF <source>; without it Chrome mis-selects w-descriptor candidates and can drop the image entirely. - AVIF conversion is cached by content hash (.avif-cache.json); the old mtime check never hit because copyDirSync refreshes mtimes, so every verify re-encoded all 26 files. - HTML output is minified (comments + indentation, <pre> preserved, newlines kept for inline-script safety): index.html 121 KB -> 93 KB. - unicode-range trimmed to U+1F1E6-1F1FF; tag-sequence flags fall through to the system emoji font. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
91 lines
3.1 KiB
Python
91 lines
3.1 KiB
Python
"""Subset TwemojiCountryFlags.woff2 to the given country flags.
|
|
|
|
Invoked by subset-flag-font.mjs — do not run directly unless debugging:
|
|
python3 subset_flag_font.py <source.woff2> <output.woff2> DE,FR,GB,...
|
|
|
|
Requires: fonttools, brotli (pip install fonttools brotli).
|
|
|
|
harfbuzz-based JS subsetters (subset-font/harfbuzzjs) silently drop the
|
|
COLR/CPAL color tables of this font, producing invisible flags — fontTools
|
|
subsets them correctly. Each requested flag is validated to have a GSUB
|
|
ligature AND COLR color layers so a broken subset can never be emitted.
|
|
"""
|
|
import io
|
|
import sys
|
|
|
|
from fontTools import subset
|
|
from fontTools.ttLib import TTFont
|
|
|
|
|
|
def flag_codepoints(code):
|
|
return [0x1F1E6 + ord(ch) - ord("A") for ch in code]
|
|
|
|
|
|
def ligature_glyph(font, first, second):
|
|
"""Return the GSUB ligature glyph for a regional-indicator pair."""
|
|
gsub = font["GSUB"].table
|
|
for lookup in gsub.LookupList.Lookup:
|
|
subtables = []
|
|
if lookup.LookupType == 4:
|
|
subtables = lookup.SubTable
|
|
elif lookup.LookupType == 7: # extension
|
|
subtables = [st.ExtSubTable for st in lookup.SubTable if st.ExtensionLookupType == 4]
|
|
for st in subtables:
|
|
for lig in st.ligatures.get(first, []):
|
|
if lig.Component == [second]:
|
|
return lig.LigGlyph
|
|
return None
|
|
|
|
|
|
def main():
|
|
src_path, out_path, codes_arg = sys.argv[1], sys.argv[2], sys.argv[3]
|
|
codes = sorted(codes_arg.split(","))
|
|
|
|
font = TTFont(src_path)
|
|
if "COLR" not in font or "CPAL" not in font:
|
|
sys.exit("source font has no COLR/CPAL tables")
|
|
|
|
text = "".join(chr(cp) for code in codes for cp in flag_codepoints(code))
|
|
options = subset.Options()
|
|
options.flavor = "woff2"
|
|
subsetter = subset.Subsetter(options=options)
|
|
subsetter.populate(text=text)
|
|
subsetter.subset(font)
|
|
|
|
buf = io.BytesIO()
|
|
font.save(buf)
|
|
result = TTFont(io.BytesIO(buf.getvalue()))
|
|
|
|
for table in ("COLR", "CPAL", "GSUB", "cmap"):
|
|
if table not in result:
|
|
sys.exit(f"subset lost the {table} table")
|
|
|
|
cmap = result.getBestCmap()
|
|
glyph_name = {}
|
|
for code in codes:
|
|
for cp in flag_codepoints(code):
|
|
if cp not in cmap:
|
|
sys.exit(f"flag {code}: regional indicator U+{cp:X} missing from subset cmap")
|
|
first, second = (cmap[cp] for cp in flag_codepoints(code))
|
|
lig = ligature_glyph(result, first, second)
|
|
if lig is None:
|
|
sys.exit(f"flag {code}: no GSUB ligature in subset — font has no artwork for it")
|
|
glyph_name[code] = lig
|
|
|
|
colr = result["COLR"]
|
|
if colr.version == 0:
|
|
for code, lig in glyph_name.items():
|
|
if not colr.ColorLayers.get(lig):
|
|
sys.exit(f"flag {code}: ligature glyph {lig} has no COLR color layers")
|
|
# COLR v1 would need different traversal; Twemoji country flags is v0
|
|
elif colr.version != 0:
|
|
sys.exit(f"unexpected COLR version {colr.version} — validation only supports v0")
|
|
|
|
with open(out_path, "wb") as f:
|
|
f.write(buf.getvalue())
|
|
print(f"subset OK: {len(codes)} flags, {len(buf.getvalue())} bytes")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|