-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfd.js
176 lines (134 loc) · 6.11 KB
/
gfd.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
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
DataView.prototype.getString = function (offset, length) {
let end = typeof length == 'number' ? offset + length : this.byteLength;
let text = '';
let val = -1;
while (offset < this.byteLength && offset < end) {
val = this.getUint8(offset++);
if (val == 0) break;
text += String.fromCharCode(val);
}
return text;
};
DataView.prototype.getHex = function (offset, length) {
let end = typeof length == 'number' ? offset + length : this.byteLength;
let text = '';
let val = -1;
while (offset < this.byteLength && offset < end) {
val = this.getUint8(offset++);
text += val.toString(16).toUpperCase().padStart(2, '0');
}
return text;
};
document.getElementById('submit').addEventListener('click', event => {
const elem = document.getElementById('importFile');
if (elem.files.length <= 0) {
alert("No file selected.");
return false;
}
document.querySelector('#gfdContent tbody').innerHTML = '';
document.getElementById('gfdHeader').style.display = 'block';
document.getElementById('gfdContent').style.display = 'table';
document.getElementById('importImgHead').style.display = 'block';
document.getElementById('importImgCont').style.display = 'flex';
const file = elem.files[0];
const reader = new FileReader();
reader.addEventListener('load', event => {
const buffer = event.target.result;
const view = new DataView(buffer);
const magic = view.getString(0, 4);
if (!magic.startsWith('GFD')) {
alert("Please select a GFD file.");
return false;
}
const version = view.getUint32(4, true);
if (version !== 68101
&& version !== 68614
&& version !== 69382
&& version !== 69639
&& version !== 69895) {
alert("Unsupported GFD version.");
return false;
}
if (version == 68101
|| version == 68614) {
var fontSize = view.getUint32(20, true),
fontTexCount = view.getUint32(24, true),
charCount = view.getUint32(28, true),
floatCount = view.getUint32(32, true),
maxCharWidth = 'undefined',
maxCharHeight = 'undefined',
baseLine = view.getFloat32(36, true),
descentLine = view.getFloat32(40, true),
fileNameLength = view.getUint32(44 + floatCount * 4, true),
fileName = view.getString(48 + floatCount * 4, fileNameLength),
items = ['version', 'fontSize', 'fontTexCount', 'charCount', 'floatCount', 'maxCharWidth', 'maxCharHeight', 'baseLine', 'descentLine', 'fileName'],
vars = [version, fontSize, fontTexCount, charCount, floatCount, maxCharWidth, maxCharHeight, baseLine, descentLine, fileName];
} else if (version == 69382
|| version == 69639
|| version == 69895) {
var fontSize = view.getUint32(20, true),
fontTexCount = view.getUint32(24, true),
charCount = view.getUint32(28, true),
floatCount = view.getUint32(36, true),
maxCharWidth = view.getFloat32(40, true),
maxCharHeight = view.getFloat32(44, true),
baseLine = view.getFloat32(48, true),
descentLine = view.getFloat32(52, true),
fileNameLength = view.getUint32(56 + floatCount * 4, true),
fileName = view.getString(60 + floatCount * 4, fileNameLength),
items = ['version', 'fontSize', 'fontTexCount', 'charCount', 'floatCount', 'maxCharWidth', 'maxCharHeight', 'baseLine', 'descentLine', 'fileName'],
vars = [version, fontSize, fontTexCount, charCount, floatCount, maxCharWidth, maxCharHeight, baseLine, descentLine, fileName];
}
for (let i = 0; i < items.length; i++) {
document.getElementById(items[i]).innerHTML = vars[i];
}
if (window.Worker) {
const worker = new Worker('./worker.js');
worker.postMessage(buffer);
worker.onmessage = function (glphs) {
document.querySelector('#gfdContent tbody').innerHTML = glphs.data;
}
} else {
alert("Your browser does not support Web Workers.");
return false;
}
});
reader.readAsArrayBuffer(file);
});
document.getElementById('importImg').addEventListener('click', event => {
const img = document.getElementById('importImgFile');
if (img.files.length <= 0) {
alert("No file selected.");
return false;
}
const fileName = document.getElementById('fileName').innerHTML;
const gfdRow = document.getElementById('gfdContent').tBodies[0].rows;
const inputImg = new Image();
let sx = [], sy = [], sWidth = [], sHeight = [];
for (let j = 0; j < img.files.length; j++) {
let readerImg = new FileReader();
readerImg.addEventListener('load', event => {
inputImg.src = event.target.result;
for (let i = 0; i < gfdRow.length; i++) {
let page = gfdRow[i].childNodes[1].innerHTML;
if (img.files[j].name.startsWith(fileName.split('\\')[fileName.split('\\').length-1]+'_'+page+'_')) {
sx[i] = parseInt(gfdRow[i].childNodes[2].innerHTML.split(', ')[0]);
sy[i] = parseInt(gfdRow[i].childNodes[2].innerHTML.split(', ')[1]);
sWidth[i] = parseInt(gfdRow[i].childNodes[3].innerHTML.split(', ')[0]);
sHeight[i] = parseInt(gfdRow[i].childNodes[3].innerHTML.split(', ')[1]);
if (gfdRow[i].firstChild.getElementsByTagName('canvas')[0] !== undefined) {
gfdRow[i].firstChild.getElementsByTagName('canvas')[0].remove();
}
gfdRow[i].firstChild.insertAdjacentHTML('afterbegin', '<canvas class="color-bg-emphasis"></canvas>');
gfdRow[i].firstChild.getElementsByTagName('canvas')[0].width = sWidth[i];
gfdRow[i].firstChild.getElementsByTagName('canvas')[0].height = sHeight[i];
gfdRow[i].firstChild.getElementsByTagName('canvas')[0].getContext('2d').drawImage(inputImg,sx[i],sy[i],sWidth[i],sHeight[i],0,0,sWidth[i],sHeight[i]);
} else {
alert("The image file name must start with the font file name.");
return false;
}
}
});
readerImg.readAsDataURL(img.files[j]);
}
});