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 gdk.Drawable;
19 import gtk.DrawingArea;
20 
21 class CairoText : DrawingArea
22 {
23 public:
24 	this()
25 	{
26 		//doesn't work, and isn't even the desired API:
27 		//image = cast(ImageSurface) ImageSurface.createFromPng("gtkD_logo.png");
28 		image = ImageSurface.createFromPng("gtkD_logo.png");
29 	
30 		//Attach our expose callback, which will draw the window.
31 		addOnExpose(&exposeCallback);
32 	}
33 
34 protected:
35 	//Override default signal handler:
36 	bool exposeCallback(GdkEventExpose* event, Widget widget)
37 	{
38 		// This is where we draw on the window
39 
40 		Drawable dr = getWindow();
41 
42 		int width;
43 		int height;
44 
45 		dr.getSize(width, height);
46 
47 		auto cr = new Context (dr);
48 
49 		if (event)
50 		{
51 			// clip to the area indicated by the expose event so that we only redraw
52 			// the portion of the window that needs to be redrawn
53 			cr.rectangle(event.area.x, event.area.y,
54 				event.area.width, event.area.height);
55 			cr.clip();
56 		}
57 
58 		cr.translate(10, 10);
59 		cr.setLineWidth(m_lineWidth);
60 
61 		cr.save();
62 			cr.setSourceRgba(0.1, 0.2, 0.1, 1.0);
63 			cr.paint();
64 		cr.restore();
65 
66 		cr.rectangle( 0.0, 0.0, 230.0, 230.0 );
67 
68 		cr.save();
69 			cr.setSourceRgba(0.3, 0.6, 0.3, 0.8);
70 			cr.fillPreserve();
71 		cr.restore();
72 
73 		cr.save();
74 			cr.setSourceRgba(1.0, 1.0, 1.0, 1.0);
75 			cr.setLineWidth( m_lineWidth * 1.7);
76 			cr.strokePreserve();
77 			cr.clip();
78 		cr.restore();
79 		
80 		//FontOption isn't necessary to get
81 		//text drawn with Cairo
82 		
83 		//gtkD API isn't ready here...
84 		FontOption font_options = FontOption.create();
85 		font_options.setHintStyle(cairo_hint_style_t.NONE);
86 		font_options.setHintMetrics(cairo_hint_metrics_t.OFF);
87 		font_options.setAntialias(cairo_antialias_t.GRAY);
88 
89 		cr.setFontOptions(font_options);
90 		
91 		
92 		//Text rendering
93 		cr.save();
94 		
95 			cr.moveTo(75.0, 75.0);
96 			cr.setSourceRgba(1.0, 1.0, 1.0, 0.5);
97 			
98 			for( int i = 0; i < 10; i++ )
99 			{
100 				cr.save();
101 					cr.rotate(2.0 * PI * i / 10);
102 					cr.showText("Cairo");
103 				cr.restore();
104 			}
105 		
106 			cr.moveTo(30.0, 100.0);
107 			cr.selectFontFace("Bitstream Vera Sans", cairo_font_slant_t.NORMAL,
108 						cairo_font_weight_t.NORMAL);
109 			cr.setFontSize(12);
110 			cr.setSourceRgb(0.0, 0.0, 0.0);
111 			cr.showText("You can draw an image");
112 			cr.moveTo(30.0, 115.0);
113 			cr.showText("and write some simple text");
114 			
115 			cr.moveTo(30.0, 140.0);
116 			cr.selectFontFace("Bitstream Vera Sans", cairo_font_slant_t.ITALIC,
117 						cairo_font_weight_t.BOLD);
118 			cr.setFontSize(22);
119 			cr.setSourceRgb(1.0, 1.0, 1.0);
120 			cr.showText("with Cairo.");
121 			
122 			cr.selectFontFace("Bitstream Vera Sans", cairo_font_slant_t.NORMAL,
123 						cairo_font_weight_t.NORMAL);
124 			cr.setFontSize(12);
125 				
126 		cr.restore();
127 		
128 		//Image rendering
129 		
130 		cr.save();
131 			
132 			cr.setSourceSurface( image, 0, 0 );
133 			cr.paint();
134 			//delete image;
135 		cr.restore();
136 			
137 			
138 		delete cr;
139 
140 		return true;
141 	}
142 
143 	double m_radius = 100.0;
144 	double m_lineWidth = 1.0;
145 	
146 	//ImageSurface image; //doesn't work with createFromPng.
147 	Surface image;
148 }
149 
150 
151 void main(string[] args)
152 {
153 	Main.init(args);
154 	
155 	MainWindow win = new MainWindow("gtkD Cairo text & image");
156 	
157 	win.setDefaultSize( 250, 250 );
158 
159 	CairoText c = new CairoText();
160 	win.add(c);
161 	c.show();
162 	win.showAll();
163 
164 	Main.run();
165 }
166 
167