Skip to content

Instantly share code, notes, and snippets.

@ebraminio
Last active June 23, 2018 09:12
Show Gist options
  • Save ebraminio/696dbf5cb67b2f87b4101073d231dc46 to your computer and use it in GitHub Desktop.
Save ebraminio/696dbf5cb67b2f87b4101073d231dc46 to your computer and use it in GitHub Desktop.
PangoCairo Draw Text
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from scipy.misc import imread
import cairo
import pygtk
pygtk.require('2.0')
import pango
import pangocairo
arabic_indic_trans = dict(
zip((ord(s) for s in u'0123456789'),
u'\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669')
)
for i in range(1, 32):
# https://www.cairographics.org/cookbook/pycairo_pango/
surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 64, 64)
context = cairo.Context(surf)
context.rectangle(0, 0, 64, 64)
context.set_source_rgba(255, 255, 255, 0)
context.fill()
pangocairo_context = pangocairo.CairoContext(context)
pangocairo_context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
layout = pangocairo_context.create_layout()
font = pango.FontDescription('68')
font.set_family('Noto Naskh Arabic')
layout.set_font_description(font)
layout.set_text(unicode(i).translate(arabic_indic_trans))
w, h = layout.get_size()
w /= pango.SCALE
h /= pango.SCALE
context.translate((64 - w) / 2, (76 - h) / 2 - 2)
context.set_source_rgb(255, 255, 255)
pangocairo_context.update_layout(layout)
pangocairo_context.show_layout(layout)
surf.write_to_png('day%d_.png' % (i))
#if i % 16 == 0:
# plt.figure()
# plt.imshow(imread('day%d_ar.png' % (i)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment