1 module text_image;
2 
3 import std.stdio;
4 import std.math;
5 
6 import gtk.Main;
7 import gtk.MainWindow;
8 
9 import cairo.FontOption;
10 import cairo.Context;
11 import cairo.Surface;
12 import cairo.ImageSurface;
13 
14 import gtk.Widget;
15 import gtk.DrawingArea;
16 
17 class CairoText : DrawingArea
18 {
19 public:
20 	this()
21 	{
22 		image = ImageSurface.createFromPng("gtkD_logo.png");
23 	
24 		//Attach our expose callback, which will draw the window.
25 		addOnDraw(&drawCallback);
26 	}
27 
28 protected:
29 	//Override default signal handler:
30 	bool drawCallback(Scoped!Context cr, Widget widget)
31 	{
32 		// This is where we draw on the window
33 		cr.translate(10, 10);
34 		cr.setLineWidth(m_lineWidth);
35 
36 		cr.save();
37 			cr.setSourceRgba(0.1, 0.2, 0.1, 1.0);
38 			cr.paint();
39 		cr.restore();
40 
41 		cr.rectangle( 0.0, 0.0, 230.0, 230.0 );
42 
43 		cr.save();
44 			cr.setSourceRgba(0.3, 0.6, 0.3, 0.8);
45 			cr.fillPreserve();
46 		cr.restore();
47 
48 		cr.save();
49 			cr.setSourceRgba(1.0, 1.0, 1.0, 1.0);
50 			cr.setLineWidth( m_lineWidth * 1.7);
51 			cr.strokePreserve();
52 			cr.clip();
53 		cr.restore();
54 		
55 		//FontOption isn't necessary to get
56 		//text drawn with Cairo
57 		
58 		//gtkD API isn't ready here...
59 		FontOption font_options = FontOption.create();
60 		font_options.setHintStyle(cairo_hint_style_t.NONE);
61 		font_options.setHintMetrics(cairo_hint_metrics_t.OFF);
62 		font_options.setAntialias(cairo_antialias_t.GRAY);
63 
64 		cr.setFontOptions(font_options);
65 
66 		//Text rendering
67 		cr.save();
68 
69 			cr.moveTo(75.0, 75.0);
70 			cr.setSourceRgba(1.0, 1.0, 1.0, 0.5);
71 			
72 			for( int i = 0; i < 10; i++ )
73 			{
74 				cr.save();
75 					cr.rotate(2.0 * PI * i / 10);
76 					cr.showText("Cairo");
77 				cr.restore();
78 			}
79 		
80 			cr.moveTo(30.0, 100.0);
81 			cr.selectFontFace("Bitstream Vera Sans", cairo_font_slant_t.NORMAL,
82 						cairo_font_weight_t.NORMAL);
83 			cr.setFontSize(12);
84 			cr.setSourceRgb(0.0, 0.0, 0.0);
85 			cr.showText("You can draw an image");
86 			cr.moveTo(30.0, 115.0);
87 			cr.showText("and write some simple text");
88 			
89 			cr.moveTo(30.0, 140.0);
90 			cr.selectFontFace("Bitstream Vera Sans", cairo_font_slant_t.ITALIC,
91 						cairo_font_weight_t.BOLD);
92 			cr.setFontSize(22);
93 			cr.setSourceRgb(1.0, 1.0, 1.0);
94 			cr.showText("with Cairo.");
95 			
96 			cr.selectFontFace("Bitstream Vera Sans", cairo_font_slant_t.NORMAL,
97 						cairo_font_weight_t.NORMAL);
98 			cr.setFontSize(12);
99 				
100 		cr.restore();
101 		
102 		//Image rendering
103 		
104 		cr.save();
105 			
106 			cr.setSourceSurface( image, 0, 0 );
107 			cr.paint();
108 			//delete image;
109 		cr.restore();
110 
111 		return true;
112 	}
113 
114 	double m_radius = 100.0;
115 	double m_lineWidth = 1.0;
116 	
117 	//ImageSurface image; //doesn't work with createFromPng.
118 	Surface image;
119 }
120 
121 
122 void main(string[] args)
123 {
124 	Main.init(args);
125 	
126 	MainWindow win = new MainWindow("gtkD Cairo text & image");
127 	
128 	win.setDefaultSize( 250, 250 );
129 
130 	CairoText c = new CairoText();
131 	win.add(c);
132 	c.show();
133 	win.showAll();
134 
135 	Main.run();
136 }
137