forked from josch/plakativ
extend testsuite with landscape input
This commit is contained in:
parent
6a0e78b409
commit
4c958b72aa
2 changed files with 169 additions and 72 deletions
70
plakativ.py
70
plakativ.py
|
@ -49,19 +49,18 @@ class Plakativ:
|
||||||
assert len(self.doc) == 1
|
assert len(self.doc) == 1
|
||||||
self.dlist = self.doc[0].getDisplayList()
|
self.dlist = self.doc[0].getDisplayList()
|
||||||
|
|
||||||
self.pagesize = PAGE_SIZES["A4 (21.0 cm × 29.7 cm)"]
|
|
||||||
self.border_left = 20
|
self.border_left = 20
|
||||||
self.border_right = 20
|
self.border_right = 20
|
||||||
self.border_top = 20
|
self.border_top = 20
|
||||||
self.border_bottom = 20
|
self.border_bottom = 20
|
||||||
self.config = {
|
self.config = {
|
||||||
"pagesize": (
|
"input_pagesize": (
|
||||||
pt_to_mm(self.dlist.rect.width),
|
pt_to_mm(self.dlist.rect.width),
|
||||||
pt_to_mm(self.dlist.rect.height),
|
pt_to_mm(self.dlist.rect.height),
|
||||||
),
|
),
|
||||||
"postersize": PAGE_SIZES["A1 (59.4 cm × 84.1 cm)"],
|
"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):
|
def compute_layout(self, mode, size=None, mult=None, npages=None):
|
||||||
if self.doc is None:
|
if self.doc is None:
|
||||||
|
@ -80,10 +79,10 @@ class Plakativ:
|
||||||
else:
|
else:
|
||||||
raise Exception("unsupported mode: %s" % mode)
|
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
|
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
|
self.border_top + self.border_bottom
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -92,20 +91,20 @@ class Plakativ:
|
||||||
# fit the input page size into the selected postersize
|
# fit the input page size into the selected postersize
|
||||||
poster_width = self.config["postersize"][0]
|
poster_width = self.config["postersize"][0]
|
||||||
poster_height = (
|
poster_height = (
|
||||||
poster_width * self.config["pagesize"][1]
|
poster_width * self.config["input_pagesize"][1]
|
||||||
) / self.config["pagesize"][0]
|
) / self.config["input_pagesize"][0]
|
||||||
if poster_height > self.config["postersize"][1]:
|
if poster_height > self.config["postersize"][1]:
|
||||||
poster_height = self.config["postersize"][1]
|
poster_height = self.config["postersize"][1]
|
||||||
poster_width = (
|
poster_width = (
|
||||||
poster_height * self.config["pagesize"][0]
|
poster_height * self.config["input_pagesize"][0]
|
||||||
) / self.config["pagesize"][1]
|
) / self.config["input_pagesize"][1]
|
||||||
elif mode == "mult":
|
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(
|
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(
|
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:
|
else:
|
||||||
raise Exception("unsupported mode: %s" % mode)
|
raise Exception("unsupported mode: %s" % mode)
|
||||||
|
@ -153,13 +152,13 @@ class Plakativ:
|
||||||
|
|
||||||
poster_width = width_portrait
|
poster_width = width_portrait
|
||||||
poster_height = (
|
poster_height = (
|
||||||
poster_width * self.config["pagesize"][1]
|
poster_width * self.config["input_pagesize"][1]
|
||||||
) / self.config["pagesize"][0]
|
) / self.config["input_pagesize"][0]
|
||||||
if poster_height > height_portrait:
|
if poster_height > height_portrait:
|
||||||
poster_height = height_portrait
|
poster_height = height_portrait
|
||||||
poster_width = (
|
poster_width = (
|
||||||
poster_height * self.config["pagesize"][0]
|
poster_height * self.config["input_pagesize"][0]
|
||||||
) / self.config["pagesize"][1]
|
) / self.config["input_pagesize"][1]
|
||||||
|
|
||||||
area_portrait = poster_width * poster_height
|
area_portrait = poster_width * poster_height
|
||||||
|
|
||||||
|
@ -172,13 +171,13 @@ class Plakativ:
|
||||||
|
|
||||||
poster_width = width_landscape
|
poster_width = width_landscape
|
||||||
poster_height = (
|
poster_height = (
|
||||||
poster_width * self.config["pagesize"][1]
|
poster_width * self.config["input_pagesize"][1]
|
||||||
) / self.config["pagesize"][0]
|
) / self.config["input_pagesize"][0]
|
||||||
if poster_height > height_landscape:
|
if poster_height > height_landscape:
|
||||||
poster_height = height_landscape
|
poster_height = height_landscape
|
||||||
poster_width = (
|
poster_width = (
|
||||||
poster_height * self.config["pagesize"][0]
|
poster_height * self.config["input_pagesize"][0]
|
||||||
) / self.config["pagesize"][1]
|
) / self.config["input_pagesize"][1]
|
||||||
|
|
||||||
area_landscape = poster_width * poster_height
|
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)
|
# 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":
|
if mode == "size":
|
||||||
self.config["mult"] = (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]
|
||||||
)
|
)
|
||||||
self.config["npages"] = pages_x * pages_y
|
self.config["npages"] = pages_x * pages_y
|
||||||
elif mode == "mult":
|
elif mode == "mult":
|
||||||
|
@ -224,7 +223,7 @@ class Plakativ:
|
||||||
elif mode == "npages":
|
elif mode == "npages":
|
||||||
self.config["postersize"] = poster_width, poster_height
|
self.config["postersize"] = poster_width, poster_height
|
||||||
self.config["mult"] = (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:
|
else:
|
||||||
raise Exception("unsupported mode: %s" % mode)
|
raise Exception("unsupported mode: %s" % mode)
|
||||||
|
@ -239,11 +238,11 @@ class Plakativ:
|
||||||
|
|
||||||
for (x, y, portrait) in self.layout["positions"]:
|
for (x, y, portrait) in self.layout["positions"]:
|
||||||
if portrait:
|
if portrait:
|
||||||
page_width=mm_to_pt(self.layout["pagesize"][0])
|
page_width=mm_to_pt(self.layout["output_pagesize"][0])
|
||||||
page_height=mm_to_pt(self.layout["pagesize"][1])
|
page_height=mm_to_pt(self.layout["output_pagesize"][1])
|
||||||
else:
|
else:
|
||||||
page_width=mm_to_pt(self.layout["pagesize"][1])
|
page_width=mm_to_pt(self.layout["output_pagesize"][1])
|
||||||
page_height=mm_to_pt(self.layout["pagesize"][0])
|
page_height=mm_to_pt(self.layout["output_pagesize"][0])
|
||||||
page = outdoc.newPage(
|
page = outdoc.newPage(
|
||||||
-1, # insert after last page
|
-1, # insert after last page
|
||||||
width=page_width,
|
width=page_width,
|
||||||
|
@ -259,11 +258,11 @@ class Plakativ:
|
||||||
target_xoffset = 0
|
target_xoffset = 0
|
||||||
target_yoffset = 0
|
target_yoffset = 0
|
||||||
if portrait:
|
if portrait:
|
||||||
target_width = self.layout["pagesize"][0]
|
target_width = self.layout["output_pagesize"][0]
|
||||||
target_height = self.layout["pagesize"][1]
|
target_height = self.layout["output_pagesize"][1]
|
||||||
else:
|
else:
|
||||||
target_width = self.layout["pagesize"][1]
|
target_width = self.layout["output_pagesize"][1]
|
||||||
target_height = self.layout["pagesize"][0]
|
target_height = self.layout["output_pagesize"][0]
|
||||||
if target_x < 0:
|
if target_x < 0:
|
||||||
target_xoffset = -target_x
|
target_xoffset = -target_x
|
||||||
target_width += target_x
|
target_width += target_x
|
||||||
|
@ -284,8 +283,7 @@ class Plakativ:
|
||||||
mm_to_pt(target_yoffset + target_height),
|
mm_to_pt(target_yoffset + target_height),
|
||||||
)
|
)
|
||||||
|
|
||||||
# FIXME: this should probably the pagesize of the input document instead
|
factor = self.config["input_pagesize"][0] / self.layout["postersize"][0]
|
||||||
factor = self.layout["pagesize"][0] / self.layout["postersize"][0]
|
|
||||||
sourcerect = fitz.Rect(
|
sourcerect = fitz.Rect(
|
||||||
mm_to_pt(factor * target_x),
|
mm_to_pt(factor * target_x),
|
||||||
mm_to_pt(factor * target_y),
|
mm_to_pt(factor * target_y),
|
||||||
|
@ -735,11 +733,11 @@ class Application(tkinter.Frame):
|
||||||
- zoom_1 * self.plakativ.layout["overallsize"][1] / 2
|
- zoom_1 * self.plakativ.layout["overallsize"][1] / 2
|
||||||
)
|
)
|
||||||
if portrait:
|
if portrait:
|
||||||
x1 = x0 + self.plakativ.layout["pagesize"][0] * zoom_1
|
x1 = x0 + self.plakativ.layout["output_pagesize"][0] * zoom_1
|
||||||
y1 = y0 + self.plakativ.layout["pagesize"][1] * zoom_1
|
y1 = y0 + self.plakativ.layout["output_pagesize"][1] * zoom_1
|
||||||
else:
|
else:
|
||||||
x1 = x0 + self.plakativ.layout["pagesize"][1] * zoom_1
|
x1 = x0 + self.plakativ.layout["output_pagesize"][1] * zoom_1
|
||||||
y1 = y0 + self.plakativ.layout["pagesize"][0] * 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, y0, x1, y1, outline="red")
|
||||||
self.canvas.create_rectangle(
|
self.canvas.create_rectangle(
|
||||||
x0 + zoom_1 * self.border_left.get(),
|
x0 + zoom_1 * self.border_left.get(),
|
||||||
|
|
171
test.py
171
test.py
|
@ -7,10 +7,16 @@ import os
|
||||||
import pdfrw
|
import pdfrw
|
||||||
|
|
||||||
|
|
||||||
|
def mm_to_pt(length):
|
||||||
|
return (72.0 * length) / 25.4
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def infile():
|
def infile_a4_portrait():
|
||||||
doc = fitz.open()
|
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()
|
img = page.newShape()
|
||||||
|
|
||||||
red = fitz.utils.getColor("red")
|
red = fitz.utils.getColor("red")
|
||||||
|
@ -20,11 +26,11 @@ def infile():
|
||||||
|
|
||||||
img.insertText(fitz.Point(97, 620), "A", fontsize=600, color=blue)
|
img.insertText(fitz.Point(97, 620), "A", fontsize=600, color=blue)
|
||||||
img.commit()
|
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.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.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.finish(color=orange)
|
||||||
img.commit()
|
img.commit()
|
||||||
|
|
||||||
|
@ -35,69 +41,162 @@ def infile():
|
||||||
os.unlink(tmpfile)
|
os.unlink(tmpfile)
|
||||||
|
|
||||||
|
|
||||||
def test_foo(infile):
|
@pytest.fixture(scope="module")
|
||||||
print("blob")
|
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")
|
fd, outfile = tempfile.mkstemp(prefix="plakativ")
|
||||||
os.close(fd)
|
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)
|
reader = pdfrw.PdfReader(outfile)
|
||||||
|
os.unlink(outfile)
|
||||||
|
|
||||||
pages = reader.Root.Pages.Kids
|
pages = reader.Root.Pages.Kids
|
||||||
assert len(pages) == 4
|
assert len(pages) == 4
|
||||||
assert pages[0].Resources.XObject.fzFrm0.BBox == [
|
assert pages[0].Resources.XObject.fzFrm0.BBox == [
|
||||||
"0",
|
"0",
|
||||||
"380.6912",
|
"380.8549",
|
||||||
"337.75163",
|
"337.72779",
|
||||||
"842",
|
"841.8898",
|
||||||
]
|
]
|
||||||
assert pages[1].Resources.XObject.fzFrm0.BBox == [
|
assert pages[1].Resources.XObject.fzFrm0.BBox == [
|
||||||
"257.524",
|
"257.5478",
|
||||||
"380.6912",
|
"380.8549",
|
||||||
"595",
|
"595.2756",
|
||||||
"842",
|
"841.8898",
|
||||||
]
|
]
|
||||||
assert pages[2].Resources.XObject.fzFrm0.BBox == [
|
assert pages[2].Resources.XObject.fzFrm0.BBox == [
|
||||||
"0",
|
"0",
|
||||||
"0",
|
"0",
|
||||||
"337.75163",
|
"337.72779",
|
||||||
"460.91883",
|
"461.03489",
|
||||||
]
|
]
|
||||||
assert pages[3].Resources.XObject.fzFrm0.BBox == [
|
assert pages[3].Resources.XObject.fzFrm0.BBox == [
|
||||||
"257.524",
|
"257.5478",
|
||||||
"0",
|
"0",
|
||||||
"595",
|
"595.2756",
|
||||||
"460.91883",
|
"461.03489",
|
||||||
]
|
]
|
||||||
assert pages[0].Resources.XObject.fzFrm0.Matrix == [
|
assert pages[0].Resources.XObject.fzFrm0.Matrix == [
|
||||||
"1.4133016",
|
"1.4141413",
|
||||||
"0",
|
"0",
|
||||||
"0",
|
"0",
|
||||||
"1.4133016",
|
"1.4141413",
|
||||||
"117.930667",
|
"117.68077",
|
||||||
"-538.03146",
|
"-538.5826",
|
||||||
]
|
]
|
||||||
assert pages[1].Resources.XObject.fzFrm0.Matrix == [
|
assert pages[1].Resources.XObject.fzFrm0.Matrix == [
|
||||||
"1.4133017",
|
"1.4141413",
|
||||||
"0",
|
"0",
|
||||||
"0",
|
"0",
|
||||||
"1.4133017",
|
"1.4141413",
|
||||||
"-363.76438",
|
"-364.20893",
|
||||||
"-538.0315",
|
"-538.5826",
|
||||||
]
|
]
|
||||||
assert pages[2].Resources.XObject.fzFrm0.Matrix == [
|
assert pages[2].Resources.XObject.fzFrm0.Matrix == [
|
||||||
"1.4133016",
|
"1.4141413",
|
||||||
"0",
|
"0",
|
||||||
"0",
|
"0",
|
||||||
"1.4133016",
|
"1.4141413",
|
||||||
"117.930667",
|
"117.68077",
|
||||||
"190.19687",
|
"189.9213",
|
||||||
]
|
]
|
||||||
assert pages[3].Resources.XObject.fzFrm0.Matrix == [
|
assert pages[3].Resources.XObject.fzFrm0.Matrix == [
|
||||||
"1.4144558",
|
"1.4141413",
|
||||||
"0",
|
"0",
|
||||||
"0",
|
"0",
|
||||||
"1.4144558",
|
"1.4141413",
|
||||||
"-364.25627",
|
"-364.20893",
|
||||||
"189.93088",
|
"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)
|
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",
|
||||||
|
]
|
||||||
|
|
Loading…
Reference in a new issue