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
1 changed files with 24 additions and 14 deletions

View File

@ -1,7 +1,9 @@
from fpdf import FPDF
from .cropping import cut_save
from pathlib import Path
from fpdf import FPDF
from .cropping import cut_save
class Photobook(FPDF):
FIG = Path("./fig/")
@ -153,11 +155,15 @@ class Photobook(FPDF):
if p_no % 2 == 1:
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)
else:
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)
def one_side_nocut(self, img, txt=""):
@ -230,7 +236,7 @@ class Photobook(FPDF):
else:
if len(content) != len(layout):
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]):
raise ValueError(
f"Content and Layout need to have same number of columns at row {r}"
@ -247,19 +253,21 @@ class Photobook(FPDF):
left = ori_left
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)
row_height = height_unit * ratios[r]
for (c, weight) in enumerate(row):
dim = (width_unit * weight, row_height)
for c, weight in enumerate(row):
dim = (width_unit * weight + sep * (weight - 1), row_height)
self.append_content(content[r][c], left, top, *dim)
left += dim[0] + sep
top += row_height + sep
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
:param content: img or text to display in layout's cells
@ -291,7 +299,7 @@ class Photobook(FPDF):
raise ValueError(
"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]):
raise ValueError(
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]
else:
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)
top = ori_top
left = ori_left
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)
column_width = width_unit * ratios[c]
for (r, weight) in enumerate(column):
dim = (column_width, height_unit * weight)
for r, weight in enumerate(column):
dim = (column_width, height_unit * weight + sep * (weight - 1))
self.append_content(content[c][r], left, top, *dim)
top += dim[1] + sep