Supports width adjustment of MAGE's tail.
[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.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 adjustTate(strokesArray){ // strokesArray\r
96     for(var i = 0; i < strokesArray.length; i++){\r
97       if((strokesArray[i][0] == 1 || strokesArray[i][0] == 3 || strokesArray[i][0] == 7) && strokesArray[i][3] == strokesArray[i][5]){\r
98         for(var j = 0; j < strokesArray.length; j++){\r
99           if(i != j && (strokesArray[j][0] == 1 || strokesArray[j][0] == 3 || strokesArray[j][0] == 7) && strokesArray[j][3] == strokesArray[j][5] &&\r
100              !(strokesArray[i][4] + 1 > strokesArray[j][6] || strokesArray[i][6] - 1 < strokesArray[j][4]) &&\r
101              Math.abs(strokesArray[i][3] - strokesArray[j][3]) < this.kMinWidthT * this.kAdjustTateStep){\r
102             strokesArray[i][1] += (this.kAdjustTateStep - Math.floor(Math.abs(strokesArray[i][3] - strokesArray[j][3]) / this.kMinWidthT)) * 1000;\r
103             if(strokesArray[i][1] > this.kAdjustTateStep * 1000){\r
104               strokesArray[i][1] = strokesArray[i][1] % 1000 + this.kAdjustTateStep * 1000;\r
105             }\r
106           }\r
107         }\r
108       }\r
109     }\r
110     return strokesArray;\r
111   }\r
112   Kage.prototype.adjustTate = adjustTate;\r
113   \r
114   function adjustMage(strokesArray){ // strokesArray\r
115     for(var i = 0; i < strokesArray.length; i++){\r
116       if((strokesArray[i][0] == 3) && strokesArray[i][6] == strokesArray[i][8]){\r
117         for(var j = 0; j < strokesArray.length; j++){\r
118           if(i != j && (\r
119              (strokesArray[j][0] == 1 && strokesArray[j][4] == strokesArray[j][6] &&\r
120               !(strokesArray[i][5] + 1 > strokesArray[j][5] || strokesArray[i][7] - 1 < strokesArray[j][3]) &&\r
121               Math.abs(strokesArray[i][6] - strokesArray[j][4]) < this.kMinWidthT * this.kAdjustMageStep) ||\r
122              (strokesArray[j][0] == 3 && strokesArray[j][6] == strokesArray[j][8] &&\r
123               !(strokesArray[i][5] + 1 > strokesArray[j][7] || strokesArray[i][7] - 1 < strokesArray[j][5]) &&\r
124               Math.abs(strokesArray[i][6] - strokesArray[j][6]) < this.kMinWidthT * this.kAdjustMageStep)\r
125              )){\r
126             strokesArray[i][2] += (this.kAdjustMageStep - Math.floor(Math.abs(strokesArray[i][6] - strokesArray[j][6]) / this.kMinWidthT)) * 1000;\r
127             if(strokesArray[i][2] > this.kAdjustMageStep * 1000){\r
128               strokesArray[i][2] = strokesArray[i][2] % 1000 + this.kAdjustMageStep * 1000;\r
129             }\r
130           }\r
131         }\r
132       }\r
133     }\r
134     return strokesArray;\r
135   }\r
136   Kage.prototype.adjustMage = adjustMage;\r
137   \r
138   function adjustKirikuchi(strokesArray){ // strokesArray\r
139     for(var i = 0; i < strokesArray.length; i++){\r
140       if(strokesArray[i][0] == 2 && strokesArray[i][1] == 32 &&\r
141          strokesArray[i][3] > strokesArray[i][5] &&\r
142          strokesArray[i][4] < strokesArray[i][6]){\r
143         for(var j = 0; j < strokesArray.length; j++){ // no need to skip when i == j\r
144           if(strokesArray[j][0] == 1 &&\r
145              strokesArray[j][3] < strokesArray[i][3] && strokesArray[j][5] > strokesArray[i][3] &&\r
146              strokesArray[j][4] == strokesArray[i][4] && strokesArray[j][4] == strokesArray[j][6]){\r
147             strokesArray[i][1] = 132;\r
148             j = strokesArray.length;\r
149           }\r
150         }\r
151       }\r
152     }\r
153     return strokesArray;\r
154   }\r
155   Kage.prototype.adjustKirikuchi = adjustKirikuchi;\r
156   \r
157   function adjustKakato(strokesArray){ // strokesArray\r
158     for(var i = 0; i < strokesArray.length; i++){\r
159       if(strokesArray[i][0] == 1 &&\r
160          (strokesArray[i][2] == 13 || strokesArray[i][2] == 23)){\r
161         for(var k = 0; k < this.kAdjustKakatoStep; k++){\r
162           if(isCrossBoxWithOthers(strokesArray, i,\r
163                                strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,\r
164                                strokesArray[i][6] + this.kAdjustKakatoRangeY[k],\r
165                                strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,\r
166                                strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])\r
167              | strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1] > 200 // adjust for baseline\r
168              | strokesArray[i][6] - strokesArray[i][4] < this.kAdjustKakatoRangeY[k + 1] // for thin box\r
169              ){\r
170             strokesArray[i][2] += (3 - k) * 100;\r
171             k = Infinity;\r
172           }\r
173         }\r
174       }\r
175     }\r
176     return strokesArray;\r
177   }\r
178   Kage.prototype.adjustKakato = adjustKakato;\r
179   \r
180   function drawStrokesArray(polygons, strokesArray){\r
181     for(var i = 0; i < strokesArray.length; i++){\r
182       dfDrawFont(this, polygons,\r
183                  strokesArray[i][0],\r
184                  strokesArray[i][1],\r
185                  strokesArray[i][2],\r
186                  strokesArray[i][3],\r
187                  strokesArray[i][4],\r
188                  strokesArray[i][5],\r
189                  strokesArray[i][6],\r
190                  strokesArray[i][7],\r
191                  strokesArray[i][8],\r
192                  strokesArray[i][9],\r
193                  strokesArray[i][10]);\r
194     }\r
195   }\r
196   Kage.prototype.drawStrokesArray = drawStrokesArray;\r
197   \r
198   function drawGlyph(polygons, glyph){ // void\r
199     // [glyph] : [stroke]$[stroke]$.....\r
200     // [stroke] : [column]:[column]:.....\r
201     var strokes = glyph.split("$");\r
202     for(var i = 0; i < strokes.length; i++){\r
203       var columns = strokes[i].split(":");\r
204       if(Math.floor(columns[0]) != 99){\r
205         dfDrawFont(this, polygons,\r
206                    Math.floor(columns[0]),\r
207                    Math.floor(columns[1]), Math.floor(columns[2]),\r
208                    Math.floor(columns[3]), Math.floor(columns[4]),\r
209                    Math.floor(columns[5]), Math.floor(columns[6]),\r
210                    Math.floor(columns[7]), Math.floor(columns[8]),\r
211                    Math.floor(columns[9]), Math.floor(columns[10]));\r
212       } else {\r
213         var buhin = this.kBuhin.search(columns[7]);\r
214         if(buhin != ""){\r
215           this.drawBuhin(polygons, buhin,\r
216                          Math.floor(columns[3]),\r
217                          Math.floor(columns[4]),\r
218                          Math.floor(columns[5]),\r
219                          Math.floor(columns[6]));\r
220         }\r
221       }\r
222     }\r
223   }\r
224   Kage.prototype.drawGlyph = drawGlyph;\r
225   \r
226   function drawBuhin(polygons, glyph, x1, y1, x2, y2){ // void\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]),\r
234                    Math.floor(columns[2]),\r
235                    x1 + Math.floor(columns[3]) * (x2 - x1) / 200,\r
236                    y1 + Math.floor(columns[4]) * (y2 - y1) / 200,\r
237                    x1 + Math.floor(columns[5]) * (x2 - x1) / 200,\r
238                    y1 + Math.floor(columns[6]) * (y2 - y1) / 200,\r
239                    x1 + Math.floor(columns[7]) * (x2 - x1) / 200,\r
240                    y1 + Math.floor(columns[8]) * (y2 - y1) / 200,\r
241                    x1 + Math.floor(columns[9]) * (x2 - x1) / 200,\r
242                    y1 + Math.floor(columns[10]) * (y2 - y1) / 200);\r
243       } else {\r
244         var buhin = this.kBuhin.search(columns[7]);\r
245         if(buhin != ""){\r
246           this.drawBuhin(polygons, buhin,\r
247                          x1 + Math.floor(columns[3]) * (x2 - x1) / 200,\r
248                          y1 + Math.floor(columns[4]) * (y2 - y1) / 200,\r
249                          x1 + Math.floor(columns[5]) * (x2 - x1) / 200,\r
250                          y1 + Math.floor(columns[6]) * (y2 - y1) / 200);\r
251         }\r
252       }\r
253     }\r
254   }\r
255   Kage.prototype.drawBuhin = drawBuhin;\r
256   \r
257   //properties\r
258   Kage.prototype.kMincho = 0;\r
259   Kage.prototype.kGothic = 1;\r
260   this.kShotai = this.kMincho;\r
261   \r
262   this.kRate = 100;\r
263   \r
264   if(size == 1){\r
265     this.kMinWidthY = 1.2;\r
266     this.kMinWidthT = 3.6;\r
267     this.kWidth = 3;\r
268     this.kKakato = 1.8;\r
269     this.kL2RDfatten = 1.1;\r
270     this.kMage = 6;\r
271     this.kUseCurve = 0;\r
272     \r
273     this.kAdjustKakatoL = ([8, 5, 3, 1]); // for KAKATO adjustment 000,100,200,300\r
274     this.kAdjustKakatoR = ([4, 3, 2, 1]); // for KAKATO adjustment 000,100,200,300\r
275     this.kAdjustKakatoRangeX = 12; // check area width\r
276     this.kAdjustKakatoRangeY = ([1, 11, 14, 18]); // 3 steps of checking\r
277     this.kAdjustKakatoStep = 3; // number of steps\r
278     \r
279     this.kAdjustUrokoX = ([14, 12, 9, 7]); // for UROKO adjustment 000,100,200,300\r
280     this.kAdjustUrokoY = ([7, 6, 5, 4]); // for UROKO adjustment 000,100,200,300\r
281     this.kAdjustUrokoLength = ([13, 21, 30]); // length for checking\r
282     this.kAdjustUrokoLengthStep = 3; // number of steps\r
283     this.kAdjustUrokoLine = ([13, 15, 18]); // check for crossing. corresponds to length\r
284   } else {\r
285     this.kMinWidthY = 2;\r
286     this.kMinWidthT = 6;\r
287     this.kWidth = 5;\r
288     this.kKakato = 3;\r
289     this.kL2RDfatten = 1.1;\r
290     this.kMage = 10;\r
291     this.kUseCurve = 0;\r
292     \r
293     this.kAdjustKakatoL = ([14, 9, 5, 2]); // for KAKATO adjustment 000,100,200,300\r
294     this.kAdjustKakatoR = ([8, 6, 4, 2]); // for KAKATO adjustment 000,100,200,300\r
295     this.kAdjustKakatoRangeX = 20; // check area width\r
296     this.kAdjustKakatoRangeY = ([1, 19, 24, 30]); // 3 steps of checking\r
297     this.kAdjustKakatoStep = 3; // number of steps\r
298     \r
299     this.kAdjustUrokoX = ([24, 20, 16, 12]); // for UROKO adjustment 000,100,200,300\r
300     this.kAdjustUrokoY = ([12, 11, 9, 8]); // for UROKO adjustment 000,100,200,300\r
301     this.kAdjustUrokoLength = ([22, 36, 50]); // length for checking\r
302     this.kAdjustUrokoLengthStep = 3; // number of steps\r
303     this.kAdjustUrokoLine = ([22, 26, 30]); // check for crossing. corresponds to length\r
304     \r
305     this.kAdjustTateStep = 4;\r
306     \r
307     this.kAdjustMageStep = 5;\r
308   }\r
309   \r
310   this.kBuhin = new Buhin();\r
311   \r
312   return this;\r
313 }\r