Fix: add sep * weight-1 for layout

This commit is contained in:
Bertrand Benjamin 2023-11-26 15:26:25 +01:00
parent 8d8db8d3b1
commit b895f0048f

View File

@ -1,7 +1,9 @@
from fpdf import FPDF
from .cropping import cut_save
from pathlib import Path from pathlib import Path
from fpdf import FPDF
from .cropping import cut_save
class Photobook(FPDF): class Photobook(FPDF):
FIG = Path("./fig/") FIG = Path("./fig/")
@ -153,11 +155,15 @@ class Photobook(FPDF):
if p_no % 2 == 1: if p_no % 2 == 1:
self.set_xy(win_dim[0], self.size[1] / 2) self.set_xy(win_dim[0], self.size[1] / 2)
self.multi_cell(self.size[0] * text_ratio/ total_ratio, self.font_size, txt, align="C") self.multi_cell(
self.size[0] * text_ratio / total_ratio, self.font_size, txt, align="C"
)
self.image(img_dest, 0, 0, *win_dim) self.image(img_dest, 0, 0, *win_dim)
else: else:
self.set_xy(0, self.size[1] / 2) self.set_xy(0, self.size[1] / 2)
self.multi_cell(self.size[0] * text_ratio / total_ratio, self.font_size, txt, align="C") self.multi_cell(
self.size[0] * text_ratio / total_ratio, self.font_size, txt, align="C"
)
self.image(img_dest, self.size[0] * text_ratio / total_ratio, 0, *win_dim) self.image(img_dest, self.size[0] * text_ratio / total_ratio, 0, *win_dim)
def one_side_nocut(self, img, txt=""): def one_side_nocut(self, img, txt=""):
@ -230,7 +236,7 @@ class Photobook(FPDF):
else: else:
if len(content) != len(layout): if len(content) != len(layout):
raise ValueError("Content and Layout need to have same number of rows") raise ValueError("Content and Layout need to have same number of rows")
for (r, row) in enumerate(content): for r, row in enumerate(content):
if len(row) != len(layout[r]): if len(row) != len(layout[r]):
raise ValueError( raise ValueError(
f"Content and Layout need to have same number of columns at row {r}" f"Content and Layout need to have same number of columns at row {r}"
@ -247,19 +253,21 @@ class Photobook(FPDF):
left = ori_left left = ori_left
height_unit = (pg_size[1] - (len(layout) - 1) * sep) / total_ratio height_unit = (pg_size[1] - (len(layout) - 1) * sep) / total_ratio
for (r, row) in enumerate(layout): for r, row in enumerate(layout):
width_unit = (pg_size[0] - (sum(row) - 1) * sep) / sum(row) width_unit = (pg_size[0] - (sum(row) - 1) * sep) / sum(row)
row_height = height_unit * ratios[r] row_height = height_unit * ratios[r]
for (c, weight) in enumerate(row): for c, weight in enumerate(row):
dim = (width_unit * weight, row_height) dim = (width_unit * weight + sep * (weight - 1), row_height)
self.append_content(content[r][c], left, top, *dim) self.append_content(content[r][c], left, top, *dim)
left += dim[0] + sep left += dim[0] + sep
top += row_height + sep top += row_height + sep
left = ori_left left = ori_left
def grid_column(self, content, layout=[], ratios=[], with_margin=True, with_sep=True): def grid_column(
self, content, layout=[], ratios=[], with_margin=True, with_sep=True
):
"""Custom layout define by column """Custom layout define by column
:param content: img or text to display in layout's cells :param content: img or text to display in layout's cells
@ -291,7 +299,7 @@ class Photobook(FPDF):
raise ValueError( raise ValueError(
"Content and Layout need to have same number of columns" "Content and Layout need to have same number of columns"
) )
for (r, column) in enumerate(content): for r, column in enumerate(content):
if len(column) != len(layout[r]): if len(column) != len(layout[r]):
raise ValueError( raise ValueError(
f"Content and Layout need to have same number of columns at column {r}" f"Content and Layout need to have same number of columns at column {r}"
@ -301,19 +309,21 @@ class Photobook(FPDF):
ratios = [1 for c in content] ratios = [1 for c in content]
else: else:
if len(content) != len(ratios): if len(content) != len(ratios):
raise ValueError("Content and ratios need to have same number of columns") raise ValueError(
"Content and ratios need to have same number of columns"
)
total_ratio = sum(ratios) total_ratio = sum(ratios)
top = ori_top top = ori_top
left = ori_left left = ori_left
width_unit = (pg_size[0] - (len(layout) - 1) * sep) / total_ratio width_unit = (pg_size[0] - (len(layout) - 1) * sep) / total_ratio
for (c, column) in enumerate(layout): for c, column in enumerate(layout):
height_unit = (pg_size[1] - (sum(column) - 1) * sep) / sum(column) height_unit = (pg_size[1] - (sum(column) - 1) * sep) / sum(column)
column_width = width_unit * ratios[c] column_width = width_unit * ratios[c]
for (r, weight) in enumerate(column): for r, weight in enumerate(column):
dim = (column_width, height_unit * weight) dim = (column_width, height_unit * weight + sep * (weight - 1))
self.append_content(content[c][r], left, top, *dim) self.append_content(content[c][r], left, top, *dim)
top += dim[1] + sep top += dim[1] + sep