Fixing build for GCC 4.9
[platform/upstream/cairo.git] / util / font-view.c
1 /*
2  * Copyright © 2008 Behdad Esfahbod
3  * Copyright © 2009 Chris Wilson
4  *
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.
15  *
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.
23  *
24  * Author: Chris Wilson <chris@chris-wilson.co.uk>
25  */
26
27 #include <gtk/gtk.h>
28 #include <cairo.h>
29
30 struct options {
31     const char *text;
32     const char *family;
33     cairo_font_weight_t weight;
34     cairo_font_slant_t slant;
35     double size;
36     int PAD;
37     const char *png;
38 };
39
40 static void
41 draw (cairo_t *cr, struct options *options)
42 {
43     cairo_text_extents_t extents;
44     cairo_font_extents_t font_extents;
45
46     cairo_select_font_face (cr,
47                             options->family, options->slant, options->weight);
48     cairo_set_font_size (cr, options->size);
49
50     cairo_text_extents (cr, options->text, &extents);
51     cairo_translate (cr,
52                      options->PAD - extents.x_bearing,
53                      options->PAD - extents.y_bearing);
54
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);
61     cairo_stroke (cr);
62
63     cairo_rectangle (cr,
64                      extents.x_bearing, extents.y_bearing,
65                      extents.width, extents.height);
66     cairo_set_source_rgba (cr, 0, 1, 0, .7);
67     cairo_stroke (cr);
68
69     cairo_move_to (cr, 0, 0);
70     cairo_set_source_rgb (cr, 0, 0, 1);
71     cairo_show_text (cr, options->text);
72     cairo_fill (cr);
73 }
74
75 static gboolean
76 expose_event (GtkWidget *w, GdkEventExpose *ev, struct options *options)
77 {
78     cairo_t *cr;
79
80     cr = gdk_cairo_create (w->window);
81
82     cairo_set_source_rgb (cr, 1, 1, 1);
83     cairo_paint (cr);
84
85     draw (cr, options);
86
87     cairo_destroy (cr);
88
89     if (options->png) {
90         cairo_surface_t *image;
91
92         image = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
93                                             w->allocation.width,
94                                             w->allocation.height);
95         cr = cairo_create (image);
96         cairo_set_source_rgb (cr, 1, 1, 1);
97         cairo_paint (cr);
98
99         draw (cr, options);
100
101         cairo_destroy (cr);
102         cairo_surface_write_to_png (image, options->png);
103         cairo_surface_destroy (image);
104     }
105
106     return TRUE;
107 }
108
109 static void
110 size_request (GtkWidget *w, GtkRequisition *req , struct options *options)
111 {
112     cairo_surface_t *dummy;
113     cairo_t *cr;
114     cairo_text_extents_t extents;
115
116     dummy = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 0, 0);
117     cr = cairo_create (dummy);
118     cairo_surface_destroy (dummy);
119
120     cairo_select_font_face (cr,
121                             options->family, options->slant, options->weight);
122     cairo_set_font_size (cr, options->size);
123
124     cairo_text_extents (cr, options->text, &extents);
125     cairo_destroy (cr);
126
127     req->width = extents.width + 2 * options->PAD;
128     req->height = extents.height + 2 * options->PAD;
129 }
130
131 int
132 main (int argc, char **argv)
133 {
134     GtkWidget *window;
135     struct options options = {
136         "The Quick Brown Fox Jumps Over The Lazy Dog!",
137         "@cairo:small-caps",
138         CAIRO_FONT_WEIGHT_NORMAL,
139         CAIRO_FONT_SLANT_NORMAL,
140         48,
141         30,
142         "font-view.png"
143     };
144
145     gtk_init (&argc, &argv);
146
147     /* rudimentary argument processing */
148     if (argc >= 2) {
149         options.family = argv[1];
150     }
151     if (argc >= 3) {
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;
156         else
157             options.slant = atoi (argv[2]);
158     }
159     if (argc >= 4) {
160         if (strcmp (argv[3], "bold") == 0)
161             options.weight = CAIRO_FONT_WEIGHT_BOLD;
162         else
163             options.weight = atoi (argv[3]);
164     }
165     if (argc >= 5) {
166         options.size = atof (argv[4]);
167     }
168     if (argc >= 6) {
169         options.text = argv[5];
170     }
171
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);
179
180     gtk_window_present (GTK_WINDOW (window));
181     gtk_main ();
182
183     return 0;
184 }