From 6e6b02aa26f52e5cf67b5fb24ef4be818c4d8de4 Mon Sep 17 00:00:00 2001 From: Aleksander Majda Date: Mon, 21 Apr 2025 17:38:59 +0200 Subject: [PATCH] fix text position calculation --- text_to_image.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/text_to_image.py b/text_to_image.py index af7e886..9c63b1e 100644 --- a/text_to_image.py +++ b/text_to_image.py @@ -100,7 +100,7 @@ class BookToolsTextToImage: # Fallback to simple width calculation return sum(font.getbbox(char)[2] - font.getbbox(char)[0] for char in text) - def calculate_text_size(self, text, font_size, font_path, max_width, max_height, line_height_factor): + def calculate_text_size(self, text, font_size, font_path, max_width, max_height, line_height_factor, char_spacing_factor): try: font = ImageFont.truetype(font_path, font_size) except Exception as e: @@ -109,19 +109,18 @@ class BookToolsTextToImage: paragraphs = text.split('\n') lines = [] - max_line_width = 0 - + + # compute pixel spacing from factor + char_spacing = font_size * char_spacing_factor + for paragraph in paragraphs: words = paragraph.split() current_line = [] current_width = 0 - # Consider character spacing in the width calculation - char_spacing = font_size * 0.2 # Default char_spacing from parameters - for word in words: - # Get word width including spacing between characters + # measure word width using char_spacing word_width = 0 for char in word: bbox = font.getbbox(char) @@ -434,7 +433,7 @@ class BookToolsTextToImage: # First check if min_font_size fits lines, fits, actual_width, actual_height = self.calculate_text_size( - text, min_font_size, font, effective_width, effective_height, line_height_factor + text, min_font_size, font, effective_width, effective_height, line_height_factor, char_spacing ) if fits: @@ -442,7 +441,7 @@ class BookToolsTextToImage: while low <= high: mid = (low + high) // 2 lines, fits, actual_width, actual_height = self.calculate_text_size( - text, mid, font, effective_width, effective_height, line_height_factor + text, mid, font, effective_width, effective_height, line_height_factor, char_spacing ) if fits: