From 6378ef583379108064f3528db3803c469133e7bf Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 6 Jul 2022 18:19:52 +0200 Subject: [PATCH] Feat: add row ration to grid_row --- example/album.py | 28 ++++++++++++++++++++++++++++ photobook/photobook.py | 17 +++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/example/album.py b/example/album.py index ae555c6..b482230 100644 --- a/example/album.py +++ b/example/album.py @@ -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) diff --git a/photobook/photobook.py b/photobook/photobook.py index 2215ec7..59e1dd0 100644 --- a/photobook/photobook.py +++ b/photobook/photobook.py @@ -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):