1
/*
2
 * Copyright © 2026
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use, copy,
8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24

            
25
#include "cairo-test.h"
26

            
27
/* Verify that transforming recording-pattern bounds during analysis does not
28
 * produce wrapped fixed-point boxes with empty extents.
29
 */
30

            
31
static cairo_test_status_t
32
1
preamble (cairo_test_context_t *ctx)
33
{
34
1
    cairo_rectangle_t extents = { 0, 0, 1, 1 };
35
    cairo_surface_t *source_surface;
36
    cairo_surface_t *recording_surface;
37
    cairo_pattern_t *pattern;
38
    cairo_t *cr;
39
    cairo_matrix_t matrix;
40
    cairo_status_t status;
41
    double x, y, width, height;
42

            
43
1
    source_surface = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, &extents);
44
1
    cr = cairo_create (source_surface);
45
1
    cairo_rectangle (cr, 0, 0, 1, 1);
46
1
    cairo_fill (cr);
47
1
    status = cairo_status (cr);
48
1
    cairo_destroy (cr);
49
1
    if (status) {
50
	cairo_surface_destroy (source_surface);
51
	return cairo_test_status_from_status (ctx, status);
52
    }
53

            
54
1
    recording_surface = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
55
1
    cr = cairo_create (recording_surface);
56
1
    pattern = cairo_pattern_create_for_surface (source_surface);
57
1
    cairo_surface_destroy (source_surface);
58

            
59
    /* This invertible matrix causes transformed bbox coordinates to exceed
60
     * the fixed-point range and exercises clamping in
61
     * _cairo_matrix_transform_bounding_box_fixed().
62
     */
63
1
    cairo_matrix_init (&matrix, -70000.0, 50000.0, -100000.0,
64
		       100000.0, -100000000.0, -50000000.0);
65
1
    cairo_pattern_set_matrix (pattern, &matrix);
66
1
    status = cairo_pattern_status (pattern);
67
1
    if (!status) {
68
1
	cairo_set_source (cr, pattern);
69
1
	cairo_paint (cr);
70
1
	status = cairo_status (cr);
71
    }
72

            
73
1
    cairo_pattern_destroy (pattern);
74
1
    cairo_destroy (cr);
75
1
    if (status) {
76
	cairo_surface_destroy (recording_surface);
77
	return cairo_test_status_from_status (ctx, status);
78
    }
79

            
80
1
    cairo_recording_surface_ink_extents (recording_surface, &x, &y, &width, &height);
81
1
    status = cairo_surface_status (recording_surface);
82
1
    cairo_surface_destroy (recording_surface);
83
1
    if (status)
84
	return cairo_test_status_from_status (ctx, status);
85

            
86
1
    if (width <= 0 || height <= 0) {
87
	cairo_test_log (ctx,
88
			"ink extents should be non-empty, got (%g, %g, %g, %g)\n",
89
			x, y, width, height);
90
	return CAIRO_TEST_FAILURE;
91
    }
92

            
93
1
    return CAIRO_TEST_SUCCESS;
94
}
95

            
96
1
CAIRO_TEST (recording_ink_extents_overflow,
97
	    "Test cairo_recording_surface_ink_extents() with overflow-prone recording pattern matrix",
98
	    "recording,extents,transform", /* keywords */
99
	    NULL, /* requirements */
100
	    0, 0,
101
	    preamble, NULL)