-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfonts.ts
326 lines (293 loc) · 9.23 KB
/
fonts.ts
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import * as assets from './assets.ts';
import * as dataURIConv from './data_uri_conv.ts';
import {AttributesDefTool} from './def_tool.ts';
import {Attributes, createElement} from './elements.ts';
import {getGlobalOptions} from './global_options.ts';
import {Defs, Piece} from './pieces.ts';
import {OrArrayRest, assert, assertNumber, flatten} from './util.ts';
export type FontType = "woff" | "woff2" | "otf" | "ttf";
export const DEFAULT_FONT_TYPE: FontType = "woff2";
export const FONT_WEIGHT_VALUES = {
thin: 100,
regular: 400,
bold: 700,
};
export type FontWeight = number | keyof typeof FONT_WEIGHT_VALUES;
export function fontWeightValue(weight: FontWeight) {
return typeof weight === "string" ? FONT_WEIGHT_VALUES[weight] : weight;
}
export interface FontAttributes {
readonly italic?: boolean;
readonly bold?: boolean;
readonly weight?: FontWeight;
readonly attributes?: Attributes;
}
function getFontWeight(fontAttributes?: FontAttributes) {
if (fontAttributes?.weight !== undefined)
return fontWeightValue(fontAttributes.weight);
if (fontAttributes?.bold !== undefined)
return FONT_WEIGHT_VALUES[fontAttributes.bold ? "bold" : "regular"];
}
export function attributesFromFontAttributes(
{italic, bold, weight, attributes}: FontAttributes): Attributes {
const attr = {...attributes};
if (italic !== undefined)
attr.fontStyle = italic ? "italic" : "normal";
attr.fontWeight = getFontWeight({bold, weight});
return attr;
}
const SIMPLE_FONT_NAME_REGEXP = /^[a-zA-Z0-9-]+$/;
const SIMPLE_QUOTABLE_FONT_NAME_REGEXP = /^[\w ]+$/;
function quoteFontName(name: string) {
if (SIMPLE_FONT_NAME_REGEXP.test(name))
return name;
if (SIMPLE_QUOTABLE_FONT_NAME_REGEXP.test(name))
return `'${name}'`;
return JSON.stringify(name);
}
export class Font implements AttributesDefTool {
protected constructor(
readonly name: string,
private readonly fontAttributes: FontAttributes | undefined,
protected readonly defs: Defs,
protected readonly fallback: Font | undefined,
private readonly finalFallback: Font | undefined,
) {}
static async fromBlob({name, blob, fontAttributes, finalFallback}: {
name: string,
blob: Blob,
fontAttributes?: FontAttributes,
finalFallback?: Font | Promise<Font> | false,
}) {
return await Font.fromURL({
name,
url: await dataURIConv.fromBlob(blob),
fontAttributes,
finalFallback,
});
}
/**
* Loads font from a URL.
* If it's an external URL, the font is fetched and encoded as a data URI instead.
*/
static async fromURL({name, url, fontAttributes, finalFallback}: {
name: string,
url: string,
fontAttributes?: FontAttributes,
finalFallback?: Font | Promise<Font> | false,
}) {
return await Font.fromStyle({
name,
styleContent: `@font-face {
font-family: ${JSON.stringify(name)};
src: url(${JSON.stringify(url)});
}`,
fontAttributes,
finalFallback,
});
}
/**
* Returns a font loaded from an asset.
*
* Example:
*
* await Font.fromAsset({
* name: "My Font",
* urlAsset: import(`./my_font.woff2`),
* })
*
* See information on assets in _src/assets.ts_.
*/
static async fromAsset({name, urlAsset, fontAttributes, finalFallback}: {
name: string,
urlAsset: assets.ModuleImport<string>,
fontAttributes?: FontAttributes,
finalFallback?: Font | Promise<Font> | false,
}) {
return await Font.fromURL({
name,
url: await assets.url(urlAsset),
fontAttributes,
finalFallback,
});
}
private static async fromStyle({
name,
styleContent,
fontAttributes,
finalFallback = getGlobalOptions().fontFallbackToNotDef ? Font.notDef() : undefined,
}: {
name: string,
styleContent: string,
fontAttributes?: FontAttributes,
finalFallback?: Font | Promise<Font> | false,
}) {
return new Font(
name,
fontAttributes,
Piece.createDefs(await loadStyle(styleContent)),
undefined,
finalFallback ? await finalFallback : undefined,
);
}
/** Returns a font assumed to be available on the system. */
static system(name: string, {fontAttributes}: {
fontAttributes?: FontAttributes,
} = {}) {
return new Font(name, fontAttributes, Piece.EMPTY, undefined, undefined);
}
/**
* Creates a font from [Google Fonts](https://fonts.google.com/).
* The font is fetched and encoded as a data URI.
*
* If the font is not available on Google Fonts with the specified attributes,
* the fetch will fail. It is still possible to use
* [faux attributes](https://fonts.google.com/knowledge/glossary/faux_fake_pseudo_synthesized),
* with:
*
* Font.googleFonts(name).setFontAttributes(fontAttributes)
*/
static async googleFonts(name: string, {fontAttributes, text}: {
fontAttributes?: FontAttributes,
text?: string,
} = {}) {
return await Font.fromStyle({
name,
styleContent: `@import url(${JSON.stringify(googleFontsURL(name, {fontAttributes, text}))});`,
fontAttributes,
});
}
/**
* Returns a font displaying a tofu character for every single character.
* To be used as fallback font to detect missing characters.
* @see https://github.com/adobe-fonts/adobe-notdef
*/
static async notDef(): Promise<Font> {
return await Font.fromURL({
name: "Adobe NotDef",
url: "https://cdn.jsdelivr.net/gh/adobe-fonts/adobe-notdef/AND-Regular.ttf",
finalFallback: false,
});
}
getDefs() {
return this.getStack().reduce((a, f) => a.addDefs(f.defs), Piece.EMPTY).getDefs();
}
asAttributes() {
return {
fontFamily: this.getStack().map(f => quoteFontName(f.name)).join(", "),
...this.fontAttributes && attributesFromFontAttributes(this.fontAttributes),
};
}
getStack() {
const stack = [];
let font: Font | undefined = this;
while (font) {
stack.push(font);
font = font.fallback;
}
if (this.finalFallback)
stack.push(this.finalFallback);
return stack;
}
setFallback(fallback: Font | undefined) {
return new Font(
this.name,
this.fontAttributes,
this.defs,
fallback,
this.finalFallback,
);
}
addFallback(...fallback: OrArrayRest<Font>) {
const fallbackFonts = flatten(fallback);
function withNextFallback(font: Font | undefined): Font | undefined {
if (!fallbackFonts.length)
return font;
if (font)
return font.setFallback(withNextFallback(font.fallback));
return withNextFallback(fallbackFonts.shift());
}
return withNextFallback(this);
}
setFinalFallback(finalFallback: Font | undefined) {
return new Font(
this.name,
this.fontAttributes,
this.defs,
this.fallback,
finalFallback,
);
}
clearFinalFallback() {
return this.setFinalFallback(undefined);
}
setFontAttributes(fontAttributes: FontAttributes) {
return new Font(
this.name,
{...this.setFontAttributes, ...fontAttributes},
this.defs,
this.fallback,
this.finalFallback,
);
}
}
export function googleFontsURL(name: string, {fontAttributes, text}: {
fontAttributes?: FontAttributes,
text?: string,
} = {}) {
const keys = [];
const vals = [];
if (fontAttributes?.italic) {
keys.push("ital");
vals.push(1);
}
const weight = getFontWeight(fontAttributes)
if (weight !== undefined) {
keys.push("wght");
vals.push(weight);
}
return `https://fonts.googleapis.com/css2?family=${encodeURIComponent(name)}` +
(keys.length ? `:${keys.join(",")}@${vals.join(",")}` : "") +
(text ? `&text=${encodeURIComponent(text)}` : "");
}
const URL_REGEXP = /(?<import>@import\s+)?\burl\((?<q>['"]?)(?<url>.+?)\k<q>\)(?<semi>;)?/g;
const stylesCache = new Map<string, string>();
/** Converts the URLs in the style content to data URIs. */
async function changeToDataURIs(styleContent: string): Promise<string> {
const cached = stylesCache.get(styleContent);
if (cached !== undefined)
return cached;
const urls = [...styleContent.matchAll(URL_REGEXP)].map(async (mat) => {
const {url} = assert(mat.groups);
let blob;
try {
blob = await (await fetch(url)).blob();
} catch (e) {
throw new Error(`Failed to fetch: ${url}\n${e}`, {cause: e});
}
if (blob.type === "text/css" && mat.groups?.import)
// If it was an import of another style, just process and paste that style.
return {mat, text: await changeToDataURIs(await blob.text()) + "\n"};
const dataURI = await dataURIConv.fromBlob(blob);
return {mat, text: `url(${JSON.stringify(dataURI)})${mat.groups?.semi ?? ""}`};
});
const out = []
let ind = 0;
for (const {mat, text} of await Promise.all(urls)) {
out.push(styleContent.slice(ind, mat.index));
out.push(text);
ind = assertNumber(mat.index) + mat[0].length;
}
out.push(styleContent.slice(ind));
const result = out.join("");
stylesCache.set(styleContent, result);
return result;
}
async function loadStyle(styleContent: string) {
// TODO: Find a reliable way of waiting for the font to load. Right now a page reload is
// sometimes necessary, because text elements are measured before the font is loaded.
return createElement({
tagName: "style",
children: await changeToDataURIs(styleContent),
});
}