From b895f0048f3ebbf9f57869636b54327777ba25a1 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 26 Nov 2023 15:26:25 +0100 Subject: [PATCH] Fix: add sep * weight-1 for layout --- photobook/photobook.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/photobook/photobook.py b/photobook/photobook.py index 5c4c389..c3dbac4 100644 --- a/photobook/photobook.py +++ b/photobook/photobook.py @@ -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