Inheritance: INativeObject, IDisposable
Exemplo n.º 1
0
		public static void RenderGlyphToContext (CGContext context, PointF position, Glyph g, Fnt font, byte[] palette, int offset)
		{
			byte[] buf = new byte[g.Width * g.Height * 4];
			int i = 0;

			for (int y = g.Height - 1; y >= 0; y--) {
				for (int x = g.Width - 1; x >= 0; x--) {
					if (g.Bitmap[y,x] == 0)
						buf [i + 0] = 0;
					else if (g.Bitmap[y,x] == 1)
						buf [i + 0] = 255;
					else
						buf [i + 0] = 128;

					buf[i + 1] = palette[ (g.Bitmap[y,x]) * 3 + offset + 0];
					buf[i + 2] = palette[ (g.Bitmap[y,x]) * 3 + offset + 1];
					buf[i + 3] = palette[ (g.Bitmap[y,x]) * 3 + offset + 2];

					if (buf[i+1] == 252 && buf[i+2] == 0 && buf[i+3] == 252)
						buf[i + 0] = 0;

					i += 4;
				}
			}

			CGImage glyphImage = CreateImage (buf, (ushort)g.Width, (ushort)g.Height, 32, g.Width * 4);
			
			context.DrawImage (new RectangleF (position, new SizeF (g.Width, g.Height)), glyphImage);
		}
Exemplo n.º 2
0
 public Graphics(CGContext context, bool flipped = true)
 {
     if (context == null)
         throw new ArgumentNullException ("context");
     isFlipped = flipped;
     InitializeContext(context);
 }
Exemplo n.º 3
0
 public CocoaGraphDrawer(CGContext g, int w, int h)
 {
     this.g = g;
     this.w = w;
     this.h = h;
     g.SetAllowsAntialiasing(true);
     g.InterpolationQuality = CGInterpolationQuality.High;
 }
Exemplo n.º 4
0
 public static void FillEllipsis(CGContext context, RectangleF rect, CGColor color, float lineWidth)
 {
     context.SaveState();
     context.SetFillColor(color);
     context.AddEllipseInRect(rect);
     context.FillPath();
     context.RestoreState();
 }
Exemplo n.º 5
0
		public override void DrawInContext (CGContext context)
		{
			base.DrawInContext (context);
			
			context.AddEllipseInRect (Bounds);
			context.SetFillColor (ClockColor);
			context.FillPath ();
		}
Exemplo n.º 6
0
 public GraphicsContextWrapper(CGContext context, float boundsWidth, float boundsHeight, BasicRectangle dirtyRect)
 {
     Context = context;
     BoundsWidth = boundsWidth;
     BoundsHeight = boundsHeight;
     DirtyRect = dirtyRect;
     Density = GetDisplayScale();
 }
Exemplo n.º 7
0
 public static void FillRect(CGContext context, RectangleF rect, CGColor color)
 {
     context.SaveState();
     context.AddRect(rect);
     context.Clip();
     context.SetFillColor(color);
     context.FillRect(rect);
     context.RestoreState();
 }
Exemplo n.º 8
0
 public static void DrawEllipsis(CGContext context, RectangleF rect, CGColor color, float lineWidth)
 {
     context.SaveState();
     context.SetStrokeColor(color);
     context.SetLineWidth(lineWidth);
     context.AddEllipseInRect(rect);
     context.StrokePath();
     context.RestoreState();
 }
Exemplo n.º 9
0
 public static void DrawRect(CGContext context, RectangleF rect, CGColor color, float lineWidth)
 {
     context.SaveState();
     context.AddRect(rect);
     context.Clip();
     context.SetLineWidth(lineWidth);
     context.SetStrokeColor(color);
     context.StrokeRect(rect);
     context.RestoreState();
 }
