File: test_image_fit.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 (66 lines) | stat: -rw-r--r-- 1,797 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
from pathlib import Path

from fpdf import FPDF
from test.conftest import assert_pdf_equal


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


def test_image_fit_in_rect(tmp_path):
    """
    Scale the image to fill the rectangle, keeping its aspect ratio,
    and ensure it does overflow the rectangle width or height in the process.
    """
    pdf = FPDF()
    pdf.add_page()

    rect1 = 30, 30, 60, 100
    pdf.rect(*rect1)
    pdf.image(
        HERE / "png_images/ba2b2b6e72ca0e4683bb640e2d5572f8.png",
        *rect1,
        keep_aspect_ratio=True
    )

    rect2 = 100, 30, 60, 100
    pdf.rect(*rect2)
    pdf.image(
        HERE / "png_images/51a4d21670dc8dfa8ffc9e54afd62f5f.png",
        *rect2,
        keep_aspect_ratio=True
    )

    assert_pdf_equal(pdf, HERE / "image_fit_in_rect.pdf", tmp_path)


def test_image_fit_in_rect_with_c_align_center(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.image(
        HERE / "png_images/6c853ed9dacd5716bc54eb59cec30889.png",
        w=72,
        h=102,
        x="C",
        keep_aspect_ratio=True,
    )
    assert_pdf_equal(pdf, HERE / "image_fit_in_rect_with_c_align_center.pdf", tmp_path)


def test_image_fit_in_rect_with_c_align_center_with_downscale(tmp_path):
    pdf = FPDF()
    pdf.oversized_images = "DOWNSCALE"
    pdf.add_page()
    pdf.image(
        HERE / "png_images/6c853ed9dacd5716bc54eb59cec30889.png",
        w=72,
        h=102,
        x="C",
        keep_aspect_ratio=True,
    )
    reference_pdf_path = (
        HERE / "image_fit_in_rect_with_c_align_center_with_downscale.pdf"
    )
    assert_pdf_equal(pdf, reference_pdf_path, tmp_path)
    pdf_with_full_img_path = HERE / "image_fit_in_rect_with_c_align_center.pdf"
    assert reference_pdf_path.stat().st_size < pdf_with_full_img_path.stat().st_size