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
|
from pathlib import Path
import fpdf
from fpdf.errors import FPDFException
from test.conftest import assert_pdf_equal, assert_same_file
import pytest
HERE = Path(__file__).resolve().parent
POLYLINE_COORDINATES = [(10, 10), (40, 10), (10, 40)]
SCALING_FACTORS_FOR_UNITS = [
("pt", 1),
("mm", 1 / (72 / 25.4)),
("cm", 1 / (72 / 2.54)),
("in", 1 / 72),
]
@pytest.mark.parametrize("unit, factor", SCALING_FACTORS_FOR_UNITS)
def test_polyline_command_all_k(unit, factor):
pdf = fpdf.FPDF(unit=unit)
pdf.add_page()
data = []
# pylint: disable=protected-access
pdf._out = data.append
pdf.polyline(scale_points(POLYLINE_COORDINATES, factor))
assert "".join(data) == "10.00 831.89 m40.00 831.89 l10.00 801.89 l S"
data.clear()
with pytest.warns(DeprecationWarning) as record:
# keep one "fill=True" to test the warning
pdf.polyline(scale_points(POLYLINE_COORDINATES, factor), fill=True)
assert "".join(data) == "10.00 831.89 m40.00 831.89 l10.00 801.89 l B"
data.clear()
assert len(record) == 1
assert_same_file(record[0].filename, __file__)
pdf.polyline(scale_points(POLYLINE_COORDINATES, factor), polygon=True)
assert "".join(data) == "10.00 831.89 m40.00 831.89 l10.00 801.89 l h S"
data.clear()
pdf.polyline(scale_points(POLYLINE_COORDINATES, factor), polygon=True, style="DF")
assert "".join(data) == "10.00 831.89 m40.00 831.89 l10.00 801.89 l h B"
def scale_points(raw_points, k_recip):
return [(k_recip * coord[0], k_recip * coord[1]) for coord in raw_points]
def test_check_page():
pdf = fpdf.FPDF(unit="pt")
with pytest.raises(FPDFException) as polyline_no_page:
pdf.polyline(POLYLINE_COORDINATES)
expected_error_msg = "No page open, you need to call add_page() first"
assert expected_error_msg == str(polyline_no_page.value)
with pytest.raises(FPDFException) as polygon_no_page:
pdf.polygon(POLYLINE_COORDINATES)
assert expected_error_msg == str(polygon_no_page.value)
def test_filled_polygon(tmp_path):
pdf = fpdf.FPDF()
pdf.add_page()
pdf.set_line_width(2)
pdf.set_fill_color(r=255, g=0, b=0)
coords = ((100, 0), (5, 69), (41, 181), (159, 181), (195, 69))
pdf.polygon(coords, style="DF")
assert_pdf_equal(pdf, HERE / "filled_polygon.pdf", tmp_path)
|