Exemplo n.º 10
0
		internal static void Draw (CGContext ctx, GradientInfo gradient)
		{
			ctx.SaveState ();
			ctx.Clip ();
			using (var cg = new CGGradient (Util.DeviceRGBColorSpace, gradient.Colors.ToArray (), gradient.Stops.ToArray ())) {
				if (gradient.Linear)
					ctx.DrawLinearGradient (cg, gradient.Start, gradient.End, CGGradientDrawingOptions.DrawsBeforeStartLocation | CGGradientDrawingOptions.DrawsAfterEndLocation);
				else
					ctx.DrawRadialGradient (cg, gradient.Start, gradient.StartRadius, gradient.End, gradient.EndRadius, CGGradientDrawingOptions.DrawsBeforeStartLocation | CGGradientDrawingOptions.DrawsAfterEndLocation);
			}
			ctx.RestoreState ();
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="CoreGraphicsRenderContext"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public CoreGraphicsRenderContext(CGContext context)
        {
            this.gctx = context;

            // Set rendering quality
            this.gctx.SetAllowsFontSmoothing (true);
            this.gctx.SetAllowsFontSubpixelQuantization (true);
            this.gctx.SetAllowsAntialiasing (true);
            this.gctx.SetShouldSmoothFonts (true);
            this.gctx.SetShouldAntialias (true);
            this.gctx.InterpolationQuality = CGInterpolationQuality.High;
            this.gctx.SetTextDrawingMode (CGTextDrawingMode.Fill);
            this.gctx.TextMatrix = CGAffineTransform.MakeScale (1, 1);
        }
Exemplo n.º 12
0
        public static float MeasureStringWidth(CGContext context, string text, string fontName, float fontSize)
        {
            if (string.IsNullOrEmpty(text))
                return 0;

            context.SaveState();
            PointF pos = context.TextPosition;
            context.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman);
            context.TextMatrix = CGAffineTransform.MakeScale(1.0f, -1.0f);
            //context.TranslateCTM(0, 20);
            context.ScaleCTM(1, -1);
            context.SetTextDrawingMode(CGTextDrawingMode.Invisible);
            context.ShowTextAtPoint(pos.X, pos.Y, text);
            PointF pos2 = context.TextPosition;
            context.RestoreState();
            
            return pos2.X - pos.X;
        }
Exemplo n.º 13
0
 public static SizeF MeasureText(CGContext context, string text, string fontName, float fontSize)
 {
     NSString str = new NSString(text);
     var dict = new NSMutableDictionary();
     dict.Add(NSAttributedString.FontAttributeName, NSFont.FromFontName(fontName, fontSize));
     var size = str.StringSize(dict);
     return size;
 }
Exemplo n.º 14
0
 public static void DrawText(CGContext context, string text, string fontName, float fontSize, float translateHeight, float x, float y)
 {
     context.SaveState();
     context.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman);
     context.SetTextDrawingMode(CGTextDrawingMode.Fill);
     context.SetFillColor(new CGColor(1, 1));
     context.SetStrokeColor(new CGColor(1.0f, 1.0f));
     //context.AddRect(rectText);
     //context.Clip();
     context.TextMatrix = CGAffineTransform.MakeScale(1.0f, -1.0f);
     context.TranslateCTM(0, translateHeight);
     context.ScaleCTM(1, -1);
     context.ShowTextAtPoint(x, y, text);
     context.RestoreState();
 }
Exemplo n.º 15
0
 public static void FillGradient(CGContext context, RectangleF rect, CGColor color1, CGColor color2, bool isHorizontal)
 {
     CGGradient gradientBackground;
     CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
     
     float[] locationListBackground = new float[] { 1.0f, 0.0f };
     List<float> colorListBackground = new List<float>();
     colorListBackground.AddRange(color1.Components);
     colorListBackground.AddRange(color2.Components);
     gradientBackground = new CGGradient(colorSpace, colorListBackground.ToArray(), locationListBackground);
     
     context.SaveState();
     context.AddRect(rect);
     context.Clip();
     //context.ScaleCTM(1, -1);
     if(isHorizontal)
         context.DrawLinearGradient(gradientBackground, new PointF(rect.X, rect.Y), new PointF(rect.X + rect.Width, rect.Y + rect.Height), CGGradientDrawingOptions.DrawsBeforeStartLocation);
     else
         context.DrawLinearGradient(gradientBackground, new PointF(0, 0), new PointF(0, rect.Height), CGGradientDrawingOptions.DrawsBeforeStartLocation);
     context.RestoreState();
 }       
Exemplo n.º 16
0
 public static void DrawTextInRect(CGContext context, RectangleF rect, string text, string fontName, float fontSize, NSColor fontColor)
 {
     context.SaveState();
     NSString str = new NSString(text);
     var dict = new NSMutableDictionary();
     dict.Add(NSAttributedString.ForegroundColorAttributeName, fontColor);
     dict.Add(NSAttributedString.FontAttributeName, NSFont.FromFontName(fontName, fontSize));
     str.DrawString(rect, dict);
     context.RestoreState();
 }
