-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtextme02.html
422 lines (371 loc) · 13 KB
/
textme02.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<!--
* Copyright 2015, Gregg Tavares.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Gregg Tavares. nor the names of his
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
canvas {
background-color: black;
}
</style>
<script src="js/jquery-1.9.1.js"></script>
<script src="js/webgl-utils.js"></script>
<script>
"use strict";
var copyOptions = function(defaults, options) {
options = options || {};
var settings = {};
Object.keys(defaults).forEach(function(key) {
var value = defaults[key];
if (typeof value === "object") {
settings[key] = copyOptions(value, options[key]);
} else {
settings[key] = options[key] || value;
}
});
return settings;
};
var GlyphMaker = (function(options) {
var defaults = {
glyphs: "",
fontOptions: {
font: "20px monospace",
fillStyle: "white",
textAlign: "center",
textBaseLine: undefined,
},
maxWidth: 32,
maxHeight: 32,
baseLine: 20,
pack: 0,
checker: false,
};
function generateGlyphRange(start, end) {
var s = ""
for (var ii = start; ii <= end; ++ii) {
s += String.fromCharCode(ii); // warning. Won't work for all unicode
}
return s;
}
var unicodeRanges = {
ascii: { start: 33, end: 127 },
greek: { start: 0x370, end: 0x3FF },
tamil: { start: 0xB80, end: 0xBFF },
telugu: { start: 0xC00, end: 0xC7F },
runic: { start: 0x16A0, end: 0x16FF },
canadian: { start: 0x1400, end: 0x167F },
phoneticExtensions: { start: 0x1D00, end: 0x1DfF },
mathOps: { start: 0x2200, end: 0x22FF },
miscTech: { start: 0x2300, end: 0x23FF },
boxDrawing: { start: 0x2500, end: 0x257F },
blockElements: { start: 0x2580, end: 0x259F },
geometricShapes: { start: 0x25A0, end: 0x25FF },
miscShapes: { start: 0x2600, end: 0x26FF },
dingbats: { start: 0x2700, end: 0x27BF },
cjk: { start: 0x4E00, end: 0x5200 }, // really 0x9FCC!
};
function generate(rangeName) {
var range = unicodeRanges[rangeName];
return generateGlyphRange(range.start, range.end);
}
var settings = copyOptions(defaults, options);
if (!settings.glyphs) {
var s = " ";
s += generate("ascii");
//s += generate("tamil");
//s += generate("runic");
//s += generate("miscTech");
//s += generate("boxDrawing");
//s += generate("blockElements");
//s += generate("geometricShapes");
//s += generate("miscShapes");
//s += generate("dingbats");
//s += generate("cjk");
settings.glyphs = s;
}
var canvas = document.createElement("canvas");
canvas.width = settings.maxWidth;
canvas.height = settings.maxHeight;
var ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
var setFontSettings = function() {
Object.keys(settings.fontOptions).forEach(function(key) {
ctx[key] = settings.fontOptions[key];
});
};
var allZero = function(imageData, offset, start, end, stride) {
for (; start < end; ++start) {
if (imageData.data[offset]) {
return false;
}
offset += stride;
}
return true;
};
var columnEmpty = function(imageData, column, opt_y, opt_height) {
var y = opt_y || 0;
var end = Math.min(y + (opt_height || imageData.height), imageData.height);
var offset = (imageData.width * y + column) * 4 + 3;
var stride = imageData.width * 4;
return allZero(imageData, offset, y, end, stride);
};
var rowEmpty = function(imageData, row, opt_x, opt_width) {
var x = opt_x || 0;
var end = Math.min(x + (opt_width || imageData.width), imageData.width);
var offset = (imageData.width * row + x) * 4 + 3;
return allZero(imageData, offset, x, end, 4);
};
var offsetX = Math.floor(ctx.canvas.width / 2);
var offsetY = settings.baseLine;
var getExtentsForGlyph = function(glyph) {
var x0 = 0;
var y0 = 0;
var x1 = settings.maxWidth - 1;
var y1 = settings.maxHeight - 1;
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fillText(glyph, offsetX, offsetY);
var imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
// scan left to right
while (x0 < settings.maxWidth && columnEmpty(imageData, x0)) {
++x0;
}
// scan right to left
while (x1 >= x0 && columnEmpty(imageData, x1)) {
--x1;
}
var width = x1 - x0 + 1;
// scan left to right
while (y0 < settings.maxHeight && rowEmpty(imageData, y0, x0, width)) {
++y0;
}
// scan right to left
while (y1 >= y0 && rowEmpty(imageData, y1, x0, width)) {
--y1;
}
var extents = {
x0: x0,
y0: y0,
x1: x1,
y1: y1,
width: x1 - x0 + 1,
height: y1 - y0 + 1,
glyph: glyph,
};
return extents;
};
setFontSettings();
var extents = Array.prototype.map.call(settings.glyphs, getExtentsForGlyph);
var limits = extents.filter(function(extent) {
return extent.x0 <= extent.x1 && extent.y0 <= extent.y1;
}).reduce(function(prev, curr) {
return {
x0: Math.min(prev.x0, curr.x0),
y0: Math.min(prev.y0, curr.y0),
x1: Math.max(prev.x1, curr.x1),
y1: Math.max(prev.y1, curr.y1),
width: Math.max(prev.width , curr.width ),
height: Math.max(prev.height, curr.height),
};
});
var maxWidth = 0;
var maxWidthGlyph;
var maxHeight = 0;
var maxHeightGlyph;
extents.forEach(function(extent) {
if (extent.width > maxWidth) {
maxWidth = extent.width;
maxWidthGlyph = extent.glyph;
}
if (extent.height > maxHeight) {
maxHeight = extent.height;
maxHeightGlyph = extent.glyph;
}
});
console.log("mw: " + maxWidth + " mwg: " + maxWidthGlyph + " : 0x" + maxWidthGlyph.charCodeAt(0).toString(16));
console.log("mh: " + maxHeight + " mhg: " + maxHeightGlyph + " : 0x" + maxHeightGlyph.charCodeAt(0).toString(16));
var glyphWidth = limits.x1 - limits.x0 + 1 - settings.pack * 2;
var glyphHeight = limits.y1 - limits.y0 + 1 - settings.pack * 2;
console.log("gw: " + glyphWidth + ", w: " + limits.width);
console.log("gh: " + glyphHeight + ", h: " + limits.height);
offsetX = Math.floor(glyphWidth / 2);// - limits.x0;
offsetY -= limits.y0 + settings.pack;
function getBrightness(imageData, x, y, width, height) {
var total = 0;
for (var yy = 0; yy < height; ++yy) {
var offset = ((yy + y) * imageData.width + x) * 4 + 3;
for (var xx = 0; xx < width; ++xx) {
total += imageData.data[offset];
offset += 4;
}
}
var maxBrightness = width * height * 255;
return total / maxBrightness;
}
function computeBrightnessOfGlyphs() {
ctx.canvas.width = glyphWidth;
ctx.canvas.height = glyphHeight;
setFontSettings();
extents.forEach(function(extent, ndx) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fillText(extent.glyph, offsetX, offsetY);
var imageData = ctx.getImageData(0, 0, glyphWidth, glyphHeight);
var brightness = [];
for (var yy = 0; yy < 2; ++yy) {
for (var xx = 0; xx < 2; ++xx) {
var width = glyphWidth / 2 | 0;
var height = glyphHeight / 2 | 0;
var x = xx * width;
var y = yy * height;
if (glyphWidth % 2 == 1) {
width += 1;
}
if (glyphHeight % 2 == 1) {
height += 1;
}
brightness.push(getBrightness(imageData, x, y, width, height));
}
}
extent.brightness = brightness;
extent.totalBrightness = brightness.reduce(function(a, b) { return a + b; }) / 4;
});
}
computeBrightnessOfGlyphs();
var maxBrightness = extents.reduce(function(prev, curr) {
return prev.map(function(a, ndx) {
return Math.max(a, curr.brightness[ndx]);
});
}, [0, 0, 0, 0]);
extents.forEach(function(extent) {
extent.brightness = extent.brightness.map(function(value, ndx) {
return value / maxBrightness[ndx];
});
});
console.log(maxBrightness);
function getDistanceSq(quadBrightness, extent) {
return extent.brightness.reduce(function(prev, curr, ndx) {
var delta = curr - quadBrightness[ndx];
return prev + delta * delta;
}, 0);
}
function getClosestMatches(quadBrightness) {
var matches = [];
var closestDistSq = 10000000;
extents.forEach(function(extent) {
var distSq = getDistanceSq(quadBrightness, extent);
if (distSq < closestDistSq) {
closestDistSq = distSq;
matches = [extent];
} else if (distSq === closestDistSq) {
matches.push(extent);
}
});
return matches;
}
function createFontTexture() {
var numGlyphs = settings.glyphs.length;
var maxTextureSize = 2048; // look up from GL?
var glyphsPerSegment = 8; // 3 bits
var segmentWidth = glyphsPerSegment * glyphWidth;
var segmentHeight = glyphsPerSegment * glyphHeight;
var textureWidth = glyphsPerSegment * segmentWidth;
var textureHeight = glyphsPerSegment * segmentHeight;
if (textureWidth > maxTextureSize || textureHeight > maxTextureSize) {
throw "can't make texture size " + textureWidth + " x " + textureHeight;
}
ctx.canvas.width = textureWidth;
ctx.canvas.height = textureHeight;
setFontSettings();
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
// console.log(extents.map(JSON.stringify).join("\n"));
var maxSegmentIndex = glyphsPerSegment - 1;
for (var ll = 0; ll < glyphsPerSegment; ++ll) {
for (var lr = 0; lr < glyphsPerSegment; ++lr) {
for (var rl = 0; rl < glyphsPerSegment; ++rl) {
for (var rr = 0; rr < glyphsPerSegment; ++rr) {
var matches = getClosestMatches([ll / maxSegmentIndex, lr / maxSegmentIndex, rl / maxSegmentIndex, rr / maxSegmentIndex]);
if (matches.length) {
var match = matches[(ll + lr + rl + rr) % matches.length];
match.spots = match.spots || [];
match.spots.push([ll, lr, rl, rr]);
ctx.fillText(
match.glyph,
offsetX + ll * glyphWidth + lr * segmentWidth,
offsetY + rl * glyphHeight + rr * segmentHeight)
}
}
}
}
}
// for every glyph that was used more than once. Find which thing it best matches
var multiUsed = extents.filter(function(extent) {
return extent.spots && extent.spots.length > 1;
});
if (!multiUsed.length) {
return;
}
multiUsed.forEach(function(extent) {
// find best matching spot
});
// extents.forEach(function(extent, ndx) {
// var col = ndx % numCols;
// var row = ndx / numCols | 0;
// // console.log(glyph + ": " + col + "," + row + ": " + (offsetX + col * glyphWidth) + "," + (offsetY + row * glyphHeight));
// ctx.save();
// if (settings.checker) {
// ctx.fillStyle = (col + row) % 2 ? "black" : "gray";
// ctx.fillRect(col * glyphWidth, row * glyphHeight, glyphWidth, glyphHeight);
// }
// ctx.beginPath();
// ctx.rect(col * glyphWidth, row * glyphHeight, glyphWidth, glyphHeight);
// ctx.clip();
// ctx.fillStyle = settings.fontOptions.fillStyle;
// ctx.fillText(extent.glyph, offsetX + col * glyphWidth, offsetY + row * glyphHeight);
// ctx.restore();
// });
}
createFontTexture();
});
function main() {
var glyphMaker = new GlyphMaker({
// glyphs: ",.!123abcABCWpgjy",
pack: 2,
checker: true,
});
}
$(function(){
main();
});
</script>
</head>
<body>
</body>
</html>