Integration with other Libraries

Pyglet & ImageSurface as Texture

Creating a pyglet.Texture from an ImageSurface:
import ctypes
import cairo

from pyglet import app, clock, gl, image, window

# create data shared by ImageSurface and Texture
width, height = 400, 400

surface_data = (ctypes.c_ubyte * (width * height * 4))()
surface = cairo.ImageSurface.create_for_data (surface_data, cairo.FORMAT_ARGB32,
width, height, width * 4);
texture = image.Texture.create_for_size(gl.GL_TEXTURE_2D, width, height, gl.GL_RGBA)
Draw pyglet.Texture bound to ImageSurface
window = window.Window(width=width, height=height)

def on_draw():

    # Draw texture backed by ImageSurface

    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_BGRA,

    gl.glTexCoord2f(0.0, 1.0)
    gl.glVertex2i(0, 0)
    gl.glTexCoord2f(1.0, 1.0)
    gl.glVertex2i(width, 0)
    gl.glTexCoord2f(1.0, 0.0)
    gl.glVertex2i(width, height)
    gl.glTexCoord2f(0.0, 0.0)
    gl.glVertex2i(0, height)

# call clock.schedule_update here to update the ImageSurface every frame

Freetype-py & Cairo

See for examples. Most of the * examples illustrate conversion from FreeType bitmaps to Cairo surfaces; the two examples, and, illustrate conversion from FreeType glyph contours to Cairo paths.

ModernGL & ImageSurface as Texture

Creating a moderngl.Texture from an ImageSurface:
import moderngl
import cairo
ctx = moderngl.create_context(standalone=True)
width, height = 400, 400
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
texture = ctx.texture((width, height), 4, data=surface.get_data())

An example can also be found in the ModernGL project: