| 1 |
import cairo |
| 2 |
import pytest |
| 3 |
import ctypes |
| 4 |
import platform |
| 5 |
|
| 6 |
|
| 7 |
@pytest.fixture |
| 8 |
def context(): |
| 9 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 42, 42) |
| 10 |
return cairo.Context(surface) |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.skipif(not hasattr(cairo.Context, "tag_begin"), |
| 14 |
reason="too old cairo") |
| 15 |
def test_tags(context): |
| 16 |
context.tag_begin("foo", "bar=quux") |
| 17 |
context.tag_end("foo") |
| 18 |
|
| 19 |
|
| 20 |
def test_cmp_hash(context): |
| 21 |
other = cairo.Context(context.get_target()) |
| 22 |
assert context != other |
| 23 |
hash(context) |
| 24 |
|
| 25 |
|
| 26 |
def test_get_antialias(context): |
| 27 |
assert context.get_antialias() == cairo.Antialias.DEFAULT |
| 28 |
assert isinstance(context.get_antialias(), cairo.Antialias) |
| 29 |
|
| 30 |
|
| 31 |
def test_get_fill_rule(context): |
| 32 |
assert context.get_fill_rule() == cairo.FillRule.WINDING |
| 33 |
assert isinstance(context.get_fill_rule(), cairo.FillRule) |
| 34 |
|
| 35 |
|
| 36 |
def test_get_line_cap(context): |
| 37 |
assert context.get_line_cap() == cairo.LineCap.BUTT |
| 38 |
assert isinstance(context.get_line_cap(), cairo.LineCap) |
| 39 |
|
| 40 |
|
| 41 |
def test_get_line_join(context): |
| 42 |
assert context.get_line_join() == cairo.LineJoin.MITER |
| 43 |
assert isinstance(context.get_line_join(), cairo.LineJoin) |
| 44 |
|
| 45 |
|
| 46 |
def test_get_operator(context): |
| 47 |
assert context.get_operator() == cairo.Operator.OVER |
| 48 |
assert isinstance(context.get_operator(), cairo.Operator) |
| 49 |
|
| 50 |
|
| 51 |
def test_get_set_operator_limits(context): |
| 52 |
max_int = 2 ** (ctypes.sizeof(ctypes.c_int()) * 8 - 1) - 1 |
| 53 |
min_int = -max_int - 1 |
| 54 |
|
| 55 |
for val in [-1, 0, max_int, min_int]: |
| 56 |
context.set_operator(val) |
| 57 |
assert context.get_operator() == val |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy") |
| 62 |
def test_show_text_glyphs(): |
| 63 |
surface = cairo.PDFSurface(None, 300, 300) |
| 64 |
context = cairo.Context(surface) |
| 65 |
context.scale(300, 300) |
| 66 |
context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, |
| 67 |
cairo.FONT_WEIGHT_NORMAL) |
| 68 |
context.set_font_size(0.08) |
| 69 |
context.set_line_width(0.09) |
| 70 |
sf = context.get_scaled_font() |
| 71 |
glyphs, clusters, flags = sf.text_to_glyphs(0.5, 0.5, "foobar") |
| 72 |
context.show_text_glyphs("foobar", glyphs, clusters, flags) |
| 73 |
|
| 74 |
glyphs, clusters, flags = sf.text_to_glyphs(0.5, 0.5, "") |
| 75 |
context.show_text_glyphs("", glyphs, clusters, flags) |
| 76 |
|
| 77 |
with pytest.raises(TypeError): |
| 78 |
context.show_text_glyphs(object(), glyphs, clusters, flags) |
| 79 |
|
| 80 |
with pytest.raises(TypeError): |
| 81 |
context.show_text_glyphs("", [object()], clusters, flags) |
| 82 |
|
| 83 |
with pytest.raises(TypeError): |
| 84 |
context.show_text_glyphs("", object(), clusters, flags) |
| 85 |
|
| 86 |
with pytest.raises(TypeError): |
| 87 |
context.show_text_glyphs("", glyphs, [object()], flags) |
| 88 |
|
| 89 |
with pytest.raises(TypeError): |
| 90 |
context.show_text_glyphs("", glyphs, object(), flags) |
| 91 |
|
| 92 |
|
| 93 |
def test_append_path(context): |
| 94 |
context.line_to(1, 2) |
| 95 |
p = context.copy_path() |
| 96 |
context.new_path() |
| 97 |
context.append_path(p) |
| 98 |
assert str(context.copy_path()) == str(p) |
| 99 |
with pytest.raises(TypeError): |
| 100 |
context.append_path(object()) |
| 101 |
|
| 102 |
|
| 103 |
def test_arc(context): |
| 104 |
assert not list(context.copy_path()) |
| 105 |
context.arc(0, 0, 0, 0, 0) |
| 106 |
assert list(context.copy_path()) |
| 107 |
with pytest.raises(TypeError): |
| 108 |
context.arc(object()) |
| 109 |
|
| 110 |
|
| 111 |
def test_arc_negative(context): |
| 112 |
assert not list(context.copy_path()) |
| 113 |
context.arc_negative(0, 0, 0, 0, 0) |
| 114 |
assert list(context.copy_path()) |
| 115 |
with pytest.raises(TypeError): |
| 116 |
context.arc_negative(object()) |
| 117 |
|
| 118 |
|
| 119 |
def test_clip_extents(context): |
| 120 |
assert context.clip_extents() == (0.0, 0.0, 42.0, 42.0) |
| 121 |
|
| 122 |
|
| 123 |
def test_in_clip(): |
| 124 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100) |
| 125 |
context = cairo.Context(surface) |
| 126 |
assert context.in_clip(50, 50) |
| 127 |
context.clip() |
| 128 |
assert not context.in_clip(50, 50) |
| 129 |
context.reset_clip() |
| 130 |
assert context.in_clip(50, 50) |
| 131 |
|
| 132 |
with pytest.raises(TypeError): |
| 133 |
context.in_clip(None, None) |
| 134 |
|
| 135 |
|
| 136 |
def test_device_to_user(context): |
| 137 |
assert context.device_to_user(0, 0) == (0, 0) |
| 138 |
with pytest.raises(TypeError): |
| 139 |
context.device_to_user(None, None) |
| 140 |
|
| 141 |
|
| 142 |
def test_device_to_user_distance(context): |
| 143 |
assert context.device_to_user_distance(0, 0) == (0, 0) |
| 144 |
with pytest.raises(TypeError): |
| 145 |
context.device_to_user_distance(None, None) |
| 146 |
|
| 147 |
|
| 148 |
def test_fill_extents(context): |
| 149 |
context.line_to(1, 1) |
| 150 |
context.line_to(1, 0) |
| 151 |
context.line_to(0, 0) |
| 152 |
context.line_to(0, 1) |
| 153 |
context.line_to(1, 1) |
| 154 |
assert context.fill_extents() == (0, 0, 1, 1) |
| 155 |
|
| 156 |
|
| 157 |
def test_curve_to(context): |
| 158 |
with pytest.raises(TypeError): |
| 159 |
context.curve_to(1, 2, 3, 4, 5, object()) |
| 160 |
|
| 161 |
|
| 162 |
def test_set_get_dash(context): |
| 163 |
assert context.get_dash() == ((), 0) |
| 164 |
assert context.get_dash_count() == 0 |
| 165 |
|
| 166 |
context.set_dash([0, 1, 2, 3], 10) |
| 167 |
assert context.get_dash() == ((0.0, 1.0, 2.0, 3.0), 4.0) |
| 168 |
assert context.get_dash_count() == 4 |
| 169 |
|
| 170 |
context.set_dash([1], 1.25) |
| 171 |
assert context.get_dash() == ((1.0,), 1.25) |
| 172 |
|
| 173 |
with pytest.raises(TypeError): |
| 174 |
context.set_dash() |
| 175 |
|
| 176 |
with pytest.raises(TypeError): |
| 177 |
context.set_dash(1, 10) |
| 178 |
|
| 179 |
with pytest.raises(TypeError): |
| 180 |
context.set_dash([object()], 1) |
| 181 |
|
| 182 |
|
| 183 |
def test_glyph_extents(context): |
| 184 |
with pytest.raises(TypeError): |
| 185 |
context.glyph_extents(None) |
| 186 |
with pytest.raises(TypeError): |
| 187 |
context.glyph_extents() |
| 188 |
|
| 189 |
|
| 190 |
def test_glyph_path(context): |
| 191 |
with pytest.raises(TypeError): |
| 192 |
context.glyph_path(None) |
| 193 |
with pytest.raises(TypeError): |
| 194 |
context.glyph_path() |
| 195 |
|
| 196 |
|
| 197 |
def test_in_stroke(context): |
| 198 |
context.line_to(0, 0) |
| 199 |
context.line_to(1, 1) |
| 200 |
assert context.in_stroke(0, 0) |
| 201 |
assert not context.in_stroke(0, 2) |
| 202 |
with pytest.raises(TypeError): |
| 203 |
context.in_stroke(object(), 0) |
| 204 |
|
| 205 |
|
| 206 |
def test_current_point(context): |
| 207 |
assert not context.has_current_point() |
| 208 |
assert context.get_current_point() == (0, 0) |
| 209 |
context.move_to(10, 10) |
| 210 |
assert context.has_current_point() |
| 211 |
assert context.get_current_point() == (10, 10) |
| 212 |
|
| 213 |
|
| 214 |
def test_in_fill(context): |
| 215 |
assert not context.in_fill(0.1, 0.1) |
| 216 |
with pytest.raises(TypeError): |
| 217 |
context.in_fill(0.1, object()) |
| 218 |
|
| 219 |
|
| 220 |
def test_line_to(context): |
| 221 |
with pytest.raises(TypeError): |
| 222 |
context.line_to(0.1, object()) |
| 223 |
|
| 224 |
|
| 225 |
def test_mask(context): |
| 226 |
pattern = cairo.SolidPattern(0, 0, 0) |
| 227 |
context.mask(pattern) |
| 228 |
with pytest.raises(TypeError): |
| 229 |
context.mask(object()) |
| 230 |
|
| 231 |
|
| 232 |
def test_mask_surface(context): |
| 233 |
context.mask_surface(context.get_target(), 0, 0) |
| 234 |
with pytest.raises(TypeError): |
| 235 |
context.mask_surface(object(), 0, 0) |
| 236 |
|
| 237 |
|
| 238 |
def test_paint_with_alpha(context): |
| 239 |
context.paint_with_alpha(0.5) |
| 240 |
with pytest.raises(TypeError): |
| 241 |
context.paint_with_alpha(object()) |
| 242 |
|
| 243 |
|
| 244 |
def test_path_extents(context): |
| 245 |
context.line_to(1, 1) |
| 246 |
context.line_to(1, 0) |
| 247 |
context.line_to(0, 0) |
| 248 |
context.line_to(0, 1) |
| 249 |
context.line_to(1, 1) |
| 250 |
assert context.path_extents() == (0.0, 0.0, 1.0, 1.0) |
| 251 |
|
| 252 |
|
| 253 |
def test_push_pop_group(context): |
| 254 |
context.push_group() |
| 255 |
context.pop_group() |
| 256 |
|
| 257 |
context.push_group() |
| 258 |
context.pop_group_to_source() |
| 259 |
|
| 260 |
with pytest.raises(TypeError): |
| 261 |
context.push_group_with_content(object()) |
| 262 |
|
| 263 |
context.push_group_with_content(cairo.Content.COLOR) |
| 264 |
context.pop_group() |
| 265 |
|
| 266 |
with pytest.raises(cairo.Error): |
| 267 |
context.pop_group() |
| 268 |
|
| 269 |
|
| 270 |
def test_rectangle(context): |
| 271 |
context.rectangle(1, 2, 4, 5) |
| 272 |
assert context.path_extents() == (1.0, 2.0, 5.0, 7.0) |
| 273 |
with pytest.raises(TypeError): |
| 274 |
context.rectangle(1, 2, 3, object()) |
| 275 |
|
| 276 |
|
| 277 |
def test_rotate(context): |
| 278 |
context.rotate(0.3) |
| 279 |
with pytest.raises(TypeError): |
| 280 |
context.rotate(object()) |
| 281 |
|
| 282 |
|
| 283 |
def test_rel_curve_to(context): |
| 284 |
context.line_to(0, 0) |
| 285 |
context.rel_curve_to(0, 0, 0, 0, 0, 0) |
| 286 |
with pytest.raises(TypeError): |
| 287 |
context.rel_curve_to(object(), 0, 0, 0, 0, 0) |
| 288 |
|
| 289 |
|
| 290 |
def test_move_to(context): |
| 291 |
context.move_to(10, 10) |
| 292 |
assert context.get_current_point() == (10, 10) |
| 293 |
with pytest.raises(TypeError): |
| 294 |
context.move_to(object(), 0) |
| 295 |
|
| 296 |
|
| 297 |
def test_rel_line_to(context): |
| 298 |
context.line_to(0, 0) |
| 299 |
context.rel_line_to(1, 1) |
| 300 |
with pytest.raises(TypeError): |
| 301 |
context.rel_line_to(object(), 0) |
| 302 |
|
| 303 |
|
| 304 |
def test_rel_move_to(context): |
| 305 |
context.line_to(0, 0) |
| 306 |
context.rel_move_to(1, 1) |
| 307 |
with pytest.raises(TypeError): |
| 308 |
context.rel_move_to(object(), 0) |
| 309 |
|
| 310 |
|
| 311 |
def test_save_restore(context): |
| 312 |
context.save() |
| 313 |
context.restore() |
| 314 |
|
| 315 |
|
| 316 |
def test_scale(context): |
| 317 |
context.scale(2, 2) |
| 318 |
with pytest.raises(TypeError): |
| 319 |
context.scale(object(), 0) |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy") |
| 324 |
def test_select_font_face(context): |
| 325 |
context.select_font_face("") |
| 326 |
with pytest.raises(TypeError): |
| 327 |
context.select_font_face(None) |
| 328 |
|
| 329 |
|
| 330 |
def test_set_antialias(context): |
| 331 |
context.set_antialias(cairo.Antialias.SUBPIXEL) |
| 332 |
assert context.get_antialias() == cairo.Antialias.SUBPIXEL |
| 333 |
with pytest.raises(TypeError): |
| 334 |
context.set_antialias(object()) |
| 335 |
|
| 336 |
|
| 337 |
def test_set_fill_rule(context): |
| 338 |
context.set_fill_rule(cairo.FillRule.EVEN_ODD) |
| 339 |
assert context.get_fill_rule() == cairo.FillRule.EVEN_ODD |
| 340 |
with pytest.raises(TypeError): |
| 341 |
context.set_fill_rule(object()) |
| 342 |
|
| 343 |
|
| 344 |
def test_set_font_face(context): |
| 345 |
assert context.get_font_face() |
| 346 |
context.set_font_face(None) |
| 347 |
assert context.get_font_face() |
| 348 |
ff = context.get_font_face() |
| 349 |
context.set_font_face(ff) |
| 350 |
assert context.get_font_face() == ff |
| 351 |
with pytest.raises(TypeError): |
| 352 |
context.set_font_face(object()) |
| 353 |
|
| 354 |
|
| 355 |
def test_set_font_matrix(context): |
| 356 |
m = cairo.Matrix() |
| 357 |
context.set_font_matrix(m) |
| 358 |
assert context.get_font_matrix() == m |
| 359 |
with pytest.raises(TypeError): |
| 360 |
context.set_font_matrix(object()) |
| 361 |
|
| 362 |
|
| 363 |
def test_set_line_cap(context): |
| 364 |
context.set_line_cap(cairo.LineCap.SQUARE) |
| 365 |
assert context.get_line_cap() == cairo.LineCap.SQUARE |
| 366 |
with pytest.raises(TypeError): |
| 367 |
context.set_line_cap(object()) |
| 368 |
|
| 369 |
|
| 370 |
def test_set_line_join(context): |
| 371 |
context.set_line_join(cairo.LineJoin.BEVEL) |
| 372 |
assert context.get_line_join() == cairo.LineJoin.BEVEL |
| 373 |
with pytest.raises(TypeError): |
| 374 |
context.set_line_join(object()) |
| 375 |
|
| 376 |
|
| 377 |
def test_set_line_width(context): |
| 378 |
context.set_line_width(42) |
| 379 |
assert context.get_line_width() == 42 |
| 380 |
with pytest.raises(TypeError): |
| 381 |
context.set_line_width(object()) |
| 382 |
|
| 383 |
|
| 384 |
def test_set_matrix(context): |
| 385 |
m = cairo.Matrix() |
| 386 |
context.set_matrix(m) |
| 387 |
assert context.get_matrix() == m |
| 388 |
with pytest.raises(TypeError): |
| 389 |
context.set_matrix(object()) |
| 390 |
|
| 391 |
|
| 392 |
def test_set_miter_limit(context): |
| 393 |
context.set_miter_limit(42) |
| 394 |
assert context.get_miter_limit() == 42 |
| 395 |
with pytest.raises(TypeError): |
| 396 |
context.set_miter_limit(object()) |
| 397 |
|
| 398 |
|
| 399 |
def test_set_scaled_font(context): |
| 400 |
context.set_scaled_font(context.get_scaled_font()) |
| 401 |
with pytest.raises(TypeError): |
| 402 |
context.set_scaled_font(object()) |
| 403 |
|
| 404 |
|
| 405 |
def test_set_font_options(context): |
| 406 |
context.set_font_options(context.get_font_options()) |
| 407 |
with pytest.raises(TypeError): |
| 408 |
context.set_font_options(object()) |
| 409 |
|
| 410 |
|
| 411 |
def test_set_font_size(context): |
| 412 |
context.set_font_size(42) |
| 413 |
assert context.get_font_matrix() == cairo.Matrix(42, 0, 0, 42, 0, 0) |
| 414 |
with pytest.raises(TypeError): |
| 415 |
context.set_font_size(object()) |
| 416 |
|
| 417 |
|
| 418 |
def test_set_source(context): |
| 419 |
p = cairo.SolidPattern(0, 0, 0) |
| 420 |
context.set_source(p) |
| 421 |
assert context.get_source() == p |
| 422 |
with pytest.raises(TypeError): |
| 423 |
context.set_source(object()) |
| 424 |
|
| 425 |
|
| 426 |
def test_set_source_rgb(context): |
| 427 |
with pytest.raises(TypeError): |
| 428 |
context.set_source_rgb(1, 1, object()) |
| 429 |
|
| 430 |
|
| 431 |
def test_get_source_rgba(context): |
| 432 |
context.set_source_rgba(1, 1, 1) |
| 433 |
assert context.get_source().get_rgba() == (1, 1, 1, 1) |
| 434 |
context.set_source_rgba(1, 1, 1, 0.5) |
| 435 |
assert context.get_source().get_rgba() == (1, 1, 1, 0.5) |
| 436 |
with pytest.raises(TypeError): |
| 437 |
context.set_source_rgba(1, 1, object()) |
| 438 |
|
| 439 |
|
| 440 |
def test_set_source_surface(context): |
| 441 |
with pytest.raises(TypeError): |
| 442 |
context.set_source_surface(object()) |
| 443 |
|
| 444 |
|
| 445 |
def test_set_tolerance(context): |
| 446 |
context.set_tolerance(42) |
| 447 |
assert context.get_tolerance() == 42 |
| 448 |
with pytest.raises(TypeError): |
| 449 |
context.set_tolerance(object()) |
| 450 |
|
| 451 |
|
| 452 |
def test_show_glyphs(context): |
| 453 |
with pytest.raises(TypeError): |
| 454 |
context.show_glyphs() |
| 455 |
|
| 456 |
with pytest.raises(TypeError): |
| 457 |
context.show_glyphs(object()) |
| 458 |
|
| 459 |
context.show_glyphs([], 0) |
| 460 |
|
| 461 |
|
| 462 |
def test_show_text(context): |
| 463 |
with pytest.raises(TypeError): |
| 464 |
context.show_text() |
| 465 |
|
| 466 |
|
| 467 |
def test_stroke_extents(context): |
| 468 |
assert context.stroke_extents() == (0.0, 0.0, 0.0, 0.0) |
| 469 |
|
| 470 |
|
| 471 |
def test_text_extents(context): |
| 472 |
with pytest.raises(TypeError): |
| 473 |
context.text_extents() |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="PyPy") |
| 478 |
def test_text_path(context): |
| 479 |
context.text_path("foo") |
| 480 |
with pytest.raises(TypeError): |
| 481 |
context.text_path(object()) |
| 482 |
|
| 483 |
|
| 484 |
def test_transform(context): |
| 485 |
context.transform(cairo.Matrix()) |
| 486 |
with pytest.raises(TypeError): |
| 487 |
context.transform(object()) |
| 488 |
|
| 489 |
|
| 490 |
def test_translate(context): |
| 491 |
context.translate(0.5, 0.5) |
| 492 |
with pytest.raises(TypeError): |
| 493 |
context.translate(0.5, object()) |
| 494 |
|
| 495 |
|
| 496 |
def test_user_to_device(context): |
| 497 |
assert context.user_to_device(0, 0) == (0, 0) |
| 498 |
with pytest.raises(TypeError): |
| 499 |
context.user_to_device(0, object()) |
| 500 |
|
| 501 |
|
| 502 |
def test_user_to_device_distance(context): |
| 503 |
assert context.user_to_device_distance(0, 0) == (0, 0) |
| 504 |
with pytest.raises(TypeError): |
| 505 |
context.user_to_device_distance(0, object()) |
| 506 |
|
| 507 |
|
| 508 |
def test_context(context): |
| 509 |
with pytest.raises(TypeError): |
| 510 |
cairo.Context(None) |
| 511 |
|
| 512 |
assert not context == object() |
| 513 |
|
| 514 |
|
| 515 |
def test_simple(context): |
| 516 |
context.clip_preserve() |
| 517 |
context.copy_page() |
| 518 |
context.copy_path_flat() |
| 519 |
context.fill() |
| 520 |
context.fill_preserve() |
| 521 |
context.font_extents() |
| 522 |
context.identity_matrix() |
| 523 |
context.new_sub_path() |
| 524 |
context.show_page() |
| 525 |
context.stroke_preserve() |
| 526 |
|
| 527 |
assert context.get_dash_count() == 0 |
| 528 |
assert isinstance(context.get_font_matrix(), cairo.Matrix) |
| 529 |
|
| 530 |
assert context.get_group_target() |
| 531 |
context.get_line_width() |
| 532 |
|
| 533 |
assert isinstance(context.get_tolerance(), float) |
| 534 |
assert isinstance(context.get_miter_limit(), float) |
| 535 |
assert isinstance(context.get_matrix(), cairo.Matrix) |
| 536 |
|
| 537 |
|
| 538 |
@pytest.mark.skipif(not hasattr(cairo.Context, "set_hairline"), |
| 539 |
reason="too old cairo") |
| 540 |
def test_hairline(context: cairo.Context): |
| 541 |
assert not context.get_hairline() |
| 542 |
context.set_hairline(True) |
| 543 |
assert isinstance(context.get_hairline(), bool) |
| 544 |
assert context.get_hairline() |