Skip to content

Instantly share code, notes, and snippets.

@santhoshtr
Created October 9, 2014 09:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save santhoshtr/bea3da00651bcd18e282 to your computer and use it in GitHub Desktop.
Save santhoshtr/bea3da00651bcd18e282 to your computer and use it in GitHub Desktop.
PangoCairo Text rendering
#!/usr/bin/python
#-*- coding:utf8 -*-
import cairo
import pango
import pangocairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 100)
context = cairo.Context(surface)
pc = pangocairo.CairoContext(context)
layout = pc.create_layout()
layout.set_font_description(pango.FontDescription('Serif Normal 12'))
layout.set_text('മലയാളം हिन्दी ଓଡ଼ିଆ தமிழ்');
# Next four lines take care of centering the text. Feel free to ignore ;-)
width, height = surface.get_width(), surface.get_height()
w, h = layout.get_pixel_size()
position = (width/2.0 - w/2.0, height/2.0 - h/2.0)
position = (w,h)
context.move_to(*position)
pc.show_layout(layout)
surface.write_to_png('malayalam.png')
@santhoshtr
Copy link
Author

PHP has these bindings. See http://php.net/manual/en/book.cairo.php

@santhoshtr
Copy link
Author

@jerinphilip
Copy link

This works in python2.
Any idea what's the current nice way to integrate something like this in python3?

@santhoshtr
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment