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