Feat: add row ration to grid_row

This commit is contained in:
Bertrand Benjamin 2022-07-06 18:19:52 +02:00
parent 1dbec31687
commit 6378ef5833
2 changed files with 41 additions and 4 deletions

View File

@ -37,4 +37,32 @@ photobook.one_side(
img_ratio=1
)
photobook.grid_row([
["eugene.jpg", "eugene.jpg", "eugene.jpg"],
["eugene.jpg", "grid_row default", "eugene.jpg"],
["eugene.jpg", "eugene.jpg", "eugene.jpg"],
],
)
photobook.grid_row([
["eugene.jpg", "eugene.jpg"],
["eugene.jpg", "grid_row\nlayout = \n[[1, 2], [1, 1, 1], [2, 1]]", "eugene.jpg"],
["eugene.jpg", "eugene.jpg"],
],
layout = [[1, 2], [1, 1, 1], [2, 1]]
)
photobook.grid_row([
["eugene.jpg", "eugene.jpg", "eugene.jpg"],
["eugene.jpg", "grid_row ratios=[1, 3, 1]", "eugene.jpg"],
["eugene.jpg", "eugene.jpg", "eugene.jpg"],
],
ratios=[1, 3, 1]
)
photobook.grid_row([
[ "eugene.jpg"],
["eugene.jpg", "grid_row ratios=[1, 3, 1]", "eugene.jpg"],
[ "eugene.jpg", "eugene.jpg"],
],
ratios=[1, 3, 1]
)
photobook.output(dest)

View File

@ -200,11 +200,12 @@ class Photobook(FPDF):
# self.image(img_dest, left, top, *win_dim)
top += win_dim[1] + sep
def grid_row(self, content, layout=[], with_margin=True, with_sep=True):
def grid_row(self, content, layout=[], ratios=[], with_margin=True, with_sep=True):
"""Custom layout define by rows
:param content: img or text to display in layout's cells
:param layout: cell layout with weight (need same shape than content)
:param ratios: row's ratio
:param with_margin: Put margins around pictures
:param with_sep: Put separation between pictures
"""
@ -235,19 +236,27 @@ class Photobook(FPDF):
f"Content and Layout need to have same number of columns at row {r}"
)
if ratios == []:
ratios = [1 for row in content]
else:
if len(content) != len(ratios):
raise ValueError("Content and ratios need to have same number of rows")
total_ratio = sum(ratios)
top = ori_top
left = ori_left
height_unit = (pg_size[1] - (len(layout) - 1) * sep) / len(layout)
height_unit = (pg_size[1] - (len(layout) - 1) * sep) / total_ratio
for (r, row) in enumerate(layout):
width_unit = (pg_size[0] - (len(row) - 1) * sep) / sum(row)
row_height = height_unit * ratios[r]
for (c, weight) in enumerate(row):
dim = (width_unit * weight, height_unit)
dim = (width_unit * weight, row_height)
self.append_content(content[r][c], left, top, *dim)
left += dim[0] + sep
top += height_unit + sep
top += row_height + sep
left = ori_left
def grid_column(self, content, layout=[], with_margin=True, with_sep=True):