8 #ifndef CAIRO_HAS_PNG_FUNCTIONS
9 #error This program requires cairo with PNG support
12 #include FT_FREETYPE_H
14 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
20 stdio_write (void *closure, const unsigned char *data, unsigned int length);
23 get_max_width (cairo_t *cr, char *characters);
26 get_max_height (cairo_t *cr, char *characters);
29 out_stroke (cairo_t *cr)
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', '{', '|', '}', '~'
61 cgiFormStringNoNewlines("fontname", fontname, 20);
62 cgiFormInteger("fontsize", &fontsize, 50);
63 cgiFormStringNoNewlines("format", format, 5);
64 cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
69 sscanf (fillcolor, " rgb : %d , %d , %d ", &fillcolor_red, &fillcolor_green, &fillcolor_blue);
71 if ( format[0] == 'j' && format[1] == 's' && format[2] == 'o' && format[3] == 'n' )
76 cgiHeaderContentType("application/json");
78 cgiHeaderContentType("image/png");
85 FT_UInt left_index, right_index;
88 error = FT_Init_FreeType( &library );
92 error = FT_New_Face( library,"/srv/rdworth.org/cgi-bin/Verdana.ttf",
95 if ( error == FT_Err_Unknown_File_Format )
96 printf("File opened. Font format unsupported.");
98 printf("Could not be open or read or broken.");
100 use_kerning = FT_HAS_KERNING( ft_face );
102 /* Compute max-width and max-height */
103 cairo_surface_t *surface;
105 cairo_font_face_t *cr_face;
107 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
110 cr = cairo_create (surface);
113 cairo_select_font_face (cr, fontname,
114 CAIRO_FONT_SLANT_NORMAL,
115 CAIRO_FONT_WEIGHT_NORMAL);
117 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
118 cairo_set_font_face (cr, cr_face);
120 cairo_set_font_size (cr, fontsize);
122 cairo_set_line_width (cr, 1.0);
124 double max_width = get_max_width(cr, characters);
125 double max_height = get_max_height(cr, characters);
127 cairo_font_face_destroy (cr_face);
131 cairo_surface_destroy(surface);
134 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
135 (max_width + 8) * 10,
136 (max_height + 8) * 10);
138 cr = cairo_create (surface);
141 cairo_select_font_face (cr, fontname,
142 CAIRO_FONT_SLANT_NORMAL,
143 CAIRO_FONT_WEIGHT_NORMAL);
145 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
146 cairo_set_font_face (cr, cr_face);
148 cairo_set_font_size (cr, fontsize);
150 cairo_text_extents_t extents;
157 cairo_translate (cr, 0, -(max_height / 2.50));
159 // cairo_translate (cr, 0, -5.0);
163 for (i = 0; i < ARRAY_SIZE(characters); i++)
170 string[0] = characters[i];
172 cairo_text_extents (cr, string, &extents);
178 printf ("u%i:{", characters[i]);
179 printf ("x:%.1f,y:%.1f,w:%.1f,h:%.1f",
181 y - (max_height * 1.15),
182 extents.width + extents.x_bearing,
185 for ( j = 0; j < ARRAY_SIZE(characters); j++ )
187 right_index = FT_Get_Char_Index( ft_face, characters[i] );
188 left_index = FT_Get_Char_Index( ft_face, characters[j] );
190 FT_Get_Kerning( ft_face, left_index, right_index,
191 FT_KERNING_UNSCALED, &kerning );
194 printf(",k%d:%d", characters[j], kerning.x);
200 x -= extents.x_bearing;
202 cairo_move_to (cr, x, y);
203 cairo_set_source_rgb (cr, 0., 0., 0.); // black
204 if ( 0 <= fillcolor_red && fillcolor_red <= 255.0
205 && 0 <= fillcolor_green && fillcolor_green <= 255.0
206 && 0 <= fillcolor_blue && fillcolor_blue <= 255.0 )
207 cairo_set_source_rgb (cr, fillcolor_red / 255.0, fillcolor_green / 255.0, fillcolor_blue / 255.0);
208 cairo_text_path (cr, string);
211 x += extents.x_bearing;
219 for (c = 'A'; c <= 'I'; c++)
222 int spacing = ((c - 'A') * letterspacing) + 1;
227 cairo_move_to (cr, spacing, 0);
228 cairo_text_path (cr, string);
229 // cairo_set_source_rgb (cr, 0.1, 0.2, 0.2); // gray
230 cairo_set_source_rgb (cr, 0.8, 0.8, 0.85); // silver
231 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
233 cairo_push_group (cr);
234 cairo_set_line_width (cr, thick_width);
235 cairo_stroke_preserve (cr);
236 cairo_set_line_width (cr, thin_width);
237 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
238 cairo_stroke_preserve (cr);
240 cairo_pop_group_to_source (cr);
246 cairo_move_to (cr, spacing, 0);
247 cairo_text_path (cr, string);
248 cairo_set_source_rgb (cr, 0.4, 0.45, 0.7); // blue
254 cairo_scale (cr, 1, -1);
255 cairo_push_group (cr);
258 cairo_pattern_t *gradient;
260 for (i = 0; i < 9; i++)
263 strncpy(c, STRING + i, 1);
265 int spacing = (i * letterspacing) + 1;
266 cairo_move_to (cr, spacing, -3);
267 cairo_show_text (cr, c);
270 cairo_pop_group_to_source (cr);
272 gradient = cairo_pattern_create_linear (0, 0, 0, -fontsize);
273 cairo_pattern_add_color_stop_rgba (gradient, 0.0, 1, 1, 1, 0.5);
274 cairo_pattern_add_color_stop_rgba (gradient, 0.7, 1, 1, 1, 0.0);
276 cairo_mask (cr, gradient);
278 cairo_pattern_destroy (gradient);
284 cairo_surface_write_to_png_stream (surface, stdio_write, cgiOut);
289 cairo_surface_destroy (surface);
294 static cairo_status_t
295 stdio_write (void *closure, const unsigned char *data, unsigned int length)
297 FILE *file = closure;
298 if (fwrite (data, 1, length, file) == length)
299 return CAIRO_STATUS_SUCCESS;
301 return CAIRO_STATUS_WRITE_ERROR;
305 get_max_width(cairo_t *cr, char *characters)
308 double max_width = 0.;
311 for (i = 0; i < ARRAY_SIZE(characters); i++) {
312 cairo_text_extents_t extents;
313 string[0] = characters[i];
314 cairo_text_extents (cr, string, &extents);
315 if ((extents.width + (extents.x_bearing * 2)) > max_width)
316 max_width = extents.width + (extents.x_bearing * 2);
322 get_max_height(cairo_t *cr, char *characters)
325 double max_height = 0.;
328 for (i = 0; i < ARRAY_SIZE(characters); i++) {
329 cairo_text_extents_t extents;
330 string[0] = characters[i];
331 cairo_text_extents (cr, string, &extents);
332 // if ((extents.height - extents.y_bearing) > max_height)
333 // max_height = extents.height - extents.y_bearing;
334 if ((extents.height) > max_height)
335 max_height = extents.height;