-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.functions.js
130 lines (121 loc) · 4.34 KB
/
main.functions.js
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
async function loadLocalFonts(callerBtn) {
let fonts = await getFontData();
if (fonts.length == 0) {
return;
}
localFontSelect.lastElementChild.remove();
Object.keys(fonts).forEach(family => {
let selected = "";
if (family == defaultFontFamily) {
selected = " selected";
}
localFontSelect.innerHTML += `
<option${selected} style="font-family: '${family}'">${family}</option>
`;
});
callerBtn.remove();
}
function changeFontFamily() {
let value = localFontSelect.value;
if (value == localFontSelect.dataset.placeholder) {
value = defaultFontFamily;
}
document.body.style.setProperty("--font-family", value);
updateCharWidthRatiosDict();
printTextSize();
}
function printUppercaseLetters() {
uppercaseContainer.innerHTML = "";
uppercaseLettersBox.value.split("").forEach(letter => {
uppercaseContainer.innerHTML += `<div class="bg-white shadow">${letter}</div>`;
});
updateCharWidthRatiosDict(uppercaseContainer);
}
function printLowercaseLetters() {
lowercaseContainer.innerHTML = "";
lowercaseLettersBox.value.split("").forEach(letter => {
lowercaseContainer.innerHTML += `<div class="bg-white shadow">${letter}</div>`;
});
updateCharWidthRatiosDict(lowercaseContainer);
}
function printDigits() {
digitsContainer.innerHTML = "";
digitsBox.value.split("").forEach(digit => {
digitsContainer.innerHTML += `<div class="bg-white shadow">${digit}</div>`;
});
updateCharWidthRatiosDict(digitsContainer);
}
function printOtherCharacters() {
otherContainer.innerHTML = "";
otherCharsBox.value.split("").forEach(letter => {
otherContainer.innerHTML += `<div class="bg-white shadow">${letter}</div>`;
});
updateCharWidthRatiosDict(otherContainer);
}
function getCharContainers() {
return {
uppercase : uppercaseContainer,
lowercase : lowercaseContainer,
digit : digitsContainer,
other : otherContainer,
};
}
function updateCharWidthRatiosDict(containers) {
if (containers instanceof HTMLElement) {
containers = [containers];
}
containers = containers || Object.values(getCharContainers());
for (const container of containers) {
let containerRatios = calculateCharWidthRatios(container);
charWidthRatiosDict = Object.assign(charWidthRatiosDict, containerRatios);
}
}
function printTextSize() {
let textEl = document.querySelector("div[contentEditable]");
let text = getTextFromEditableEl(textEl);
let htmlWidth = parseFloat(getComputedStyle(textEl).width);
let htmlHeight = parseFloat(getComputedStyle(textEl).height);
let htmlLines = htmlHeight / 16;
output.htmlWidth.textContent = toFixed(htmlWidth, 3);
output.htmlHeight.textContent = toFixed(htmlHeight, 3);
output.htmlLines.textContent = toFixed(htmlLines, 3);
let maxWidth = Infinity;
if (isElMaxWidthReached(textEl)) {
maxWidth = getElMaxWidth(textEl);
}
let size = calcTextSize(text, 16, charWidthRatiosDict, maxWidth);
output.calcWidth.textContent = toFixed(size.width, 3);
output.calcHeight.textContent = toFixed(size.height, 3);
output.calcLines.textContent = toFixed(size.lines.length, 3);
output.matchWidth.textContent = toFixed(size.width / htmlWidth, 3);
output.matchHeight.textContent = toFixed(size.height / htmlHeight, 3);
output.matchLines.textContent = toFixed(size.lines.length / htmlLines, 3);
}
function toFixed(number, precision) {
number = number.toFixed(precision);
let [int, fraction] = number.split(".");
number = int;
if (fraction && fraction != "0".repeat(fraction.length)) {
number = `${int}.${fraction}`;
}
return number;
}
function downloadCharWidthRatiosDict() {
downloadViaAnchor(
createJSONObjURL(charWidthRatiosDict),
localFontSelect.value.toLowerCase() + "-width-ratio-dict.json",
);
}
function createJSONObjURL(obj) {
const str = JSON.stringify(obj);
const blob = new Blob([str], { type: 'application/json' });
const url = URL.createObjectURL(blob);
return url;
}
function downloadViaAnchor(url, filename) {
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = filename;
anchor.click();
URL.revokeObjectURL(url);
}