SvgToGeometry.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. svgJsStr = svgJsStr.Substring(svgJsStr.IndexOf("<svg>"),
  26. svgJsStr.Length - (svgJsStr.Length - (svgJsStr.IndexOf("</svg>") + "</svg>".Length)) - svgJsStr.IndexOf("<svg>"));
  27. XmlDocument xmlDoc = new XmlDocument();
  28. xmlDoc.LoadXml(svgJsStr);
  29. XmlNodeList nodeList = xmlDoc.SelectNodes("/svg/symbol");
  30. JObject jo = ReadJson(jsonStream);
  31. JArray ja = JArray.Parse(jo["glyphs"].ToString());
  32. List<IconfontInfo> listInfo = new List<IconfontInfo>();
  33. for (int i = 0; i < nodeList.Count; i++)
  34. {
  35. XmlNodeList pathNodes = nodeList[i].SelectNodes("path");
  36. string text = "";
  37. foreach (XmlNode pathNode in pathNodes)
  38. {
  39. text += pathNode.Attributes["d"].Value;
  40. }
  41. string name = JObject.Parse(ja[i].ToString())["name"].ToString();
  42. listInfo.Add(new IconfontInfo
  43. {
  44. Text = text,
  45. Name = name
  46. });
  47. }
  48. return listInfo;
  49. }
  50. public static JObject ReadJson(Stream stream)
  51. {
  52. using (StreamReader file = new StreamReader(stream))
  53. {
  54. using (JsonTextReader reader = new JsonTextReader(file))
  55. {
  56. JObject o = (JObject)JToken.ReadFrom(reader);
  57. return o;
  58. }
  59. }
  60. }
  61. public static string GetMd5Str(string ConvertString)
  62. {
  63. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  64. string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
  65. t2 = t2.Replace("-", "");
  66. t2 = t2.ToLower();
  67. return t2;
  68. }
  69. }
  70. }