diff --git a/Cairo/Perspex.Cairo/CairoExtensions.cs b/Cairo/Perspex.Cairo/CairoExtensions.cs new file mode 100644 index 0000000000..791cc2de7d --- /dev/null +++ b/Cairo/Perspex.Cairo/CairoExtensions.cs @@ -0,0 +1,24 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Cairo +{ + using Cairo = global::Cairo; + + public static class CairoExtensions + { + public static Cairo.Matrix ToCairo(this Matrix m) + { + return new Cairo.Matrix(m.M11, m.M12, m.M21, m.M22, m.OffsetX, m.OffsetY); + } + + public static Cairo.Rectangle ToCairo(this Rect rect) + { + return new Cairo.Rectangle(rect.X, rect.Y, rect.Width, rect.Height); + } + } +} + diff --git a/Cairo/Perspex.Cairo/Media/DrawingContext.cs b/Cairo/Perspex.Cairo/Media/DrawingContext.cs index af96caa20b..200ec37ae1 100644 --- a/Cairo/Perspex.Cairo/Media/DrawingContext.cs +++ b/Cairo/Perspex.Cairo/Media/DrawingContext.cs @@ -3,6 +3,7 @@ // Copyright 2013 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- +using Perspex.Cairo.Media.Imaging; namespace Perspex.Cairo.Media { @@ -83,7 +84,10 @@ namespace Perspex.Cairo.Media public void DrawImage(IBitmap bitmap, double opacity, Rect sourceRect, Rect destRect) { - throw new NotImplementedException(); + var impl = bitmap.PlatformImpl as BitmapImpl; + this.context.SetSourceSurface(impl.Surface, 0, 0); + this.context.Rectangle(destRect.ToCairo()); + this.context.Fill(); } /// @@ -116,7 +120,7 @@ namespace Perspex.Cairo.Media public void DrawRectange(Pen pen, Rect rect) { this.SetPen(pen); - this.context.Rectangle(rect.X, rect.Y, rect.Width, rect.Height); + this.context.Rectangle(rect.ToCairo()); this.context.Stroke(); } @@ -142,7 +146,7 @@ namespace Perspex.Cairo.Media public void FillRectange(Perspex.Media.Brush brush, Rect rect) { this.SetBrush(brush); - this.context.Rectangle(rect.X, rect.Y, rect.Width, rect.Height); + this.context.Rectangle(rect.ToCairo()); this.context.Fill(); } @@ -153,7 +157,11 @@ namespace Perspex.Cairo.Media /// A disposable used to undo the clip rectangle. public IDisposable PushClip(Rect clip) { - return Disposable.Empty; + this.context.Save(); + this.context.Rectangle(clip.ToCairo()); + this.context.Clip(); + + return Disposable.Create(() => this.context.Restore()); } /// @@ -163,19 +171,10 @@ namespace Perspex.Cairo.Media /// A disposable used to undo the transformation. public IDisposable PushTransform(Matrix matrix) { - var m = Convert(matrix); - this.context.Transform(m); + this.context.Save(); + this.context.Transform(matrix.ToCairo()); - return Disposable.Create(() => - { - m.Invert(); - this.context.Transform(m); - }); - } - - private static Cairo.Matrix Convert(Matrix m) - { - return new Cairo.Matrix(m.M11, m.M12, m.M21, m.M22, m.OffsetX, m.OffsetY); + return Disposable.Create(() => this.context.Restore()); } private void SetBrush(Brush brush) diff --git a/Cairo/Perspex.Cairo/Media/Imaging/BitmapImpl.cs b/Cairo/Perspex.Cairo/Media/Imaging/BitmapImpl.cs index 6868002794..1b87ee8fd5 100644 --- a/Cairo/Perspex.Cairo/Media/Imaging/BitmapImpl.cs +++ b/Cairo/Perspex.Cairo/Media/Imaging/BitmapImpl.cs @@ -1,37 +1,42 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.Cairo.Media.Imaging -{ - using System; - using global::Cairo; - using Perspex.Platform; - - public class BitmapImpl : IBitmapImpl - { - private ImageSurface surface; - - public BitmapImpl(ImageSurface surface) - { - this.surface = surface; - } - - public int PixelWidth - { - get { return this.surface.Width; } - } - - public int PixelHeight - { - get { return this.surface.Height; } - } - - public void Save(string fileName) - { - throw new NotImplementedException(); - } - } -} +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Cairo.Media.Imaging +{ + using System; + using Perspex.Platform; + using Cairo = global::Cairo; + + public class BitmapImpl : IBitmapImpl + { + + public BitmapImpl(Cairo.ImageSurface surface) + { + this.Surface = surface; + } + + public int PixelWidth + { + get { return this.Surface.Width; } + } + + public int PixelHeight + { + get { return this.Surface.Height; } + } + + public Cairo.ImageSurface Surface + { + get; + private set; + } + + public void Save(string fileName) + { + throw new NotImplementedException(); + } + } +} diff --git a/Cairo/Perspex.Cairo/Perspex.Cairo.csproj b/Cairo/Perspex.Cairo/Perspex.Cairo.csproj index b9d063bd7b..a385fb600a 100644 --- a/Cairo/Perspex.Cairo/Perspex.Cairo.csproj +++ b/Cairo/Perspex.Cairo/Perspex.Cairo.csproj @@ -70,6 +70,7 @@ +