Adjust LR combine function.
[chise/kage.git] / kagecgi / kagecgi.c
1 //kagecgi.c
2 //
3
4 #include "kage.h"
5 #include "kagecgi.h"
6 #include "sysdep.h"
7
8 int main(int argc, char *argv[]){
9   KGString *tmp1, *tmp2, *test1, *test2, *filename;
10   FILE *err;
11   char errbuf[errorFileSize];
12   char *pos, *cur;
13   int dummy;
14   int type;
15   
16   dummy = initDB();
17   
18   //set default
19   kShotai = kMincho;
20   //kShotai = kGothic;
21   kSize = 200;
22   kType = 0; //png
23   kInput = 0; //ids or direct
24   kResultText = kg_string_new("");
25   kMode = 0;
26   
27   //confirm request
28   type = 0;
29   //GET request
30   if(type == 0){
31     tmp1 = kg_string_new((kgchar *)getenv("QUERY_STRING"));
32     if(tmp1->len != 0) type = 2;
33   }
34   //argv(detect after GET request)
35   if(type == 0){
36     tmp1 = kg_string_new((kgchar *)argv[1]);
37     if(tmp1->len != 0) type = 1;
38   }
39   //redirect
40   if(type == 0){
41     tmp1 = kg_string_new((kgchar *)getenv("REDIRECT_URL"));
42     if(tmp1->len != 0) type = 3;
43   }
44   //error
45   if(type == 0){
46     fprintf(stderr, "Request Error.\n");
47     return 0;
48   }
49   pos = tmp1->str;
50   
51   //separate token
52   if(type == 1 || type == 2){ //argv or GET request
53     while(1){
54       cur = strchr(pos, '&');
55       tmp2 = kg_string_new(pos);
56       if(cur != NULL) kg_string_set_size(tmp2, cur - pos);
57       //got request string
58       if(strncmp(tmp2->str, "shotai=mincho", 13) == 0) kShotai = kMincho;
59       else if(strncmp(tmp2->str, "shotai=gothic", 13) == 0) kShotai = kGothic;
60       else if(strncmp(tmp2->str, "shotai=skeleton", 15) == 0) kShotai = kGothic;
61       else if(strncmp(tmp2->str, "type=png", 8) == 0) kType = 0;
62       else if(strncmp(tmp2->str, "type=svg", 8) == 0) kType = 1;
63       else if(strncmp(tmp2->str, "type=eps", 8) == 0) kType = 2;
64       else if(strncmp(tmp2->str, "type=raw", 8) == 0) kType = 3;
65       else if(strncmp(tmp2->str, "input=ids", 9) == 0) kInput = 0;
66       else if(strncmp(tmp2->str, "input=directwithadjust", 22) == 0) kInput = 2;
67       else if(strncmp(tmp2->str, "input=direct", 12) == 0) kInput = 1;
68       else if(strncmp(tmp2->str, "size=24", 7) == 0) kSize = 24;
69       else if(strncmp(tmp2->str, "size=200", 8) == 0) kSize = 200;
70       else test1 = kg_string_new(tmp2->str);
71       if(cur == NULL) break;
72       pos = cur + 1;
73     }
74   }
75   else{ // redirected request
76     kInput = 0;
77     while(1){
78       cur = strchr(pos, '/');
79       tmp2 = kg_string_new(pos);
80       if(cur != NULL) kg_string_set_size(tmp2, cur - pos);
81       //got request string
82       if(strncmp(tmp2->str, "mincho", 6) == 0) kShotai = kMincho;
83       else if(strncmp(tmp2->str, "gothic", 6) == 0) kShotai = kGothic;
84       else if(strncmp(tmp2->str, "skeleton", 8) == 0) kShotai = kGothic;
85       else if(strncmp(tmp2->str, "v0.4", 4) == 0);
86       else test1 = kg_string_new(tmp2->str);
87       if(cur == NULL) break;
88       pos = cur + 1;
89     }
90     if(strncmp(test1->str + test1->len - 4, ".png", 4) == 0) kType = 0;
91     if(strncmp(test1->str + test1->len - 4, ".svg", 4) == 0) kType = 1;
92     if(strncmp(test1->str + test1->len - 4, ".eps", 4) == 0) kType = 2;
93     if(strncmp(test1->str + test1->len - 4, ".raw", 4) == 0) kType = 3;
94     kg_string_set_size(test1, test1->len - 4);
95   }
96   
97   //clear result buffer
98   test2 = kg_string_new("");
99   if(kType == 1){ //svg
100     kg_string_append(kResultText, "<?xml version=\"1.0\"?>\n");
101     kg_string_append(kResultText, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n");
102     kg_string_append(kResultText, "<svg width=\"1024\" height=\"1024\" viewBox=\"0 0 1024 1024\">");
103     kg_string_append(kResultText, "<g style=\"fill: black; stroke: black\">");
104   }
105   else if(kType == 2){ //eps
106     kg_string_append(kResultText, "%!PS-Adobe-3.0 EPSF-3.0\n");
107     kg_string_append(kResultText, "%%BoundingBox: 0 -208 1024 816\n");
108     kg_string_append(kResultText, "%%Pages: 0\n");
109     kg_string_append(kResultText, "%%Title: ");
110     kg_string_append(kResultText, test1->str);
111     kg_string_append(kResultText, "\n");
112     kg_string_append(kResultText, "%%Creator: KAGE System\n");
113     kg_string_append(kResultText, "%%CreationDate: 00:00 1-1-2004\n");
114     kg_string_append(kResultText, "%%EndComments\n");
115     kg_string_append(kResultText, "%%EndProlog\n");
116     kg_string_append(kResultText, "%%Page \"");
117     kg_string_append(kResultText, test1->str);
118     kg_string_append(kResultText, "\" 1\n");
119     kg_string_append(kResultText, "newpath\n");
120   }
121   kageCanvas = initPng(canvasWidth, canvasHeight);
122   if(kInput == 0) generateGlyph(test1, test2);
123   else{
124     convert99(test1, test2);
125     //    kg_string_append(test2, test1->str);
126   }
127   
128   if(kType == 0){ //png(image)
129     if(test2->len != 0){
130       if(kInput != 1){ //0 and 2
131         test2 = CalcSizes(test2, 1);
132       }
133       DrawBox();
134       drawGlyph(test2, 0);
135       //output to file
136       filename = kg_string_new(pngFilePath);
137       if(kShotai == kMincho) kg_string_append(filename, "mincho/");
138       else if(kShotai == kGothic) kg_string_append(filename, "gothic/");//skeleton??
139       kg_string_append(filename, test1->str);
140       kg_string_append(filename, ".png");
141       
142       //skip for adjustment mode
143       //fp = fopen(filename->str, "w");
144       //writePng(pngWidth, pngHeight, kageCanvas, fp);
145       //fclose(fp);
146       //output to stdout
147       if(type != 1) fprintf(stdout, "Content-type: image/png\n\n");
148       writePng(pngWidth, pngHeight, kageCanvas, stdout);
149       //done
150       closePng(pngWidth, pngHeight, kageCanvas);
151     }
152     else{
153       err = fopen(errorFileName, "r");
154       fread(errbuf, sizeof(char), errorFileSize, err);
155       //printf("An error occurred.\r\n");
156       if(type != 1) fprintf(stdout, "Content-type: image/png\n\n");
157       fwrite(errbuf, sizeof(char), errorFileSize, stdout);
158       fclose(err);
159     }
160   }
161   else if(kType == 1){ //svg(vector graphics)
162     if(test2->len != 0){
163       test2 = CalcSizes(test2, 1);
164       kMode = 1;
165       drawGlyph(test2, 0);
166       kg_string_append(kResultText, "</g></svg>\n");
167       if(type != 1) fprintf(stdout, "Content-type: image/svg-xml\n\n");
168       fprintf(stdout, "%s", kResultText->str);
169     }
170     else{
171       if(type != 1) fprintf(stdout, "Content-type: text/plain\n\n");
172       fprintf(stdout, "An error occurred.");
173     }
174   }
175   else if(kType == 2){ //eps(vector graphics)
176     if(test2->len != 0){
177       test2 = CalcSizes(test2, 1);
178       kMode = 2;
179       drawGlyph(test2, 0);
180       kg_string_append(kResultText, "fill\n");
181       kg_string_append(kResultText, "%%EOF\n");
182       if(type != 1) fprintf(stdout, "Content-type: application/postscript\n\n");
183       fprintf(stdout, "%s", kResultText->str);
184     }
185     else{
186       if(type != 1) fprintf(stdout, "Content-type: text/plain\n\n");
187       fprintf(stdout, "An error occurred.");
188     }
189   }
190   else{ //raw(text)
191     
192     if(test2->len != 0){
193       test2 = CalcSizes(test2, 1);
194       if(type != 1) fprintf(stdout, "Content-type: text/plain\n\n");
195       fprintf(stdout, "result=%s", test2->str);
196     }
197     else{
198       if(type != 1) fprintf(stdout, "Content-type: text/plain\n\n");
199       fprintf(stdout, "result=nodata");
200     }
201   }
202   dummy = closeDB();
203   
204   return 0;
205 }
206