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