与其他类库集成

作为纹理的Pyglet和ImageSurface

从ImageSurface创建一个pyglet.Texture:
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)
绘制绑定到ImageSurface的pyglet.Texture
window = window.Window(width=width, height=height)

@window.event
def on_draw():
    window.clear()

    # Draw texture backed by ImageSurface
    gl.glEnable(gl.GL_TEXTURE_2D)

    gl.glBindTexture(gl.GL_TEXTURE_2D, texture.id)
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_BGRA,
    gl.GL_UNSIGNED_BYTE,
    surface_data)

    gl.glBegin(gl.GL_QUADS)
    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)
    gl.glEnd()

# call clock.schedule_update here to update the ImageSurface every frame
app.run()

Freetype-py和开罗

有关示例,请参阅https://github.com/rougier/freetype-py/tree/master/examples。大多数 *-cairo.py 示例说明了从FreeType位图到开罗曲面的转换;这两个示例 glyph-vector-cairo.pyglyph-vector-2-cairo.py ,说明了从FreeType字形轮廓到开罗路径的转换。

作为纹理的modnGL和ImageSurface

从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())

还可以在https://github.com/moderngl/moderngl/blob/master/examples/integration_pycairo.py项目中找到一个示例