Font catalog
Browse files- README.md +1 -0
- catalog.db +3 -0
- catalog.db.gz +3 -0
- catalog.json +3 -0
- catalog_manifest.json +22 -0
- catalog_schema.sql +18 -0
- download_font.py +152 -0
README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Font
|
catalog.db
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:14784001628833d8e462d8f092feb6991815c684653408ad22ec81d00a6d391b
|
| 3 |
+
size 79814656
|
catalog.db.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e4fb7b2dd84d78eb81b77adafd2d63a2bc635762e904c51e96e2068ad89b5c79
|
| 3 |
+
size 23732907
|
catalog.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fd41ee0a5295ec6aa66d17964616475d12eaa2c06ffff8e6b134356620831d33
|
| 3 |
+
size 126628208
|
catalog_manifest.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"schema_version": 1,
|
| 3 |
+
"title": "Font",
|
| 4 |
+
"kind": "font",
|
| 5 |
+
"repository": "Visecall/font",
|
| 6 |
+
"asset_revision": "8179534f88e816963ac8dfd46e1eb7a66b8d0f76",
|
| 7 |
+
"asset_base_url": "https://huggingface.co/datasets/Visecall/font/resolve/8179534f88e816963ac8dfd46e1eb7a66b8d0f76/",
|
| 8 |
+
"fonts": 6275,
|
| 9 |
+
"files": 2635,
|
| 10 |
+
"faces": 2635,
|
| 11 |
+
"scalable": 6257,
|
| 12 |
+
"path_fonts": 1,
|
| 13 |
+
"atlas_fonts": 17,
|
| 14 |
+
"bytes": 3476269890,
|
| 15 |
+
"catalog_bytes": 79814656,
|
| 16 |
+
"catalog_sha256": "14784001628833d8e462d8f092feb6991815c684653408ad22ec81d00a6d391b",
|
| 17 |
+
"compressed_catalog": {
|
| 18 |
+
"bytes": 23732907,
|
| 19 |
+
"sha256": "e4fb7b2dd84d78eb81b77adafd2d63a2bc635762e904c51e96e2068ad89b5c79",
|
| 20 |
+
"url": "https://huggingface.co/datasets/Visecall/font/resolve/main/catalog.db.gz"
|
| 21 |
+
}
|
| 22 |
+
}
|
catalog_schema.sql
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
PRAGMA foreign_keys=ON;
|
| 3 |
+
CREATE TABLE meta(key TEXT PRIMARY KEY,value TEXT NOT NULL);
|
| 4 |
+
CREATE TABLE files(sha256 TEXT PRIMARY KEY,path TEXT NOT NULL,format TEXT NOT NULL,bytes INTEGER NOT NULL,
|
| 5 |
+
download_url TEXT NOT NULL,metadata_json TEXT NOT NULL);
|
| 6 |
+
CREATE TABLE fonts(id TEXT PRIMARY KEY,name TEXT NOT NULL,family TEXT NOT NULL,style TEXT NOT NULL,
|
| 7 |
+
weight INTEGER NOT NULL,italic INTEGER NOT NULL,kind TEXT NOT NULL,position INTEGER NOT NULL,
|
| 8 |
+
primary_file TEXT NOT NULL REFERENCES files(sha256),primary_face INTEGER NOT NULL,metadata_json TEXT NOT NULL);
|
| 9 |
+
CREATE TABLE font_files(font_id TEXT REFERENCES fonts(id),sha256 TEXT REFERENCES files(sha256),
|
| 10 |
+
position INTEGER NOT NULL,PRIMARY KEY(font_id,sha256));
|
| 11 |
+
CREATE TABLE faces(sha256 TEXT REFERENCES files(sha256),face_index INTEGER NOT NULL,family TEXT NOT NULL,
|
| 12 |
+
style TEXT NOT NULL,weight INTEGER NOT NULL,italic INTEGER NOT NULL,glyphs INTEGER NOT NULL,
|
| 13 |
+
languages_json TEXT NOT NULL,unicode_ranges_json TEXT NOT NULL,metadata_json TEXT NOT NULL,
|
| 14 |
+
PRIMARY KEY(sha256,face_index));
|
| 15 |
+
CREATE INDEX font_names ON fonts(name COLLATE NOCASE);
|
| 16 |
+
CREATE INDEX font_families ON fonts(family COLLATE NOCASE,weight,italic);
|
| 17 |
+
CREATE VIEW available_fonts AS SELECT fonts.*,files.download_url,files.format,files.bytes,files.sha256
|
| 18 |
+
FROM fonts JOIN files ON files.sha256=fonts.primary_file;
|
download_font.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Download a Font catalog item with SHA-256 verification."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
import gzip
|
| 6 |
+
import hashlib
|
| 7 |
+
import json
|
| 8 |
+
from pathlib import Path, PurePosixPath
|
| 9 |
+
import re
|
| 10 |
+
import shutil
|
| 11 |
+
import sqlite3
|
| 12 |
+
import time
|
| 13 |
+
from urllib.request import Request, urlopen
|
| 14 |
+
import uuid
|
| 15 |
+
import zipfile
|
| 16 |
+
|
| 17 |
+
REPO = 'Visecall/font'
|
| 18 |
+
BASE = f'https://huggingface.co/datasets/{REPO}/resolve/'
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def digest(path):
|
| 22 |
+
h = hashlib.sha256()
|
| 23 |
+
with path.open('rb') as stream:
|
| 24 |
+
for chunk in iter(lambda: stream.read(1024 * 1024), b''):
|
| 25 |
+
h.update(chunk)
|
| 26 |
+
return h.hexdigest()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def download(url, target, sha=None, size=None):
|
| 30 |
+
if not url.startswith(BASE):
|
| 31 |
+
raise ValueError('URL outside the Font repository')
|
| 32 |
+
if sha is not None and not re.fullmatch('[0-9a-f]{64}', sha):
|
| 33 |
+
raise ValueError('Invalid SHA-256')
|
| 34 |
+
if target.is_file() and sha and target.stat().st_size == size and digest(target) == sha:
|
| 35 |
+
return target
|
| 36 |
+
target.parent.mkdir(parents=True, exist_ok=True)
|
| 37 |
+
temporary = target.with_name(target.name + '.' + uuid.uuid4().hex + '.part')
|
| 38 |
+
try:
|
| 39 |
+
for attempt in range(4):
|
| 40 |
+
try:
|
| 41 |
+
with urlopen(Request(url, headers={'User-Agent': 'Font-Catalog/1'}), timeout=120) as source, temporary.open('wb') as sink:
|
| 42 |
+
shutil.copyfileobj(source, sink, 1024 * 1024)
|
| 43 |
+
if size is not None and temporary.stat().st_size != size:
|
| 44 |
+
raise ValueError('Font size mismatch')
|
| 45 |
+
if sha and digest(temporary) != sha:
|
| 46 |
+
raise ValueError('Font SHA-256 mismatch')
|
| 47 |
+
temporary.replace(target)
|
| 48 |
+
return target
|
| 49 |
+
except Exception:
|
| 50 |
+
if attempt == 3:
|
| 51 |
+
raise
|
| 52 |
+
time.sleep(attempt + 1)
|
| 53 |
+
finally:
|
| 54 |
+
temporary.unlink(missing_ok=True)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def refresh_catalog(output):
|
| 58 |
+
manifest = output / 'catalog_manifest.json'
|
| 59 |
+
download(BASE + 'main/catalog_manifest.json', manifest)
|
| 60 |
+
value = json.loads(manifest.read_text(encoding='utf-8'))
|
| 61 |
+
if value['schema_version'] != 1 or value['kind'] != 'font' or value['repository'] != REPO:
|
| 62 |
+
raise ValueError('Expected Font catalog version 1')
|
| 63 |
+
catalog = output / ('catalog-' + value['catalog_sha256'] + '.db')
|
| 64 |
+
if catalog.is_file() and digest(catalog) == value['catalog_sha256']:
|
| 65 |
+
return catalog
|
| 66 |
+
info = value['compressed_catalog']
|
| 67 |
+
archive = download(info['url'], output / 'catalog.db.gz', info['sha256'], info['bytes'])
|
| 68 |
+
temporary = catalog.with_suffix('.pending')
|
| 69 |
+
try:
|
| 70 |
+
with gzip.open(archive, 'rb') as source, temporary.open('wb') as sink:
|
| 71 |
+
shutil.copyfileobj(source, sink)
|
| 72 |
+
if temporary.stat().st_size != value['catalog_bytes'] or digest(temporary) != value['catalog_sha256']:
|
| 73 |
+
raise ValueError('Expanded Font catalog checksum mismatch')
|
| 74 |
+
temporary.replace(catalog)
|
| 75 |
+
finally:
|
| 76 |
+
temporary.unlink(missing_ok=True)
|
| 77 |
+
return catalog
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def install(font_id, catalog, output):
|
| 81 |
+
db = sqlite3.connect(catalog.resolve().as_uri() + '?mode=ro', uri=True)
|
| 82 |
+
try:
|
| 83 |
+
meta = dict(db.execute('SELECT key,value FROM meta'))
|
| 84 |
+
if meta['schema_version'] != '1' or meta['kind'] != 'font' or meta['repository'] != REPO:
|
| 85 |
+
raise ValueError('Expected Font catalog version 1')
|
| 86 |
+
row = db.execute('SELECT metadata_json FROM fonts WHERE id=?', (font_id,)).fetchone()
|
| 87 |
+
if row is None:
|
| 88 |
+
raise ValueError('Unknown font ID')
|
| 89 |
+
font = json.loads(row[0])
|
| 90 |
+
paths, roots = [], []
|
| 91 |
+
for sha in font['files']:
|
| 92 |
+
row = db.execute('SELECT path,bytes,download_url FROM files WHERE sha256=?', (sha,)).fetchone()
|
| 93 |
+
rel, size, url = row
|
| 94 |
+
parts = PurePosixPath(rel).parts
|
| 95 |
+
if (not parts or '\\' in rel or ':' in rel or rel.startswith('/')
|
| 96 |
+
or any(p in ('', '.', '..') for p in rel.split('/'))):
|
| 97 |
+
raise ValueError('Unsafe font path')
|
| 98 |
+
target = output.joinpath(*parts)
|
| 99 |
+
paths.append(download(url, target, sha, size).resolve().as_posix())
|
| 100 |
+
if font['kind'] == 'atlas':
|
| 101 |
+
roots.append(install_atlas(target, output / 'assets' / sha, sha).resolve().as_posix())
|
| 102 |
+
return dict(id=font_id, name=font['name'], family=font['family'], kind=font['kind'], files=paths, asset_roots=roots)
|
| 103 |
+
finally:
|
| 104 |
+
db.close()
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def install_atlas(archive, destination, sha):
|
| 108 |
+
marker = destination / '.font-installed.json'
|
| 109 |
+
if marker.is_file() and json.loads(marker.read_text(encoding='utf-8')).get('sha256') == sha:
|
| 110 |
+
return destination
|
| 111 |
+
if destination.exists():
|
| 112 |
+
raise FileExistsError('Incomplete font destination: ' + str(destination))
|
| 113 |
+
staging = destination.with_name(destination.name[:16] + '.' + uuid.uuid4().hex[:12] + '.installing')
|
| 114 |
+
staging.mkdir(parents=True)
|
| 115 |
+
try:
|
| 116 |
+
with zipfile.ZipFile(archive) as z:
|
| 117 |
+
if len(z.infolist()) > 10000 or sum(i.file_size for i in z.infolist()) > 1024**3:
|
| 118 |
+
raise ValueError('Font atlas exceeds extraction limits')
|
| 119 |
+
seen = set()
|
| 120 |
+
for info in z.infolist():
|
| 121 |
+
name = info.filename
|
| 122 |
+
parts = name.split('/')
|
| 123 |
+
if (not name or name.startswith('/') or '\\' in name or ':' in name
|
| 124 |
+
or any(p in ('', '.', '..') or p.endswith((' ', '.')) for p in parts)
|
| 125 |
+
or name.casefold() in seen or ((info.external_attr >> 16) & 0o170000) == 0o120000):
|
| 126 |
+
raise ValueError('Unsafe font atlas member')
|
| 127 |
+
seen.add(name.casefold())
|
| 128 |
+
target = staging.joinpath(*parts)
|
| 129 |
+
target.parent.mkdir(parents=True, exist_ok=True)
|
| 130 |
+
with z.open(info) as source, target.open('xb') as sink:
|
| 131 |
+
shutil.copyfileobj(source, sink)
|
| 132 |
+
if json.loads((staging / 'content.json').read_text(encoding='utf-8'))['type'] != 'preRenderedFont':
|
| 133 |
+
raise ValueError('Invalid font atlas')
|
| 134 |
+
(staging / marker.name).write_text(json.dumps({'sha256': sha}), encoding='utf-8')
|
| 135 |
+
staging.replace(destination)
|
| 136 |
+
return destination
|
| 137 |
+
finally:
|
| 138 |
+
if staging.exists():
|
| 139 |
+
shutil.rmtree(staging)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
if __name__ == '__main__':
|
| 143 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 144 |
+
parser.add_argument('id')
|
| 145 |
+
parser.add_argument('--output', type=Path, default=Path('Fonts'))
|
| 146 |
+
parser.add_argument('--catalog', type=Path)
|
| 147 |
+
args = parser.parse_args()
|
| 148 |
+
if not re.fullmatch('[0-9]{1,32}', args.id):
|
| 149 |
+
parser.error('Invalid font ID')
|
| 150 |
+
args.output.mkdir(parents=True, exist_ok=True)
|
| 151 |
+
catalog = args.catalog or refresh_catalog(args.output)
|
| 152 |
+
print(json.dumps(install(args.id, catalog, args.output), ensure_ascii=False))
|