Browse Source

Finally rendering text on linux!

(very ugly and in-the-wrong place text)
pull/10/head
Steven Kirk 11 years ago
parent
commit
123ebabedc
  1. 19
      Cairo/Perspex.Cairo/CairoPlatform.cs
  2. 6
      Cairo/Perspex.Cairo/Media/DrawingContext.cs
  3. 37
      Cairo/Perspex.Cairo/Media/TextService.cs
  4. 15
      Cairo/Perspex.Cairo/Perspex.Cairo.csproj
  5. 21
      Perspex-Mono.userprefs

19
Cairo/Perspex.Cairo/CairoPlatform.cs

@ -40,6 +40,11 @@ namespace Perspex.Cairo
public IRenderer CreateRenderer(IPlatformHandle handle, double width, double height)
{
if (textService.Context == null)
{
textService.Context = GetPangoContext(handle);
}
return new Renderer(handle, width, height);
}
@ -60,5 +65,19 @@ namespace Perspex.Cairo
ImageSurface result = new ImageSurface(fileName);
return new BitmapImpl(result);
}
private Pango.Context GetPangoContext(IPlatformHandle handle)
{
switch (handle.HandleDescriptor)
{
case "GtkWindow":
var window = GLib.Object.GetObject(handle.Handle) as Gtk.Window;
return window.PangoContext;
default:
throw new NotSupportedException(string.Format(
"Don't know how to get a Pango Context from a '{0}'.",
handle.HandleDescriptor));
}
}
}
}

6
Cairo/Perspex.Cairo/Media/DrawingContext.cs

@ -121,7 +121,11 @@ namespace Perspex.Cairo.Media
/// <param name="text">The text.</param>
public void DrawText(Perspex.Media.Brush foreground, Rect rect, FormattedText text)
{
// TODO: Implement
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);
}
/// <summary>

37
Cairo/Perspex.Cairo/Media/TextService.cs

@ -8,13 +8,23 @@ namespace Perspex.Cairo.Media
{
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Perspex.Media;
using Perspex.Platform;
public class TextService : ITextService
{
public TextService()
/// <summary>
/// Gets the pango context to be used by the service.
/// </summary>
/// <remarks>>
/// There seems to be no way in GtkSharp to create a new Pango Context, so this has to
/// be injected by CairoPlatform the first time a renderer is created.
/// </remarks>
public Pango.Context Context
{
get;
internal set;
}
public int GetCaretIndex(FormattedText text, Point point, Size constraint)
@ -34,7 +44,30 @@ namespace Perspex.Cairo.Media
public Size Measure(FormattedText text, Size constraint)
{
return new Size(100, 30);
var layout = this.CreateLayout(text);
Pango.Rectangle inkRect;
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;
}
}
}

15
Cairo/Perspex.Cairo/Perspex.Cairo.csproj

@ -30,9 +30,15 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f, processorArchitecture=MSIL" />
<Reference Include="glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f, processorArchitecture=MSIL" />
<Reference Include="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f, processorArchitecture=MSIL" />
<Reference Include="gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f, processorArchitecture=MSIL">
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f, processorArchitecture=MSIL">
<Package>glib-sharp-2.0</Package>
</Reference>
<Reference Include="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f, processorArchitecture=MSIL">
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="pango-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f, processorArchitecture=MSIL">
<Package>gtk-sharp-2.0</Package>
</Reference>
@ -53,6 +59,9 @@
<Reference Include="System.Reactive.Interfaces">
<HintPath>..\..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll</HintPath>
</Reference>
<Reference Include="atk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<Package>gtk-sharp-2.0</Package>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CairoPlatform.cs" />

21
Perspex-Mono.userprefs

@ -1,29 +1,28 @@
<Properties GitUserInfo="UsingGIT">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Perspex.Base/Threading/MainLoop.cs">
<Files>
<File FileName="Perspex.Base/Threading/MainLoop.cs" Line="18" Column="18" />
</Files>
<MonoDevelop.Ide.Workbench>
<Pads>
<Pad Id="ProjectPad">
<State expanded="True">
<Node name="Perspex.Base" expanded="True">
<Node name="Threading" expanded="True">
<Node name="MainLoop.cs" selected="True" />
<Node name="Gtk" expanded="True">
<Node name="Perspex.Gtk" expanded="True">
<Node name="Input" expanded="True" />
<Node name="WindowImpl.cs" selected="True" />
</Node>
</Node>
<Node name="Perspex.Base" expanded="True" />
<Node name="Perspex.Input" expanded="True">
<Node name="Raw" expanded="True" />
</Node>
<Node name="TestApplication-Mono" expanded="True" />
</State>
</Pad>
<Pad Id="MonoDevelop.Debugger.WatchPad">
<State />
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore>
<Breakpoint file="/home/steven/projects/Perspex/Gtk/Perspex.Gtk/Window.cs" line="56" column="1" />
<Breakpoint file="/home/steven/projects/Perspex/Perspex.Base/Threading/MainLoop.cs" line="60" column="1" />
<Catchpoint exceptionName="System.Exception" includeSubclasses="True" />
</BreakpointStore>
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />

Loading…
Cancel
Save