[reportlab-users] Using accented chars with TTF?
Tue, 10 Sep 2002 09:22:20 +0200
On Tue, Sep 10, 2002 at 08:53:51AM +0200, Dinu Gherman wrote:
> I'm trying to use TTF fonts with Platypus where I found a little
> surprise: I can see accented characters after converting my text
> input to UTF-8, but these are prefixed by the logical NOT charac-
> ter. See attached PDF sample and generator file. Can anybody ex-
> plain this, or, even better, suggest how to get rid of them?
That would be the case if you fed UTF-8 text to a traditional Reportlab
font which expects an 8-bit encoding.
> # rinatest1.py
> from reportlab.lib.styles import ParagraphStyle
> from reportlab.lib.fonts import addMapping
> from reportlab.pdfbase import pdfmetrics
> from reportlab.pdfbase.ttfonts import TTFont
> from reportlab.platypus.doctemplate import SimpleDocTemplate
> from reportlab.platypus.paragraph import Paragraph
> pdfmetrics.registerFont(TTFont('Rina', 'Rina.ttf'))
> for i in (0, 1):
> for j in (0, 1):
> addMapping('Rina', i, j, 'Rina')
(Is that allowed now? I used to get errors when I tried to map normal
and bold versions into the same PS font.)
> def latin1toUtf8(s):
> return unicode(s, "iso-8859-1").encode("utf-8")
> normal = ParagraphStyle('normal')
normal.fontName = 'Rina'
here to prevent that.
> p = Paragraph(latin1toUtf8('Andr�'), normal)
This is not Latin-1. Characters in range 0x80-0x9F are non-printable
there. This is probably Mac-Roman? If so, you should use something
p = Paragraph(mac2utf8('Andr\216'), normal)
where mac2utf8 is defined as
return unicode(s, "Mac-Roman").encode("utf-8")
(And this time I actually tested that my solution works ;)
Never, ever expect hackers to be able to read closed proprietary document
formats like Microsoft Word. Most hackers react to these about as well as you
would to having a pile of steaming pig manure dumped on your doorstep.
-- ESR (http://www.tuxedo.org/~esr/faqs/smart-questions.html)