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