]> git.cworth.org Git - spritext/blob - spritext.c
Refactor argument parsing into parse_args instead of just being in main
[spritext] / spritext.c
1 /* XXX: Copyright/license blurb needed here */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <libgen.h>
7 #include <cairo.h>
8 #include <cairo-ft.h>
9
10 #ifndef CAIRO_HAS_PNG_FUNCTIONS
11 #error This program requires cairo with PNG support
12 #endif
13
14 #include <ft2build.h>
15 #include FT_FREETYPE_H
16
17 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
18
19 #define TRUE (1==1)
20 #define FALSE (!TRUE)
21
22 #if USE_CGIC
23 static cairo_status_t
24 stdio_write (void *closure, const unsigned char *data, unsigned int length);
25 #endif
26
27 double
28 get_max_width (cairo_t *cr, char *characters);
29
30 double
31 get_max_height (cairo_t *cr, char *characters);
32
33 typedef struct {
34     char *family;
35     double size;
36     struct {
37         double red;
38         double green;
39         double blue;
40     } color;
41 } args_t;
42
43 static void
44 parse_args (args_t *args)
45 {
46     /* First some defaults */
47     args->family = "Vera";
48     args->size = 20;
49     args->color.red = 0.0;
50     args->color.green = 0.0;
51     args->color.blue = 0.0;
52
53     /* XXX: Next, we should override the defaults based on
54      * command-line arguments. */
55
56     /* XXX: Finally, we'll want to allow CGI-based arguments as
57      * well. */
58 #if USE_CGIG
59     /* QueryString */
60     int fontsize;
61     char format[5];
62     char fillcolor[20];
63     char fontname[20];
64     cgiFormStringNoNewlines("fontname", fontname, 20);
65     cgiFormInteger("fontsize", &fontsize, 50);
66     cgiFormStringNoNewlines("format", format, 5);
67     cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
68
69     int fillcolor_red;
70     int fillcolor_green;
71     int fillcolor_blue;
72     sscanf (fillcolor, " rgb : %d , %d , %d ", &fillcolor_red, &fillcolor_green, &fillcolor_blue);
73     if ( format[0] == 'j' && format[1] == 's' && format[2] == 'o' && format[3] == 'n' )
74         outputJson = TRUE;
75 #endif
76 }
77
78 int
79 main (void)
80 {
81     /* XXX: Instead of a hard-coded filename here, we should really be
82      * taking args.family and using fontconfig to map that to a
83      * filename. */
84     const char font_filename[] = "./Vera.ttf";
85     char outputJson = FALSE;
86
87     char characters[] = {
88         '!', '"', '#', '$', '%', '&','\'', '(',
89         ')', '*', '+', ',', '-', '.', '/', '0',
90         '1', '2', '3', '4', '5', '6', '7', '8',
91         '9', ':', ';', '<', '=', '>', '?', '@',
92         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
93         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
94         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
95         'Y', 'Z', '[','\\', ']', '^', '_', '`',
96         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
97         'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
98         'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
99         'y', 'z', '{', '|', '}', '~'
100     };
101
102     int i;
103     args_t args;
104
105     parse_args (&args);
106
107 #if USE_CGIC
108     if (outputJson)
109     {
110         cgiHeaderContentType("application/json");
111     } else {
112         cgiHeaderContentType("image/png");
113     }
114 #endif
115
116     int error;  
117     FT_Library  library;
118     FT_Face     ft_face;
119     FT_Bool     use_kerning;
120     FT_UInt     left_index, right_index;
121     FT_Vector   kerning;
122
123     error = FT_Init_FreeType( &library );
124     if ( error ) {
125         fprintf (stderr, "Fatal error initializing freetype.\n");
126         exit (1);
127     }
128
129     error = FT_New_Face( library, font_filename,
130                          0,
131                          &ft_face );
132     if ( error == FT_Err_Unknown_File_Format ) {
133         fprintf (stderr, "Unsupported font format: %s\n", font_filename);
134         exit (1);
135     } else if ( error ) {
136         fprintf (stderr, "Failed to open file: %s\n", font_filename);
137         exit (1);
138     }
139
140     use_kerning = FT_HAS_KERNING( ft_face );
141
142     /* Compute max-width and max-height */
143     cairo_surface_t *surface;
144     cairo_t *cr;
145     cairo_font_face_t *cr_face;
146
147     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
148                                           1, 1);
149
150     cr = cairo_create (surface);
151
152 /*
153   cairo_select_font_face (cr, fontname,
154   CAIRO_FONT_SLANT_NORMAL,
155   CAIRO_FONT_WEIGHT_NORMAL);
156 */
157     cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
158     cairo_set_font_face (cr, cr_face);
159
160     cairo_set_font_size (cr, args.size);
161
162     cairo_set_line_width (cr, 1.0);
163
164     double max_width = get_max_width(cr, characters);
165     double max_height = get_max_height(cr, characters);
166
167     cairo_font_face_destroy (cr_face);
168
169     cairo_destroy (cr);
170
171     cairo_surface_destroy(surface);
172
173     /* Draw */
174     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
175                                           (max_width + 8) * 10,
176                                           (max_height + 8) * 10);
177
178     cr = cairo_create (surface);
179
180 /*
181   cairo_select_font_face (cr, fontname,
182   CAIRO_FONT_SLANT_NORMAL,
183   CAIRO_FONT_WEIGHT_NORMAL);
184 */
185     cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
186     cairo_set_font_face (cr, cr_face);
187
188     cairo_set_font_size (cr, args.size);
189
190     cairo_text_extents_t extents;
191     double x = 0.0;
192     double y = 0.0;
193
194     if (outputJson)
195         printf("{");
196
197     cairo_translate (cr, 0, -(max_height / 2.50));
198
199 //    cairo_translate (cr, 0, -5.0);
200
201     char string[2];
202     string[1] = '\0';
203     for (i = 0; i < ARRAY_SIZE(characters); i++)
204     {
205         if (i % 10 == 0)
206         {
207             x = 0.;
208             y += max_height + 8;
209         }
210         string[0] = characters[i];
211
212         cairo_text_extents (cr, string, &extents);
213
214         if (outputJson)
215         {
216             if (i > 0)
217                 printf (",\n");
218             printf ("u%i:{", characters[i]);
219             printf ("x:%.1f,y:%.1f,w:%.1f,h:%.1f",
220                     x,
221                     y - (max_height * 1.15),
222                     extents.width + extents.x_bearing,
223                     extents.height);
224             int j;
225             for ( j = 0; j < ARRAY_SIZE(characters); j++ )
226             {
227                 right_index = FT_Get_Char_Index( ft_face, characters[i] );
228                 left_index = FT_Get_Char_Index( ft_face, characters[j] );
229     
230                 FT_Get_Kerning( ft_face, left_index, right_index,
231                                 FT_KERNING_UNSCALED, &kerning );
232     
233                 if ( kerning.x )
234                     printf(",k%d:%ld", characters[j], kerning.x);
235             }
236
237             printf ("}");
238         }
239
240         x -= extents.x_bearing;
241   
242         cairo_move_to (cr, x, y);
243         cairo_set_source_rgb (cr, 0., 0., 0.); // black
244         cairo_set_source_rgb (cr,
245                               args.color.red,
246                               args.color.green,
247                               args.color.blue);
248         cairo_text_path (cr, string);
249         cairo_fill (cr);
250
251         x += extents.x_bearing;
252         x += max_width + 8;
253     }
254
255 /*
256   int thick_width = 4;
257   int thin_width = 0;
258
259   for (c = 'A'; c <= 'I'; c++)
260   {
261   string[0] = c;
262   int spacing = ((c - 'A') * letterspacing) + 1;
263
264
265   cairo_save(cr);
266
267   cairo_move_to (cr, spacing, 0);
268   cairo_text_path (cr, string);
269 //      cairo_set_source_rgb (cr, 0.1, 0.2, 0.2); // gray
270 cairo_set_source_rgb (cr, 0.8, 0.8, 0.85); // silver
271 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
272
273 cairo_push_group (cr);
274 cairo_set_line_width (cr, thick_width);
275 cairo_stroke_preserve (cr);
276 cairo_set_line_width (cr, thin_width);
277 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
278 cairo_stroke_preserve (cr);
279 cairo_fill (cr);
280 cairo_pop_group_to_source (cr);
281
282 cairo_paint (cr);
283
284 cairo_restore(cr);
285
286 cairo_move_to (cr, spacing, 0);
287 cairo_text_path (cr, string);
288 cairo_set_source_rgb (cr, 0.4, 0.45, 0.7); // blue
289
290 cairo_fill (cr);
291 }
292 */
293
294     cairo_scale (cr, 1, -1);
295     cairo_push_group (cr);
296 /*
297
298 cairo_pattern_t *gradient;
299
300 for (i = 0; i < 9; i++)
301 {
302 char c[2];
303 strncpy(c, STRING + i, 1);
304 c[1] = '\0';
305 int spacing = (i * letterspacing) + 1;
306 cairo_move_to (cr, spacing, -3);
307 cairo_show_text (cr, c);
308
309 }
310 cairo_pop_group_to_source (cr);
311
312 gradient = cairo_pattern_create_linear (0, 0, 0, -fontsize);
313 cairo_pattern_add_color_stop_rgba (gradient, 0.0, 1, 1, 1, 0.5);
314 cairo_pattern_add_color_stop_rgba (gradient, 0.7, 1, 1, 1, 0.0);
315
316 cairo_mask (cr, gradient);
317
318 cairo_pattern_destroy (gradient);
319 */
320
321 #if USE_CGIC
322     if (outputJson)
323     {
324         printf("}");
325     } else {
326         cairo_surface_write_to_png_stream (surface, stdio_write, cgiOut);
327     }
328 #endif
329
330     cairo_surface_write_to_png (surface, "spritext-output.png");
331     printf ("Result written to spritext-output.png\n");
332
333     cairo_destroy (cr);
334
335     cairo_surface_destroy (surface);
336
337     return 0;
338 }
339
340 #if USE_CGIC
341 static cairo_status_t
342 stdio_write (void *closure, const unsigned char *data, unsigned int length)
343 {
344     FILE *file = closure;
345     if (fwrite (data, 1, length, file) == length)
346         return CAIRO_STATUS_SUCCESS;
347     else
348         return CAIRO_STATUS_WRITE_ERROR;
349 }
350 #endif
351
352 double
353 get_max_width(cairo_t *cr, char *characters)
354 {
355     int i;
356     double max_width = 0.;
357     char string[2];
358     string[1] = '\0';
359     for (i = 0; i < ARRAY_SIZE(characters); i++) {
360         cairo_text_extents_t extents;
361         string[0] = characters[i];
362         cairo_text_extents (cr, string, &extents);
363         if ((extents.width + (extents.x_bearing * 2)) > max_width)
364             max_width = extents.width + (extents.x_bearing * 2);
365     }
366     return max_width;
367 }
368
369 double
370 get_max_height(cairo_t *cr, char *characters)
371 {
372     int i;
373     double max_height = 0.;
374     char string[2];
375     string[1] = '\0';
376     for (i = 0; i < ARRAY_SIZE(characters); i++) {
377         cairo_text_extents_t extents;
378         string[0] = characters[i];
379         cairo_text_extents (cr, string, &extents);
380 //    if ((extents.height - extents.y_bearing) > max_height)
381 //      max_height = extents.height - extents.y_bearing;
382         if ((extents.height) > max_height)
383             max_height = extents.height;
384     }
385     return max_height;
386 }
387