File: pangocairo.d

package info (click to toggle)
gtk-d 3.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 20,152 kB
  • sloc: javascript: 565; sh: 71; makefile: 25
file content (95 lines) | stat: -rw-r--r-- 2,060 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module pangocairo;

import gio.Application : GioApplication = Application;
import gtk.Application;
import gtk.ApplicationWindow;

import gtk.DrawingArea;
import gtk.Widget;

import cairo.Context;
import cairo.ImageSurface;

import pango.PgCairo;
import pango.PgLayout;
import pango.PgFontDescription;

import std.stdio;
import std.math;

class PangoText : DrawingArea
{
	int m_radius = 150;
	int m_nWords = 10;
	string m_font = "Sans Bold 27";

	public this()
	{
		addOnDraw(&drawText);
	}

	public bool drawText (Scoped!Context cr, Widget widget)
	{
		PgLayout layout;
		PgFontDescription desc;

		// Center coordinates on the middle of the region we are drawing
		cr.translate(m_radius, m_radius);

		// Create a PangoLayout, set the font and text
		layout = PgCairo.createLayout(cr);

		layout.setText("Text");
		desc = PgFontDescription.fromString(m_font);
		layout.setFontDescription(desc);
		desc.free();

		// Draw the layout m_nWords times in a circle
		for (int i = 0; i < m_nWords; i++)
		{
			int width, height;
			double angle = (360. * i) / m_nWords;
			double red;

			cr.save();

			/* Gradient from red at angle == 60 to blue at angle == 240 */
			red   = (1 + cos ((angle - 60) * PI / 180.)) / 2;
			cr.setSourceRgb(red, 0, 1.0 - red);

			cr.rotate(angle * PI / 180.);

			/* Inform Pango to re-layout the text with the new transformation */
			PgCairo.updateLayout(cr, layout);

			layout.getSize(width, height);
			cr.moveTo( -(cast(double)width / PANGO_SCALE) / 2, - m_radius );
			PgCairo.showLayout(cr, layout);

			cr.restore();
		}

		return true;
	}
}


int main(string[] args)
{
	Application application;

	void activatePangoText(GioApplication app)
	{
		auto window = new ApplicationWindow(application);
		window.setTitle("gtkD Pango text");
		window.setDefaultSize(300, 300);
		auto pt = new PangoText();
		window.add(pt);
		pt.show();
		window.showAll();
	}

	application = new Application("org.gtkd.demo.pangocairo", GApplicationFlags.FLAGS_NONE);
	application.addOnActivate(&activatePangoText);
	return application.run(args);
}