原文: http://zetcode.com/gui/gtksharp/pango/

在 GTK# 编程教程的这一部分中,我们将探索 Pango 库。

Pango 是一个免费的开源计算库,可高质量呈现国际化文本。 可以使用不同的字体后端,从而允许跨平台支持。 (维基百科)

Pango 提供了用于 Gdk 和 Gtk 的高级字体和文本处理。

简单的例子

在第一个示例中,我们展示了如何更改标签小部件的字体。

quotes.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. private Label label;
  5. public SharpApp() : base("Pango")
  6. {
  7. SetPosition(WindowPosition.Center);
  8. DeleteEvent += delegate { Application.Quit(); };
  9. string text = @"Excess of joy is harder to bear than any amount of sorrow.
  10. The more one judges, the less one loves.
  11. There is no such thing as a great talent without great will power. ";
  12. label = new Label(text);
  13. Pango.FontDescription fontdesc = Pango.FontDescription.FromString("Purisa 10");
  14. label.ModifyFont(fontdesc);
  15. Fixed fix = new Fixed();
  16. fix.Put(label, 5, 5);
  17. Add(fix);
  18. ShowAll();
  19. }
  20. public static void Main()
  21. {
  22. Application.Init();
  23. new SharpApp();
  24. Application.Run();
  25. }
  26. }

在上面的代码示例中,我们有一个带有三个引号的标签小部件。 我们将其字体更改为 Purisa 10。

  1. string text = @"Excess of joy is harder to bear than any amount of sorrow.
  2. ...

这是要在标签中显示的文本。

  1. Pango.FontDescription fontdesc = Pango.FontDescription.FromString("Purisa 10");

FontDescription用于指定要加载的字体的特征。 FromString()方法从字符串表示形式创建新的字体描述。

  1. label.ModifyFont(fontdesc);

我们将标签小部件的字体更改为 Purisa 10。

Pango - 图1

图:Quotations

系统字体

下一个代码示例显示TreeView小部件中的所有可用字体。

systemfonts.cs

  1. using System;
  2. using Pango;
  3. using Gtk;
  4. public class SharpApp : Window
  5. {
  6. ListStore store;
  7. FontFamily[] fam;
  8. public SharpApp() : base("System fonts")
  9. {
  10. BorderWidth = 8;
  11. SetDefaultSize(350, 250);
  12. SetPosition(WindowPosition.Center);
  13. DeleteEvent += delegate { Application.Quit(); };
  14. ScrolledWindow sw = new ScrolledWindow();
  15. sw.ShadowType = ShadowType.EtchedIn;
  16. sw.SetPolicy(PolicyType.Automatic, PolicyType.Automatic);
  17. Context context = this.CreatePangoContext();
  18. fam = context.Families;
  19. store = CreateModel();
  20. TreeView treeView = new TreeView(store);
  21. treeView.RulesHint = true;
  22. sw.Add(treeView);
  23. CreateColumn(treeView);
  24. Add(sw);
  25. ShowAll();
  26. }
  27. void CreateColumn(TreeView treeView)
  28. {
  29. CellRendererText rendererText = new CellRendererText();
  30. TreeViewColumn column = new TreeViewColumn("FontName",
  31. rendererText, "text", Column.FontName);
  32. column.SortColumnId = (int) Column.FontName;
  33. treeView.AppendColumn(column);
  34. }
  35. ListStore CreateModel()
  36. {
  37. ListStore store = new ListStore( typeof(string) );
  38. foreach (FontFamily ff in fam) {
  39. store.AppendValues(ff.Name);
  40. }
  41. return store;
  42. }
  43. enum Column
  44. {
  45. FontName
  46. }
  47. public static void Main()
  48. {
  49. Application.Init();
  50. new SharpApp();
  51. Application.Run();
  52. }
  53. }

该代码示例显示了系统上所有可用的字体。

  1. Context context = this.CreatePangoContext();

此代码行创建一个Pango.Context对象。 它包含有关文本渲染过程的全局信息。

  1. fam = context.Families;

从上下文对象中,我们检索所有可用的字体系列。

  1. foreach (FontFamily ff in fam) {
  2. store.AppendValues(ff.Name);
  3. }

TreeView小部件的模型创建期间,我们从字体家族数组中获取所有字体名称,并将它们放入列表存储中。

Pango - 图2

图:系统字体

Unicode

Pango 用于处理国际化文本。

