-
Notifications
You must be signed in to change notification settings - Fork 16
/
css2-next.mjs
166 lines (139 loc) · 4.65 KB
/
css2-next.mjs
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
export const css2 = (request, reply, data, domain, subsets) => {
try {
const { family: families, display, text } = request.query;
if (!families) {
throw { statusCode: 500, message: "Wrong request" };
}
const familyList = Array.isArray(families) ? families : [families];
const payload = [];
if (text) {
payload.push(`/* Optimized for text: ${decodeURIComponent(text)} */`);
}
for (const family of familyList) {
const { weights, styles, dashFamily, familyName } = parseFamily(family);
const properties = getFontProperties(dashFamily, data);
if (!properties?.subsets) {
return reply
.type('text/html')
.send('This font is not available. <br>If you think this is a bug, <a href="https://docs.coollabs.io/contact">let us know</a>.');
}
const sortedWeights = weights.sort((a, b) => parseInt(a) - parseInt(b));
for (const style of styles) {
for (const weight of sortedWeights) {
for (const subset of properties.subsets) {
const css = generateFontFace({
subset,
familyName,
style,
weightValue: weight,
display,
domain,
dashFamily,
subsets,
text
});
payload.push(css);
}
}
}
}
if (payload.length === 0) {
throw 'Wrong request. Please <a href="https://docs.coollabs.io/contact">contact us</a> if you think this is a bug.';
}
return reply
.header("content-type", "text/css")
.send(payload.join(" ").trim());
} catch (error) {
if (typeof error === "string") {
reply.header("content-type", "text/html");
throw error;
}
throw { statusCode: 500, message: error.message };
}
};
function parseFamily(family) {
let weights = ["400"];
let styles = new Set(["normal"]);
let dashFamily = family.toLowerCase().replace(/ /g, "-");
const familyName = family.split(":")[0];
if (family.includes(":")) {
const [_, params] = family.split(":");
if (params.startsWith("wght@")) {
const weightParam = params.split("@")[1];
const weightSpecs = weightParam.split(";");
weights = new Set();
for (const spec of weightSpecs) {
if (spec.includes("..")) {
const [start, end] = spec.split("..").map(Number);
const standardWeights = [100, 200, 300, 400, 500, 600, 700, 800, 900];
standardWeights.forEach(weight => {
if (weight >= start && weight <= end) {
weights.add(weight.toString());
}
});
} else {
weights.add(spec);
}
}
weights = Array.from(weights);
}
else if (params.includes("@")) {
const [axes, values] = params.split("@");
const valuesList = values.split(";");
const axisList = axes.split(",");
styles.clear();
weights = new Set();
for (const value of valuesList) {
const axisValues = value.split(",");
for (let i = 0; i < axisList.length; i++) {
const axisValue = axisValues[i];
if (axisList[i] === "ital") {
styles.add(axisValue === "1" ? "italic" : "normal");
}
if (axisList[i] === "wght" && axisValue) {
if (axisValue.includes("..")) {
const [start, end] = axisValue.split("..").map(Number);
const standardWeights = [100, 200, 300, 400, 500, 600, 700, 800, 900];
standardWeights.forEach(weight => {
if (weight >= start && weight <= end) {
weights.add(weight.toString());
}
});
} else {
weights.add(axisValue);
}
}
}
}
}
dashFamily = familyName.toLowerCase().replace(/ /g, "-");
}
if (dashFamily === 'source-sans-pro') {
dashFamily = 'source-sans-3';
}
return { weights: Array.from(weights), styles: Array.from(styles), dashFamily, familyName };
}
function getFontProperties(dashFamily, data) {
return data.find((f) => f.id === dashFamily);
}
function generateFontFace({ subset, familyName, style, weightValue, display, domain, dashFamily, subsets, text }) {
let css = `
/* ${subset} */
@font-face {
font-family: '${familyName}';
font-style: ${style};
font-weight: ${weightValue};`;
if (display) {
css += `
font-display: ${display};`;
}
let url = `https://${domain}/${dashFamily}/${style}/${weightValue}.woff2`;
if (text) {
url += `?text=${encodeURIComponent(text)}`;
}
css += `
src: url(${url}) format('woff2');
unicode-range: ${subsets[subset]};
}`;
return css;
}