matplotlib font in cairo

Hallo,

I'm try to use MathTextParser output for cairo in my wxpython gui. Works well under ubunutu linux (maybe dublicated font) but fails under windows (screenshot: http://www.ubuntu-pics.de/bild/62985/auswahl_017_RC86gk.png ).

How to tell cairo to use the font file from mpl-data?

Here some code:

from matplotlib.mathtext import MathTextParser
from matplotlib.font_manager import ttfFontProperty
import wx
import wx.lib.wxcairo
import cairo

#...
MPC = MathTextParser("Cairo")
dc = wx.BufferedPaintDC(self)
ctx = wx.lib.wxcairo.ContextFromDC(dc)
#...
width, height, descent, glyphs, rects = self.mathtext_parser.parse(r"$a_0+a_1\xi+a_2\xi^2+a_3\xi^3$",dpi,prop)
#...

for font, fontsize, s, ox, oy in glyphs:
   #...
   fontProp = ttfFontProperty(font)
   #...
   ctx.select_font_face (fontProp.name,
                         self.fontangles [fontProp.style],
                         self.fontweights[fontProp.weight])

   #...
   ctx.show_text(s.encode("utf-8"))
   #...
#...

greetz

Frank

We went through this a few years ago on the Cairo mailing list.

The short answer is you need to copy the fonts from mpl-data to C:\Windows\Fonts to correctly use Cairo.

The long answer: Cairo uses fontconfig for font lookup, which by default searches the system font directory (C:\Windows\Fonts). There is an fontconfig API to add another font directory to search, but it is not exposed to Python. The cairo guys don't want to expose it in pycairo because that API is not technically part of cairo, but there isn't a proper Python wrapper to fontconfig, so until one is created we're sort of left with our hands tied.

This is probably worthy of a FAQ in the docs.

Mike

copyrights@...380... wrote:

···

Hallo,

I'm try to use MathTextParser output for cairo in my wxpython gui. Works well under ubunutu linux (maybe dublicated font) but fails under windows (screenshot: http://www.ubuntu-pics.de/bild/62985/auswahl_017_RC86gk.png ).

How to tell cairo to use the font file from mpl-data?

Here some code:

from matplotlib.mathtext import MathTextParser
from matplotlib.font_manager import ttfFontProperty
import wx
import wx.lib.wxcairo
import cairo

#...
MPC = MathTextParser("Cairo")
dc = wx.BufferedPaintDC(self)
ctx = wx.lib.wxcairo.ContextFromDC(dc)
#...
width, height, descent, glyphs, rects = self.mathtext_parser.parse(r"$a_0+a_1\xi+a_2\xi^2+a_3\xi^3$",dpi,prop)
#...

for font, fontsize, s, ox, oy in glyphs:
   #...
   fontProp = ttfFontProperty(font)
   #...
   ctx.select_font_face (fontProp.name,
                         self.fontangles [fontProp.style],
                         self.fontweights[fontProp.weight])

   #...
   ctx.show_text(s.encode("utf-8"))
   #...
#...

greetz

Frank

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

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
  
--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

I've found a little work-a-round to get my custom font.

Here it is:

from ctypes import windll
gdi32 = windll.gdi32

FR_PRIVATE = 0x10

gdi32.AddFontResourceExA(FILENAME_OF_TTF,FR_PRIVATE,0)

greetz

Frank

···

Am 18.05.2010 16:09, schrieb Michael Droettboom:

We went through this a few years ago on the Cairo mailing list.
The short answer is you need to copy the fonts from mpl-data to
C:\Windows\Fonts to correctly use Cairo.

The long answer: Cairo uses fontconfig for font lookup, which by default
searches the system font directory (C:\Windows\Fonts). There is an
fontconfig API to add another font directory to search, but it is not
exposed to Python. The cairo guys don't want to expose it in pycairo
because that API is not technically part of cairo, but there isn't a
proper Python wrapper to fontconfig, so until one is created we're sort
of left with our hands tied.

This is probably worthy of a FAQ in the docs.

Mike

copyrights@...380... wrote:

Hallo,

I'm try to use MathTextParser output for cairo in my wxpython gui.
Works well under ubunutu linux (maybe dublicated font) but fails under
windows (screenshot:
http://www.ubuntu-pics.de/bild/62985/auswahl_017_RC86gk.png ).

How to tell cairo to use the font file from mpl-data?

Here some code:

from matplotlib.mathtext import MathTextParser
from matplotlib.font_manager import ttfFontProperty
import wx
import wx.lib.wxcairo
import cairo

#...
MPC = MathTextParser("Cairo")
dc = wx.BufferedPaintDC(self)
ctx = wx.lib.wxcairo.ContextFromDC(dc)
#...
width, height, descent, glyphs, rects =
self.mathtext_parser.parse(r"$a_0+a_1\xi+a_2\xi^2+a_3\xi^3$",dpi,prop)
#...

for font, fontsize, s, ox, oy in glyphs:
   #...
   fontProp = ttfFontProperty(font)
   #...
   ctx.select_font_face (fontProp.name,
                         self.fontangles [fontProp.style],
                         self.fontweights[fontProp.weight])

   #...
   ctx.show_text(s.encode("utf-8"))
   #...
#...

greetz

Frank

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

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users