From 3487dc14f24608d7ed92dd5888e6a4832591af8c Mon Sep 17 00:00:00 2001 From: Marco van Dijk Date: Fri, 9 Jul 2021 17:29:41 +0200 Subject: [PATCH] Added final optimalisation to increase the font size as long as we do not require a new page by doing so --- lib/dataStructures.py | 32 ++++++++++++++++++++++++++++---- main.py | 4 ++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/dataStructures.py b/lib/dataStructures.py index 1db3d28..d86d702 100644 --- a/lib/dataStructures.py +++ b/lib/dataStructures.py @@ -243,7 +243,7 @@ class Song: @return None """ def resizeAllSections(self, mutator): - print("Resizing font by {} to {}".format(mutator, self.fontSize)) + #print("Resizing font by {} to {}".format(mutator, self.fontSize)) self.fontSize += mutator self.fontLyrics = ImageFont.truetype(self.configObj['lyricfontfamily'], self.fontSize) self.fontTablature = ImageFont.truetype(self.configObj['tablaturefontfamliy'], self.fontSize) @@ -275,6 +275,30 @@ class Song: print("There is an overflow on width: this section has a width of {}, but we have {} ({}-{}-{}) amount of space".format(section.expectedWidth, self.imageWidth - self.leftMargin - self.rightMargin, self.imageWidth, self.leftMargin, self.rightMargin)) return False return True + + """!@brief Checks whether we can increase the font size without creating more pages + @return None + """ + def increaseWhileSameAmountOfPages(self): + targetPageAmount = len(self.pages) + originalFontsize = self.fontSize + self.resizeAllSections(1) + self.sectionsToPages() + currentPageAmount = len(self.pages) + # Increase fontSize as long as we do not add a page + while currentPageAmount <= targetPageAmount: + self.resizeAllSections(+1) + self.sectionsToPages() + currentPageAmount = len(self.pages) + # Now undo latest increase to go back to target page amount + self.resizeAllSections(-1) + self.sectionsToPages() + currentPageAmount = len(self.pages) + if targetPageAmount != currentPageAmount: + print("Oops! While resizing up we changed the amount of pages from {} to {}".format(targetPageAmount, currentPageAmount)) + if self.fontSize != originalFontsize: + print("Managed to change the font size from {} to {}".format(originalFontsize, self.fontSize)) + """!@brief Tries to fill in the whitespace on the current render It will compare the size of existing whitespace with the size of the first section on the next page @@ -304,11 +328,11 @@ class Song: # Sections vary in width, some are very small to begin with # Since (almost empty) lines will result in large whitespace sizes, we are less strict on checking that if biggestWhitespace / self.imageWidth > 0.9: - print("Stopping resizing down, since the smallest section has {}% whitespace on the width of the image".format((smallestWhitespace / self.imageWidth )* 100)) + print("Stopping resizing down, since the smallest section has {}% whitespace on the width of the image".format((biggestWhitespace / self.imageWidth )* 100)) return False # But the largest section on the page should be able to fit at least half of the available page - if smallestWhitespace / self.imageWidth > 0.5: - print("Stopping resizing down, since we largest section has {}% whitespace on the width of the image".format((biggestWhitespace / self.imageWidth )* 100)) + if smallestWhitespace / self.imageWidth > 0.4: + print("Stopping resizing down, since we largest section has {}% whitespace on the width of the image".format((smallestWhitespace / self.imageWidth )* 100)) return False # get first section on next page, if we have a next page to begin with while currentPageIt < amountOfPages - 1: diff --git a/main.py b/main.py index 2ef879f..f85eda7 100644 --- a/main.py +++ b/main.py @@ -49,6 +49,10 @@ def main(): print("Resizing down to fit whitespace more efficiently") song.resizeAllSections(-1) song.sectionsToPages() + # Optimalisation: increase font size as long as the amount of pages does not increase + song.increaseWhileSameAmountOfPages() + # Fit all sections on each page, resizes down if it does not fit on width + song.fitSectionsByWidth() # Parse as PNG a4 # Create subdirectory where we will output our images targetDirectory = song.outputLocation + "-a4-png"