Advertisement
Guest User

Marcelo Fernndez

a guest
Apr 9th, 2009
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. import ctypes
  2. import cairo
  3. from ctypes.util import find_library
  4.  
  5. cairo_dll = ctypes.CDLL(find_library("cairo"))
  6.  
  7. # Pycairo's API representation (from pycairo.h)
  8. class Pycairo_CAPI(ctypes.Structure):
  9.    _fields_ = [
  10.       ('Context_Type', ctypes.py_object),
  11.       ('Context_FromContext', ctypes.PYFUNCTYPE(ctypes.py_object,
  12.                                                 ctypes.c_void_p,
  13.                                                 ctypes.py_object,
  14.                                                 ctypes.py_object)),
  15.       ('FontFace_Type', ctypes.py_object),
  16.       ('FontFace_FromFontFace', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
  17.       ('FontOptions_Type', ctypes.py_object),
  18.       ('FontOptions_FromFontOptions', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
  19.       ('Matrix_Type', ctypes.py_object),
  20.       ('Matrix_FromMatrix', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
  21.       ('Path_Type', ctypes.py_object),
  22.       ('Path_FromPath', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
  23.       ('Pattern_Type', ctypes.py_object),
  24.       ('SolidPattern_Type', ctypes.py_object),
  25.       ('SurfacePattern_Type', ctypes.py_object),
  26.       ('Gradient_Type', ctypes.py_object),
  27.       ('LinearGradient_Type', ctypes.py_object),
  28.       ('RadialGradient_Type', ctypes.py_object),
  29.       ('Pattern_FromPattern', ctypes.c_void_p),
  30.       ('ScaledFont_Type', ctypes.py_object),
  31.       ('ScaledFont_FromScaledFont', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
  32.       ('Surface_Type', ctypes.py_object),
  33.       ('ImageSurface_Type', ctypes.py_object),
  34.       ('PDFSurface_Type', ctypes.py_object),
  35.       ('PSSurface_Type', ctypes.py_object),
  36.       ('SVGSurface_Type', ctypes.py_object),
  37.       ('Win32Surface_Type', ctypes.py_object),
  38.       ('XlibSurface_Type', ctypes.py_object),
  39.       ('Surface_FromSurface', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
  40.       ('Check_Status', ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.c_int))]
  41.  
  42. # look up the API
  43. ctypes.pythonapi.PyCObject_Import.restype = ctypes.POINTER(Pycairo_CAPI)
  44. pycairo_api = ctypes.pythonapi.PyCObject_Import("cairo", "CAPI").contents;
  45.  
  46. ContextType = pycairo_api.Context_Type
  47.  
  48. def Context_FromSWIGObject(swigObj):
  49.     ptr = ctypes.c_void_p(int(swigObj))
  50.     #increment the native context's ref count, since the Pycairo_Context decrements it
  51.     #when it is finalised.
  52.     cairo_dll.cairo_reference(ptr)
  53.     return pycairo_api.Context_FromContext(ptr, ContextType, None)
  54.  
  55. if __name__=="__main__":
  56.     import wx
  57.     import math
  58.     import sys
  59.     import poppler
  60.  
  61.     class myframe(wx.Frame):
  62.         def __init__(self):
  63.             wx.Frame.__init__(self, None, -1, "test", size=(500,400))
  64.             self.Bind(wx.EVT_PAINT, self.OnPaint)
  65.             uri = "file://" + sys.argv[1]
  66.             self.document = poppler.document_new_from_file (uri, None)
  67.             self.n_pages = self.document.get_n_pages()
  68.  
  69.             self.current_page = self.document.get_page(0)
  70.             self.scale = 1
  71.             self.width, self.height = self.current_page.get_size()
  72.             self.SetSize((self.width, self.height))
  73.  
  74.         def OnPaint(self, event):
  75.             dc = wx.PaintDC(self)
  76.             w,h = dc.GetSizeTuple()
  77.             gc = wx.GraphicsContext.Create(dc)
  78.             nc = gc.GetNativeContext()
  79.             ctx = Context_FromSWIGObject(nc)
  80.    
  81.             ctx.set_source_rgb(1, 1, 1)
  82.            
  83.             if self.scale != 1:
  84.                 ctx.scale(self.scale, self.scale)
  85.            
  86.             ctx.rectangle(0, 0, self.width, self.height)
  87.             ctx.fill()
  88.             self.current_page.render(ctx)
  89.  
  90.  
  91.     app = wx.App()
  92.     f = myframe()
  93.     f.Show()
  94.     app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement