| 1 |
import io |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import array |
| 5 |
import tempfile |
| 6 |
import struct |
| 7 |
import sysconfig |
| 8 |
import platform |
| 9 |
|
| 10 |
import cairo |
| 11 |
import pytest |
| 12 |
|
| 13 |
|
| 14 |
def test_context_manager(): |
| 15 |
with cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) as surface: |
| 16 |
surface.show_page() |
| 17 |
with pytest.raises(cairo.Error) as excinfo: |
| 18 |
surface.show_page() |
| 19 |
assert excinfo.value.status == cairo.Status.SURFACE_FINISHED |
| 20 |
|
| 21 |
|
| 22 |
def test_surface_cmp_hash(): |
| 23 |
main = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 24 |
ctx = cairo.Context(main) |
| 25 |
assert ctx.get_target() == main |
| 26 |
assert not ctx.get_target() != main |
| 27 |
assert main != object() |
| 28 |
assert hash(ctx.get_target()) == hash(main) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
mapped = main.map_to_image(None) |
| 33 |
with pytest.raises(TypeError): |
| 34 |
hash(mapped) |
| 35 |
assert mapped == mapped |
| 36 |
main.unmap_image(mapped) |
| 37 |
|
| 38 |
|
| 39 |
def test_surface_map_to_image(): |
| 40 |
main = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 41 |
image = main.map_to_image(None) |
| 42 |
|
| 43 |
with pytest.raises(TypeError): |
| 44 |
type(image)() |
| 45 |
|
| 46 |
other = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 47 |
with pytest.raises(ValueError): |
| 48 |
other.unmap_image(image) |
| 49 |
|
| 50 |
with pytest.raises(TypeError): |
| 51 |
main.unmap_image(other) |
| 52 |
|
| 53 |
with pytest.raises(RuntimeError): |
| 54 |
image.finish() |
| 55 |
|
| 56 |
main.unmap_image(image) |
| 57 |
|
| 58 |
with pytest.raises(RuntimeError): |
| 59 |
main.unmap_image(image) |
| 60 |
|
| 61 |
|
| 62 |
with pytest.raises(cairo.Error): |
| 63 |
cairo.Context(image) |
| 64 |
|
| 65 |
with pytest.raises(TypeError): |
| 66 |
main.map_to_image(object()) |
| 67 |
|
| 68 |
with pytest.raises(TypeError): |
| 69 |
main.map_to_image() |
| 70 |
|
| 71 |
gced = main.map_to_image(None) |
| 72 |
del gced |
| 73 |
|
| 74 |
|
| 75 |
def test_surface_map_to_image_context_manager(): |
| 76 |
main = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 77 |
with main.map_to_image(None) as image: |
| 78 |
pass |
| 79 |
|
| 80 |
with pytest.raises(RuntimeError): |
| 81 |
main.unmap_image(image) |
| 82 |
|
| 83 |
with pytest.raises(cairo.Error) as excinfo: |
| 84 |
image.show_page() |
| 85 |
assert excinfo.value.status == cairo.Status.SURFACE_FINISHED |
| 86 |
|
| 87 |
with pytest.raises(RuntimeError): |
| 88 |
with image: |
| 89 |
pass |
| 90 |
|
| 91 |
|
| 92 |
def test_surface_map_to_image_data(): |
| 93 |
main = cairo.ImageSurface(cairo.Format.RGB24, 2, 1) |
| 94 |
|
| 95 |
main.flush() |
| 96 |
assert bytes(main.get_data()) == b"\x00\x00\x00\x00\x00\x00\x00\x00" |
| 97 |
image = main.map_to_image(None) |
| 98 |
ctx = cairo.Context(image) |
| 99 |
ctx.set_source_rgb(1, 1, 1) |
| 100 |
ctx.paint() |
| 101 |
main.unmap_image(image) |
| 102 |
main.flush() |
| 103 |
assert bytes(main.get_data()) == b"\xff\xff\xff\xff\xff\xff\xff\xff" |
| 104 |
|
| 105 |
main = cairo.ImageSurface(cairo.Format.RGB24, 2, 1) |
| 106 |
image = main.map_to_image(cairo.RectangleInt(0, 0, 1, 1)) |
| 107 |
ctx = cairo.Context(image) |
| 108 |
ctx.set_source_rgb(1, 1, 1) |
| 109 |
ctx.paint() |
| 110 |
main.unmap_image(image) |
| 111 |
main.flush() |
| 112 |
assert bytes(main.get_data()) == b"\xff\xff\xff\xff\x00\x00\x00\x00" |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.skipif(not cairo.HAS_TEE_SURFACE, reason="no tee surface") |
| 116 |
def test_tee_surface(): |
| 117 |
main = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 118 |
tee = cairo.TeeSurface(main) |
| 119 |
assert isinstance(tee, cairo.TeeSurface) |
| 120 |
with pytest.raises(TypeError): |
| 121 |
cairo.TeeSurface(object()) |
| 122 |
with pytest.raises(TypeError): |
| 123 |
tee.add(object()) |
| 124 |
with pytest.raises(TypeError): |
| 125 |
tee.remove(object()) |
| 126 |
with pytest.raises(TypeError): |
| 127 |
tee.index(object()) |
| 128 |
|
| 129 |
|
| 130 |
s1 = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 131 |
tee.add(s1) |
| 132 |
assert tee.index(0) |
| 133 |
assert tee.index(1) |
| 134 |
tee.remove(s1) |
| 135 |
|
| 136 |
|
| 137 |
@pytest.mark.skipif(not hasattr(sys, "getrefcount"), reason="PyPy") |
| 138 |
def test_image_surface_get_data_refcount(): |
| 139 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 140 |
refcount = sys.getrefcount(surface) |
| 141 |
d = surface.get_data() |
| 142 |
assert sys.getrefcount(surface) != refcount |
| 143 |
del d |
| 144 |
assert sys.getrefcount(surface) == refcount |
| 145 |
|
| 146 |
|
| 147 |
def test_image_surface_get_data_crasher(): |
| 148 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 149 |
try: |
| 150 |
assert 0, surface.get_data() |
| 151 |
except Exception: |
| 152 |
pass |
| 153 |
|
| 154 |
|
| 155 |
def test_surface_get_content(): |
| 156 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 157 |
assert surface.get_content() == cairo.Content.COLOR_ALPHA |
| 158 |
assert isinstance(surface.get_content(), cairo.Content) |
| 159 |
|
| 160 |
|
| 161 |
def test_surface_get_format(): |
| 162 |
surface = cairo.ImageSurface(cairo.Format.ARGB32, 10, 10) |
| 163 |
assert surface.get_format() == cairo.Format.ARGB32 |
| 164 |
assert isinstance(surface.get_format(), cairo.Format) |
| 165 |
|
| 166 |
|
| 167 |
def test_pdf_get_error(): |
| 168 |
cairo.PDFSurface(io.BytesIO(), 10, 10) |
| 169 |
with pytest.raises(TypeError): |
| 170 |
cairo.PDFSurface(object(), 10, 10) |
| 171 |
with pytest.raises(TypeError): |
| 172 |
cairo.PDFSurface(io.StringIO(), 10, 10) |
| 173 |
|
| 174 |
|
| 175 |
def test_pdf_get_versions(): |
| 176 |
versions = cairo.PDFSurface.get_versions() |
| 177 |
assert isinstance(versions, list) |
| 178 |
assert all(isinstance(v, cairo.PDFVersion) for v in versions) |
| 179 |
|
| 180 |
|
| 181 |
def test_pdf_set_size(): |
| 182 |
fileobj = io.BytesIO() |
| 183 |
surface = cairo.PDFSurface(fileobj, 128, 128) |
| 184 |
surface.set_size(10, 10) |
| 185 |
with pytest.raises(TypeError): |
| 186 |
surface.set_size(10, object()) |
| 187 |
|
| 188 |
|
| 189 |
@pytest.mark.skipif(not hasattr(cairo.PDFSurface, "set_page_label"), |
| 190 |
reason="too old cairo") |
| 191 |
def test_pdf_set_page_label(): |
| 192 |
fileobj = io.BytesIO() |
| 193 |
with cairo.PDFSurface(fileobj, 128, 128) as surface: |
| 194 |
surface.set_page_label("foo") |
| 195 |
surface.set_page_label("bar") |
| 196 |
with pytest.raises(cairo.Error): |
| 197 |
surface.set_page_label("bar") |
| 198 |
|
| 199 |
|
| 200 |
@pytest.mark.skipif(not hasattr(cairo.PDFSurface, "set_metadata"), |
| 201 |
reason="too old cairo") |
| 202 |
def test_pdf_set_metadata(): |
| 203 |
fileobj = io.BytesIO() |
| 204 |
with cairo.PDFSurface(fileobj, 128, 128) as surface: |
| 205 |
surface.set_metadata(cairo.PDFMetadata.TITLE, "title") |
| 206 |
surface.set_metadata(cairo.PDFMetadata.TITLE, "title") |
| 207 |
surface.set_metadata(cairo.PDFMetadata.AUTHOR, "author") |
| 208 |
with pytest.raises(cairo.Error): |
| 209 |
surface.set_metadata(cairo.PDFMetadata.AUTHOR, "author") |
| 210 |
|
| 211 |
|
| 212 |
@pytest.mark.skipif(not hasattr(cairo.PDFSurface, "set_custom_metadata"), |
| 213 |
reason="too old cairo") |
| 214 |
def test_pdf_set_custom_metadata(): |
| 215 |
fileobj = io.BytesIO() |
| 216 |
with cairo.PDFSurface(fileobj, 128, 128) as surface: |
| 217 |
surface.set_custom_metadata("ISBN", "978-0123456789") |
| 218 |
with pytest.raises(cairo.Error): |
| 219 |
surface.set_custom_metadata("Author", "Author isn't allowed") |
| 220 |
|
| 221 |
|
| 222 |
@pytest.mark.skipif(not hasattr(cairo.PDFSurface, "add_outline"), |
| 223 |
reason="too old cairo") |
| 224 |
def test_pdf_add_outline(): |
| 225 |
fileobj = io.BytesIO() |
| 226 |
with cairo.PDFSurface(fileobj, 128, 128) as surface: |
| 227 |
res = surface.add_outline( |
| 228 |
cairo.PDF_OUTLINE_ROOT, "foo", "page=3 pos=[3.1 6.2]", |
| 229 |
cairo.PDFOutlineFlags.OPEN) |
| 230 |
assert isinstance(res, int) |
| 231 |
|
| 232 |
|
| 233 |
@pytest.mark.skipif(not hasattr(cairo.PDFSurface, "set_thumbnail_size"), |
| 234 |
reason="too old cairo") |
| 235 |
def test_pdf_set_thumbnail_size(): |
| 236 |
fileobj = io.BytesIO() |
| 237 |
with cairo.PDFSurface(fileobj, 128, 128) as surface: |
| 238 |
surface.set_thumbnail_size(10, 10) |
| 239 |
surface.set_thumbnail_size(0, 0) |
| 240 |
surface.set_thumbnail_size(1, 1) |
| 241 |
with pytest.raises(cairo.Error): |
| 242 |
surface.set_thumbnail_size(0, 0) |
| 243 |
|
| 244 |
|
| 245 |
@pytest.mark.skipif( |
| 246 |
sysconfig.get_platform().startswith("win"), reason="msvc fixme") |
| 247 |
def test_pdf_surface(): |
| 248 |
fd, fname = tempfile.mkstemp() |
| 249 |
os.close(fd) |
| 250 |
try: |
| 251 |
cairo.PDFSurface(fname, 10, 10).finish() |
| 252 |
finally: |
| 253 |
os.unlink(fname) |
| 254 |
|
| 255 |
with pytest.raises(TypeError): |
| 256 |
cairo.PDFSurface() |
| 257 |
|
| 258 |
with pytest.raises((ValueError, TypeError)): |
| 259 |
cairo.PDFSurface("\x00") |
| 260 |
|
| 261 |
with pytest.raises(TypeError): |
| 262 |
cairo.PDFSurface(object(), 100, 100) |
| 263 |
|
| 264 |
|
| 265 |
def test_svg_version_to_string(): |
| 266 |
ver = cairo.SVGSurface.version_to_string(cairo.SVG_VERSION_1_1) |
| 267 |
assert ver and isinstance(ver, str) |
| 268 |
with pytest.raises(ValueError): |
| 269 |
cairo.SVGSurface.version_to_string(-1) |
| 270 |
with pytest.raises(TypeError): |
| 271 |
cairo.SVGSurface.version_to_string(object()) |
| 272 |
|
| 273 |
|
| 274 |
@pytest.mark.skipif(not hasattr(cairo.SVGSurface, "get_document_unit"), |
| 275 |
reason="too old cairo") |
| 276 |
def test_svg_surface_get_document_unit(): |
| 277 |
with cairo.SVGSurface(None, 10, 10) as surface: |
| 278 |
|
| 279 |
assert surface.get_document_unit() in [cairo.SVGUnit.PT, cairo.SVGUnit.USER] |
| 280 |
|
| 281 |
with cairo.SVGSurface(None, 10, 10) as surface: |
| 282 |
surface.set_document_unit(cairo.SVGUnit.PX) |
| 283 |
assert surface.get_document_unit() == cairo.SVGUnit.PX |
| 284 |
with pytest.raises(cairo.Error): |
| 285 |
surface.set_document_unit(cairo.SVGUnit.PX) |
| 286 |
|
| 287 |
|
| 288 |
def test_svg_surface_restrict_to_version(): |
| 289 |
surface = cairo.SVGSurface(None, 10, 10) |
| 290 |
surface.restrict_to_version(cairo.SVG_VERSION_1_1) |
| 291 |
surface.finish() |
| 292 |
with pytest.raises(cairo.Error): |
| 293 |
surface.restrict_to_version(cairo.SVG_VERSION_1_2) |
| 294 |
with pytest.raises(TypeError): |
| 295 |
surface.restrict_to_version(object()) |
| 296 |
|
| 297 |
|
| 298 |
@pytest.mark.skipif( |
| 299 |
sysconfig.get_platform().startswith("win"), reason="msvc fixme") |
| 300 |
def test_pdf_surface_restrict_to_version(): |
| 301 |
surface = cairo.PDFSurface(None, 10, 10) |
| 302 |
surface.restrict_to_version(cairo.PDF_VERSION_1_4) |
| 303 |
surface.finish() |
| 304 |
with pytest.raises(cairo.Error): |
| 305 |
surface.restrict_to_version(cairo.PDF_VERSION_1_5) |
| 306 |
with pytest.raises(TypeError): |
| 307 |
surface.restrict_to_version(object()) |
| 308 |
|
| 309 |
|
| 310 |
def test_pdf_version_to_string(): |
| 311 |
ver = cairo.PDFSurface.version_to_string(cairo.PDF_VERSION_1_4) |
| 312 |
assert ver and isinstance(ver, str) |
| 313 |
with pytest.raises(ValueError): |
| 314 |
cairo.PDFSurface.version_to_string(-1) |
| 315 |
with pytest.raises(TypeError): |
| 316 |
cairo.PDFSurface.version_to_string(object()) |
| 317 |
|
| 318 |
|
| 319 |
def test_ps_surface_error(): |
| 320 |
cairo.PSSurface(io.BytesIO(), 10, 10) |
| 321 |
with pytest.raises(TypeError): |
| 322 |
cairo.PSSurface(object(), 10, 10) |
| 323 |
with pytest.raises(TypeError): |
| 324 |
cairo.PSSurface(io.StringIO(), 10, 10) |
| 325 |
|
| 326 |
|
| 327 |
def test_ps_surface_misc(): |
| 328 |
surface = cairo.PSSurface(None, 10, 10) |
| 329 |
surface.dsc_begin_page_setup() |
| 330 |
surface.dsc_begin_setup() |
| 331 |
|
| 332 |
|
| 333 |
def test_ps_surface_dsc_comment(): |
| 334 |
surface = cairo.PSSurface(None, 10, 10) |
| 335 |
surface.dsc_comment("%%Title: My excellent document") |
| 336 |
with pytest.raises(cairo.Error): |
| 337 |
surface.dsc_comment("") |
| 338 |
with pytest.raises(TypeError): |
| 339 |
surface.dsc_comment(object()) |
| 340 |
|
| 341 |
|
| 342 |
def test_ps_get_eps(): |
| 343 |
surface = cairo.PSSurface(None, 10, 10) |
| 344 |
assert isinstance(surface.get_eps(), bool) |
| 345 |
surface.set_eps(True) |
| 346 |
assert surface.get_eps() |
| 347 |
with pytest.raises(TypeError): |
| 348 |
surface.set_eps(object()) |
| 349 |
|
| 350 |
|
| 351 |
def test_ps_set_size(): |
| 352 |
surface = cairo.PSSurface(None, 10, 10) |
| 353 |
surface.set_size(10, 10) |
| 354 |
with pytest.raises(TypeError): |
| 355 |
surface.set_size(10, object()) |
| 356 |
|
| 357 |
|
| 358 |
def test_ps_restrict_to_level(): |
| 359 |
surface = cairo.PSSurface(None, 10, 10) |
| 360 |
surface.restrict_to_level(cairo.PSLevel.LEVEL_2) |
| 361 |
with pytest.raises(TypeError): |
| 362 |
surface.restrict_to_level(object()) |
| 363 |
|
| 364 |
|
| 365 |
@pytest.mark.skipif( |
| 366 |
sysconfig.get_platform().startswith("win"), reason="msvc fixme") |
| 367 |
def test_ps_surface_level_to_string(): |
| 368 |
level_id = cairo.PSSurface.level_to_string(cairo.PS_LEVEL_2) |
| 369 |
assert isinstance(level_id, str) |
| 370 |
assert cairo.PSSurface.ps_level_to_string(cairo.PS_LEVEL_2) == level_id |
| 371 |
with pytest.raises(ValueError): |
| 372 |
cairo.PSSurface.level_to_string(-1) |
| 373 |
with pytest.raises(TypeError): |
| 374 |
cairo.PSSurface.level_to_string(object()) |
| 375 |
|
| 376 |
|
| 377 |
def test_ps_surface_get_levels(): |
| 378 |
levels = cairo.PSSurface.get_levels() |
| 379 |
assert isinstance(levels, list) |
| 380 |
assert all(isinstance(v, cairo.PSLevel) for v in levels) |
| 381 |
|
| 382 |
|
| 383 |
def test_ps_surface(): |
| 384 |
assert isinstance(cairo.PSSurface(None, 10, 10), cairo.PSSurface) |
| 385 |
|
| 386 |
fd, fname = tempfile.mkstemp() |
| 387 |
os.close(fd) |
| 388 |
try: |
| 389 |
cairo.PSSurface(fname, 10, 10).finish() |
| 390 |
finally: |
| 391 |
os.unlink(fname) |
| 392 |
|
| 393 |
with pytest.raises(TypeError): |
| 394 |
cairo.PSSurface() |
| 395 |
|
| 396 |
with pytest.raises((ValueError, TypeError)): |
| 397 |
cairo.PSSurface("\x00", 100, 100) |
| 398 |
|
| 399 |
with pytest.raises(TypeError): |
| 400 |
cairo.PSSurface(object(), 100, 100) |
| 401 |
|
| 402 |
|
| 403 |
def test_scg_surface(): |
| 404 |
fd, fname = tempfile.mkstemp() |
| 405 |
os.close(fd) |
| 406 |
try: |
| 407 |
cairo.SVGSurface(fname, 10, 10).finish() |
| 408 |
finally: |
| 409 |
os.unlink(fname) |
| 410 |
|
| 411 |
with pytest.raises(TypeError): |
| 412 |
cairo.SVGSurface() |
| 413 |
|
| 414 |
with pytest.raises((ValueError, TypeError)): |
| 415 |
cairo.SVGSurface("\x00", 10, 10) |
| 416 |
|
| 417 |
with pytest.raises(TypeError): |
| 418 |
cairo.SVGSurface(object(), 100, 100) |
| 419 |
|
| 420 |
|
| 421 |
def test_svg_surface_get_versions(): |
| 422 |
versions = cairo.SVGSurface.get_versions() |
| 423 |
assert isinstance(versions, list) |
| 424 |
assert all(isinstance(v, cairo.SVGVersion) for v in versions) |
| 425 |
|
| 426 |
|
| 427 |
def test_surface_get_device_scale(): |
| 428 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 429 |
device_scale = surface.get_device_scale() |
| 430 |
assert all(isinstance(s, float) for s in device_scale) |
| 431 |
|
| 432 |
|
| 433 |
def test_surface_set_device_scale(): |
| 434 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 435 |
ret = surface.set_device_scale(5.0, 3.0) |
| 436 |
assert ret is None |
| 437 |
|
| 438 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100) |
| 439 |
with pytest.raises(cairo.Error): |
| 440 |
surface.set_device_scale(1, 0) |
| 441 |
|
| 442 |
with pytest.raises(TypeError): |
| 443 |
surface.set_device_scale(1, object()) |
| 444 |
|
| 445 |
|
| 446 |
def test_surface_create_for_rectangle(): |
| 447 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100) |
| 448 |
new = surface.create_for_rectangle(0, 0, 10, 10) |
| 449 |
assert new |
| 450 |
assert isinstance(new, cairo.Surface) |
| 451 |
|
| 452 |
with pytest.raises(cairo.Error) as excinfo: |
| 453 |
surface.create_for_rectangle(0, 0, 10, -1) |
| 454 |
assert excinfo.value.status == cairo.STATUS_INVALID_SIZE |
| 455 |
|
| 456 |
with pytest.raises(TypeError): |
| 457 |
surface.create_for_rectangle(0, 0, 10, object()) |
| 458 |
|
| 459 |
|
| 460 |
def test_surface_create_similar_image(): |
| 461 |
surface = cairo.PDFSurface(None, 1, 1) |
| 462 |
image = surface.create_similar_image(cairo.FORMAT_ARGB32, 24, 42) |
| 463 |
assert image |
| 464 |
assert isinstance(image, cairo.ImageSurface) |
| 465 |
del surface |
| 466 |
assert image.get_width() == 24 |
| 467 |
assert image.get_height() == 42 |
| 468 |
surface = cairo.PDFSurface(None, 1, 1) |
| 469 |
with pytest.raises(TypeError): |
| 470 |
surface.create_similar_image(cairo.FORMAT_ARGB32, 24, object()) |
| 471 |
|
| 472 |
|
| 473 |
def test_surface_get_set_mime_data(): |
| 474 |
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1) |
| 475 |
assert surface.get_mime_data("foo") is None |
| 476 |
assert surface.get_mime_data(cairo.MIME_TYPE_JPEG) is None |
| 477 |
|
| 478 |
surface.set_mime_data("foo", b"bar") |
| 479 |
assert surface.get_mime_data("foo") == b"bar" |
| 480 |
surface.set_mime_data("foo", None) |
| 481 |
assert surface.get_mime_data("foo") is None |
| 482 |
|
| 483 |
surface.set_mime_data(cairo.MIME_TYPE_JPEG, b"\x00quux\x00") |
| 484 |
assert surface.get_mime_data(cairo.MIME_TYPE_JPEG)[:] == b"\x00quux\x00" |
| 485 |
surface.set_mime_data(cairo.MIME_TYPE_JPEG, None) |
| 486 |
assert surface.get_mime_data(cairo.MIME_TYPE_JPEG) is None |
| 487 |
with pytest.raises(TypeError): |
| 488 |
surface.set_mime_data(cairo.MIME_TYPE_JPEG, object()) |
| 489 |
with pytest.raises(TypeError): |
| 490 |
surface.get_mime_data(object()) |
| 491 |
|
| 492 |
|
| 493 |
def test_supports_mime_type(): |
| 494 |
surface = cairo.PDFSurface(None, 3, 3) |
| 495 |
assert surface.supports_mime_type(cairo.MIME_TYPE_JPEG) |
| 496 |
assert not surface.supports_mime_type("nope") |
| 497 |
with pytest.raises(TypeError): |
| 498 |
surface.supports_mime_type(object()) |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy") |
| 503 |
def test_image_surface_create_for_data_array(): |
| 504 |
width, height = 255, 255 |
| 505 |
data = array.array('B', [0] * width * height * 4) |
| 506 |
|
| 507 |
for y in range(height): |
| 508 |
for x in range(width): |
| 509 |
offset = (x + (y * width)) * 4 |
| 510 |
alpha = y |
| 511 |
|
| 512 |
b = int(x * alpha / 255.0) |
| 513 |
g = int(y * alpha / 255.0) |
| 514 |
|
| 515 |
data[offset:offset + 4] = array.array( |
| 516 |
"B", struct.pack("=I", b | g << 8 | alpha << 24)) |
| 517 |
|
| 518 |
surface = cairo.ImageSurface.create_for_data( |
| 519 |
data, cairo.FORMAT_ARGB32, width, height) |
| 520 |
|
| 521 |
|
| 522 |
fd, filename = tempfile.mkstemp(prefix='pycairo_', suffix='.png') |
| 523 |
os.close(fd) |
| 524 |
surface.write_to_png(filename) |
| 525 |
os.unlink(filename) |
| 526 |
|
| 527 |
|
| 528 |
def test_image_surface_write_to_png_filename_and_obj_compare(): |
| 529 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 128, 128) |
| 530 |
fd, filename = tempfile.mkstemp(prefix='pycairo_', suffix='.png') |
| 531 |
os.close(fd) |
| 532 |
surface.write_to_png(filename) |
| 533 |
|
| 534 |
fileobj = io.BytesIO() |
| 535 |
surface.write_to_png(fileobj) |
| 536 |
|
| 537 |
with open(filename, "rb") as h: |
| 538 |
assert h.read() == fileobj.getvalue() |
| 539 |
|
| 540 |
os.unlink(filename) |
| 541 |
|
| 542 |
|
| 543 |
def test_image_surface_png_obj_roundtrip(): |
| 544 |
fileobj = io.BytesIO() |
| 545 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 128, 128) |
| 546 |
surface.write_to_png(fileobj) |
| 547 |
fileobj.seek(0) |
| 548 |
new_surface = cairo.ImageSurface.create_from_png(fileobj) |
| 549 |
assert surface.get_data() == new_surface.get_data() |
| 550 |
with pytest.raises(TypeError): |
| 551 |
cairo.ImageSurface.create_from_png() |
| 552 |
with pytest.raises((ValueError, TypeError)): |
| 553 |
cairo.ImageSurface.create_from_png("\x00") |
| 554 |
with pytest.raises(TypeError): |
| 555 |
cairo.ImageSurface.create_from_png(object()) |
| 556 |
with pytest.raises(TypeError): |
| 557 |
cairo.ImageSurface.create_from_png(io.StringIO()) |
| 558 |
|
| 559 |
|
| 560 |
def test_image_surface_png_file_roundtrip(): |
| 561 |
fd, filename = tempfile.mkstemp(prefix='pycairo_', suffix='.png') |
| 562 |
os.close(fd) |
| 563 |
|
| 564 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 128, 128) |
| 565 |
surface.write_to_png(filename) |
| 566 |
|
| 567 |
new_surface = cairo.ImageSurface.create_from_png(filename) |
| 568 |
assert surface.get_data() == new_surface.get_data() |
| 569 |
os.unlink(filename) |
| 570 |
|
| 571 |
|
| 572 |
def test_image_surface_write_to_png_error(): |
| 573 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 128, 128) |
| 574 |
with pytest.raises(TypeError): |
| 575 |
surface.write_to_png(42) |
| 576 |
with pytest.raises((ValueError, TypeError)): |
| 577 |
surface.write_to_png("\x00") |
| 578 |
|
| 579 |
|
| 580 |
def test_surface_from_stream_closed_before_finished(): |
| 581 |
for _ in [cairo.PDFSurface, cairo.PSSurface, cairo.SVGSurface]: |
| 582 |
fileobj = io.BytesIO() |
| 583 |
surface = cairo.PDFSurface(fileobj, 128, 128) |
| 584 |
fileobj.close() |
| 585 |
with pytest.raises(IOError): |
| 586 |
surface.finish() |
| 587 |
|
| 588 |
|
| 589 |
def test_script_surface(): |
| 590 |
f = io.BytesIO() |
| 591 |
dev = cairo.ScriptDevice(f) |
| 592 |
surface = cairo.ScriptSurface(dev, cairo.Content.COLOR_ALPHA, 42, 10) |
| 593 |
assert isinstance(surface, cairo.ScriptSurface) |
| 594 |
cairo.Context(surface).paint() |
| 595 |
dev.flush() |
| 596 |
assert b"42" in f.getvalue() |
| 597 |
assert b"paint" in f.getvalue() |
| 598 |
with pytest.raises(TypeError): |
| 599 |
cairo.ScriptSurface() |
| 600 |
|
| 601 |
|
| 602 |
def test_script_device_device_ref(): |
| 603 |
f = io.BytesIO() |
| 604 |
dev = cairo.ScriptDevice(f) |
| 605 |
surface = cairo.ScriptSurface(dev, cairo.Content.COLOR_ALPHA, 42, 10) |
| 606 |
del dev |
| 607 |
for _ in range(10): |
| 608 |
surface.get_device() |
| 609 |
|
| 610 |
|
| 611 |
def test_script_surface_create_for_target(): |
| 612 |
|
| 613 |
f = io.BytesIO() |
| 614 |
dev = cairo.ScriptDevice(f) |
| 615 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 616 |
script = cairo.ScriptSurface.create_for_target(dev, surface) |
| 617 |
with pytest.raises(TypeError): |
| 618 |
cairo.ScriptSurface.create_for_target(dev, object()) |
| 619 |
assert isinstance(script, cairo.ScriptSurface) |
| 620 |
ctx = cairo.Context(script) |
| 621 |
ctx.set_source_rgb(0.25, 0.5, 1.0) |
| 622 |
ctx.paint() |
| 623 |
assert b"paint" in f.getvalue() |
| 624 |
surface.flush() |
| 625 |
image_data = bytes(surface.get_data()) |
| 626 |
|
| 627 |
|
| 628 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 629 |
ctx = cairo.Context(surface) |
| 630 |
ctx.set_source_rgb(0.25, 0.5, 1.0) |
| 631 |
ctx.paint() |
| 632 |
surface.flush() |
| 633 |
assert bytes(surface.get_data()) == image_data |
| 634 |
|
| 635 |
|
| 636 |
def test_misc(): |
| 637 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 638 |
surface.copy_page() |
| 639 |
surface.mark_dirty() |
| 640 |
surface.show_page() |
| 641 |
|
| 642 |
|
| 643 |
@pytest.fixture |
| 644 |
def surface(): |
| 645 |
return cairo.ImageSurface(cairo.Format.ARGB32, 10, 10) |
| 646 |
|
| 647 |
|
| 648 |
@pytest.fixture |
| 649 |
def image_surface(): |
| 650 |
return cairo.ImageSurface(cairo.Format.ARGB32, 10, 10) |
| 651 |
|
| 652 |
|
| 653 |
def test_create_similar(surface): |
| 654 |
similar = surface.create_similar(cairo.Content.COLOR, 10, 10) |
| 655 |
assert isinstance(similar, cairo.Surface) |
| 656 |
with pytest.raises(TypeError): |
| 657 |
surface.create_similar() |
| 658 |
|
| 659 |
|
| 660 |
def test_get_device_offset(surface): |
| 661 |
surface.set_device_offset(1, 1) |
| 662 |
assert surface.get_device_offset() == (1, 1) |
| 663 |
with pytest.raises(TypeError): |
| 664 |
surface.set_device_offset(1, object()) |
| 665 |
|
| 666 |
|
| 667 |
def test_get_fallback_resolution(surface): |
| 668 |
surface.set_fallback_resolution(42, 42) |
| 669 |
assert surface.get_fallback_resolution() == (42, 42) |
| 670 |
with pytest.raises(TypeError): |
| 671 |
surface.set_fallback_resolution(42, object()) |
| 672 |
|
| 673 |
|
| 674 |
def test_mark_dirty_rectangle(surface): |
| 675 |
surface.mark_dirty_rectangle(0, 0, 10, 10) |
| 676 |
with pytest.raises(TypeError): |
| 677 |
surface.mark_dirty_rectangle(0, 0, 10, object()) |
| 678 |
|
| 679 |
|
| 680 |
def test_write_to_png(image_surface): |
| 681 |
with pytest.raises(TypeError): |
| 682 |
image_surface.write_to_png() |
| 683 |
|
| 684 |
with pytest.raises(TypeError): |
| 685 |
image_surface.write_to_png(io.StringIO()) |
| 686 |
|
| 687 |
with pytest.raises((ValueError, TypeError)) as excinfo: |
| 688 |
image_surface.write_to_png("\x00") |
| 689 |
excinfo.match(r'.* (null|NUL) .*') |
| 690 |
|
| 691 |
with pytest.raises(TypeError): |
| 692 |
image_surface.write_to_png(object()) |
| 693 |
|
| 694 |
|
| 695 |
def test_image_surface(): |
| 696 |
with pytest.raises(TypeError): |
| 697 |
cairo.ImageSurface(cairo.FORMAT_ARGB32, 3, object()) |
| 698 |
|
| 699 |
|
| 700 |
def test_image_surface_create_for_data(): |
| 701 |
format_ = cairo.FORMAT_ARGB32 |
| 702 |
surface = cairo.ImageSurface(format_, 3, 3) |
| 703 |
ctx = cairo.Context(surface) |
| 704 |
ctx.paint() |
| 705 |
surface.flush() |
| 706 |
buf = surface.get_data() |
| 707 |
|
| 708 |
new = cairo.ImageSurface.create_for_data(buf, format_, 3, 3) |
| 709 |
assert new.get_data() == buf |
| 710 |
|
| 711 |
with pytest.raises(ValueError): |
| 712 |
cairo.ImageSurface.create_for_data(buf, format_, 3, -1) |
| 713 |
with pytest.raises(ValueError): |
| 714 |
cairo.ImageSurface.create_for_data(buf, format_, -1, 3) |
| 715 |
|
| 716 |
with pytest.raises(ValueError): |
| 717 |
cairo.ImageSurface.create_for_data(buf, format_, 0, 0, -1) |
| 718 |
|
| 719 |
with pytest.raises(cairo.Error) as excinfo: |
| 720 |
cairo.ImageSurface.create_for_data(buf, format_, 3, 3, 3) |
| 721 |
|
| 722 |
assert excinfo.value.status == cairo.STATUS_INVALID_STRIDE |
| 723 |
|
| 724 |
with pytest.raises(TypeError): |
| 725 |
cairo.ImageSurface.create_for_data(buf, format_, 3, object()) |
| 726 |
|
| 727 |
|
| 728 |
def test_image_surface_stride_for_width(): |
| 729 |
v = cairo.ImageSurface.format_stride_for_width(cairo.Format.ARGB32, 10) |
| 730 |
assert v == 40 |
| 731 |
|
| 732 |
with pytest.raises(TypeError): |
| 733 |
cairo.ImageSurface.format_stride_for_width( |
| 734 |
cairo.Format.ARGB32, object()) |
| 735 |
|
| 736 |
|
| 737 |
def test_image_surface_get_stride(image_surface): |
| 738 |
assert image_surface.get_stride() == 40 |
| 739 |
|
| 740 |
|
| 741 |
def test_recording_surface(): |
| 742 |
with pytest.raises(TypeError): |
| 743 |
cairo.RecordingSurface(cairo.CONTENT_COLOR, object()) |
| 744 |
|
| 745 |
with pytest.raises(TypeError): |
| 746 |
cairo.RecordingSurface() |
| 747 |
|
| 748 |
surface = cairo.RecordingSurface(cairo.CONTENT_COLOR, None) |
| 749 |
assert surface.ink_extents() == (0.0, 0.0, 0.0, 0.0) |
| 750 |
|
| 751 |
|
| 752 |
@pytest.mark.skipif(not hasattr(cairo.Format, "RGB96F"), reason="too old cairo") |
| 753 |
def test_format_rgbf(): |
| 754 |
surface = cairo.ImageSurface(cairo.Format.RGB96F, 3, 3) |
| 755 |
assert surface.get_format() == cairo.Format.RGB96F |
| 756 |
surface = cairo.ImageSurface(cairo.Format.RGBA128F, 3, 3) |
| 757 |
assert surface.get_format() == cairo.Format.RGBA128F |