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 char outputJson = FALSE;
39 '!', '"', '#', '$', '%', '&','\'', '(',
40 ')', '*', '+', ',', '-', '.', '/', '0',
41 '1', '2', '3', '4', '5', '6', '7', '8',
42 '9', ':', ';', '<', '=', '>', '?', '@',
43 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
44 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
45 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
46 'Y', 'Z', '[','\\', ']', '^', '_', '`',
47 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
48 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
49 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
50 'y', 'z', '{', '|', '}', '~'
62 cgiFormStringNoNewlines("fontname", fontname, 20);
63 cgiFormInteger("fontsize", &fontsize, 50);
64 cgiFormStringNoNewlines("format", format, 5);
65 cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
71 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 cgiHeaderContentType("application/json");
81 cgiHeaderContentType("image/png");
89 FT_UInt left_index, right_index;
92 error = FT_Init_FreeType( &library );
96 error = FT_New_Face( library,"/srv/rdworth.org/cgi-bin/Verdana.ttf",
99 if ( error == FT_Err_Unknown_File_Format )
100 printf("File opened. Font format unsupported.");
102 printf("Could not be open or read or broken.");
104 use_kerning = FT_HAS_KERNING( ft_face );
106 /* Compute max-width and max-height */
107 cairo_surface_t *surface;
109 cairo_font_face_t *cr_face;
111 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
114 cr = cairo_create (surface);
117 cairo_select_font_face (cr, fontname,
118 CAIRO_FONT_SLANT_NORMAL,
119 CAIRO_FONT_WEIGHT_NORMAL);
121 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
122 cairo_set_font_face (cr, cr_face);
124 cairo_set_font_size (cr, fontsize);
126 cairo_set_line_width (cr, 1.0);
128 double max_width = get_max_width(cr, characters);
129 double max_height = get_max_height(cr, characters);
131 cairo_font_face_destroy (cr_face);
135 cairo_surface_destroy(surface);
138 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
139 (max_width + 8) * 10,
140 (max_height + 8) * 10);
142 cr = cairo_create (surface);
145 cairo_select_font_face (cr, fontname,
146 CAIRO_FONT_SLANT_NORMAL,
147 CAIRO_FONT_WEIGHT_NORMAL);
149 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
150 cairo_set_font_face (cr, cr_face);
152 cairo_set_font_size (cr, fontsize);
154 cairo_text_extents_t extents;
161 cairo_translate (cr, 0, -(max_height / 2.50));
163 // cairo_translate (cr, 0, -5.0);
167 for (i = 0; i < ARRAY_SIZE(characters); i++)
174 string[0] = characters[i];
176 cairo_text_extents (cr, string, &extents);
182 printf ("u%i:{", characters[i]);
183 printf ("x:%.1f,y:%.1f,w:%.1f,h:%.1f",
185 y - (max_height * 1.15),
186 extents.width + extents.x_bearing,
189 for ( j = 0; j < ARRAY_SIZE(characters); j++ )
191 right_index = FT_Get_Char_Index( ft_face, characters[i] );
192 left_index = FT_Get_Char_Index( ft_face, characters[j] );
194 FT_Get_Kerning( ft_face, left_index, right_index,
195 FT_KERNING_UNSCALED, &kerning );
198 printf(",k%d:%ld", characters[j], kerning.x);
204 x -= extents.x_bearing;
206 cairo_move_to (cr, x, y);
207 cairo_set_source_rgb (cr, 0., 0., 0.); // black
208 if ( 0 <= fillcolor_red && fillcolor_red <= 255.0
209 && 0 <= fillcolor_green && fillcolor_green <= 255.0
210 && 0 <= fillcolor_blue && fillcolor_blue <= 255.0 )
211 cairo_set_source_rgb (cr, fillcolor_red / 255.0, fillcolor_green / 255.0, fillcolor_blue / 255.0);
212 cairo_text_path (cr, string);
215 x += extents.x_bearing;
223 for (c = 'A'; c <= 'I'; c++)
226 int spacing = ((c - 'A') * letterspacing) + 1;
231 cairo_move_to (cr, spacing, 0);
232 cairo_text_path (cr, string);
233 // cairo_set_source_rgb (cr, 0.1, 0.2, 0.2); // gray
234 cairo_set_source_rgb (cr, 0.8, 0.8, 0.85); // silver
235 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
237 cairo_push_group (cr);
238 cairo_set_line_width (cr, thick_width);
239 cairo_stroke_preserve (cr);
240 cairo_set_line_width (cr, thin_width);
241 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
242 cairo_stroke_preserve (cr);
244 cairo_pop_group_to_source (cr);
250 cairo_move_to (cr, spacing, 0);
251 cairo_text_path (cr, string);
252 cairo_set_source_rgb (cr, 0.4, 0.45, 0.7); // blue
258 cairo_scale (cr, 1, -1);
259 cairo_push_group (cr);
262 cairo_pattern_t *gradient;
264 for (i = 0; i < 9; i++)
267 strncpy(c, STRING + i, 1);
269 int spacing = (i * letterspacing) + 1;
270 cairo_move_to (cr, spacing, -3);
271 cairo_show_text (cr, c);
274 cairo_pop_group_to_source (cr);
276 gradient = cairo_pattern_create_linear (0, 0, 0, -fontsize);
277 cairo_pattern_add_color_stop_rgba (gradient, 0.0, 1, 1, 1, 0.5);
278 cairo_pattern_add_color_stop_rgba (gradient, 0.7, 1, 1, 1, 0.0);
280 cairo_mask (cr, gradient);
282 cairo_pattern_destroy (gradient);
290 cairo_surface_write_to_png_stream (surface, stdio_write, cgiOut);
296 cairo_surface_destroy (surface);
302 static cairo_status_t
303 stdio_write (void *closure, const unsigned char *data, unsigned int length)
305 FILE *file = closure;
306 if (fwrite (data, 1, length, file) == length)
307 return CAIRO_STATUS_SUCCESS;
309 return CAIRO_STATUS_WRITE_ERROR;
314 get_max_width(cairo_t *cr, char *characters)
317 double max_width = 0.;
320 for (i = 0; i < ARRAY_SIZE(characters); i++) {
321 cairo_text_extents_t extents;
322 string[0] = characters[i];
323 cairo_text_extents (cr, string, &extents);
324 if ((extents.width + (extents.x_bearing * 2)) > max_width)
325 max_width = extents.width + (extents.x_bearing * 2);
331 get_max_height(cairo_t *cr, char *characters)
334 double max_height = 0.;
337 for (i = 0; i < ARRAY_SIZE(characters); i++) {
338 cairo_text_extents_t extents;
339 string[0] = characters[i];
340 cairo_text_extents (cr, string, &extents);
341 // if ((extents.height - extents.y_bearing) > max_height)
342 // max_height = extents.height - extents.y_bearing;
343 if ((extents.height) > max_height)
344 max_height = extents.height;