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

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

@ -27,6 +27,23 @@ namespace Perspex.Cairo.Media
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)
{
throw new NotImplementedException();
@ -50,24 +67,7 @@ namespace Perspex.Cairo.Media
Pango.Rectangle logicalRect;
layout.GetExtents(out inkRect, out logicalRect);
return new Size(Pango.Units.ToDouble(inkRect.Width), Pango.Units.ToDouble(inkRect.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;
return new Size(Pango.Units.ToDouble(logicalRect.Width), Pango.Units.ToDouble(logicalRect.Height));
}
}
}

Loading…
Cancel
Save