1 module pangocairo; 2 3 import gio.Application : GioApplication = Application; 4 import gtk.Application; 5 import gtk.ApplicationWindow; 6 7 import gtk.DrawingArea; 8 import gtk.Widget; 9 10 import cairo.Context; 11 import cairo.ImageSurface; 12 13 import pango.PgCairo; 14 import pango.PgLayout; 15 import pango.PgFontDescription; 16 17 import std.stdio; 18 import std.math; 19 20 class PangoText : DrawingArea 21 { 22 int m_radius = 150; 23 int m_nWords = 10; 24 string m_font = "Sans Bold 27"; 25 26 public this() 27 { 28 addOnDraw(&drawText); 29 } 30 31 public bool drawText (Scoped!Context cr, Widget widget) 32 { 33 PgLayout layout; 34 PgFontDescription desc; 35 36 // Center coordinates on the middle of the region we are drawing 37 cr.translate(m_radius, m_radius); 38 39 // Create a PangoLayout, set the font and text 40 layout = PgCairo.createLayout(cr); 41 42 layout.setText("Text"); 43 desc = PgFontDescription.fromString(m_font); 44 layout.setFontDescription(desc); 45 desc.free(); 46 47 // Draw the layout m_nWords times in a circle 48 for (int i = 0; i < m_nWords; i++) 49 { 50 int width, height; 51 double angle = (360. * i) / m_nWords; 52 double red; 53 54 cr.save(); 55 56 /* Gradient from red at angle == 60 to blue at angle == 240 */ 57 red = (1 + cos ((angle - 60) * PI / 180.)) / 2; 58 cr.setSourceRgb(red, 0, 1.0 - red); 59 60 cr.rotate(angle * PI / 180.); 61 62 /* Inform Pango to re-layout the text with the new transformation */ 63 PgCairo.updateLayout(cr, layout); 64 65 layout.getSize(width, height); 66 cr.moveTo( -(cast(double)width / PANGO_SCALE) / 2, - m_radius ); 67 PgCairo.showLayout(cr, layout); 68 69 cr.restore(); 70 } 71 72 return true; 73 } 74 } 75 76 77 int main(string[] args) 78 { 79 Application application; 80 81 void activatePangoText(GioApplication app) 82 { 83 auto window = new ApplicationWindow(application); 84 window.setTitle("gtkD Pango text"); 85 window.setDefaultSize(300, 300); 86 auto pt = new PangoText(); 87 window.add(pt); 88 pt.show(); 89 window.showAll(); 90 } 91 92 application = new Application("org.gtkd.demo.pangocairo", GApplicationFlags.FLAGS_NONE); 93 application.addOnActivate(&activatePangoText); 94 return application.run(args); 95 }