SvgToGeometry.cs 3.0 KB

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