Merge branch 'master' of git://git.ucc.asn.au/ipdf/code
[ipdf/code.git] / src / main.cpp
1 #include "main.h"
2 #include <unistd.h> // Because we can.
3 #include "stb_truetype.h"
4 int main(int argc, char ** argv)
5 {       
6         #ifndef __STDC_IEC_559__
7         Warn("__STDC_IEC_559__ not defined. IEEE 754 floating point not fully supported.\n");
8         #endif
9
10
11         Debug("Compiled with REAL = %d => \"%s\" sizeof(Real) == %d bytes", REAL, g_real_name[REAL], sizeof(Real));
12
13         Document doc;
14         srand(time(NULL));
15
16         enum {OUTPUT_TO_BMP, LOOP} mode = LOOP;
17         
18         
19         Colour c(0,0,0,1);
20         const char * input_bmp = NULL;
21         const char * output_bmp = NULL;
22         const char * input_filename = NULL;
23         float b[4] = {0,0,1,1};
24
25         int i = 0;
26         while (++i < argc)
27         {
28                 if (argv[i][0] != '-')
29                 {
30                         input_filename = argv[i];
31                         continue;               
32                 }
33                 switch (argv[i][1])
34                 {
35                         case 'o':
36                                 mode = OUTPUT_TO_BMP;
37                                 if (++i >= argc)
38                                         Fatal("No input argument following -o switch");
39                                 input_bmp = argv[i];
40                                 if (++i >= argc)
41                                         Fatal("No output argument following -o switch");
42                                 output_bmp = argv[i];
43
44                                 break;
45                         case 'c':
46                         {
47                                 Debug("Reading paint colour");
48                                 for (int j = 1; j <= 4; ++j)
49                                 {
50                                         if (i+j >= argc)
51                                                 Fatal("No %d colour component following -c switch", j);
52                                         char * e;
53                                         float * comp = (j == 1) ? (&c.r) : ((j == 2) ? (&c.g) : ((j == 3) ? (&c.b) : &(c.a)));
54                                         *comp = strtof(argv[i+j], &e);
55                                         if (*e != '\0')
56                                                 Fatal("Colour component %d not a valid float", j); 
57                                 }
58                                 i += 4;
59                                 break;
60                         }
61                         case 'b':
62                         {
63                                 Debug("Reading view bounds");
64                                 for (int j = 1; j <= 4; ++j)
65                                 {
66                                         if (i+j >= argc)
67                                                 Fatal("No %d bounds component following -b switch", j);
68                                         char * e;
69                                         b[j-1] = strtof(argv[i+j], &e);
70                                         if (*e != '\0')
71                                                 Fatal("Bounds component %d not a valid float", j); 
72                                 }
73                                 i += 4;
74                                 break;
75                         }
76                 }       
77         }
78
79         if (input_filename != NULL)
80         {
81                 doc.LoadSVG(input_filename);
82         }
83         else 
84         {
85 /*              doc.AddBezierData(Bezier(0,0,0,1,1,0));
86                 doc.AddBezierData(Bezier(0,0,1,0,0,1));
87                 doc.AddBezierData(Bezier(0,0,1,1,1,0));
88                 doc.AddBezierData(Bezier(0,1,1,0,0,1));*/
89                 
90                 stbtt_fontinfo font;
91                 FILE *font_file = fopen("DejaVuSansMono.ttf", "rb");
92                 fseek(font_file, 0, SEEK_END);
93                 size_t font_file_size = ftell(font_file);
94                 fseek(font_file, 0, SEEK_SET);
95                 unsigned char *font_file_data = (unsigned char*)malloc(font_file_size);
96                 SDL_assert(fread(font_file_data, 1, font_file_size, font_file) == font_file_size);
97                 fclose(font_file);
98                 stbtt_InitFont(&font, font_file_data, 0);
99
100                 float font_scale = stbtt_ScaleForPixelHeight(&font, 1);
101                 doc.AddFontGlyphAtPoint(&font,'a', Real(font_scale), Real(0), Real(1));  
102                 doc.AddFontGlyphAtPoint(&font,'b', Real(font_scale), Real(0.5), Real(1));  
103                 doc.AddFontGlyphAtPoint(&font,'c', Real(font_scale), Real(1), Real(1));  
104                 doc.AddFontGlyphAtPoint(&font,'d', Real(font_scale), Real(1.5), Real(1));  
105
106                 free(font_file_data);
107                 
108                 
109                 for(int x = 0; x < 8; ++x)
110                 {
111                         
112                         for (int y = 0; y < 8; ++y)
113                         {
114                                 //doc.Add(static_cast<IPDF::ObjectType>((x^y)%3), Rect(0.2+x-4.0,0.2+y-4.0,0.6,0.6));
115                                 //doc.Add(BEZIER, Rect(0.2+x-4.0, 0.2+y-4.0, 0.6,0.6), (x^y)%3);
116                         }
117                 }
118         /*      doc.Add(BEZIER, Rect(0.1,0.1,0.8,0.8), 0);
119                 doc.Add(BEZIER, Rect(0.1,0.1,0.8,0.8), 1);
120                 doc.Add(BEZIER, Rect(0.1,0.1,0.8,0.8), 2);
121                 doc.Add(BEZIER, Rect(0.1,0.1,0.8,0.8), 3);*/
122                 //doc.Add(CIRCLE_FILLED, Rect(0.1,0.1,0.8,0.8), 0);
123         }
124         Debug("Start!");
125         Rect bounds(b[0],b[1],b[2],b[3]);
126
127         if (mode == LOOP)
128                 MainLoop(doc, bounds, c);
129         else if (mode == OUTPUT_TO_BMP)
130                 OverlayBMP(doc, input_bmp, output_bmp, bounds, c);
131         return 0;
132 }

UCC git Repository :: git.ucc.asn.au