Error in Pango while using cairo/librsvg

H

Hilmar Bunjes

Hi,
I try using Python (on Windows) with Cairo and librsvg to convert a svg
file to a png image. I got some help from the German python newsgroup to
get this running but now I run into some problems with pango I don't
know how to solve.

Running the python script in the console it tells me:
(python.exe:4908): Pango-CRITICAL **:
pango_win32_font_map_get_font_cache: assertion `font_map != NULL' failed
**
Pango:ERROR:pangowin32.c:838:pango_win32_font_finalize: assertion
failed: (win32font->fontmap != NULL)

This application has requested the Runtime to terminate it in an unusual
way.
Please contact the application's support team for more information.


The script I run is:
#!/usr/bin/python

from ctypes import *
import cairo
from rsvg_wrapper import rsvgClass
from sys import argv
from os.path import exists
import getopt

if __name__ == "__main__":
opts, args = getopt.getopt (argv[1:], 'o:h',
['width=', 'height=', 'output=', 'help'])
file = args[0]

rsvg = rsvgClass()

svg = rsvg.Handle (file = file)
svgDimensionData=svg.get_dimension_data()

if file[-4:] == ".svg":
file = file[:-4]
output = "%s.png" % file
base = "%s%d.png"
i = 1
while exists (output):
output = base % (file, i)
i += 1

width = svgDimensionData[0]
height = svgDimensionData[1]

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context (surface)

wscale = float (width) / svgDimensionData[0]
hscale = float (height) / svgDimensionData[1]

cr.scale (wscale, hscale)

svg.render_cairo (cr)

surface.write_to_png (output)

and the rsvg_wrapper.py is:
#some code to give rsvg.render_cairo(ctx) ability
#on windows.
import os
try:
import rsvg
WINDOWS=False
except ImportError:
print"Warning, could not import 'rsvg'"
if os.name == 'nt':
print "Detected windows, creating rsvg."
#some workarounds for windows

from ctypes import *

l=CDLL('librsvg-2-2.dll')
g=CDLL('libgobject-2.0-0.dll')
g.g_type_init()

class rsvgHandle():
class RsvgDimensionData(Structure):
_fields_ = [("width", c_int),
("height", c_int),
("em",c_double),
("ex",c_double)]

class PycairoContext(Structure):
_fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
("ctx", c_void_p),
("base", c_void_p)]

def __init__(self, path):
self.path = path
error = ''
self.handle = l.rsvg_handle_new_from_file(self.path,error)


def get_dimension_data(self):
svgDim = self.RsvgDimensionData()
l.rsvg_handle_get_dimensions(self.handle,byref(svgDim))
return (svgDim.width,svgDim.height)

def render_cairo(self, ctx):
ctx.save()
z = self.PycairoContext.from_address(id(ctx))
l.rsvg_handle_render_cairo(self.handle, z.ctx)
ctx.restore()



class rsvgClass():
def Handle(self,file):
return rsvgHandle(file)

I have the GIMP 2.6 bin folder in my path so all DLLs are used from that
folder.

I have no idea why the error is raised. I also tried to "import pango"
in the script but then it tells me:
Traceback (most recent call last):
File "svg2png2.py", line 41, in <module>
svg.render_cairo (cr)
File "...\rsvg_wrapper.py", line 45, in render_cairo
l.rsvg_handle_render_cairo(self.handle, z.ctx)
WindowsError: exception: access violation reading 0x00000008

Can anybody help my with the svg2png conversion here?

Thanks,
Hilmar
 
S

saxi

I use pyWxSVG to convert a svg file to a png image.
pyWxSVG is svg canvas for wxPython.
View and print svg file or svg content, convert svg to raster
graphics. Open svg, svgz file as wx.Image, use wx.BITMAP_TYPE_SVG,
wx.BITMAP_TYPE_SVGZ type. Partial support svg format. Tested with
Python 2.5 and wxPython 2.8.9.2. Path parser and elliptical arc
approximation from Enable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top