Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ctypes
- import cairo
- from ctypes.util import find_library
- cairo_dll = ctypes.CDLL(find_library("cairo"))
- # Pycairo's API representation (from pycairo.h)
- class Pycairo_CAPI(ctypes.Structure):
- _fields_ = [
- ('Context_Type', ctypes.py_object),
- ('Context_FromContext', ctypes.PYFUNCTYPE(ctypes.py_object,
- ctypes.c_void_p,
- ctypes.py_object,
- ctypes.py_object)),
- ('FontFace_Type', ctypes.py_object),
- ('FontFace_FromFontFace', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
- ('FontOptions_Type', ctypes.py_object),
- ('FontOptions_FromFontOptions', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
- ('Matrix_Type', ctypes.py_object),
- ('Matrix_FromMatrix', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
- ('Path_Type', ctypes.py_object),
- ('Path_FromPath', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
- ('Pattern_Type', ctypes.py_object),
- ('SolidPattern_Type', ctypes.py_object),
- ('SurfacePattern_Type', ctypes.py_object),
- ('Gradient_Type', ctypes.py_object),
- ('LinearGradient_Type', ctypes.py_object),
- ('RadialGradient_Type', ctypes.py_object),
- ('Pattern_FromPattern', ctypes.c_void_p),
- ('ScaledFont_Type', ctypes.py_object),
- ('ScaledFont_FromScaledFont', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
- ('Surface_Type', ctypes.py_object),
- ('ImageSurface_Type', ctypes.py_object),
- ('PDFSurface_Type', ctypes.py_object),
- ('PSSurface_Type', ctypes.py_object),
- ('SVGSurface_Type', ctypes.py_object),
- ('Win32Surface_Type', ctypes.py_object),
- ('XlibSurface_Type', ctypes.py_object),
- ('Surface_FromSurface', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
- ('Check_Status', ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.c_int))]
- # look up the API
- ctypes.pythonapi.PyCObject_Import.restype = ctypes.POINTER(Pycairo_CAPI)
- pycairo_api = ctypes.pythonapi.PyCObject_Import("cairo", "CAPI").contents;
- ContextType = pycairo_api.Context_Type
- def Context_FromSWIGObject(swigObj):
- ptr = ctypes.c_void_p(int(swigObj))
- #increment the native context's ref count, since the Pycairo_Context decrements it
- #when it is finalised.
- cairo_dll.cairo_reference(ptr)
- return pycairo_api.Context_FromContext(ptr, ContextType, None)
- if __name__=="__main__":
- import wx
- import math
- import sys
- import poppler
- class myframe(wx.Frame):
- def __init__(self):
- wx.Frame.__init__(self, None, -1, "test", size=(500,400))
- self.Bind(wx.EVT_PAINT, self.OnPaint)
- uri = "file://" + sys.argv[1]
- self.document = poppler.document_new_from_file (uri, None)
- self.n_pages = self.document.get_n_pages()
- self.current_page = self.document.get_page(0)
- self.scale = 1
- self.width, self.height = self.current_page.get_size()
- self.SetSize((self.width, self.height))
- def OnPaint(self, event):
- dc = wx.PaintDC(self)
- w,h = dc.GetSizeTuple()
- gc = wx.GraphicsContext.Create(dc)
- nc = gc.GetNativeContext()
- ctx = Context_FromSWIGObject(nc)
- ctx.set_source_rgb(1, 1, 1)
- if self.scale != 1:
- ctx.scale(self.scale, self.scale)
- ctx.rectangle(0, 0, self.width, self.height)
- ctx.fill()
- self.current_page.render(ctx)
- app = wx.App()
- f = myframe()
- f.Show()
- app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement