summaryrefslogtreecommitdiff
path: root/examples/pango/pangocairo-simple.py
blob: 61e05ec700ae727611bdc92155406dec2986c4e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env python
import sys
import math
import pango
import cairo
import pangocairo

RADIUS = 150

def draw_text(cr):
    N_WORDS = 10
    FONT = "Sans Bold 27"

    # Center coordinates on the middle of the region we are drawing
    cr.translate(RADIUS, RADIUS);

    # Create a PangoLayout, set the font and text */
    layout = cr.create_layout()
  
    layout.set_text("Text")
    layout.set_font_description(pango.FontDescription(FONT))

    # Draw the layout N_WORDS times in a circle
    for i in range(N_WORDS):
         angle = (360 * i) / N_WORDS;
         cr.save()

         # Gradient from red at angle == 60 to blue at angle == 300
         red   = (1 + math.cos((angle - 60)*math.pi/180))/2
         cr.set_source_rgb(red, 0, 1 - red)
         cr.rotate(angle*math.pi/180)
    
         # Inform Pango to re-layout the text with the new transformation */
         cr.update_layout(layout)
    
         width, height = layout.get_size()
         cr.move_to(-width/pango.SCALE/2, -RADIUS)
         cr.show_layout(layout)

         cr.restore()

def main(argv):
    if len(argv) != 2:
        print >> sys.stderr, "Usage: cairosimple OUTPUT_BASENAME\n"
        return 1

    filename = argv[1]
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 2*RADIUS, 2*RADIUS)
    cr = pangocairo.CairoContext(cairo.Context(surface))
    cr.set_source_rgb(1.0, 1.0, 1.0)
    cr.rectangle(0, 0, 2*RADIUS, 2*RADIUS)
    cr.fill()
    draw_text(cr)

    surface.write_to_png(filename + ".png")

    ## output also a PDF file
    surface = cairo.PDFSurface(filename + ".pdf", 2*RADIUS, 2*RADIUS)
    cr = pangocairo.CairoContext(cairo.Context(surface))
    draw_text(cr)
    cr.show_page()
    surface.finish()
    

if __name__ == '__main__':
    sys.exit(main(sys.argv))