DrawRect() public method

public DrawRect ( SKRect rect, SKPaint paint ) : void
rect SKRect
paint SKPaint
return void
Beispiel #1
1
		public static void MeasureTextSample (SKCanvas canvas, int width, int height)
		{
			canvas.DrawColor (SKColors.White);

			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x42, 0x81, 0xA4);
				paint.TextEncoding = SKTextEncoding.Utf32;

				canvas.DrawText ("Skia (UTF-32)", 0, 64.0f, paint);

				var bounds = new SKRect();
				paint.MeasureText ("Skia (UTF-32)", ref bounds);
				bounds.Top += 64.0f;
				bounds.Bottom += 64.0f;

				paint.IsStroke = true;
				paint.Color = SKColors.Red;

				canvas.DrawRect (bounds, paint);
			}

			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x9C, 0xAF, 0xB7);
				paint.TextEncoding = SKTextEncoding.Utf16;

				canvas.DrawText ("Skia (UTF-16)", 0, 144.0f, paint);

				var bounds = new SKRect();
				paint.MeasureText ("Skia (UTF-16)", ref bounds);
				bounds.Top += 144.0f;
				bounds.Bottom += 144.0f;

				paint.IsStroke = true;
				paint.Color = SKColors.Red;

				canvas.DrawRect (bounds, paint);
			}

			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0xE6, 0xB8, 0x9C);
				paint.TextEncoding = SKTextEncoding.Utf8;

				canvas.DrawText ("Skia (UTF-8)", 0, 224.0f, paint);

				var bounds = new SKRect();
				paint.MeasureText ("Skia (UTF-8)", ref bounds);
				bounds.Top += 224.0f;
				bounds.Bottom += 224.0f;

				paint.IsStroke = true;
				paint.Color = SKColors.Red;

				canvas.DrawRect (bounds, paint);
			}
		}
