Added final optimalisation to increase the font size as long as we do not require a new page by doing so

This commit is contained in:
Marco van Dijk 2021-07-09 17:29:41 +02:00
parent 88ef505d50
commit 3487dc14f2
2 changed files with 32 additions and 4 deletions

View File

@ -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)
@ -276,6 +276,30 @@ class Song:
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
While the amount we are short is within X% of the current image height, resize down
@ -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:

View File

@ -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"