-
Notifications
You must be signed in to change notification settings - Fork 14
/
kage.js
executable file
·437 lines (409 loc) · 16.6 KB
/
kage.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
function Kage(size){
// method
function makeGlyph(polygons, buhin){ // void
var glyphData = this.kBuhin.search(buhin);
this.makeGlyph2(polygons, glyphData);
}
Kage.prototype.makeGlyph = makeGlyph;
function makeGlyph2(polygons, data){ // void
if(data != ""){
var strokesArray = this.adjustStrokes(this.getEachStrokes(data));
for(var i = 0; i < strokesArray.length; i++){
dfDrawFont(this, polygons,
strokesArray[i][0],
strokesArray[i][1],
strokesArray[i][2],
strokesArray[i][3],
strokesArray[i][4],
strokesArray[i][5],
strokesArray[i][6],
strokesArray[i][7],
strokesArray[i][8],
strokesArray[i][9],
strokesArray[i][10]);
}
}
}
Kage.prototype.makeGlyph2 = makeGlyph2;
function makeGlyph3(data){ // void
var result = new Array();
if(data != ""){
var strokesArray = this.adjustStrokes(this.getEachStrokes(data));
for(var i = 0; i < strokesArray.length; i++){
var polygons = new Polygons();
dfDrawFont(this, polygons,
strokesArray[i][0],
strokesArray[i][1],
strokesArray[i][2],
strokesArray[i][3],
strokesArray[i][4],
strokesArray[i][5],
strokesArray[i][6],
strokesArray[i][7],
strokesArray[i][8],
strokesArray[i][9],
strokesArray[i][10]);
result.push(polygons);
}
}
return result;
}
Kage.prototype.makeGlyph3 = makeGlyph3;
function getEachStrokes(glyphData){ // strokes array
var strokesArray = new Array();
var strokes = glyphData.split("$");
for(var i = 0; i < strokes.length; i++){
var columns = strokes[i].split(":");
if(Math.floor(columns[0]) != 99){
strokesArray.push([
Math.floor(columns[0]),
Math.floor(columns[1]),
Math.floor(columns[2]),
Math.floor(columns[3]),
Math.floor(columns[4]),
Math.floor(columns[5]),
Math.floor(columns[6]),
Math.floor(columns[7]),
Math.floor(columns[8]),
Math.floor(columns[9]),
Math.floor(columns[10])
]);
} else {
var buhin = this.kBuhin.search(columns[7]);
if(buhin != ""){
strokesArray = strokesArray.concat(this.getEachStrokesOfBuhin(buhin,
Math.floor(columns[3]),
Math.floor(columns[4]),
Math.floor(columns[5]),
Math.floor(columns[6]),
Math.floor(columns[1]),
Math.floor(columns[2]),
Math.floor(columns[9]),
Math.floor(columns[10]))
);
}
}
}
return strokesArray;
}
Kage.prototype.getEachStrokes = getEachStrokes;
function getEachStrokesOfBuhin(buhin, x1, y1, x2, y2, sx, sy, sx2, sy2){
var temp = this.getEachStrokes(buhin);
var result = new Array();
var box = this.getBox(temp);
if(sx != 0 || sy != 0){
if(sx > 100){
sx -= 200;
} else {
sx2 = 0;
sy2 = 0;
}
}
for(var i = 0; i < temp.length; i++){
if(sx != 0 || sy != 0){
temp[i][3] = stretch(sx, sx2, temp[i][3], box.minX, box.maxX);
temp[i][4] = stretch(sy, sy2, temp[i][4], box.minY, box.maxY);
temp[i][5] = stretch(sx, sx2, temp[i][5], box.minX, box.maxX);
temp[i][6] = stretch(sy, sy2,temp[i][6], box.minY, box.maxY);
if(temp[i][0] != 99){
temp[i][7] = stretch(sx, sx2, temp[i][7], box.minX, box.maxX);
temp[i][8] = stretch(sy, sy2, temp[i][8], box.minY, box.maxY);
temp[i][9] = stretch(sx, sx2, temp[i][9], box.minX, box.maxX);
temp[i][10] = stretch(sy, sy2, temp[i][10], box.minY, box.maxY);
}
}
result.push([temp[i][0],
temp[i][1],
temp[i][2],
x1 + temp[i][3] * (x2 - x1) / 200,
y1 + temp[i][4] * (y2 - y1) / 200,
x1 + temp[i][5] * (x2 - x1) / 200,
y1 + temp[i][6] * (y2 - y1) / 200,
x1 + temp[i][7] * (x2 - x1) / 200,
y1 + temp[i][8] * (y2 - y1) / 200,
x1 + temp[i][9] * (x2 - x1) / 200,
y1 + temp[i][10] * (y2 - y1) / 200]);
}
return result;
}
Kage.prototype.getEachStrokesOfBuhin = getEachStrokesOfBuhin;
function adjustStrokes(strokesArray){
if(this.kShotai == this.kMincho){
strokesArray = this.adjustHane(strokesArray);
strokesArray = this.adjustMage(strokesArray);
strokesArray = this.adjustTate(strokesArray);
strokesArray = this.adjustKakato(strokesArray);
strokesArray = this.adjustUroko(strokesArray);
strokesArray = this.adjustUroko2(strokesArray);
strokesArray = this.adjustKirikuchi(strokesArray);
}
return strokesArray;
}
Kage.prototype.adjustStrokes = adjustStrokes;
function adjustHane(sa){ // strokesArray
for(var i = 0; i < sa.length; i++){
if((sa[i][0] == 1 || sa[i][0] == 2 || sa[i][0] == 6) && sa[i][2] == 4){
var lpx; // lastPointX
var lpy; // lastPointY
if(sa[i][0] == 1){
lpx = sa[i][5];
lpy = sa[i][6];
} else if(sa[i][0] == 2){
lpx = sa[i][7];
lpy = sa[i][8];
} else {
lpx = sa[i][9];
lpy = sa[i][10];
}
var mn = Infinity; // mostNear
if(lpx + 18 < 100){
mn = lpx + 18;
}
for(var j = 0; j < sa.length; j++){
if(i != j && sa[j][0] == 1 && sa[j][3] == sa[j][5] && sa[j][3] < lpx && sa[j][4] <= lpy && sa[j][6] >= lpy){
if(lpx - sa[j][3] < 100){
mn = Math.min(mn, lpx - sa[j][3]);
}
}
}
if(mn != Infinity){
sa[i][2] += 700 - Math.floor(mn / 15) * 100; // 0-99 -> 0-700
}
}
}
return sa;
}
Kage.prototype.adjustHane = adjustHane;
function adjustUroko(strokesArray){ // strokesArray
for(var i = 0; i < strokesArray.length; i++){
if(strokesArray[i][0] == 1 && strokesArray[i][2] == 0){ // no operation for TATE
for(var k = 0; k < this.kAdjustUrokoLengthStep; k++){
var tx, ty, tlen;
if(strokesArray[i][4] == strokesArray[i][6]){ // YOKO
tx = strokesArray[i][5] - this.kAdjustUrokoLine[k];
ty = strokesArray[i][6] - 0.5;
tlen = strokesArray[i][5] - strokesArray[i][3];
} else {
var rad = Math.atan((strokesArray[i][6] - strokesArray[i][4]) / (strokesArray[i][5] - strokesArray[i][3]));
tx = strokesArray[i][5] - this.kAdjustUrokoLine[k] * Math.cos(rad) - 0.5 * Math.sin(rad);
ty = strokesArray[i][6] - this.kAdjustUrokoLine[k] * Math.sin(rad) - 0.5 * Math.cos(rad);
tlen = Math.sqrt((strokesArray[i][6] - strokesArray[i][4]) * (strokesArray[i][6] - strokesArray[i][4]) +
(strokesArray[i][5] - strokesArray[i][3]) * (strokesArray[i][5] - strokesArray[i][3]));
}
if(tlen < this.kAdjustUrokoLength[k] ||
isCrossWithOthers(strokesArray, i, tx, ty, strokesArray[i][5], strokesArray[i][6])
){
strokesArray[i][2] += (this.kAdjustUrokoLengthStep - k) * 100;
k = Infinity;
}
}
}
}
return strokesArray;
}
Kage.prototype.adjustUroko = adjustUroko;
function adjustUroko2(strokesArray){ // strokesArray
for(var i = 0; i < strokesArray.length; i++){
if(strokesArray[i][0] == 1 && strokesArray[i][2] == 0 && strokesArray[i][4] == strokesArray[i][6]){
var pressure = 0;
for(var j = 0; j < strokesArray.length; j++){
if(i != j && (
(strokesArray[j][0] == 1 && strokesArray[j][4] == strokesArray[j][6] &&
!(strokesArray[i][3] + 1 > strokesArray[j][5] || strokesArray[i][5] - 1 < strokesArray[j][3]) &&
Math.abs(strokesArray[i][4] - strokesArray[j][4]) < this.kAdjustUroko2Length) ||
(strokesArray[j][0] == 3 && strokesArray[j][6] == strokesArray[j][8] &&
!(strokesArray[i][3] + 1 > strokesArray[j][7] || strokesArray[i][5] - 1 < strokesArray[j][5]) &&
Math.abs(strokesArray[i][4] - strokesArray[j][6]) < this.kAdjustUroko2Length)
)){
pressure += Math.pow(this.kAdjustUroko2Length - Math.abs(strokesArray[i][4] - strokesArray[j][6]), 1.1);
}
}
var result = Math.min(Math.floor(pressure / this.kAdjustUroko2Length), this.kAdjustUroko2Step) * 100;
if(strokesArray[i][2] < result){
strokesArray[i][2] = strokesArray[i][2] % 100 + Math.min(Math.floor(pressure / this.kAdjustUroko2Length), this.kAdjustUroko2Step) * 100;
}
}
}
return strokesArray;
}
Kage.prototype.adjustUroko2 = adjustUroko2;
function adjustTate(strokesArray){ // strokesArray
for(var i = 0; i < strokesArray.length; i++){
if((strokesArray[i][0] == 1 || strokesArray[i][0] == 3 || strokesArray[i][0] == 7) && strokesArray[i][3] == strokesArray[i][5]){
for(var j = 0; j < strokesArray.length; j++){
if(i != j && (strokesArray[j][0] == 1 || strokesArray[j][0] == 3 || strokesArray[j][0] == 7) && strokesArray[j][3] == strokesArray[j][5] &&
!(strokesArray[i][4] + 1 > strokesArray[j][6] || strokesArray[i][6] - 1 < strokesArray[j][4]) &&
Math.abs(strokesArray[i][3] - strokesArray[j][3]) < this.kMinWidthT * this.kAdjustTateStep){
strokesArray[i][1] += (this.kAdjustTateStep - Math.floor(Math.abs(strokesArray[i][3] - strokesArray[j][3]) / this.kMinWidthT)) * 1000;
if(strokesArray[i][1] > this.kAdjustTateStep * 1000){
strokesArray[i][1] = strokesArray[i][1] % 1000 + this.kAdjustTateStep * 1000;
}
}
}
}
}
return strokesArray;
}
Kage.prototype.adjustTate = adjustTate;
function adjustMage(strokesArray){ // strokesArray
for(var i = 0; i < strokesArray.length; i++){
if((strokesArray[i][0] == 3) && strokesArray[i][6] == strokesArray[i][8]){
for(var j = 0; j < strokesArray.length; j++){
if(i != j && (
(strokesArray[j][0] == 1 && strokesArray[j][4] == strokesArray[j][6] &&
!(strokesArray[i][5] + 1 > strokesArray[j][5] || strokesArray[i][7] - 1 < strokesArray[j][3]) &&
Math.abs(strokesArray[i][6] - strokesArray[j][4]) < this.kMinWidthT * this.kAdjustMageStep) ||
(strokesArray[j][0] == 3 && strokesArray[j][6] == strokesArray[j][8] &&
!(strokesArray[i][5] + 1 > strokesArray[j][7] || strokesArray[i][7] - 1 < strokesArray[j][5]) &&
Math.abs(strokesArray[i][6] - strokesArray[j][6]) < this.kMinWidthT * this.kAdjustMageStep)
)){
strokesArray[i][2] += (this.kAdjustMageStep - Math.floor(Math.abs(strokesArray[i][6] - strokesArray[j][6]) / this.kMinWidthT)) * 1000;
if(strokesArray[i][2] > this.kAdjustMageStep * 1000){
strokesArray[i][2] = strokesArray[i][2] % 1000 + this.kAdjustMageStep * 1000;
}
}
}
}
}
return strokesArray;
}
Kage.prototype.adjustMage = adjustMage;
function adjustKirikuchi(strokesArray){ // strokesArray
for(var i = 0; i < strokesArray.length; i++){
if(strokesArray[i][0] == 2 && strokesArray[i][1] == 32 &&
strokesArray[i][3] > strokesArray[i][5] &&
strokesArray[i][4] < strokesArray[i][6]){
for(var j = 0; j < strokesArray.length; j++){ // no need to skip when i == j
if(strokesArray[j][0] == 1 &&
strokesArray[j][3] < strokesArray[i][3] && strokesArray[j][5] > strokesArray[i][3] &&
strokesArray[j][4] == strokesArray[i][4] && strokesArray[j][4] == strokesArray[j][6]){
strokesArray[i][1] = 132;
j = strokesArray.length;
}
}
}
}
return strokesArray;
}
Kage.prototype.adjustKirikuchi = adjustKirikuchi;
function adjustKakato(strokesArray){ // strokesArray
for(var i = 0; i < strokesArray.length; i++){
if(strokesArray[i][0] == 1 &&
(strokesArray[i][2] == 13 || strokesArray[i][2] == 23)){
for(var k = 0; k < this.kAdjustKakatoStep; k++){
if(isCrossBoxWithOthers(strokesArray, i,
strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])
| strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1] > 200 // adjust for baseline
| strokesArray[i][6] - strokesArray[i][4] < this.kAdjustKakatoRangeY[k + 1] // for thin box
){
strokesArray[i][2] += (3 - k) * 100;
k = Infinity;
}
}
}
}
return strokesArray;
}
Kage.prototype.adjustKakato = adjustKakato;
function getBox(strokes){ // minX, minY, maxX, maxY
var a = new Object();
a.minX = 200;
a.minY = 200;
a.maxX = 0;
a.maxY = 0;
for(var i = 0; i < strokes.length; i++){
if(strokes[i][0] == 0){ continue; }
a.minX = Math.min(a.minX, strokes[i][3]);
a.maxX = Math.max(a.maxX, strokes[i][3]);
a.minY = Math.min(a.minY, strokes[i][4]);
a.maxY = Math.max(a.maxY, strokes[i][4]);
a.minX = Math.min(a.minX, strokes[i][5]);
a.maxX = Math.max(a.maxX, strokes[i][5]);
a.minY = Math.min(a.minY, strokes[i][6]);
a.maxY = Math.max(a.maxY, strokes[i][6]);
if(strokes[i][0] == 1){ continue; }
if(strokes[i][0] == 99){ continue; }
a.minX = Math.min(a.minX, strokes[i][7]);
a.maxX = Math.max(a.maxX, strokes[i][7]);
a.minY = Math.min(a.minY, strokes[i][8]);
a.maxY = Math.max(a.maxY, strokes[i][8]);
if(strokes[i][0] == 2){ continue; }
if(strokes[i][0] == 3){ continue; }
if(strokes[i][0] == 4){ continue; }
a.minX = Math.min(a.minX, strokes[i][9]);
a.maxX = Math.max(a.maxX, strokes[i][9]);
a.minY = Math.min(a.minY, strokes[i][10]);
a.maxY = Math.max(a.maxY, strokes[i][10]);
}
return a;
}
Kage.prototype.getBox = getBox;
function stretch(dp, sp, p, min, max){ // interger
var p1, p2, p3, p4;
if(p < sp + 100){
p1 = min;
p3 = min;
p2 = sp + 100;
p4 = dp + 100;
} else {
p1 = sp + 100;
p3 = dp + 100;
p2 = max;
p4 = max;
}
return Math.floor(((p - p1) / (p2 - p1)) * (p4 - p3) + p3);
}
Kage.prototype.stretch = stretch;
//properties
Kage.prototype.kMincho = 0;
Kage.prototype.kGothic = 1;
this.kShotai = this.kMincho;
this.kRate = 100;
if(size == 1){
this.kMinWidthY = 1.2;
this.kMinWidthT = 3.6;
this.kWidth = 3;
this.kKakato = 1.8;
this.kL2RDfatten = 1.1;
this.kMage = 6;
this.kUseCurve = 0;
this.kAdjustKakatoL = ([8, 5, 3, 1, 0]); // for KAKATO adjustment 000,100,200,300,400
this.kAdjustKakatoR = ([4, 3, 2, 1]); // for KAKATO adjustment 000,100,200,300
this.kAdjustKakatoRangeX = 12; // check area width
this.kAdjustKakatoRangeY = ([1, 11, 14, 18]); // 3 steps of checking
this.kAdjustKakatoStep = 3; // number of steps
this.kAdjustUrokoX = ([14, 12, 9, 7]); // for UROKO adjustment 000,100,200,300
this.kAdjustUrokoY = ([7, 6, 5, 4]); // for UROKO adjustment 000,100,200,300
this.kAdjustUrokoLength = ([13, 21, 30]); // length for checking
this.kAdjustUrokoLengthStep = 3; // number of steps
this.kAdjustUrokoLine = ([13, 15, 18]); // check for crossing. corresponds to length
} else {
this.kMinWidthY = 2;
this.kMinWidthU = 2;
this.kMinWidthT = 6;
this.kWidth = 5;
this.kKakato = 3;
this.kL2RDfatten = 1.1;
this.kMage = 10;
this.kUseCurve = 0;
this.kAdjustKakatoL = ([14, 9, 5, 2, 0]); // for KAKATO adjustment 000,100,200,300,400
this.kAdjustKakatoR = ([8, 6, 4, 2]); // for KAKATO adjustment 000,100,200,300
this.kAdjustKakatoRangeX = 20; // check area width
this.kAdjustKakatoRangeY = ([1, 19, 24, 30]); // 3 steps of checking
this.kAdjustKakatoStep = 3; // number of steps
this.kAdjustUrokoX = ([24, 20, 16, 12]); // for UROKO adjustment 000,100,200,300
this.kAdjustUrokoY = ([12, 11, 9, 8]); // for UROKO adjustment 000,100,200,300
this.kAdjustUrokoLength = ([22, 36, 50]); // length for checking
this.kAdjustUrokoLengthStep = 3; // number of steps
this.kAdjustUrokoLine = ([22, 26, 30]); // check for crossing. corresponds to length
this.kAdjustUroko2Step = 3;
this.kAdjustUroko2Length = 40;
this.kAdjustTateStep = 4;
this.kAdjustMageStep = 5;
}
this.kBuhin = new Buhin();
return this;
}