]> git.cworth.org Git - spritext/blob - spritext.c
5ae64ba2309bb9a17d04ca3929c5175409423cd8
[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 static double
23 get_max_width (cairo_t *cr, const char *characters, int num_characters)
24 {
25     int i;
26     double max_width = 0.;
27     char string[2];
28     string[1] = '\0';
29     for (i = 0; i < num_characters; i++) {
30         cairo_text_extents_t extents;
31         string[0] = characters[i];
32         cairo_text_extents (cr, string, &extents);
33         if ((extents.width + (extents.x_bearing * 2)) > max_width)
34             max_width = extents.width + (extents.x_bearing * 2);
35     }
36     return max_width;
37 }
38
39 static double
40 get_max_height (cairo_t *cr, const char *characters, int num_characters)
41 {
42     int i;
43     double max_height = 0.;
44     char string[2];
45     string[1] = '\0';
46     for (i = 0; i < num_characters; i++) {
47         cairo_text_extents_t extents;
48         string[0] = characters[i];
49         cairo_text_extents (cr, string, &extents);
50         if ((extents.height) > max_height)
51             max_height = extents.height;
52     }
53     return max_height;
54 }
55
56 typedef struct {
57     char *family;
58     double size;
59     struct {
60         double red;
61         double green;
62         double blue;
63     } color;
64 } args_t;
65
66 static void
67 parse_args (args_t *args)
68 {
69     /* First some defaults */
70     args->family = "Vera";
71     args->size = 20;
72     args->color.red = 0.0;
73     args->color.green = 0.0;
74     args->color.blue = 0.0;
75
76     /* XXX: Next, we should override the defaults based on
77      * command-line arguments. */
78
79     /* XXX: Finally, we'll want to allow CGI-based arguments as
80      * well. */
81 #if USE_CGIG
82     /* QueryString */
83     int fontsize;
84     char format[5];
85     char fillcolor[20];
86     char fontname[20];
87     cgiFormStringNoNewlines("fontname", fontname, 20);
88     cgiFormInteger("fontsize", &fontsize, 50);
89     cgiFormStringNoNewlines("format", format, 5);
90     cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
91
92     int fillcolor_red;
93     int fillcolor_green;
94     int fillcolor_blue;
95     sscanf (fillcolor, " rgb : %d , %d , %d ", &fillcolor_red, &fillcolor_green, &fillcolor_blue);
96     if ( format[0] == 'j' && format[1] == 's' && format[2] == 'o' && format[3] == 'n' )
97         outputJson = TRUE;
98 #endif
99 }
100
101 static FT_Face
102 load_ft_face_for_family (const char *family)
103 {
104     /* XXX: Instead of a hard-coded filename here, we should really be
105      * taking family and using fontconfig to map that to a
106      * filename. */
107     const char font_filename[] = "./Vera.ttf";
108
109     int error;
110     FT_Face     ft_face;
111     FT_Library  library;
112
113     error = FT_Init_FreeType( &library );
114     if ( error ) {
115         fprintf (stderr, "Fatal error initializing freetype.\n");
116         exit (1);
117     }
118
119     error = FT_New_Face( library, font_filename,
120                          0,
121                          &ft_face );
122     if ( error == FT_Err_Unknown_File_Format ) {
123         fprintf (stderr, "Unsupported font format: %s\n", font_filename);
124         exit (1);
125     } else if ( error ) {
126         fprintf (stderr, "Failed to open file: %s\n", font_filename);
127         exit (1);
128     }
129
130     return ft_face;
131 }
132
133 static void
134 get_characters_max_width_height (FT_Face ft_face, double size,
135                                  const char *characters, int num_characters,
136                                  double *max_width, double *max_height)
137 {
138     cairo_surface_t *surface;
139     cairo_t *cr;
140     cairo_font_face_t *cr_face;
141
142     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
143                                           1, 1);
144
145     cr = cairo_create (surface);
146
147     cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
148     cairo_set_font_face (cr, cr_face);
149
150     cairo_set_font_size (cr, size);
151
152     cairo_set_line_width (cr, 1.0);
153
154     *max_width = get_max_width(cr, characters, num_characters);
155     *max_height = get_max_height(cr, characters, num_characters);
156
157     cairo_font_face_destroy (cr_face);
158
159     cairo_destroy (cr);
160
161     cairo_surface_destroy(surface);
162 }
163
164 static void
165 draw_character_table (cairo_t *cr,
166                       FT_Face ft_face, double size,
167                       const char *characters, int num_characters,
168                       double character_width, double character_height)
169 {
170     cairo_font_face_t *cr_face;
171     int i;
172
173     cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
174     cairo_set_font_face (cr, cr_face);
175
176     cairo_set_font_size (cr, size);
177
178     cairo_text_extents_t extents;
179     double x = 0.0;
180     double y = 0.0;
181
182     cairo_translate (cr, 0, -(character_height / 2.50));
183
184     char string[2];
185     string[1] = '\0';
186     for (i = 0; i < num_characters; i++)
187     {
188         if (i % 10 == 0)
189         {
190             x = 0.;
191             y += character_height + 8;
192         }
193         string[0] = characters[i];
194
195         cairo_text_extents (cr, string, &extents);
196
197         x -= extents.x_bearing;
198   
199         cairo_move_to (cr, x, y);
200         cairo_text_path (cr, string);
201         cairo_fill (cr);
202
203         x += extents.x_bearing;
204         x += character_width + 8;
205     }
206
207     cairo_scale (cr, 1, -1);
208     cairo_push_group (cr);
209 }
210
211 int
212 main (void)
213 {
214     char characters[] = {
215         '!', '"', '#', '$', '%', '&','\'', '(',
216         ')', '*', '+', ',', '-', '.', '/', '0',
217         '1', '2', '3', '4', '5', '6', '7', '8',
218         '9', ':', ';', '<', '=', '>', '?', '@',
219         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
220         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
221         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
222         'Y', 'Z', '[','\\', ']', '^', '_', '`',
223         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
224         'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
225         'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
226         'y', 'z', '{', '|', '}', '~'
227     };
228
229     args_t args;
230     FT_Face ft_face;
231     double max_width, max_height;
232     cairo_surface_t *surface;
233     cairo_t *cr;
234
235     parse_args (&args);
236
237     ft_face = load_ft_face_for_family (args.family);
238
239     get_characters_max_width_height (ft_face, args.size,
240                                      characters, ARRAY_SIZE (characters),
241                                      &max_width, &max_height);
242
243     /* Draw */
244     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
245                                           (max_width + 8) * 10,
246                                           (max_height + 8) * 10);
247
248     cr = cairo_create (surface);
249
250     cairo_set_source_rgb (cr,
251                           args.color.red,
252                           args.color.green,
253                           args.color.blue);
254
255     draw_character_table (cr, ft_face, args.size,
256                           characters, ARRAY_SIZE (characters),
257                           max_width, max_height);
258
259     cairo_surface_write_to_png (surface, "spritext-output.png");
260     printf ("Result written to spritext-output.png\n");
261
262     cairo_destroy (cr);
263
264     cairo_surface_destroy (surface);
265
266     return 0;
267 }