Exemplo n.º 17
0
 public static void StrokePath(CGContext context, CGPath path, float pathWidth, CGColor color)
 {
     context.SaveState();
     context.SetLineWidth(pathWidth);
     context.SetStrokeColor(color);
     context.AddPath(path);
     context.StrokePath();
     context.RestoreState();
 }
Exemplo n.º 18
0
 public static void EOFillPath(CGContext context, CGPath path, CGColor color)
 {
     context.SaveState();
     context.SetFillColor(color);
     context.AddPath(path);
     context.EOFillPath();
     context.RestoreState();
 }
        /// <summary>
        /// Rasterizes the triangle specified by the vector / points and their associated colors
        /// using barycentric coordinates.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="vt1"></param>
        /// <param name="vt2"></param>
        /// <param name="vt3"></param>
        /// <param name="colorV1"></param>
        /// <param name="colorV2"></param>
        /// <param name="colorV3"></param>
        internal void RasterizeTriangle(CGContext context, PointF vt1, PointF vt2, PointF vt3, Color colorV1, Color colorV2, Color colorV3)
        {
            // get the bounding box of the triangle
            int maxX = (int)Math.Max(vt1.X, Math.Max(vt2.X, vt3.X));
            int minX = (int)Math.Min(vt1.X, Math.Min(vt2.X, vt3.X));
            int maxY = (int)Math.Max(vt1.Y, Math.Max(vt2.Y, vt3.Y));
            int minY = (int)Math.Min(vt1.Y, Math.Min(vt2.Y, vt3.Y));

            // Barycentric coordinates at minX/minY corner
            PointF pm = new PointF( minX, minY );

            var edge32 = new Edge(vt3, vt2, vt1, pm, colorV1);
            var edge13 = new Edge (vt1, vt3, vt2, pm, colorV2);
            var edge21 = new Edge (vt2, vt1, vt3, pm, colorV3);

            int span32 = edge32.EdgeOrigin;
            int span13 = edge13.EdgeOrigin;
            int span21 = edge21.EdgeOrigin;

            edge32Red = colorV1.R;
            edge32Green = colorV1.G;
            edge32Blue = colorV1.B;
            edge32Alpha = colorV1.A;

            edge13Red = colorV2.R;
            edge13Green = colorV2.G;
            edge13Blue = colorV2.B;
            edge13Alpha = colorV2.A;

            edge21Red = colorV3.R;
            edge21Green = colorV3.G;
            edge21Blue = colorV3.B;
            edge21Alpha = colorV3.A;

            int span32XOffset = 0;
            int span13XOffset = 0;
            int span21XOffset = 0;

            bool inside = false;
            int mask = 0;
            //  Iterate over each pixel of bounding box and check if it's inside
            //  the triangle using the barycentirc approach.
            for (int y = minY; y <= maxY; y += Edge.StepYSize)
            {
                // Barycentric coordinates at start of row
                span32XOffset = span32;
                span13XOffset = span13;
                span21XOffset = span21;

                inside = false;
                for (int x = minX; x <= maxX; x += Edge.StepXSize)
                {

                    mask = span32XOffset | span13XOffset | span21XOffset;

                    // If p is on or inside all edges for any pixels,
                    // render those pixels.
                    if (mask >= 0)
                    {
                        if (!inside)
                        {
                            inside = true;
                        }
                        RenderPixels(context, x, y, edge32, edge13, edge21, span32XOffset, span13XOffset, span21XOffset);
                    }

                    // Step to the right
                    span32XOffset += edge32.StepX;
                    span13XOffset += edge13.StepX;
                    span21XOffset += edge21.StepX;
                    if (mask < 0 && inside)
                    {
                        inside = false;
                        break;
                    }

                }

                // Row step
                span32 += edge32.StepY;
                span13 += edge13.StepY;
                span21 += edge21.StepY;
            }
        }
        internal void RasterizePolygon(CGContext context, PointF center, PointF[] pathPoints,
		                                      Color[] surroundColors, Color centerColor)
        {
            var last = pathPoints[0];

            Color start = Color.Empty;
            Color end = Color.Empty;
            var count = pathPoints.Length - 1;
            var colorCount = surroundColors.Length;
            var startIndex = 0;
            var endIndex = 1;

            //			// Create new stopwatch
            //			var stopwatch = new System.Diagnostics.Stopwatch ();
            //
            //			// Begin timing
            //			stopwatch.Start();

            for (int p = 1; p <= count; p++)
            {

                var next = pathPoints[p];

                if (startIndex >= colorCount)
                {
                    start = surroundColors[colorCount - 1];
                    end = surroundColors[colorCount - 1];
                }
                else
                {
                    start = surroundColors[startIndex++];
                    if (startIndex == colorCount)
                    {
                        end = surroundColors[0];
                    }
                    else
                    {
                        if (endIndex >= colorCount)
                        {
                            end = surroundColors[colorCount - 1];
                        }
                        else
                        {
                            end = surroundColors[endIndex++];
                        }
                    }
                }

                //Console.WriteLine("triangle {0} P1 {1} P2 {2} P3 {3} color {4}", p, last, next, center, start);
                if (polygonWinding == FillMode.Winding)
                    RasterizeTriangle(context, center, last, next, centerColor, start, end);
                else
                    RasterizeTriangle(context, last, center, next, start, centerColor, end);

                last = next;

            }

            //			// Stop timing
            //			stopwatch.Stop();
            //
            //			// Write result
            //			Console.WriteLine("Time elapsed: {0}",
            //				stopwatch.Elapsed);
        }
        void RenderPixels(CGContext context, int x, int y, Edge edge32, Edge edge13, Edge edge21, int w1, int w2, int w3)
        {
            //VertexInterpoliation = A * x + B * y + C;
            //			float alpha = (edge32.A * x + edge32.B * y + edge32.C) * edge32.VertexFactor;
            //			float beta = (edge13.A * x + edge13.B * y + edge13.C)  * edge13.VertexFactor;
            //			float gamma = (edge21.A * x + edge21.B * y + edge21.C) * edge21.VertexFactor;

            // Determine barycentric coordinates
            float alpha = (float)(w1 * edge32.VertexFactor);
            float beta = (float)(w2 * edge13.VertexFactor);
            float gamma = (float)(w3 * edge21.VertexFactor);

            GradientLerp3 (alpha, beta, gamma);
            // Set the color
            context.SetFillColor (colorOutput [0], colorOutput [1], colorOutput [2], colorOutput [3]);

            // Set our pixel location
            pixelRect.X = x;
            pixelRect.Y = y;

            // Fill the pixel
            context.FillRect (pixelRect);
        }
