Explorar el Código

Fixed cairo image tests.

Steven Kirk hace 10 años
padre
commit
0963f913f2
Se han modificado 2 ficheros con 29 adiciones y 21 borrados
  1. 21 5
      src/Gtk/Perspex.Cairo/Media/DrawingContext.cs
  2. 8 16
      src/Perspex.SceneGraph/Rect.cs

+ 21 - 5
src/Gtk/Perspex.Cairo/Media/DrawingContext.cs

@@ -42,6 +42,9 @@ namespace Perspex.Cairo.Media
             CurrentTransform = Matrix.Identity;
         }
 
+        /// <summary>
+        /// Gets the current transform of the drawing context.
+        /// </summary>
         public Matrix CurrentTransform
         {
             get; }
@@ -54,17 +57,30 @@ namespace Perspex.Cairo.Media
             _context.Dispose();
         }
 
+        /// <summary>
+        /// Draws a bitmap image.
+        /// </summary>
+        /// <param name="source">The bitmap image.</param>
+        /// <param name="opacity">The opacity to draw with.</param>
+        /// <param name="sourceRect">The rect in the image to draw.</param>
+        /// <param name="destRect">The rect in the output to draw to.</param>
         public void DrawImage(IBitmap bitmap, double opacity, Rect sourceRect, Rect destRect)
         {
             var impl = bitmap.PlatformImpl as BitmapImpl;
             var size = new Size(impl.PixelWidth, impl.PixelHeight);
-            var scaleX = destRect.Size.Width / sourceRect.Size.Width;
-            var scaleY = destRect.Size.Height / sourceRect.Size.Height;
+            var scale = new Vector(destRect.Width / sourceRect.Width, destRect.Height / sourceRect.Height);
 
             _context.Save();
-            _context.Scale(scaleX, scaleY);
-            Gdk.CairoHelper.SetSourcePixbuf(_context, impl.Surface, (int)sourceRect.X, (int)sourceRect.Y);
-            _context.Rectangle(sourceRect.ToCairo());
+            _context.Scale(scale.X, scale.Y);
+            destRect /= scale;
+
+            Gdk.CairoHelper.SetSourcePixbuf(
+                _context, 
+                impl.Surface, 
+                -sourceRect.X + destRect.X, 
+                -sourceRect.Y + destRect.Y);
+
+            _context.Rectangle(destRect.ToCairo());
             _context.Fill();
             _context.Restore();
         }

+ 8 - 16
src/Perspex.SceneGraph/Rect.cs

@@ -184,15 +184,11 @@ namespace Perspex
         /// <returns>The scaled rectangle.</returns>
         public static Rect operator *(Rect rect, Vector scale)
         {
-            double centerX = rect._x + (rect._width / 2);
-            double centerY = rect._y + (rect._height / 2);
-            double width = rect._width * scale.X;
-            double height = rect._height * scale.Y;
             return new Rect(
-                centerX - (width / 2),
-                centerY - (height / 2),
-                width,
-                height);
+                rect.X * scale.X,
+                rect.Y * scale.Y,
+                rect.Width * scale.X,
+                rect.Height * scale.Y);
         }
 
         /// <summary>
@@ -214,15 +210,11 @@ namespace Perspex
         /// <returns>The scaled rectangle.</returns>
         public static Rect operator /(Rect rect, Vector scale)
         {
-            double centerX = rect._x + (rect._width / 2);
-            double centerY = rect._y + (rect._height / 2);
-            double width = rect._width / scale.X;
-            double height = rect._height / scale.Y;
             return new Rect(
-                centerX - (width / 2),
-                centerY - (height / 2),
-                width,
-                height);
+                rect.X / scale.X, 
+                rect.Y / scale.Y, 
+                rect.Width / scale.X, 
+                rect.Height / scale.Y);
         }
 
         /// <summary>