TextObject.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. using System.Threading.Tasks;
  8. namespace ConsoleApplication
  9. {
  10. public class TextObject : SceneObjectDecorator
  11. {
  12. private string _align, _antiAlias, _font, _autosize, _color;
  13. private List<TextStyle> _styleList;
  14. private List<TextFragment> _fragmentsList;
  15. private int _leading, _letterSpacing, _size;
  16. private Boolean _bold, _italic, _underline, _selectable, _runningText, _useScroller;
  17. private XmlDocument _doc;
  18. private string[] _textObjectPropertiesAttributes = new string[14]{ "font", "align", "color", "italic", "bold", "underline", "size", "runningText",
  19. "autosize", "leading", "letterSpacing", "antiAlias", "useScroller", "selectable"};
  20. private SimpleSceneObject _simpleSceneObject;
  21. public TextObject(SimpleSceneObject sceneobject) : base(sceneobject)
  22. {
  23. _simpleSceneObject = sceneobject;
  24. _align = "left";
  25. _antiAlias = "normal";
  26. _font = "Arial";
  27. _autosize = "none";
  28. _color = "000000";
  29. _leading = 0;
  30. _letterSpacing = 0;
  31. _size = 20;
  32. _bold = false;
  33. _italic = false;
  34. _underline = false;
  35. _selectable = false;
  36. _runningText = true;
  37. _useScroller = false;
  38. string objectType = "com.customObjects.TextObject";
  39. _styleList = new List<TextStyle>();
  40. _fragmentsList = new List<TextFragment>();
  41. setProperties(new Properties(true, false, true, true, true, true, true, true,
  42. false, true, true, true, true, true, true));
  43. sceneobject.setObjectType(objectType);
  44. }
  45. public override object Clone()
  46. {
  47. SimpleSceneObject simpleSceneObject = _simpleSceneObject;
  48. TextObject textObject = new TextObject(simpleSceneObject);
  49. textObject.Align = _align;
  50. textObject.AntiAlias = _antiAlias;
  51. textObject.Font = _font;
  52. textObject.Autosize = _autosize;
  53. textObject.Color = _color;
  54. textObject.Leading = _leading;
  55. textObject.LetterSpacing = _letterSpacing;
  56. textObject.Size = _size;
  57. textObject.Bold = _bold;
  58. textObject.Italic = _italic;
  59. textObject.Underline = _underline;
  60. textObject.Selectable = _selectable;
  61. textObject.RunningText = _runningText;
  62. textObject.UseScroller = _useScroller;
  63. string objectType = "com.customObjects.TextObject";
  64. _styleList = new List<TextStyle>();
  65. _fragmentsList = new List<TextFragment>();
  66. setProperties(new Properties(true, false, true, true, true, true, true, true,
  67. false, true, true, true, true, true, true));
  68. textObject.setObjectType(objectType);
  69. return textObject;
  70. }
  71. public TextObject setClipWidth(int clipWidth)
  72. {
  73. _simpleSceneObject.ClipWidth = clipWidth;
  74. return (TextObject)Clone();
  75. }
  76. public void addToStyleList(TextStyle textStyle)
  77. {
  78. bool isEqual = false;
  79. if (_styleList.Count == 0)
  80. _styleList.Add(textStyle);
  81. else
  82. {
  83. foreach (TextStyle item in _styleList)
  84. if (textStyle.isEqual(item))
  85. {
  86. isEqual = true;
  87. break;
  88. }
  89. if (!isEqual)
  90. _styleList.Add(textStyle);
  91. }
  92. }
  93. public XmlElement getTextObjectPropertiesNode()
  94. {
  95. XmlElement textObjectPropNode = getXMLDocumentRoot().CreateElement("textObjectProperties");
  96. //XmlElement stylesNode = getStylesNode();
  97. //XmlElement fragmentNode = getFragmentsNode();
  98. //XmlElement textNode = getTextNode();
  99. //textObjectPropNode.AppendChild(textNode);
  100. XmlElement textNode = getXMLDocumentRoot().CreateElement("text");
  101. textNode.InnerText = getHTML();
  102. textObjectPropNode.AppendChild(textNode);
  103. _doc = base.getXMLDocumentRoot();
  104. foreach (string s in _textObjectPropertiesAttributes)
  105. {
  106. XmlAttribute xmlAttr = _doc.CreateAttribute(s);
  107. FieldInfo fieldInfo = GetType().GetField("_" + s, BindingFlags.NonPublic | BindingFlags.Instance);
  108. if (fieldInfo!=null)
  109. xmlAttr.Value = fieldInfo.GetValue(this).ToString().ToLower();
  110. textObjectPropNode.Attributes.Append(xmlAttr);
  111. }
  112. //textObjectPropNode.AppendChild(stylesNode);
  113. //textObjectPropNode.AppendChild(fragmentNode);
  114. return textObjectPropNode;
  115. }
  116. public XmlElement getPropertiesNode()
  117. {
  118. string properties = getProperties().toString();
  119. XmlElement prop = getXMLDocumentRoot().CreateElement("properties");
  120. prop.InnerText = properties;
  121. return prop;
  122. }
  123. public string getHTML()
  124. {
  125. string HTML = "";
  126. HTML += "<TEXTFORMAT LEFTMARGIN=\"1\" RIGHTMARGIN=\"2\">";
  127. TextStyle newStyle = new TextStyle(), oldStyle = new TextStyle();
  128. bool bold = false, underline = false, italic = false;
  129. int fontCount = 0;
  130. foreach (TextFragment textFragment in _fragmentsList)
  131. {
  132. if (textFragment.NewParagraph)
  133. HTML += "<br>";
  134. newStyle = StyleList[textFragment.StyleId];
  135. //First fragment
  136. if (_fragmentsList.IndexOf(textFragment) == 0)
  137. {
  138. HTML += "<P ALIGN=\"" + newStyle.Alignment + "\">";
  139. fontCount++;
  140. HTML += "<FONT FACE=\"" + newStyle.Font + "\" SIZE=\"" + newStyle.FontSize + "\" COLOR=\"#" + newStyle.FontColor + "\" LETTERSPACING=\"0\" KERNING=\"1\">";
  141. if (newStyle.Bold)
  142. {
  143. HTML += "<B>";
  144. bold = true;
  145. }
  146. if (newStyle.Underline)
  147. {
  148. HTML += "<U>";
  149. underline = true;
  150. }
  151. if (newStyle.Italic)
  152. {
  153. HTML += "<I>";
  154. italic = true;
  155. }
  156. for (int i = 0; i < textFragment.Level; i++)
  157. HTML += "\t";
  158. HTML += textFragment.Text.Replace("<", "&#60;").Replace(">", "&#62;");
  159. oldStyle = newStyle;
  160. continue;
  161. }
  162. if (oldStyle != newStyle)
  163. {
  164. if (oldStyle.Font != newStyle.Font ||
  165. oldStyle.FontColor != newStyle.FontColor ||
  166. oldStyle.FontSize != newStyle.FontSize ||
  167. oldStyle.Alignment != newStyle.Alignment)
  168. {
  169. if (oldStyle.Alignment != newStyle.Alignment)
  170. {
  171. for (int i = 0; i < fontCount; i++)
  172. HTML += "</FONT>";
  173. HTML += "</P>";
  174. HTML += "</TEXTFORMAT>";
  175. fontCount = 0;
  176. HTML += "<P ALIGN=\"" + newStyle.Alignment + "\">";
  177. fontCount++;
  178. HTML += "<FONT FACE=\"" + newStyle.Font + "\" SIZE=\"" + newStyle.FontSize + "\" COLOR=\"#" + newStyle.FontColor + "\" LETTERSPACING=\"0\" KERNING=\"1\">";
  179. }
  180. HTML += (bold) ? "</B>" : "";
  181. HTML += (underline) ? "</U>" : "";
  182. HTML += (italic) ? "</I>" : "";
  183. bold = false;
  184. underline = false;
  185. italic = false;
  186. fontCount++;
  187. HTML += "<FONT ";
  188. if (oldStyle.Font != newStyle.Font)
  189. HTML += "FACE=\"" + newStyle.Font + "\" ";
  190. if (oldStyle.FontSize != newStyle.FontSize)
  191. HTML += "SIZE=\"" + newStyle.FontSize + "\" ";
  192. if (oldStyle.FontColor != newStyle.FontColor)
  193. HTML += "COLOR=\"#" + newStyle.FontColor + "\" ";
  194. HTML += ">";
  195. if (newStyle.Bold)
  196. {
  197. HTML += "<B>";
  198. bold = true;
  199. }
  200. if (newStyle.Underline)
  201. {
  202. HTML += "<U>";
  203. underline = true;
  204. }
  205. if (newStyle.Italic)
  206. {
  207. HTML += "<I>";
  208. italic = true;
  209. }
  210. for (int i = 0; i < textFragment.Level; i++)
  211. HTML += "\t";
  212. HTML += textFragment.Text.Replace("<", "&#60;").Replace(">", "&#62;");
  213. }
  214. else
  215. {
  216. if (newStyle.Bold != bold)
  217. {
  218. HTML += (newStyle.Bold) ? "<B>" : "</B>";
  219. bold = (newStyle.Bold) ? true : false;
  220. }
  221. if (newStyle.Underline != underline)
  222. {
  223. HTML += (newStyle.Underline) ? "<U>" : "</U>";
  224. underline = (newStyle.Underline) ? true : false;
  225. }
  226. if (newStyle.Italic != italic)
  227. {
  228. HTML += (newStyle.Italic) ? "<I>" : "</I>";
  229. italic = (newStyle.Italic) ? true : false;
  230. }
  231. for (int i = 0; i < textFragment.Level; i++)
  232. HTML += "\t";
  233. HTML += textFragment.Text.Replace("<", "&#60;").Replace(">", "&#62;");
  234. }
  235. }
  236. else
  237. {
  238. for (int i = 0; i < textFragment.Level; i++)
  239. HTML += "\t";
  240. HTML += textFragment.Text.Replace("<", "&#60;").Replace(">", "&#62;");
  241. }
  242. for (int i = 0; i < textFragment.Breaks; i++)
  243. HTML += "<br>";
  244. oldStyle = newStyle;
  245. }
  246. HTML += (bold) ? "</B>" : "";
  247. HTML += (underline) ? "</U>" : "";
  248. HTML += (italic) ? "</I>" : "";
  249. for (int i = 0; i < fontCount; i++)
  250. HTML += "</FONT>";
  251. HTML += "</P>";
  252. HTML += "</TEXTFORMAT>";
  253. return HTML;
  254. }
  255. public XmlElement getTextNode()
  256. {
  257. XmlElement textNode = getXMLDocumentRoot().CreateElement("text");
  258. XmlElement textFormatNode = getXMLDocumentRoot().CreateElement("TEXTFORMAT");
  259. XmlElement pNode = getPnode();
  260. textFormatNode.AppendChild(pNode);
  261. textNode.AppendChild(textFormatNode);
  262. return textNode;
  263. }
  264. public XmlElement getPnode()
  265. {
  266. XmlElement pNode = getXMLDocumentRoot().CreateElement("P");
  267. List<XmlElement> fontList = new List<XmlElement>();
  268. TextStyle old_style = new TextStyle();
  269. for (int i = 0; i < _fragmentsList.Count; i++)
  270. {
  271. XmlElement temp = getXMLDocumentRoot().CreateElement("FONT");
  272. TextStyle style = _styleList[_fragmentsList[i].StyleId];
  273. if (i == 0)
  274. {
  275. XmlAttribute font = getXMLDocumentRoot().CreateAttribute("FACE");
  276. font.Value = style.Font;
  277. XmlAttribute size = getXMLDocumentRoot().CreateAttribute("SIZE");
  278. size.Value = style.FontSize.ToString();
  279. XmlAttribute color = getXMLDocumentRoot().CreateAttribute("COLOR");
  280. color.Value = style.FontColor.ToString();
  281. temp.Attributes.Append(font);
  282. temp.Attributes.Append(size);
  283. temp.Attributes.Append(color);
  284. style.getTextNode(_fragmentsList[i].Text);
  285. temp.InnerText = _fragmentsList[i].Text;
  286. fontList.Add(temp);
  287. //pNode.AppendChild(temp);
  288. old_style = style;
  289. continue;
  290. }
  291. if (_fragmentsList[i].StyleId != _fragmentsList[i - 1].StyleId)
  292. {
  293. string BIU = style.getTextNode(_fragmentsList[i].Text);
  294. if (old_style.FontColor != style.FontColor || !old_style.FontSize.Equals(style.FontSize) || !old_style.Font.Equals(style.Font))
  295. {
  296. if (!old_style.Font.Equals(style.Font))
  297. {
  298. XmlAttribute font = getXMLDocumentRoot().CreateAttribute("FACE");
  299. font.Value = style.Font;
  300. temp.Attributes.Append(font);
  301. }
  302. if (old_style.FontSize != style.FontSize)
  303. {
  304. XmlAttribute fontSize = getXMLDocumentRoot().CreateAttribute("SIZE");
  305. fontSize.Value = style.FontSize.ToString();
  306. temp.Attributes.Append(fontSize);
  307. }
  308. if (old_style.FontColor != style.FontColor)
  309. {
  310. XmlAttribute color = getXMLDocumentRoot().CreateAttribute("COLOR");
  311. color.Value = style.FontColor.ToString();
  312. temp.Attributes.Append(color);
  313. }
  314. temp.InnerText = BIU;
  315. fontList.Add(temp);
  316. }
  317. else
  318. {
  319. fontList[i - 1].InnerText += BIU;
  320. }
  321. }
  322. old_style = style;
  323. }
  324. /*fontList.Reverse();
  325. XmlElement fontRoot = getXMLDocumentRoot().CreateElement("TEST");
  326. XmlElement oldItem = new XmlElement();
  327. int counter = 0;
  328. foreach (XmlElement item in fontList)
  329. {
  330. XmlElement temp = item;
  331. if (counter == 0)
  332. {
  333. fontRoot = temp;
  334. temp.AppendChild(oldItem);
  335. oldItem = temp;
  336. continue;
  337. }
  338. else
  339. {
  340. temp.AppendChild(oldItem);
  341. fontRoot = temp;
  342. oldItem = temp;
  343. }
  344. }*/
  345. //pNode.AppendChild(fontRoot);
  346. return pNode;
  347. }
  348. public XmlElement getFragmentsNode()
  349. {
  350. XmlElement fragments = getXMLDocumentRoot().CreateElement("fragments");
  351. foreach (TextFragment tFragment in _fragmentsList)
  352. {
  353. XmlElement f = tFragment.getFragmentChild();
  354. fragments.AppendChild(f);
  355. }
  356. return fragments;
  357. }
  358. public XmlElement getStylesNode()
  359. {
  360. XmlElement styles = getXMLDocumentRoot().CreateElement("styles");
  361. foreach (TextStyle tStyle in _styleList)
  362. {
  363. XmlElement s = tStyle.getStylesChild();
  364. styles.AppendChild(s);
  365. }
  366. return styles;
  367. }
  368. public override XmlElement getXMLTree()
  369. {
  370. XmlElement parent = base.getXMLTree();
  371. XmlElement properties = getPropertiesNode();
  372. XmlElement acce = getXMLDocumentRoot().CreateElement("accessors");
  373. XmlElement textObjectProps = getTextObjectPropertiesNode();
  374. acce.AppendChild(textObjectProps);
  375. parent.AppendChild(properties);
  376. parent.AppendChild(acce);
  377. return parent;
  378. }
  379. public override XmlDocument getXMLDocumentRoot()
  380. {
  381. return base.getXMLDocumentRoot();
  382. }
  383. public override void setXMLDocumentRoot(ref XmlDocument xmldocument)
  384. {
  385. base.setXMLDocumentRoot(ref xmldocument);
  386. }
  387. public override Properties getProperties()
  388. {
  389. return base.getProperties();
  390. }
  391. public override void setProperties(Properties properties)
  392. {
  393. base.setProperties(properties);
  394. }
  395. public override void ConvertToYoobaUnits(int width, int height)
  396. {
  397. base.ConvertToYoobaUnits(width, height);
  398. //FONT, SIZE, COLOR, ALIGNMENT
  399. //Font size convertion
  400. _size /= 100;
  401. //_size = (int)Math.Round(_size * 1.5);
  402. //Font color
  403. _color = getFontColorAsInteger(_color).ToString();
  404. foreach(TextStyle style in StyleList)
  405. {
  406. //Font size convertion
  407. style.FontSize /= 100;
  408. //style.FontSize = (int) Math.Round(style.FontSize*1.5);
  409. //Fake font and color
  410. style.Font = "Arial";
  411. }
  412. //Alignment
  413. _align = (_align.ToLower() == "l" || _align.ToLower() == "left") ? "left" : _align;
  414. _align = (_align.ToLower() == "r" || _align.ToLower() == "right") ? "right" : _align;
  415. _align = (_align.ToLower() == "c" || _align.ToLower() == "ctr" || _align.ToLower() == "center") ? "center" : _align;
  416. }
  417. public int getFontColorAsInteger(string color)
  418. {
  419. if(color.Length == 6)
  420. return int.Parse(color, System.Globalization.NumberStyles.HexNumber);
  421. else
  422. {
  423. Console.WriteLine("Error in color convertion, '" + color + "' could not be converted!");
  424. return 0;
  425. }
  426. }
  427. public string Align
  428. {
  429. get { return _align; }
  430. set { _align = value; }
  431. }
  432. internal List<TextStyle> StyleList
  433. {
  434. get { return _styleList; }
  435. set { _styleList = value; }
  436. }
  437. internal List<TextFragment> FragmentsList
  438. {
  439. get { return _fragmentsList; }
  440. set { _fragmentsList = value; }
  441. }
  442. public string Autosize
  443. {
  444. get { return _autosize; }
  445. set { _autosize = value; }
  446. }
  447. public string Font
  448. {
  449. get { return _font; }
  450. set { _font = value; }
  451. }
  452. public string AntiAlias
  453. {
  454. get { return _antiAlias; }
  455. set { _antiAlias = value; }
  456. }
  457. public int Size
  458. {
  459. get { return _size; }
  460. set { _size = value; }
  461. }
  462. public int LetterSpacing
  463. {
  464. get { return _letterSpacing; }
  465. set { _letterSpacing = value; }
  466. }
  467. public int Leading
  468. {
  469. get { return _leading; }
  470. set { _leading = value; }
  471. }
  472. public string Color
  473. {
  474. get { return _color; }
  475. set { _color = value; }
  476. }
  477. public Boolean RunningText
  478. {
  479. get { return _runningText; }
  480. set { _runningText = value; }
  481. }
  482. public Boolean Selectable
  483. {
  484. get { return _selectable; }
  485. set { _selectable = value; }
  486. }
  487. public Boolean Underline
  488. {
  489. get { return _underline; }
  490. set { _underline = value; }
  491. }
  492. public Boolean Italic
  493. {
  494. get { return _italic; }
  495. set { _italic = value; }
  496. }
  497. public Boolean Bold
  498. {
  499. get { return _bold; }
  500. set { _bold = value; }
  501. }
  502. public Boolean UseScroller
  503. {
  504. get { return _useScroller; }
  505. set { _useScroller = value; }
  506. }
  507. internal void setAttributes(TableStyle tableStyle)
  508. {
  509. if (tableStyle == null)
  510. return;
  511. Color = (tableStyle.FontColor != "") ? tableStyle.FontColor : Color;
  512. Size = (tableStyle.FontSize != 0) ? tableStyle.FontSize : Size;
  513. Bold = tableStyle.Bold;
  514. Italic = tableStyle.Italic;
  515. Underline = tableStyle.Underline;
  516. }
  517. }
  518. }