public override void SetPattern(object backend, object p) { CGContextBackend gc = (CGContextBackend)backend; var toolkit = ApplicationContext.Toolkit; gc.CurrentStatus.Pattern = toolkit.GetSafeBackend(p); }
internal void DrawInContext(CGContextBackend ctx) { var s = ctx.Size != SizeF.Empty ? ctx.Size : Size; actx.InvokeUserCode(delegate { drawCallback(ctx, new Rectangle(0, 0, s.Width, s.Height)); }); }
internal void DrawInContext(CGContextBackend ctx, ImageDescription idesc) { var s = ctx.Size != CGSize.Empty ? ctx.Size : Size; actx.InvokeUserCode(delegate { drawCallback(ctx, new Rectangle(0, 0, s.Width, s.Height), Image, actx.Toolkit); }); }
internal void DrawInContext(CGContext ctx) { var backend = new CGContextBackend { Context = ctx, InverseViewTransform = ctx.GetCTM().Invert() }; DrawInContext(backend, Image); }
public override void SetColor(object backend, Xwt.Drawing.Color color) { CGContextBackend gc = (CGContextBackend)backend; gc.CurrentStatus.Pattern = null; CGContext ctx = gc.Context; ctx.SetFillColorSpace(Util.DeviceRGBColorSpace); ctx.SetStrokeColorSpace(Util.DeviceRGBColorSpace); ctx.SetFillColor((float)color.Red, (float)color.Green, (float)color.Blue, (float)color.Alpha); ctx.SetStrokeColor((float)color.Red, (float)color.Green, (float)color.Blue, (float)color.Alpha); }
public override void DrawInteriorWithFrame (RectangleF cellFrame, NSView inView) { CGContext ctx = NSGraphicsContext.CurrentContext.GraphicsPort; var backend = new CGContextBackend { Context = ctx, InverseViewTransform = ctx.GetCTM ().Invert () }; Frontend.ApplicationContext.InvokeUserCode (delegate { Frontend.Draw (backend, new Rectangle (cellFrame.X, cellFrame.Y, cellFrame.Width, cellFrame.Height)); }); }
public override void DrawInteriorWithFrame(RectangleF cellFrame, NSView inView) { CGContext ctx = NSGraphicsContext.CurrentContext.GraphicsPort; var backend = new CGContextBackend { Context = ctx, InverseViewTransform = ctx.GetCTM().Invert() }; cellView.ApplicationContext.InvokeUserCode(delegate { cellView.Draw(backend, new Rectangle(cellFrame.X, cellFrame.Y, cellFrame.Width, cellFrame.Height)); }); }
static CGAffineTransform GetContextTransform(CGContextBackend gc) { CGAffineTransform t = gc.Context.GetCTM(); // The CTM returned above actually includes the full view transform. // We only want the transform that is applied to the context, so concat // the inverse of the view transform to nullify that part. if (gc.InverseViewTransform.HasValue) { t.Multiply(gc.InverseViewTransform.Value); } return(t); }
public override void SetColor(object backend, Xwt.Drawing.Color color) { CGContextBackend gc = (CGContextBackend)backend; gc.CurrentStatus.Pattern = null; // Store the current color for TextLayout using NSLayoutManager gc.CurrentStatus.GlobalColor = color.ToCGColor(); CGContext ctx = gc.Context; ctx.SetFillColorSpace(Util.DeviceRGBColorSpace); ctx.SetStrokeColorSpace(Util.DeviceRGBColorSpace); ctx.SetFillColor((float)color.Red, (float)color.Green, (float)color.Blue, (float)color.Alpha); ctx.SetStrokeColor((float)color.Red, (float)color.Green, (float)color.Blue, (float)color.Alpha); }
public override void AppendPath(object backend, object otherBackend) { CGPath dest = (CGPath)backend; CGContextBackend src = otherBackend as CGContextBackend; if (src != null) { using (var path = src.Context.CopyPath()) dest.AddPath(path); } else { dest.AddPath((CGPath)otherBackend); } }
public override void DrawRect(CGRect dirtyRect) { Backend.Load(this); Frontend.ApplicationContext.InvokeUserCode(delegate { CGContext ctx = NSGraphicsContext.CurrentContext.GraphicsPort; var backend = new CGContextBackend { Context = ctx, InverseViewTransform = ctx.GetCTM().Invert() }; var bounds = Backend.CellBounds; backend.Context.ClipToRect(dirtyRect); backend.Context.TranslateCTM((nfloat)(-bounds.X), (nfloat)(-bounds.Y)); Frontend.Draw(backend, new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height)); }); }
public override void DrawRect(System.Drawing.RectangleF dirtyRect) { context.InvokeUserCode(delegate { CGContext ctx = NSGraphicsContext.CurrentContext.GraphicsPort; //fill BackgroundColor ctx.SetFillColor(Backend.Frontend.BackgroundColor.ToCGColor()); ctx.FillRect(Bounds); var backend = new CGContextBackend { Context = ctx, InverseViewTransform = ctx.GetCTM().Invert() }; eventSink.OnDraw(backend, new Rectangle(dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height)); }); }
public override object ConvertToBitmap(object handle, int pixelWidth, int pixelHeight, ImageFormat format) { if (handle is CustomImage) { var flags = CGBitmapFlags.ByteOrderDefault; int bytesPerRow; switch (format) { case ImageFormat.ARGB32: bytesPerRow = pixelWidth * 4; flags |= CGBitmapFlags.PremultipliedFirst; break; case ImageFormat.RGB24: bytesPerRow = pixelWidth * 3; flags |= CGBitmapFlags.None; break; default: throw new NotImplementedException("ImageFormat: " + format.ToString()); } var bmp = new CGBitmapContext(IntPtr.Zero, pixelWidth, pixelHeight, 8, bytesPerRow, Util.DeviceRGBColorSpace, flags); bmp.TranslateCTM(0, pixelHeight); bmp.ScaleCTM(1, -1); var ctx = new CGContextBackend { Context = bmp, Size = new SizeF(pixelWidth, pixelHeight), InverseViewTransform = bmp.GetCTM().Invert() }; var ci = (CustomImage)handle; ci.DrawInContext(ctx); var img = new NSImage(((CGBitmapContext)bmp).ToImage(), new SizeF(pixelWidth, pixelHeight)); var imageData = img.AsTiff(); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData(imageData); var im = new NSImage(); im.AddRepresentation(imageRep); return(im); } else { return(handle); } }
void SetupPattern(CGContextBackend gc) { gc.Context.SetPatternPhase(new CGSize(0, 0)); if (gc.CurrentStatus.Pattern is GradientInfo) { return; } if (gc.CurrentStatus.Pattern is ImagePatternInfo) { var pi = (ImagePatternInfo)gc.CurrentStatus.Pattern; var bounds = new CGRect(CGPoint.Empty, new CGSize(pi.Image.Size.Width, pi.Image.Size.Height)); var t = CGAffineTransform.Multiply(CGAffineTransform.MakeScale(1f, -1f), gc.Context.GetCTM()); CGPattern pattern; if (pi.Image is CustomImage) { pattern = new CGPattern(bounds, t, bounds.Width, bounds.Height, CGPatternTiling.ConstantSpacing, true, c => { c.TranslateCTM(0, bounds.Height); c.ScaleCTM(1f, -1f); ((CustomImage)pi.Image).DrawInContext(c); }); } else { var empty = CGRect.Empty; CGImage cgimg = pi.Image.AsCGImage(ref empty, null, null); pattern = new CGPattern(bounds, t, bounds.Width, bounds.Height, CGPatternTiling.ConstantSpacing, true, c => c.DrawImage(bounds, cgimg)); } using (pattern) { CGContext ctx = gc.Context; var alpha = new[] { (nfloat)pi.Alpha }; ctx.SetFillColorSpace(Util.PatternColorSpace); ctx.SetStrokeColorSpace(Util.PatternColorSpace); ctx.SetFillPattern(pattern, alpha); ctx.SetStrokePattern(pattern, alpha); } } }
public override void Fill(object backend) { CGContextBackend gc = (CGContextBackend)backend; CGContext ctx = gc.Context; SetupContextForDrawing(ctx); if (gc.CurrentStatus.Pattern is GradientInfo) { MacGradientBackendHandler.Draw(ctx, ((GradientInfo)gc.CurrentStatus.Pattern)); } else if (gc.CurrentStatus.Pattern is ImagePatternInfo) { SetupPattern(gc); ctx.DrawPath(CGPathDrawingMode.Fill); } else { ctx.DrawPath(CGPathDrawingMode.Fill); } }
internal void DrawInContext(CGContext ctx) { var backend = new CGContextBackend { Context = ctx, InverseViewTransform = ctx.GetCTM ().Invert () }; DrawInContext (backend); }
internal void DrawInContext(CGContextBackend ctx) { DrawInContext(ctx, Image); }
public override object ConvertToBitmap(object handle, int pixelWidth, int pixelHeight, ImageFormat format) { if (handle is CustomImage) { var flags = CGBitmapFlags.ByteOrderDefault; int bytesPerRow; switch (format) { case ImageFormat.ARGB32: bytesPerRow = pixelWidth * 4; flags |= CGBitmapFlags.PremultipliedFirst; break; case ImageFormat.RGB24: bytesPerRow = pixelWidth * 3; flags |= CGBitmapFlags.None; break; default: throw new NotImplementedException ("ImageFormat: " + format.ToString ()); } var bmp = new CGBitmapContext (IntPtr.Zero, pixelWidth, pixelHeight, 8, bytesPerRow, Util.DeviceRGBColorSpace, flags); bmp.TranslateCTM (0, pixelHeight); bmp.ScaleCTM (1, -1); var ctx = new CGContextBackend { Context = bmp, Size = new SizeF (pixelWidth, pixelHeight), InverseViewTransform = bmp.GetCTM ().Invert () }; var ci = (CustomImage)handle; ci.DrawInContext (ctx); var img = new NSImage (((CGBitmapContext)bmp).ToImage (), new SizeF (pixelWidth, pixelHeight)); var imageData = img.AsTiff (); var imageRep = (NSBitmapImageRep) NSBitmapImageRep.ImageRepFromData (imageData); var im = new NSImage (); im.AddRepresentation (imageRep); return im; } else return handle; }
public override object ConvertToBitmap(ImageDescription idesc, double scaleFactor, ImageFormat format) { double width = idesc.Size.Width; double height = idesc.Size.Height; int pixelWidth = (int)(width * scaleFactor); int pixelHeight = (int)(height * scaleFactor); if (idesc.Backend is CustomImage) { var flags = CGBitmapFlags.ByteOrderDefault; int bytesPerRow; switch (format) { case ImageFormat.ARGB32: bytesPerRow = pixelWidth * 4; flags |= CGBitmapFlags.PremultipliedFirst; break; case ImageFormat.RGB24: bytesPerRow = pixelWidth * 3; flags |= CGBitmapFlags.None; break; default: throw new NotImplementedException("ImageFormat: " + format.ToString()); } var bmp = new CGBitmapContext(IntPtr.Zero, pixelWidth, pixelHeight, 8, bytesPerRow, Util.DeviceRGBColorSpace, flags); bmp.TranslateCTM(0, pixelHeight); bmp.ScaleCTM((float)scaleFactor, (float)-scaleFactor); var ctx = new CGContextBackend { Context = bmp, Size = new CGSize((nfloat)width, (nfloat)height), InverseViewTransform = bmp.GetCTM().Invert(), ScaleFactor = scaleFactor }; var ci = (CustomImage)idesc.Backend; ci.DrawInContext(ctx, idesc); var img = new NSImage(((CGBitmapContext)bmp).ToImage(), new CGSize(pixelWidth, pixelHeight)); var imageData = img.AsTiff(); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData(imageData); var im = new NSImage(); im.AddRepresentation(imageRep); im.Size = new CGSize((nfloat)width, (nfloat)height); bmp.Dispose(); return(im); } else { NSImage img = (NSImage)idesc.Backend; NSBitmapImageRep bitmap = img.Representations().OfType <NSBitmapImageRep> ().FirstOrDefault(); if (bitmap == null) { var imageData = img.AsTiff(); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData(imageData); var im = new NSImage(); im.AddRepresentation(imageRep); im.Size = new CGSize((nfloat)width, (nfloat)height); return(im); } return(idesc.Backend); } }
static CGContext SetupContextForDrawing(CGContextBackend gc, out bool needsRestore) { CGContext ctx = gc.Context; if (!ctx.IsPathEmpty ()) { var drawPoint = ctx.GetCTM ().TransformPoint (ctx.GetPathBoundingBox ().Location); var patternPhase = new SizeF (drawPoint.X, drawPoint.Y); if (patternPhase != SizeF.Empty) ctx.SetPatternPhase (patternPhase); } if (gc.ClipPath == null) { needsRestore = false; return ctx; } ctx.SaveState (); using (CGPath oldPath = ctx.CopyPath ()) { ctx.BeginPath (); ctx.AddPath (gc.ClipPath); ctx.Clip (); ctx.AddPath (oldPath); } needsRestore = true; return ctx; }
void SetupPattern(CGContextBackend gc) { gc.Context.SetPatternPhase (new SizeF (0, 0)); if (gc.CurrentStatus.Pattern is GradientInfo) return; if (gc.CurrentStatus.Pattern is ImagePatternInfo) { var pi = (ImagePatternInfo) gc.CurrentStatus.Pattern; RectangleF bounds = new RectangleF (PointF.Empty, new SizeF (pi.Image.Size.Width, pi.Image.Size.Height)); var t = CGAffineTransform.Multiply (CGAffineTransform.MakeScale (1f, -1f), gc.Context.GetCTM ()); CGPattern pattern; if (pi.Image is CustomImage) { pattern = new CGPattern (bounds, t, bounds.Width, bounds.Height, CGPatternTiling.ConstantSpacing, true, c => { c.TranslateCTM (0, bounds.Height); c.ScaleCTM (1f, -1f); ((CustomImage)pi.Image).DrawInContext (c); }); } else { RectangleF empty = RectangleF.Empty; CGImage cgimg = ((NSImage)pi.Image).AsCGImage (ref empty, null, null); pattern = new CGPattern (bounds, CGAffineTransform.MakeScale (1f, -1f), bounds.Width, bounds.Height, CGPatternTiling.ConstantSpacing, true, c => c.DrawImage (bounds, cgimg)); } CGContext ctx = gc.Context; float[] alpha = new[] { 1.0f }; ctx.SetFillColorSpace (Util.PatternColorSpace); ctx.SetStrokeColorSpace (Util.PatternColorSpace); ctx.SetFillPattern (pattern, alpha); ctx.SetStrokePattern (pattern, alpha); } }
static CGAffineTransform GetContextTransform(CGContextBackend gc) { CGAffineTransform t = gc.Context.GetCTM (); // The CTM returned above actually includes the full view transform. // We only want the transform that is applied to the context, so concat // the inverse of the view transform to nullify that part. if (gc.InverseViewTransform.HasValue) t.Multiply (gc.InverseViewTransform.Value); return t; }
internal static void Draw(CGContextBackend ctx, object layout, double x, double y) { LayoutInfo li = (LayoutInfo)layout; li.Draw(ctx.Context, ctx.CurrentStatus.GlobalColor, x, y); }
internal void DrawInContext (CGContextBackend ctx) { DrawInContext (ctx, Image); }
internal void DrawInContext (CGContextBackend ctx, ImageDescription idesc) { var s = ctx.Size != CGSize.Empty ? ctx.Size : Size; actx.InvokeUserCode (delegate { drawCallback (ctx, new Rectangle (0, 0, s.Width, s.Height), Image, actx.Toolkit); }); }
public override void SetPattern(object backend, object p) { CGContextBackend gc = (CGContextBackend)backend; gc.CurrentStatus.Pattern = p; }
internal void DrawInContext(CGContextBackend ctx) { var s = ctx.Size != SizeF.Empty ? ctx.Size : Size; actx.InvokeUserCode (delegate { drawCallback (ctx, new Rectangle (0, 0, s.Width, s.Height)); }); }
public override object ConvertToBitmap(object handle, double width, double height, double scaleFactor, ImageFormat format) { int pixelWidth = (int)(width * scaleFactor); int pixelHeight = (int)(height * scaleFactor); if (handle is CustomImage) { var flags = CGBitmapFlags.ByteOrderDefault; int bytesPerRow; switch (format) { case ImageFormat.ARGB32: bytesPerRow = pixelWidth * 4; flags |= CGBitmapFlags.PremultipliedFirst; break; case ImageFormat.RGB24: bytesPerRow = pixelWidth * 3; flags |= CGBitmapFlags.None; break; default: throw new NotImplementedException ("ImageFormat: " + format.ToString ()); } var bmp = new CGBitmapContext (IntPtr.Zero, pixelWidth, pixelHeight, 8, bytesPerRow, Util.DeviceRGBColorSpace, flags); bmp.TranslateCTM (0, pixelHeight); bmp.ScaleCTM ((float)scaleFactor, (float)-scaleFactor); var ctx = new CGContextBackend { Context = bmp, Size = new SizeF ((float)width, (float)height), InverseViewTransform = bmp.GetCTM ().Invert (), ScaleFactor = scaleFactor }; var ci = (CustomImage)handle; ci.DrawInContext (ctx); var img = new NSImage (((CGBitmapContext)bmp).ToImage (), new SizeF (pixelWidth, pixelHeight)); var imageData = img.AsTiff (); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData (imageData); var im = new NSImage (); im.AddRepresentation (imageRep); im.Size = new SizeF ((float)width, (float)height); bmp.Dispose (); return im; } else { NSImage img = (NSImage)handle; NSBitmapImageRep bitmap = img.Representations ().OfType<NSBitmapImageRep> ().FirstOrDefault (); if (bitmap == null) { var imageData = img.AsTiff (); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData (imageData); var im = new NSImage (); im.AddRepresentation (imageRep); im.Size = new SizeF ((float)width, (float)height); return im; } return handle; } }