Skip to content

Instantly share code, notes, and snippets.

@jerinphilip
Forked from santhoshtr/render.py
Last active June 30, 2023 09:35
Show Gist options
  • Save jerinphilip/7300fb1ee4f73133723db6db862a2a89 to your computer and use it in GitHub Desktop.
Save jerinphilip/7300fb1ee4f73133723db6db862a2a89 to your computer and use it in GitHub Desktop.

Revisions

  1. jerinphilip revised this gist Nov 29, 2017. 1 changed file with 11 additions and 10 deletions.
    21 changes: 11 additions & 10 deletions render.py
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,23 @@
    #!/usr/bin/python
    #!/usr/bin/python3
    #-*- coding:utf8 -*-
    # Modified for python3
    # python2 version https://gist.github.com/santhoshtr/bea3da00651bcd18e282
    import cairo
    from gi.repository import Gtk, Gdk, Pango, PangoCairo
    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('മലയാളം हिन्दी ଓଡ଼ିଆ தமிழ்');
    pc = PangoCairo.create_context(context)
    layout = PangoCairo.create_layout(context)
    layout.set_font_description(Pango.FontDescription('Serif Normal 12'))
    layout.set_text('മലയാളം हिन्दी ଓଡ଼ିଆ தமிழ்', -1);
    # Next four lines take care of centering the text. Feel free to ignore ;-)
    width, height = surface.get_width(), surface.get_height()
    print(width, 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)
    PangoCairo.show_layout(context, layout)
    surface.write_to_png('malayalam.png')
  2. @santhoshtr santhoshtr created this gist Oct 9, 2014.
    22 changes: 22 additions & 0 deletions render.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #!/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')