Exemplo n.º 22
0
        public static void DrawLine(CGContext context, List<PointF> points, CGColor color, float lineWidth, bool closePath, bool dashed)
        {
            if (points == null)
                throw new NullReferenceException();

            if (points.Count == 0)
                throw new ArgumentException("The line must have at least one point.");

            context.SaveState();
            context.SetStrokeColor(color);
            context.SetLineWidth(lineWidth);
            context.MoveTo(points[0].X, points[0].Y);
            for(int a = 1; a < points.Count; a++)
                context.AddLineToPoint(points[a].X, points[a].Y);
            if (dashed)
                context.SetLineDash(0, new float[2] { 1, 2 }, 2);
            if (closePath)
                context.ClosePath();
            context.StrokePath();
            context.RestoreState();
        }
Exemplo n.º 23
0
 public static CGLayer Create(CGContext context, SizeF size)
 {
     return(new CGLayer(CGLayerCreateWithContext(context.Handle, size, IntPtr.Zero), true));
 }
Exemplo n.º 24
0
		public static void RenderTextToContext (CGContext context, PointF position, string text, Fnt font, byte[] palette, int offset)
		{
			int i;
			
			/* create a run of text, for now ignoring any control codes in the string */
			StringBuilder run = new StringBuilder ();
			for (i = 0; i < text.Length; i ++) {
				if ((text[i] == 0x0a /* allow newlines */||
				    !Char.IsControl (text[i])) &&
					(text[i] >= font.LowIndex && text[i] <= font.HighIndex)) {
					run.Append (text[i]);
				}
			}

			string rs = run.ToString ();
			byte[] r = Encoding.ASCII.GetBytes (rs);

			/* the draw it */
			for (i = 0; i < r.Length; i ++) {
				int glyph_width = 0;
				Glyph g = null;

				if (r[i] == 0x20)  /* space */{
					glyph_width = font.SpaceSize;
				}
				else {
					g = font[r[i]-1];
					glyph_width = g.Width + g.XOffset;

					RenderGlyphToContext (context, position, g, font, palette, offset);
				}

				position.X += glyph_width;
			}
		}
