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