Feat: add column ratio to grid_column

This commit is contained in:
Bertrand Benjamin 2022-07-07 06:43:02 +02:00
parent 6378ef5833
commit be80ba6959
2 changed files with 80 additions and 35 deletions

View File

@ -17,52 +17,88 @@ photobook.set_fig_folder("textures")
photobook.one_centered("eugene.jpg", "one_centered default")
photobook.one_centered("eugene.jpg", "")
photobook.one_centered(
"eugene.jpg",
"one_centered text_ratio=1, img_ratio=1",
text_ratio=1,
img_ratio=1
"eugene.jpg", "one_centered text_ratio=1, img_ratio=1", text_ratio=1, img_ratio=1
)
photobook.one_side("eugene.jpg", "one_side default")
photobook.one_side(
"eugene.jpg",
"one_side text_ratio=1, img_ratio=1",
text_ratio=1,
img_ratio=1
"eugene.jpg", "one_side text_ratio=1, img_ratio=1", text_ratio=1, img_ratio=1
)
photobook.one_side(
"eugene.jpg",
"one_side text_ratio=1, img_ratio=1",
text_ratio=1,
img_ratio=1
"eugene.jpg", "one_side text_ratio=1, img_ratio=1", text_ratio=1, img_ratio=1
)
photobook.grid_row([
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([
photobook.grid_row(
[
["eugene.jpg", "eugene.jpg"],
["eugene.jpg", "grid_row\nlayout = \n[[1, 2], [1, 1, 1], [2, 1]]", "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]]
layout=[[1, 2], [1, 1, 1], [2, 1]],
)
photobook.grid_row([
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]
ratios=[1, 3, 1],
)
photobook.grid_row([
[ "eugene.jpg"],
photobook.grid_row(
[
["eugene.jpg"],
["eugene.jpg", "grid_row ratios=[1, 3, 1]", "eugene.jpg"],
[ "eugene.jpg", "eugene.jpg"],
["eugene.jpg", "eugene.jpg"],
],
ratios=[1, 3, 1]
ratios=[1, 3, 1],
)
photobook.grid_column(
[
["eugene.jpg", "eugene.jpg", "eugene.jpg"],
["eugene.jpg", "grid_column default", "eugene.jpg"],
["eugene.jpg", "eugene.jpg", "eugene.jpg"],
],
)
photobook.grid_column(
[
["eugene.jpg", "eugene.jpg"],
[
"eugene.jpg",
"grid_column\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_column(
[
["eugene.jpg", "eugene.jpg", "eugene.jpg"],
["eugene.jpg", "grid_column ratios=[1, 3, 1]", "eugene.jpg"],
["eugene.jpg", "eugene.jpg", "eugene.jpg"],
],
ratios=[1, 3, 1],
)
photobook.grid_column(
[
["eugene.jpg"],
["eugene.jpg", "grid_column ratios=[1, 3, 1]", "eugene.jpg"],
["eugene.jpg", "eugene.jpg"],
],
ratios=[1, 3, 1],
)
photobook.output(dest)

View File

@ -259,11 +259,12 @@ class Photobook(FPDF):
top += row_height + sep
left = ori_left
def grid_column(self, content, layout=[], 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
:param layout: cell layout with weight (need same shape than content)
:param ratios: column's ratio
:param with_margin: Put margins around pictures
:param with_sep: Put separation between pictures
"""
@ -296,19 +297,27 @@ class Photobook(FPDF):
f"Content and Layout need to have same number of columns at column {r}"
)
if ratios == []:
ratios = [1 for c in content]
else:
if len(content) != len(ratios):
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) / len(layout)
width_unit = (pg_size[0] - (len(layout) - 1) * sep) / total_ratio
for (c, column) in enumerate(layout):
height_unit = (pg_size[1] - (len(column) - 1) * sep) / sum(column)
column_width = width_unit * ratios[c]
for (r, weight) in enumerate(column):
dim = (width_unit, height_unit * weight)
dim = (column_width, height_unit * weight)
self.append_content(content[c][r], left, top, *dim)
top += dim[1] + sep
left += width_unit + sep
left += column_width + sep
top = ori_top
def append_content(self, content, left, top, width, height):