Ejemplo n.º 1
0
        /// <inheritdoc/>
        public IList <IBaseShape> FromString(string text, out double width, out double height)
        {
            using var stream = ToStream(text);
            var document = SKSvg.Open(stream);

            if (document == null)
            {
                width  = double.NaN;
                height = double.NaN;
                return(null);
            }
            return(Convert(document, out width, out height));
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public IList <IBaseShape> Convert(string path, out double width, out double height)
        {
            var document = SKSvg.Open(path);

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

            return(Convert(document, out width, out height));
        }
Ejemplo n.º 3
0
        private void LoadSvg(Item item, Action <string> statusOpen, Action <string> statusToPicture)
        {
            var currentDirectory = Directory.GetCurrentDirectory();

            try
            {
                if (!File.Exists(item.SvgPath))
                {
                    return;
                }

                Directory.SetCurrentDirectory(Path.GetDirectoryName(item.SvgPath));

                var stopwatchOpen = Stopwatch.StartNew();
                item.Svg = SKSvg.Open(item.SvgPath);
                stopwatchOpen.Stop();
                statusOpen?.Invoke($"{Math.Round(stopwatchOpen.Elapsed.TotalMilliseconds, 3)}ms");
                Debug.WriteLine($"Open: {Math.Round(stopwatchOpen.Elapsed.TotalMilliseconds, 3)}ms");

                if (item.Svg != null)
                {
                    var stopwatchToPicture = Stopwatch.StartNew();
                    item.Picture  = SKSvg.ToPicture(item.Svg, out var drawable);
                    item.Drawable = drawable;
                    stopwatchToPicture.Stop();
                    statusToPicture?.Invoke($"{Math.Round(stopwatchToPicture.Elapsed.TotalMilliseconds, 3)}ms");
                    Debug.WriteLine($"ToPicture: {Math.Round(stopwatchToPicture.Elapsed.TotalMilliseconds, 3)}ms");
                }
                else
                {
                    statusToPicture?.Invoke($"");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Failed to load svg file: {item.SvgPath}");
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }

            Directory.SetCurrentDirectory(currentDirectory);
        }
Ejemplo n.º 4
0
        /// <inheritdoc/>
        public IList <IBaseShape> Convert(string path)
        {
            var document = SKSvg.Open(path);

            if (document == null)
            {
                return(null);
            }

            using var drawable = SKSvg.ToDrawable(document);
            if (drawable == null)
            {
                return(null);
            }

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

            ToShape(drawable, shapes, factory);

            return(shapes);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public void ExportItem(string svgPath, string outputPath, SKColor background, float scaleX, float scaleY)
        {
            if (!File.Exists(svgPath))
            {
                return;
            }

            var currentDirectory = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(Path.GetDirectoryName(svgPath));

            var extension = Path.GetExtension(outputPath);

            if (string.Compare(extension, ".pdf", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    picture.ToPdf(outputPath, background, scaleX, scaleY);
                }
            }
            else if (string.Compare(extension, ".xps", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    picture.ToXps(outputPath, background, scaleX, scaleY);
                }
            }
            else if (string.Compare(extension, ".svg", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    picture.ToSvg(outputPath, background, scaleX, scaleY);
                }
            }
            else if (string.Compare(extension, ".jpeg", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    using var stream = File.OpenWrite(outputPath);
#if USE_COLORSPACE
                    picture.ToImage(stream, background, SKEncodedImageFormat.Jpeg, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType, SvgPaintingExtensions.Srgb);
#else
                    picture.ToImage(stream, background, SKEncodedImageFormat.Jpeg, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType);
#endif
                }
            }
            else if (string.Compare(extension, ".jpg", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    using var stream = File.OpenWrite(outputPath);
#if USE_COLORSPACE
                    picture.ToImage(stream, background, SKEncodedImageFormat.Jpeg, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType, SvgPaintingExtensions.Srgb);
#else
                    picture.ToImage(stream, background, SKEncodedImageFormat.Jpeg, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType);
#endif
                }
            }
            else if (string.Compare(extension, ".png", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    using var stream = File.OpenWrite(outputPath);
#if USE_COLORSPACE
                    picture.ToImage(stream, background, SKEncodedImageFormat.Png, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType, SvgPaintingExtensions.Srgb);
#else
                    picture.ToImage(stream, background, SKEncodedImageFormat.Png, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType);
#endif
                }
            }
            else if (string.Compare(extension, ".webp", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    using var stream = File.OpenWrite(outputPath);
#if USE_COLORSPACE
                    picture.ToImage(stream, background, SKEncodedImageFormat.Webp, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType, SvgPaintingExtensions.Srgb);
#else
                    picture.ToImage(stream, background, SKEncodedImageFormat.Webp, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType);
#endif
                }
            }

            Directory.SetCurrentDirectory(currentDirectory);
        }
Ejemplo n.º 7
0
        public SkiaSharpSvgTools(SharedImageInfo info, ILogger logger)
        {
            Info   = info;
            Logger = logger;

            var sw = new Stopwatch();

            sw.Start();

            var svgDoc = SKSvg.Open(Info.Filename);

            void ChangeFill(SvgElement element)
            {
                if (element is SvgPath ePath && ePath.Fill is SvgColourServer eFill)
                {
                    Logger?.Log($"Found Fill: {eFill.Colour.ToString()}");

                    if (!eFill.Colour.IsEmpty)
                    {
                        ePath.Fill = new SvgColourServer(Info.TintColor.Value);

                        Logger?.Log($"Changing Fill: {Info.TintColor.ToString()}");
                    }
                }

                if (element.Children.Count > 0)
                {
                    foreach (var item in element.Children)
                    {
                        ChangeFill(item);
                    }
                }
            }

            if (Info.TintColor.HasValue)
            {
                Logger?.Log($"Changing Tint: {Info.TintColor.Value.ToString()}");

                foreach (var elem in svgDoc.Children)
                {
                    ChangeFill(elem);
                }
            }

            Svg.SvgDocument.SkipGdiPlusCapabilityCheck = true;


            //svgDoc.Write(Info.Filename + ".mod.svg");
            sw.Stop();
            Logger?.Log($"Open SVG took {sw.ElapsedMilliseconds}ms");
            sw.Reset();
            sw.Start();


            svg = new SKSvg();
            sw.Stop();
            Logger?.Log($"new SKSvg() took {sw.ElapsedMilliseconds}ms");
            sw.Reset();
            sw.Start();

            svg.FromSvgDocument(svgDoc);
            sw.Stop();
            Logger?.Log($"svg.FromSvgDocument took {sw.ElapsedMilliseconds}ms");
        }
Ejemplo n.º 8
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
                    }
                }
            }
        }