Adjust LR combine function.
[chise/kage.git] / kagecgi / kageeg.c
1 //kageeg.c
2 //
3
4 #include "kage.h"
5 #include "kagecgi.h"
6 #include "sysdep.h"
7
8 void generateGlyphByIDS(const KGString *in, KGString *out, int flag){
9   KGString *tmp1, *tmp2;
10   
11   //pass this method if 'in' is not UCS parts
12   if((in->str)[0] != 'u'){
13     generateGlyph(in, out);
14     return;
15   }
16   //pass this method if 'in' is place-variant-flag defined
17   if(in->len < 5 && 7 < in->len){
18     generateGlyph(in, out);
19     return;
20   }
21   
22   tmp1 = kg_string_new(in->str);
23   tmp2 = kg_string_new(in->str);
24   
25   //append place flag
26   if(1 <= flag && flag <= 7){
27     if(tmp1->len != 7) kg_string_append(tmp1, "-");
28     if(flag == 1) kg_string_append(tmp1, "01");
29     else if(flag == 2) kg_string_append(tmp1, "02");
30     else if(flag == 3) kg_string_append(tmp1, "03");
31     else if(flag == 4) kg_string_append(tmp1, "04");
32     else if(flag == 5) kg_string_append(tmp1, "05");
33     else if(flag == 6) kg_string_append(tmp1, "06");
34   }
35   
36   generateGlyph(tmp1, out);
37   if(out->len != 0) return;
38   generateGlyph(tmp2, out);
39   if(out->len != 0) return;
40   return;
41 }
42
43 void generateGlyph(const KGString *in, KGString *out){
44   KGString *tmp;
45   tmp = kg_string_new("");
46   kg_string_set_size(out, 0);
47   
48   //search from parts
49   searchPartsData(in, tmp);
50   if(tmp->len != 0){
51     tmp = CalcSizes(tmp, 1);
52     out = kg_string_assign(out, tmp->str);
53     return;
54   }
55   
56   //search from alias
57   searchAliasData(in, tmp);
58   if(tmp->len != 0){
59     generateGlyph(tmp, out);
60     if(out->len == 0) return;
61     //save to cache ... not yet
62     return;
63   }
64   
65   //check if its IDS
66   if(isIDS(in)){
67     doCombine(in, out);
68     if(out->len == 0) return;
69     //save to cache ... not yet
70     return;
71   }
72 }
73
74 void doCombine(const KGString *in, KGString *out){
75   KGString *partIDS1, *partIDS2, *partIDS3;
76   KGString *partStroke1, *partStroke2, *partStroke3;
77   int result[12];
78   
79   partIDS1 = kg_string_new("");
80   partIDS2 = kg_string_new("");
81   partIDS3 = kg_string_new("");
82   partStroke1 = kg_string_new("");
83   partStroke2 = kg_string_new("");
84   partStroke3 = kg_string_new("");
85   
86   kg_string_set_size(out, 0);
87   
88   //check first IDC
89   if(strncmp(in->str, "u2ff", 4) != 0) return;
90   //switch by combine types
91   switch((in->str)[4]){
92   case '0':
93     divideInto2(in, partIDS1, partIDS3);
94     if(partIDS1->len == 0) return;
95     //ready each parts
96     generateGlyphByIDS(partIDS1, partStroke1, 1);
97     if(partStroke1->len == 0) return;
98     partStroke1 = CalcSizes(partStroke1, 0);
99     generateGlyphByIDS(partIDS3, partStroke3, 2);
100     if(partStroke3->len == 0) return;
101     partStroke3 = CalcSizes(partStroke3, 0);
102     break;
103   case '1':
104     divideInto2(in, partIDS1, partIDS3);
105     if(partIDS1->len == 0) return;
106     //ready each parts
107     generateGlyphByIDS(partIDS1, partStroke1, 3);
108     if(partStroke1->len == 0) return;
109     partStroke1 = CalcSizes(partStroke1, 0);
110     generateGlyphByIDS(partIDS3, partStroke3, 4);
111     if(partStroke3->len == 0) return;
112     partStroke3 = CalcSizes(partStroke3, 0);
113     break;
114   case '4': //full surround
115   case '5': //surround from above
116   case '6': //surround from below
117   case '7': //surround from left
118   case '8': //surround from upper left
119   case '9': //surround from upper right
120   case 'a': //surround from lower left
121     //case 'b': //overlaid (not supported)
122     divideInto2(in, partIDS1, partIDS3);
123     if(partIDS1->len == 0) return;
124     //ready each parts
125     generateGlyphByIDS(partIDS1, partStroke1, 5);
126     if(partStroke1->len == 0) return;
127     partStroke1 = CalcSizes(partStroke1, 0);
128     generateGlyphByIDS(partIDS3, partStroke3, 6);
129     if(partStroke3->len == 0) return;
130     partStroke3 = CalcSizes(partStroke3, 0);
131     break;
132   case '2':
133     divideInto3(in, partIDS1, partIDS2, partIDS3);
134     if(partIDS1->len == 0) return;
135     //ready each parts
136     generateGlyphByIDS(partIDS1, partStroke1, 1);
137     if(partStroke1->len == 0) return;
138     partStroke1 = CalcSizes(partStroke1, 0);
139     generateGlyphByIDS(partIDS2, partStroke2, 1);
140     if(partStroke2->len == 0) return;
141     partStroke2 = CalcSizes(partStroke2, 0);
142     generateGlyphByIDS(partIDS3, partStroke3, 2);
143     if(partStroke3->len == 0) return;
144     partStroke3 = CalcSizes(partStroke3, 0);
145     break;
146   case '3':
147     divideInto3(in, partIDS1, partIDS2, partIDS3);
148     if(partIDS1->len == 0) return;
149     //ready each parts
150     generateGlyphByIDS(partIDS1, partStroke1, 3);
151     if(partStroke1->len == 0) return;
152     partStroke1 = CalcSizes(partStroke1, 0);
153     generateGlyphByIDS(partIDS2, partStroke2, 3);
154     if(partStroke2->len == 0) return;
155     partStroke2 = CalcSizes(partStroke2, 0);
156     generateGlyphByIDS(partIDS3, partStroke3, 4);
157     if(partStroke3->len == 0) return;
158     partStroke3 = CalcSizes(partStroke3, 0);
159     break;
160   }
161   switch((in->str)[4]){
162   case '0':
163     combineYoko2(partStroke1, partStroke3, result);
164     break;
165   case '1':
166     combineTate2(partStroke1, partStroke3, result);
167     break;
168   case '2':
169     combineYoko3(partStroke1, partStroke2, partStroke3, result);
170     break;
171   case '3':
172     combineTate3(partStroke1, partStroke2, partStroke3, result);
173     break;
174   case '4': //full surround
175     combineHame2(partStroke1, partStroke3, result,
176                  FLAG_SURROUND_LEFT |
177                  FLAG_SURROUND_RIGHT |
178                  FLAG_SURROUND_TOP |
179                  FLAG_SURROUND_BOTTOM);
180     break;
181   case '5': //surround from above
182     combineHame2(partStroke1, partStroke3, result,
183                  FLAG_SURROUND_LEFT |
184                  FLAG_SURROUND_RIGHT |
185                  FLAG_SURROUND_TOP);
186     break;
187   case '6': //surround from below
188     combineHame2(partStroke1, partStroke3, result,
189                  FLAG_SURROUND_LEFT |
190                  FLAG_SURROUND_RIGHT |
191                  FLAG_SURROUND_BOTTOM);
192     break;
193   case '7': //surround from left
194     combineHame2(partStroke1, partStroke3, result,
195                  FLAG_SURROUND_LEFT |
196                  FLAG_SURROUND_TOP |
197                  FLAG_SURROUND_BOTTOM);
198     break;
199   case '8': //surround from upper left
200     combineHame2(partStroke1, partStroke3, result,
201                  FLAG_SURROUND_LEFT |
202                  FLAG_SURROUND_TOP);
203     break;
204   case '9': //surround from upper right
205     combineHame2(partStroke1, partStroke3, result,
206                  FLAG_SURROUND_RIGHT |
207                  FLAG_SURROUND_TOP);
208     break;
209   case 'a': //surround from lower left
210     combineHame2(partStroke1, partStroke3, result,
211                  FLAG_SURROUND_LEFT |
212                  FLAG_SURROUND_BOTTOM);
213     break;
214     //case 'b': //overlaid (not supported)
215   }
216   switch((in->str)[4]){
217   case '0':
218   case '1':
219   case '4':
220   case '5':
221   case '6':
222   case '7':
223   case '8':
224   case '9':
225   case 'a':
226     //case 'b':
227     addStrokeWithTransform(partStroke1, 1, result, out, 1);
228     addStrokeWithTransform(partStroke3, 3, result, out, 1);
229     break;
230   case '2':
231   case '3':
232     addStrokeWithTransform(partStroke1, 1, result, out, 1);
233     addStrokeWithTransform(partStroke2, 2, result, out, 1);
234     addStrokeWithTransform(partStroke3, 3, result, out, 1);
235     break;
236   }
237   fprintf(stderr,"%s\n",out->str);
238 }
239
240 void drawGlyph(const KGString *in, const int mode){
241   int i, j;
242   int *buf;
243   buf = convertStroke(in->str, buf, &j);
244   for(i = 0; i < j; i++){
245     if(mode == 0){ //normal
246       dfDrawFont(buf[i * 11 + 0],
247                  buf[i * 11 + 1],
248                  buf[i * 11 + 2],
249                  buf[i * 11 + 3],
250                  buf[i * 11 + 4],
251                  buf[i * 11 + 5],
252                  buf[i * 11 + 6],
253                  buf[i * 11 + 7],
254                  buf[i * 11 + 8],
255                  buf[i * 11 + 9],
256                  buf[i * 11 + 10]);
257     }
258     else if(mode == 1){ //without decoration
259       dfDrawFont(buf[i * 11 + 0],
260                  0,
261                  0,
262                  buf[i * 11 + 3],
263                  buf[i * 11 + 4],
264                  buf[i * 11 + 5],
265                  buf[i * 11 + 6],
266                  buf[i * 11 + 7],
267                  buf[i * 11 + 8],
268                  buf[i * 11 + 9],
269                  buf[i * 11 + 10]);
270     }
271   }
272   free(buf);
273 }
274