Added UROKO design function(size adjustment).
[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.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){ // \8fc\82Í\83E\83\8d\83R\82È\82¢\82Ì\82Å\96³\8e\8b\81B\82Å\82à\8cv\8eZ\97Ê\82ª\96³\91Ê\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]){ // \89¡\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 adjustKakato(strokesArray){ // strokesArray\r
96     for(var i = 0; i < strokesArray.length; i++){\r
97       if(strokesArray[i][0] == 1 &&\r
98          (strokesArray[i][2] == 13 || strokesArray[i][2] == 23)){\r
99         for(var k = 0; k < this.kAdjustKakatoStep; k++){\r
100           if(isCrossBoxWithOthers(strokesArray, i,\r
101                                strokesArray[i][5] - this.kAdjustKakatoRangeX / 2,\r
102                                strokesArray[i][6] + this.kAdjustKakatoRangeY[k],\r
103                                strokesArray[i][5] + this.kAdjustKakatoRangeX / 2,\r
104                                strokesArray[i][6] + this.kAdjustKakatoRangeY[k + 1])\r
105              ){\r
106             strokesArray[i][2] += (3 - k) * 100;\r
107             k = Infinity;\r
108           }\r
109         }\r
110       }\r
111     }\r
112     return strokesArray;\r
113   }\r
114   Kage.prototype.adjustKakato = adjustKakato;\r
115   \r
116   function drawStrokesArray(polygons, strokesArray){\r
117     for(var i = 0; i < strokesArray.length; i++){\r
118       dfDrawFont(this, polygons,\r
119                  strokesArray[i][0],\r
120                  strokesArray[i][1],\r
121                  strokesArray[i][2],\r
122                  strokesArray[i][3],\r
123                  strokesArray[i][4],\r
124                  strokesArray[i][5],\r
125                  strokesArray[i][6],\r
126                  strokesArray[i][7],\r
127                  strokesArray[i][8],\r
128                  strokesArray[i][9],\r
129                  strokesArray[i][10]);\r
130     }\r
131   }\r
132   Kage.prototype.drawStrokesArray = drawStrokesArray;\r
133   \r
134   function drawGlyph(polygons, glyph){ // void\r
135     // [glyph] : [stroke]$[stroke]$.....\r
136     // [stroke] : [column]:[column]:.....\r
137     var strokes = glyph.split("$");\r
138     for(var i = 0; i < strokes.length; i++){\r
139       var columns = strokes[i].split(":");\r
140       if(Math.floor(columns[0]) != 99){\r
141         dfDrawFont(this, polygons,\r
142                    Math.floor(columns[0]),\r
143                    Math.floor(columns[1]), Math.floor(columns[2]),\r
144                    Math.floor(columns[3]), Math.floor(columns[4]),\r
145                    Math.floor(columns[5]), Math.floor(columns[6]),\r
146                    Math.floor(columns[7]), Math.floor(columns[8]),\r
147                    Math.floor(columns[9]), Math.floor(columns[10]));\r
148       } else {\r
149         var buhin = this.kBuhin.search(columns[7]);\r
150         if(buhin != ""){\r
151           this.drawBuhin(polygons, buhin,\r
152                          Math.floor(columns[3]),\r
153                          Math.floor(columns[4]),\r
154                          Math.floor(columns[5]),\r
155                          Math.floor(columns[6]));\r
156         }\r
157       }\r
158     }\r
159   }\r
160   Kage.prototype.drawGlyph = drawGlyph;\r
161   \r
162   function drawBuhin(polygons, glyph, x1, y1, x2, y2){ // void\r
163     var strokes = glyph.split("$");\r
164     for(var i = 0; i < strokes.length; i++){\r
165       var columns = strokes[i].split(":");\r
166       if(Math.floor(columns[0]) != 99){\r
167         dfDrawFont(this, polygons,\r
168                    Math.floor(columns[0]),\r
169                    Math.floor(columns[1]),\r
170                    Math.floor(columns[2]),\r
171                    x1 + Math.floor(columns[3]) * (x2 - x1) / 200,\r
172                    y1 + Math.floor(columns[4]) * (y2 - y1) / 200,\r
173                    x1 + Math.floor(columns[5]) * (x2 - x1) / 200,\r
174                    y1 + Math.floor(columns[6]) * (y2 - y1) / 200,\r
175                    x1 + Math.floor(columns[7]) * (x2 - x1) / 200,\r
176                    y1 + Math.floor(columns[8]) * (y2 - y1) / 200,\r
177                    x1 + Math.floor(columns[9]) * (x2 - x1) / 200,\r
178                    y1 + Math.floor(columns[10]) * (y2 - y1) / 200);\r
179       } else {\r
180         var buhin = this.kBuhin.search(columns[7]);\r
181         if(buhin != ""){\r
182           this.drawBuhin(polygons, buhin,\r
183                          x1 + Math.floor(columns[3]) * (x2 - x1) / 200,\r
184                          y1 + Math.floor(columns[4]) * (y2 - y1) / 200,\r
185                          x1 + Math.floor(columns[5]) * (x2 - x1) / 200,\r
186                          y1 + Math.floor(columns[6]) * (y2 - y1) / 200);\r
187         }\r
188       }\r
189     }\r
190   }\r
191   Kage.prototype.drawBuhin = drawBuhin;\r
192   \r
193   //properties\r
194   Kage.prototype.kMincho = 0;\r
195   Kage.prototype.kGothic = 1;\r
196   this.kShotai = this.kMincho;\r
197         \r
198   this.kRate = 100;\r
199         \r
200   this.kMinWidthY = 2;\r
201   this.kMinWidthT = 6;\r
202   this.kWidth = 5;\r
203   this.kKakato = 3;\r
204   this.kL2RDfatten = 1.1;\r
205   this.kMage = 10;\r
206         \r
207   this.kAdjustKakatoL = ([14, 9, 5, 2]); // \92²\90®\8dÏ\82Ý\83J\83J\83g\97p 000,100,200,300\r
208   this.kAdjustKakatoR = ([8, 6, 4, 2]); // \92²\90®\8dÏ\82Ý\83J\83J\83g\97p 000,100,200,300\r
209   this.kAdjustKakatoRangeX = 20; // \89e\8b¿\94»\92è\8bé\8c`\82Ì\91å\82«\82³\r
210   this.kAdjustKakatoRangeY = ([1, 19, 24, 30]); // \89e\8b¿\94»\92è\8bé\8c`\82Ì\91å\82«\82³\8b«\8aE\81i3\97Ì\88æ\81j\r
211   this.kAdjustKakatoStep = 3; // \89e\8b¿\94»\92è\8bé\8c`\82Ì\92i\8aK\r
212         \r
213   this.kAdjustUrokoX = ([24, 20, 16, 12]); // \92²\90®\8dÏ\82Ý\83T\83C\83Y 000,100,200,300\r
214   this.kAdjustUrokoY = ([12, 11, 9, 8]); // \92²\90®\8dÏ\82Ý\83T\83C\83Y 000,100,200,300\r
215   this.kAdjustUrokoLength = ([22, 36, 50]); // \89e\8b¿\94»\92è\92·\82³\82Ì\92i\8aK\r
216   this.kAdjustUrokoLengthStep = 3; // \89e\8b¿\94»\92è\92·\82³\82Ì\92i\8aK\r
217   this.kAdjustUrokoLine = ([22, 26, 30]); // \8cð\8d·\82Ì\89e\8b¿\94»\92è\81BLength\82Æ\91Î\89\9e\r
218         \r
219   this.kBuhin = new Buhin();\r
220   \r
221   return this;\r
222 }\r