Fix rendering of non ascii characters in generated masthead images when downloading news for the Kindle

This commit is contained in:
Kovid Goyal 2011-12-28 09:10:01 +05:30
parent 1b9f2c2fa3
commit 502575375b

View File

@ -9,7 +9,7 @@ from various formats.
import traceback, os, re import traceback, os, re
from cStringIO import StringIO from cStringIO import StringIO
from calibre import CurrentDir from calibre import CurrentDir, force_unicode
class ConversionError(Exception): class ConversionError(Exception):
@ -237,10 +237,10 @@ def generate_masthead(title, output_path=None, width=600, height=60):
img = Image.new('RGB', (width, height), 'white') img = Image.new('RGB', (width, height), 'white')
draw = ImageDraw.Draw(img) draw = ImageDraw.Draw(img)
try: try:
font = ImageFont.truetype(font_path, 48) font = ImageFont.truetype(font_path, 48, encoding='unic')
except: except:
font = ImageFont.truetype(default_font, 48) font = ImageFont.truetype(default_font, 48, encoding='unic')
text = title.encode('utf-8') if isinstance(title, unicode) else title text = force_unicode(title)
width, height = draw.textsize(text, font=font) width, height = draw.textsize(text, font=font)
left = max(int((width - width)/2.), 0) left = max(int((width - width)/2.), 0)
top = max(int((height - height)/2.), 0) top = max(int((height - height)/2.), 0)