From 3305efd7d02d6a71ff4f371501b6ed0fa2c825ff Mon Sep 17 00:00:00 2001 From: Marco van Dijk Date: Fri, 9 Jul 2021 17:40:28 +0200 Subject: [PATCH] Set font size based on PPI In combination with the dynamic up/down sizing to fit whitespace, we get pretty good output images Also removed the check that we do not resize the font size that much fixes #13 --- lib/config.py | 1 - lib/dataStructures.py | 12 ++++++------ main.py | 4 +--- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/config.py b/lib/config.py index 8ad338b..b7d0a60 100644 --- a/lib/config.py +++ b/lib/config.py @@ -37,7 +37,6 @@ def initConfig(): 'metaFontWeight': 16, 'lyricfontfamily': 'fonts/CourierPrime-Regular.ttf', 'tablaturefontfamliy': 'fonts/CourierPrime-Bold.ttf', - 'songFontWeight': 32, 'imageppi': 144, 'backgroundColour': '255,255,255', 'fontColour': '0,0,0', diff --git a/lib/dataStructures.py b/lib/dataStructures.py index d86d702..c3ba26c 100644 --- a/lib/dataStructures.py +++ b/lib/dataStructures.py @@ -207,10 +207,13 @@ class Song: # 0.03937 pixels per minimeter per ppi self.imageWidth = int(self.ppi * A4['width'] * 0.03937) self.imageHeight = int(self.ppi * A4['height'] * 0.03937) + # With a PPI of 72, a font size of 14-18 is a good starting point (PPI / 4 or 4.5) + # Since font size is then shrunk and grown to fit whitespace we do not need to be as accurate + # PPI of 144 -> fontSize of 32 + self.fontSize = int(self.ppi / 4.5) + self.fontMetadata = ImageFont.truetype(configObj['metafontfamily'], int(configObj['metaFontWeight'])) self.leftMargin = int(configObj['leftMargin']) 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) @@ -286,7 +289,7 @@ class Song: self.sectionsToPages() currentPageAmount = len(self.pages) # Increase fontSize as long as we do not add a page - while currentPageAmount <= targetPageAmount: + while currentPageAmount <= targetPageAmount and self.checkOverflowX(): self.resizeAllSections(+1) self.sectionsToPages() currentPageAmount = len(self.pages) @@ -312,9 +315,6 @@ class Song: 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 diff --git a/main.py b/main.py index f85eda7..3452e16 100644 --- a/main.py +++ b/main.py @@ -49,10 +49,8 @@ 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 + # Optimalisation: increase font size as long as the amount of pages does not increase or we cause an overflow on width 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"