-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
202 lines (174 loc) · 5.77 KB
/
script.js
File metadata and controls
202 lines (174 loc) · 5.77 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
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
// Parasync: script.js
import { SamsaFont, SamsaBuffer } from "./samsa-core.js";
// shorthands
function Q (selector, root=document) {
return root.querySelector(selector);
}
function QA (selector, root=document) {
return root.querySelectorAll(selector);
}
function EL (tag, attrs) {
let el = document.createElement(tag);
if (attrs)
Object.assign(el, attrs);
return el;
}
function updateFvs (initialize) {
let fvsA = [], fvsS;
QA("#sliders li").forEach(li => {
let elRange = li.querySelector("input[type=range]");
let elText = li.querySelector("input[type=text]");
let tag = li.querySelector("label").textContent;
if (initialize)
elRange.value = axes[tag].default;
fvsA.push(`"${tag}" ${elRange.value}`);
elText.value = elRange.value;
});
// go thru each font
for (const font of fonts) {
const thisFvsA = [...fvsA];
// set any axes other than the editable parametric axes to their default values, to avoid the browser overriding defaults
if (font?.fvar?.axes) {
for (const axis of font.fvar.axes) {
if (!Object.keys(axes).includes(axis.axisTag)) {
thisFvsA.push(`"${axis.axisTag}" ${axis.defaultValue}`);
}
}
// set all font sample elements to the new FVS
const thisFvsS = thisFvsA.join();
for (const sampleEl of QA(".sample", font.node)) {
sampleEl.style.fontVariationSettings = thisFvsS;
}
}
}
}
function newFontPanel(font) {
let fontBox = Q(".panel.fontbox").cloneNode(true); // deep clone
font.node = fontBox;
fontBox.innerHTML = fontBox.innerHTML.replace("$FONTNAME$", font.name);
// get the fvar table via Samsa
if (typeof font.src == "string") {
let match = font.src.match(/[^/]+$/);
font.filename = match[0] ?? "unknown";
fetch(font.src)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
const samsaFont = new SamsaFont(new SamsaBuffer(arrayBuffer));
font.fvar = samsaFont.fvar;
updateFvs(true);
});
}
else {
const samsaFont = new SamsaFont(new SamsaBuffer(font.src));
font.fvar = samsaFont.fvar;
updateFvs(true);
}
fontBox.innerHTML = fontBox.innerHTML.replace("$FILENAME$", font.filename);
fontBox.style.display = "inline-block";
let family = font.family || "DEFAULT-" + font.name;
// load the font as a webfont
let fontSource;
if (typeof font.src == "string")
fontSource = `url(${font.src})`;
else if (font.src instanceof ArrayBuffer)
fontSource = font.src;
else
console.error("Unknown font source type", font.src);
if (fontSource) {
let webfontFace = new FontFace(family, fontSource);
webfontFace.load().then(webfontFace => {
document.fonts.add(webfontFace);
for (let sampleEl of fontBox.querySelectorAll(".sample"))
sampleEl.style.fontFamily = `${webfontFace.family},AdobeBlank`;
updateFvs(true);
});
Q("#container").insertBefore(fontBox, Q(".panel.dragdrop"));
}
// reset any parametric axes that are in this font to this font’s defaults
Q(".reset", fontBox).onclick = () => {
if (font.fvar) {
QA(".axis-record").forEach (axisEl => {
const axisTag = axisEl.querySelector("label").textContent;
const axis = font.fvar.axes.find(a => a.axisTag == axisTag);
if (axis) {
Q("input[type=range]", axisEl).value =
Q("input[type=text]", axisEl).value = axis.defaultValue;;
}
});
updateFvs();
}
};
}
// initialize things
let dragdropId = 0;
let fonts = [
{ name: "Amstelvar", src: "fonts/AmstelvarA2-Roman_avar2.ttf" },
{ name: "RobotoFlex", src: "fonts/RobotoFlex[GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght].ttf" },
{ name: "ScienceGothic", src: "fonts/ScienceGothic[YOPQ,wdth,wght,slnt].ttf" },
{ name: "CaliperFlex", src: "fonts/CaliperFlex-VF.ttf" },
{ name: "Squeak", src: "fonts/Squeak-VF.ttf" },
{ name: "Mihuri", src: "fonts/Mihuri_Para.ttf" },
];
// these are the editable parametric axes
const axes = {
XOPQ: { min: 1, max: 1000, default: 110 },
XTRA: { min: 1, max: 1000, default: 340 },
YOPQ: { min: 1, max: 1000, default: 75 },
YTUC: { min: 1, max: 1000, default: 640 },
YTSE: { min: 1, max: 1000, default: 90 },
YOSE: { min: 1, max: 1000, default: 90 },
YTLC: { min: 1, max: 1000, default: 500 },
YTAS: { min: 1, max: 1000, default: 740 },
YTDE: { min: -1000, max: 0, default: -200 },
};
// add axis ui
Object.keys(axes).forEach(tag => {
let axis = axes[tag];
let row = EL("li");
let label = EL("label", {textContent: tag});
let input = EL("input", {type: "range", min: axis.min, max: axis.max, value: 5});
let numeric = EL("input", {type: "text", readOnly: true});
input.addEventListener("input", e => updateFvs() );
row.classList.add("axis-record");
row.append(label, input, numeric);
Q("#sliders").append(row);
});
// add fontbox panels for all the default fonts
for (let font of fonts)
newFontPanel(font);
// handle reset
Q(".panel.title .reset").onclick = () => {
updateFvs(true);
};
// handle text entry in the editable fields: copy it to all other fields of that size
for (let sample of QA(".sample")) {
sample.addEventListener("input", e => {
let size;
if (e.target.classList.contains("small"))
size = "small";
else if (e.target.classList.contains("medium"))
size = "medium";
else if (e.target.classList.contains("large"))
size = "large";
for (let el of QA(`.${size}`)) {
if (el != e.target)
el.textContent = e.target.textContent;
}
});
}
// handle dragdrop (multiple is ok)
Q("#dropzone").onchange = e => {
for (let file of e.target.files) {
const reader = new FileReader();
reader.onload = function (e) {
let family = `DRAGDROP-${dragdropId}`;
let name = file.name.replace(/\.ttf$/, "");
let font = {name: name, family: family, filename: file.name, src: this.result}; // this.result is an ArrayBuffer
newFontPanel(font);
fonts.push(font);
dragdropId++;
};
reader.readAsArrayBuffer(file);
}
Q(".panel.dragdrop form").reset();
}