update site URL
[chise/kage.git] / engine / kage.js
1 function Kage(size){\r
2   // method\r
3   function makeGlyph(polygons, buhin){ // void\r
4     var glyphData = this.kBuhin.search(buhin);\r
5     if(glyphData != ""){\r
6       this.drawStrokesArray(polygons, this.adjustKirikuchi(this.adjustUroko2(this.adjustUroko(this.adjustKakato(this.adjustTate(this.adjustMage(this.getEachStrokes(glyphData))))))));\r
7     }\r
8   }\r
9   Kage.prototype.makeGlyph = makeGlyph;\r
10   \r
11   function getEachStrokes(glyphData){ // strokes array\r
12     var strokesArray = new Array();\r
13     var strokes = glyphData.split("$");\r
14     for(var i = 0; i < strokes.length; i++){\r
15       var columns = strokes[i].split(":");\r
16       if(Math.floor(columns[0]) != 99){\r
17         strokesArray.push([\r
18           Math.floor(columns[0]),\r
19           Math.floor(columns[1]),\r
20           Math.floor(columns[2]),\r
21           Math.floor(columns[3]),\r
22           Math.floor(columns[4]),\r
23           Math.floor(columns[5]),\r
24           Math.floor(columns[6]),\r
25           Math.floor(columns[7]),\r
26           Math.floor(columns[8]),\r
27           Math.floor(columns[9]),\r
28           Math.floor(columns[10])\r
29           ]);\r
30       } else {\r
31         var buhin = this.kBuhin.search(columns[7]);\r
32         if(buhin != ""){\r
33           strokesArray = strokesArray.concat(this.getEachStrokesOfBuhin(buhin,\r
34                                                   Math.floor(columns[3]),\r
35                                                   Math.floor(columns[4]),\r
36                                                   Math.floor(columns[5]),\r
37                                                   Math.floor(columns[6]))\r
38                             );\r
39         }\r
40       }\r
41     }\r
42     return strokesArray;\r
43   }\r
44   Kage.prototype.getEachStrokes = getEachStrokes;\r
45   \r
46   function getEachStrokesOfBuhin(buhin, x1, y1, x2, y2){\r
47     var temp = this.getEachStrokes(buhin);\r
48     var result = new Array();\r
49     for(var i = 0; i < temp.length; i++){\r
50       result.push([temp[i][0],\r
51                    temp[i][1],\r
52                    temp[i][2],\r
53                    x1 + temp[i][3] * (x2 - x1) / 200,\r
54                    y1 + temp[i][4] * (y2 - y1) / 200,\r
55                    x1 + temp[i][5] * (x2 - x1) / 200,\r
56                    y1 + temp[i][6] * (y2 - y1) / 200,\r
57                    x1 + temp[i][7] * (x2 - x1) / 200,\r
58                    y1 + temp[i][8] * (y2 - y1) / 200,\r
59                    x1 + temp[i][9] * (x2 - x1) / 200,\r
60                    y1 + temp[i][10] * (y2 - y1) / 200]);\r
61     }\r
62     return result;\r
63   }\r
64   Kage.prototype.getEachStrokesOfBuhin = getEachStrokesOfBuhin;\r
65   \r
66   function adjustUroko(strokesArray){ // strokesArray\r
67     for(var i = 0; i < strokesArray.length; i++){\r
68       if(strokesArray[i][0] == 1 && strokesArray[i][2] == 0){ // no operation for TATE\r
69         for(var k = 0; k < this.kAdjustUrokoLengthStep; k++){\r
70           var tx, ty, tlen;\r
71           if(strokesArray[i][4] == strokesArray[i][6]){ // YOKO\r
72             tx = strokesArray[i][5] - this.kAdjustUrokoLine[k];\r
73             ty = strokesArray[i][6] - 0.5;\r
74             tlen = strokesArray[i][5] - strokesArray[i][3];\r
75           } else {\r
76             var rad = Math.atan((strokesArray[i][6] - strokesArray[i][4]) / (strokesArray[i][5] - strokesArray[i][3]));\r
77             tx = strokesArray[i][5] - this.kAdjustUrokoLine[k] * Math.cos(rad) - 0.5 * Math.sin(rad);\r
78             ty = strokesArray[i][6] - this.kAdjustUrokoLine[k] * Math.sin(rad) - 0.5 * Math.cos(rad);\r
79             tlen = Math.sqrt((strokesArray[i][6] - strokesArray[i][4]) * (strokesArray[i][6] - strokesArray[i][4]) +\r
80                              (strokesArray[i][5] - strokesArray[i][3]) * (strokesArray[i][5] - strokesArray[i][3]));\r
81           }\r
82           if(tlen < this.kAdjustUrokoLength[k] ||\r
83              isCrossWithOthers(strokesArray, i, tx, ty, strokesArray[i][5], strokesArray[i][6])\r
84              ){\r
85             strokesArray[i][2] += (this.kAdjustUrokoLengthStep - k) * 100;\r
86             k = Infinity;\r
87           }\r
88         }\r
89       }\r
90     }\r
91     return strokesArray;\r
92   }\r
93   Kage.prototype.adjustUroko = adjustUroko;\r
94   \r
95   function adjustUroko2(strokesArray){ // strokesArray\r
96     for(var i = 0; i < strokesArray.length; i++){\r
97       if(strokesArray[i][0] == 1 && strokesArray[i][2] == 0 && strokesArray[i][4] == strokesArray[i][6]){\r
98         var pressure = 0;\r
99         for(var j = 0; j < strokesArray.length; j++){\r
100           if(i != j && (\r
101              (strokesArray[j][0] == 1 && strokesArray[j][4] == strokesArray[j][6] &&\r
102               !(strokesArray[i][3] + 1 > strokesArray[j][5] || strokesArray[i][5] - 1 < strokesArray[j][3]) &&\r
103               Math.abs(strokesArray[i][4] - strokesArray[j][4]) < this.kAdjustUroko2Length) ||\r
104              (strokesArray[j][0] == 3 && strokesArray[j][6] == strokesArray[j][8] &&\r
105               !(strokesArray[i][3] + 1 > strokesArray[j][7] || strokesArray[i][5] - 1 < strokesArray[j][5]) &&\r
106               Math.abs(strokesArray[i][4] - strokesArray[j][6]) < this.kAdjustUroko2Length)\r
107              )){\r
108             pressure += Math.pow(this.kAdjustUroko2Length - Math.abs(strokesArray[i][4] - strokesArray[j][6]), 1.1);\r
109           }\r
110         }\r
111         var result = Math.min(Math.floor(pressure / this.kAdjustUroko2Length), this.kAdjustUroko2Step) * 100;\r
112         if(strokesArray[i][2] < result){\r
113           strokesArray[i][2] = strokesArray[i][2] % 100 + Math.min(Math.floor(pressure / this.kAdjustUroko2Length), this.kAdjustUroko2Step) * 100;\r
114         }\r
115       }\r
116     }\r
117     return strokesArray;\r
118   }\r
119   Kage.prototype.adjustUroko2 = adjustUroko2;\r
120   \r
121   function adjustTate(strokesArray){ // strokesArray\r
122     for(var i = 0; i < strokesArray.length; i++){\r
123       if((strokesArray[i][0] == 1 || strokesArray[i][0] == 3 || strokesArray[i][0] == 7) && strokesArray[i][3] == strokesArray[i][5]){\r
124         for(var j = 0; j < strokesArray.length; j++){\r
125           if(i != j && (strokesArray[j][0] == 1 || strokesArray[j][0] == 3 || strokesArray[j][0] == 7) && strokesArray[j][3] == strokesArray[j][5] &&\r
126              !(strokesArray[i][4] + 1 > strokesArray[j][6] || strokesArray[i][6] - 1 < strokesArray[j][4]) &&\r
127              Math.abs(strokesArray[i][3] - strokesArray[j][3]) < this.kMinWidthT * this.kAdjustTateStep){\r
128             strokesArray[i][1] += (this.kAdjustTateStep - Math.floor(Math.abs(strokesArray[i][3] - strokesArray[j][3]) / this.kMinWidthT)) * 1000;\r
129             if(strokesArray[i][1] > this.kAdjustTateStep * 1000){\r
130               strokesArray[i][1] = strokesArray[i][1] % 1000 + this.kAdjustTateStep * 1000;\r
131             }\r
132           }\r
133         }\r
134       }\r
135     }\r
136     return strokesArray;\r
137   }\r
138   Kage.prototype.adjustTate = adjustTate;\r
139   \r
140   function adjustMage(strokesArray){ // strokesArray\r
141     for(var i = 0; i < strokesArray.length; i++){\r
142       if((strokesArray[i][0] == 3) && strokesArray[i][6] == strokesArray[i][8]){\r
143         for(var j = 0; j < strokesArray.length; j++){\r
144           if(i != j && (\r
145              (strokesArray[j][0] == 1 && strokesArray[j][4] == strokesArray[j][6] &&\r
146               !(strokesArray[i][5] + 1 > strokesArray[j][5] || strokesArray[i][7] - 1 < strokesArray[j][3]) &&\r
147               Math.abs(strokesArray[i][6] - strokesArray[j][4]) < this.kMinWidthT * this.kAdjustMageStep) ||\r
148              (strokesArray[j][0] == 3 && strokesArray[j][6] == strokesArray[j][8] &&\r
149               !(strokesArray[i][5] + 1 > strokesArray[j][7] || strokesArray[i][7] - 1 < strokesArray[j][5]) &&\r
150               Math.abs(strokesArray[i][6] - strokesArray[j][6]) < this.kMinWidthT * this.kAdjustMageStep)\r
151              )){\r
152             strokesArray[i][2] += (this.kAdjustMageStep - Math.floor(Math.abs(strokesArray[i][6] - strokesArray[j][6]) / this.kMinWidthT)) * 1000;\r
153             if(strokesArray[i][2] > this.kAdjustMageStep * 1000){\r
154               strokesArray[i][2] = strokesArray[i][2] % 1000 + this.kAdjustMageStep * 1000;\r
155             }\r
156           }\r
157         }\r
158       }\r
159     }\r
160     return strokesArray;\r
161   }\r
162   Kage.prototype.adjustMage = adjustMage;\r
163   \r
164   function adjustKirikuchi(strokesArray){ // strokesArray\r
165     for(var i = 0; i < strokesArray.length; i++){\r
166       if(strokesArray[i][0] == 2 && strokesArray[i][1] == 32 &&\r
167          strokesArray[i][3] > strokesArray[i][5] &&\r
168          strokesArray[i][4] < strokesArray[i][6]){\r
169         for(var j = 0; j < strokesArray.length; j++){ // no need to skip when i == j\r
170           if(strokesArray[j][0] == 1 &&\r
171              strokesArray[j][3] < strokesArray[i][3] && strokesArray[j][5] > strokesArray[i][3] &&\r
172              strokesArray[j][4] == strokesArray[i][4] && strokesArray[j][4] == strokesArray[j][6]){\r
173             strokesArray[i][1] = 132;\r
174             j = strokesArray.length;\r
175           }\r
176         }\r
177       }\r
178     }\r
179     return strokesArray;\r
180   }\r
181   Kage.prototype.adjustKirikuchi = adjustKirikuchi;\r
182   \r
183   function adjustKakato(strokesArray){ // strokesArray\r
184     for(var i = 0; i < strokesArray.length; i++){\r
185       if(strokesArray[i][0] == 1 &&\r
186          (strokesArray[i][2] == 13 || strokesArray[i][2] == 23)){\r
187         for(var k = 0; k < this.kAdjustKakatoStep; k++){\r
188           if(isCrossBoxWithOthers(strokesArray, i,\r
189                                strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,\r
190                                strokesArray[i][6] + this.kAdjustKakatoRangeY[k],\r
191                                strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,\r
192                                strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])\r
193              | strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1] > 200 // adjust for baseline\r
194              | strokesArray[i][6] - strokesArray[i][4] < this.kAdjustKakatoRangeY[k + 1] // for thin box\r
195              ){\r
196             strokesArray[i][2] += (3 - k) * 100;\r
197             k = Infinity;\r
198           }\r
199         }\r
200       }\r
201     }\r
202     return strokesArray;\r
203   }\r
204   Kage.prototype.adjustKakato = adjustKakato;\r
205   \r
206   function drawStrokesArray(polygons, strokesArray){\r
207     for(var i = 0; i < strokesArray.length; i++){\r
208       dfDrawFont(this, polygons,\r
209                  strokesArray[i][0],\r
210                  strokesArray[i][1],\r
211                  strokesArray[i][2],\r
212                  strokesArray[i][3],\r
213                  strokesArray[i][4],\r
214                  strokesArray[i][5],\r
215                  strokesArray[i][6],\r
216                  strokesArray[i][7],\r
217                  strokesArray[i][8],\r
218                  strokesArray[i][9],\r
219                  strokesArray[i][10]);\r
220     }\r
221   }\r
222   Kage.prototype.drawStrokesArray = drawStrokesArray;\r
223   \r
224   function drawGlyph(polygons, glyph){ // void\r
225     // [glyph] : [stroke]$[stroke]$.....\r
226     // [stroke] : [column]:[column]:.....\r
227     var strokes = glyph.split("$");\r
228     for(var i = 0; i < strokes.length; i++){\r
229       var columns = strokes[i].split(":");\r
230       if(Math.floor(columns[0]) != 99){\r
231         dfDrawFont(this, polygons,\r
232                    Math.floor(columns[0]),\r
233                    Math.floor(columns[1]), Math.floor(columns[2]),\r
234                    Math.floor(columns[3]), Math.floor(columns[4]),\r
235                    Math.floor(columns[5]), Math.floor(columns[6]),\r
236                    Math.floor(columns[7]), Math.floor(columns[8]),\r
237                    Math.floor(columns[9]), Math.floor(columns[10]));\r
238       } else {\r
239         var buhin = this.kBuhin.search(columns[7]);\r
240         if(buhin != ""){\r
241           this.drawBuhin(polygons, buhin,\r
242                          Math.floor(columns[3]),\r
243                          Math.floor(columns[4]),\r
244                          Math.floor(columns[5]),\r
245                          Math.floor(columns[6]));\r
246         }\r
247       }\r
248     }\r
249   }\r
250   Kage.prototype.drawGlyph = drawGlyph;\r
251   \r
252   function drawBuhin(polygons, glyph, x1, y1, x2, y2){ // void\r
253     var strokes = glyph.split("$");\r
254     for(var i = 0; i < strokes.length; i++){\r
255       var columns = strokes[i].split(":");\r
256       if(Math.floor(columns[0]) != 99){\r
257         dfDrawFont(this, polygons,\r
258                    Math.floor(columns[0]),\r
259                    Math.floor(columns[1]),\r
260                    Math.floor(columns[2]),\r
261                    x1 + Math.floor(columns[3]) * (x2 - x1) / 200,\r
262                    y1 + Math.floor(columns[4]) * (y2 - y1) / 200,\r
263                    x1 + Math.floor(columns[5]) * (x2 - x1) / 200,\r
264                    y1 + Math.floor(columns[6]) * (y2 - y1) / 200,\r
265                    x1 + Math.floor(columns[7]) * (x2 - x1) / 200,\r
266                    y1 + Math.floor(columns[8]) * (y2 - y1) / 200,\r
267                    x1 + Math.floor(columns[9]) * (x2 - x1) / 200,\r
268                    y1 + Math.floor(columns[10]) * (y2 - y1) / 200);\r
269       } else {\r
270         var buhin = this.kBuhin.search(columns[7]);\r
271         if(buhin != ""){\r
272           this.drawBuhin(polygons, buhin,\r
273                          x1 + Math.floor(columns[3]) * (x2 - x1) / 200,\r
274                          y1 + Math.floor(columns[4]) * (y2 - y1) / 200,\r
275                          x1 + Math.floor(columns[5]) * (x2 - x1) / 200,\r
276                          y1 + Math.floor(columns[6]) * (y2 - y1) / 200);\r
277         }\r
278       }\r
279     }\r
280   }\r
281   Kage.prototype.drawBuhin = drawBuhin;\r
282   \r
283   //properties\r
284   Kage.prototype.kMincho = 0;\r
285   Kage.prototype.kGothic = 1;\r
286   this.kShotai = this.kMincho;\r
287   \r
288   this.kRate = 100;\r
289   \r
290   if(size == 1){\r
291     this.kMinWidthY = 1.2;\r
292     this.kMinWidthT = 3.6;\r
293     this.kWidth = 3;\r
294     this.kKakato = 1.8;\r
295     this.kL2RDfatten = 1.1;\r
296     this.kMage = 6;\r
297     this.kUseCurve = 0;\r
298     \r
299     this.kAdjustKakatoL = ([8, 5, 3, 1]); // for KAKATO adjustment 000,100,200,300\r
300     this.kAdjustKakatoR = ([4, 3, 2, 1]); // for KAKATO adjustment 000,100,200,300\r
301     this.kAdjustKakatoRangeX = 12; // check area width\r
302     this.kAdjustKakatoRangeY = ([1, 11, 14, 18]); // 3 steps of checking\r
303     this.kAdjustKakatoStep = 3; // number of steps\r
304     \r
305     this.kAdjustUrokoX = ([14, 12, 9, 7]); // for UROKO adjustment 000,100,200,300\r
306     this.kAdjustUrokoY = ([7, 6, 5, 4]); // for UROKO adjustment 000,100,200,300\r
307     this.kAdjustUrokoLength = ([13, 21, 30]); // length for checking\r
308     this.kAdjustUrokoLengthStep = 3; // number of steps\r
309     this.kAdjustUrokoLine = ([13, 15, 18]); // check for crossing. corresponds to length\r
310   } else {\r
311     this.kMinWidthY = 2;\r
312     this.kMinWidthT = 6;\r
313     this.kWidth = 5;\r
314     this.kKakato = 3;\r
315     this.kL2RDfatten = 1.1;\r
316     this.kMage = 10;\r
317     this.kUseCurve = 0;\r
318     \r
319     this.kAdjustKakatoL = ([14, 9, 5, 2]); // for KAKATO adjustment 000,100,200,300\r
320     this.kAdjustKakatoR = ([8, 6, 4, 2]); // for KAKATO adjustment 000,100,200,300\r
321     this.kAdjustKakatoRangeX = 20; // check area width\r
322     this.kAdjustKakatoRangeY = ([1, 19, 24, 30]); // 3 steps of checking\r
323     this.kAdjustKakatoStep = 3; // number of steps\r
324     \r
325     this.kAdjustUrokoX = ([24, 20, 16, 12]); // for UROKO adjustment 000,100,200,300\r
326     this.kAdjustUrokoY = ([12, 11, 9, 8]); // for UROKO adjustment 000,100,200,300\r
327     this.kAdjustUrokoLength = ([22, 36, 50]); // length for checking\r
328     this.kAdjustUrokoLengthStep = 3; // number of steps\r
329     this.kAdjustUrokoLine = ([22, 26, 30]); // check for crossing. corresponds to length\r
330     \r
331     this.kAdjustUroko2Step = 3;\r
332     this.kAdjustUroko2Length = 40;\r
333     \r
334     this.kAdjustTateStep = 4;\r
335     \r
336     this.kAdjustMageStep = 5;\r
337   }\r
338   \r
339   this.kBuhin = new Buhin();\r
340   \r
341   return this;\r
342 }\r