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