1 /* XXX: Copyright/license blurb needed here */
10 #ifndef CAIRO_HAS_PNG_FUNCTIONS
11 #error This program requires cairo with PNG support
15 #include FT_FREETYPE_H
17 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
24 stdio_write (void *closure, const unsigned char *data, unsigned int length);
28 get_max_width (cairo_t *cr, const char *characters)
31 double max_width = 0.;
34 for (i = 0; i < ARRAY_SIZE(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);
45 get_max_height (cairo_t *cr, const char *characters)
48 double max_height = 0.;
51 for (i = 0; i < ARRAY_SIZE(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;
74 parse_args (args_t *args)
76 /* First some defaults */
77 args->family = "Vera";
79 args->color.red = 0.0;
80 args->color.green = 0.0;
81 args->color.blue = 0.0;
83 /* XXX: Next, we should override the defaults based on
84 * command-line arguments. */
86 /* XXX: Finally, we'll want to allow CGI-based arguments as
94 cgiFormStringNoNewlines("fontname", fontname, 20);
95 cgiFormInteger("fontsize", &fontsize, 50);
96 cgiFormStringNoNewlines("format", format, 5);
97 cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
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' )
109 load_ft_face_for_family (const char *family)
111 /* XXX: Instead of a hard-coded filename here, we should really be
112 * taking family and using fontconfig to map that to a
114 const char font_filename[] = "./Vera.ttf";
120 error = FT_Init_FreeType( &library );
122 fprintf (stderr, "Fatal error initializing freetype.\n");
126 error = FT_New_Face( library, font_filename,
129 if ( error == FT_Err_Unknown_File_Format ) {
130 fprintf (stderr, "Unsupported font format: %s\n", font_filename);
132 } else if ( error ) {
133 fprintf (stderr, "Failed to open file: %s\n", font_filename);
141 get_characters_max_width_height (FT_Face ft_face, double size,
142 const char *characters,
143 double *max_width, double *max_height)
145 cairo_surface_t *surface;
147 cairo_font_face_t *cr_face;
149 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
152 cr = cairo_create (surface);
155 cairo_select_font_face (cr, fontname,
156 CAIRO_FONT_SLANT_NORMAL,
157 CAIRO_FONT_WEIGHT_NORMAL);
159 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
160 cairo_set_font_face (cr, cr_face);
162 cairo_set_font_size (cr, size);
164 cairo_set_line_width (cr, 1.0);
166 *max_width = get_max_width(cr, characters);
167 *max_height = get_max_height(cr, characters);
169 cairo_font_face_destroy (cr_face);
173 cairo_surface_destroy(surface);
179 char outputJson = FALSE;
181 char characters[] = {
182 '!', '"', '#', '$', '%', '&','\'', '(',
183 ')', '*', '+', ',', '-', '.', '/', '0',
184 '1', '2', '3', '4', '5', '6', '7', '8',
185 '9', ':', ';', '<', '=', '>', '?', '@',
186 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
187 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
188 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
189 'Y', 'Z', '[','\\', ']', '^', '_', '`',
190 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
191 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
192 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
193 'y', 'z', '{', '|', '}', '~'
199 FT_UInt left_index, right_index;
202 double max_width, max_height;
203 cairo_surface_t *surface;
205 cairo_font_face_t *cr_face;
212 cgiHeaderContentType("application/json");
214 cgiHeaderContentType("image/png");
218 ft_face = load_ft_face_for_family (args.family);
220 use_kerning = FT_HAS_KERNING( ft_face );
222 get_characters_max_width_height (ft_face, args.size,
224 &max_width, &max_height);
227 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
228 (max_width + 8) * 10,
229 (max_height + 8) * 10);
231 cr = cairo_create (surface);
234 cairo_select_font_face (cr, fontname,
235 CAIRO_FONT_SLANT_NORMAL,
236 CAIRO_FONT_WEIGHT_NORMAL);
238 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
239 cairo_set_font_face (cr, cr_face);
241 cairo_set_font_size (cr, args.size);
243 cairo_text_extents_t extents;
250 cairo_translate (cr, 0, -(max_height / 2.50));
252 // cairo_translate (cr, 0, -5.0);
256 for (i = 0; i < ARRAY_SIZE(characters); i++)
263 string[0] = characters[i];
265 cairo_text_extents (cr, string, &extents);
271 printf ("u%i:{", characters[i]);
272 printf ("x:%.1f,y:%.1f,w:%.1f,h:%.1f",
274 y - (max_height * 1.15),
275 extents.width + extents.x_bearing,
278 for ( j = 0; j < ARRAY_SIZE(characters); j++ )
280 right_index = FT_Get_Char_Index( ft_face, characters[i] );
281 left_index = FT_Get_Char_Index( ft_face, characters[j] );
283 FT_Get_Kerning( ft_face, left_index, right_index,
284 FT_KERNING_UNSCALED, &kerning );
287 printf(",k%d:%ld", characters[j], kerning.x);
293 x -= extents.x_bearing;
295 cairo_move_to (cr, x, y);
296 cairo_set_source_rgb (cr, 0., 0., 0.); // black
297 cairo_set_source_rgb (cr,
301 cairo_text_path (cr, string);
304 x += extents.x_bearing;
312 for (c = 'A'; c <= 'I'; c++)
315 int spacing = ((c - 'A') * letterspacing) + 1;
320 cairo_move_to (cr, spacing, 0);
321 cairo_text_path (cr, string);
322 // cairo_set_source_rgb (cr, 0.1, 0.2, 0.2); // gray
323 cairo_set_source_rgb (cr, 0.8, 0.8, 0.85); // silver
324 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
326 cairo_push_group (cr);
327 cairo_set_line_width (cr, thick_width);
328 cairo_stroke_preserve (cr);
329 cairo_set_line_width (cr, thin_width);
330 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
331 cairo_stroke_preserve (cr);
333 cairo_pop_group_to_source (cr);
339 cairo_move_to (cr, spacing, 0);
340 cairo_text_path (cr, string);
341 cairo_set_source_rgb (cr, 0.4, 0.45, 0.7); // blue
347 cairo_scale (cr, 1, -1);
348 cairo_push_group (cr);
351 cairo_pattern_t *gradient;
353 for (i = 0; i < 9; i++)
356 strncpy(c, STRING + i, 1);
358 int spacing = (i * letterspacing) + 1;
359 cairo_move_to (cr, spacing, -3);
360 cairo_show_text (cr, c);
363 cairo_pop_group_to_source (cr);
365 gradient = cairo_pattern_create_linear (0, 0, 0, -fontsize);
366 cairo_pattern_add_color_stop_rgba (gradient, 0.0, 1, 1, 1, 0.5);
367 cairo_pattern_add_color_stop_rgba (gradient, 0.7, 1, 1, 1, 0.0);
369 cairo_mask (cr, gradient);
371 cairo_pattern_destroy (gradient);
379 cairo_surface_write_to_png_stream (surface, stdio_write, cgiOut);
383 cairo_surface_write_to_png (surface, "spritext-output.png");
384 printf ("Result written to spritext-output.png\n");
388 cairo_surface_destroy (surface);
394 static cairo_status_t
395 stdio_write (void *closure, const unsigned char *data, unsigned int length)
397 FILE *file = closure;
398 if (fwrite (data, 1, length, file) == length)
399 return CAIRO_STATUS_SUCCESS;
401 return CAIRO_STATUS_WRITE_ERROR;