2 * Copyright © 2008 Behdad Esfahbod
3 * Copyright © 2009 Chris Wilson
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without
7 * fee, provided that the above copyright notice appear in all copies
8 * and that both that copyright notice and this permission notice
9 * appear in supporting documentation, and that the name of
10 * Chris Wilson not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. Chris Wilson makes no representations about the
13 * suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
16 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
19 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * Author: Chris Wilson <chris@chris-wilson.co.uk>
33 cairo_font_weight_t weight;
34 cairo_font_slant_t slant;
41 draw (cairo_t *cr, struct options *options)
43 cairo_text_extents_t extents;
44 cairo_font_extents_t font_extents;
46 cairo_select_font_face (cr,
47 options->family, options->slant, options->weight);
48 cairo_set_font_size (cr, options->size);
50 cairo_text_extents (cr, options->text, &extents);
52 options->PAD - extents.x_bearing,
53 options->PAD - extents.y_bearing);
55 cairo_font_extents (cr, &font_extents);
56 cairo_rectangle (cr, 0, -font_extents.ascent,
57 extents.x_advance, font_extents.height);
58 cairo_move_to (cr, -options->PAD, 0);
59 cairo_line_to (cr, extents.width + options->PAD, 0);
60 cairo_set_source_rgba (cr, 1, 0, 0, .7);
64 extents.x_bearing, extents.y_bearing,
65 extents.width, extents.height);
66 cairo_set_source_rgba (cr, 0, 1, 0, .7);
69 cairo_move_to (cr, 0, 0);
70 cairo_set_source_rgb (cr, 0, 0, 1);
71 cairo_show_text (cr, options->text);
76 expose_event (GtkWidget *w, GdkEventExpose *ev, struct options *options)
80 cr = gdk_cairo_create (w->window);
82 cairo_set_source_rgb (cr, 1, 1, 1);
90 cairo_surface_t *image;
92 image = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
94 w->allocation.height);
95 cr = cairo_create (image);
96 cairo_set_source_rgb (cr, 1, 1, 1);
102 cairo_surface_write_to_png (image, options->png);
103 cairo_surface_destroy (image);
110 size_request (GtkWidget *w, GtkRequisition *req , struct options *options)
112 cairo_surface_t *dummy;
114 cairo_text_extents_t extents;
116 dummy = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 0, 0);
117 cr = cairo_create (dummy);
118 cairo_surface_destroy (dummy);
120 cairo_select_font_face (cr,
121 options->family, options->slant, options->weight);
122 cairo_set_font_size (cr, options->size);
124 cairo_text_extents (cr, options->text, &extents);
127 req->width = extents.width + 2 * options->PAD;
128 req->height = extents.height + 2 * options->PAD;
132 main (int argc, char **argv)
135 struct options options = {
136 "The Quick Brown Fox Jumps Over The Lazy Dog!",
138 CAIRO_FONT_WEIGHT_NORMAL,
139 CAIRO_FONT_SLANT_NORMAL,
145 gtk_init (&argc, &argv);
147 /* rudimentary argument processing */
149 options.family = argv[1];
152 if (strcmp (argv[2], "italic") == 0)
153 options.slant = CAIRO_FONT_SLANT_ITALIC;
154 else if (strcmp (argv[2], "oblique") == 0)
155 options.slant = CAIRO_FONT_SLANT_OBLIQUE;
157 options.slant = atoi (argv[2]);
160 if (strcmp (argv[3], "bold") == 0)
161 options.weight = CAIRO_FONT_WEIGHT_BOLD;
163 options.weight = atoi (argv[3]);
166 options.size = atof (argv[4]);
169 options.text = argv[5];
172 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
173 g_signal_connect (window, "size-request",
174 G_CALLBACK (size_request), &options);
175 g_signal_connect (window, "expose-event",
176 G_CALLBACK (expose_event), &options);
177 g_signal_connect (window, "delete-event",
178 G_CALLBACK (gtk_main_quit), NULL);
180 gtk_window_present (GTK_WINDOW (window));