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, char *characters);
31 get_max_height (cairo_t *cr, char *characters);
44 parse_args (args_t *args)
46 /* First some defaults */
47 args->family = "Vera";
49 args->color.red = 0.0;
50 args->color.green = 0.0;
51 args->color.blue = 0.0;
53 /* XXX: Next, we should override the defaults based on
54 * command-line arguments. */
56 /* XXX: Finally, we'll want to allow CGI-based arguments as
64 cgiFormStringNoNewlines("fontname", fontname, 20);
65 cgiFormInteger("fontsize", &fontsize, 50);
66 cgiFormStringNoNewlines("format", format, 5);
67 cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
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' )
79 load_ft_face_for_family (const char *family)
81 /* XXX: Instead of a hard-coded filename here, we should really be
82 * taking family and using fontconfig to map that to a
84 const char font_filename[] = "./Vera.ttf";
90 error = FT_Init_FreeType( &library );
92 fprintf (stderr, "Fatal error initializing freetype.\n");
96 error = FT_New_Face( library, font_filename,
99 if ( error == FT_Err_Unknown_File_Format ) {
100 fprintf (stderr, "Unsupported font format: %s\n", font_filename);
102 } else if ( error ) {
103 fprintf (stderr, "Failed to open file: %s\n", font_filename);
113 char outputJson = FALSE;
115 char characters[] = {
116 '!', '"', '#', '$', '%', '&','\'', '(',
117 ')', '*', '+', ',', '-', '.', '/', '0',
118 '1', '2', '3', '4', '5', '6', '7', '8',
119 '9', ':', ';', '<', '=', '>', '?', '@',
120 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
121 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
122 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
123 'Y', 'Z', '[','\\', ']', '^', '_', '`',
124 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
125 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
126 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
127 'y', 'z', '{', '|', '}', '~'
133 FT_UInt left_index, right_index;
142 cgiHeaderContentType("application/json");
144 cgiHeaderContentType("image/png");
148 ft_face = load_ft_face_for_family (args.family);
150 use_kerning = FT_HAS_KERNING( ft_face );
152 /* Compute max-width and max-height */
153 cairo_surface_t *surface;
155 cairo_font_face_t *cr_face;
157 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
160 cr = cairo_create (surface);
163 cairo_select_font_face (cr, fontname,
164 CAIRO_FONT_SLANT_NORMAL,
165 CAIRO_FONT_WEIGHT_NORMAL);
167 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
168 cairo_set_font_face (cr, cr_face);
170 cairo_set_font_size (cr, args.size);
172 cairo_set_line_width (cr, 1.0);
174 double max_width = get_max_width(cr, characters);
175 double max_height = get_max_height(cr, characters);
177 cairo_font_face_destroy (cr_face);
181 cairo_surface_destroy(surface);
184 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
185 (max_width + 8) * 10,
186 (max_height + 8) * 10);
188 cr = cairo_create (surface);
191 cairo_select_font_face (cr, fontname,
192 CAIRO_FONT_SLANT_NORMAL,
193 CAIRO_FONT_WEIGHT_NORMAL);
195 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
196 cairo_set_font_face (cr, cr_face);
198 cairo_set_font_size (cr, args.size);
200 cairo_text_extents_t extents;
207 cairo_translate (cr, 0, -(max_height / 2.50));
209 // cairo_translate (cr, 0, -5.0);
213 for (i = 0; i < ARRAY_SIZE(characters); i++)
220 string[0] = characters[i];
222 cairo_text_extents (cr, string, &extents);
228 printf ("u%i:{", characters[i]);
229 printf ("x:%.1f,y:%.1f,w:%.1f,h:%.1f",
231 y - (max_height * 1.15),
232 extents.width + extents.x_bearing,
235 for ( j = 0; j < ARRAY_SIZE(characters); j++ )
237 right_index = FT_Get_Char_Index( ft_face, characters[i] );
238 left_index = FT_Get_Char_Index( ft_face, characters[j] );
240 FT_Get_Kerning( ft_face, left_index, right_index,
241 FT_KERNING_UNSCALED, &kerning );
244 printf(",k%d:%ld", characters[j], kerning.x);
250 x -= extents.x_bearing;
252 cairo_move_to (cr, x, y);
253 cairo_set_source_rgb (cr, 0., 0., 0.); // black
254 cairo_set_source_rgb (cr,
258 cairo_text_path (cr, string);
261 x += extents.x_bearing;
269 for (c = 'A'; c <= 'I'; c++)
272 int spacing = ((c - 'A') * letterspacing) + 1;
277 cairo_move_to (cr, spacing, 0);
278 cairo_text_path (cr, string);
279 // cairo_set_source_rgb (cr, 0.1, 0.2, 0.2); // gray
280 cairo_set_source_rgb (cr, 0.8, 0.8, 0.85); // silver
281 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
283 cairo_push_group (cr);
284 cairo_set_line_width (cr, thick_width);
285 cairo_stroke_preserve (cr);
286 cairo_set_line_width (cr, thin_width);
287 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
288 cairo_stroke_preserve (cr);
290 cairo_pop_group_to_source (cr);
296 cairo_move_to (cr, spacing, 0);
297 cairo_text_path (cr, string);
298 cairo_set_source_rgb (cr, 0.4, 0.45, 0.7); // blue
304 cairo_scale (cr, 1, -1);
305 cairo_push_group (cr);
308 cairo_pattern_t *gradient;
310 for (i = 0; i < 9; i++)
313 strncpy(c, STRING + i, 1);
315 int spacing = (i * letterspacing) + 1;
316 cairo_move_to (cr, spacing, -3);
317 cairo_show_text (cr, c);
320 cairo_pop_group_to_source (cr);
322 gradient = cairo_pattern_create_linear (0, 0, 0, -fontsize);
323 cairo_pattern_add_color_stop_rgba (gradient, 0.0, 1, 1, 1, 0.5);
324 cairo_pattern_add_color_stop_rgba (gradient, 0.7, 1, 1, 1, 0.0);
326 cairo_mask (cr, gradient);
328 cairo_pattern_destroy (gradient);
336 cairo_surface_write_to_png_stream (surface, stdio_write, cgiOut);
340 cairo_surface_write_to_png (surface, "spritext-output.png");
341 printf ("Result written to spritext-output.png\n");
345 cairo_surface_destroy (surface);
351 static cairo_status_t
352 stdio_write (void *closure, const unsigned char *data, unsigned int length)
354 FILE *file = closure;
355 if (fwrite (data, 1, length, file) == length)
356 return CAIRO_STATUS_SUCCESS;
358 return CAIRO_STATUS_WRITE_ERROR;
363 get_max_width(cairo_t *cr, char *characters)
366 double max_width = 0.;
369 for (i = 0; i < ARRAY_SIZE(characters); i++) {
370 cairo_text_extents_t extents;
371 string[0] = characters[i];
372 cairo_text_extents (cr, string, &extents);
373 if ((extents.width + (extents.x_bearing * 2)) > max_width)
374 max_width = extents.width + (extents.x_bearing * 2);
380 get_max_height(cairo_t *cr, char *characters)
383 double max_height = 0.;
386 for (i = 0; i < ARRAY_SIZE(characters); i++) {
387 cairo_text_extents_t extents;
388 string[0] = characters[i];
389 cairo_text_extents (cr, string, &extents);
390 // if ((extents.height - extents.y_bearing) > max_height)
391 // max_height = extents.height - extents.y_bearing;
392 if ((extents.height) > max_height)
393 max_height = extents.height;