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