[reportlab-users] TTF, Platypus & Unicode...
Dinu Gherman
reportlab-users@reportlab.com
Tue, 30 Jul 2002 11:23:19 +0200
--Apple-Mail-1-321675668
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=ISO-8859-1;
format=flowed
Hi,
I'm trying to reuse TrueType fonts in Platypus and find myself
diving deep into Unicode adventures. Can someone explain how to
convert what I suppose to be Latin-1 into UTF-8 (I thought that
would be the same, but maybe not for TrueType fonts...)?
I'm attaching a sample generator script that should print "To your
sant=E9!" with an accented e, but the e is just invisible...
Thanks,
Dinu
--Apple-Mail-1-321675668
Content-Disposition: attachment;
filename=ttfunicode.py
Content-Transfer-Encoding: quoted-printable
Content-Type: application/text;
x-mac-creator=522A6368;
x-unix-mode=0644;
x-mac-type=54455854;
name="ttfunicode.py"
#!=20/usr/bin/env=20python=0A=0A=0Aimport=20os,=20string,=20sys=0A=0A=
from=20reportlab.lib.units=20import=20cm=0Afrom=20=
reportlab.lib.pagesizes=20import=20A4=0Afrom=20reportlab.lib.styles=20=
import=20getSampleStyleSheet=0Afrom=20reportlab.pdfgen=20import=20canvas=0A=
from=20reportlab.platypus.paragraph=20import=20Paragraph=0Afrom=20=
reportlab.platypus.frames=20import=20Frame=0Afrom=20=
reportlab.platypus.doctemplate=20\=0A=20=20=20=20import=20PageTemplate,=20=
BaseDocTemplate=0A=0Afrom=20reportlab.pdfbase=20import=20pdfmetrics=0A=
from=20reportlab.lib.fonts=20import=20addMapping=0Afrom=20=
reportlab.pdfbase.ttfonts=20import=20TTFont=0A=0A=0A=0A=0Adef=20=
mainPageFrame(canvas,=20doc):=0A=20=20=20=20"The=20page=20frame=20used=20=
for=20all=20PDF=20documents."=0A=20=20=20=20=0A=20=20=20=20=
canvas.saveState()=0A=20=20=20=20canvas.setFont('Helvetica',=208)=0A=20=20=
=20=20canvas.restoreState()=0A=20=20=20=20=0A=20=20=20=20=0A=20=20=20=20=0A=
=0Aclass=20MyTemplate(BaseDocTemplate):=0A=20=20=20=20"The=20document=20=
template=20used=20for=20all=20PDF=20documents."=0A=20=20=20=20=0A=20=20=20=
=20_invalidInitArgs=20=3D=20('pageTemplates',)=0A=20=20=20=20=0A=20=20=20=
=20def=20__init__(self,=20filename,=20**kw):=0A=20=20=20=20=20=20=20=20=
f1=20=3D=20Frame(2*cm,=202*cm,=2017*cm,=2025*cm,=20id=3D'col1')=0A=20=20=20=
=20=20=20=20=20apply(BaseDocTemplate.__init__,=20(self,=20filename),=20=
kw)=0A=20=20=20=20=20=20=20=20self.showBoundary=20=3D=201=0A=20=20=20=20=20=
=20=20=20pt=20=3D=20PageTemplate('PT',=20[f1],=20mainPageFrame)=0A=20=20=20=
=20=20=20=20=20self.addPageTemplates(pt)=0A=0A=0A=0A=0Adef=20=
fixString0(s):=0A=20=20=20=20d=20=3D=20{'=9A':=20'\xc3\xb6',=20'=8A':=20=
'\xc3\xa4',=20'=9F':=20'\xc3\xbc',=20=0A=20=20=20=20=20=20=20=20=20'=85':=20=
'\xc3\x96',=20'=80':=20'\xc3\x84',=20'=86':=20'\xc3\x9c',=20=0A=20=20=20=20=
=20=20=20=20=20'=A7':=20'\xc3\x9f'}=0A=20=20=20=20keys=20=3D=20d.keys()=0A=
=20=20=20=20for=20k=20in=20keys:=0A=20=20=20=20=20=20=20=20s=20=3D=20=
s.replace(k,=20d[k])=0A=0A=20=20=20=20return=20s=0A=20=20=20=20=0A=20=20=20=
=20=0A=20=20=20=20=0Adef=20fixString(s):=0A=20=20=20=20return=20=
s.decode('latin-1').encode('utf-8')=0A=20=20=20=20=0A=20=20=20=20=0A=20=20=
=20=20=0A=0Adef=20main():=0A=20=20=20=20story=20=3D=20[]=0A=20=20=20=20=0A=
=20=20=20=20font=20=3D=20TTFont('Rina',=20'rina.ttf')=0A=20=20=20=20=
pdfmetrics.registerFont(font)=0A=20=20=20=20addMapping('Rina',=200,=200,=20=
'Rina')=20#=20normal=0A=20=20=20=20addMapping('Rina',=200,=201,=20=
'Rina')=20#=20italic=0A=20=20=20=20addMapping('Rina',=201,=200,=20=
'Rina')=20#=20bold=0A=20=20=20=20addMapping('Rina',=201,=201,=20'Rina')=20=
#=20italic=20and=20bold=0A=0A=20=20=20=20styleSheet=20=3D=20=
getSampleStyleSheet()=0A=20=20=20=20bt=20=3D=20styleSheet['BodyText']=0A=20=
=20=20=20bt.fontName=20=3D=20'Rina'=0A=20=20=20=20bt.fontSize=20=3D=2028=0A=
=20=20=20=20bt.leading=20=3D=2034=0A=20=20=20=20=0A=20=20=20=20text=20=3D=20=
"To=20your=20sant%s!=20"=20%=20chr(142)=20=20#=20e=20accent=20aigu=0A=20=20=
=20=20text=20=3D=20fixString(text)=0A=20=20=20=20text=20=3D=20text=20*=20=
30=0A=20=20=20=20p=20=3D=20Paragraph(text,=20bt)=0A=20=20=20=20=
story.append(p)=0A=0A=20=20=20=20path=20=3D=20'ttfunicode.pdf'=0A=20=20=20=
=20doc=20=3D=20MyTemplate(path)=0A=20=20=20=20doc.build(story)=0A=0A=0A=0A=
=0Aif=20__name__=20=3D=3D=20'__main__':=0A=20=20=20=20main()=0A=20=20=20=20=
--Apple-Mail-1-321675668--