diff --git a/lib/dataStructures.py b/lib/dataStructures.py index 3e0ca23..1db3d28 100644 --- a/lib/dataStructures.py +++ b/lib/dataStructures.py @@ -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] diff --git a/main.py b/main.py index a9024bf..2ef879f 100644 --- a/main.py +++ b/main.py @@ -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,12 +50,11 @@ 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)) - # Write out metadata and sections, as many as can fit on one page - output2img.outputToImage(targetDirectory, song) + # Create subdirectory where we will output our images + targetDirectory = song.outputLocation + "-a4-png" + print("Successfully parsed file. Writing output to '{}'\n".format(targetDirectory)) + # Write out metadata and sections, as many as can fit on one page + output2img.outputToImage(targetDirectory, song) if __name__ == "__main__": main()