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 "gtkstylepropertiesprivate.h"
30 #include "gtkthemingengineprivate.h"
32 /* this is in case round() is not provided by the compiler,
33 * such as in the case of C89 compilers, like MSVC
35 #include "fallback-c89.c"
38 _gtk_border_image_init (GtkBorderImage *image,
39 GtkThemingEngine *engine)
43 image->source = _gtk_css_image_value_get_image (_gtk_theming_engine_peek_property (engine, "border-image-source"));
44 if (image->source == NULL)
47 image->slice = *(GtkBorder *) _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, "border-image-slice"));
48 width = _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, "border-image-width"));
51 image->width = *width;
52 image->has_width = TRUE;
55 image->has_width = FALSE;
57 image->repeat = *_gtk_css_value_get_border_image_repeat (_gtk_theming_engine_peek_property (engine, "border-image-repeat"));
62 typedef struct _GtkBorderImageSliceSize GtkBorderImageSliceSize;
63 struct _GtkBorderImageSliceSize {
69 gtk_border_image_compute_border_size (GtkBorderImageSliceSize sizes[3],
75 /* This code assumes area_size >= start_border + end_border */
77 sizes[0].offset = offset;
78 sizes[0].size = start_border;
79 sizes[1].offset = offset + start_border;
80 sizes[1].size = area_size - start_border - end_border;
81 sizes[2].offset = offset + area_size - end_border;
82 sizes[2].size = end_border;
86 gtk_border_image_render_slice (cairo_t *cr,
87 cairo_surface_t *slice,
94 GtkCssBorderRepeatStyle hrepeat,
95 GtkCssBorderRepeatStyle vrepeat)
97 double hscale, vscale;
99 cairo_extend_t extend = CAIRO_EXTEND_PAD;
100 cairo_matrix_t matrix;
101 cairo_pattern_t *pattern;
103 /* We can't draw center tiles yet */
104 g_assert (hrepeat == GTK_CSS_REPEAT_STYLE_STRETCH || vrepeat == GTK_CSS_REPEAT_STYLE_STRETCH);
106 hscale = width / slice_width;
107 vscale = height / slice_height;
113 case GTK_CSS_REPEAT_STYLE_REPEAT:
114 extend = CAIRO_EXTEND_REPEAT;
117 case GTK_CSS_REPEAT_STYLE_SPACE:
121 extend = CAIRO_EXTEND_NONE;
124 xstep = hscale * slice_width;
125 n = floor (width / xstep);
126 space = (width - n * xstep) / (n + 1);
132 case GTK_CSS_REPEAT_STYLE_STRETCH:
134 case GTK_CSS_REPEAT_STYLE_ROUND:
135 extend = CAIRO_EXTEND_REPEAT;
136 hscale = width / (slice_width * MAX (round (width / (slice_width * vscale)), 1));
139 g_assert_not_reached ();
145 case GTK_CSS_REPEAT_STYLE_REPEAT:
146 extend = CAIRO_EXTEND_REPEAT;
149 case GTK_CSS_REPEAT_STYLE_SPACE:
153 extend = CAIRO_EXTEND_NONE;
156 ystep = vscale * slice_height;
157 n = floor (height / ystep);
158 space = (height - n * ystep) / (n + 1);
164 case GTK_CSS_REPEAT_STYLE_STRETCH:
166 case GTK_CSS_REPEAT_STYLE_ROUND:
167 extend = CAIRO_EXTEND_REPEAT;
168 vscale = height / (slice_height * MAX (round (height / (slice_height * hscale)), 1));
171 g_assert_not_reached ();
175 pattern = cairo_pattern_create_for_surface (slice);
177 cairo_matrix_init_translate (&matrix,
178 hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_width / 2 : 0,
179 vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_height / 2 : 0);
180 cairo_matrix_scale (&matrix, 1 / hscale, 1 / vscale);
181 cairo_matrix_translate (&matrix,
182 hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - width / 2 : 0,
183 vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - height / 2 : 0);
185 cairo_pattern_set_matrix (pattern, &matrix);
186 cairo_pattern_set_extend (pattern, extend);
189 cairo_translate (cr, x, y);
191 for (y = 0; y < height; y += ystep)
193 for (x = 0; x < width; x += xstep)
196 cairo_translate (cr, x, y);
197 cairo_set_source (cr, pattern);
198 cairo_rectangle (cr, 0, 0, xstep, ystep);
206 cairo_pattern_destroy (pattern);
210 gtk_border_image_compute_slice_size (GtkBorderImageSliceSize sizes[3],
215 sizes[0].size = MIN (start_size, surface_size);
218 sizes[2].size = MIN (end_size, surface_size);
219 sizes[2].offset = surface_size - sizes[2].size;
221 sizes[1].size = MAX (0, surface_size - sizes[0].size - sizes[2].size);
222 sizes[1].offset = sizes[0].size;
226 _gtk_border_image_render (GtkBorderImage *image,
227 GtkBorder *border_width,
234 cairo_surface_t *surface, *slice;
235 GtkBorderImageSliceSize vertical_slice[3], horizontal_slice[3];
236 GtkBorderImageSliceSize vertical_border[3], horizontal_border[3];
237 double source_width, source_height;
240 if (image->has_width)
241 border_width = &image->width;
243 _gtk_css_image_get_concrete_size (image->source,
246 &source_width, &source_height);
248 /* XXX: Optimize for (source_width == width && source_height == height) */
250 surface = _gtk_css_image_get_surface (image->source,
251 cairo_get_target (cr),
252 source_width, source_height);
254 gtk_border_image_compute_slice_size (horizontal_slice,
258 gtk_border_image_compute_slice_size (vertical_slice,
261 image->slice.bottom);
262 gtk_border_image_compute_border_size (horizontal_border,
266 border_width->right);
267 gtk_border_image_compute_border_size (vertical_border,
271 border_width->bottom);
273 for (v = 0; v < 3; v++)
275 if (vertical_slice[v].size == 0 ||
276 vertical_border[v].size == 0)
279 for (h = 0; h < 3; h++)
281 if (horizontal_slice[h].size == 0 ||
282 horizontal_border[h].size == 0)
285 if (h == 1 && v == 1)
288 slice = cairo_surface_create_for_rectangle (surface,
289 horizontal_slice[h].offset,
290 vertical_slice[v].offset,
291 horizontal_slice[h].size,
292 vertical_slice[v].size);
294 gtk_border_image_render_slice (cr,
296 horizontal_slice[h].size,
297 vertical_slice[v].size,
298 horizontal_border[h].offset,
299 vertical_border[v].offset,
300 horizontal_border[h].size,
301 vertical_border[v].size,
302 h == 1 ? image->repeat.hrepeat : GTK_CSS_REPEAT_STYLE_STRETCH,
303 v == 1 ? image->repeat.vrepeat : GTK_CSS_REPEAT_STYLE_STRETCH);
305 cairo_surface_destroy (slice);
309 cairo_surface_destroy (surface);