Added adjust KAKATO function.
[chise/kage.git] / engine / kage.js
1 function Kage(){
2   // method
3   function makeGlyph(polygons, buhin){ // void
4     var glyphData = this.kBuhin.search(buhin);
5     if(glyphData != ""){
6       this.drawStrokesArray(polygons, this.adjustKakato(this.getEachStrokes(glyphData)));
7     }
8   }
9   Kage.prototype.makeGlyph = makeGlyph;
10   
11   function getEachStrokes(glyphData){ // strokes array
12     var strokesArray = new Array();
13     var strokes = glyphData.split("$");
14     for(var i = 0; i < strokes.length; i++){
15       var columns = strokes[i].split(":");
16       if(Math.floor(columns[0]) != 99){
17         strokesArray.push([
18           Math.floor(columns[0]),
19           Math.floor(columns[1]),
20           Math.floor(columns[2]),
21           Math.floor(columns[3]),
22           Math.floor(columns[4]),
23           Math.floor(columns[5]),
24           Math.floor(columns[6]),
25           Math.floor(columns[7]),
26           Math.floor(columns[8]),
27           Math.floor(columns[9]),
28           Math.floor(columns[10])
29           ]);
30       } else {
31         var buhin = this.kBuhin.search(columns[7]);
32         if(buhin != ""){
33           strokesArray = strokesArray.concat(this.getEachStrokesOfBuhin(buhin,
34                                                   Math.floor(columns[3]),
35                                                   Math.floor(columns[4]),
36                                                   Math.floor(columns[5]),
37                                                   Math.floor(columns[6]))
38                             );
39         }
40       }
41     }
42     return strokesArray;
43   }
44   Kage.prototype.getEachStrokes = getEachStrokes;
45   
46   function getEachStrokesOfBuhin(buhin, x1, y1, x2, y2){
47     var temp = this.getEachStrokes(buhin);
48     var result = new Array();
49     for(var i = 0; i < temp.length; i++){
50       result.push([temp[i][0],
51                    temp[i][1],
52                    temp[i][2],
53                    x1 + temp[i][3] * (x2 - x1) / 200,
54                    y1 + temp[i][4] * (y2 - y1) / 200,
55                    x1 + temp[i][5] * (x2 - x1) / 200,
56                    y1 + temp[i][6] * (y2 - y1) / 200,
57                    x1 + temp[i][7] * (x2 - x1) / 200,
58                    y1 + temp[i][8] * (y2 - y1) / 200,
59                    x1 + temp[i][9] * (x2 - x1) / 200,
60                    y1 + temp[i][10] * (y2 - y1) / 200]);
61     }
62     return result;
63   }
64   Kage.prototype.getEachStrokesOfBuhin = getEachStrokesOfBuhin;
65   
66   function adjustKakato(strokesArray){ // strokesArray
67     for(var i = 0; i < strokesArray.length; i++){
68       if(strokesArray[i][0] == 1 &&
69          (strokesArray[i][2] == 13 || strokesArray[i][2] == 23)){
70         for(var k = 0; k < this.kAdjustKakatoStep; k++){
71           var crossing = 0;
72           for(var j = 0; j < strokesArray.length && !crossing; j++){
73             if(i == j){ continue; }
74             switch(strokesArray[j][0]){
75             case 0:
76             case 8:
77             case 9:
78               break;
79             case 6:
80             case 7:
81               if(!crossing && isCross(strokesArray[j][7],
82                          strokesArray[j][8],
83                          strokesArray[j][9],
84                          strokesArray[j][10],
85                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
86                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
87                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
88                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k])){
89                 crossing++;
90               }
91               if(!crossing && isCross(strokesArray[j][7],
92                          strokesArray[j][8],
93                          strokesArray[j][9],
94                          strokesArray[j][10],
95                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
96                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
97                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
98                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])){
99                 crossing++;
100               }
101               if(!crossing && isCross(strokesArray[j][7],
102                          strokesArray[j][8],
103                          strokesArray[j][9],
104                          strokesArray[j][10],
105                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
106                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
107                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
108                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])){
109                 crossing++;
110               }
111               if(!crossing && isCross(strokesArray[j][7],
112                          strokesArray[j][8],
113                          strokesArray[j][9],
114                          strokesArray[j][10],
115                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
116                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1],
117                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
118                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])){
119                 crossing++;
120               }
121             case 2:
122             case 12:
123             case 3:
124               if(!crossing && isCross(strokesArray[j][5],
125                          strokesArray[j][6],
126                          strokesArray[j][7],
127                          strokesArray[j][8],
128                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
129                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
130                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
131                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k])){
132                 crossing++;
133               }
134               if(!crossing && isCross(strokesArray[j][5],
135                          strokesArray[j][6],
136                          strokesArray[j][7],
137                          strokesArray[j][8],
138                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
139                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
140                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
141                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])){
142                 crossing++;
143               }
144               if(!crossing && isCross(strokesArray[j][5],
145                          strokesArray[j][6],
146                          strokesArray[j][7],
147                          strokesArray[j][8],
148                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
149                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
150                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
151                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])){
152                 crossing++;
153               }
154               if(!crossing && isCross(strokesArray[j][5],
155                          strokesArray[j][6],
156                          strokesArray[j][7],
157                          strokesArray[j][8],
158                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
159                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1],
160                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
161                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])){
162                 crossing++;
163               }
164             default:
165               if(!crossing && isCross(strokesArray[j][3],
166                          strokesArray[j][4],
167                          strokesArray[j][5],
168                          strokesArray[j][6],
169                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
170                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
171                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
172                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k])){
173                 crossing++;
174               }
175               if(!crossing && isCross(strokesArray[j][3],
176                          strokesArray[j][4],
177                          strokesArray[j][5],
178                          strokesArray[j][6],
179                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
180                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
181                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
182                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])){
183                 crossing++;
184               }
185               if(!crossing && isCross(strokesArray[j][3],
186                          strokesArray[j][4],
187                          strokesArray[j][5],
188                          strokesArray[j][6],
189                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
190                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k],
191                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
192                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])){
193                 crossing++;
194               }
195               if(!crossing && isCross(strokesArray[j][3],
196                          strokesArray[j][4],
197                          strokesArray[j][5],
198                          strokesArray[j][6],
199                          strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,
200                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1],
201                          strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,
202                          strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])){
203                 crossing++;
204               }
205             }
206           }
207           if(crossing){
208             strokesArray[i][2] += (3 - k) * 100;
209             k = Infinity;
210           }
211         }
212       }
213     }
214     return strokesArray;
215   }
216   Kage.prototype.adjustKakato = adjustKakato;
217   
218   function drawStrokesArray(polygons, strokesArray){
219     for(var i = 0; i < strokesArray.length; i++){
220       dfDrawFont(this, polygons,
221                  strokesArray[i][0],
222                  strokesArray[i][1],
223                  strokesArray[i][2],
224                  strokesArray[i][3],
225                  strokesArray[i][4],
226                  strokesArray[i][5],
227                  strokesArray[i][6],
228                  strokesArray[i][7],
229                  strokesArray[i][8],
230                  strokesArray[i][9],
231                  strokesArray[i][10]);
232     }
233   }
234   Kage.prototype.drawStrokesArray = drawStrokesArray;
235   
236   function drawGlyph(polygons, glyph){ // void
237     // [glyph] : [stroke]$[stroke]$.....
238     // [stroke] : [column]:[column]:.....
239     var strokes = glyph.split("$");
240     for(var i = 0; i < strokes.length; i++){
241       var columns = strokes[i].split(":");
242       if(Math.floor(columns[0]) != 99){
243         dfDrawFont(this, polygons,
244                    Math.floor(columns[0]),
245                    Math.floor(columns[1]), Math.floor(columns[2]),
246                    Math.floor(columns[3]), Math.floor(columns[4]),
247                    Math.floor(columns[5]), Math.floor(columns[6]),
248                    Math.floor(columns[7]), Math.floor(columns[8]),
249                    Math.floor(columns[9]), Math.floor(columns[10]));
250       } else {
251         var buhin = this.kBuhin.search(columns[7]);
252         if(buhin != ""){
253           this.drawBuhin(polygons, buhin,
254                          Math.floor(columns[3]),
255                          Math.floor(columns[4]),
256                          Math.floor(columns[5]),
257                          Math.floor(columns[6]));
258         }
259       }
260     }
261   }
262   Kage.prototype.drawGlyph = drawGlyph;
263   
264   function drawBuhin(polygons, glyph, x1, y1, x2, y2){ // void
265     var strokes = glyph.split("$");
266     for(var i = 0; i < strokes.length; i++){
267       var columns = strokes[i].split(":");
268       if(Math.floor(columns[0]) != 99){
269         dfDrawFont(this, polygons,
270                    Math.floor(columns[0]),
271                    Math.floor(columns[1]),
272                    Math.floor(columns[2]),
273                    x1 + Math.floor(columns[3]) * (x2 - x1) / 200,
274                    y1 + Math.floor(columns[4]) * (y2 - y1) / 200,
275                    x1 + Math.floor(columns[5]) * (x2 - x1) / 200,
276                    y1 + Math.floor(columns[6]) * (y2 - y1) / 200,
277                    x1 + Math.floor(columns[7]) * (x2 - x1) / 200,
278                    y1 + Math.floor(columns[8]) * (y2 - y1) / 200,
279                    x1 + Math.floor(columns[9]) * (x2 - x1) / 200,
280                    y1 + Math.floor(columns[10]) * (y2 - y1) / 200);
281       } else {
282         var buhin = this.kBuhin.search(columns[7]);
283         if(buhin != ""){
284           this.drawBuhin(polygons, buhin,
285                          x1 + Math.floor(columns[3]) * (x2 - x1) / 200,
286                          y1 + Math.floor(columns[4]) * (y2 - y1) / 200,
287                          x1 + Math.floor(columns[5]) * (x2 - x1) / 200,
288                          y1 + Math.floor(columns[6]) * (y2 - y1) / 200);
289         }
290       }
291     }
292   }
293   Kage.prototype.drawBuhin = drawBuhin;
294   
295   //properties
296   Kage.prototype.kMincho = 0;
297   Kage.prototype.kGothic = 1;
298   this.kShotai = this.kMincho;
299   this.kMage = 10;
300   this.kRate = 100;
301   this.kMinWidthY = 2;
302   this.kMinWidthT = 6;
303   this.kWidth = 5;
304   this.kAdjustKakatoL = ([14, 9, 5, 2]); // 調整済みカカト用
305   this.kAdjustKakatoR = ([8, 6, 4, 2]); // 調整済みカカト用
306   this.kAdjustKakatoRangeX = 20; // 影響判定矩形の大きさ
307   this.kAdjustKakatoRangeY = ([1, 19, 24, 30]); // 影響判定矩形の大きさ
308   this.kAdjustKakatoStep = 3; // 影響判定矩形の段階
309   this.kKakato = 3;
310   this.kL2RDfatten = 1.1;
311   this.kBuhin = new Buhin();
312   
313   return this;
314 }