1 module pangocairo;
2 
3 import gtk.DrawingArea;
4 import gtk.Main;
5 import gtk.MainWindow;
6 import gtk.Widget;
7 
8 import cairo.Context;
9 import cairo.ImageSurface;
10 
11 import pango.PgCairo;
12 import pango.PgLayout;
13 import pango.PgFontDescription;
14 
15 import std.stdio;
16 import std.math;
17 
18 class PangoText : DrawingArea
19 {
20 	int m_radius = 150;
21 	int m_nWords = 10;
22 	string m_font = "Sans Bold 27";
23 
24 	public this()
25 	{
26 		addOnDraw(&drawText);
27 	}
28 
29 	public bool drawText (Scoped!Context cr, Widget widget)
30 	{
31 		PgLayout layout;
32 		PgFontDescription desc;
33 
34 		// Center coordinates on the middle of the region we are drawing
35 		cr.translate(m_radius, m_radius);
36 
37 		// Create a PangoLayout, set the font and text
38 		layout = PgCairo.createLayout(cr);
39 	  
40 		layout.setText("Text");
41 		desc = PgFontDescription.fromString(m_font);
42 		layout.setFontDescription(desc);
43 		desc.free();
44 
45 		// Draw the layout m_nWords times in a circle
46 		for (int i = 0; i < m_nWords; i++)
47 		{
48 			int width, height;
49 			double angle = (360. * i) / m_nWords;
50 			double red;
51 
52 			cr.save();
53 
54 			/* Gradient from red at angle == 60 to blue at angle == 240 */
55 			red   = (1 + cos ((angle - 60) * PI / 180.)) / 2;
56 			cr.setSourceRgb(red, 0, 1.0 - red);
57 
58 			cr.rotate(angle * PI / 180.);
59 		
60 			/* Inform Pango to re-layout the text with the new transformation */
61 			PgCairo.updateLayout(cr, layout);
62 		
63 			layout.getSize(width, height);
64 			cr.moveTo( -(cast(double)width / PANGO_SCALE) / 2, - m_radius );
65 			PgCairo.showLayout(cr, layout);
66 
67 			cr.restore();
68 		}
69 
70 		return true;
71 	}
72 }
73 
74 
75 void main(string[] args)
76 {
77 	Main.init(args);
78 	
79 	MainWindow win = new MainWindow("gtkD Pango text");
80 	
81 	win.setDefaultSize( 300, 300 );
82 
83 	PangoText p = new PangoText();
84 	win.add(p);
85 	p.show();
86 	win.showAll();
87 
88 	Main.run();
89 }