1
0
Fork 0
forked from josch/plakativ

extend testsuite with landscape input

This commit is contained in:
Johannes 'josch' Schauer 2019-07-01 11:25:25 +02:00
parent 6a0e78b409
commit 4c958b72aa
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1
2 changed files with 169 additions and 72 deletions

View file

@ -49,19 +49,18 @@ class Plakativ:
assert len(self.doc) == 1
self.dlist = self.doc[0].getDisplayList()
self.pagesize = PAGE_SIZES["A4 (21.0 cm × 29.7 cm)"]
self.border_left = 20
self.border_right = 20
self.border_top = 20
self.border_bottom = 20
self.config = {
"pagesize": (
"input_pagesize": (
pt_to_mm(self.dlist.rect.width),
pt_to_mm(self.dlist.rect.height),
),
"postersize": PAGE_SIZES["A1 (59.4 cm × 84.1 cm)"],
}
self.layout = {"pagesize": PAGE_SIZES["A4 (21.0 cm × 29.7 cm)"]}
self.layout = {"output_pagesize": PAGE_SIZES["A4 (21.0 cm × 29.7 cm)"]}
def compute_layout(self, mode, size=None, mult=None, npages=None):
if self.doc is None:
@ -80,10 +79,10 @@ class Plakativ:
else:
raise Exception("unsupported mode: %s" % mode)
printable_width = self.layout["pagesize"][0] - (
printable_width = self.layout["output_pagesize"][0] - (
self.border_left + self.border_right
)
printable_height = self.layout["pagesize"][1] - (
printable_height = self.layout["output_pagesize"][1] - (
self.border_top + self.border_bottom
)
@ -92,20 +91,20 @@ class Plakativ:
# fit the input page size into the selected postersize
poster_width = self.config["postersize"][0]
poster_height = (
poster_width * self.config["pagesize"][1]
) / self.config["pagesize"][0]
poster_width * self.config["input_pagesize"][1]
) / self.config["input_pagesize"][0]
if poster_height > self.config["postersize"][1]:
poster_height = self.config["postersize"][1]
poster_width = (
poster_height * self.config["pagesize"][0]
) / self.config["pagesize"][1]
poster_height * self.config["input_pagesize"][0]
) / self.config["input_pagesize"][1]
elif mode == "mult":
area = self.config["pagesize"][0] * self.config["pagesize"][1] * mult
area = self.config["input_pagesize"][0] * self.config["input_pagesize"][1] * mult
poster_width = math.sqrt(
area * self.config["pagesize"][0] / self.config["pagesize"][1]
area * self.config["input_pagesize"][0] / self.config["input_pagesize"][1]
)
poster_height = math.sqrt(
area * self.config["pagesize"][1] / self.config["pagesize"][0]
area * self.config["input_pagesize"][1] / self.config["input_pagesize"][0]
)
else:
raise Exception("unsupported mode: %s" % mode)
@ -153,13 +152,13 @@ class Plakativ:
poster_width = width_portrait
poster_height = (
poster_width * self.config["pagesize"][1]
) / self.config["pagesize"][0]
poster_width * self.config["input_pagesize"][1]
) / self.config["input_pagesize"][0]
if poster_height > height_portrait:
poster_height = height_portrait
poster_width = (
poster_height * self.config["pagesize"][0]
) / self.config["pagesize"][1]
poster_height * self.config["input_pagesize"][0]
) / self.config["input_pagesize"][1]
area_portrait = poster_width * poster_height
@ -172,13 +171,13 @@ class Plakativ:
poster_width = width_landscape
poster_height = (
poster_width * self.config["pagesize"][1]
) / self.config["pagesize"][0]
poster_width * self.config["input_pagesize"][1]
) / self.config["input_pagesize"][0]
if poster_height > height_landscape:
poster_height = height_landscape
poster_width = (
poster_height * self.config["pagesize"][0]
) / self.config["pagesize"][1]
poster_height * self.config["input_pagesize"][0]
) / self.config["input_pagesize"][1]
area_landscape = poster_width * poster_height
@ -215,7 +214,7 @@ class Plakativ:
# return (self.postersize, (poster_width*poster_height)/(pt_to_mm(self.dlist.rect.width)*pt_to_mm(self.dlist.rect.height)), pages_x*pages_y)
if mode == "size":
self.config["mult"] = (poster_width * poster_height) / (
self.config["pagesize"][0] * self.config["pagesize"][1]
self.config["input_pagesize"][0] * self.config["input_pagesize"][1]
)
self.config["npages"] = pages_x * pages_y
elif mode == "mult":
@ -224,7 +223,7 @@ class Plakativ:
elif mode == "npages":
self.config["postersize"] = poster_width, poster_height
self.config["mult"] = (poster_width * poster_height) / (
self.config["pagesize"][0] * self.config["pagesize"][1]
self.config["input_pagesize"][0] * self.config["input_pagesize"][1]
)
else:
raise Exception("unsupported mode: %s" % mode)
@ -239,11 +238,11 @@ class Plakativ:
for (x, y, portrait) in self.layout["positions"]:
if portrait:
page_width=mm_to_pt(self.layout["pagesize"][0])
page_height=mm_to_pt(self.layout["pagesize"][1])
page_width=mm_to_pt(self.layout["output_pagesize"][0])
page_height=mm_to_pt(self.layout["output_pagesize"][1])
else:
page_width=mm_to_pt(self.layout["pagesize"][1])
page_height=mm_to_pt(self.layout["pagesize"][0])
page_width=mm_to_pt(self.layout["output_pagesize"][1])
page_height=mm_to_pt(self.layout["output_pagesize"][0])
page = outdoc.newPage(
-1, # insert after last page
width=page_width,
@ -259,11 +258,11 @@ class Plakativ:
target_xoffset = 0
target_yoffset = 0
if portrait:
target_width = self.layout["pagesize"][0]
target_height = self.layout["pagesize"][1]
target_width = self.layout["output_pagesize"][0]
target_height = self.layout["output_pagesize"][1]
else:
target_width = self.layout["pagesize"][1]
target_height = self.layout["pagesize"][0]
target_width = self.layout["output_pagesize"][1]
target_height = self.layout["output_pagesize"][0]
if target_x < 0:
target_xoffset = -target_x
target_width += target_x
@ -284,8 +283,7 @@ class Plakativ:
mm_to_pt(target_yoffset + target_height),
)
# FIXME: this should probably the pagesize of the input document instead
factor = self.layout["pagesize"][0] / self.layout["postersize"][0]
factor = self.config["input_pagesize"][0] / self.layout["postersize"][0]
sourcerect = fitz.Rect(
mm_to_pt(factor * target_x),
mm_to_pt(factor * target_y),
@ -735,11 +733,11 @@ class Application(tkinter.Frame):
- zoom_1 * self.plakativ.layout["overallsize"][1] / 2
)
if portrait:
x1 = x0 + self.plakativ.layout["pagesize"][0] * zoom_1
y1 = y0 + self.plakativ.layout["pagesize"][1] * zoom_1
x1 = x0 + self.plakativ.layout["output_pagesize"][0] * zoom_1
y1 = y0 + self.plakativ.layout["output_pagesize"][1] * zoom_1
else:
x1 = x0 + self.plakativ.layout["pagesize"][1] * zoom_1
y1 = y0 + self.plakativ.layout["pagesize"][0] * zoom_1
x1 = x0 + self.plakativ.layout["output_pagesize"][1] * zoom_1
y1 = y0 + self.plakativ.layout["output_pagesize"][0] * zoom_1
self.canvas.create_rectangle(x0, y0, x1, y1, outline="red")
self.canvas.create_rectangle(
x0 + zoom_1 * self.border_left.get(),

171
test.py
View file

@ -7,10 +7,16 @@ import os
import pdfrw
def mm_to_pt(length):
return (72.0 * length) / 25.4
@pytest.fixture(scope="module")
def infile():
def infile_a4_portrait():
doc = fitz.open()
page = doc.newPage(pno=-1, width=595, height=842)
width = mm_to_pt(210)
height = mm_to_pt(297)
page = doc.newPage(pno=-1, width=width, height=height)
img = page.newShape()
red = fitz.utils.getColor("red")
@ -20,11 +26,11 @@ def infile():
img.insertText(fitz.Point(97, 620), "A", fontsize=600, color=blue)
img.commit()
img.drawLine(fitz.Point(0, 0), fitz.Point(595, 842))
img.drawLine(fitz.Point(0, 0), fitz.Point(width, height))
img.finish(color=red)
img.drawLine(fitz.Point(0, 842), fitz.Point(595, 0))
img.drawLine(fitz.Point(0, height), fitz.Point(width, 0))
img.finish(color=green)
img.drawRect(fitz.Rect(fitz.Point(0, 0), fitz.Point(595, 842)))
img.drawRect(fitz.Rect(fitz.Point(0, 0), fitz.Point(width, height)))
img.finish(color=orange)
img.commit()
@ -35,69 +41,162 @@ def infile():
os.unlink(tmpfile)
def test_foo(infile):
print("blob")
@pytest.fixture(scope="module")
def infile_a4_landscape():
doc = fitz.open()
width = mm_to_pt(297)
height = mm_to_pt(210)
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, tmpfile = tempfile.mkstemp(prefix="plakativ")
os.close(fd)
doc.save(tmpfile, pretty=True, expand=255)
yield tmpfile
os.unlink(tmpfile)
def test_foo_a3_portrait(infile_a4_portrait):
fd, outfile = tempfile.mkstemp(prefix="plakativ")
os.close(fd)
plakativ.compute_layout(infile, outfile, mode="size", size=(297, 420))
plakativ.compute_layout(infile_a4_portrait, outfile, mode="size", size=(297, 420))
reader = pdfrw.PdfReader(outfile)
os.unlink(outfile)
pages = reader.Root.Pages.Kids
assert len(pages) == 4
assert pages[0].Resources.XObject.fzFrm0.BBox == [
"0",
"380.6912",
"337.75163",
"842",
"380.8549",
"337.72779",
"841.8898",
]
assert pages[1].Resources.XObject.fzFrm0.BBox == [
"257.524",
"380.6912",
"595",
"842",
"257.5478",
"380.8549",
"595.2756",
"841.8898",
]
assert pages[2].Resources.XObject.fzFrm0.BBox == [
"0",
"0",
"337.75163",
"460.91883",
"337.72779",
"461.03489",
]
assert pages[3].Resources.XObject.fzFrm0.BBox == [
"257.524",
"257.5478",
"0",
"595",
"460.91883",
"595.2756",
"461.03489",
]
assert pages[0].Resources.XObject.fzFrm0.Matrix == [
"1.4133016",
"1.4141413",
"0",
"0",
"1.4133016",
"117.930667",
"-538.03146",
"1.4141413",
"117.68077",
"-538.5826",
]
assert pages[1].Resources.XObject.fzFrm0.Matrix == [
"1.4133017",
"1.4141413",
"0",
"0",
"1.4133017",
"-363.76438",
"-538.0315",
"1.4141413",
"-364.20893",
"-538.5826",
]
assert pages[2].Resources.XObject.fzFrm0.Matrix == [
"1.4133016",
"1.4141413",
"0",
"0",
"1.4133016",
"117.930667",
"190.19687",
"1.4141413",
"117.68077",
"189.9213",
]
assert pages[3].Resources.XObject.fzFrm0.Matrix == [
"1.4144558",
"1.4141413",
"0",
"0",
"1.4144558",
"-364.25627",
"189.93088",
"1.4141413",
"-364.20893",
"189.9213",
]
def test_foo_a3_landscape(infile_a4_landscape):
fd, outfile = tempfile.mkstemp(prefix="plakativ")
os.close(fd)
plakativ.compute_layout(infile_a4_landscape, outfile, mode="size", size=(420, 296))
reader = pdfrw.PdfReader(outfile)
os.unlink(outfile)
pages = reader.Root.Pages.Kids
assert len(pages) == 4
assert pages[0].Resources.XObject.fzFrm0.BBox == [
"0",
"257.41648",
"461.1662",
"595.2756",
]
assert pages[1].Resources.XObject.fzFrm0.BBox == [
"380.72358",
"257.41648",
"841.8898",
"595.2756",
]
assert pages[2].Resources.XObject.fzFrm0.BBox == ["0", "0", "461.1662", "337.8591"]
assert pages[3].Resources.XObject.fzFrm0.BBox == [
"380.72358",
"0",
"841.8898",
"337.8591",
]
assert pages[0].Resources.XObject.fzFrm0.Matrix == [
"1.4095239",
"0",
"0",
"1.4095239",
"191.86499",
"-362.83467",
]
assert pages[1].Resources.XObject.fzFrm0.Matrix == [
"1.4095239",
"0",
"0",
"1.4095239",
"-536.6389",
"-362.83467",
]
assert pages[2].Resources.XObject.fzFrm0.Matrix == [
"1.4095239",
"0",
"0",
"1.4095239",
"191.86499",
"119.055118",
]
assert pages[3].Resources.XObject.fzFrm0.Matrix == [
"1.4095239",
"0",
"0",
"1.4095239",
"-536.6389",
"119.055118",
]