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