Beispiel #2
0
        private void ReadElement(XElement e, SKCanvas canvas, SKPaint stroke, SKPaint fill)
        {
            ReadPaints(e, ref stroke, ref fill);

            // transform matrix
            var transform = ReadTransform(e.Attribute("transform")?.Value ?? string.Empty);

            canvas.Save();
            canvas.Concat(ref transform);

            // SVG elements
            var elementName = e.Name.LocalName;

            switch (elementName)
            {
            case "text":
                if (stroke != null || fill != null)
                {
                    ReadText(e, canvas, stroke?.Clone(), fill?.Clone());
                }
                break;

            case "rect":
                if (stroke != null || fill != null)
                {
                    var x      = ReadNumber(e.Attribute("x"));
                    var y      = ReadNumber(e.Attribute("y"));
                    var width  = ReadNumber(e.Attribute("width"));
                    var height = ReadNumber(e.Attribute("height"));
                    var rx     = ReadNumber(e.Attribute("rx"));
                    var ry     = ReadNumber(e.Attribute("ry"));
                    var rect   = SKRect.Create(x, y, width, height);
                    if (rx > 0 || ry > 0)
                    {
                        if (fill != null)
                        {
                            canvas.DrawRoundRect(rect, rx, ry, fill);
                        }
                        if (stroke != null)
                        {
                            canvas.DrawRoundRect(rect, rx, ry, stroke);
                        }
                    }
                    else
                    {
                        if (fill != null)
                        {
                            canvas.DrawRect(rect, fill);
                        }
                        if (stroke != null)
                        {
                            canvas.DrawRect(rect, stroke);
                        }
                    }
                }
                break;

            case "ellipse":
                if (stroke != null || fill != null)
                {
                    var cx = ReadNumber(e.Attribute("cx"));
                    var cy = ReadNumber(e.Attribute("cy"));
                    var rx = ReadNumber(e.Attribute("rx"));
                    var ry = ReadNumber(e.Attribute("ry"));
                    if (fill != null)
                    {
                        canvas.DrawOval(cx, cy, rx, ry, fill);
                    }
                    if (stroke != null)
                    {
                        canvas.DrawOval(cx, cy, rx, ry, stroke);
                    }
                }
                break;

            case "circle":
                if (stroke != null || fill != null)
                {
                    var cx = ReadNumber(e.Attribute("cx"));
                    var cy = ReadNumber(e.Attribute("cy"));
                    var rr = ReadNumber(e.Attribute("r"));
                    if (fill != null)
                    {
                        canvas.DrawCircle(cx, cy, rr, fill);
                    }
                    if (stroke != null)
                    {
                        canvas.DrawCircle(cx, cy, rr, stroke);
                    }
                }
                break;

            case "path":
                if (stroke != null || fill != null)
                {
                    var d = e.Attribute("d")?.Value;
                    if (!string.IsNullOrWhiteSpace(d))
                    {
                        var path = SKPath.ParseSvgPathData(d);
                        if (fill != null)
                        {
                            canvas.DrawPath(path, fill);
                        }
                        if (stroke != null)
                        {
                            canvas.DrawPath(path, stroke);
                        }
                    }
                }
                break;

            case "polygon":
            case "polyline":
                if (stroke != null || fill != null)
                {
                    var close = elementName == "polygon";
                    var p     = e.Attribute("points")?.Value;
                    if (!string.IsNullOrWhiteSpace(p))
                    {
                        var path = ReadPolyPath(p, close);
                        if (fill != null)
                        {
                            canvas.DrawPath(path, fill);
                        }
                        if (stroke != null)
                        {
                            canvas.DrawPath(path, stroke);
                        }
                    }
                }
                break;

            case "g":
                if (e.HasElements)
                {
                    foreach (var gElement in e.Elements())
                    {
                        ReadElement(gElement, canvas, stroke?.Clone(), fill?.Clone());
                    }
                }
                break;

            case "use":
                if (e.HasAttributes)
                {
                    var href = ReadHref(e);
                    if (href != null)
                    {
                        // TODO: copy/process other attributes

                        var x            = ReadNumber(e.Attribute("x"));
                        var y            = ReadNumber(e.Attribute("y"));
                        var useTransform = SKMatrix.MakeTranslation(x, y);

                        canvas.Save();
                        canvas.Concat(ref useTransform);

                        ReadElement(href, canvas, stroke?.Clone(), fill?.Clone());

                        canvas.Restore();
                    }
                }
                break;

            case "line":
                if (stroke != null)
                {
                    var x1 = ReadNumber(e.Attribute("x1"));
                    var x2 = ReadNumber(e.Attribute("x2"));
                    var y1 = ReadNumber(e.Attribute("y1"));
                    var y2 = ReadNumber(e.Attribute("y2"));
                    canvas.DrawLine(x1, y1, x2, y2, stroke);
                }
                break;

            case "switch":
                if (e.HasElements)
                {
                    foreach (var ee in e.Elements())
                    {
                        var requiredFeatures   = ee.Attribute("requiredFeatures");
                        var requiredExtensions = ee.Attribute("requiredExtensions");
                        var systemLanguage     = ee.Attribute("systemLanguage");

                        // TODO: evaluate requiredFeatures, requiredExtensions and systemLanguage
                        var isVisible =
                            requiredFeatures == null &&
                            requiredExtensions == null &&
                            systemLanguage == null;

                        if (isVisible)
                        {
                            ReadElement(ee, canvas, stroke?.Clone(), fill?.Clone());
                        }
                    }
                }
                break;

            case "defs":
            case "title":
            case "desc":
            case "description":
                // already read earlier
                break;

            default:
                LogOrThrow($"SVG element '{elementName}' is not supported");
                break;
            }

            // restore matrix
            canvas.Restore();
        }
