-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwoff2base.js
110 lines (93 loc) · 3.21 KB
/
woff2base.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
// TODO: Copy to Clipboard: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard
// TODO: Check mime type: https://stackoverflow.com/questions/18299806/how-to-check-file-mime-type-with-javascript-before-upload
(function () {
const $currentfont = document.getElementById("currentfont");
const $csscode = document.getElementById("csscode");
function dir(m) {
if (console.dir && m) {
console.dir(m);
}
}
function log(m) {
if (console.log) {
m = m !== undefined ? m : "-----------------";
console.log(m);
}
}
function displayFontInfo(name, text) {
const cssHeader = `@font-face { \r\n` +
` font-family: "${name}";\r\n` +
` /* Add other properties here, as needed. For example: */\r\n` +
` /*\r\n` +
` font-weight: 100 900;\r\n` +
` font-style: normal italic;\r\n` +
` */\r\n` +
` src: url(`;
const cssFooter = `);\r\n` +
`}`;
$currentfont.innerHTML = name;
$csscode.value = cssHeader + text + cssFooter;
//console.clear();
//log(text);
}
function convertFontFile() {
const file = document.getElementById("fontinput").files[0];
const fileName = file.name;
//dir(file);
let fontName = "";
let fontBase64 = "";
const readerFontkit = new FileReader();
const readerBase64 = new FileReader();
try {
if (fileName.endsWith(".woff2") || fileName.endsWith(".woff") || fileName.endsWith(".ttf")) {
readerFontkit.readAsArrayBuffer(file);
readerFontkit.onload = (e) => {
const arrayBuffer = readerFontkit.result;
const fontkitBuffer = new Buffer(arrayBuffer);
const font = fontkit.create(fontkitBuffer);
fontName = font.familyName
log("Fontkit reader loaded...");
//dir(fontName);
}
readerBase64.readAsDataURL(file);
readerBase64.onload = (e) => {
log("Base64 reader loaded...");
fontBase64 = e.target.result;
//dir(e);
displayFontInfo(fontName, fontBase64);
log(fontBase64)
}
displayFontInfo(fontName, fontBase64);
} else {
$csscode.value = `Sorry. I couldn't read ${fileName}. `
+ `If you submitted a valid font, maybe it was in a `
+ `format this app doesn't understand (e.g., .eot).`;
}
} catch (e) {
log(e);
} finally {
}
}
function loadDefaultFont() {
const fontName = "Firava";
const fontPath = "fonts/Firava.woff2";
fetch(fontPath)
.then(response => response.blob())
.then(font => {
const reader = new FileReader();
reader.onload = function () {
let fontBase64 = this.result;
//displayFontInfo(fontName, fontBase64);
};
reader.readAsDataURL(font);
});
}
function listenForFontChooser() {
document.getElementById("fontinput").addEventListener("change", convertFontFile, false);
}
window.addEventListener("DOMContentLoaded", function () {
//loadDefaultFont();
listenForFontChooser();
displayFontInfo("", "");
});
}());