Exemplo n.º 25
0
        static void SetupContextForDrawing(CGContext ctx)
        {
            if (ctx.IsPathEmpty ())
                return;

            // setup pattern drawing to better match the behavior of Cairo
            var drawPoint = ctx.GetCTM ().TransformPoint (ctx.GetPathBoundingBox ().Location);
            var patternPhase = new SizeF (drawPoint.X, drawPoint.Y);
            if (patternPhase != SizeF.Empty)
                ctx.SetPatternPhase (patternPhase);
        }
Exemplo n.º 26
0
 public static void DrawLine(CGContext context, PointF[] points, float lineWidth, CGColor color)
 {
     context.SaveState();
     var path = new CGPath();
     path.AddLines(points);
     context.AddPath(path);
     context.SetLineWidth(lineWidth);
     context.SetStrokeColor(color);
     context.StrokePath();
     context.RestoreState();
 }
Exemplo n.º 27
0
 public static CIContext FromContext(CGContext ctx)
 {
     return FromContext (ctx, (CIContextOptions) null);
 }
Exemplo n.º 28
0
        public void Draw(CGContext ctx)
        {
            if (ctx == null)
                throw new ArgumentNullException ("ctx");

            CTFrameDraw (handle, ctx.Handle);
        }
Exemplo n.º 29
0
		internal static void Draw (CGContext ctx, object layout, double x, double y)
		{
			LayoutInfo li = (LayoutInfo)layout;
			using (CTFrame frame = CreateFrame (li)) {
				if (frame == null)
					return;

				CTLine ellipsis = null;
				bool ellipsize = li.Width.HasValue && li.TextTrimming == TextTrimming.WordElipsis;
				if (ellipsize)
					ellipsis = new CTLine (CreateAttributedString (li, "..."));

				float lineHeight = li.Font.Ascender - li.Font.Descender + li.Font.Leading;

				ctx.SaveState ();
				ctx.TextMatrix = CGAffineTransform.MakeScale (1f, -1f);
				ctx.TranslateCTM ((float)x, (float)y + li.Font.Ascender);
				foreach (var line in frame.GetLines ()) {
					ctx.TextPosition = PointF.Empty;
					if (ellipsize) // we need to create a new CTLine here because the framesetter already truncated the text for the line
						new CTLine (CreateAttributedString (li, li.Text.Substring (line.StringRange.Location)))
							.GetTruncatedLine (li.Width.Value, CTLineTruncation.End, ellipsis).Draw (ctx);
					else
						line.Draw (ctx);
					ctx.TranslateCTM (0, lineHeight);
				}
				ctx.RestoreState ();
			}
		}
Exemplo n.º 30
0
		public override void DrawInContext (CGContext ctx)
		{
			current_frame = current_frame % frames.Length;

			if (frames[current_frame] == null)
				frames[current_frame] = GuiUtil.CGImageFromBitmap (grp.GetFrame (current_frame),
											   grp.Width, grp.Height,
											   palette,
											   true);
				
			ctx.DrawImage (new RectangleF (x, y, grp.Width, grp.Height), frames[current_frame]);
		}
Exemplo n.º 31
0
        internal static void Draw(CGContext ctx, object layout, double x, double y)
        {
            LayoutInfo li = (LayoutInfo)layout;
            if (li.Framesetter == null)
                return;

            CGPath path = new CGPath ();
            path.AddRect (new RectangleF (0, 0, li.Width ?? float.MaxValue, li.Height ?? float.MaxValue));
            CTFrame frame = li.Framesetter.GetFrame (new NSRange (0, li.Text.Length), path, null);

            CTLine ellipsis = null;
            bool ellipsize = li.Width.HasValue && li.TextTrimming == TextTrimming.WordElipsis;
            if (ellipsize)
                ellipsis = new CTLine (CreateAttributedString (li, "..."));

            float lineHeight = layoutManager.DefaultLineHeightForFont (li.Font);

            ctx.SaveState ();
            ctx.TextMatrix = CGAffineTransform.MakeScale (1f, -1f);
            ctx.TranslateCTM ((float)x, (float)y + li.Font.Ascender);
            foreach (var line in frame.GetLines ()) {
                ctx.TextPosition = PointF.Empty;
                if (ellipsize) // we need to create a new CTLine here because the framesetter already truncated the text for the line
                    new CTLine (CreateAttributedString (li, li.Text.Substring (line.StringRange.Location)))
                        .GetTruncatedLine (li.Width.Value, CTLineTruncation.End, ellipsis).Draw (ctx);
                else
                    line.Draw (ctx);
                ctx.TranslateCTM (0, lineHeight);
            }
            ctx.RestoreState ();
        }