Пример #1
0
        static void Generate(string inputPath, string outputPath, string namespaceName = "Svg", string className = "Generated")
        {
            var svg = System.IO.File.ReadAllText(inputPath);

            SvgDocument.SkipGdiPlusCapabilityCheck = true;
            SvgDocument.PointsPerInch = 96;
            var svgDocument = SvgDocument.FromSvg <SvgDocument>(svg);

            if (svgDocument != null)
            {
                var picture = SKSvg.ToModel(svgDocument);
                if (picture != null && picture.Commands != null)
                {
                    var text = SkiaCodeGen.Generate(picture, namespaceName, className);
                    System.IO.File.WriteAllText(outputPath, text);
                }
            }
        }
Пример #2
0
        /// <inheritdoc/>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var s   = (string)value;
            var uri = s.StartsWith("/")
                ? new Uri(s, UriKind.Relative)
                : new Uri(s, UriKind.RelativeOrAbsolute);
            var svg = new SvgSource();

            if (uri.IsAbsoluteUri && uri.IsFile)
            {
#if USE_PICTURE
                var document = SKSvg.Open(uri.LocalPath);
                if (document != null)
                {
                    svg.Picture = SKSvg.ToModel(document);
                }
#else
                svg.Load(uri.LocalPath);
#endif
                return(svg);
            }
            else
            {
                var assets = AvaloniaLocator.Current.GetService <IAssetLoader>();
#if USE_PICTURE
                var document = SKSvg.Open(assets.Open(uri, context.GetContextBaseUri()));
                if (document != null)
                {
                    svg.Picture = SKSvg.ToModel(document);
                }
#else
                svg.Load(assets.Open(uri, context.GetContextBaseUri()));
#endif
            }
            return(svg);
        }
Пример #3
0
        private IList <IBaseShape> Convert(Svg.SvgDocument document, out double width, out double height)
        {
            var picture = SKSvg.ToModel(document);

            if (picture == null)
            {
                width  = double.NaN;
                height = double.NaN;
                return(null);
            }

            var shapes  = new List <IBaseShape>();
            var factory = _serviceProvider.GetService <IFactory>();

            ToShape(picture, shapes, factory);

            var group = factory.CreateGroupShape("svg");

            group.Shapes = group.Shapes.AddRange(shapes);

            width  = picture.CullRect.Width;
            height = picture.CullRect.Height;
            return(Enumerable.Repeat <IBaseShape>(group, 1).ToList());
        }
Пример #4
0
        private void Drop(object sender, DragEventArgs e)
        {
            if (e.Data.Contains(DataFormats.FileNames))
            {
                var fileName = e.Data.GetFileNames()?.FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    if (sender == _svgSourceDockPanel)
                    {
                        var svg = new SvgSource();
#if USE_PICTURE
                        var document = SKSvg.Open(fileName);
                        if (document != null)
                        {
                            var picture = SKSvg.ToModel(document);
                            if (picture != null)
                            {
                                svg.Picture            = picture;
                                _svgSourceImage.Source = new SvgImage()
                                {
                                    Source = svg
                                };
                            }
                        }
#else
                        var picture = svg.Load(fileName);
                        if (picture != null)
                        {
                            _svgSourceImage.Source = new SvgImage()
                            {
                                Source = svg
                            };
                        }
#endif
                    }

                    if (sender == _svgResourceDockPanel)
                    {
#if USE_PICTURE
                        var svg      = new SvgSource();
                        var document = SKSvg.Open(fileName);
                        if (document != null)
                        {
                            var picture = SKSvg.ToModel(document);
                            if (picture != null)
                            {
                                svg.Picture = picture;
                                _svgResourceImage.Source = new SvgImage()
                                {
                                    Source = svg
                                };
                            }
                        }
#else
                        var svg     = new SvgSource();
                        var picture = svg.Load(fileName);
                        if (picture != null)
                        {
                            _svgResourceImage.Source = new SvgImage()
                            {
                                Source = svg
                            };
                        }
#endif
                    }
                }
            }
        }