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, see <http://www.gnu.org/licenses/>.
17 * Authors: Benjamin Otte <otte@gnome.org>
22 #include "gtkcssimageprivate.h"
24 /* for the types only */
25 #include "gtk/gtkcssimagecrossfadeprivate.h"
26 #include "gtk/gtkcssimagegradientprivate.h"
27 #include "gtk/gtkcssimagelinearprivate.h"
28 #include "gtk/gtkcssimageurlprivate.h"
29 #include "gtk/gtkcssimagewin32private.h"
31 G_DEFINE_ABSTRACT_TYPE (GtkCssImage, _gtk_css_image, G_TYPE_OBJECT)
34 gtk_css_image_real_get_width (GtkCssImage *image)
40 gtk_css_image_real_get_height (GtkCssImage *image)
46 gtk_css_image_real_get_aspect_ratio (GtkCssImage *image)
50 width = _gtk_css_image_get_width (image);
51 height = _gtk_css_image_get_height (image);
54 return (double) width / height;
60 gtk_css_image_real_compute (GtkCssImage *image,
61 GtkStyleContext *context)
63 return g_object_ref (image);
67 _gtk_css_image_class_init (GtkCssImageClass *klass)
69 klass->get_width = gtk_css_image_real_get_width;
70 klass->get_height = gtk_css_image_real_get_height;
71 klass->get_aspect_ratio = gtk_css_image_real_get_aspect_ratio;
72 klass->compute = gtk_css_image_real_compute;
76 _gtk_css_image_init (GtkCssImage *image)
81 _gtk_css_image_get_width (GtkCssImage *image)
83 GtkCssImageClass *klass;
85 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), 0);
87 klass = GTK_CSS_IMAGE_GET_CLASS (image);
89 return klass->get_width (image);
93 _gtk_css_image_get_height (GtkCssImage *image)
95 GtkCssImageClass *klass;
97 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), 0);
99 klass = GTK_CSS_IMAGE_GET_CLASS (image);
101 return klass->get_height (image);
105 _gtk_css_image_get_aspect_ratio (GtkCssImage *image)
107 GtkCssImageClass *klass;
109 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), 0);
111 klass = GTK_CSS_IMAGE_GET_CLASS (image);
113 return klass->get_aspect_ratio (image);
117 _gtk_css_image_compute (GtkCssImage *image,
118 GtkStyleContext *context)
120 GtkCssImageClass *klass;
122 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), NULL);
123 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
125 klass = GTK_CSS_IMAGE_GET_CLASS (image);
127 return klass->compute (image, context);
131 _gtk_css_image_draw (GtkCssImage *image,
136 GtkCssImageClass *klass;
138 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
139 g_return_if_fail (cr != NULL);
143 klass = GTK_CSS_IMAGE_GET_CLASS (image);
145 klass->draw (image, cr, width, height);
151 _gtk_css_image_print (GtkCssImage *image,
154 GtkCssImageClass *klass;
156 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
157 g_return_if_fail (string != NULL);
159 klass = GTK_CSS_IMAGE_GET_CLASS (image);
161 klass->print (image, string);
164 /* Applies the algorithm outlined in
165 * http://dev.w3.org/csswg/css3-images/#default-sizing
168 _gtk_css_image_get_concrete_size (GtkCssImage *image,
169 double specified_width,
170 double specified_height,
171 double default_width,
172 double default_height,
173 double *concrete_width,
174 double *concrete_height)
176 double image_width, image_height, image_aspect;
178 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
179 g_return_if_fail (specified_width >= 0);
180 g_return_if_fail (specified_height >= 0);
181 g_return_if_fail (default_width > 0);
182 g_return_if_fail (default_height > 0);
183 g_return_if_fail (concrete_width != NULL);
184 g_return_if_fail (concrete_height != NULL);
186 /* If the specified size is a definite width and height,
187 * the concrete object size is given that width and height.
189 if (specified_width && specified_height)
191 *concrete_width = specified_width;
192 *concrete_height = specified_height;
196 image_width = _gtk_css_image_get_width (image);
197 image_height = _gtk_css_image_get_height (image);
198 image_aspect = _gtk_css_image_get_aspect_ratio (image);
200 /* If the specified size has neither a definite width nor height,
201 * and has no additional contraints, the dimensions of the concrete
202 * object size are calculated as follows:
204 if (specified_width == 0.0 && specified_height == 0.0)
206 /* If the object has only an intrinsic aspect ratio,
207 * the concrete object size must have that aspect ratio,
208 * and additionally be as large as possible without either
209 * its height or width exceeding the height or width of the
210 * default object size.
212 if (image_aspect > 0 && image_width == 0 && image_height == 0)
214 if (image_aspect * default_height > default_width)
216 *concrete_width = default_height * image_aspect;
217 *concrete_height = default_height;
221 *concrete_width = default_width;
222 *concrete_height = default_width / image_aspect;
226 /* Otherwise, the width and height of the concrete object
227 * size is the same as the object's intrinsic width and
228 * intrinsic height, if they exist.
229 * If the concrete object size is still missing a width or
230 * height, and the object has an intrinsic aspect ratio,
231 * the missing dimension is calculated from the present
232 * dimension and the intrinsic aspect ratio.
233 * Otherwise, the missing dimension is taken from the default
237 *concrete_width = image_width;
238 else if (image_aspect)
239 *concrete_width = image_height * image_aspect;
241 *concrete_width = default_width;
244 *concrete_height = image_height;
245 else if (image_aspect)
246 *concrete_height = image_width / image_aspect;
248 *concrete_height = default_height;
253 /* If the specified size has only a width or height, but not both,
254 * then the concrete object size is given that specified width or height.
255 * The other dimension is calculated as follows:
256 * If the object has an intrinsic aspect ratio, the missing dimension of
257 * the concrete object size is calculated using the intrinsic aspect-ratio
258 * and the present dimension.
259 * Otherwise, if the missing dimension is present in the object's intrinsic
260 * dimensions, the missing dimension is taken from the object's intrinsic
262 * Otherwise, the missing dimension of the concrete object size is taken
263 * from the default object size.
267 *concrete_width = specified_width;
269 *concrete_height = specified_width / image_aspect;
270 else if (image_height)
271 *concrete_height = image_height;
273 *concrete_height = default_height;
277 *concrete_height = specified_height;
279 *concrete_width = specified_height * image_aspect;
280 else if (image_width)
281 *concrete_width = image_width;
283 *concrete_width = default_width;
288 _gtk_css_image_get_surface (GtkCssImage *image,
289 cairo_surface_t *target,
293 cairo_surface_t *result;
296 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), NULL);
297 g_return_val_if_fail (surface_width > 0, NULL);
298 g_return_val_if_fail (surface_height > 0, NULL);
301 result = cairo_surface_create_similar (target,
302 CAIRO_CONTENT_COLOR_ALPHA,
306 result = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
310 cr = cairo_create (result);
311 _gtk_css_image_draw (image, cr, surface_width, surface_height);
318 gtk_css_image_get_parser_type (GtkCssParser *parser)
320 static const struct {
322 GType (* type_func) (void);
324 { "url", _gtk_css_image_url_get_type },
325 { "-gtk-gradient", _gtk_css_image_gradient_get_type },
326 { "-gtk-win32-theme-part", _gtk_css_image_win32_get_type },
327 { "linear-gradient", _gtk_css_image_linear_get_type },
328 { "repeating-linear-gradient", _gtk_css_image_linear_get_type },
329 { "cross-fade", _gtk_css_image_cross_fade_get_type }
333 for (i = 0; i < G_N_ELEMENTS (image_types); i++)
335 if (_gtk_css_parser_has_prefix (parser, image_types[i].prefix))
336 return image_types[i].type_func ();
339 return G_TYPE_INVALID;
343 * _gtk_css_image_can_parse:
344 * @parser: a css parser
346 * Checks if the parser can potentially parse the given stream as an
347 * image from looking at the first token of @parser. This is useful for
348 * implementing shorthand properties. A successful parse of an image
349 * can not be guaranteed.
351 * Returns: %TURE if it looks like an image.
354 _gtk_css_image_can_parse (GtkCssParser *parser)
356 return gtk_css_image_get_parser_type (parser) != G_TYPE_INVALID;
360 _gtk_css_image_new_parse (GtkCssParser *parser,
363 GtkCssImageClass *klass;
367 g_return_val_if_fail (parser != NULL, NULL);
368 g_return_val_if_fail (G_IS_FILE (base), NULL);
370 image_type = gtk_css_image_get_parser_type (parser);
371 if (image_type == G_TYPE_INVALID)
373 _gtk_css_parser_error (parser, "Not a valid image");
377 image = g_object_new (image_type, NULL);
379 klass = GTK_CSS_IMAGE_GET_CLASS (image);
380 if (!klass->parse (image, parser, base))
382 g_object_unref (image);