Browse Source

Use Pango to draw text in Cairo engine.

Makes text look a lot better, and is aligned correctly! Yay!
pull/10/head
Steven Kirk 12 years ago
parent
commit
f31fe557ae
  1. 32
      Cairo/Perspex.Cairo/Media/DrawingContext.cs
  2. 36
      Cairo/Perspex.Cairo/Media/TextService.cs

32
Cairo/Perspex.Cairo/Media/DrawingContext.cs

@ -8,11 +8,11 @@ namespace Perspex.Cairo.Media
{ {
using System; using System;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using global::Cairo;
using Perspex.Media; using Perspex.Media;
using Perspex.Platform;
using Splat;
using Cairo = global::Cairo;
using IBitmap = Perspex.Media.Imaging.IBitmap; using IBitmap = Perspex.Media.Imaging.IBitmap;
using Matrix = Perspex.Matrix;
using CairoMatrix = global::Cairo.Matrix;
/// <summary> /// <summary>
/// Draws using Direct2D1. /// Draws using Direct2D1.
@ -22,21 +22,27 @@ namespace Perspex.Cairo.Media
/// <summary> /// <summary>
/// The cairo context. /// The cairo context.
/// </summary> /// </summary>
private Context context; private Cairo.Context context;
/// <summary> /// <summary>
/// The cairo surface. /// The cairo surface.
/// </summary> /// </summary>
private Surface surface; private Cairo.Surface surface;
/// <summary>
/// The text service.
/// </summary>
private TextService textService;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DrawingContext"/> class. /// Initializes a new instance of the <see cref="DrawingContext"/> class.
/// </summary> /// </summary>
/// <param name="surface">The target surface.</param> /// <param name="surface">The target surface.</param>
public DrawingContext(Surface surface) public DrawingContext(Cairo.Surface surface)
{ {
this.surface = surface; this.surface = surface;
this.context = new Context(surface); this.context = new Cairo.Context(surface);
this.textService = Locator.Current.GetService<TextService>() as TextService;
} }
/// <summary> /// <summary>
@ -47,6 +53,7 @@ namespace Perspex.Cairo.Media
{ {
this.Drawable = drawable; this.Drawable = drawable;
this.context = Gdk.CairoHelper.Create(drawable); this.context = Gdk.CairoHelper.Create(drawable);
this.textService = Locator.Current.GetService<ITextService>() as TextService;
} }
public Matrix CurrentTransform public Matrix CurrentTransform
@ -121,11 +128,10 @@ namespace Perspex.Cairo.Media
/// <param name="text">The text.</param> /// <param name="text">The text.</param>
public void DrawText(Perspex.Media.Brush foreground, Rect rect, FormattedText text) public void DrawText(Perspex.Media.Brush foreground, Rect rect, FormattedText text)
{ {
var layout = this.textService.CreateLayout(text);
this.SetBrush(foreground); this.SetBrush(foreground);
this.context.MoveTo(rect.X, rect.Bottom); this.context.MoveTo(rect.X, rect.Y);
this.context.SelectFontFace(text.FontFamilyName, (FontSlant)text.FontStyle, FontWeight.Normal); Pango.CairoHelper.ShowLayout(this.context, layout);
this.context.SetFontSize(text.FontSize);
this.context.ShowText(text.Text);
} }
/// <summary> /// <summary>
@ -167,9 +173,9 @@ namespace Perspex.Cairo.Media
}); });
} }
private static CairoMatrix Convert(Matrix m) private static Cairo.Matrix Convert(Matrix m)
{ {
return new CairoMatrix(m.M11, m.M12, m.M21, m.M22, m.OffsetX, m.OffsetY); return new Cairo.Matrix(m.M11, m.M12, m.M21, m.M22, m.OffsetX, m.OffsetY);
} }
private void SetBrush(Brush brush) private void SetBrush(Brush brush)

36
Cairo/Perspex.Cairo/Media/TextService.cs

@ -27,6 +27,23 @@ namespace Perspex.Cairo.Media
internal set; internal set;
} }
public Pango.Layout CreateLayout(FormattedText text)
{
var layout = new Pango.Layout(this.Context)
{
FontDescription = new Pango.FontDescription
{
Family = text.FontFamilyName,
Size = Pango.Units.FromDouble(text.FontSize),
Style = (Pango.Style)text.FontStyle,
}
};
layout.SetText(text.Text);
return layout;
}
public int GetCaretIndex(FormattedText text, Point point, Size constraint) public int GetCaretIndex(FormattedText text, Point point, Size constraint)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@ -50,24 +67,7 @@ namespace Perspex.Cairo.Media
Pango.Rectangle logicalRect; Pango.Rectangle logicalRect;
layout.GetExtents(out inkRect, out logicalRect); layout.GetExtents(out inkRect, out logicalRect);
return new Size(Pango.Units.ToDouble(inkRect.Width), Pango.Units.ToDouble(inkRect.Height)); return new Size(Pango.Units.ToDouble(logicalRect.Width), Pango.Units.ToDouble(logicalRect.Height));
}
private Pango.Layout CreateLayout(FormattedText text)
{
var layout = new Pango.Layout(this.Context)
{
FontDescription = new Pango.FontDescription
{
Family = text.FontFamilyName,
Size = Pango.Units.FromDouble(text.FontSize),
Style = (Pango.Style)text.FontStyle,
}
};
layout.SetText(text.Text);
return layout;
} }
} }
} }

Loading…
Cancel
Save