public static AvaloniaPicture Record(Svg.Picture.Picture picture) { var avaloniaPicture = new AvaloniaPicture(); if (picture.Commands == null) { return(avaloniaPicture); } foreach (var canvasCommand in picture.Commands) { Record(canvasCommand, avaloniaPicture); } return(avaloniaPicture); }
public static string Generate(SP.Picture picture, string namespaceName, string className) { var counter = new SkiaCodeGenObjectCounter(); var sb = new StringBuilder(); sb.AppendLine($"using SkiaSharp;"); sb.AppendLine($""); sb.AppendLine($"namespace {namespaceName}"); sb.AppendLine($"{{"); sb.AppendLine($" public class {className}"); sb.AppendLine($" {{"); sb.AppendLine($" public static SKPicture Picture {{ get; }}"); sb.AppendLine($""); sb.AppendLine($" static {className}()"); sb.AppendLine($" {{"); sb.AppendLine($" Picture = Record();"); sb.AppendLine($" }}"); sb.AppendLine($""); sb.AppendLine($" private static SKPicture Record()"); sb.AppendLine($" {{"); var indent = " "; var counterPicture = ++counter.Picture; picture.ToSKPicture(counter, sb, indent); sb.AppendLine($"{indent}return {counter.PictureVarName}{counterPicture};"); sb.AppendLine($" }}"); sb.AppendLine($""); sb.AppendLine($" public static void Draw(SKCanvas {counter.CanvasVarName})"); sb.AppendLine($" {{"); sb.AppendLine($" {counter.CanvasVarName}.DrawPicture(Picture);"); sb.AppendLine($" }}"); sb.AppendLine($" }}"); sb.AppendLine($"}}"); var code = sb.ToString(); return(code); }
private static void ToShape(SP.Picture picture, List <IBaseShape> shapes, IFactory factory) { foreach (var canvasCommand in picture.Commands) { switch (canvasCommand) { case SP.ClipPathCanvasCommand clipPathCanvasCommand: { // TODO: } break; case SP.ClipRectCanvasCommand clipRectCanvasCommand: { // TODO: } break; case SP.SaveCanvasCommand _: { // TODO: } break; case SP.RestoreCanvasCommand _: { // TODO: } break; case SP.SetMatrixCanvasCommand setMatrixCanvasCommand: { // TODO: } break; case SP.SaveLayerCanvasCommand saveLayerCanvasCommand: { // TODO: } break; case SP.DrawImageCanvasCommand drawImageCanvasCommand: { // TODO: } break; case SP.DrawPathCanvasCommand drawPathCanvasCommand: { if (drawPathCanvasCommand.Path != null && drawPathCanvasCommand.Paint != null) { if (drawPathCanvasCommand.Path.Commands?.Count == 1) { var pathCommand = drawPathCanvasCommand.Path.Commands[0]; var success = false; switch (pathCommand) { case SP.AddRectPathCommand addRectPathCommand: { var style = ToStyle(drawPathCanvasCommand.Paint, factory); var rectangleShape = factory.CreateRectangleShape( addRectPathCommand.Rect.Left, addRectPathCommand.Rect.Top, addRectPathCommand.Rect.Right, addRectPathCommand.Rect.Bottom, style, IsStroked(drawPathCanvasCommand.Paint), IsFilled(drawPathCanvasCommand.Paint)); shapes.Add(rectangleShape); success = true; } break; case SP.AddRoundRectPathCommand addRoundRectPathCommand: { // TODO: } break; case SP.AddOvalPathCommand addOvalPathCommand: { var style = ToStyle(drawPathCanvasCommand.Paint, factory); var ellipseShape = factory.CreateEllipseShape( addOvalPathCommand.Rect.Left, addOvalPathCommand.Rect.Top, addOvalPathCommand.Rect.Right, addOvalPathCommand.Rect.Bottom, style, IsStroked(drawPathCanvasCommand.Paint), IsFilled(drawPathCanvasCommand.Paint)); shapes.Add(ellipseShape); success = true; } break; case SP.AddCirclePathCommand addCirclePathCommand: { var style = ToStyle(drawPathCanvasCommand.Paint, factory); var x = addCirclePathCommand.X; var y = addCirclePathCommand.Y; var radius = addCirclePathCommand.Radius; var ellipseShape = factory.CreateEllipseShape( x - radius, y - radius, x + radius, y + radius, style, IsStroked(drawPathCanvasCommand.Paint), IsFilled(drawPathCanvasCommand.Paint)); shapes.Add(ellipseShape); success = true; } break; case SP.AddPolyPathCommand addPolyPathCommand: { if (addPolyPathCommand.Points != null) { var polyGeometry = ToPathGeometry( addPolyPathCommand, drawPathCanvasCommand.Path.FillType, IsFilled(drawPathCanvasCommand.Paint), addPolyPathCommand.Close, factory); if (polyGeometry != null) { var style = ToStyle(drawPathCanvasCommand.Paint, factory); var pathShape = factory.CreatePathShape( "Path", style, polyGeometry, IsStroked(drawPathCanvasCommand.Paint), IsFilled(drawPathCanvasCommand.Paint)); shapes.Add(pathShape); success = true; } } } break; } if (success) { break; } } if (drawPathCanvasCommand.Path.Commands?.Count == 2) { var pathCommand1 = drawPathCanvasCommand.Path.Commands[0]; var pathCommand2 = drawPathCanvasCommand.Path.Commands[1]; if (pathCommand1 is SP.MoveToPathCommand moveTo && pathCommand2 is SP.LineToPathCommand lineTo) { var style = ToStyle(drawPathCanvasCommand.Paint, factory); var pathShape = factory.CreateLineShape( moveTo.X, moveTo.Y, lineTo.X, lineTo.Y, style, IsStroked(drawPathCanvasCommand.Paint)); shapes.Add(pathShape); break; } } var geometry = ToPathGeometry(drawPathCanvasCommand.Path, IsFilled(drawPathCanvasCommand.Paint), factory); if (geometry != null) { var style = ToStyle(drawPathCanvasCommand.Paint, factory); var pathShape = factory.CreatePathShape( "Path", style, geometry, IsStroked(drawPathCanvasCommand.Paint), IsFilled(drawPathCanvasCommand.Paint)); shapes.Add(pathShape); } } } break; case SP.DrawTextBlobCanvasCommand drawTextBlobCanvasCommand: { // TODO: } break; case SP.DrawTextCanvasCommand drawTextCanvasCommand: { if (drawTextCanvasCommand.Paint != null) { var style = ToStyle(drawTextCanvasCommand.Paint, factory); var pathShape = factory.CreateTextShape( drawTextCanvasCommand.X, drawTextCanvasCommand.Y, style, drawTextCanvasCommand.Text, IsFilled(drawTextCanvasCommand.Paint)); shapes.Add(pathShape); } } break; case SP.DrawTextOnPathCanvasCommand drawTextOnPathCanvasCommand: { // TODO: } break; default: break; } } }