mirror of
https://github.com/stronk-dev/Guitar-Sheet-Parser.git
synced 2025-07-05 08:25:09 +02:00
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:
parent
3487dc14f2
commit
3305efd7d0
@ -37,7 +37,6 @@ def initConfig():
|
|||||||
'metaFontWeight': 16,
|
'metaFontWeight': 16,
|
||||||
'lyricfontfamily': 'fonts/CourierPrime-Regular.ttf',
|
'lyricfontfamily': 'fonts/CourierPrime-Regular.ttf',
|
||||||
'tablaturefontfamliy': 'fonts/CourierPrime-Bold.ttf',
|
'tablaturefontfamliy': 'fonts/CourierPrime-Bold.ttf',
|
||||||
'songFontWeight': 32,
|
|
||||||
'imageppi': 144,
|
'imageppi': 144,
|
||||||
'backgroundColour': '255,255,255',
|
'backgroundColour': '255,255,255',
|
||||||
'fontColour': '0,0,0',
|
'fontColour': '0,0,0',
|
||||||
|
@ -207,10 +207,13 @@ class Song:
|
|||||||
# 0.03937 pixels per minimeter per ppi
|
# 0.03937 pixels per minimeter per ppi
|
||||||
self.imageWidth = int(self.ppi * A4['width'] * 0.03937)
|
self.imageWidth = int(self.ppi * A4['width'] * 0.03937)
|
||||||
self.imageHeight = int(self.ppi * A4['height'] * 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.leftMargin = int(configObj['leftMargin'])
|
||||||
self.rightMargin = int(configObj['rightMargin'])
|
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.originalFontSize = int(configObj['songFontWeight'])
|
||||||
self.fontLyrics = ImageFont.truetype(configObj['lyricfontfamily'], self.fontSize)
|
self.fontLyrics = ImageFont.truetype(configObj['lyricfontfamily'], self.fontSize)
|
||||||
self.fontTablature = ImageFont.truetype(configObj['tablaturefontfamliy'], self.fontSize)
|
self.fontTablature = ImageFont.truetype(configObj['tablaturefontfamliy'], self.fontSize)
|
||||||
@ -286,7 +289,7 @@ class Song:
|
|||||||
self.sectionsToPages()
|
self.sectionsToPages()
|
||||||
currentPageAmount = len(self.pages)
|
currentPageAmount = len(self.pages)
|
||||||
# Increase fontSize as long as we do not add a page
|
# Increase fontSize as long as we do not add a page
|
||||||
while currentPageAmount <= targetPageAmount:
|
while currentPageAmount <= targetPageAmount and self.checkOverflowX():
|
||||||
self.resizeAllSections(+1)
|
self.resizeAllSections(+1)
|
||||||
self.sectionsToPages()
|
self.sectionsToPages()
|
||||||
currentPageAmount = len(self.pages)
|
currentPageAmount = len(self.pages)
|
||||||
@ -312,9 +315,6 @@ class Song:
|
|||||||
return False
|
return False
|
||||||
# Stop resizing if the font size is becoming too small
|
# Stop resizing if the font size is becoming too small
|
||||||
fontDifference = self.originalFontSize - self.fontSize
|
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
|
# Stop resizing if we are creating too much widespace on the width
|
||||||
smallestWhitespace = self.imageHeight
|
smallestWhitespace = self.imageHeight
|
||||||
biggestWhitespace = -1
|
biggestWhitespace = -1
|
||||||
|
4
main.py
4
main.py
@ -49,10 +49,8 @@ def main():
|
|||||||
print("Resizing down to fit whitespace more efficiently")
|
print("Resizing down to fit whitespace more efficiently")
|
||||||
song.resizeAllSections(-1)
|
song.resizeAllSections(-1)
|
||||||
song.sectionsToPages()
|
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()
|
song.increaseWhileSameAmountOfPages()
|
||||||
# Fit all sections on each page, resizes down if it does not fit on width
|
|
||||||
song.fitSectionsByWidth()
|
|
||||||
# Parse as PNG a4
|
# Parse as PNG a4
|
||||||
# Create subdirectory where we will output our images
|
# Create subdirectory where we will output our images
|
||||||
targetDirectory = song.outputLocation + "-a4-png"
|
targetDirectory = song.outputLocation + "-a4-png"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user