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);
36 const char font_filename[] = "./Vera.ttf";
37 char outputJson = FALSE;
40 '!', '"', '#', '$', '%', '&','\'', '(',
41 ')', '*', '+', ',', '-', '.', '/', '0',
42 '1', '2', '3', '4', '5', '6', '7', '8',
43 '9', ':', ';', '<', '=', '>', '?', '@',
44 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
45 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
46 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
47 'Y', 'Z', '[','\\', ']', '^', '_', '`',
48 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
49 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
50 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
51 'y', 'z', '{', '|', '}', '~'
63 cgiFormStringNoNewlines("fontname", fontname, 20);
64 cgiFormInteger("fontsize", &fontsize, 50);
65 cgiFormStringNoNewlines("format", format, 5);
66 cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
72 sscanf (fillcolor, " rgb : %d , %d , %d ", &fillcolor_red, &fillcolor_green, &fillcolor_blue);
74 if ( format[0] == 'j' && format[1] == 's' && format[2] == 'o' && format[3] == 'n' )
80 cgiHeaderContentType("application/json");
82 cgiHeaderContentType("image/png");
90 FT_UInt left_index, right_index;
93 error = FT_Init_FreeType( &library );
95 fprintf (stderr, "Fatal error initializing freetype.\n");
99 error = FT_New_Face( library, font_filename,
102 if ( error == FT_Err_Unknown_File_Format ) {
103 fprintf (stderr, "Unsupported font format: %s\n", font_filename);
105 } else if ( error ) {
106 fprintf (stderr, "Failed to open file: %s\n", font_filename);
110 use_kerning = FT_HAS_KERNING( ft_face );
112 /* Compute max-width and max-height */
113 cairo_surface_t *surface;
115 cairo_font_face_t *cr_face;
117 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
120 cr = cairo_create (surface);
123 cairo_select_font_face (cr, fontname,
124 CAIRO_FONT_SLANT_NORMAL,
125 CAIRO_FONT_WEIGHT_NORMAL);
127 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
128 cairo_set_font_face (cr, cr_face);
130 cairo_set_font_size (cr, fontsize);
132 cairo_set_line_width (cr, 1.0);
134 double max_width = get_max_width(cr, characters);
135 double max_height = get_max_height(cr, characters);
137 cairo_font_face_destroy (cr_face);
141 cairo_surface_destroy(surface);
144 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
145 (max_width + 8) * 10,
146 (max_height + 8) * 10);
148 cr = cairo_create (surface);
151 cairo_select_font_face (cr, fontname,
152 CAIRO_FONT_SLANT_NORMAL,
153 CAIRO_FONT_WEIGHT_NORMAL);
155 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
156 cairo_set_font_face (cr, cr_face);
158 cairo_set_font_size (cr, fontsize);
160 cairo_text_extents_t extents;
167 cairo_translate (cr, 0, -(max_height / 2.50));
169 // cairo_translate (cr, 0, -5.0);
173 for (i = 0; i < ARRAY_SIZE(characters); i++)
180 string[0] = characters[i];
182 cairo_text_extents (cr, string, &extents);
188 printf ("u%i:{", characters[i]);
189 printf ("x:%.1f,y:%.1f,w:%.1f,h:%.1f",
191 y - (max_height * 1.15),
192 extents.width + extents.x_bearing,
195 for ( j = 0; j < ARRAY_SIZE(characters); j++ )
197 right_index = FT_Get_Char_Index( ft_face, characters[i] );
198 left_index = FT_Get_Char_Index( ft_face, characters[j] );
200 FT_Get_Kerning( ft_face, left_index, right_index,
201 FT_KERNING_UNSCALED, &kerning );
204 printf(",k%d:%ld", characters[j], kerning.x);
210 x -= extents.x_bearing;
212 cairo_move_to (cr, x, y);
213 cairo_set_source_rgb (cr, 0., 0., 0.); // black
214 if ( 0 <= fillcolor_red && fillcolor_red <= 255.0
215 && 0 <= fillcolor_green && fillcolor_green <= 255.0
216 && 0 <= fillcolor_blue && fillcolor_blue <= 255.0 )
217 cairo_set_source_rgb (cr, fillcolor_red / 255.0, fillcolor_green / 255.0, fillcolor_blue / 255.0);
218 cairo_text_path (cr, string);
221 x += extents.x_bearing;
229 for (c = 'A'; c <= 'I'; c++)
232 int spacing = ((c - 'A') * letterspacing) + 1;
237 cairo_move_to (cr, spacing, 0);
238 cairo_text_path (cr, string);
239 // cairo_set_source_rgb (cr, 0.1, 0.2, 0.2); // gray
240 cairo_set_source_rgb (cr, 0.8, 0.8, 0.85); // silver
241 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
243 cairo_push_group (cr);
244 cairo_set_line_width (cr, thick_width);
245 cairo_stroke_preserve (cr);
246 cairo_set_line_width (cr, thin_width);
247 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
248 cairo_stroke_preserve (cr);
250 cairo_pop_group_to_source (cr);
256 cairo_move_to (cr, spacing, 0);
257 cairo_text_path (cr, string);
258 cairo_set_source_rgb (cr, 0.4, 0.45, 0.7); // blue
264 cairo_scale (cr, 1, -1);
265 cairo_push_group (cr);
268 cairo_pattern_t *gradient;
270 for (i = 0; i < 9; i++)
273 strncpy(c, STRING + i, 1);
275 int spacing = (i * letterspacing) + 1;
276 cairo_move_to (cr, spacing, -3);
277 cairo_show_text (cr, c);
280 cairo_pop_group_to_source (cr);
282 gradient = cairo_pattern_create_linear (0, 0, 0, -fontsize);
283 cairo_pattern_add_color_stop_rgba (gradient, 0.0, 1, 1, 1, 0.5);
284 cairo_pattern_add_color_stop_rgba (gradient, 0.7, 1, 1, 1, 0.0);
286 cairo_mask (cr, gradient);
288 cairo_pattern_destroy (gradient);
296 cairo_surface_write_to_png_stream (surface, stdio_write, cgiOut);
302 cairo_surface_destroy (surface);
308 static cairo_status_t
309 stdio_write (void *closure, const unsigned char *data, unsigned int length)
311 FILE *file = closure;
312 if (fwrite (data, 1, length, file) == length)
313 return CAIRO_STATUS_SUCCESS;
315 return CAIRO_STATUS_WRITE_ERROR;
320 get_max_width(cairo_t *cr, char *characters)
323 double max_width = 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.width + (extents.x_bearing * 2)) > max_width)
331 max_width = extents.width + (extents.x_bearing * 2);
337 get_max_height(cairo_t *cr, char *characters)
340 double max_height = 0.;
343 for (i = 0; i < ARRAY_SIZE(characters); i++) {
344 cairo_text_extents_t extents;
345 string[0] = characters[i];
346 cairo_text_extents (cr, string, &extents);
347 // if ((extents.height - extents.y_bearing) > max_height)
348 // max_height = extents.height - extents.y_bearing;
349 if ((extents.height) > max_height)
350 max_height = extents.height;