SvgToGeometry.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using GeekDesk.ViewModel;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Xml;
  13. namespace GeekDesk.Util
  14. {
  15. class SvgToGeometry
  16. {
  17. public static List<IconfontInfo> GetIconfonts()
  18. {
  19. string svgPath = "/GeekDesk;component/Resource/Iconfont/iconfont.js";
  20. string jsonPath = "/GeekDesk;component/Resource/Iconfont/iconfont.json";
  21. Stream svgStream = Application.GetResourceStream(new Uri(svgPath, UriKind.Relative)).Stream;
  22. Stream jsonStream = Application.GetResourceStream(new Uri(jsonPath, UriKind.Relative)).Stream;
  23. StreamReader streamReader = new StreamReader(svgStream);
  24. string svgJsStr = streamReader.ReadToEnd();
  25. JObject jo = ReadJson(jsonStream);
  26. return GetIconfonts(svgJsStr, jo);
  27. }
  28. public static List<IconfontInfo> GetIconfonts(string svgJsStr, string jsonStr)
  29. {
  30. return GetIconfonts(svgJsStr, JObject.Parse(jsonStr));
  31. }
  32. public static List<IconfontInfo> GetIconfonts(string svgJsStr, JObject json)
  33. {
  34. svgJsStr = svgJsStr.Substring(svgJsStr.IndexOf("<svg>"),
  35. svgJsStr.Length - (svgJsStr.Length - (svgJsStr.IndexOf("</svg>") + "</svg>".Length)) - svgJsStr.IndexOf("<svg>"));
  36. XmlDocument xmlDoc = new XmlDocument();
  37. xmlDoc.LoadXml(svgJsStr);
  38. XmlNodeList nodeList = xmlDoc.SelectNodes("/svg/symbol");
  39. JArray ja = JArray.Parse(json["glyphs"].ToString());
  40. List<IconfontInfo> listInfo = new List<IconfontInfo>();
  41. for (int i = 0; i < nodeList.Count; i++)
  42. {
  43. XmlNodeList pathNodes = nodeList[i].SelectNodes("path");
  44. string text = "";
  45. foreach (XmlNode pathNode in pathNodes)
  46. {
  47. text += pathNode.Attributes["d"].Value;
  48. }
  49. string name = JObject.Parse(ja[i].ToString())["name"].ToString();
  50. listInfo.Add(new IconfontInfo
  51. {
  52. Text = text,
  53. Name = name
  54. });
  55. }
  56. return listInfo;
  57. }
  58. public static JObject ReadJson(Stream stream)
  59. {
  60. using (StreamReader file = new StreamReader(stream))
  61. {
  62. using (JsonTextReader reader = new JsonTextReader(file))
  63. {
  64. JObject o = (JObject)JToken.ReadFrom(reader);
  65. return o;
  66. }
  67. }
  68. }
  69. public static string GetMd5Str(string ConvertString)
  70. {
  71. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  72. string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
  73. t2 = t2.Replace("-", "");
  74. t2 = t2.ToLower();
  75. return t2;
  76. }
  77. }
  78. }