- int surface_width, surface_height;
- cairo_surface_t *surface;
- cairo_t *cr2;
-
- /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
- * there is a second step. The UA must scale the image in that
- * dimension (or both dimensions) so that it fits a whole number of
- * times in the background positioning area. In the case of the width
- * (height is analogous):
- *
- * If X ≠ 0 is the width of the image after step one and W is the width
- * of the background positioning area, then the rounded width
- * X' = W / round(W / X) where round() is a function that returns the
- * nearest natural number (integer greater than zero).
- *
- * If ‘background-repeat’ is ‘round’ for one dimension only and if
- * ‘background-size’ is ‘auto’ for the other dimension, then there is
- * a third step: that other dimension is scaled so that the original
- * aspect ratio is restored.
- */
- if (hrepeat == GTK_CSS_BACKGROUND_ROUND)
- {
- double n = round (width / image_width);
-
- n = MAX (1, n);
-
- if (vrepeat != GTK_CSS_BACKGROUND_ROUND
- /* && vsize == auto (it is by default) */)
- image_height *= width / (image_width * n);
- image_width = width / n;
- }
- if (vrepeat == GTK_CSS_BACKGROUND_ROUND)
- {
- double n = round (height / image_height);
-
- n = MAX (1, n);
-
- if (hrepeat != GTK_CSS_BACKGROUND_ROUND
- /* && hsize == auto (it is by default) */)
- image_width *= height / (image_height * n);
- image_height = height / n;
- }
-
- /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
- * to store the extra space. */
- if (hrepeat == GTK_CSS_BACKGROUND_SPACE)
- {
- double n = floor (width / image_width);
- surface_width = n ? round (width / n) : 0;
- }
- else
- surface_width = round (image_width);
-
- if (vrepeat == GTK_CSS_BACKGROUND_SPACE)
- {
- double n = floor (height / image_height);
- surface_height = n ? round (height / n) : 0;
- }
- else
- surface_height = round (image_height);
-
- surface = cairo_surface_create_similar (cairo_get_target (cr),
- CAIRO_CONTENT_COLOR_ALPHA,
- surface_width, surface_height);
- cr2 = cairo_create (surface);
- cairo_translate (cr2,
- 0.5 * (surface_width - image_width),
- 0.5 * (surface_height - image_height));
- _gtk_css_image_draw (bg->image, cr2, image_width, image_height);
- cairo_destroy (cr2);
-
- cairo_set_source_surface (cr, surface,
- /* background-position goes here */
- 0, 0);
- cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
- cairo_surface_destroy (surface);
-
- cairo_rectangle (cr,
- 0, 0,
- hrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_width : width,
- vrepeat == GTK_CSS_BACKGROUND_NO_REPEAT ? image_height : height);
- cairo_fill (cr);