Beispiel #3
0
        public static void MeasureTextSample(SKCanvas canvas, int width, int height)
        {
            canvas.DrawColor(SKColors.White);

            using (var paint = new SKPaint()) {
                paint.TextSize     = 64.0f;
                paint.IsAntialias  = true;
                paint.Color        = new SKColor(0x42, 0x81, 0xA4);
                paint.TextEncoding = SKTextEncoding.Utf32;

                canvas.DrawText("Skia (UTF-32)", 0, 64.0f, paint);

                var bounds = new SKRect();
                paint.MeasureText("Skia (UTF-32)", ref bounds);
                bounds.Top    += 64.0f;
                bounds.Bottom += 64.0f;

                paint.IsStroke = true;
                paint.Color    = SKColors.Red;

                canvas.DrawRect(bounds, paint);
            }

            using (var paint = new SKPaint()) {
                paint.TextSize     = 64.0f;
                paint.IsAntialias  = true;
                paint.Color        = new SKColor(0x9C, 0xAF, 0xB7);
                paint.TextEncoding = SKTextEncoding.Utf16;

                canvas.DrawText("Skia (UTF-16)", 0, 144.0f, paint);

                var bounds = new SKRect();
                paint.MeasureText("Skia (UTF-16)", ref bounds);
                bounds.Top    += 144.0f;
                bounds.Bottom += 144.0f;

                paint.IsStroke = true;
                paint.Color    = SKColors.Red;

                canvas.DrawRect(bounds, paint);
            }

            using (var paint = new SKPaint()) {
                paint.TextSize     = 64.0f;
                paint.IsAntialias  = true;
                paint.Color        = new SKColor(0xE6, 0xB8, 0x9C);
                paint.TextEncoding = SKTextEncoding.Utf8;

                canvas.DrawText("Skia (UTF-8)", 0, 224.0f, paint);

                var bounds = new SKRect();
                paint.MeasureText("Skia (UTF-8)", ref bounds);
                bounds.Top    += 224.0f;
                bounds.Bottom += 224.0f;

                paint.IsStroke = true;
                paint.Color    = SKColors.Red;

                canvas.DrawRect(bounds, paint);
            }
        }
Beispiel #4
0
        public static void Xfermode(SKCanvas canvas, int width, int height)
        {
            var modes = Enum.GetValues(typeof(SKXferMode)).Cast <SKXferMode> ().ToArray();

            var cols = width < height ? 3 : 5;
            var rows = (modes.Length - 1) / cols + 1;

            var w         = (float)width / cols;
            var h         = (float)height / rows;
            var rect      = SKRect.Create(w, h);
            var srcPoints = new[] {
                new SKPoint(0.0f, 0.0f),
                new SKPoint(w, 0.0f)
            };
            var srcColors = new [] {
                SKColors.Magenta.WithAlpha(0),
                SKColors.Magenta
            };
            var dstPoints = new [] {
                new SKPoint(0.0f, 0.0f),
                new SKPoint(0.0f, h)
            };
            var dstColors = new [] {
                SKColors.Cyan.WithAlpha(0),
                SKColors.Cyan
            };

            using (var text = new SKPaint())
                using (var stroke = new SKPaint())
                    using (var src = new SKPaint())
                        using (var dst = new SKPaint())
                            using (var srcShader = SKShader.CreateLinearGradient(srcPoints [0], srcPoints [1], srcColors, null, SKShaderTileMode.Clamp))
                                using (var dstShader = SKShader.CreateLinearGradient(dstPoints [0], dstPoints [1], dstColors, null, SKShaderTileMode.Clamp)) {
                                    text.TextSize    = 12.0f;
                                    text.IsAntialias = true;
                                    text.TextAlign   = SKTextAlign.Center;
                                    stroke.IsStroke  = true;
                                    src.Shader       = srcShader;
                                    dst.Shader       = dstShader;

                                    canvas.Clear(SKColors.White);

                                    for (var i = 0; i < modes.Length; ++i)
                                    {
                                        using (new SKAutoCanvasRestore(canvas, true)) {
                                            canvas.Translate(w * (i / rows), h * (i % rows));

                                            canvas.ClipRect(rect);
                                            canvas.DrawColor(SKColors.LightGray);

                                            canvas.SaveLayer(null);
                                            canvas.Clear(SKColors.Transparent);
                                            canvas.DrawPaint(dst);

                                            src.XferMode = modes [i];
                                            canvas.DrawPaint(src);
                                            canvas.DrawRect(rect, stroke);

                                            var desc = modes [i].ToString();
                                            canvas.DrawText(desc, w / 2f, h / 2f, text);
                                        }
                                    }
                                }
        }
 public void DrawRect(float x, float y, float width, float height, PaintBrush paint)
 => _canvas.DrawRect(x, y, width, height, GetSKPaint(paint));
Beispiel #6
0
 private void DrawBackgroundInternal(SKCanvas canvas, ArgbColor color, Rect2 rect)
 {
     using (SKPaint brush = ToSKPaintBrush(color))
     {
         SKRect srect = SKRect.Create(
             _scaleToPage(rect.X),
             _scaleToPage(rect.Y),
             _scaleToPage(rect.Width),
             _scaleToPage(rect.Height));
         canvas.DrawRect(srect, brush);
     }
 }
