]> git.cworth.org Git - spritext/blob - spritext.c
Remove unused function
[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 #include <cgic.h>
10
11 #ifndef CAIRO_HAS_PNG_FUNCTIONS
12 #error This program requires cairo with PNG support
13 #endif
14
15 #include <ft2build.h>
16 #include FT_FREETYPE_H
17
18 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
19
20 #define TRUE (1==1)
21 #define FALSE (!TRUE)
22
23 static cairo_status_t
24 stdio_write (void *closure, const unsigned char *data, unsigned int length);
25
26 double
27 get_max_width (cairo_t *cr, char *characters);
28
29 double
30 get_max_height (cairo_t *cr, char *characters);
31
32 int
33 cgiMain ()
34 {
35     char outputJson = FALSE;
36
37     char characters[] = {
38         '!', '"', '#', '$', '%', '&','\'', '(',
39         ')', '*', '+', ',', '-', '.', '/', '0',
40         '1', '2', '3', '4', '5', '6', '7', '8',
41         '9', ':', ';', '<', '=', '>', '?', '@',
42         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
43         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
44         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
45         'Y', 'Z', '[','\\', ']', '^', '_', '`',
46         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
47         'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
48         'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
49         'y', 'z', '{', '|', '}', '~'
50     };
51
52     int i;
53
54     /* QueryString */
55     char fontname[20];
56     int fontsize;
57     char format[5];
58     char fillcolor[20];
59     cgiFormStringNoNewlines("fontname", fontname, 20);
60     cgiFormInteger("fontsize", &fontsize, 50);
61     cgiFormStringNoNewlines("format", format, 5);
62     cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
63
64     int fillcolor_red;
65     int fillcolor_green;
66     int fillcolor_blue;
67     sscanf (fillcolor, " rgb : %d , %d , %d ", &fillcolor_red, &fillcolor_green, &fillcolor_blue);
68
69     if ( format[0] == 'j' && format[1] == 's' && format[2] == 'o' && format[3] == 'n' )
70         outputJson = TRUE;
71
72     if (outputJson)
73     {
74         cgiHeaderContentType("application/json");
75     } else {
76         cgiHeaderContentType("image/png");
77     }
78
79     int error;  
80     FT_Library  library;
81     FT_Face     ft_face;
82     FT_Bool     use_kerning;
83     FT_UInt     left_index, right_index;
84     FT_Vector   kerning;
85
86     error = FT_Init_FreeType( &library );
87     if ( error )
88         printf("error");
89
90     error = FT_New_Face( library,"/srv/rdworth.org/cgi-bin/Verdana.ttf",
91                          0,
92                          &ft_face );
93     if ( error == FT_Err_Unknown_File_Format )
94         printf("File opened. Font format unsupported.");
95     else if ( error )
96         printf("Could not be open or read or broken.");
97
98     use_kerning = FT_HAS_KERNING( ft_face );
99
100     /* Compute max-width and max-height */
101     cairo_surface_t *surface;
102     cairo_t *cr;
103     cairo_font_face_t *cr_face;
104
105     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
106                                           1, 1);
107
108     cr = cairo_create (surface);
109
110 /*
111   cairo_select_font_face (cr, fontname,
112   CAIRO_FONT_SLANT_NORMAL,
113   CAIRO_FONT_WEIGHT_NORMAL);
114 */
115     cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
116     cairo_set_font_face (cr, cr_face);
117
118     cairo_set_font_size (cr, fontsize);
119
120     cairo_set_line_width (cr, 1.0);
121
122     double max_width = get_max_width(cr, characters);
123     double max_height = get_max_height(cr, characters);
124
125     cairo_font_face_destroy (cr_face);
126
127     cairo_destroy (cr);
128
129     cairo_surface_destroy(surface);
130
131     /* Draw */
132     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
133                                           (max_width + 8) * 10,
134                                           (max_height + 8) * 10);
135
136     cr = cairo_create (surface);
137
138 /*
139   cairo_select_font_face (cr, fontname,
140   CAIRO_FONT_SLANT_NORMAL,
141   CAIRO_FONT_WEIGHT_NORMAL);
142 */
143     cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
144     cairo_set_font_face (cr, cr_face);
145
146     cairo_set_font_size (cr, fontsize);
147
148     cairo_text_extents_t extents;
149     double x = 0.0;
150     double y = 0.0;
151
152     if (outputJson)
153         printf("{");
154
155     cairo_translate (cr, 0, -(max_height / 2.50));
156
157 //    cairo_translate (cr, 0, -5.0);
158
159     char string[2];
160     string[1] = '\0';
161     for (i = 0; i < ARRAY_SIZE(characters); i++)
162     {
163         if (i % 10 == 0)
164         {
165             x = 0.;
166             y += max_height + 8;
167         }
168         string[0] = characters[i];
169
170         cairo_text_extents (cr, string, &extents);
171
172         if (outputJson)
173         {
174             if (i > 0)
175                 printf (",\n");
176             printf ("u%i:{", characters[i]);
177             printf ("x:%.1f,y:%.1f,w:%.1f,h:%.1f",
178                     x,
179                     y - (max_height * 1.15),
180                     extents.width + extents.x_bearing,
181                     extents.height);
182             int j;
183             for ( j = 0; j < ARRAY_SIZE(characters); j++ )
184             {
185                 right_index = FT_Get_Char_Index( ft_face, characters[i] );
186                 left_index = FT_Get_Char_Index( ft_face, characters[j] );
187     
188                 FT_Get_Kerning( ft_face, left_index, right_index,
189                                 FT_KERNING_UNSCALED, &kerning );
190     
191                 if ( kerning.x )
192                     printf(",k%d:%d", characters[j], kerning.x);
193             }
194
195             printf ("}");
196         }
197
198         x -= extents.x_bearing;
199   
200         cairo_move_to (cr, x, y);
201         cairo_set_source_rgb (cr, 0., 0., 0.); // black
202         if ( 0 <= fillcolor_red && fillcolor_red <= 255.0
203              && 0 <= fillcolor_green && fillcolor_green <= 255.0
204              && 0 <= fillcolor_blue && fillcolor_blue <= 255.0 )
205             cairo_set_source_rgb (cr, fillcolor_red / 255.0, fillcolor_green / 255.0, fillcolor_blue / 255.0);
206         cairo_text_path (cr, string);
207         cairo_fill (cr);
208
209         x += extents.x_bearing;
210         x += max_width + 8;
211     }
212
213 /*
214   int thick_width = 4;
215   int thin_width = 0;
216
217   for (c = 'A'; c <= 'I'; c++)
218   {
219   string[0] = c;
220   int spacing = ((c - 'A') * letterspacing) + 1;
221
222
223   cairo_save(cr);
224
225   cairo_move_to (cr, spacing, 0);
226   cairo_text_path (cr, string);
227 //      cairo_set_source_rgb (cr, 0.1, 0.2, 0.2); // gray
228 cairo_set_source_rgb (cr, 0.8, 0.8, 0.85); // silver
229 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
230
231 cairo_push_group (cr);
232 cairo_set_line_width (cr, thick_width);
233 cairo_stroke_preserve (cr);
234 cairo_set_line_width (cr, thin_width);
235 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
236 cairo_stroke_preserve (cr);
237 cairo_fill (cr);
238 cairo_pop_group_to_source (cr);
239
240 cairo_paint (cr);
241
242 cairo_restore(cr);
243
244 cairo_move_to (cr, spacing, 0);
245 cairo_text_path (cr, string);
246 cairo_set_source_rgb (cr, 0.4, 0.45, 0.7); // blue
247
248 cairo_fill (cr);
249 }
250 */
251
252     cairo_scale (cr, 1, -1);
253     cairo_push_group (cr);
254 /*
255
256 cairo_pattern_t *gradient;
257
258 for (i = 0; i < 9; i++)
259 {
260 char c[2];
261 strncpy(c, STRING + i, 1);
262 c[1] = '\0';
263 int spacing = (i * letterspacing) + 1;
264 cairo_move_to (cr, spacing, -3);
265 cairo_show_text (cr, c);
266
267 }
268 cairo_pop_group_to_source (cr);
269
270 gradient = cairo_pattern_create_linear (0, 0, 0, -fontsize);
271 cairo_pattern_add_color_stop_rgba (gradient, 0.0, 1, 1, 1, 0.5);
272 cairo_pattern_add_color_stop_rgba (gradient, 0.7, 1, 1, 1, 0.0);
273
274 cairo_mask (cr, gradient);
275
276 cairo_pattern_destroy (gradient);
277 */
278     if (outputJson)
279     {
280         printf("}");
281     } else {
282         cairo_surface_write_to_png_stream (surface, stdio_write, cgiOut);
283     }
284
285     cairo_destroy (cr);
286
287     cairo_surface_destroy (surface);
288
289     return 0;
290 }
291
292 static cairo_status_t
293 stdio_write (void *closure, const unsigned char *data, unsigned int length)
294 {
295     FILE *file = closure;
296     if (fwrite (data, 1, length, file) == length)
297         return CAIRO_STATUS_SUCCESS;
298     else
299         return CAIRO_STATUS_WRITE_ERROR;
300 }
301
302 double
303 get_max_width(cairo_t *cr, char *characters)
304 {
305     int i;
306     double max_width = 0.;
307     char string[2];
308     string[1] = '\0';
309     for (i = 0; i < ARRAY_SIZE(characters); i++) {
310         cairo_text_extents_t extents;
311         string[0] = characters[i];
312         cairo_text_extents (cr, string, &extents);
313         if ((extents.width + (extents.x_bearing * 2)) > max_width)
314             max_width = extents.width + (extents.x_bearing * 2);
315     }
316     return max_width;
317 }
318
319 double
320 get_max_height(cairo_t *cr, char *characters)
321 {
322     int i;
323     double max_height = 0.;
324     char string[2];
325     string[1] = '\0';
326     for (i = 0; i < ARRAY_SIZE(characters); i++) {
327         cairo_text_extents_t extents;
328         string[0] = characters[i];
329         cairo_text_extents (cr, string, &extents);
330 //    if ((extents.height - extents.y_bearing) > max_height)
331 //      max_height = extents.height - extents.y_bearing;
332         if ((extents.height) > max_height)
333             max_height = extents.height;
334     }
335     return max_height;
336 }
337