rendering unicode using the PDF backend

Hi All,

I am having trouble rendering my Unicode strings in matplotlib using the PDF backend. When I use certain fonts (like the Win 7 default), I get no complaints but the characters are not rendered…. When I use a font like Arial Unicode MS, that I know contains all the chars, then I get the error message below. I did in fact, find a tty file that would work with Chinese (“Microsoft YaHei”), but I would like to avoid trying to map font files to languages. Any info on this subject would be greatly appreciated. Here is a snippet that reproduces the error below… if you remove the fontproperties option to the PYLAB.xlabel() call then the error is avoided but the result is not rendered. Thanks so much!

MJ

import matplotlib.pyplot as PLT

import pylab as PYLAB

from matplotlib.backends.backend_pdf import PdfPages as PDF

import matplotlib.font_manager as fm

fontFile = r’C:\Windows\Fonts\ARIALUNI.TTF’

fp1 = fm.FontProperties(fname=fontFile)

reportFile = r’C:\Temp\TestUnicode.pdf’

pdfOutput = PDF(reportFile)

vals = range(100)

PLT.plot(vals, vals, color = “r”, linestyle = “-”)

mess = u’\u6B63\u5728\u8BFB\u53D6\u6570\u636E…’

PYLAB.xlabel(mess, fontproperties = fp1)

PLT.savefig(pdfOutput, format=‘pdf’)

PLT.close()

pdfOutput.close()

Traceback (most recent call last):

File “C:\Data\CRs\10.1\MemLeak\matplotlib\Scripts\matplotlib_unicode.py”, line 27, in

PLT.savefig(pdfOutput, format=‘pdf’)

File “C:\Python27\lib\site-packages\matplotlib\pyplot.py”, line 472, in savefig

