/// <summary> /// 构建 Path 字符串,这里输出的是 svg 路径格式,可以作为 WPF 或 UWP 的 Geometry 字符串 /// </summary> /// <param name="pathList"></param> /// 有关 Geometry 使用字符串表示方式请自行百度 “svg 路径 格式” public static (string stringPath, bool isLine) BuildPathString(PathList pathList) { var stringPath = new StringBuilder(128); bool isLine = true; foreach (var path in pathList.Elements <DocumentFormat.OpenXml.Drawing.Path>()) { EmuPoint currentPixelPoint = default; foreach (var pathData in path.ChildElements) { currentPixelPoint = ConvertToPathString(pathData, stringPath, currentPixelPoint, out var isPathLine); if (!isPathLine) { isLine = false; } } } return(stringPath.ToString(), isLine); }
private static EmuPoint ConvertToPathString(OpenXmlElement pathData, StringBuilder stringPath, EmuPoint currentPoint, out bool isLine) { isLine = true; switch (pathData) { case MoveTo moveTo: { // 关于定义的 Key 的值请百度参考 svg 规范 var defineKey = "M"; var moveToPoint = moveTo.Point; if (moveToPoint?.X != null && moveToPoint?.Y != null) { stringPath.Append(defineKey); var emuPoint = moveToPoint.PointToEmuPoint(); var point = emuPoint.ToPixelPoint(); PointToString(point); return(emuPoint); } break; } case LineTo lineTo: { var defineKey = "L"; var lineToPoint = lineTo.Point; if (lineToPoint?.X != null && lineToPoint?.Y != null) { stringPath.Append(defineKey); var emuPoint = lineToPoint.PointToEmuPoint(); var point = emuPoint.ToPixelPoint(); PointToString(point); return(emuPoint); } break; } case ArcTo arcTo: { Degree swingAngDegree = new Degree(0); var swingAngleString = arcTo.SwingAngle; if (swingAngleString != null) { if (int.TryParse(swingAngleString, out var swingAngle)) { swingAngDegree = new Degree(swingAngle); } } Degree startAngleDegree = new Degree(0); var startAngleString = arcTo.StartAngle; if (startAngleString != null) { if (int.TryParse(startAngleString.Value, out var startAngle)) { startAngleDegree = new Degree(startAngle); } } var widthRadius = EmuStringToEmu(arcTo.WidthRadius); var heightRadius = EmuStringToEmu(arcTo.HeightRadius); return(ArcToToString(stringPath, currentPoint, widthRadius, heightRadius, startAngleDegree, swingAngDegree)); } case QuadraticBezierCurveTo quadraticBezierCurveTo: { var defineKey = "Q"; return(ConvertPointList(quadraticBezierCurveTo, defineKey, stringPath)); } case CubicBezierCurveTo cubicBezierCurveTo: { var defineKey = "C"; return(ConvertPointList(cubicBezierCurveTo, defineKey, stringPath)); } case CloseShapePath closeShapePath: { var defineKey = "Z"; isLine = false; stringPath.Append(defineKey); break; } } return(default);