2 * Copyright © 2011 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Benjamin Otte <otte@gnome.org>
23 #include "gtkcssimageprivate.h"
25 /* for the types only */
26 #include "gtk/gtkcssimagegradientprivate.h"
27 #include "gtk/gtkcssimageurlprivate.h"
28 #include "gtk/gtkcssimagewin32private.h"
30 G_DEFINE_ABSTRACT_TYPE (GtkCssImage, _gtk_css_image, G_TYPE_OBJECT)
33 gtk_css_image_real_get_width (GtkCssImage *image)
39 gtk_css_image_real_get_height (GtkCssImage *image)
45 gtk_css_image_real_get_aspect_ratio (GtkCssImage *image)
49 width = _gtk_css_image_get_width (image);
50 height = _gtk_css_image_get_height (image);
53 return (double) width / height;
59 gtk_css_image_real_compute (GtkCssImage *image,
60 GtkStyleContext *context)
62 return g_object_ref (image);
66 _gtk_css_image_class_init (GtkCssImageClass *klass)
68 klass->get_width = gtk_css_image_real_get_width;
69 klass->get_height = gtk_css_image_real_get_height;
70 klass->get_aspect_ratio = gtk_css_image_real_get_aspect_ratio;
71 klass->compute = gtk_css_image_real_compute;
75 _gtk_css_image_init (GtkCssImage *image)
80 _gtk_css_image_get_width (GtkCssImage *image)
82 GtkCssImageClass *klass;
84 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), 0);
86 klass = GTK_CSS_IMAGE_GET_CLASS (image);
88 return klass->get_width (image);
92 _gtk_css_image_get_height (GtkCssImage *image)
94 GtkCssImageClass *klass;
96 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), 0);
98 klass = GTK_CSS_IMAGE_GET_CLASS (image);
100 return klass->get_height (image);
104 _gtk_css_image_get_aspect_ratio (GtkCssImage *image)
106 GtkCssImageClass *klass;
108 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), 0);
110 klass = GTK_CSS_IMAGE_GET_CLASS (image);
112 return klass->get_aspect_ratio (image);
116 _gtk_css_image_compute (GtkCssImage *image,
117 GtkStyleContext *context)
119 GtkCssImageClass *klass;
121 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), NULL);
122 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
124 klass = GTK_CSS_IMAGE_GET_CLASS (image);
126 return klass->compute (image, context);
130 _gtk_css_image_draw (GtkCssImage *image,
135 GtkCssImageClass *klass;
137 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
138 g_return_if_fail (cr != NULL);
142 klass = GTK_CSS_IMAGE_GET_CLASS (image);
144 klass->draw (image, cr, width, height);
150 _gtk_css_image_print (GtkCssImage *image,
153 GtkCssImageClass *klass;
155 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
156 g_return_if_fail (string != NULL);
158 klass = GTK_CSS_IMAGE_GET_CLASS (image);
160 klass->print (image, string);
163 /* Applies the algorithm outlined in
164 * http://dev.w3.org/csswg/css3-images/#default-sizing
167 _gtk_css_image_get_concrete_size (GtkCssImage *image,
168 double specified_width,
169 double specified_height,
170 double default_width,
171 double default_height,
172 double *concrete_width,
173 double *concrete_height)
175 double image_width, image_height, image_aspect;
177 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
178 g_return_if_fail (specified_width >= 0);
179 g_return_if_fail (specified_height >= 0);
180 g_return_if_fail (default_width > 0);
181 g_return_if_fail (default_height > 0);
182 g_return_if_fail (concrete_width != NULL);
183 g_return_if_fail (concrete_height != NULL);
185 /* If the specified size is a definite width and height,
186 * the concrete object size is given that width and height.
188 if (specified_width && specified_height)
190 *concrete_width = specified_width;
191 *concrete_height = specified_height;
195 image_width = _gtk_css_image_get_width (image);
196 image_height = _gtk_css_image_get_height (image);
197 image_aspect = _gtk_css_image_get_aspect_ratio (image);
199 /* If the specified size has neither a definite width nor height,
200 * and has no additional contraints, the dimensions of the concrete
201 * object size are calculated as follows:
203 if (specified_width == 0.0 && specified_height == 0.0)
205 /* If the object has only an intrinsic aspect ratio,
206 * the concrete object size must have that aspect ratio,
207 * and additionally be as large as possible without either
208 * its height or width exceeding the height or width of the
209 * default object size.
211 if (image_aspect > 0 && image_width == 0 && image_height == 0)
213 if (image_aspect * default_height > default_width)
215 *concrete_width = default_height * image_aspect;
216 *concrete_height = default_height;
220 *concrete_width = default_width;
221 *concrete_height = default_width / image_aspect;
225 /* Otherwise, the width and height of the concrete object
226 * size is the same as the object's intrinsic width and
227 * intrinsic height, if they exist.
228 * If the concrete object size is still missing a width or
229 * height, and the object has an intrinsic aspect ratio,
230 * the missing dimension is calculated from the present
231 * dimension and the intrinsic aspect ratio.
232 * Otherwise, the missing dimension is taken from the default
236 *concrete_width = image_width;
237 else if (image_aspect)
238 *concrete_width = image_height * image_aspect;
240 *concrete_width = default_width;
243 *concrete_height = image_height;
244 else if (image_aspect)
245 *concrete_height = image_width / image_aspect;
247 *concrete_height = default_height;
252 /* If the specified size has only a width or height, but not both,
253 * then the concrete object size is given that specified width or height.
254 * The other dimension is calculated as follows:
255 * If the object has an intrinsic aspect ratio, the missing dimension of
256 * the concrete object size is calculated using the intrinsic aspect-ratio
257 * and the present dimension.
258 * Otherwise, if the missing dimension is present in the object's intrinsic
259 * dimensions, the missing dimension is taken from the object's intrinsic
261 * Otherwise, the missing dimension of the concrete object size is taken
262 * from the default object size.
266 *concrete_width = specified_width;
268 *concrete_height = specified_width / image_aspect;
269 else if (image_height)
270 *concrete_height = image_height;
272 *concrete_height = default_height;
276 *concrete_height = specified_height;
278 *concrete_width = specified_height * image_aspect;
279 else if (image_width)
280 *concrete_width = image_width;
282 *concrete_width = default_width;
287 _gtk_css_image_get_surface (GtkCssImage *image,
288 cairo_surface_t *target,
292 cairo_surface_t *result;
295 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), NULL);
296 g_return_val_if_fail (surface_width > 0, NULL);
297 g_return_val_if_fail (surface_height > 0, NULL);
300 result = cairo_surface_create_similar (target,
301 CAIRO_CONTENT_COLOR_ALPHA,
305 result = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
309 cr = cairo_create (result);
310 _gtk_css_image_draw (image, cr, surface_width, surface_height);
317 gtk_css_image_get_parser_type (GtkCssParser *parser)
319 static const struct {
321 GType (* type_func) (void);
323 { "url", _gtk_css_image_url_get_type },
324 { "-gtk-gradient", _gtk_css_image_gradient_get_type },
325 { "-gtk-win32-theme-part", _gtk_css_image_win32_get_type }
329 for (i = 0; i < G_N_ELEMENTS (image_types); i++)
331 if (_gtk_css_parser_has_prefix (parser, image_types[i].prefix))
332 return image_types[i].type_func ();
335 return G_TYPE_INVALID;
339 * _gtk_css_image_can_parse:
340 * @parser: a css parser
342 * Checks if the parser can potentially parse the given stream as an
343 * image from looking at the first token of @parser. This is useful for
344 * implementing shorthand properties. A successful parse of an image
345 * can not be guaranteed.
347 * Returns: %TURE if it looks like an image.
350 _gtk_css_image_can_parse (GtkCssParser *parser)
352 return gtk_css_image_get_parser_type (parser) != G_TYPE_INVALID;
356 _gtk_css_image_new_parse (GtkCssParser *parser,
359 GtkCssImageClass *klass;
363 g_return_val_if_fail (parser != NULL, NULL);
364 g_return_val_if_fail (G_IS_FILE (base), NULL);
366 image_type = gtk_css_image_get_parser_type (parser);
367 if (image_type == G_TYPE_INVALID)
369 _gtk_css_parser_error (parser, "Not a valid image");
373 image = g_object_new (image_type, NULL);
375 klass = GTK_CSS_IMAGE_GET_CLASS (image);
376 if (!klass->parse (image, parser, base))
378 g_object_unref (image);