File: test_compositing.py

package info (click to toggle)
fpdf2 2.8.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 114,352 kB
  • sloc: python: 50,410; sh: 133; makefile: 12
file content (66 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download
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
import pytest
from pathlib import Path

from fpdf import FPDF
from fpdf.drawing import PaintedPath, PaintComposite
from fpdf.enums import CompositingOperation, PathPaintRule
from test.conftest import assert_pdf_equal

HERE = Path(__file__).resolve().parent


def _blue_square():
    p = PaintedPath()
    p.rectangle(10, 10, 50, 50)
    p.style.fill_color = "#0000ff"
    p.style.fill_opacity = 1
    p.style.paint_rule = PathPaintRule.FILL_NONZERO
    return p


def _red_square():
    p = PaintedPath()
    p.rectangle(35, 35, 50, 50)
    p.style.fill_color = "#ff0000"
    p.style.fill_opacity = 1
    p.style.paint_rule = PathPaintRule.FILL_NONZERO
    return p


def _generate_pdf(op: CompositingOperation):
    pdf = FPDF()
    pdf.page_background = (211, 211, 211)
    pdf.add_page()
    with pdf.drawing_context() as gc:
        src = _blue_square()
        dst = _red_square()
        comp = PaintComposite(backdrop=dst, source=src, operation=op)
        gc.add_item(comp)
    return pdf


@pytest.mark.parametrize(
    "op",
    [
        CompositingOperation.CLEAR,
        CompositingOperation.SOURCE,
        CompositingOperation.DESTINATION,
        CompositingOperation.SOURCE_OVER,
        CompositingOperation.DESTINATION_OVER,
        CompositingOperation.SOURCE_IN,
        CompositingOperation.DESTINATION_IN,
        CompositingOperation.SOURCE_OUT,
        CompositingOperation.DESTINATION_OUT,
        CompositingOperation.SOURCE_ATOP,
        CompositingOperation.DESTINATION_ATOP,
        CompositingOperation.XOR,
    ],
)
def test_compositing_operations(op, tmp_path):
    pdf = _generate_pdf(op)
    name = f"compositing_{op.value}.pdf"
    assert_pdf_equal(
        pdf,
        HERE / "generated_pdf" / name,
        tmp_path,
    )