Exemplo n.º 1
0
        public static void WriteSvgCssFile(IVirtualFiles vfs, string name,
                                           List <string> dataUris,
                                           Dictionary <string, string> adjacentCssRules = null,
                                           Dictionary <string, string> appendToCssFiles = null)
        {
            var sb = StringBuilderCache.Allocate();

            WriteDataUris(sb, dataUris);

            if (adjacentCssRules != null)
            {
                WriteAdjacentCss(sb, dataUris, adjacentCssRules);
            }

            if (appendToCssFiles != null)
            {
                if (appendToCssFiles.TryGetValue(name, out var suffix))
                {
                    sb.AppendLine(suffix);
                }
            }

            var css = StringBuilderCache.ReturnAndFree(sb);

            if (Svg.CssFillColor.TryGetValue(name, out var fillColor))
            {
                css = Svg.Fill(css, fillColor);
            }

            vfs.WriteFile($"/css/{name}.css", css);
        }
Exemplo n.º 2
0
 public static void WriteDataUris(StringBuilder sb, List <string> dataUris)
 {
     foreach (var name in dataUris)
     {
         AppendEntry(sb, name, Svg.GetDataUri(name));
     }
 }
Exemplo n.º 3
0
        public override async Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
        {
            var svg = Svg.GetImage(Id, Fill);

            if (svg == null)
            {
                httpRes.StatusCode        = 404;
                httpRes.StatusDescription = "SVG Image was not found";
            }
            else if (Format == "svg")
            {
                httpRes.ContentType = MimeTypes.ImageSvg;
                await httpRes.WriteAsync(svg);
            }
            else if (Format == "css")
            {
                httpRes.ContentType = "text/css";
                var css = $".svg-{Id} {{\n  {Svg.InBackgroundImageCss(Svg.GetImage(Id, Fill))}\n}}\n";
                await httpRes.WriteAsync(css);
            }
            else if (Format == "datauri")
            {
                var dataUri = Svg.GetDataUri(Id, Fill);
                httpRes.ContentType = MimeTypes.PlainText;
                await httpRes.WriteAsync(dataUri);
            }
            else
            {
                httpRes.StatusCode        = 400;
                httpRes.StatusDescription = "Unknown format, valid formats: svg, css, datauri";
            }
            httpRes.EndRequest();
        }