(1)Make fatten the bezier curve (2)Adjust box's Kakato (3)Update sample
[chise/kage.git] / engine / kage.js
1 function Kage(){\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.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 adjustKirikuchi(strokesArray){ // strokesArray\r
96     for(var i = 0; i < strokesArray.length; i++){\r
97       if(strokesArray[i][0] == 2 && strokesArray[i][1] == 32 &&\r
98          strokesArray[i][3] > strokesArray[i][5] &&\r
99          strokesArray[i][4] < strokesArray[i][6]){\r
100         for(var j = 0; j < strokesArray.length; j++){ // no need to skip when i == j\r
101           if(strokesArray[j][0] == 1 &&\r
102              strokesArray[j][3] < strokesArray[i][3] && strokesArray[j][5] > strokesArray[i][3] &&\r
103              strokesArray[j][4] == strokesArray[i][4] && strokesArray[j][4] == strokesArray[j][6]){\r
104             strokesArray[i][1] = 132;\r
105             j = strokesArray.length;\r
106           }\r
107         }\r
108       }\r
109     }\r
110     return strokesArray;\r
111   }\r
112   Kage.prototype.adjustKirikuchi = adjustKirikuchi;\r
113         \r
114   function adjustKakato(strokesArray){ // strokesArray\r
115     for(var i = 0; i < strokesArray.length; i++){\r
116       if(strokesArray[i][0] == 1 &&\r
117          (strokesArray[i][2] == 13 || strokesArray[i][2] == 23)){\r
118         for(var k = 0; k < this.kAdjustKakatoStep; k++){\r
119           if(isCrossBoxWithOthers(strokesArray, i,\r
120                                strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,\r
121                                strokesArray[i][6] + this.kAdjustKakatoRangeY[k],\r
122                                strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,\r
123                                strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])\r
124              | strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1] > 200 // adjust for baseline\r
125              | strokesArray[i][6] - strokesArray[i][4] < this.kAdjustKakatoRangeY[k + 1] // for thin box\r
126              ){\r
127             strokesArray[i][2] += (3 - k) * 100;\r
128             k = Infinity;\r
129           }\r
130         }\r
131       }\r
132     }\r
133     return strokesArray;\r
134   }\r
135   Kage.prototype.adjustKakato = adjustKakato;\r
136   \r
137   function drawStrokesArray(polygons, strokesArray){\r
138     for(var i = 0; i < strokesArray.length; i++){\r
139       dfDrawFont(this, polygons,\r
140                  strokesArray[i][0],\r
141                  strokesArray[i][1],\r
142                  strokesArray[i][2],\r
143                  strokesArray[i][3],\r
144                  strokesArray[i][4],\r
145                  strokesArray[i][5],\r
146                  strokesArray[i][6],\r
147                  strokesArray[i][7],\r
148                  strokesArray[i][8],\r
149                  strokesArray[i][9],\r
150                  strokesArray[i][10]);\r
151     }\r
152   }\r
153   Kage.prototype.drawStrokesArray = drawStrokesArray;\r
154   \r
155   function drawGlyph(polygons, glyph){ // void\r
156     // [glyph] : [stroke]$[stroke]$.....\r
157     // [stroke] : [column]:[column]:.....\r
158     var strokes = glyph.split("$");\r
159     for(var i = 0; i < strokes.length; i++){\r
160       var columns = strokes[i].split(":");\r
161       if(Math.floor(columns[0]) != 99){\r
162         dfDrawFont(this, polygons,\r
163                    Math.floor(columns[0]),\r
164                    Math.floor(columns[1]), Math.floor(columns[2]),\r
165                    Math.floor(columns[3]), Math.floor(columns[4]),\r
166                    Math.floor(columns[5]), Math.floor(columns[6]),\r
167                    Math.floor(columns[7]), Math.floor(columns[8]),\r
168                    Math.floor(columns[9]), Math.floor(columns[10]));\r
169       } else {\r
170         var buhin = this.kBuhin.search(columns[7]);\r
171         if(buhin != ""){\r
172           this.drawBuhin(polygons, buhin,\r
173                          Math.floor(columns[3]),\r
174                          Math.floor(columns[4]),\r
175                          Math.floor(columns[5]),\r
176                          Math.floor(columns[6]));\r
177         }\r
178       }\r
179     }\r
180   }\r
181   Kage.prototype.drawGlyph = drawGlyph;\r
182   \r
183   function drawBuhin(polygons, glyph, x1, y1, x2, y2){ // void\r
184     var strokes = glyph.split("$");\r
185     for(var i = 0; i < strokes.length; i++){\r
186       var columns = strokes[i].split(":");\r
187       if(Math.floor(columns[0]) != 99){\r
188         dfDrawFont(this, polygons,\r
189                    Math.floor(columns[0]),\r
190                    Math.floor(columns[1]),\r
191                    Math.floor(columns[2]),\r
192                    x1 + Math.floor(columns[3]) * (x2 - x1) / 200,\r
193                    y1 + Math.floor(columns[4]) * (y2 - y1) / 200,\r
194                    x1 + Math.floor(columns[5]) * (x2 - x1) / 200,\r
195                    y1 + Math.floor(columns[6]) * (y2 - y1) / 200,\r
196                    x1 + Math.floor(columns[7]) * (x2 - x1) / 200,\r
197                    y1 + Math.floor(columns[8]) * (y2 - y1) / 200,\r
198                    x1 + Math.floor(columns[9]) * (x2 - x1) / 200,\r
199                    y1 + Math.floor(columns[10]) * (y2 - y1) / 200);\r
200       } else {\r
201         var buhin = this.kBuhin.search(columns[7]);\r
202         if(buhin != ""){\r
203           this.drawBuhin(polygons, buhin,\r
204                          x1 + Math.floor(columns[3]) * (x2 - x1) / 200,\r
205                          y1 + Math.floor(columns[4]) * (y2 - y1) / 200,\r
206                          x1 + Math.floor(columns[5]) * (x2 - x1) / 200,\r
207                          y1 + Math.floor(columns[6]) * (y2 - y1) / 200);\r
208         }\r
209       }\r
210     }\r
211   }\r
212   Kage.prototype.drawBuhin = drawBuhin;\r
213   \r
214   //properties\r
215   Kage.prototype.kMincho = 0;\r
216   Kage.prototype.kGothic = 1;\r
217   this.kShotai = this.kMincho;\r
218         \r
219   this.kRate = 100;\r
220         \r
221   this.kMinWidthY = 2;\r
222   this.kMinWidthT = 6;\r
223   this.kWidth = 5;\r
224   this.kKakato = 3;\r
225   this.kL2RDfatten = 1.1;\r
226   this.kMage = 10;\r
227   this.kUseCurve = 0;\r
228         \r
229   this.kAdjustKakatoL = ([14, 9, 5, 2]); // for KAKATO adjustment 000,100,200,300\r
230   this.kAdjustKakatoR = ([8, 6, 4, 2]); // for KAKATO adjustment 000,100,200,300\r
231   this.kAdjustKakatoRangeX = 20; // check area width\r
232   this.kAdjustKakatoRangeY = ([1, 19, 24, 30]); // 3 steps of checking\r
233   this.kAdjustKakatoStep = 3; // number of steps\r
234         \r
235   this.kAdjustUrokoX = ([24, 20, 16, 12]); // for UROKO adjustment 000,100,200,300\r
236   this.kAdjustUrokoY = ([12, 11, 9, 8]); // for UROKO adjustment 000,100,200,300\r
237   this.kAdjustUrokoLength = ([22, 36, 50]); // length for checking\r
238   this.kAdjustUrokoLengthStep = 3; // number of steps\r
239   this.kAdjustUrokoLine = ([22, 26, 30]); // check for crossing. corresponds to length\r
240         \r
241   this.kBuhin = new Buhin();\r
242   \r
243   return this;\r
244 }\r