154 lines
4.4 KiB
Python
154 lines
4.4 KiB
Python
import pytest
|
|
import plakativ
|
|
import tempfile
|
|
import fitz
|
|
import fitz.utils
|
|
import os
|
|
import pdfrw
|
|
|
|
|
|
def mm_to_pt(length):
|
|
return (72.0 * length) / 25.4
|
|
|
|
|
|
def _create_pdf(width, height):
|
|
return tmpfile
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def infile_a4_portrait():
|
|
tmpfile = _create_pdf(mm_to_pt(210), mm_to_pt(297))
|
|
yield tmpfile
|
|
os.unlink(tmpfile)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def infile_custom_portrait():
|
|
tmpfile = _create_pdf(mm_to_pt(200), mm_to_pt(400))
|
|
yield tmpfile
|
|
os.unlink(tmpfile)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def infile_a4_landscape():
|
|
tmpfile = _create_pdf(mm_to_pt(297), mm_to_pt(210))
|
|
yield tmpfile
|
|
os.unlink(tmpfile)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def infile_custom_landscape():
|
|
tmpfile = _create_pdf(mm_to_pt(400), mm_to_pt(200))
|
|
yield tmpfile
|
|
os.unlink(tmpfile)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def infile_custom_square():
|
|
tmpfile = _create_pdf(mm_to_pt(300), mm_to_pt(300))
|
|
yield tmpfile
|
|
os.unlink(tmpfile)
|
|
|
|
|
|
_formats = {
|
|
"dina4_portrait": (210, 297),
|
|
"dina4_landscape": (297, 210),
|
|
"dina3_portrait": (297, 420),
|
|
"dina3_landscape": (420, 297),
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"postersize,input_pagesize,output_pagesize,expected",
|
|
[
|
|
(
|
|
_formats["dina3_portrait"],
|
|
_formats["dina4_portrait"],
|
|
None,
|
|
[
|
|
(
|
|
["0", "380.8549", "337.72779", "841.8898"],
|
|
["1.4141413", "0", "0", "1.4141413", "117.68077", "-538.5826"],
|
|
),
|
|
(
|
|
["257.5478", "380.8549", "595.2756", "841.8898"],
|
|
["1.4141413", "0", "0", "1.4141413", "-364.20893", "-538.5826"],
|
|
),
|
|
(
|
|
["0", "0", "337.72779", "461.03489"],
|
|
["1.4141413", "0", "0", "1.4141413", "117.68077", "189.9213"],
|
|
),
|
|
(
|
|
["257.5478", "0", "595.2756", "461.03489"],
|
|
["1.4141413", "0", "0", "1.4141413", "-364.20893", "189.9213"],
|
|
),
|
|
],
|
|
),
|
|
(
|
|
_formats["dina3_landscape"],
|
|
_formats["dina4_landscape"],
|
|
None,
|
|
[
|
|
(
|
|
["0", "257.5478", "461.03489", "595.2756"],
|
|
["1.4141413", "0", "0", "1.4141413", "189.9213", "-364.20893"],
|
|
),
|
|
(
|
|
["380.8549", "257.5478", "841.8898", "595.2756"],
|
|
["1.4141413", "0", "0", "1.4141413", "-538.5826", "-364.20893"],
|
|
),
|
|
(
|
|
["0", "0", "461.03489", "337.72779"],
|
|
["1.4141413", "0", "0", "1.4141413", "189.9213", "117.68074"],
|
|
),
|
|
(
|
|
["380.8549", "0", "841.8898", "337.72779"],
|
|
["1.4141413", "0", "0", "1.4141413", "-538.5826", "117.68074"],
|
|
),
|
|
],
|
|
),
|
|
],
|
|
)
|
|
def test_cases(postersize, input_pagesize, output_pagesize, expected):
|
|
width = mm_to_pt(input_pagesize[0])
|
|
height = mm_to_pt(input_pagesize[1])
|
|
|
|
doc = fitz.open()
|
|
page = doc.newPage(pno=-1, width=width, height=height)
|
|
img = page.newShape()
|
|
|
|
red = fitz.utils.getColor("red")
|
|
green = fitz.utils.getColor("green")
|
|
blue = fitz.utils.getColor("blue")
|
|
orange = fitz.utils.getColor("orange")
|
|
|
|
img.insertText(fitz.Point(97, 620), "A", fontsize=600, color=blue)
|
|
img.commit()
|
|
img.drawLine(fitz.Point(0, 0), fitz.Point(width, height))
|
|
img.finish(color=red)
|
|
img.drawLine(fitz.Point(0, height), fitz.Point(width, 0))
|
|
img.finish(color=green)
|
|
img.drawRect(fitz.Rect(fitz.Point(0, 0), fitz.Point(width, height)))
|
|
img.finish(color=orange)
|
|
img.commit()
|
|
|
|
fd, infile = tempfile.mkstemp(
|
|
prefix="plakativ", suffix="%f_%f.pdf" % (width, height)
|
|
)
|
|
os.close(fd)
|
|
doc.save(infile, pretty=True, expand=255)
|
|
|
|
fd, outfile = tempfile.mkstemp(prefix="plakativ")
|
|
os.close(fd)
|
|
plakativ.compute_layout(infile, outfile, mode="size", size=postersize, border=(20, 20, 20, 20))
|
|
os.unlink(infile)
|
|
|
|
reader = pdfrw.PdfReader(outfile)
|
|
os.unlink(outfile)
|
|
|
|
pages = reader.Root.Pages.Kids
|
|
assert len(pages) == len(expected)
|
|
|
|
for page, (bbox, matrix) in zip(pages, expected):
|
|
assert page.Resources.XObject.fzFrm0.BBox == bbox
|
|
assert page.Resources.XObject.fzFrm0.Matrix == matrix
|