-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
151 lines (134 loc) · 4.37 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Font Width Calculator</title>
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
<![endif]-->
<style type="text/css">
body {
font-size: 14px;
font-family: sans-serif;
}
#container {
white-space: pre;
}
span {
margin: 0;
padding: 0;
border: none;
}
</style>
</head>
<body>
<div>
I want the widths of all glyphs in the font from this
<label>
url
<input id="fontUrl" type="url" placeholder="e.g. file:// or https://">
</label>
,
<br/>
<label>
with a font size of
<input id="fontSizeValue" type="number" value="14">
</label>
<select id="fontSizeUnit">
<option value="ch">ch</option>
<option value="cm">cm</option>
<option value="ex">ex</option>
<option value="in">in</option>
<option value="mm">mm</option>
<option value="pc">pc</option>
<option value="pt">pt</option>
<option value="px" selected>px</option>
<option value="vh">vh</option>
<option value="vw">vw</option>
</select>
,
<br/>
with char codes
<label>
from
<input id="from" type="number" placeholder="inclusive">
</label>
<label>
to
<input id="to" type="number" placeholder="exclusive">
</label>
.
<br/>
<button id="calculate">Be a dear and get those for me.</button>
<span id="container">
</span>
</div>
<script>
(function iife() {
const range = (start, end) => {
if (start > end) {
throw new Error(`start ${start} cannot be greater than end ${end}.`);
}
const unfilled = Array(end - start).fill(0);
return unfilled.map((_, i) => start + i);
};
const calculate = (start, end) => {
const chars = range(start, end).map((charCode) => {
return String.fromCharCode(charCode);
});
const charNodes = chars.map((char) => {
const el = document.createElement('span');
el.textContent = char;
return el;
});
const container = document.getElementById('container');
Array.from(container.children).forEach(child => {
container.removeChild(child);
});
charNodes.forEach(charNode => {
container.appendChild(charNode);
});
const glyphWidthMapping = Array.from(container.children).reduce((mapping, node) => {
const glyph = node.innerText;
const charCode = glyph.charCodeAt(0);
const width = node.getBoundingClientRect().width;
return {
...mapping,
[charCode]: { glyph, width },
};
}, {});
const maxWidthGlyph = Object.keys(glyphWidthMapping).reduce((max, charCode) => {
const mapping = glyphWidthMapping[charCode];
return mapping.width > max.width ? mapping : max;
}, { glyph: 'initial glyph', width: 0 });
console.log(`Max width glyph: { glyph: ${maxWidthGlyph.glyph}, width: ${maxWidthGlyph.width} }`);
console.log(JSON.stringify(glyphWidthMapping, null, 2));
};
const fontUrlInput = document.getElementById('fontUrl');
const fontSizeValueInput = document.getElementById('fontSizeValue');
const fontSizeUnitInput = document.getElementById('fontSizeUnit');
const fromInput = document.getElementById('from');
const toInput = document.getElementById('to');
const calculateButton = document.getElementById('calculate');
calculateButton.addEventListener('click', () => {
const fontUrl = fontUrlInput.value;
document.styleSheets[0].insertRule(`\
@font-face {\
font-family: "User Defined";\
src: url('${fontUrl}');\
}\
`, document.styleSheets[0].cssRules.length - 1);
const fontSizeValue = fontSizeValueInput.value;
const fontSizeUnit = fontSizeUnitInput.options[fontSizeUnitInput.selectedIndex].value;
document.body.setAttribute('style', `
font-size: ${fontSizeValue}${fontSizeUnit};
font-family: "User Defined";
`);
const from = parseInt(fromInput.value);
const to = parseInt(toInput.value);
setTimeout(() => { calculate(from, to); }, 1000)
});
})();
</script>
</body>
</html>