-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.ts
81 lines (69 loc) · 2.07 KB
/
common.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
// deno-lint-ignore no-explicit-any
function assertNotNullOrUndef(...x: any[]) {
for (const param of x) {
if (param === undefined || param === null) {
throw new TypeError("Cannot be null or undefined");
}
}
}
const headers = new Headers({
"Accept":
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"User-Agent": "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116",
});
export class FontFace {
readonly family: string;
readonly url: string;
readonly italic: boolean;
readonly weight: number;
readonly script: string;
static fromCssDecl = (fontFaceDecl: string): FontFace => {
const scriptRegex = /\/\*\s([^\s]*)\s\*\//g;
const urlRegex = /src:\s*url\(([^\(]*)\)/g;
const familyRegex = /font-family:\s*'(.*)'/g;
const styleRegex = /font-style:\s*(\w+)/g;
const weightRegex = /font-weight:\s*(\d+)/g;
const script = scriptRegex.exec(fontFaceDecl)?.pop();
const url = urlRegex.exec(fontFaceDecl)?.pop();
const family = familyRegex.exec(fontFaceDecl)?.pop();
const style = styleRegex.exec(fontFaceDecl)?.pop();
const weight = weightRegex.exec(fontFaceDecl)?.pop();
assertNotNullOrUndef(family, style, weight, url, script);
const ret = new FontFace(
family!,
url!,
style === "italic",
parseInt(weight!),
script!,
);
return ret;
};
constructor(
family: string,
url: string,
italic: boolean,
weight: number,
script: string,
) {
this.family = family;
this.url = url;
this.italic = italic;
this.weight = weight;
this.script = script;
}
get fileName(): string {
return `${this.family.replace(/ /g, "")}${
this.italic ? "Italic" : ""
}${this.weight}${this.script}.woff2`;
}
async fetchFont(): Promise<Blob> {
const response = await fetch(new URL(this.url), {
headers: headers,
});
return response.blob();
}
}
export async function fetchCss(url: string | URL | Request): Promise<string> {
const raw = await fetch(url, { headers: headers });
return await raw.text();
}