return fig.savefig(*args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\figure.py”, line 1173, in savefig

self.canvas.print_figure(*args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\backend_bases.py”, line 2027, in print_figure

**kwargs)

File “C:\Python27\lib\site-packages\matplotlib\backends\backend_pdf.py”, line 2181, in print_pdf

self.figure.draw(renderer)

File “C:\Python27\lib\site-packages\matplotlib\artist.py”, line 55, in draw_wrapper

draw(artist, renderer, *args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\figure.py”, line 886, in draw

func(*args)

File “C:\Python27\lib\site-packages\matplotlib\artist.py”, line 55, in draw_wrapper

draw(artist, renderer, *args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\axes.py”, line 1983, in draw

a.draw(renderer)

File “C:\Python27\lib\site-packages\matplotlib\artist.py”, line 55, in draw_wrapper

draw(artist, renderer, *args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\axis.py”, line 1054, in draw

self.label.draw(renderer)

File “C:\Python27\lib\site-packages\matplotlib\artist.py”, line 55, in draw_wrapper

draw(artist, renderer, *args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\text.py”, line 587, in draw

ismath=ismath)

File “C:\Python27\lib\site-packages\matplotlib\backends\backend_pdf.py”, line 1784, in draw_text

return draw_text_woven(chunks)

File “C:\Python27\lib\site-packages\matplotlib\backends\backend_pdf.py”, line 1754, in draw_text_woven

glyph_name = font.get_glyph_name(gind)

RuntimeError: Face has no glyph names

PS. I cannot use a different backend.

Hi All,

      I am having trouble rendering my Unicode

strings in matplotlib using the PDF backend. When I use
certain fonts (like the Win 7 default), I get no complaints
but the characters are not rendered…. When I use a font like
Arial Unicode MS, that I know contains all the chars, then I
get the error message below. I did in fact, find a tty file
that would work with Chinese (“Microsoft YaHei”), but I would
like to avoid trying to map font files to languages. Any info
on this subject would be greatly appreciated. Here is a
snippet that reproduces the error below… if you remove the
fontproperties option to the PYLAB.xlabel() call then the
error is avoided but the result is not rendered. Thanks so
much!

MJ

···

import matplotlib.pyplot as PLT

import pylab as PYLAB

      from matplotlib.backends.backend_pdf import

PdfPages as PDF

import matplotlib.font_manager as fm

fontFile = r’C:\Windows\Fonts\ARIALUNI.TTF’

fp1 = fm.FontProperties(fname=fontFile)

reportFile = r’C:\Temp\TestUnicode.pdf’

pdfOutput = PDF(reportFile)

vals = range(100)

      PLT.plot(vals, vals, color = "r", linestyle

= “-”)

      mess =

u’\u6B63\u5728\u8BFB\u53D6\u6570\u636E…’

PYLAB.xlabel(mess, fontproperties = fp1)

PLT.savefig(pdfOutput, format=‘pdf’)

PLT.close()

pdfOutput.close()

Traceback (most recent call last):

        File

“C:\Data\CRs\10.1\MemLeak\matplotlib\Scripts\matplotlib_unicode.py”,
line 27, in

        PLT.savefig(pdfOutput,

format=‘pdf’)

        File

“C:\Python27\lib\site-packages\matplotlib\pyplot.py”, line
472, in savefig

        return fig.savefig(*args,

**kwargs)

        File

“C:\Python27\lib\site-packages\matplotlib\figure.py”, line
1173, in savefig

        self.canvas.print_figure(*args,

**kwargs)

        File

“C:\Python27\lib\site-packages\matplotlib\backend_bases.py”,
line 2027, in print_figure

**kwargs)

        File

“C:\Python27\lib\site-packages\matplotlib\backends\backend_pdf.py”,
line 2181, in print_pdf

self.figure.draw(renderer)

        File

“C:\Python27\lib\site-packages\matplotlib\artist.py”, line
55, in draw_wrapper

        draw(artist, renderer, *args,

**kwargs)

        File

“C:\Python27\lib\site-packages\matplotlib\figure.py”, line
886, in draw

func(*args)

        File

“C:\Python27\lib\site-packages\matplotlib\artist.py”, line
55, in draw_wrapper

        draw(artist, renderer, *args,

**kwargs)

        File

“C:\Python27\lib\site-packages\matplotlib\axes.py”, line
1983, in draw

a.draw(renderer)

        File

“C:\Python27\lib\site-packages\matplotlib\artist.py”, line
55, in draw_wrapper

        draw(artist, renderer, *args,

**kwargs)

        File

“C:\Python27\lib\site-packages\matplotlib\axis.py”, line
1054, in draw

self.label.draw(renderer)

        File

“C:\Python27\lib\site-packages\matplotlib\artist.py”, line
55, in draw_wrapper

        draw(artist, renderer, *args,

**kwargs)

        File

“C:\Python27\lib\site-packages\matplotlib\text.py”, line
587, in draw

ismath=ismath)

        File

“C:\Python27\lib\site-packages\matplotlib\backends\backend_pdf.py”,
line 1784, in draw_text

return draw_text_woven(chunks)

        File

“C:\Python27\lib\site-packages\matplotlib\backends\backend_pdf.py”,
line 1754, in draw_text_woven

        glyph_name =

font.get_glyph_name(gind)

        RuntimeError: Face has no glyph

names

PS. I cannot use a different backend.

I downloaded DejaVu Sans and pointed to that ttf file, and no love L… Again, the default font seems to work for English, French, German, Spanish, Italian other Euros etal… but Chinese and Arabic either come out blank (default font, DejaVu Sans) or choke on the glyphs (MS Unicode).

MJ

···

From: Michael Droettboom [mailto:mdroe@…86…]
Sent: Tuesday, February 07, 2012 6:56 AM
To: matplotlib-users@…1543…rge.net
Subject: Re: [Matplotlib-users] rendering unicode using the PDF backend

It looks like perhaps we’ll need to “synthesize” glyph names if they aren’t provided in the font (though it’s not clear to me why they are not). This will be difficult to test for, as that’s a proprietary font. Have you tried using another Unicode font, such as DejaVu Sans?

Mike

On 02/06/2012 06:53 PM, Mark Janikas wrote:

Hi All,

I am having trouble rendering my Unicode strings in matplotlib using the PDF backend. When I use certain fonts (like the Win 7 default), I get no complaints but the characters are not rendered…. When I use a font like Arial Unicode MS, that I know contains all the chars, then I get the error message below. I did in fact, find a tty file that would work with Chinese (“Microsoft YaHei”), but I would like to avoid trying to map font files to languages. Any info on this subject would be greatly appreciated. Here is a snippet that reproduces the error below… if you remove the fontproperties option to the PYLAB.xlabel() call then the error is avoided but the result is not rendered. Thanks so much!

MJ

import matplotlib.pyplot as PLT

import pylab as PYLAB

from matplotlib.backends.backend_pdf import PdfPages as PDF

import matplotlib.font_manager as fm

fontFile = r’C:\Windows\Fonts\ARIALUNI.TTF’

fp1 = fm.FontProperties(fname=fontFile)

reportFile = r’C:\Temp\TestUnicode.pdf’

pdfOutput = PDF(reportFile)

vals = range(100)

PLT.plot(vals, vals, color = “r”, linestyle = “-”)

mess = u’\u6B63\u5728\u8BFB\u53D6\u6570\u636E…’

PYLAB.xlabel(mess, fontproperties = fp1)

PLT.savefig(pdfOutput, format=‘pdf’)

PLT.close()

pdfOutput.close()

Traceback (most recent call last):

File “C:\Data\CRs\10.1\MemLeak\matplotlib\Scripts\matplotlib_unicode.py”, line 27, in

PLT.savefig(pdfOutput, format='pdf')

File “C:\Python27\lib\site-packages\matplotlib\pyplot.py”, line 472, in savefig

return fig.savefig(*args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\figure.py”, line 1173, in savefig

self.canvas.print_figure(*args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\backend_bases.py”, line 2027, in print_figure

**kwargs)

File “C:\Python27\lib\site-packages\matplotlib\backends\backend_pdf.py”, line 2181, in print_pdf

self.figure.draw(renderer)

File “C:\Python27\lib\site-packages\matplotlib\artist.py”, line 55, in draw_wrapper

draw(artist, renderer, *args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\figure.py”, line 886, in draw

func(*args)

File “C:\Python27\lib\site-packages\matplotlib\artist.py”, line 55, in draw_wrapper

draw(artist, renderer, *args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\axes.py”, line 1983, in draw

a.draw(renderer)

File “C:\Python27\lib\site-packages\matplotlib\artist.py”, line 55, in draw_wrapper

draw(artist, renderer, *args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\axis.py”, line 1054, in draw

self.label.draw(renderer)

File “C:\Python27\lib\site-packages\matplotlib\artist.py”, line 55, in draw_wrapper

draw(artist, renderer, *args, **kwargs)

File “C:\Python27\lib\site-packages\matplotlib\text.py”, line 587, in draw

ismath=ismath)

File “C:\Python27\lib\site-packages\matplotlib\backends\backend_pdf.py”, line 1784, in draw_text

return draw_text_woven(chunks)

File “C:\Python27\lib\site-packages\matplotlib\backends\backend_pdf.py”, line 1754, in draw_text_woven

glyph_name = font.get_glyph_name(gind)

RuntimeError: Face has no glyph names

PS. I cannot use a different backend.

------------------------------------------------------------------------------

Try before you buy = See our experts in action!

The most comprehensive online learning library for Microsoft developers

is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,

Metro Style Apps, more. Free future releases when you subscribe now!

http://p.sf.net/sfu/learndevnow-dev2

_______________________________________________

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Just a quick sanity check, does everything work properly if you render to the screen or to a PNG file? What about EPS?

Ben Root

···

On Tue, Feb 7, 2012 at 12:30 PM, Mark Janikas <mjanikas@…3375…> wrote:

I downloaded DejaVu Sans and pointed to that ttf file, and no love L… Again, the default font seems to work for English, French, German, Spanish, Italian other Euros etal… but Chinese and Arabic either come out blank (default font, DejaVu Sans) or choke on the glyphs (MS Unicode).

MJ

I get perfect output when I use MSUnicode with png and using PYLAB.show()… using DejaVu and the default I get boxes instead of the characters. I attached the two images. So, MSUnicode is the font that my main software uses to render its output, but I get the glyph error when I try to call is using the PDF backend. Thanks so much!

MJ

outputDeja.png

outputMSUni.png

···

From: ben.v.root@…287… [mailto:ben.v.root@…83…287…] On Behalf Of Benjamin Root
Sent: Tuesday, February 07, 2012 10:33 AM
To: Mark Janikas
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] rendering unicode using the PDF backend

On Tue, Feb 7, 2012 at 12:30 PM, Mark Janikas <mjanikas@…3955…> wrote:

I downloaded DejaVu Sans and pointed to that ttf file, and no love L… Again, the default font seems to work for English, French, German, Spanish, Italian other Euros etal… but Chinese and Arabic either come out blank (default font, DejaVu Sans) or choke on the glyphs (MS Unicode).

MJ

Just a quick sanity check, does everything work properly if you render to the screen or to a PNG file? What about EPS?

Ben Root