From be80ba69590104597f5c4a189824e5db75a23070 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Thu, 7 Jul 2022 06:43:02 +0200 Subject: [PATCH] Feat: add column ratio to grid_column --- example/album.py | 98 +++++++++++++++++++++++++++++------------- photobook/photobook.py | 17 ++++++-- 2 files changed, 80 insertions(+), 35 deletions(-) diff --git a/example/album.py b/example/album.py index b482230..9e3a810 100644 --- a/example/album.py +++ b/example/album.py @@ -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([ - ["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"], + ["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"], +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]] + 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"], +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"], - ["eugene.jpg", "grid_row ratios=[1, 3, 1]", "eugene.jpg"], - [ "eugene.jpg", "eugene.jpg"], +photobook.grid_row( + [ + ["eugene.jpg"], + ["eugene.jpg", "grid_row ratios=[1, 3, 1]", "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) diff --git a/photobook/photobook.py b/photobook/photobook.py index 59e1dd0..9c7e1ae 100644 --- a/photobook/photobook.py +++ b/photobook/photobook.py @@ -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):