1
/*
2
 * Copyright © 2021 Matthias Clasen
3
 * Copyright © 2021 Uli Schlachter
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software
6
 * and its documentation for any purpose is hereby granted without
7
 * fee, provided that the above copyright notice appear in all copies
8
 * and that both that copyright notice and this permission notice
9
 * appear in supporting documentation, and that the name of
10
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
11
 * distribution of the software without specific, written prior
12
 * permission. Red Hat, Inc. makes no representations about the
13
 * suitability of this software for any purpose.  It is provided "as
14
 * is" without express or implied warranty.
15
 *
16
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
19
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
 *
24
 * Authors:
25
 *	Matthias Clasen
26
 *	Uli Schlachter
27
 */
28

            
29
// Test case for https://gitlab.freedesktop.org/cairo/cairo/-/merge_requests/118
30
// A recording surface with a non-zero origin gets cut off when passed to
31
// cairo_surface_write_to_png().
32

            
33
#include "cairo-test.h"
34

            
35
#include <assert.h>
36

            
37
struct buffer {
38
    char *data;
39
    size_t length;
40
};
41

            
42
static cairo_surface_t *
43
3
prepare_recording (void)
44
{
45
    cairo_surface_t *surface;
46
    cairo_rectangle_t rect;
47
    cairo_t *cr;
48

            
49
3
    rect.x = -1;
50
3
    rect.y = -2;
51
3
    rect.width = 3;
52
3
    rect.height = 4;
53
3
    surface = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, &rect);
54

            
55
3
    cr = cairo_create (surface);
56
3
    cairo_set_line_width (cr, 1);
57
3
    cairo_set_source_rgb (cr, 1, 1, 1);
58
3
    cairo_rectangle (cr, 0.5, -0.5, 1., 1.);
59
3
    cairo_stroke (cr);
60
3
    cairo_destroy (cr);
61

            
62
3
    return surface;
63
}
64

            
65
static cairo_status_t
66
36
write_callback (void *closure, const unsigned char *data, unsigned int length)
67
{
68
36
    struct buffer *buffer = closure;
69

            
70
36
    buffer->data = realloc (buffer->data, buffer->length + length);
71
36
    memcpy (&buffer->data[buffer->length], data, length);
72
36
    buffer->length += length;
73

            
74
36
    return CAIRO_STATUS_SUCCESS;
75
}
76

            
77
static cairo_status_t
78
36
read_callback (void *closure, unsigned char *data, unsigned int length)
79
{
80
36
    struct buffer *buffer = closure;
81

            
82
36
    assert (buffer->length >= length);
83
36
    memcpy (data, buffer->data, length);
84
36
    buffer->data += length;
85
36
    buffer->length -= length;
86

            
87
36
    return CAIRO_STATUS_SUCCESS;
88
}
89

            
90
static cairo_surface_t *
91
3
png_round_trip (cairo_surface_t *input_surface)
92
{
93
    cairo_surface_t *output_surface;
94
    struct buffer buffer;
95
    void *to_free;
96

            
97
    // Turn the surface into a PNG
98
3
    buffer.data = NULL;
99
3
    buffer.length = 0;
100
3
    cairo_surface_write_to_png_stream (input_surface, write_callback, &buffer);
101
3
    to_free = buffer.data;
102

            
103
    // Load the PNG again
104
3
    output_surface = cairo_image_surface_create_from_png_stream (read_callback, &buffer);
105

            
106
3
    free (to_free);
107
3
    return output_surface;
108
}
109

            
110
static cairo_test_status_t
111
3
draw (cairo_t *cr, int width, int height)
112
{
113
    cairo_surface_t *recording, *surface;
114

            
115
    // Draw a black background so that the output does not vary with alpha
116
3
    cairo_set_source_rgb (cr, 0, 0, 0);
117
3
    cairo_paint (cr);
118

            
119
3
    recording = prepare_recording ();
120
3
    surface = png_round_trip (recording);
121

            
122
3
    cairo_set_source_surface (cr, surface, 0, 0);
123
3
    cairo_paint (cr);
124

            
125
3
    cairo_surface_destroy (recording);
126
3
    cairo_surface_destroy (surface);
127

            
128
3
    return CAIRO_TEST_SUCCESS;
129
}
130

            
131
1
CAIRO_TEST (record_write_png,
132
	    "Test writing to png with non-zero origin",
133
	    "record, transform", /* keywords */
134
	    NULL, /* requirements */
135
	    4, 4,
136
	    NULL, draw)