protected virtual SKPicture OnSnapshot ()
		{
			var recorder = new SKPictureRecorder ();
			var canvas = recorder.BeginRecording (Bounds);
			Draw (canvas, 0, 0);
			return recorder.EndRecording ();
		}
Example #2
0
        private SKPicture Load(XDocument xdoc)
        {
            var svg = xdoc.Root;
            var ns  = svg.Name.Namespace;

            // find the defs (gradients) - and follow all hrefs
            foreach (var d in svg.Descendants())
            {
                var id = d.Attribute("id")?.Value?.Trim();
                if (!string.IsNullOrEmpty(id))
                {
                    defs[id] = ReadDefinition(d);
                }
            }

            Version     = svg.Attribute("version")?.Value;
            Title       = svg.Element(ns + "title")?.Value;
            Description = svg.Element(ns + "desc")?.Value ?? svg.Element(ns + "description")?.Value;

            if (CanvasSize.IsEmpty)
            {
                // get the dimensions
                var widthA  = svg.Attribute("width");
                var heightA = svg.Attribute("height");
                var width   = ReadNumber(widthA);
                var height  = ReadNumber(heightA);
                var size    = new SKSize(width, height);

                var viewBox  = SKRect.Create(size);
                var viewBoxA = svg.Attribute("viewBox") ?? svg.Attribute("viewPort");
                if (viewBoxA != null)
                {
                    viewBox = ReadRectangle(viewBoxA.Value);
                }

                if (widthA != null && widthA.Value.Contains("%"))
                {
                    size.Width *= viewBox.Width;
                }
                if (heightA != null && heightA.Value.Contains("%"))
                {
                    size.Height *= viewBox.Height;
                }

                // set the property
                CanvasSize = size;
            }

            // create the picture from the elements
            using (var recorder = new SKPictureRecorder())
                using (var canvas = recorder.BeginRecording(SKRect.Create(CanvasSize)))
                {
                    LoadElements(svg.Elements(), canvas);

                    Picture = recorder.EndRecording();
                }

            return(Picture);
        }
Example #3
0
        private SKPicture Load(XDocument xdoc)
        {
            var svg = xdoc.Root;
            var ns  = svg.Name.Namespace;

            // find the defs (gradients) - and follow all hrefs
            foreach (var d in svg.Descendants())
            {
                var id = d.Attribute("id")?.Value?.Trim();
                if (!string.IsNullOrEmpty(id))
                {
                    defs[id] = ReadDefinition(d);
                }
            }

            Version     = svg.Attribute("version")?.Value;
            Title       = svg.Element(ns + "title")?.Value;
            Description = svg.Element(ns + "desc")?.Value ?? svg.Element(ns + "description")?.Value;

            // TODO: parse the "preserveAspectRatio" values properly
            var preserveAspectRatio = svg.Attribute("preserveAspectRatio")?.Value;

            // get the SVG dimensions
            var viewBoxA = svg.Attribute("viewBox") ?? svg.Attribute("viewPort");

            if (viewBoxA != null)
            {
                ViewBox = ReadRectangle(viewBoxA.Value);
            }
            else
            {
                var widthA  = svg.Attribute("width");
                var heightA = svg.Attribute("height");
                var width   = ReadNumber(widthA);
                var height  = ReadNumber(heightA);
                var size    = new SKSize(width, height);
                ViewBox = SKRect.Create(size);
            }

            if (CanvasSize.IsEmpty)
            {
                // get the user dimensions
                var widthA  = svg.Attribute("width");
                var heightA = svg.Attribute("height");
                var width   = ReadNumber(widthA);
                var height  = ReadNumber(heightA);
                var size    = new SKSize(width, height);

                if (widthA == null)
                {
                    size.Width = ViewBox.Width;
                }
                else if (widthA.Value.Contains("%"))
                {
                    size.Width *= ViewBox.Width;
                }
                if (heightA == null)
                {
                    size.Height = ViewBox.Height;
                }
                else if (heightA != null && heightA.Value.Contains("%"))
                {
                    size.Height *= ViewBox.Height;
                }

                // set the property
                CanvasSize = size;
            }

            // create the picture from the elements
            using (var recorder = new SKPictureRecorder())
                using (var canvas = recorder.BeginRecording(SKRect.Create(CanvasSize)))
                {
                    // scale the SVG dimensions to fit inside the user dimensions
                    if (ViewBox.Width != CanvasSize.Width || ViewBox.Height != CanvasSize.Height)
                    {
                        if (preserveAspectRatio == "none")
                        {
                            canvas.Scale(CanvasSize.Width / ViewBox.Width, CanvasSize.Height / ViewBox.Height);
                        }
                        else
                        {
                            // TODO: just center scale for now
                            var scale    = Math.Min(CanvasSize.Width / ViewBox.Width, CanvasSize.Height / ViewBox.Height);
                            var centered = SKRect.Create(CanvasSize).AspectFit(ViewBox.Size);
                            canvas.Translate(centered.Left, centered.Top);
                            canvas.Scale(scale, scale);
                        }
                    }

                    // translate the canvas by the viewBox origin
                    canvas.Translate(-ViewBox.Left, -ViewBox.Top);

                    LoadElements(svg.Elements(), canvas);

                    Picture = recorder.EndRecording();
                }

            return(Picture);
        }