Exemplo n.º 1
0
        public void Execute()
        {
            // Load the SVG
            var svg = Svg.Load(options.SvgPath);

            // Save the SVG
            var outPath = Path.Join("OUTPUT", Path.GetFileName(options.SvgPath));

            FileHelpers.SaveSvg(svg, outPath);

            // Optionally, print the SVG to the console
            if (options.Echo)
            {
                Svg.Save(svg, Console.Out);

                Console.WriteLine();
            }

            // Create the HTML wrapper, unless requested otherwise.
            string htmlPath = null;

            if (!options.NoHtml)
            {
                htmlPath = Path.Join("OUTPUT", Path.GetFileNameWithoutExtension(outPath) + ".html");

                FileHelpers.WriteHtmlWrapper(htmlPath, options.SvgPath, outPath);
            }

            // Optionally, open the HTML wrapper (or SVG) in the default browser
            // TODO
        }
Exemplo n.º 2
0
        public static void SaveSvg(SvgSvgElement svg, string path)
        {
            // Make sure the output directory exists
            var outDir = Path.GetDirectoryName(path);

            if (!Directory.Exists(outDir))
            {
                Console.WriteLine("Creating output directory: {0}.", outDir);
                Directory.CreateDirectory(outDir);
            }

            // Write the SVG
            using (var writer = new StreamWriter(path))
            {
                Svg.Save(svg, writer);
            }

            // Tell the user where we wrote it
            Console.WriteLine("Wrote SVG to {0}.", path);
        }
Exemplo n.º 3
0
        public static bool Save(FileInfo path, DirectoryInfo output, string format, int quality, string background, float scale, float scaleX, float scaleY, bool debug, bool quiet, int i)
        {
            try
            {
                if (quiet == false)
                {
                    Log($"[{i}] File: {path}");
                }

                var    extension = path.Extension;
                string imagePath = path.FullName.Remove(path.FullName.Length - extension.Length) + "." + format.ToLower();
                if (!string.IsNullOrEmpty(output.FullName))
                {
                    imagePath = Path.Combine(output.FullName, Path.GetFileName(imagePath));
                }

                using (var svg = new Svg())
                {
                    if (svg.Load(path.FullName) != null)
                    {
                        if (debug == true && svg.Document != null)
                        {
                            string ymlPath = path.FullName.Remove(path.FullName.Length - extension.Length) + ".yml";
                            if (!string.IsNullOrEmpty(output.FullName))
                            {
                                ymlPath = Path.Combine(output.FullName, Path.GetFileName(ymlPath));
                            }
                            SvgDebug.Print(svg.Document, ymlPath);
                        }

                        if (Enum.TryParse <SKEncodedImageFormat>(format, true, out var skEncodedImageFormat))
                        {
                            if (SKColor.TryParse(background, out var skBackgroundColor))
                            {
                                if (scale != 1f)
                                {
                                    svg.Save(imagePath, skBackgroundColor, skEncodedImageFormat, quality, scale, scale);
                                }
                                else
                                {
                                    svg.Save(imagePath, skBackgroundColor, skEncodedImageFormat, quality, scaleX, scaleY);
                                }
                            }
                            else
                            {
                                throw new ArgumentException($"Invalid output image background.", nameof(background));
                            }
                        }
                        else
                        {
                            throw new ArgumentException($"Invalid output image format.", nameof(format));
                        }
                    }
                }

                if (quiet == false)
                {
                    Log($"[{i}] Success: {imagePath}");
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (quiet == false)
                {
                    Log($"[{i}] Error: {path}");
                    Error(ex);
                }
            }

            return(false);
        }