[reportlab-users] Using TTFs with non-ASCII characters

Marius Gedminas reportlab-users@reportlab.com
Wed, 3 Jul 2002 11:42:22 +0200


On Wed, Jul 03, 2002 at 11:14:31AM +0200, Dinu Gherman wrote:
> Gherman <gherman@darwin.in-berlin.de>:
> 
> > > Fixed:
> > 
> > Works great! I've updated my sample file:
> 
> BTW, what is the canonical way to make non-ASCII characters 
> appear as expected in strings "automatically"?

Use UTF-8. ;)

> I've tried
> using something like this:
> 
>   c.drawString(100, y, u"?")

Unicode strings are not present in Python 1.5.2.  With 2.0 or later you
can do

  c.drawString(100, y, any_unicode_string.encode("UTF-8"))

I'm not sure what happens if you write u"text in some 8-bit encoding" --
what encoding will be chosen by Python interpret those 8-bit characters?
To be safe you could write

  c.drawString(100, y, unicode("Latin-1 text", "ISO-8859-1").encode("UTF-8"))

It might be easier to use UTF-8 directly.

I wonder... is there a way to support Unicode string literals on Python
2+ in a way that's compatible with Python 1.5.2?  Basically you'd need
to change parse_utf8() in ttfonts.py to something like

  if type(string) is UnicodeType:
      return map(ord, string)

and above that declare

  try:
    from types import UnicodeType
  except:
    # Old Python has no Unicode strings
    UnicodeType = None

Not tested, but you get the idea.  Then you could just pass Unicode
strings to drawString if you have Python 2+.

(Platypus would need a couple of additional lines to cope with Unicode
strings.)


Marius Gedminas
-- 
Most security experts REALLY believe in firewalls. The expect that, when they
die, arrive at the great firewall in the sky where Saint Peter is running a
default policy of REJECT.
		--- Sander Plomp