@compatifont/fonts 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +46 -2
- package/dist/catalog.generated.d.ts +3 -0
- package/dist/catalog.generated.js +634 -0
- package/dist/catalog.json +638 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +107 -0
- package/package.json +51 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { CATALOG, PACKAGE_VERSION } from "./catalog.generated.js";
|
|
2
|
+
export { CATALOG, PACKAGE_VERSION };
|
|
3
|
+
/** Default git tag used for CDN font downloads. */
|
|
4
|
+
export const DEFAULT_REF = `v${PACKAGE_VERSION}`;
|
|
5
|
+
/** Default jsDelivr base URL serving verified Compatifont `.ttf` files. */
|
|
6
|
+
export const DEFAULT_BASE_URL = `https://cdn.jsdelivr.net/gh/osolmaz/compatifont@${DEFAULT_REF}/fonts`;
|
|
7
|
+
const SYMBOL_ALIASES = [
|
|
8
|
+
"symbol",
|
|
9
|
+
"wingdings",
|
|
10
|
+
"wingdings 2",
|
|
11
|
+
"wingdings 3",
|
|
12
|
+
"webdings",
|
|
13
|
+
];
|
|
14
|
+
function normalizeName(name) {
|
|
15
|
+
return name
|
|
16
|
+
.normalize("NFD")
|
|
17
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
18
|
+
.trim()
|
|
19
|
+
.toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
const INDEX = (() => {
|
|
22
|
+
const map = new Map();
|
|
23
|
+
for (const family of CATALOG) {
|
|
24
|
+
map.set(normalizeName(family.slug), family);
|
|
25
|
+
map.set(normalizeName(family.familyName), family);
|
|
26
|
+
for (const target of family.compatibleWith) {
|
|
27
|
+
const key = normalizeName(target.familyName);
|
|
28
|
+
if (!map.has(key)) {
|
|
29
|
+
map.set(key, family);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (family.slug === "noto-symbols-cf") {
|
|
33
|
+
for (const alias of SYMBOL_ALIASES) {
|
|
34
|
+
if (!map.has(alias)) {
|
|
35
|
+
map.set(alias, family);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return map;
|
|
41
|
+
})();
|
|
42
|
+
/**
|
|
43
|
+
* Resolves a Compatifont family name (`"Celeborn CF"`, `"celeborn-cf"`), accent-folded spelling
|
|
44
|
+
* (`"Curunir CF"`), or compatible Microsoft Office family name (`"Calibri"`, `"Arial"`,
|
|
45
|
+
* `"Times New Roman"`, `"Cambria"`, `"Aptos"`, `"Symbol"`, etc.) to its catalog entry.
|
|
46
|
+
*/
|
|
47
|
+
export function resolveFamily(name) {
|
|
48
|
+
return INDEX.get(normalizeName(name));
|
|
49
|
+
}
|
|
50
|
+
/** Returns the full download URL for one `.ttf` face. */
|
|
51
|
+
export function fontUrl(family, file, baseUrl = DEFAULT_BASE_URL) {
|
|
52
|
+
const trimmed = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
53
|
+
return `${trimmed}/${family.slug}/files/${file.filename}`;
|
|
54
|
+
}
|
|
55
|
+
async function sha256Hex(bytes) {
|
|
56
|
+
const copy = new Uint8Array(bytes.byteLength);
|
|
57
|
+
copy.set(bytes);
|
|
58
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", copy.buffer);
|
|
59
|
+
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Creates an async font fetcher directly compatible with `@f-office/wasm`'s `loadFontsFor`.
|
|
63
|
+
*
|
|
64
|
+
* Unknown families return `null` so callers can fall through to other font sources.
|
|
65
|
+
*/
|
|
66
|
+
export function fontFetcher(options = {}) {
|
|
67
|
+
const baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
|
|
68
|
+
const verify = options.verify ?? true;
|
|
69
|
+
const useCache = options.cache ?? true;
|
|
70
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
71
|
+
const cache = new Map();
|
|
72
|
+
const downloadFamily = async (family) => {
|
|
73
|
+
return Promise.all(family.files.map(async (file) => {
|
|
74
|
+
const url = fontUrl(family, file, baseUrl);
|
|
75
|
+
const response = await fetchImpl(url);
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
throw new Error(`Failed to fetch Compatifont face ${file.filename} from ${url}: HTTP ${response.status}`);
|
|
78
|
+
}
|
|
79
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
80
|
+
if (verify) {
|
|
81
|
+
const actual = await sha256Hex(bytes);
|
|
82
|
+
if (actual !== file.sha256) {
|
|
83
|
+
throw new Error(`SHA-256 mismatch for ${file.filename}: expected ${file.sha256}, got ${actual}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return bytes;
|
|
87
|
+
}));
|
|
88
|
+
};
|
|
89
|
+
return async (name) => {
|
|
90
|
+
const family = resolveFamily(name);
|
|
91
|
+
if (!family) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
if (!useCache) {
|
|
95
|
+
return downloadFamily(family);
|
|
96
|
+
}
|
|
97
|
+
let pending = cache.get(family.slug);
|
|
98
|
+
if (!pending) {
|
|
99
|
+
pending = downloadFamily(family).catch((err) => {
|
|
100
|
+
cache.delete(family.slug);
|
|
101
|
+
throw err;
|
|
102
|
+
});
|
|
103
|
+
cache.set(family.slug, pending);
|
|
104
|
+
}
|
|
105
|
+
return pending;
|
|
106
|
+
};
|
|
107
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@compatifont/fonts",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Catalog and SHA-256-verified fetcher for Compatifont open fonts (OFL-1.1), metric-compatible with Microsoft Office fonts.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/osolmaz/compatifont.git",
|
|
11
|
+
"directory": "packages/fonts"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/osolmaz/compatifont",
|
|
14
|
+
"bugs": "https://github.com/osolmaz/compatifont/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"fonts",
|
|
17
|
+
"opentype",
|
|
18
|
+
"truetype",
|
|
19
|
+
"metrics",
|
|
20
|
+
"office",
|
|
21
|
+
"docx",
|
|
22
|
+
"calibri",
|
|
23
|
+
"arial",
|
|
24
|
+
"times-new-roman",
|
|
25
|
+
"cambria",
|
|
26
|
+
"aptos",
|
|
27
|
+
"ofl"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20"
|
|
31
|
+
},
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"default": "./dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./catalog.json": "./dist/catalog.json"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"README.md",
|
|
42
|
+
"LICENSE"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "node scripts/build.mjs",
|
|
46
|
+
"test": "node scripts/smoke.mjs",
|
|
47
|
+
"typecheck": "tsc --noEmit"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "^22.15.0",
|
|
51
|
+
"typescript": "^5.8.3"
|
|
52
|
+
}
|
|
53
|
+
}
|