-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencodeFonts.js
More file actions
139 lines (110 loc) · 3.66 KB
/
encodeFonts.js
File metadata and controls
139 lines (110 loc) · 3.66 KB
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
const fs = require("fs");
const { base64 } = require("ethers/lib/utils");
const dir = "src/fonts/";
let fileContents = "export const FONTS = {";
function writeFontsFile() {
console.log("Writing fonts file");
const files = fs.readdirSync(dir);
for (let file of files) {
if (!file.endsWith(".woff")) continue;
console.log("encoding", file);
let weight = file.replace(".woff", "").split("-")[1];
if (!weight) continue;
fileContents += `${weight}: "${base64.encode(
fs.readFileSync(dir + file)
)}",`;
}
// Remove trailing comma
fileContents = fileContents.substring(0, fileContents.length - 1);
fileContents += "};";
fs.writeFileSync(dir + "/fonts.ts", fileContents);
}
function writeUnicode() {
const txt = fs.readFileSync(dir + "unicode.txt").toString();
const lines = txt.split("\n").filter((x) => x.startsWith("uni"));
const unicodes = lines
.map((x) => x.split(" ")[0].split("uni")[1])
.filter((x) => x.toLowerCase() !== "ffff" && x !== "0000");
const charMap = unicodes.reduce(
(acc, curr, i) => ({
...acc,
[curr.toLowerCase()]: lines[i].split(" ")[2],
}),
{}
);
const groups = [];
let temp = [];
unicodes
.sort((a, b) => (a > b ? 1 : -1))
.forEach((curr, i) => {
const prev = unicodes[i - 1];
const formatted = `0x${curr.toString(16)}`;
if (i === unicodes.length - 1) {
temp.push(formatted);
groups.push(temp);
} else if (prev && parseInt(curr, 16) - parseInt(prev, 16) > 1) {
groups.push(temp);
temp = [formatted];
} else {
temp.push(formatted);
}
});
// temp = [];
// const bytes1Groups = [];
// unicodes
// .sort((a, b) => (a > b ? 1 : -1))
// .forEach((curr, i) => {
// const prev = unicodes[i - 1];
// const formatted = `0x${curr.toString(16)}`;
// const isBytes1 = formatted.startsWith("0x00");
// if (!isBytes1 || (prev && parseInt(curr, 16) - parseInt(prev, 16) > 1)) {
// if (temp.length) bytes1Groups.push(temp);
// temp = isBytes1 ? [formatted] : [];
// } else if (isBytes1) {
// temp.push(formatted);
// }
// });
// function trimBytes1(char) {
// return `0x${char.toString().split("0x00")[1]}`;
// }
// let bytes1Comparison = "(";
// bytes1Groups.forEach((g, i) => {
// if (g.length === 1) {
// bytes1Comparison += ` char == ${trimBytes1(g)}`;
// } else {
// bytes1Comparison += ` (char >= ${trimBytes1(
// g[0]
// )} && char <= ${trimBytes1(g[g.length - 1])})`;
// }
// if (i < groups.length - 1) bytes1Comparison += " ||";
// });
// bytes1Comparison += ")";
// bytes1Comparison = bytes1Comparison.toLowerCase();
function padBytes3(char) {
return `0x${char.toString().split("0x")[1].padStart(6, "0")}`;
}
const varName = "cp";
let bytes2Comparison = "(";
groups.forEach((g, i) => {
if (g.length === 1) {
bytes2Comparison += ` ${varName} == ${padBytes3(g)}`;
} else {
bytes2Comparison += ` (${varName} >= ${padBytes3(
g[0]
)} && ${varName} <= ${padBytes3(g[g.length - 1])})`;
}
if (i < groups.length - 1) bytes2Comparison += " ||";
});
bytes2Comparison += ")";
bytes2Comparison = bytes2Comparison.toLowerCase();
fs.writeFileSync(
dir + "/unicode.ts",
`export const unicodes = [${unicodes.map((x) => "0x" + x)}];
export const unicodeGroups = [${groups.map((g) => `[${g}]`)}];
export const bytes2Comparison = '${bytes2Comparison}';
export const unicodeNames: Record<string, string> = ${JSON.stringify(charMap)};`
);
console.log(`Wrote ${unicodes.length} unicodes`);
}
writeFontsFile();
writeUnicode();