ShapeHelper.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Xml;
  5. using System.Runtime;
  6. using System.Xml.Linq;
  7. using System.Xml.XPath;
  8. using DocumentFormat.OpenXml;
  9. namespace HTEXLib
  10. {
  11. public static class ShapeHelper
  12. {
  13. private const int degree = 4;
  14. private const double px96 = 96.00, px72 = 72.00, px914400 = 914400.00, px12700 = 12700.00;
  15. private const double rot60000 = 60000.00;
  16. public static XmlNamespaceManager xmlnsManager;
  17. public static XmlNode GetTextByPath(this XmlDocument document, string expression)
  18. {
  19. return document.SelectSingleNode(expression, xmlnsManager);
  20. }
  21. public static XmlNode GetTextByPath(this XmlNode node, string expression)
  22. {
  23. return node.SelectSingleNode(expression, xmlnsManager);
  24. }
  25. public static XmlNodeList GetTextByPathList(this XmlDocument document, string expression)
  26. {
  27. return document.SelectNodes(expression, xmlnsManager);
  28. }
  29. public static XmlNodeList GetTextByPathList(this XmlNode node, string expression)
  30. {
  31. return node.SelectNodes(expression, xmlnsManager);
  32. }
  33. public static IEnumerable<XElement> GetTextByPathList(this XElement xElement, string expression)
  34. {
  35. return xElement.XPathSelectElements(expression, xmlnsManager);
  36. }
  37. public static XElement GetTextByPath(this XElement xElement, string expression)
  38. {
  39. return xElement.XPathSelectElement(expression, xmlnsManager);
  40. }
  41. public static IEnumerable<XElement> GetTextByPathList(this OpenXmlElement node, string expression)
  42. {
  43. return XElement.Parse(node.OuterXml).XPathSelectElements(expression, xmlnsManager);
  44. }
  45. public static XElement GetTextByPath(this OpenXmlElement node, string expression)
  46. {
  47. return XElement.Parse(node.OuterXml).XPathSelectElement(expression, xmlnsManager);
  48. }
  49. static ShapeHelper()
  50. {
  51. xmlnsManager = new XmlNamespaceManager(new NameTable());
  52. xmlnsManager.AddNamespace("p", "http://schemas.openxmlformats.org/presentationml/2006/main");
  53. xmlnsManager.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
  54. xmlnsManager.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
  55. xmlnsManager.AddNamespace("fo", "http://www.w3.org/1999/XSL/Format");
  56. xmlnsManager.AddNamespace("app", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties");
  57. xmlnsManager.AddNamespace("cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
  58. xmlnsManager.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
  59. xmlnsManager.AddNamespace("dcterms", "http://purl.org/dc/terms/");
  60. xmlnsManager.AddNamespace("dcmitype", "http://purl.org/dc/dcmitype/");
  61. xmlnsManager.AddNamespace("rel", "http://schemas.openxmlformats.org/package/2006/relationships");
  62. xmlnsManager.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
  63. xmlnsManager.AddNamespace("cus", "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties");
  64. xmlnsManager.AddNamespace("vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes");
  65. xmlnsManager.AddNamespace("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");
  66. xmlnsManager.AddNamespace("dsp", "http://schemas.microsoft.com/office/drawing/2008/diagram");
  67. xmlnsManager.AddNamespace("dgm", "http://schemas.openxmlformats.org/drawingml/2006/diagram");
  68. xmlnsManager.AddNamespace("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
  69. xmlnsManager.AddNamespace("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
  70. xmlnsManager.AddNamespace("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
  71. xmlnsManager.AddNamespace("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
  72. xmlnsManager.AddNamespace("pkg", "http://schemas.microsoft.com/office/2006/xmlPackage");
  73. xmlnsManager.AddNamespace("p15", "http://schemas.microsoft.com/office/powerpoint/2012/main");
  74. xmlnsManager.AddNamespace("a16", "http://schemas.microsoft.com/office/drawing/2014/main");
  75. xmlnsManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
  76. xmlnsManager.AddNamespace("c", "http://schemas.openxmlformats.org/drawingml/2006/chart");
  77. xmlnsManager.AddNamespace("c14", "http://schemas.microsoft.com/office/drawing/2007/8/2/chart");
  78. xmlnsManager.AddNamespace("c16", "http://schemas.microsoft.com/office/drawing/2014/chart");
  79. xmlnsManager.AddNamespace("cs", "http://schemas.microsoft.com/office/drawing/2012/chartStyle");
  80. xmlnsManager.AddNamespace("thm15", "http://schemas.microsoft.com/office/thememl/2012/main");
  81. xmlnsManager.AddNamespace("fn", "http://www.w3.org/2005/xpath-functions");
  82. }
  83. /// <summary>
  84. /// 调色补充 Shade Tint LumMod LumOff 等
  85. /// </summary>
  86. /// <param name="xml"></param>
  87. /// <param name="color"></param>
  88. /// <returns></returns>
  89. public static string ColorToning(string nodeXml, string colorHex)
  90. {
  91. if (string.IsNullOrEmpty(colorHex)) { return null; }
  92. ColorConverter converter = new ColorConverter();
  93. XmlDocument doc = new XmlDocument();
  94. doc.LoadXml(nodeXml);
  95. XmlNode tint = doc.SelectSingleNode("//a:tint/@val", xmlnsManager);
  96. if (tint != null)
  97. {
  98. colorHex = converter.SetTint(colorHex, double.Parse(tint.Value));
  99. }
  100. XmlNode hueMod = doc.SelectSingleNode("//a:hueMod/@val", xmlnsManager);
  101. if (hueMod != null)
  102. {
  103. colorHex = converter.SetHueMod(colorHex, double.Parse(hueMod.Value));
  104. }
  105. XmlNode satMod = doc.SelectSingleNode("//a:satMod/@val", xmlnsManager);
  106. if (satMod != null)
  107. {
  108. colorHex = converter.SetSaturationMod(colorHex, double.Parse(satMod.Value));
  109. }
  110. XmlNode satOff = doc.SelectSingleNode("//a:satOff/@val", xmlnsManager);
  111. if (satOff != null)
  112. {
  113. colorHex = converter.SetSaturationOff(colorHex, double.Parse(satOff.Value));
  114. }
  115. XmlNode lumMod = doc.SelectSingleNode("//a:lumMod/@val", xmlnsManager);
  116. if (lumMod != null)
  117. {
  118. colorHex = converter.SetLuminanceMod(colorHex, double.Parse(lumMod.Value));
  119. }
  120. XmlNode lumOff = doc.SelectSingleNode("//a:lumOff/@val", xmlnsManager);
  121. if (lumOff != null)
  122. {
  123. colorHex = converter.SetLuminanceOff(colorHex, double.Parse(lumOff.Value));
  124. }
  125. XmlNode hueOff = doc.SelectSingleNode("//a:hueOff/@val", xmlnsManager);
  126. if (hueOff != null)
  127. {
  128. colorHex = converter.SetHueOff(colorHex, double.Parse(hueOff.Value));
  129. }
  130. XmlNode alphaOff = doc.SelectSingleNode("//a:alphaOff/@val", xmlnsManager);
  131. if (alphaOff != null)
  132. {
  133. Color color = ColorTranslator.FromHtml("#" + colorHex);
  134. //color.A * (int.Parse(alpha.Value) / 100000);
  135. color = Color.FromArgb(color.A- color.A * (int.Parse(alphaOff.Value) / 100000), color.R, color.G, color.B);
  136. }
  137. XmlNode shade = doc.SelectSingleNode("//a:shade/@val", xmlnsManager);
  138. if (shade != null)
  139. {
  140. colorHex = converter.SetShade(colorHex, double.Parse(shade.Value));
  141. }
  142. XmlNode alpha = doc.SelectSingleNode("//a:alpha/@val", xmlnsManager);
  143. if (alpha != null)
  144. {
  145. Color color= ColorTranslator.FromHtml("#" + colorHex);
  146. //color.A * (int.Parse(alpha.Value) / 100000);
  147. //TODO是用255计算 还是颜色本身的A alpha计算
  148. color = Color.FromArgb(color.A * (int.Parse(alpha.Value) / 100000), color.R,color.G,color.B);
  149. }
  150. //TODO //颜色调和 说明网站 https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_comp_topic_ID0EATEJB.html
  151. XmlNode comp = doc.SelectSingleNode("//a:comp", xmlnsManager);
  152. if (comp != null) {
  153. colorHex = converter.SetComp(colorHex);
  154. }
  155. XmlNode inv = doc.SelectSingleNode("//a:inv/@val", xmlnsManager);
  156. XmlNode alphaMod = doc.SelectSingleNode("//a:alphaMod/@val", xmlnsManager);
  157. XmlNode gray = doc.SelectSingleNode("//a:gray/@val", xmlnsManager);
  158. XmlNode hue = doc.SelectSingleNode("//a:hue/@val", xmlnsManager);
  159. XmlNode sat = doc.SelectSingleNode("//a:sat/@val", xmlnsManager);
  160. XmlNode lum = doc.SelectSingleNode("//a:lum/@val", xmlnsManager);
  161. XmlNode red = doc.SelectSingleNode("//a:red/@val", xmlnsManager);
  162. XmlNode redOff = doc.SelectSingleNode("//a:redOff/@val", xmlnsManager);
  163. XmlNode redMod = doc.SelectSingleNode("//a:redMod/@val", xmlnsManager);
  164. XmlNode green = doc.SelectSingleNode("//a:green/@val", xmlnsManager);
  165. XmlNode greenOff = doc.SelectSingleNode("//a:greenOff/@val", xmlnsManager);
  166. XmlNode greenMod = doc.SelectSingleNode("//a:greenMod/@val", xmlnsManager);
  167. XmlNode blue = doc.SelectSingleNode("//a:blue/@val", xmlnsManager);
  168. XmlNode blueOff = doc.SelectSingleNode("//a:blueOff/@val", xmlnsManager);
  169. XmlNode blueMod = doc.SelectSingleNode("//a:blueMod/@val", xmlnsManager);
  170. XmlNode gamma = doc.SelectSingleNode("//a:gamma/@val", xmlnsManager);
  171. XmlNode invGamma = doc.SelectSingleNode("//a:invGamma/@val", xmlnsManager);
  172. return colorHex;
  173. }
  174. public static string GetSchemeColorFromTheme(string schemeClr, XmlNode sldMasterNode, Dictionary<string, string> slideLayoutClrOvride, XmlNode themeContent)
  175. {
  176. //<p:clrMap ...> in slide master
  177. // e.g. tx2="dk2" bg2="lt2" tx1="dk1" bg1="lt1" slideLayoutClrOvride
  178. if (slideLayoutClrOvride == null || slideLayoutClrOvride.Count <= 0)
  179. {
  180. if (sldMasterNode != null)
  181. {
  182. var sldLayoutClrOvr = ShapeHelper.GetTextByPath(sldMasterNode, "p:sldMaster/p:clrMap");
  183. if (sldLayoutClrOvr != null)
  184. {
  185. //获取 overrideClrMapping所有属性节点 检查测试 是否slideLayoutClrOvride 需要被覆盖
  186. slideLayoutClrOvride = new Dictionary<string, string>() {
  187. { "accent1", sldLayoutClrOvr.GetTextByPath("@accent1").Value},
  188. { "accent2", sldLayoutClrOvr.GetTextByPath("@accent2").Value},
  189. { "accent3", sldLayoutClrOvr.GetTextByPath("@accent3").Value},
  190. { "accent4", sldLayoutClrOvr.GetTextByPath("@accent4").Value},
  191. { "accent5", sldLayoutClrOvr.GetTextByPath("@accent5").Value},
  192. { "accent6", sldLayoutClrOvr.GetTextByPath("@accent6").Value},
  193. { "bg1", sldLayoutClrOvr.GetTextByPath("@bg1").Value},
  194. { "bg2", sldLayoutClrOvr.GetTextByPath("@bg2").Value},
  195. { "folHlink", sldLayoutClrOvr.GetTextByPath("@folHlink").Value},
  196. { "hlink", sldLayoutClrOvr.GetTextByPath("@hlink").Value},
  197. { "tx1", sldLayoutClrOvr.GetTextByPath("@tx1").Value},
  198. { "tx2", sldLayoutClrOvr.GetTextByPath("@tx2").Value}
  199. };
  200. }
  201. }
  202. }
  203. if (!schemeClr.StartsWith("a:"))
  204. {
  205. schemeClr = "a:" + schemeClr;
  206. }
  207. //console.log(slideLayoutClrOvride);
  208. var schmClrName = schemeClr.Replace("a:", "");
  209. if (slideLayoutClrOvride != null)
  210. {
  211. switch (schmClrName)
  212. {
  213. case "tx1":
  214. case "tx2":
  215. case "bg1":
  216. case "bg2":
  217. schemeClr = "a:" + slideLayoutClrOvride[schmClrName];
  218. //schemeClr = "a:" + slideLayoutClrOvride[schmClrName];
  219. //console.log(schmClrName+ "=> "+schemeClr);
  220. break;
  221. }
  222. }
  223. else
  224. {
  225. switch (schmClrName)
  226. {
  227. case "tx1":
  228. schemeClr = "a:dk1";
  229. break;
  230. case "tx2":
  231. schemeClr = "a:dk2";
  232. break;
  233. case "bg1":
  234. schemeClr = "a:lt1";
  235. break;
  236. case "bg2":
  237. schemeClr = "a:lt2";
  238. break;
  239. }
  240. }
  241. //var refNode = ShapeHelper.GetTextByPathList(themeContent, "a:theme/a:themeElements/a:clrScheme/"+ schemeClr.Value);
  242. var color = ShapeHelper.GetTextByPath(themeContent, "pkg:xmlData/a:theme/a:themeElements/a:clrScheme/" + schemeClr + "/a:srgbClr/@val");
  243. if (color == null)//&& refNode != null
  244. {
  245. color = ShapeHelper.GetTextByPath(themeContent, "pkg:xmlData/a:theme/a:themeElements/a:clrScheme/" + schemeClr + "/a:sysClr/@lastClr");
  246. }
  247. //console.log(color)
  248. return color.Value;
  249. }
  250. public static string GetSvgImagePattern(string fillColor, int shpId)
  251. {
  252. var ptrn = "<pattern id=\"imgPtrn_" + shpId + "\"patternContentUnits=\"objectBoundingBox\" width=\"1\" height=\"1\">";// '<pattern id="imgPtrn_' + shpId + '" patternContentUnits="objectBoundingBox" width="1" height="1">';
  253. ptrn += "<image xlink:href=\"" + fillColor + "\"preserveAspectRatio=\"none\" width=\"1\" height=\"1\"></image>";// '<image xlink:href="' + fillColor + '" preserveAspectRatio="none" width="1" height="1"></image>';
  254. ptrn += "</pattern>";// '';
  255. return ptrn;
  256. }
  257. public static string GetSvgGradient(double w, double h, double angl, List<string> color_arry, int shpId, Dictionary<string, string> slideLayoutClrOvride, XmlNode themeContent)
  258. {
  259. var stopsArray = GetMiddleStops(color_arry.Count - 2);
  260. var svgAngle = "";
  261. double svgHeight = h;
  262. double svgWidth = w;
  263. string svg = "";
  264. List<double> xy_ary = SVGangle(angl, svgHeight, svgWidth, slideLayoutClrOvride, themeContent);
  265. double x1 = xy_ary[0];
  266. double y1 = xy_ary[1];
  267. double x2 = xy_ary[2];
  268. double y2 = xy_ary[3];
  269. var sal = stopsArray.Count;
  270. double sr = sal < 20 ? 100 : 1000;
  271. svgAngle = " gradientUnits=\"userSpaceOnUse\" x1=\"" + x1 + "%\" y1=\"" + y1 + "%\" x2=\"" + x2 + "%\" y2=\"" + y2 + "%\"";
  272. svgAngle = "<linearGradient id=\"linGrd_" + shpId + "\"" + svgAngle + ">\n";
  273. svg += svgAngle;
  274. for (var i = 0; i < sal; i++)
  275. {
  276. svg += "<stop offset=\"" + (System.Math.Round(double.Parse(stopsArray[i].Replace("%", ""))) / 100 * sr) / sr + "\" stop-color=\"" + color_arry[i] + "\"";
  277. svg += "/>\n";
  278. }
  279. svg += "</linearGradient>\n" + "";
  280. return svg;
  281. }
  282. public static List<string> GetMiddleStops(int s)
  283. {
  284. var sArry = new List<string> { "0%" };
  285. if (s == 0)
  286. {
  287. return sArry;
  288. }
  289. else
  290. {
  291. for (int i = s; i < 0; i--)
  292. {
  293. var middleStop = 100 - ((100 / (s + 1)) * (i + 1)); // AM: Ex - For 3 middle stops, progression will be 25%, 50%, and 75%, plus 0% and 100% at the ends.
  294. var middleStopString = middleStop + "%";
  295. sArry.Add(middleStopString);
  296. // sArry.splice(-1, 0, middleStopString);
  297. }
  298. // AM: add into stopsArray before 100%
  299. }
  300. sArry.Add("100%");
  301. return sArry;
  302. }
  303. public static List<double> SVGangle(double deg, double svgHeight, double svgWidth, Dictionary<string, string> slideLayoutClrOvride, XmlNode themeContent)
  304. {
  305. double w = svgWidth;
  306. double h = svgHeight;
  307. double ang = deg;
  308. double o = 2;
  309. double n = 2;
  310. double wc = w / 2;
  311. double hc = h / 2;
  312. double tx1 = 2;
  313. double ty1 = 2;
  314. double tx2 = 2;
  315. double ty2 = 2;
  316. double k = (((ang % 360) + 360) % 360);
  317. double j = (360 - k) * System.Math.PI / 180;
  318. double i = System.Math.Tan(j);
  319. double l = hc - i * wc;
  320. if (k == 0)
  321. {
  322. tx1 = w;
  323. ty1 = hc;
  324. tx2 = 0;
  325. ty2 = hc;
  326. }
  327. else if (k < 90)
  328. {
  329. n = w;
  330. o = 0;
  331. }
  332. else if (k == 90)
  333. {
  334. tx1 = wc;
  335. ty1 = 0;
  336. tx2 = wc;
  337. ty2 = h;
  338. }
  339. else if (k < 180)
  340. {
  341. n = 0;
  342. o = 0;
  343. }
  344. else if (k == 180)
  345. {
  346. tx1 = 0;
  347. ty1 = hc;
  348. tx2 = w;
  349. ty2 = hc;
  350. }
  351. else if (k < 270)
  352. {
  353. n = 0;
  354. o = h;
  355. }
  356. else if (k == 270)
  357. {
  358. tx1 = wc;
  359. ty1 = h;
  360. tx2 = wc;
  361. ty2 = 0;
  362. }
  363. else
  364. {
  365. n = w;
  366. o = h;
  367. }
  368. // AM: I could not quite figure out what m, n, and o are supposed to represent from the original code on visualcsstools.com.
  369. var m = o + (n / i);
  370. tx1 = tx1 == 2 ? i * (m - l) / (System.Math.Pow(i, 2) + 1) : tx1;
  371. ty1 = ty1 == 2 ? i * tx1 + l : ty1;
  372. tx2 = tx2 == 2 ? w - tx1 : tx2;
  373. ty2 = ty2 == 2 ? h - ty1 : ty2;
  374. double x1 = System.Math.Round(tx2 / w * 100 * 100) / 100.0;
  375. double y1 = System.Math.Round(ty2 / h * 100 * 100) / 100.0;
  376. double x2 = System.Math.Round(tx1 / w * 100 * 100) / 100.0;
  377. double y2 = System.Math.Round(ty1 / h * 100 * 100) / 100.0;
  378. return new List<double> { x1, y1, x2, y2 };
  379. }
  380. public static dynamic GetSlideSize(XmlDocument xdoc)
  381. {
  382. var sldSzNode = xdoc.GetTextByPath("//pkg:part[@pkg:name='/ppt/presentation.xml']/pkg:xmlData/p:presentation/p:sldSz");
  383. double width = double.Parse(sldSzNode.GetTextByPath("@cx").Value) * px96 / px914400;
  384. double height = double.Parse(sldSzNode.GetTextByPath("@cy").Value) * px96 / px914400;
  385. return new { width, height };
  386. }
  387. public static Position GetPosition(XmlNode slideXfrmNode, XmlNode slideLayoutXfrmNode, XmlNode slideMasterXfrmNode)
  388. {
  389. XmlNode off = null;
  390. XmlNode ext = null;
  391. double x, y, w, h;
  392. //ext
  393. if (slideXfrmNode != null)
  394. {
  395. ext = ShapeHelper.GetTextByPath(slideXfrmNode, "a:ext");
  396. }
  397. else if (slideLayoutXfrmNode != null && ext == null)
  398. {
  399. ext = ShapeHelper.GetTextByPath(slideLayoutXfrmNode, "a:ext");
  400. }
  401. else if (slideMasterXfrmNode != null && ext == null)
  402. {
  403. ext = ShapeHelper.GetTextByPath(slideMasterXfrmNode, "a:ext");
  404. }
  405. //off
  406. if (slideXfrmNode != null)
  407. {
  408. off = ShapeHelper.GetTextByPath(slideXfrmNode, "a:off");
  409. }
  410. else if (slideLayoutXfrmNode != null && off == null)
  411. {
  412. off = ShapeHelper.GetTextByPath(slideLayoutXfrmNode, "a:off");
  413. }
  414. else if (slideMasterXfrmNode != null && off == null)
  415. {
  416. off = ShapeHelper.GetTextByPath(slideMasterXfrmNode, "a:off");
  417. }
  418. x = System.Math.Round(double.Parse(off.GetTextByPath("@x").Value) * px96 / px914400, degree);
  419. y = System.Math.Round(double.Parse(off.GetTextByPath("@y").Value) * px96 / px914400, degree);
  420. w = System.Math.Round(double.Parse(ext.GetTextByPath("@cx").Value) * px96 / px914400, degree);
  421. h = System.Math.Round(double.Parse(ext.GetTextByPath("@cy").Value) * px96 / px914400, degree);
  422. return new Position()
  423. {
  424. cx = w,
  425. cy = h,
  426. x = x,
  427. y = y
  428. };
  429. }
  430. public static NodesTable IndexNodes(XmlNode content)
  431. {
  432. var idTable = new Dictionary<string, XmlNode>();
  433. var idxTable = new Dictionary<string, XmlNode>();
  434. var typeTable = new Dictionary<string, XmlNode>();
  435. var spTreeNode = content.GetTextByPath("p:cSld/p:spTree");
  436. // Dictionary<string, Dictionary<string, string>> slideResObj = new Dictionary<string, Dictionary<string, string>>();
  437. var spTreeNodeChildren = spTreeNode.ChildNodes;
  438. foreach (XmlNode child in spTreeNodeChildren)
  439. {
  440. if (child.Name.Equals("p:nvGrpSpPr") || child.Name.Equals("p:grpSpPr"))
  441. {
  442. continue;
  443. }
  444. //var targetNode = child.ChildNodes;
  445. var targetNode = child.GetTextByPath("p:nvSpPr");
  446. // < p:cNvCxnSpPr >
  447. // < a:cxnSpLocks /> ///这个是什么意思
  448. // </ p:cNvCxnSpPr >
  449. if (targetNode == null)
  450. {
  451. targetNode = child.GetTextByPath("p:nvCxnSpPr");
  452. }
  453. XmlNode id = null, idx = null, type = null;
  454. if (targetNode != null)
  455. {
  456. id = targetNode.GetTextByPath("p:cNvPr/@id");
  457. idx = targetNode.GetTextByPath("p:nvPr/p:ph/@idx");
  458. type = targetNode.GetTextByPath("p:nvPr/p:ph/@type");
  459. }
  460. // Dictionary<string, XmlNode> node = new Dictionary<string, XmlNode>();
  461. //if (child.ChildNodes != null)
  462. //{
  463. // foreach (XmlNode xmlNode in child.ChildNodes)
  464. // {
  465. // node.TryAdd(xmlNode.Name, xmlNode);
  466. // }
  467. //}
  468. if (id != null)
  469. {
  470. idTable[id.Value] = child;
  471. }
  472. if (idx != null)
  473. {
  474. idxTable[idx.Value] = child;
  475. }
  476. if (type != null)
  477. {
  478. ///会出现相同Key
  479. typeTable[type.Value] = child;
  480. }
  481. }
  482. return new NodesTable { idTable = idTable, idxTable = idxTable, typeTable = typeTable };
  483. }
  484. public static double AngleToDegrees(XmlNode angle)
  485. {
  486. if (angle == null)
  487. {
  488. return 0;
  489. }
  490. return double.Parse(angle.Value) / rot60000;
  491. }
  492. public static double AngleToDegrees(string angle)
  493. {
  494. if (angle == null)
  495. {
  496. return 0;
  497. }
  498. return double.Parse(angle) / rot60000;
  499. }
  500. public static XmlNode GetNodesTable(XmlNode id, XmlNode idx, XmlNode type, WarpObj warpObj, string LayoutOrMaster)
  501. {
  502. XmlNode resNode = null;
  503. if (resNode == null)
  504. {
  505. if (id != null)
  506. {
  507. if (LayoutOrMaster.Equals("Layout"))
  508. {
  509. if (warpObj.slideLayoutTables.idTable.TryGetValue(id.Value, out XmlNode LayoutNode))
  510. {
  511. // LayoutNode.TryGetValue(id.Value, out XmlNode node);
  512. resNode = LayoutNode;
  513. };
  514. }
  515. if (LayoutOrMaster.Equals("Master"))
  516. {
  517. if (warpObj.slideMasterTables.idTable.TryGetValue(id.Value, out XmlNode MasterNode))
  518. {
  519. // LayoutNode.TryGetValue(id.Value, out XmlNode node);
  520. resNode = MasterNode;
  521. };
  522. }
  523. }
  524. }
  525. if (resNode == null)
  526. {
  527. if (idx != null)
  528. {
  529. if (LayoutOrMaster.Equals("Layout"))
  530. {
  531. if (warpObj.slideLayoutTables.idxTable.TryGetValue(idx.Value, out XmlNode LayoutNode))
  532. {
  533. //LayoutNode.TryGetValue(idx.Value, out XmlNode node);
  534. resNode = LayoutNode;
  535. };
  536. }
  537. if (LayoutOrMaster.Equals("Master"))
  538. {
  539. if (warpObj.slideMasterTables.idxTable.TryGetValue(idx.Value, out XmlNode MasterNode))
  540. {
  541. // LayoutNode.TryGetValue(idx.Value, out XmlNode node);
  542. resNode = MasterNode;
  543. };
  544. }
  545. }
  546. }
  547. if (resNode == null)
  548. {
  549. if (type != null)
  550. {
  551. if (LayoutOrMaster.Equals("Layout"))
  552. {
  553. if (warpObj.slideLayoutTables.typeTable.TryGetValue(type.Value, out XmlNode LayoutNode))
  554. {
  555. // LayoutNode.TryGetValue(type.Value, out XmlNode node);
  556. resNode = LayoutNode;
  557. };
  558. }
  559. if (LayoutOrMaster.Equals("Master"))
  560. {
  561. if (warpObj.slideMasterTables.typeTable.TryGetValue(type.Value, out XmlNode MasterNode))
  562. {
  563. // MasterNode.TryGetValue(type.Value, out XmlNode node);
  564. resNode = MasterNode;
  565. };
  566. }
  567. }
  568. }
  569. return resNode;
  570. }
  571. public static string HexToRgbNew(string bgColor)
  572. {
  573. //var arrBuff = new ArrayBuffer(4);
  574. //var vw = new DataView(arrBuff);
  575. //vw.setUint32(0, parseInt(hex, 16), false);
  576. //var arrByte = new Uint8Array(arrBuff);
  577. int r = System.Convert.ToInt32("0x" + bgColor.Substring(0, 2), 16);
  578. int g = System.Convert.ToInt32("0x" + bgColor.Substring(2, 2), 16);
  579. int b = System.Convert.ToInt32("0x" + bgColor.Substring(4, 2), 16);
  580. return r + "," + g + "," + b;
  581. }
  582. public static double GetColorOpacity(XmlNode solidFill)
  583. {
  584. double opcity = 1;
  585. if (solidFill == null)
  586. {
  587. return opcity;
  588. }
  589. if (solidFill.GetTextByPath("a:srgbClr") != null)
  590. {
  591. var tint = solidFill.GetTextByPath("a:srgbClr/a:tint/@val");
  592. if (tint != null)
  593. {
  594. opcity = double.Parse(tint.Value) / 100000.0;
  595. }
  596. }
  597. else if (solidFill.GetTextByPath("a:schemeClr") != null)
  598. {
  599. var tint = solidFill.GetTextByPath("a:schemeClr/a:tint/@val");
  600. if (tint != null)
  601. {
  602. opcity = double.Parse(tint.Value) / 100000.0;
  603. }
  604. }
  605. else if (solidFill.GetTextByPath("a:scrgbClr") != null)
  606. {
  607. var tint = solidFill.GetTextByPath("a:scrgbClr/a:tint/@val");
  608. if (tint != null)
  609. {
  610. opcity = double.Parse(tint.Value) / 100000.0;
  611. }
  612. }
  613. else if (solidFill.GetTextByPath("a:prstClr") != null)
  614. {
  615. var tint = solidFill.GetTextByPath("a:prstClr/a:tint/@val");
  616. if (tint != null)
  617. {
  618. opcity = double.Parse(tint.Value) / 100000.0;
  619. }
  620. }
  621. else if (solidFill.GetTextByPath("a:hslClr") != null)
  622. {
  623. var tint = solidFill.GetTextByPath("a:hslClr/a:tint/@val");
  624. if (tint != null)
  625. {
  626. opcity = double.Parse(tint.Value) / 100000.0;
  627. }
  628. }
  629. else if (solidFill.GetTextByPath("a:sysClr") != null)
  630. {
  631. var tint = solidFill.GetTextByPath("a:sysClr/a:tint/@val");
  632. if (tint != null)
  633. {
  634. opcity = double.Parse(tint.Value) / 100000.0;
  635. }
  636. }
  637. return opcity;
  638. }
  639. public static string GetSolidFill(XmlNode node, Dictionary<string, string> slideLayoutClrOvride, XmlNode slideMasterContent, XmlNode themeContent)
  640. {
  641. if (node == null)
  642. {
  643. return null;
  644. }
  645. string Color = null;
  646. // Fill fill = new Fill() { Color = "FFF", Type = 1 };
  647. var srgbClrval = node.GetTextByPath("a:srgbClr/@val");
  648. var schemeClrval = node.GetTextByPath("a:schemeClr/@val");
  649. if (srgbClrval != null)
  650. {
  651. Color = srgbClrval.Value; //#...
  652. }
  653. else if (schemeClrval != null)//node["a:schemeClr"] != null)
  654. {
  655. //a:schemeClr
  656. // var schemeClr = ShapeHelper.GetTextByPath(node, "a:schemeClr/@val");
  657. //console.log(schemeClr)
  658. Color = ShapeHelper.GetSchemeColorFromTheme(schemeClrval.Value, slideMasterContent, slideLayoutClrOvride, themeContent); //#...
  659. }
  660. else if (ShapeHelper.GetTextByPath(node, "a:scrgbClr") != null)
  661. {
  662. //<a:scrgbClr r="50%" g="50%" b="50%"/> //Need to test/////////////////////////////////////////////
  663. var defBultColorValsR = ShapeHelper.GetTextByPath(node, "a:scrgbClr/@r");
  664. var defBultColorValsG = ShapeHelper.GetTextByPath(node, "a:scrgbClr/@g");
  665. var defBultColorValsB = ShapeHelper.GetTextByPath(node, "a:scrgbClr/@b");
  666. var red = (defBultColorValsR.Value.IndexOf("%") != -1) ? defBultColorValsR.Value.Split('%').First() : defBultColorValsR.Value;
  667. var green = (defBultColorValsG.Value.IndexOf("%") != -1) ? defBultColorValsG.Value.Split('%').First() : defBultColorValsG.Value;
  668. var blue = (defBultColorValsB.Value.IndexOf("%") != -1) ? defBultColorValsB.Value.Split('%').First() : defBultColorValsB.Value;
  669. var scrgbClr = red + "," + green + "," + blue;
  670. Color = ToHex(255 * (int.Parse(red) / 100)) + ToHex(255 * (int.Parse(green) / 100)) + ToHex(255 * (int.Parse(blue) / 100));
  671. //console.log("scrgbClr: " + scrgbClr);
  672. }
  673. else if (ShapeHelper.GetTextByPath(node, "a:prstClr") != null)
  674. {
  675. //<a:prstClr val="black"/> //Need to test/////////////////////////////////////////////
  676. var prstClr = ShapeHelper.GetTextByPath(node, "a:prstClr/@val");// node["a:prstClr"]["attrs"]["val"];
  677. Color = GetColorName2Hex(prstClr.Value);
  678. //console.log("prstClr: " + prstClr+" => hexClr: "+color);
  679. }
  680. else if (ShapeHelper.GetTextByPath(node, "a:hslClr") != null)
  681. {
  682. //<a:hslClr hue="14400000" sat="100%" lum="50%"/> //Need to test/////////////////////////////////////////////
  683. var defBultColorVals = ShapeHelper.GetTextByPath(node, "a:hslClr");//["attrs"];
  684. var defBultColorVals_hue = ShapeHelper.GetTextByPath(defBultColorVals, "@hue");
  685. var defBultColorVals_sat = ShapeHelper.GetTextByPath(defBultColorVals, "@sat");
  686. var defBultColorVals_lum = ShapeHelper.GetTextByPath(defBultColorVals, "@lum");
  687. var hue = int.Parse(defBultColorVals_hue.Value) / 100000;
  688. var sat = int.Parse((defBultColorVals_sat.Value.IndexOf("%") != -1) ? defBultColorVals_sat.Value.Split('%').First() : defBultColorVals_sat.Value) / 100;
  689. var lum = int.Parse((defBultColorVals_lum.Value.IndexOf("%") != -1) ? defBultColorVals_lum.Value.Split('%').First() : defBultColorVals_lum.Value) / 100;
  690. var hslClr = defBultColorVals_hue.Value.ToString() + "," + defBultColorVals_sat.Value.ToString() + "," + defBultColorVals_lum.Value.ToString();
  691. var hsl2rgb = HslToRgb(hue, sat, lum);
  692. Color = ToHex(hsl2rgb.r) + ToHex(hsl2rgb.g) + ToHex(hsl2rgb.b);
  693. //defBultColor = cnvrtHslColor2Hex(hslClr); //TODO
  694. // console.log("hslClr: " + hslClr);
  695. }
  696. else if (ShapeHelper.GetTextByPath(node, "a:sysClr") != null)
  697. {
  698. //<a:sysClr val="windowText" lastClr="000000"/> //Need to test/////////////////////////////////////////////
  699. var sysClr = ShapeHelper.GetTextByPath(node, "a:sysClr/@lastClr").Value;
  700. if (sysClr != null)
  701. {
  702. Color = sysClr;
  703. }
  704. }
  705. return Color;
  706. }
  707. public static string ToHex(int n)
  708. {
  709. string hex = System.Convert.ToString(n, 16);
  710. while (hex.Length < 2) { hex = "0" + hex; }
  711. return hex;
  712. }
  713. public static dynamic HslToRgb(int hue, int sat, int light)
  714. {
  715. int t1, t2, r, g, b;
  716. hue /= 60;
  717. if (light <= 0.5)
  718. {
  719. t2 = light * (sat + 1);
  720. }
  721. else
  722. {
  723. t2 = light + sat - (light * sat);
  724. }
  725. t1 = light * 2 - t2;
  726. r = HueToRgb(t1, t2, hue + 2) * 255;
  727. g = HueToRgb(t1, t2, hue) * 255;
  728. b = HueToRgb(t1, t2, hue - 2) * 255;
  729. return new { r, g, b };
  730. }
  731. public static int HueToRgb(int t1, int t2, int hue)
  732. {
  733. if (hue < 0) hue += 6;
  734. if (hue >= 6) hue -= 6;
  735. if (hue < 1) return (t2 - t1) * hue + t1;
  736. else if (hue < 3) return t2;
  737. else if (hue < 4) return (t2 - t1) * (4 - hue) + t1;
  738. else return t1;
  739. }
  740. public static string GetColorName2Hex(string name)
  741. {
  742. var hex = "";
  743. var colorName = new List<string> { "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond", "Blue", "BlueViolet", "Brown", "BurlyWood", "CadetBlue", "Chartreuse", "Chocolate", "Coral", "CornflowerBlue", "Cornsilk", "Crimson", "Cyan", "DarkBlue", "DarkCyan", "DarkGoldenRod", "DarkGray", "DarkGrey", "DarkGreen", "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "DarkOrange", "DarkOrchid", "DarkRed", "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkSlateGrey", "DarkTurquoise", "DarkViolet", "DeepPink", "DeepSkyBlue", "DimGray", "DimGrey", "DodgerBlue", "FireBrick", "FloralWhite", "ForestGreen", "Fuchsia", "Gainsboro", "GhostWhite", "Gold", "GoldenRod", "Gray", "Grey", "Green", "GreenYellow", "HoneyDew", "HotPink", "IndianRed", "Indigo", "Ivory", "Khaki", "Lavender", "LavenderBlush", "LawnGreen", "LemonChiffon", "LightBlue", "LightCoral", "LightCyan", "LightGoldenRodYellow", "LightGray", "LightGrey", "LightGreen", "LightPink", "LightSalmon", "LightSeaGreen", "LightSkyBlue", "LightSlateGray", "LightSlateGrey", "LightSteelBlue", "LightYellow", "Lime", "LimeGreen", "Linen", "Magenta", "Maroon", "MediumAquaMarine", "MediumBlue", "MediumOrchid", "MediumPurple", "MediumSeaGreen", "MediumSlateBlue", "MediumSpringGreen", "MediumTurquoise", "MediumVioletRed", "MidnightBlue", "MintCream", "MistyRose", "Moccasin", "NavajoWhite", "Navy", "OldLace", "Olive", "OliveDrab", "Orange", "OrangeRed", "Orchid", "PaleGoldenRod", "PaleGreen", "PaleTurquoise", "PaleVioletRed", "PapayaWhip", "PeachPuff", "Peru", "Pink", "Plum", "PowderBlue", "Purple", "RebeccaPurple", "Red", "RosyBrown", "RoyalBlue", "SaddleBrown", "Salmon", "SandyBrown", "SeaGreen", "SeaShell", "Sienna", "Silver", "SkyBlue", "SlateBlue", "SlateGray", "SlateGrey", "Snow", "SpringGreen", "SteelBlue", "Tan", "Teal", "Thistle", "Tomato", "Turquoise", "Violet", "Wheat", "White", "WhiteSmoke", "Yellow", "YellowGreen" };
  744. var colorHex = new List<string> { "f0f8ff", "faebd7", "00ffff", "7fffd4", "f0ffff", "f5f5dc", "ffe4c4", "000000", "ffebcd", "0000ff", "8a2be2", "a52a2a", "deb887", "5f9ea0", "7fff00", "d2691e", "ff7f50", "6495ed", "fff8dc", "dc143c", "00ffff", "00008b", "008b8b", "b8860b", "a9a9a9", "a9a9a9", "006400", "bdb76b", "8b008b", "556b2f", "ff8c00", "9932cc", "8b0000", "e9967a", "8fbc8f", "483d8b", "2f4f4f", "2f4f4f", "00ced1", "9400d3", "ff1493", "00bfff", "696969", "696969", "1e90ff", "b22222", "fffaf0", "228b22", "ff00ff", "dcdcdc", "f8f8ff", "ffd700", "daa520", "808080", "808080", "008000", "adff2f", "f0fff0", "ff69b4", "cd5c5c", "4b0082", "fffff0", "f0e68c", "e6e6fa", "fff0f5", "7cfc00", "fffacd", "add8e6", "f08080", "e0ffff", "fafad2", "d3d3d3", "d3d3d3", "90ee90", "ffb6c1", "ffa07a", "20b2aa", "87cefa", "778899", "778899", "b0c4de", "ffffe0", "00ff00", "32cd32", "faf0e6", "ff00ff", "800000", "66cdaa", "0000cd", "ba55d3", "9370db", "3cb371", "7b68ee", "00fa9a", "48d1cc", "c71585", "191970", "f5fffa", "ffe4e1", "ffe4b5", "ffdead", "000080", "fdf5e6", "808000", "6b8e23", "ffa500", "ff4500", "da70d6", "eee8aa", "98fb98", "afeeee", "db7093", "ffefd5", "ffdab9", "cd853f", "ffc0cb", "dda0dd", "b0e0e6", "800080", "663399", "ff0000", "bc8f8f", "4169e1", "8b4513", "fa8072", "f4a460", "2e8b57", "fff5ee", "a0522d", "c0c0c0", "87ceeb", "6a5acd", "708090", "708090", "fffafa", "00ff7f", "4682b4", "d2b48c", "008080", "d8bfd8", "ff6347", "40e0d0", "ee82ee", "f5deb3", "ffffff", "f5f5f5", "ffff00", "9acd32" };
  745. var findIndx = colorName.IndexOf(name);
  746. if (findIndx != -1)
  747. {
  748. hex = colorHex[findIndx];
  749. }
  750. return hex;
  751. }
  752. public static string GetFillType(XmlNode node)
  753. {
  754. //Need to test/////////////////////////////////////////////
  755. //SOLID_FILL
  756. //PIC_FILL
  757. //GRADIENT_FILL
  758. //PATTERN_FILL
  759. //NO_FILL
  760. var fillType = "";
  761. if (node.GetTextByPath("a:noFill") != null)
  762. {
  763. fillType = "NO_FILL";
  764. }
  765. if (node.GetTextByPath("a:solidFill") != null)
  766. {
  767. fillType = "SOLID_FILL";
  768. }
  769. if (node.GetTextByPath("a:gradFill") != null)
  770. {
  771. fillType = "GRADIENT_FILL";
  772. }
  773. if (node.GetTextByPath("a:pattFill") != null)
  774. {
  775. fillType = "PATTERN_FILL";
  776. }
  777. if (node.GetTextByPath("a:blipFill") != null)
  778. {
  779. fillType = "PIC_FILL";
  780. }
  781. if (fillType == "")
  782. {
  783. var clrName = node.GetTextByPath("p:style/a:fillRef");
  784. if (clrName != null)
  785. {
  786. fillType = "SOLID_FILL";
  787. }
  788. }
  789. return fillType;
  790. }
  791. }
  792. }