unicode.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base("Unicode")
  5. {
  6. SetPosition(WindowPosition.Center);
  7. DeleteEvent += delegate { Application.Quit(); };
  8. string text = @"Фёдор Михайлович Достоевский родился 30 октября (11 ноября)
  9. 1821 года в Москве.Был вторым из 7 детей. Отец, Михаил Андреевич, работал в
  10. госпитале для бедных. Мать, Мария Фёдоровна (в девичестве Нечаева),
  11. происходила из купеческого рода.";
  12. Label label = new Label(text);
  13. Pango.FontDescription fontdesc = Pango.FontDescription.FromString("Purisa 10");
  14. label.ModifyFont(fontdesc);
  15. Fixed fix = new Fixed();
  16. fix.Put(label, 5, 5);
  17. Add(fix);
  18. ShowAll();
  19. }
  20. public static void Main()
  21. {
  22. Application.Init();
  23. new SharpApp();
  24. Application.Run();
  25. }
  26. }

我们在西里尔字母中显示一些文本。

  1. string text = @"Фёдор Михайлович Достоевский родился 30 октября ...

我们可以直接使用 unicode 文本。

  1. Label label = new Label(text);

我们通常在标签小部件中使用它。

Pango - 图3

图:Unicode

彩色文字

在最后一个示例中,我们将进一步探索 Pango 功能。 我们将在DrawingArea小部件上绘制居中的彩色文本。

coloured.cs

  1. using System;
  2. using Gtk;
  3. using Pango;
  4. public class SharpApp : Window
  5. {
  6. public SharpApp () : base ("Australia")
  7. {
  8. SetDefaultSize(250, 200);
  9. SetPosition(WindowPosition.Center);
  10. DeleteEvent += delegate { Application.Quit(); };
  11. Gdk.Color white = new Gdk.Color(255, 255, 255);
  12. DrawingArea drawingArea = new DrawingArea();
  13. drawingArea.ModifyBg(StateType.Normal, white);
  14. drawingArea.ExposeEvent += OnExposeEvent;
  15. Add(drawingArea);
  16. ShowAll();
  17. }
  18. void OnExposeEvent (object sender, ExposeEventArgs a)
  19. {
  20. DrawingArea drawingArea = sender as DrawingArea;
  21. int width = drawingArea.Allocation.Width;
  22. Gdk.PangoRenderer renderer = Gdk.PangoRenderer.GetDefault(drawingArea.Screen);
  23. renderer.Drawable = drawingArea.GdkWindow;
  24. renderer.Gc = drawingArea.Style.BlackGC;
  25. Context context = drawingArea.CreatePangoContext();
  26. Pango.Layout layout = new Pango.Layout(context);
  27. layout.Width = Pango.Units.FromPixels(width);
  28. layout.SetText("Australia");
  29. FontDescription desc = FontDescription.FromString("Serif Bold 20");
  30. layout.FontDescription = desc;
  31. renderer.SetOverrideColor(RenderPart.Foreground, new Gdk.Color(200, 30, 30));
  32. layout.Alignment = Pango.Alignment.Center;
  33. renderer.DrawLayout(layout, 0, 0);
  34. renderer.SetOverrideColor(RenderPart.Foreground, Gdk.Color.Zero);
  35. renderer.Drawable = null;
  36. renderer.Gc = null;
  37. }
  38. public static void Main()
  39. {
  40. Application.Init();
  41. new SharpApp();
  42. Application.Run();
  43. }
  44. }

我们绘制水平居中的"Australia"文本,颜色为深红色。

  1. Gdk.PangoRenderer renderer = Gdk.PangoRenderer.GetDefault(drawingArea.Screen);
  2. renderer.Drawable = drawingArea.GdkWindow;
  3. renderer.Gc = drawingArea.Style.BlackGC;

我们获得了屏幕的默认渲染器,并将其设置为绘制。

  1. Context context = drawingArea.CreatePangoContext();
  2. Pango.Layout layout = new Pango.Layout(context);

我们创建一个Pango.Layout。 它是用于布置整个文本块的高级驱动程序。

  1. layout.Width = Pango.Units.FromPixels(width);

我们指定布局的宽度。

  1. layout.SetText("Australia");

我们设置文本。

  1. FontDescription desc = FontDescription.FromString("Serif Bold 20");
  2. layout.FontDescription = desc;

我们为布局指定字体。

  1. renderer.SetOverrideColor(RenderPart.Foreground, new Gdk.Color(200, 30, 30));
  2. layout.Alignment = Pango.Alignment.Center;

我们设置颜色和对齐方式。

  1. renderer.DrawLayout(layout, 0, 0);

我们绘制 Pango 布局。

  1. renderer.SetOverrideColor(RenderPart.Foreground, Gdk.Color.Zero);
  2. renderer.Drawable = null;
  3. renderer.Gc = null;

我们清理资源。

Pango - 图4

图:澳大利亚

在 GTK# 编程库的这一章中,我们使用了 Pango 库。