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 "gtkstylepropertiesprivate.h"
29 #include "gtkthemingengineprivate.h"
31 /* this is in case round() is not provided by the compiler,
32 * such as in the case of C89 compilers, like MSVC
34 #include "fallback-c89.c"
37 _gtk_border_image_init (GtkBorderImage *image,
38 GtkThemingEngine *engine)
42 image->source = _gtk_css_value_get_object (_gtk_theming_engine_peek_property (engine, "border-image-source"));
43 if (image->source == NULL)
46 image->slice = *(GtkBorder *) _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, "border-image-slice"));
47 width = _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, "border-image-width"));
50 image->width = *width;
51 image->has_width = TRUE;
54 image->has_width = FALSE;
56 image->repeat = *_gtk_css_value_get_border_image_repeat (_gtk_theming_engine_peek_property (engine, "border-image-repeat"));
61 typedef struct _GtkBorderImageSliceSize GtkBorderImageSliceSize;
62 struct _GtkBorderImageSliceSize {
68 gtk_border_image_compute_border_size (GtkBorderImageSliceSize sizes[3],
74 /* This code assumes area_size >= start_border + end_border */
76 sizes[0].offset = offset;
77 sizes[0].size = start_border;
78 sizes[1].offset = offset + start_border;
79 sizes[1].size = area_size - start_border - end_border;
80 sizes[2].offset = offset + area_size - end_border;
81 sizes[2].size = end_border;
85 gtk_border_image_render_slice (cairo_t *cr,
86 cairo_surface_t *slice,
93 GtkCssBorderRepeatStyle hrepeat,
94 GtkCssBorderRepeatStyle vrepeat)
96 double hscale, vscale;
98 cairo_extend_t extend = CAIRO_EXTEND_PAD;
99 cairo_matrix_t matrix;
100 cairo_pattern_t *pattern;
102 /* We can't draw center tiles yet */
103 g_assert (hrepeat == GTK_CSS_REPEAT_STYLE_STRETCH || vrepeat == GTK_CSS_REPEAT_STYLE_STRETCH);
105 hscale = width / slice_width;
106 vscale = height / slice_height;
112 case GTK_CSS_REPEAT_STYLE_REPEAT:
113 extend = CAIRO_EXTEND_REPEAT;
116 case GTK_CSS_REPEAT_STYLE_SPACE:
120 extend = CAIRO_EXTEND_NONE;
123 xstep = hscale * slice_width;
124 n = floor (width / xstep);
125 space = (width - n * xstep) / (n + 1);
131 case GTK_CSS_REPEAT_STYLE_STRETCH:
133 case GTK_CSS_REPEAT_STYLE_ROUND:
134 extend = CAIRO_EXTEND_REPEAT;
135 hscale = width / (slice_width * MAX (round (width / (slice_width * vscale)), 1));
138 g_assert_not_reached ();
144 case GTK_CSS_REPEAT_STYLE_REPEAT:
145 extend = CAIRO_EXTEND_REPEAT;
148 case GTK_CSS_REPEAT_STYLE_SPACE:
152 extend = CAIRO_EXTEND_NONE;
155 ystep = vscale * slice_height;
156 n = floor (height / ystep);
157 space = (height - n * ystep) / (n + 1);
163 case GTK_CSS_REPEAT_STYLE_STRETCH:
165 case GTK_CSS_REPEAT_STYLE_ROUND:
166 extend = CAIRO_EXTEND_REPEAT;
167 vscale = height / (slice_height * MAX (round (height / (slice_height * hscale)), 1));
170 g_assert_not_reached ();
174 pattern = cairo_pattern_create_for_surface (slice);
176 cairo_matrix_init_translate (&matrix,
177 hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_width / 2 : 0,
178 vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_height / 2 : 0);
179 cairo_matrix_scale (&matrix, 1 / hscale, 1 / vscale);
180 cairo_matrix_translate (&matrix,
181 hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - width / 2 : 0,
182 vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - height / 2 : 0);
184 cairo_pattern_set_matrix (pattern, &matrix);
185 cairo_pattern_set_extend (pattern, extend);
188 cairo_translate (cr, x, y);
190 for (y = 0; y < height; y += ystep)
192 for (x = 0; x < width; x += xstep)
195 cairo_translate (cr, x, y);
196 cairo_set_source (cr, pattern);
197 cairo_rectangle (cr, 0, 0, xstep, ystep);
205 cairo_pattern_destroy (pattern);
209 gtk_border_image_compute_slice_size (GtkBorderImageSliceSize sizes[3],
214 sizes[0].size = MIN (start_size, surface_size);
217 sizes[2].size = MIN (end_size, surface_size);
218 sizes[2].offset = surface_size - sizes[2].size;
220 sizes[1].size = MAX (0, surface_size - sizes[0].size - sizes[2].size);
221 sizes[1].offset = sizes[0].size;
225 _gtk_border_image_render (GtkBorderImage *image,
226 GtkBorder *border_width,
233 cairo_surface_t *surface, *slice;
234 GtkBorderImageSliceSize vertical_slice[3], horizontal_slice[3];
235 GtkBorderImageSliceSize vertical_border[3], horizontal_border[3];
236 double source_width, source_height;
239 if (image->has_width)
240 border_width = &image->width;
242 _gtk_css_image_get_concrete_size (image->source,
245 &source_width, &source_height);
247 /* XXX: Optimize for (source_width == width && source_height == height) */
249 surface = _gtk_css_image_get_surface (image->source,
250 cairo_get_target (cr),
251 source_width, source_height);
253 gtk_border_image_compute_slice_size (horizontal_slice,
257 gtk_border_image_compute_slice_size (vertical_slice,
260 image->slice.bottom);
261 gtk_border_image_compute_border_size (horizontal_border,
265 border_width->right);
266 gtk_border_image_compute_border_size (vertical_border,
270 border_width->bottom);
272 for (v = 0; v < 3; v++)
274 if (vertical_slice[v].size == 0 ||
275 vertical_border[v].size == 0)
278 for (h = 0; h < 3; h++)
280 if (horizontal_slice[h].size == 0 ||
281 horizontal_border[h].size == 0)
284 if (h == 1 && v == 1)
287 slice = cairo_surface_create_for_rectangle (surface,
288 horizontal_slice[h].offset,
289 vertical_slice[v].offset,
290 horizontal_slice[h].size,
291 vertical_slice[v].size);
293 gtk_border_image_render_slice (cr,
295 horizontal_slice[h].size,
296 vertical_slice[v].size,
297 horizontal_border[h].offset,
298 vertical_border[v].offset,
299 horizontal_border[h].size,
300 vertical_border[v].size,
301 h == 1 ? image->repeat.hrepeat : GTK_CSS_REPEAT_STYLE_STRETCH,
302 v == 1 ? image->repeat.vrepeat : GTK_CSS_REPEAT_STYLE_STRETCH);
304 cairo_surface_destroy (slice);
308 cairo_surface_destroy (surface);