<commit_before>using Eto.Drawing;
using sd = System.Drawing;

#if DESKTOP
using MonoMac.CoreGraphics;

namespace Eto.Platform.Mac.Drawing
#else
using MonoTouch.CoreGraphics;
using MonoTouch.ImageIO;

namespace Eto.Platform.iOS.Drawing
#endif
{
	/// <summary>
	/// Handler for <see cref="ITextureBrush"/>
	/// </summary>
	/// <copyright>(c) 2012 by <NAME></copyright>
	/// <license type="BSD-3">See LICENSE for full terms</license>
	public class TextureBrushHandler : BrushHandler, ITextureBrush
	{
		class BrushObject
		{
			CGImage image;

			CGAffineTransform transform = CGAffineTransform.MakeIdentity();
			CGAffineTransform? viewTransform;
			readonly float [] alpha = { 1f };
			CGPattern pattern;
			static readonly CGColorSpace patternColorSpace = CGColorSpace.CreatePattern (null);

			public void Apply (GraphicsHandler graphics)
			{
				graphics.Control.SetFillColorSpace (patternColorSpace);

				// make current transform apply to the pattern
				var currentTransform = graphics.Control.GetCTM();

				if (graphics.DisplayView != null)
				{
					var pos = graphics.DisplayView.ConvertPointToView (sd.PointF.Empty, null);
					currentTransform.Translate(pos.X, pos.Y);
					graphics.Control.SetPatternPhase(new sd.SizeF(-pos.X, -pos.Y));
				}
				if (pattern == null || viewTransform != currentTransform) {
					viewTransform = currentTransform;
					SetPattern ();
				}

				graphics.Control.SetFillPattern (pattern, alpha);
			}
			
			public CGImage Image
			{
				get { return image; }
				set
				{
					image = value;
					ClearPattern();
				}
			}

			public float Opacity
			{
				get { return alpha[0]; }
				set { alpha[0] = value; }
			}
			
			public CGAffineTransform Transform
			{
				get { return transform; }
				set {
					transform = value;					
					ClearPattern();
				}
			}

			void ClearPattern()
			{
				if (pattern != null)
					pattern.Dispose();
					pattern = null;
				}

			void DrawPattern (CGContext context)
			{
				var destRect = new sd.RectangleF(0, 0, image.Width, image.Height);
				context.ConcatCTM (new CGAffineTransform (1, 0, 0, -1, 0, image.Height));
				context.DrawImage (destRect, image);
			}

			void SetPattern ()
			{
				var t = transform;
				if (viewTransform != null)
					t.Multiply (viewTransform.Value);
				ClearPattern();
				pattern = new CGPattern(new sd.RectangleF(0, 0, image.Width, image.Height), t, image.Width, image.Height, CGPatternTiling.ConstantSpacing, true, DrawPattern);
			}
		}

		public object Create (Image image, float opacity)
		{
			return new BrushObject {
				Image = image.ToCG (),
				Opacity = opacity
			};
		}

		public IMatrix GetTransform (TextureBrush widget)
		{
			return ((BrushObject)widget.ControlObject).Transform.ToEto ();
		}

		public void SetTransform (TextureBrush widget, IMatrix transform)
		{
			((BrushObject)widget.ControlObject).Transform = transform.ToCG ();
		}

		public override void Apply (object control, GraphicsHandler graphics)
		{
			((BrushObject)control).Apply (graphics);
		}

		public float GetOpacity (TextureBrush widget)
		{
			return ((BrushObject)widget.ControlObject).Opacity;
		}<commit_msg>Simplify code as part of debugging the texture brush on iOS.<commit_after>using Eto.Drawing;
using sd = System.Drawing;

#if DESKTOP
using MonoMac.CoreGraphics;

namespace Eto.Platform.Mac.Drawing
#else
using MonoTouch.CoreGraphics;
using MonoTouch.ImageIO;

namespace Eto.Platform.iOS.Drawing
#endif
{
	/// <summary>
	/// Handler for <see cref="ITextureBrush"/>
	/// </summary>
	/// <copyright>(c) 2012 by <NAME></copyright>
	/// <license type="BSD-3">See LICENSE for full terms</license>
	public class TextureBrushHandler : BrushHandler, ITextureBrush
	{
		class BrushObject
		{
			CGImage image;

			CGAffineTransform transform = CGAffineTransform.MakeIdentity();
			CGAffineTransform? viewTransform;
			readonly float [] alpha = { 1f };
			CGPattern pattern;
			static readonly CGColorSpace patternColorSpace = CGColorSpace.CreatePattern (null);

			public void Apply (GraphicsHandler graphics)
			{
				graphics.Control.SetFillColorSpace (patternColorSpace);

				// make current transform apply to the pattern
				var currentTransform = graphics.Control.GetCTM();

				if (graphics.DisplayView != null)
				{
					var pos = graphics.DisplayView.ConvertPointToView (sd.PointF.Empty, null);
					currentTransform.Translate(pos.X, pos.Y);
					graphics.Control.SetPatternPhase(new sd.SizeF(-pos.X, -pos.Y));
				}
				if (pattern == null || viewTransform != currentTransform) {
					viewTransform = currentTransform;
					// Create a new pattern.
					var t = transform;
					if (viewTransform != null)
						t.Multiply(viewTransform.Value);
					ClearPattern();
					pattern = new CGPattern(new sd.RectangleF(0, 0, image.Width, image.Height), t, image.Width, image.Height, CGPatternTiling.ConstantSpacing, true, DrawPattern);
				}

				graphics.Control.SetFillPattern (pattern, alpha);
			}
			
			public CGImage Image
			{
				get { return image; }
				set
				{
					image = value;
					ClearPattern();
				}
			}

			public float Opacity
			{
				get { return alpha[0]; }
				set { alpha[0] = value; }
			}
			
			public CGAffineTransform Transform
			{
				get { return transform; }
				set {
					transform = value;					
					ClearPattern();
				}
			}

			void ClearPattern()
			{
				if (pattern != null)
					pattern.Dispose();
					pattern = null;
				}

			void DrawPattern (CGContext context)
			{
				var destRect = new sd.RectangleF(0, 0, image.Width, image.Height);
				context.ConcatCTM (new CGAffineTransform (1, 0, 0, -1, 0, image.Height));
				context.DrawImage (destRect, image);
			}
		}

		public object Create (Image image, float opacity)
		{
			return new BrushObject {
				Image = image.ToCG (),
				Opacity = opacity
			};
		}

		public IMatrix GetTransform (TextureBrush widget)
		{
			return ((BrushObject)widget.ControlObject).Transform.ToEto ();
		}

		public void SetTransform (TextureBrush widget, IMatrix transform)
		{
			((BrushObject)widget.ControlObject).Transform = transform.ToCG ();
		}

		public override void Apply (object control, GraphicsHandler graphics)
		{
			((BrushObject)control).Apply (graphics);
		}

		public float GetOpacity (TextureBrush widget)
		{
			return ((BrushObject)widget.ControlObject).Opacity;
		}
{'fraction_non_alphanumeric': 0.11274976213130353, 'fraction_numerical': 0.005391690453536315, 'mean_word_length': 3.2243804420629605, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6435524', 'n_tokens_mistral': 2437, 'n_tokens_neox': 1884, 'n_words': 476}