File: test_text_mode.py

package info (click to toggle)
fpdf2 2.8.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,860 kB
  • sloc: python: 39,487; sh: 133; makefile: 12
file content (69 lines) | stat: -rw-r--r-- 2,156 bytes parent folder | download | duplicates (2)
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
from pathlib import Path

from fpdf import FPDF, TextMode

import pytest

from test.conftest import assert_pdf_equal


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


def test_text_modes(tmp_path):
    pdf = FPDF(format=(350, 150))
    pdf.add_page()
    pdf.set_font("Helvetica", size=80)
    with pdf.local_context(fill_color=(255, 128, 0)):
        pdf.cell(text="FILL default")
    with pdf.local_context(text_color=(0, 128, 255)):
        pdf.cell(text=" text mode")
    pdf.ln()
    with pdf.local_context(text_mode=TextMode.STROKE, line_width=2):
        pdf.cell(text="STROKE text mode")
    pdf.ln()
    pdf.text_mode = TextMode.FILL_STROKE
    pdf.line_width = 4
    pdf.set_draw_color(255, 0, 255)
    pdf.cell(text="FILL_STROKE text mode")
    pdf.ln()
    with pdf.local_context():
        pdf.text_mode = "INVISIBLE"  # testing TextMode.coerce
        pdf.cell(text="INVISIBLE text mode")
    assert_pdf_equal(pdf, HERE / "text_modes.pdf", tmp_path)


def test_clip_text_modes(tmp_path):
    pdf = FPDF(format=(420, 150))
    pdf.add_page()
    pdf.set_font("Helvetica", size=80)
    pdf.line_width = 1
    with pdf.local_context(text_mode=TextMode.FILL_CLIP, text_color=(0, 255, 255)):
        pdf.cell(text="FILL_CLIP text mode")
        for r in range(0, 100, 1):
            pdf.circle(x=110, y=22, radius=r)
    pdf.ln()
    with pdf.local_context(text_mode=TextMode.STROKE_CLIP):
        pdf.cell(text="STROKE_CLIP text mode")
        for r in range(0, 100, 1):
            pdf.circle(x=110, y=50, radius=r)
    pdf.ln()
    with pdf.local_context(
        text_mode=TextMode.FILL_STROKE_CLIP, text_color=(0, 255, 255)
    ):
        pdf.cell(text="FILL_STROKE_CLIP text mode")
        for r in range(0, 100, 1):
            pdf.circle(x=110, y=78, radius=r)
    pdf.ln()
    with pdf.local_context(text_mode=TextMode.CLIP):
        pdf.cell(text="CLIP text mode")
        for r in range(0, 100, 1):
            pdf.circle(x=110, y=106, radius=r)
    pdf.ln()
    assert_pdf_equal(pdf, HERE / "clip_text_modes.pdf", tmp_path)


def test_invalid_text_mode():
    pdf = FPDF()
    with pytest.raises(ValueError):
        pdf.text_mode = "DUMMY"