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 - extents.y_bearing) > max_height)
51 // max_height = extents.height - extents.y_bearing;
52 if ((extents.height) > max_height)
53 max_height = extents.height;
69 parse_args (args_t *args)
71 /* First some defaults */
72 args->family = "Vera";
74 args->color.red = 0.0;
75 args->color.green = 0.0;
76 args->color.blue = 0.0;
78 /* XXX: Next, we should override the defaults based on
79 * command-line arguments. */
81 /* XXX: Finally, we'll want to allow CGI-based arguments as
89 cgiFormStringNoNewlines("fontname", fontname, 20);
90 cgiFormInteger("fontsize", &fontsize, 50);
91 cgiFormStringNoNewlines("format", format, 5);
92 cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
97 sscanf (fillcolor, " rgb : %d , %d , %d ", &fillcolor_red, &fillcolor_green, &fillcolor_blue);
98 if ( format[0] == 'j' && format[1] == 's' && format[2] == 'o' && format[3] == 'n' )
104 load_ft_face_for_family (const char *family)
106 /* XXX: Instead of a hard-coded filename here, we should really be
107 * taking family and using fontconfig to map that to a
109 const char font_filename[] = "./Vera.ttf";
115 error = FT_Init_FreeType( &library );
117 fprintf (stderr, "Fatal error initializing freetype.\n");
121 error = FT_New_Face( library, font_filename,
124 if ( error == FT_Err_Unknown_File_Format ) {
125 fprintf (stderr, "Unsupported font format: %s\n", font_filename);
127 } else if ( error ) {
128 fprintf (stderr, "Failed to open file: %s\n", font_filename);
136 get_characters_max_width_height (FT_Face ft_face, double size,
137 const char *characters, int num_characters,
138 double *max_width, double *max_height)
140 cairo_surface_t *surface;
142 cairo_font_face_t *cr_face;
144 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
147 cr = cairo_create (surface);
150 cairo_select_font_face (cr, fontname,
151 CAIRO_FONT_SLANT_NORMAL,
152 CAIRO_FONT_WEIGHT_NORMAL);
154 cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
155 cairo_set_font_face (cr, cr_face);
157 cairo_set_font_size (cr, size);
159 cairo_set_line_width (cr, 1.0);
161 *max_width = get_max_width(cr, characters, num_characters);
162 *max_height = get_max_height(cr, characters, num_characters);
164 cairo_font_face_destroy (cr_face);
168 cairo_surface_destroy(surface);
172 draw_character_table (cairo_t *cr,
173 FT_Face ft_face, double size,
174 const char *characters, int num_characters,
175 double character_width, double character_height)
177 cairo_font_face_t *cr_face;
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, size);
190 cairo_text_extents_t extents;
194 cairo_translate (cr, 0, -(character_height / 2.50));
196 // cairo_translate (cr, 0, -5.0);
200 for (i = 0; i < num_characters; i++)
205 y += character_height + 8;
207 string[0] = characters[i];
209 cairo_text_extents (cr, string, &extents);
211 x -= extents.x_bearing;
213 cairo_move_to (cr, x, y);
214 cairo_text_path (cr, string);
217 x += extents.x_bearing;
218 x += character_width + 8;
225 for (c = 'A'; c <= 'I'; c++)
228 int spacing = ((c - 'A') * letterspacing) + 1;
233 cairo_move_to (cr, spacing, 0);
234 cairo_text_path (cr, string);
235 // cairo_set_source_rgb (cr, 0.1, 0.2, 0.2); // gray
236 cairo_set_source_rgb (cr, 0.8, 0.8, 0.85); // silver
237 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
239 cairo_push_group (cr);
240 cairo_set_line_width (cr, thick_width);
241 cairo_stroke_preserve (cr);
242 cairo_set_line_width (cr, thin_width);
243 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
244 cairo_stroke_preserve (cr);
246 cairo_pop_group_to_source (cr);
252 cairo_move_to (cr, spacing, 0);
253 cairo_text_path (cr, string);
254 cairo_set_source_rgb (cr, 0.4, 0.45, 0.7); // blue
260 cairo_scale (cr, 1, -1);
261 cairo_push_group (cr);
264 cairo_pattern_t *gradient;
266 for (i = 0; i < 9; i++)
269 strncpy(c, STRING + i, 1);
271 int spacing = (i * letterspacing) + 1;
272 cairo_move_to (cr, spacing, -3);
273 cairo_show_text (cr, c);
276 cairo_pop_group_to_source (cr);
278 gradient = cairo_pattern_create_linear (0, 0, 0, -fontsize);
279 cairo_pattern_add_color_stop_rgba (gradient, 0.0, 1, 1, 1, 0.5);
280 cairo_pattern_add_color_stop_rgba (gradient, 0.7, 1, 1, 1, 0.0);
282 cairo_mask (cr, gradient);
284 cairo_pattern_destroy (gradient);
291 char characters[] = {
292 '!', '"', '#', '$', '%', '&','\'', '(',
293 ')', '*', '+', ',', '-', '.', '/', '0',
294 '1', '2', '3', '4', '5', '6', '7', '8',
295 '9', ':', ';', '<', '=', '>', '?', '@',
296 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
297 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
298 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
299 'Y', 'Z', '[','\\', ']', '^', '_', '`',
300 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
301 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
302 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
303 'y', 'z', '{', '|', '}', '~'
308 double max_width, max_height;
309 cairo_surface_t *surface;
314 ft_face = load_ft_face_for_family (args.family);
316 get_characters_max_width_height (ft_face, args.size,
317 characters, ARRAY_SIZE (characters),
318 &max_width, &max_height);
321 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
322 (max_width + 8) * 10,
323 (max_height + 8) * 10);
325 cr = cairo_create (surface);
327 cairo_set_source_rgb (cr,
332 draw_character_table (cr, ft_face, args.size,
333 characters, ARRAY_SIZE (characters),
334 max_width, max_height);
336 cairo_surface_write_to_png (surface, "spritext-output.png");
337 printf ("Result written to spritext-output.png\n");
341 cairo_surface_destroy (surface);