1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3 * Copyright (C) 2011 Red Hat, Inc.
5 * Authors: Carlos Garnacho <carlosg@gnome.org>
6 * Cosimo Cecchi <cosimoc@gnome.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include <cairo-gobject.h>
27 #include "gtkborderimageprivate.h"
28 #include "gtkcssimagevalueprivate.h"
29 #include "gtkcssrepeatvalueprivate.h"
30 #include "gtkstylepropertiesprivate.h"
31 #include "gtkthemingengineprivate.h"
33 /* this is in case round() is not provided by the compiler,
34 * such as in the case of C89 compilers, like MSVC
36 #include "fallback-c89.c"
39 _gtk_border_image_init (GtkBorderImage *image,
40 GtkThemingEngine *engine)
44 image->source = _gtk_css_image_value_get_image (_gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_BORDER_IMAGE_SOURCE));
45 if (image->source == NULL)
48 image->slice = *(GtkBorder *) _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_BORDER_IMAGE_SLICE));
49 width = _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_BORDER_IMAGE_WIDTH));
52 image->width = *width;
53 image->has_width = TRUE;
56 image->has_width = FALSE;
58 image->repeat = _gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_BORDER_IMAGE_REPEAT);
63 typedef struct _GtkBorderImageSliceSize GtkBorderImageSliceSize;
64 struct _GtkBorderImageSliceSize {
70 gtk_border_image_compute_border_size (GtkBorderImageSliceSize sizes[3],
76 /* This code assumes area_size >= start_border + end_border */
78 sizes[0].offset = offset;
79 sizes[0].size = start_border;
80 sizes[1].offset = offset + start_border;
81 sizes[1].size = area_size - start_border - end_border;
82 sizes[2].offset = offset + area_size - end_border;
83 sizes[2].size = end_border;
87 gtk_border_image_render_slice (cairo_t *cr,
88 cairo_surface_t *slice,
95 GtkCssRepeatStyle hrepeat,
96 GtkCssRepeatStyle vrepeat)
98 double hscale, vscale;
100 cairo_extend_t extend = CAIRO_EXTEND_PAD;
101 cairo_matrix_t matrix;
102 cairo_pattern_t *pattern;
104 /* We can't draw center tiles yet */
105 g_assert (hrepeat == GTK_CSS_REPEAT_STYLE_STRETCH || vrepeat == GTK_CSS_REPEAT_STYLE_STRETCH);
107 hscale = width / slice_width;
108 vscale = height / slice_height;
114 case GTK_CSS_REPEAT_STYLE_REPEAT:
115 extend = CAIRO_EXTEND_REPEAT;
118 case GTK_CSS_REPEAT_STYLE_SPACE:
122 extend = CAIRO_EXTEND_NONE;
125 xstep = hscale * slice_width;
126 n = floor (width / xstep);
127 space = (width - n * xstep) / (n + 1);
133 case GTK_CSS_REPEAT_STYLE_STRETCH:
135 case GTK_CSS_REPEAT_STYLE_ROUND:
136 extend = CAIRO_EXTEND_REPEAT;
137 hscale = width / (slice_width * MAX (round (width / (slice_width * vscale)), 1));
140 g_assert_not_reached ();
146 case GTK_CSS_REPEAT_STYLE_REPEAT:
147 extend = CAIRO_EXTEND_REPEAT;
150 case GTK_CSS_REPEAT_STYLE_SPACE:
154 extend = CAIRO_EXTEND_NONE;
157 ystep = vscale * slice_height;
158 n = floor (height / ystep);
159 space = (height - n * ystep) / (n + 1);
165 case GTK_CSS_REPEAT_STYLE_STRETCH:
167 case GTK_CSS_REPEAT_STYLE_ROUND:
168 extend = CAIRO_EXTEND_REPEAT;
169 vscale = height / (slice_height * MAX (round (height / (slice_height * hscale)), 1));
172 g_assert_not_reached ();
176 pattern = cairo_pattern_create_for_surface (slice);
178 cairo_matrix_init_translate (&matrix,
179 hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_width / 2 : 0,
180 vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_height / 2 : 0);
181 cairo_matrix_scale (&matrix, 1 / hscale, 1 / vscale);
182 cairo_matrix_translate (&matrix,
183 hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - width / 2 : 0,
184 vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - height / 2 : 0);
186 cairo_pattern_set_matrix (pattern, &matrix);
187 cairo_pattern_set_extend (pattern, extend);
190 cairo_translate (cr, x, y);
192 for (y = 0; y < height; y += ystep)
194 for (x = 0; x < width; x += xstep)
197 cairo_translate (cr, x, y);
198 cairo_set_source (cr, pattern);
199 cairo_rectangle (cr, 0, 0, xstep, ystep);
207 cairo_pattern_destroy (pattern);
211 gtk_border_image_compute_slice_size (GtkBorderImageSliceSize sizes[3],
216 sizes[0].size = MIN (start_size, surface_size);
219 sizes[2].size = MIN (end_size, surface_size);
220 sizes[2].offset = surface_size - sizes[2].size;
222 sizes[1].size = MAX (0, surface_size - sizes[0].size - sizes[2].size);
223 sizes[1].offset = sizes[0].size;
227 _gtk_border_image_render (GtkBorderImage *image,
228 GtkBorder *border_width,
235 cairo_surface_t *surface, *slice;
236 GtkBorderImageSliceSize vertical_slice[3], horizontal_slice[3];
237 GtkBorderImageSliceSize vertical_border[3], horizontal_border[3];
238 double source_width, source_height;
241 if (image->has_width)
242 border_width = &image->width;
244 _gtk_css_image_get_concrete_size (image->source,
247 &source_width, &source_height);
249 /* XXX: Optimize for (source_width == width && source_height == height) */
251 surface = _gtk_css_image_get_surface (image->source,
252 cairo_get_target (cr),
253 source_width, source_height);
255 gtk_border_image_compute_slice_size (horizontal_slice,
259 gtk_border_image_compute_slice_size (vertical_slice,
262 image->slice.bottom);
263 gtk_border_image_compute_border_size (horizontal_border,
267 border_width->right);
268 gtk_border_image_compute_border_size (vertical_border,
272 border_width->bottom);
274 for (v = 0; v < 3; v++)
276 if (vertical_slice[v].size == 0 ||
277 vertical_border[v].size == 0)
280 for (h = 0; h < 3; h++)
282 if (horizontal_slice[h].size == 0 ||
283 horizontal_border[h].size == 0)
286 if (h == 1 && v == 1)
289 slice = cairo_surface_create_for_rectangle (surface,
290 horizontal_slice[h].offset,
291 vertical_slice[v].offset,
292 horizontal_slice[h].size,
293 vertical_slice[v].size);
295 gtk_border_image_render_slice (cr,
297 horizontal_slice[h].size,
298 vertical_slice[v].size,
299 horizontal_border[h].offset,
300 vertical_border[v].offset,
301 horizontal_border[h].size,
302 vertical_border[v].size,
303 h == 1 ? _gtk_css_border_repeat_value_get_x (image->repeat) : GTK_CSS_REPEAT_STYLE_STRETCH,
304 v == 1 ? _gtk_css_border_repeat_value_get_y (image->repeat) : GTK_CSS_REPEAT_STYLE_STRETCH);
306 cairo_surface_destroy (slice);
310 cairo_surface_destroy (surface);