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]))
23 get_max_width (cairo_t *cr, const char *characters, int num_characters)
26 double max_width = 0.;
29 for (i = 0; i < num_characters; i++) {
30 cairo_text_extents_t extents;
31 string[0] = characters[i];
32 cairo_text_extents (cr, string, &extents);
33 if ((extents.width + (extents.x_bearing * 2)) > max_width)
34 max_width = extents.width + (extents.x_bearing * 2);
40 get_max_height (cairo_t *cr, const char *characters, int num_characters)
43 double max_height = 0.;
46 for (i = 0; i < num_characters; i++) {
47 cairo_text_extents_t extents;
48 string[0] = characters[i];
49 cairo_text_extents (cr, string, &extents);
50 if ((extents.height) > max_height)
51 max_height = extents.height;
67 parse_args (args_t *args)
69 /* First some defaults */
70 args->family = "Vera";
72 args->color.red = 0.0;
73 args->color.green = 0.0;
74 args->color.blue = 0.0;
76 /* XXX: Next, we should override the defaults based on
77 * command-line arguments. */
79 /* XXX: Finally, we'll want to allow CGI-based arguments as
87 cgiFormStringNoNewlines("fontname", fontname, 20);
88 cgiFormInteger("fontsize", &fontsize, 50);
89 cgiFormStringNoNewlines("format", format, 5);
90 cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
95 sscanf (fillcolor, " rgb : %d , %d , %d ", &fillcolor_red, &fillcolor_green, &fillcolor_blue);
96 if ( format[0] == 'j' && format[1] == 's' && format[2] == 'o' && format[3] == 'n' )
102 load_ft_face_for_family (const char *family)
104 /* XXX: Instead of a hard-coded filename here, we should really be
105 * taking family and using fontconfig to map that to a
107 const char font_filename[] = "./Vera.ttf";
113 error = FT_Init_FreeType( &library );
115 fprintf (stderr, "Fatal error initializing freetype.\n");
119 error = FT_New_Face( library, font_filename,
122 if ( error == FT_Err_Unknown_File_Format ) {
123 fprintf (stderr, "Unsupported font format: %s\n", font_filename);
125 } else if ( error ) {
126 fprintf (stderr, "Failed to open file: %s\n", font_filename);
134 get_characters_max_width_height (FT_Face ft_face, double size,
135 const char *characters, int num_characters,
136 double *max_width, double *max_height)
138 cairo_surface_t *surface;
140 cairo_font_face_t *cr_face;
142 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
145 cr = cairo_create (surface);
147 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
148 cairo_set_font_face (cr, cr_face);
150 cairo_set_font_size (cr, size);
152 cairo_set_line_width (cr, 1.0);
154 *max_width = get_max_width(cr, characters, num_characters);
155 *max_height = get_max_height(cr, characters, num_characters);
157 cairo_font_face_destroy (cr_face);
161 cairo_surface_destroy(surface);
165 draw_character_table (cairo_t *cr,
166 FT_Face ft_face, double size,
167 const char *characters, int num_characters,
168 double character_width, double character_height)
170 cairo_font_face_t *cr_face;
173 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
174 cairo_set_font_face (cr, cr_face);
176 cairo_set_font_size (cr, size);
178 cairo_text_extents_t extents;
182 cairo_translate (cr, 0, -(character_height / 2.50));
186 for (i = 0; i < num_characters; i++)
191 y += character_height + 8;
193 string[0] = characters[i];
195 cairo_text_extents (cr, string, &extents);
197 x -= extents.x_bearing;
199 cairo_move_to (cr, x, y);
200 cairo_text_path (cr, string);
203 x += extents.x_bearing;
204 x += character_width + 8;
207 cairo_scale (cr, 1, -1);
208 cairo_push_group (cr);
214 char characters[] = {
215 '!', '"', '#', '$', '%', '&','\'', '(',
216 ')', '*', '+', ',', '-', '.', '/', '0',
217 '1', '2', '3', '4', '5', '6', '7', '8',
218 '9', ':', ';', '<', '=', '>', '?', '@',
219 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
220 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
221 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
222 'Y', 'Z', '[','\\', ']', '^', '_', '`',
223 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
224 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
225 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
226 'y', 'z', '{', '|', '}', '~'
231 double max_width, max_height;
232 cairo_surface_t *surface;
237 ft_face = load_ft_face_for_family (args.family);
239 get_characters_max_width_height (ft_face, args.size,
240 characters, ARRAY_SIZE (characters),
241 &max_width, &max_height);
244 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
245 (max_width + 8) * 10,
246 (max_height + 8) * 10);
248 cr = cairo_create (surface);
250 cairo_set_source_rgb (cr,
255 draw_character_table (cr, ft_face, args.size,
256 characters, ARRAY_SIZE (characters),
257 max_width, max_height);
259 cairo_surface_write_to_png (surface, "spritext-output.png");
260 printf ("Result written to spritext-output.png\n");
264 cairo_surface_destroy (surface);