Added checks to stop resizing down if we are creating too much widespace or if the font has decreased too much

This commit is contained in:
Marco van Dijk 2021-07-09 17:04:17 +02:00
parent 682278fcd5
commit 88ef505d50
2 changed files with 33 additions and 6 deletions

View File

@ -211,6 +211,7 @@ class Song:
self.rightMargin = int(configObj['rightMargin'])
self.fontMetadata = ImageFont.truetype(configObj['metafontfamily'], int(configObj['metaFontWeight']))
self.fontSize = int(configObj['songFontWeight'])
self.originalFontSize = int(configObj['songFontWeight'])
self.fontLyrics = ImageFont.truetype(configObj['lyricfontfamily'], self.fontSize)
self.fontTablature = ImageFont.truetype(configObj['tablaturefontfamliy'], self.fontSize)
self.configObj = configObj
@ -285,6 +286,30 @@ class Song:
currentPageIt = 0
if not amountOfPages:
return False
# Stop resizing if the font size is becoming too small
fontDifference = self.originalFontSize - self.fontSize
if fontDifference > self.originalFontSize / 2:
print("The original fontSize has already decreased from {} to {}, so we will stop resizing the image down".format(self.originalFontSize, self.fontSize))
return False
# Stop resizing if we are creating too much widespace on the width
smallestWhitespace = self.imageHeight
biggestWhitespace = -1
for page in self.pages:
for section in page.sections:
whitespaceOnWidth = self.imageWidth - self.leftMargin - self.rightMargin - section.expectedWidth
if whitespaceOnWidth < smallestWhitespace:
smallestWhitespace = whitespaceOnWidth
if whitespaceOnWidth > biggestWhitespace:
biggestWhitespace = whitespaceOnWidth
# 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))
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))
return False
# get first section on next page, if we have a next page to begin with
while currentPageIt < amountOfPages - 1:
curPage = self.pages[currentPageIt]

View File

@ -37,6 +37,9 @@ def main():
print("Start parsing of file '{}'...".format(song.inputFile))
# Initialise internal data structures
song.initSections()
if not song.isParsed:
print("Song was not initialized correctly. Skipping...")
continue
# Fit all sections on each page, resizes down if it does not fit on width
song.fitSectionsByWidth()
# Prerender: calculate Pages, and move sections into Pages
@ -47,7 +50,6 @@ def main():
song.resizeAllSections(-1)
song.sectionsToPages()
# Parse as PNG a4
if song.isParsed:
# Create subdirectory where we will output our images
targetDirectory = song.outputLocation + "-a4-png"
print("Successfully parsed file. Writing output to '{}'\n".format(targetDirectory))