Skip to content

Commit 737bc6c

Browse files
Akulilazka
authored andcommitted
ImageSurface.create_for_data: support empty images
1 parent 81106ab commit 737bc6c

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

cairo/surface.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,12 +857,12 @@ image_surface_create_for_data (PyTypeObject *type, PyObject *args) {
857857

858858
format = (cairo_format_t)format_arg;
859859

860-
if (width <= 0) {
861-
PyErr_SetString(PyExc_ValueError, "width must be positive");
860+
if (width < 0) {
861+
PyErr_SetString(PyExc_ValueError, "width cannot be negative");
862862
return NULL;
863863
}
864-
if (height <= 0) {
865-
PyErr_SetString(PyExc_ValueError, "height must be positive");
864+
if (height < 0) {
865+
PyErr_SetString(PyExc_ValueError, "height cannot be negative");
866866
return NULL;
867867
}
868868
/* if stride is missing, calculate it from width */

tests/test_surface.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,6 @@ def test_image_surface_create_for_data() -> None:
726726
with pytest.raises(ValueError):
727727
cairo.ImageSurface.create_for_data(buf, format_, -1, 3)
728728

729-
with pytest.raises(ValueError):
730-
cairo.ImageSurface.create_for_data(buf, format_, 0, 0, -1)
731-
732729
with pytest.raises(cairo.Error) as excinfo:
733730
cairo.ImageSurface.create_for_data(buf, format_, 3, 3, 3)
734731

@@ -738,6 +735,16 @@ def test_image_surface_create_for_data() -> None:
738735
cairo.ImageSurface.create_for_data(buf, format_, 3, object()) # type: ignore
739736

740737

738+
@pytest.mark.parametrize("width, height", [(0, 0), (10, 0), (0, 10)])
739+
def test_image_surface_create_for_data_empty(width, height) -> None:
740+
surface = cairo.ImageSurface.create_for_data(
741+
bytearray(), cairo.FORMAT_ARGB32, width, height
742+
)
743+
assert surface.get_width() == width
744+
assert surface.get_height() == height
745+
assert surface.get_stride() == 4 * width
746+
747+
741748
def test_image_surface_get_data_finished() -> None:
742749
surface = cairo.ImageSurface(cairo.Format.ARGB32, 30, 30)
743750
surface.finish()

0 commit comments

Comments
 (0)