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
This commit is contained in:
Marco van Dijk 2021-07-09 17:40:28 +02:00
parent 3487dc14f2
commit 3305efd7d0
3 changed files with 7 additions and 10 deletions

View File

@ -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',

View File

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

View File

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