Beispiel #7
0
        private void DrawRectangleInternal(SKCanvas canvas, SKPaint brush, SKPaint pen, bool isStroked, bool isFilled, ref SKRect rect)
        {
            if (isFilled)
            {
                canvas.DrawRect(rect, brush);
            }

            if (isStroked)
            {
                canvas.DrawRect(rect, pen);
            }
        }
		public static void Xfermode (SKCanvas canvas, int width, int height)
		{
			var modes = Enum.GetValues (typeof(SKXferMode)).Cast<SKXferMode> ().ToArray ();

			var cols = width < height ? 3 : 5;
			var rows = (modes.Length - 1) / cols + 1;

			var w = (float)width / cols;
			var h = (float)height / rows;
			var rect = SKRect.Create (w, h);
			var srcPoints = new[] {
				new SKPoint (0.0f, 0.0f),
				new SKPoint (w, 0.0f)
			};
			var srcColors = new [] {
				SKColors.Magenta.WithAlpha (0),
				SKColors.Magenta
			};
			var dstPoints = new [] {
				new SKPoint (0.0f, 0.0f),
				new SKPoint (0.0f, h)
			};
			var dstColors = new [] {
				SKColors.Cyan.WithAlpha (0),
				SKColors.Cyan
			};

			using (var text = new SKPaint ())
			using (var stroke = new SKPaint ())
			using (var src = new SKPaint ())
			using (var dst = new SKPaint ())
			using (var srcShader = SKShader.CreateLinearGradient (srcPoints [0], srcPoints [1], srcColors, null, SKShaderTileMode.Clamp))
			using (var dstShader = SKShader.CreateLinearGradient (dstPoints [0], dstPoints [1], dstColors, null, SKShaderTileMode.Clamp)) {
				text.TextSize = 12.0f;
				text.IsAntialias = true;
				text.TextAlign = SKTextAlign.Center;
				stroke.IsStroke = true;
				src.Shader = srcShader;
				dst.Shader = dstShader;

				canvas.Clear (SKColors.White);

				for (var i = 0; i < modes.Length; ++i) {
					using (new SKAutoCanvasRestore (canvas, true)) {
						canvas.Translate (w * (i / rows), h * (i % rows));

						canvas.ClipRect (rect);
						canvas.DrawColor (SKColors.LightGray);

						canvas.SaveLayer (null);
						canvas.Clear (SKColors.Transparent);
						canvas.DrawPaint (dst);

						src.XferMode = modes [i];
						canvas.DrawPaint (src);
						canvas.DrawRect (rect, stroke);

						var desc = modes [i].ToString ();
						canvas.DrawText (desc, w / 2f, h / 2f, text);
					}
				}
			}
		}
		public static void ManageDrawMatrix (SKCanvas canvas, int width, int height)
		{
			var size = ((float)height > width ? width : height) * 0.5f;
			var center = new SKPoint ((width - size) / 2f, (height - size) / 2f);

			// draw these at specific locations
			var leftRect = SKRect.Create (center.X - size / 2f, center.Y, size, size);
			var rightRect = SKRect.Create (center.X + size / 2f, center.Y, size, size);

			// draw this at the current location / transformation
			var rotatedRect = SKRect.Create (0f, 0f, size, size);

			using (var paint = new SKPaint ()) {
				paint.IsAntialias = true;
				canvas.Clear (XamPurple);

				// draw
				paint.Color = XamDkBlue;
				canvas.DrawRect (leftRect, paint);

				// save
				canvas.Save();

				// transform
				canvas.Translate (width / 2f, center.Y);
				canvas.RotateDegrees (45);

				// draw
				paint.Color = XamGreen;
				canvas.DrawRect (rotatedRect, paint);

				// undo transform / restore
				canvas.Restore();

				// draw
				paint.Color = XamLtBlue;
				canvas.DrawRect (rightRect, paint);
			}
		}
Beispiel #10
0
 public void DrawRect(float x, float y, float width, float height, PaintBrush paint)
 => _canvas.DrawRect(x, y, width, height, paint.ToSkia());
Beispiel #11
0
 private static void DrawRect(SKCanvas canvas, SKRect rect, SKPaint fillColor, SKPaint lineColor)
 {
     if ((fillColor != null) && fillColor.Color.Alpha != 0) canvas.DrawRect(rect, fillColor);
     if ((lineColor != null) && lineColor.Color.Alpha != 0) canvas.DrawRect(rect, lineColor);
 }