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' )
81 /* XXX: Instead of a hard-coded filename here, we should really be
82 * taking args.family and using fontconfig to map that to a
84 const char font_filename[] = "./Vera.ttf";
85 char outputJson = FALSE;
88 '!', '"', '#', '$', '%', '&','\'', '(',
89 ')', '*', '+', ',', '-', '.', '/', '0',
90 '1', '2', '3', '4', '5', '6', '7', '8',
91 '9', ':', ';', '<', '=', '>', '?', '@',
92 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
93 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
94 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
95 'Y', 'Z', '[','\\', ']', '^', '_', '`',
96 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
97 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
98 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
99 'y', 'z', '{', '|', '}', '~'
110 cgiHeaderContentType("application/json");
112 cgiHeaderContentType("image/png");
120 FT_UInt left_index, right_index;
123 error = FT_Init_FreeType( &library );
125 fprintf (stderr, "Fatal error initializing freetype.\n");
129 error = FT_New_Face( library, font_filename,
132 if ( error == FT_Err_Unknown_File_Format ) {
133 fprintf (stderr, "Unsupported font format: %s\n", font_filename);
135 } else if ( error ) {
136 fprintf (stderr, "Failed to open file: %s\n", font_filename);
140 use_kerning = FT_HAS_KERNING( ft_face );
142 /* Compute max-width and max-height */
143 cairo_surface_t *surface;
145 cairo_font_face_t *cr_face;
147 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
150 cr = cairo_create (surface);
153 cairo_select_font_face (cr, fontname,
154 CAIRO_FONT_SLANT_NORMAL,
155 CAIRO_FONT_WEIGHT_NORMAL);
157 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
158 cairo_set_font_face (cr, cr_face);
160 cairo_set_font_size (cr, args.size);
162 cairo_set_line_width (cr, 1.0);
164 double max_width = get_max_width(cr, characters);
165 double max_height = get_max_height(cr, characters);
167 cairo_font_face_destroy (cr_face);
171 cairo_surface_destroy(surface);
174 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
175 (max_width + 8) * 10,
176 (max_height + 8) * 10);
178 cr = cairo_create (surface);
181 cairo_select_font_face (cr, fontname,
182 CAIRO_FONT_SLANT_NORMAL,
183 CAIRO_FONT_WEIGHT_NORMAL);
185 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
186 cairo_set_font_face (cr, cr_face);
188 cairo_set_font_size (cr, args.size);
190 cairo_text_extents_t extents;
197 cairo_translate (cr, 0, -(max_height / 2.50));
199 // cairo_translate (cr, 0, -5.0);
203 for (i = 0; i < ARRAY_SIZE(characters); i++)
210 string[0] = characters[i];
212 cairo_text_extents (cr, string, &extents);
218 printf ("u%i:{", characters[i]);
219 printf ("x:%.1f,y:%.1f,w:%.1f,h:%.1f",
221 y - (max_height * 1.15),
222 extents.width + extents.x_bearing,
225 for ( j = 0; j < ARRAY_SIZE(characters); j++ )
227 right_index = FT_Get_Char_Index( ft_face, characters[i] );
228 left_index = FT_Get_Char_Index( ft_face, characters[j] );
230 FT_Get_Kerning( ft_face, left_index, right_index,
231 FT_KERNING_UNSCALED, &kerning );
234 printf(",k%d:%ld", characters[j], kerning.x);
240 x -= extents.x_bearing;
242 cairo_move_to (cr, x, y);
243 cairo_set_source_rgb (cr, 0., 0., 0.); // black
244 cairo_set_source_rgb (cr,
248 cairo_text_path (cr, string);
251 x += extents.x_bearing;
259 for (c = 'A'; c <= 'I'; c++)
262 int spacing = ((c - 'A') * letterspacing) + 1;
267 cairo_move_to (cr, spacing, 0);
268 cairo_text_path (cr, string);
269 // cairo_set_source_rgb (cr, 0.1, 0.2, 0.2); // gray
270 cairo_set_source_rgb (cr, 0.8, 0.8, 0.85); // silver
271 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
273 cairo_push_group (cr);
274 cairo_set_line_width (cr, thick_width);
275 cairo_stroke_preserve (cr);
276 cairo_set_line_width (cr, thin_width);
277 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
278 cairo_stroke_preserve (cr);
280 cairo_pop_group_to_source (cr);
286 cairo_move_to (cr, spacing, 0);
287 cairo_text_path (cr, string);
288 cairo_set_source_rgb (cr, 0.4, 0.45, 0.7); // blue
294 cairo_scale (cr, 1, -1);
295 cairo_push_group (cr);
298 cairo_pattern_t *gradient;
300 for (i = 0; i < 9; i++)
303 strncpy(c, STRING + i, 1);
305 int spacing = (i * letterspacing) + 1;
306 cairo_move_to (cr, spacing, -3);
307 cairo_show_text (cr, c);
310 cairo_pop_group_to_source (cr);
312 gradient = cairo_pattern_create_linear (0, 0, 0, -fontsize);
313 cairo_pattern_add_color_stop_rgba (gradient, 0.0, 1, 1, 1, 0.5);
314 cairo_pattern_add_color_stop_rgba (gradient, 0.7, 1, 1, 1, 0.0);
316 cairo_mask (cr, gradient);
318 cairo_pattern_destroy (gradient);
326 cairo_surface_write_to_png_stream (surface, stdio_write, cgiOut);
330 cairo_surface_write_to_png (surface, "spritext-output.png");
331 printf ("Result written to spritext-output.png\n");
335 cairo_surface_destroy (surface);
341 static cairo_status_t
342 stdio_write (void *closure, const unsigned char *data, unsigned int length)
344 FILE *file = closure;
345 if (fwrite (data, 1, length, file) == length)
346 return CAIRO_STATUS_SUCCESS;
348 return CAIRO_STATUS_WRITE_ERROR;
353 get_max_width(cairo_t *cr, char *characters)
356 double max_width = 0.;
359 for (i = 0; i < ARRAY_SIZE(characters); i++) {
360 cairo_text_extents_t extents;
361 string[0] = characters[i];
362 cairo_text_extents (cr, string, &extents);
363 if ((extents.width + (extents.x_bearing * 2)) > max_width)
364 max_width = extents.width + (extents.x_bearing * 2);
370 get_max_height(cairo_t *cr, char *characters)
373 double max_height = 0.;
376 for (i = 0; i < ARRAY_SIZE(characters); i++) {
377 cairo_text_extents_t extents;
378 string[0] = characters[i];
379 cairo_text_extents (cr, string, &extents);
380 // if ((extents.height - extents.y_bearing) > max_height)
381 // max_height = extents.height - extents.y_bearing;
382 if ((extents.height) > max_height)
383 max_height = extents.height;