View Single Post
Old 12-27-2011, 03:57 PM   #8
pietvo
Reader
pietvo can name that song in three notespietvo can name that song in three notespietvo can name that song in three notespietvo can name that song in three notespietvo can name that song in three notespietvo can name that song in three notespietvo can name that song in three notespietvo can name that song in three notespietvo can name that song in three notespietvo can name that song in three notespietvo can name that song in three notes
 
pietvo's Avatar
 
Posts: 520
Karma: 24612
Join Date: Aug 2009
Location: Utrecht, NL
Device: Kobo Aura 2, iPhone, iPad
I don't know the reason the code is there. Maybe there used to be a reason which is no longer valid.

And u'La Raz\xc3\xb3n' is a double encoding, it is an not an ascii representation of u'La Razón'. That would be u'La Raz\xf3n', which is probably what you meant. What you have written is a utf-8 encoding of ó, and that put in an ascii representation in a Unicode string. Putting utf-8 bytes in a Unicode string is most of the times wrong. I I print that string it outputs La Razón, which is exactly the text I got in the masthead image, showing that the utf-8 encoding that the code does should not be done. And the parser didn't parse it incorrectly because I had a # -*- coding: utf-8 -*- line and saved the file in utf-8. To safeguard against source code problems indeed \x3f could be used but then the title.encode('utf-8') would still cause the wrong rendering.

Fredrik Lundh, the author of PIL also says that the text can be a Unicode string if the font you use supports Unicode. Here is an example the you can try to see that it works.

Quote:
# -*- coding: utf-8 -*-
import ImageFont, Image, ImageDraw
s = u'La Razón € ñ'
font = ImageFont.truetype('/System/Library/Fonts/LucidaGrande.ttc', 18, encoding='unic')
print font.getsize(s)
im = Image.new('RGB', (200,200))
draw = ImageDraw.Draw(im)
draw.text((40,40), s, font=font)
im.show()
pietvo is offline   Reply With Quote