/// <summary> /// Generates the drawing brush source code for a given geometry. /// </summary> private static void Generate(GraphicVisual visual, StringBuilder result, int level) { switch (visual) { case GraphicGroup group: { var tag = "DrawingGroup"; var indentTag = SourceGeneratorHelper.GetTagIndent(level); result.Append($"{indentTag}<{tag}"); bool tagIndent = false; if (!DoubleUtilities.IsEqual(group.Opacity, 1.0)) { tagIndent = true; string opac = string.Format(CultureInfo.InvariantCulture, " Opacity=\"{0}\"", DoubleUtilities.FormatString(group.Opacity)); result.Append(opac); } if (group.Clip != null) { if (tagIndent) { var indentProperty = SourceGeneratorHelper.GetPropertyIndent(level, tag); result.AppendLine(""); result.Append(indentProperty); } else { result.Append(" "); } result.Append(string.Format("ClipGeometry=\"")); var stream = StreamSourceGenerator.GenerateStreamGeometry(group.Clip); result.Append(stream); result.Append("\""); } result.AppendLine(">"); foreach (var childVisual in group.Childreen) { Generate(childVisual, result, level + 1); } result.AppendLine($"{indentTag}</{tag}>"); break; } case GraphicPath graphicPath: { GeneratePath(graphicPath, result, level); break; } } }
/// <summary> /// Generate the XAML source code (a <Path/> for a single graphic path /// </summary> public static string GenerateGeometry(GraphicPath graphicPath) { var tag = "Geometry"; var indentTag = SourceGeneratorHelper.GetTagIndent(0); var indentProperty = SourceGeneratorHelper.GetPropertyIndent(0, tag); StringBuilder result = new StringBuilder(); result.Append($"<{tag} x:Key=\"xyzIcon\">"); result.Append(GenerateStreamGeometry(graphicPath.Geometry, false)); result.Append($"\"</{tag}>"); return(result.ToString()); }
/// <summary> /// Generate path /// </summary> private static void GeneratePath(GraphicPath graphicPath, StringBuilder result, int level) { var tag = "GeometryDrawing"; var indentTag = SourceGeneratorHelper.GetTagIndent(level); var indentProperty = SourceGeneratorHelper.GetPropertyIndent(level, tag); result.Append($"{indentTag}<{tag} "); bool fillColorInExtendedProperties = false; if (graphicPath.FillBrush != null) { if (graphicPath.FillBrush is GraphicSolidColorBrush solidFillColor) { Color color = solidFillColor.Color; var brush = string.Format("Brush=\"{0}\"", SourceGeneratorHelper.FormatColorParamter(color)); result.AppendLine(brush); result.Append(indentProperty); } else { fillColorInExtendedProperties = true; } } result.Append(string.Format("Geometry=\"")); var stream = StreamSourceGenerator.GenerateStreamGeometry(graphicPath.Geometry); result.Append(stream); result.Append("\""); if (fillColorInExtendedProperties || graphicPath.StrokeBrush != null) { result.AppendLine(">"); if (fillColorInExtendedProperties) { SourceGeneratorHelper.GenerateBrush(result, graphicPath.FillBrush, "GeometryDrawing.Brush", level + 1); } GeneratePen(result, graphicPath, level + 1); result.Append(indentTag); result.AppendLine("</GeometryDrawing>"); } else { result.AppendLine("/>"); } }
/// <summary> /// Generate the pen for a GeometryDrawing /// </summary> private static void GeneratePen(StringBuilder result, GraphicPath graphicPath, int level) { if (graphicPath.StrokeBrush != null) { var indent1 = SourceGeneratorHelper.GetTagIndent(level); var indent2 = SourceGeneratorHelper.GetTagIndent(level + 1); bool strokeColorInExtendedProperties = false; result.Append(indent1); result.AppendLine("<GeometryDrawing.Pen>"); var tag = "Pen"; var indentPenProperty = SourceGeneratorHelper.GetPropertyIndent(level + 1, tag); result.Append(indent2); result.Append(string.Format(CultureInfo.InvariantCulture, "<{0} Thickness=\"{1}\"", tag, DoubleUtilities.FormatString(graphicPath.StrokeThickness))); if (graphicPath.StrokeLineCap != GraphicLineCap.Flat) { result.AppendLine(""); result.Append(indentPenProperty); result.AppendLine(string.Format(CultureInfo.InvariantCulture, "StartLineCap=\"{0}\" ", Converter.ConvertToWPF(graphicPath.StrokeLineCap).ToString())); result.Append(indentPenProperty); result.Append(string.Format(CultureInfo.InvariantCulture, "EndLineCap=\"{0}\" ", Converter.ConvertToWPF(graphicPath.StrokeLineCap).ToString())); } if (graphicPath.StrokeDashes != null) { if (graphicPath.StrokeLineCap != GraphicLineCap.Square) { result.AppendLine(""); result.Append(indentPenProperty); result.Append(string.Format(CultureInfo.InvariantCulture, "DashCap=\"{0}\" ", Converter.ConvertToWPF(graphicPath.StrokeLineCap).ToString())); } } if (graphicPath.StrokeLineJoin != GraphicLineJoin.Miter) { result.AppendLine(""); result.Append(indentPenProperty); result.Append(string.Format(CultureInfo.InvariantCulture, "LineJoin=\"{0}\" ", Converter.ConvertToWpf(graphicPath.StrokeLineJoin).ToString())); } else if (!DoubleUtilities.IsEqual(graphicPath.StrokeMiterLimit, 10)) { result.AppendLine(""); result.Append(indentPenProperty); result.Append(string.Format(CultureInfo.InvariantCulture, "MiterLimit=\"{0}\"", DoubleUtilities.FormatString(graphicPath.StrokeMiterLimit))); } if (graphicPath.StrokeBrush is GraphicSolidColorBrush strokeColor) { result.AppendLine(""); Color color = strokeColor.Color; result.Append(indentPenProperty); result.Append(string.Format("Brush=\"{0}\"", SourceGeneratorHelper.FormatColorParamter(color))); } else { strokeColorInExtendedProperties = true; } if (strokeColorInExtendedProperties || graphicPath.StrokeDashes != null) { result.Append(">"); result.AppendLine(""); if (graphicPath.StrokeDashes != null) { var indent3 = SourceGeneratorHelper.GetTagIndent(level + 2); var indent4 = SourceGeneratorHelper.GetTagIndent(level + 3); var tagDashStyle = "DashStyle"; result.Append(indent3); result.AppendLine($"<Pen.{tagDashStyle}>"); result.Append(indent4); result.Append($"<{tagDashStyle} Dashes=\""); for (int i = 0; i < graphicPath.StrokeDashes.Count; i++) { if (i != 0) { result.Append(" "); } result.Append(DoubleUtilities.FormatString(graphicPath.StrokeDashes[i])); } result.Append("\""); if (!DoubleUtilities.IsZero(graphicPath.StrokeDashOffset)) { var indentDashStyleProperty = SourceGeneratorHelper.GetPropertyIndent(level + 3, tagDashStyle); result.AppendLine(""); result.Append(indentDashStyleProperty); result.Append(string.Format(CultureInfo.InvariantCulture, "Offset=\"{0}\"", DoubleUtilities.FormatString(graphicPath.StrokeDashOffset))); } result.AppendLine("/>"); result.Append(indent3); result.AppendLine($"</Pen.{tagDashStyle}>"); } if (strokeColorInExtendedProperties) { SourceGeneratorHelper.GenerateBrush(result, graphicPath.StrokeBrush, "Pen.Brush", level + 3); } result.AppendLine($"{indent2}</{tag}>"); } else { result.AppendLine(" />"); } result.Append(indent1); result.AppendLine("</GeometryDrawing.Pen>"); } }
/// <summary> /// Generate the XAML source code (a <Path/> for a single graphic path /// </summary> public static string GeneratePath(GraphicPath graphicPath, bool stretch, int level) { var tag = "Path"; var indentTag = SourceGeneratorHelper.GetTagIndent(level); var indentProperty = SourceGeneratorHelper.GetPropertyIndent(level, tag); StringBuilder result = new StringBuilder(); string stretchParam = stretch ? "Uniform" : "None"; result.AppendLine($"{indentTag}<{tag} Stretch=\"{stretchParam}\""); bool fillColorInExtendedProperties = false; bool strokeColorInExtendedProperties = false; if (graphicPath.FillBrush != null) { if (graphicPath.FillBrush is GraphicSolidColorBrush solidFillColor) { Color color = solidFillColor.Color; result.Append(indentProperty); result.AppendLine(string.Format("Fill=\"{0}\"", SourceGeneratorHelper.FormatColorParamter(color))); } else { fillColorInExtendedProperties = true; } } if (graphicPath.StrokeBrush != null) { if (graphicPath.StrokeBrush is GraphicSolidColorBrush solidStrokeColor) { Color color = solidStrokeColor.Color; result.Append(indentProperty); result.AppendLine(string.Format("Stroke=\"{0}\" ", SourceGeneratorHelper.FormatColorParamter(color))); } else { strokeColorInExtendedProperties = true; } result.Append(indentProperty); result.AppendLine(string.Format(CultureInfo.InvariantCulture, "StrokeThickness=\"{0}\" ", DoubleUtilities.FormatString(graphicPath.StrokeThickness))); if (graphicPath.StrokeLineCap != GraphicLineCap.Flat) { result.Append(indentProperty); result.AppendLine(string.Format(CultureInfo.InvariantCulture, "StrokeStartLineCap=\"{0}\" ", Converter.ConvertToWPF(graphicPath.StrokeLineCap).ToString())); result.Append(indentProperty); result.AppendLine(string.Format(CultureInfo.InvariantCulture, "StrokeEndLineCap=\"{0}\" ", Converter.ConvertToWPF(graphicPath.StrokeLineCap).ToString())); } if (graphicPath.StrokeDashes != null) { if (graphicPath.StrokeLineCap != GraphicLineCap.Flat) { result.Append(indentProperty); result.AppendLine(string.Format(CultureInfo.InvariantCulture, "StrokeDashCap=\"{0}\" ", Converter.ConvertToWPF(graphicPath.StrokeLineCap).ToString())); } result.Append(indentProperty); result.Append("StrokeDashArray=\""); for (int i = 0; i < graphicPath.StrokeDashes.Count; i++) { if (i != 0) { result.Append(" "); } result.Append(DoubleUtilities.FormatString(graphicPath.StrokeDashes[i])); } result.AppendLine("\""); if (!DoubleUtilities.IsZero(graphicPath.StrokeDashOffset)) { result.Append(indentProperty); result.AppendLine(string.Format(CultureInfo.InvariantCulture, "StrokeDashOffset=\"{0}\"", DoubleUtilities.FormatString(graphicPath.StrokeDashOffset))); } } if (graphicPath.StrokeLineJoin != GraphicLineJoin.Miter) { result.Append(indentProperty); result.AppendLine(string.Format(CultureInfo.InvariantCulture, "StrokeLineJoin=\"{0}\" ", Converter.ConvertToWpf(graphicPath.StrokeLineJoin).ToString())); } else if (!DoubleUtilities.IsEqual(graphicPath.StrokeMiterLimit, 10)) { result.Append(indentProperty); result.AppendLine(string.Format(CultureInfo.InvariantCulture, "MiterLimit=\"{0}\"", DoubleUtilities.FormatString(graphicPath.StrokeMiterLimit))); } } result.Append(indentProperty); result.Append("Data=\""); result.Append(GenerateStreamGeometry(graphicPath.Geometry)); result.Append("\""); if (fillColorInExtendedProperties || strokeColorInExtendedProperties) { result.AppendLine(">"); if (fillColorInExtendedProperties) { SourceGeneratorHelper.GenerateBrush(result, graphicPath.FillBrush, "Path.Fill", level + 1); } if (strokeColorInExtendedProperties) { SourceGeneratorHelper.GenerateBrush(result, graphicPath.StrokeBrush, "Path.Stroke", level + 1); } result.Append(indentTag); result.Append($"</{tag}>"); } else { result.Append(" />"); } return(result.ToString()); }