"""Losslessly unpack the pinned Bootstrap Icons 1.11.3 WOFF into TrueType.
The original website's CSS and JavaScript are unchanged. The complete font is
used by the generic font-family resolver, not a four-icon replacement atlas.
Reproduce with FontTools 4.61.1 on PYTHONPATH; no Brotli is required for WOFF.
"""
import hashlib
import json
from pathlib import Path
import fontTools
from fontTools.ttLib import TTFont
ROOT = Path(__file__).resolve().parents[2]
OUTPUT = ROOT / "assets/fonts/bootstrap-icons-1.11.3"
EXPECTED = {
"BootstrapIcons.woff": "bb1de989b83970f6f4e54de1cd974c5cba55b73582da5e1b225a6d0edf029483",
"BootstrapIcons.woff2": "476adf42b40325098fcfa8b36ab3e769186bb4f6ce6a249753e2e1a9c22bf99e",
}
def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
assert fontTools.__version__ == "4.61.1", "Use pinned FontTools 4.61.1."
for name, expected in EXPECTED.items():
assert digest(OUTPUT / name) == expected, name
source = TTFont(OUTPUT / "BootstrapIcons.woff", recalcTimestamp=False)
assert "glyf" in source and "CFF " not in source and "CFF2" not in source
mapping = source.getBestCmap()
outlines = {name: source["glyf"][name].compile(source["glyf"]) for name in source.getGlyphOrder()}
source.flavor = None
target = OUTPUT / "BootstrapIcons.ttf"
source.save(target, reorderTables=True)
verified = TTFont(target, recalcTimestamp=False)
assert mapping == verified.getBestCmap() and len(mapping) == 2050
assert all(data == verified["glyf"][name].compile(verified["glyf"]) for name, data in outlines.items())
files = [{"Path": path.name, "Bytes": path.stat().st_size, "Sha256": digest(path)}
for path in [*(OUTPUT / name for name in EXPECTED), target, OUTPUT / "License.md"]]
manifest = {
"Schema": "XfeWebIconFont1", "Family": "bootstrap-icons", "LibraryVersion": "1.11.3",
"License": "MIT; License.md", "LicenseSource": "https://raw.githubusercontent.com/twbs/icons/v1.11.3/LICENSE",
"SourceUrl": "https://lib.baomitu.com/bootstrap-icons/1.11.3/font/fonts/bootstrap-icons.woff",
"SourceWoff2Url": "https://lib.baomitu.com/bootstrap-icons/1.11.3/font/fonts/bootstrap-icons.woff2",
"SourceSha256": EXPECTED["BootstrapIcons.woff"], "OutputSha256": digest(target),
"GlyphCount": len(verified.getGlyphOrder()), "UnicodeCoverageCount": len(mapping),
"Subset": False, "Outline": "TrueType glyf; complete original outlines and cmap preserved",
"Transformation": {"Tool": "FontTools", "Version": fontTools.__version__,
"Script": "Tools/BrowserCompatibility/BuildWebIconFont.py", "ScriptSha256": digest(Path(__file__).resolve()),
"Operation": "WOFF decompression to sfnt TTF; no outline, cmap, name or metric changes"},
"Files": files,
}
(OUTPUT / "Manifest.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
print(json.dumps(manifest, indent=2))
"""Losslessly unpack the pinned Bootstrap Icons 1.11.3 WOFF into TrueType.
The original website's CSS and JavaScript are unchanged. The complete font is
used by the generic font-family resolver, not a four-icon replacement atlas.
Reproduce with FontTools 4.61.1 on PYTHONPATH; no Brotli is required for WOFF.
"""
import hashlib
import json
from pathlib import Path
import fontTools
from fontTools.ttLib import TTFont
ROOT = Path(__file__).resolve().parents[2]
OUTPUT = ROOT / "assets/fonts/bootstrap-icons-1.11.3"
EXPECTED = {
"BootstrapIcons.woff": "bb1de989b83970f6f4e54de1cd974c5cba55b73582da5e1b225a6d0edf029483",
"BootstrapIcons.woff2": "476adf42b40325098fcfa8b36ab3e769186bb4f6ce6a249753e2e1a9c22bf99e",
}
def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
assert fontTools.__version__ == "4.61.1", "Use pinned FontTools 4.61.1."
for name, expected in EXPECTED.items():
assert digest(OUTPUT / name) == expected, name
source = TTFont(OUTPUT / "BootstrapIcons.woff", recalcTimestamp=False)
assert "glyf" in source and "CFF " not in source and "CFF2" not in source
mapping = source.getBestCmap()
outlines = {name: source["glyf"][name].compile(source["glyf"]) for name in source.getGlyphOrder()}
source.flavor = None
target = OUTPUT / "BootstrapIcons.ttf"
source.save(target, reorderTables=True)
verified = TTFont(target, recalcTimestamp=False)
assert mapping == verified.getBestCmap() and len(mapping) == 2050
assert all(data == verified["glyf"][name].compile(verified["glyf"]) for name, data in outlines.items())
files = [{"Path": path.name, "Bytes": path.stat().st_size, "Sha256": digest(path)}
for path in [*(OUTPUT / name for name in EXPECTED), target, OUTPUT / "License.md"]]
manifest = {
"Schema": "XfeWebIconFont1", "Family": "bootstrap-icons", "LibraryVersion": "1.11.3",
"License": "MIT; License.md", "LicenseSource": "https://raw.githubusercontent.com/twbs/icons/v1.11.3/LICENSE",
"SourceUrl": "https://lib.baomitu.com/bootstrap-icons/1.11.3/font/fonts/bootstrap-icons.woff",
"SourceWoff2Url": "https://lib.baomitu.com/bootstrap-icons/1.11.3/font/fonts/bootstrap-icons.woff2",
"SourceSha256": EXPECTED["BootstrapIcons.woff"], "OutputSha256": digest(target),
"GlyphCount": len(verified.getGlyphOrder()), "UnicodeCoverageCount": len(mapping),
"Subset": False, "Outline": "TrueType glyf; complete original outlines and cmap preserved",
"Transformation": {"Tool": "FontTools", "Version": fontTools.__version__,
"Script": "Tools/BrowserCompatibility/BuildWebIconFont.py", "ScriptSha256": digest(Path(__file__).resolve()),
"Operation": "WOFF decompression to sfnt TTF; no outline, cmap, name or metric changes"},
"Files": files,
}
(OUTPUT / "Manifest.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
print(json.dumps(manifest, indent=2))