private static RenderingResult ProcessGalleryItem(GalleryItem item, string outputDirPath, Maybe <string> regressionDirPath) => ( from svgElement in Svg.Renderer.RenderSvgElement(item.Diagram.Compile().Invoke()) .IfSuccessDo(svgElement => WriteOutToGallery(svgElement, item, outputDirPath)) from _regressionTest in regressionDirPath.To <Fallible <string, Unit> >( dir => CheckForRegressions( svgElement, Path.Combine(dir, $"{item.Title}.svg")), () => Fallible.Success <string, Unit>(Unit.Instance)) select _regressionTest ) .GetFailure() .Pipe(maybeFailure => new RenderingResult(item, maybeFailure));
/// <summary> /// Generates an SVG element containing the diagram /// </summary> /// <param name="diagram">the diagram to render</param> /// <param name="internalToSvgScaling">the "internal" to SVG scaling ratio.</param> /// <returns></returns> public static Fallible <string, XElement> RenderSvgElement( IDiagram diagram, int internalToSvgScaling = 1000) { return (from inputDiagram in Fallible.Success <string, IDiagram>(diagram) let boundingBox = EnsureNonZeroDimensions(inputDiagram.Bounds) let converter = new CoordinatesConverter(boundingBox, internalToSvgScaling) let drawState = new SvgDrawState(converter) let renderingResult = RenderSvg(diagram).Run(drawState) let mainElement = renderingResult.Value let finalDrawState = renderingResult.State let svgElements = finalDrawState.BuildDeclarations() .Concat(new[] { mainElement.Build() }) select new XElement("svg", new XAttribute("stroke", "black"), // provide a reasonable default stroke new XAttribute("stroke-width", "0"), // no stroke width. Those that need it will set it explicitly. new XAttribute("viewbox", $"0 0 {converter.SvgWidth} {converter.SvgHeight}"), svgElements)); }