TextMarkup.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "TextMarkup.h"
  9. #include <AzCore/std/containers/stack.h>
  10. #include <AzCore/std/string/conversions.h>
  11. #include <AzCore/std/string/regex.h>
  12. #include <CryPath.h>
  13. namespace
  14. {
  15. //! Takes an input source string and wraps it for XML parsing
  16. void InsertMarkup(const AZStd::string& sourceBuffer, AZStd::string& targetBuffer)
  17. {
  18. targetBuffer = "<root>" + sourceBuffer + "</root>";
  19. AZStd::string::size_type pos = targetBuffer.find(">");
  20. while (pos != AZStd::string::npos)
  21. {
  22. ++pos;
  23. if (pos < targetBuffer.length())
  24. {
  25. AZStd::string::value_type nextChar = targetBuffer.at(pos);
  26. if (nextChar != '<')
  27. {
  28. static const AZStd::string CharStartTag("<ch value=\"");
  29. targetBuffer.insert(pos, CharStartTag);
  30. pos += CharStartTag.length();
  31. pos = targetBuffer.find("<", pos);
  32. if (AZStd::string::npos != pos)
  33. {
  34. static const AZStd::string CharEndTag("\" />");
  35. targetBuffer.insert(pos, CharEndTag);
  36. pos += CharEndTag.length();
  37. }
  38. }
  39. }
  40. pos = targetBuffer.find(">", pos);
  41. }
  42. // Newlines need to be escaped or the XML parser could toss them out
  43. targetBuffer = AZStd::regex_replace(targetBuffer, AZStd::regex("\n"), "\\n");
  44. }
  45. //! Takes an TextMarkup::Tag and dumps all character data to the given output string buffer.
  46. void DumpCharData(const TextMarkup::Tag& markupRootTag, AZStd::string& ouputText)
  47. {
  48. AZStd::stack<const TextMarkup::Tag*> tagStack;
  49. tagStack.push(&markupRootTag);
  50. while (!tagStack.empty())
  51. {
  52. const TextMarkup::Tag* curTag = tagStack.top();
  53. tagStack.pop();
  54. auto riter = curTag->children.rbegin();
  55. for (; riter != curTag->children.rend(); ++riter)
  56. {
  57. tagStack.push(*riter);
  58. }
  59. TextMarkup::TagType type = curTag->GetType();
  60. if (type == TextMarkup::TagType::Text)
  61. {
  62. const TextMarkup::TextTag* pText = static_cast<const TextMarkup::TextTag*>(curTag);
  63. ouputText += pText->text;
  64. }
  65. }
  66. }
  67. //! Serializes a given XML root object to an TextMarkup tag tree.
  68. bool PopulateTagTreeFromXml(const XmlNodeRef& node, TextMarkup::Tag& markupTag)
  69. {
  70. if (!node)
  71. {
  72. return false;
  73. }
  74. TextMarkup::Tag* newTag = &markupTag;
  75. if (AZStd::string(node->getTag()) == "b")
  76. {
  77. newTag = new TextMarkup::BoldTag();
  78. }
  79. else if (AZStd::string(node->getTag()) == "i")
  80. {
  81. newTag = new TextMarkup::ItalicTag();
  82. }
  83. else if (AZStd::string(node->getTag()) == "a")
  84. {
  85. const int numAttributes = node->getNumAttributes();
  86. if (numAttributes < 1)
  87. {
  88. // Expecting at least one attribute
  89. return false;
  90. }
  91. AZStd::string action;
  92. AZStd::string data;
  93. for (int i = 0, count = numAttributes; i < count; ++i)
  94. {
  95. const char* key = "";
  96. const char* value = "";
  97. if (node->getAttributeByIndex(i, &key, &value))
  98. {
  99. if (AZStd::string(key) == "action")
  100. {
  101. action = value;
  102. }
  103. else if (AZStd::string(key) == "data")
  104. {
  105. data = value;
  106. }
  107. else
  108. {
  109. // Unexpected font tag attribute
  110. return false;
  111. }
  112. }
  113. }
  114. TextMarkup::AnchorTag* anchorTag = new TextMarkup::AnchorTag();
  115. newTag = anchorTag;
  116. anchorTag->action = action;
  117. anchorTag->data = data;
  118. }
  119. else if (AZStd::string(node->getTag()) == "font")
  120. {
  121. const int numAttributes = node->getNumAttributes();
  122. if (numAttributes <= 0)
  123. {
  124. // Expecting at least one attribute
  125. return false;
  126. }
  127. AZStd::string face;
  128. AZ::Vector3 color(TextMarkup::ColorInvalid);
  129. for (int i = 0, count = node->getNumAttributes(); i < count; ++i)
  130. {
  131. const char* key = "";
  132. const char* value = "";
  133. if (node->getAttributeByIndex(i, &key, &value))
  134. {
  135. if (AZStd::string(key) == "face")
  136. {
  137. face = value;
  138. }
  139. else if (AZStd::string(key) == "color")
  140. {
  141. AZStd::string colorValue(value);
  142. AZ::StringFunc::TrimWhiteSpace(colorValue, true, true);
  143. AZStd::string::size_type ExpectedNumChars = 7;
  144. if (ExpectedNumChars == colorValue.size() && '#' == colorValue.at(0))
  145. {
  146. static const int base16 = 16;
  147. static const float normalizeRgbMultiplier = 1.0f / 255.0f;
  148. color.SetX(static_cast<float>(AZStd::stoi(colorValue.substr(1, 2), 0, base16)) * normalizeRgbMultiplier);
  149. color.SetY(static_cast<float>(AZStd::stoi(colorValue.substr(3, 2), 0, base16)) * normalizeRgbMultiplier);
  150. color.SetZ(static_cast<float>(AZStd::stoi(colorValue.substr(5, 2), 0, base16)) * normalizeRgbMultiplier);
  151. }
  152. }
  153. else
  154. {
  155. // Unexpected font tag attribute
  156. return false;
  157. }
  158. }
  159. }
  160. TextMarkup::FontTag* fontTag = new TextMarkup::FontTag();
  161. newTag = fontTag;
  162. fontTag->face = face;
  163. fontTag->color = color;
  164. }
  165. else if (AZStd::string(node->getTag()) == "img")
  166. {
  167. const int numAttributes = node->getNumAttributes();
  168. if (numAttributes <= 0)
  169. {
  170. // Expecting at least one attribute
  171. return false;
  172. }
  173. AZStd::string imagePathname;
  174. AZStd::string height;
  175. float scale = 1.0f;
  176. AZStd::string vAlign;
  177. float yOffset = 0.0f;
  178. float leftPadding = 0.0f;
  179. float rightPadding = 0.0f;
  180. for (int i = 0, count = node->getNumAttributes(); i < count; ++i)
  181. {
  182. const char* key = "";
  183. const char* value = "";
  184. if (node->getAttributeByIndex(i, &key, &value))
  185. {
  186. if (AZStd::string(key) == "src")
  187. {
  188. imagePathname = value;
  189. }
  190. else if (AZStd::string(key) == "height")
  191. {
  192. height = value;
  193. }
  194. else if (AZStd::string(key) == "scale")
  195. {
  196. scale = AZStd::stof(AZStd::string(value));
  197. }
  198. else if (AZStd::string(key) == "vAlign")
  199. {
  200. vAlign = value;
  201. }
  202. else if (AZStd::string(key) == "yOffset")
  203. {
  204. yOffset = AZStd::stof(AZStd::string(value));
  205. }
  206. else if (AZStd::string(key) == "xPadding")
  207. {
  208. leftPadding = AZStd::stof(AZStd::string(value));
  209. rightPadding = leftPadding;
  210. }
  211. else if (AZStd::string(key) == "lPadding")
  212. {
  213. leftPadding = AZStd::stof(AZStd::string(value));
  214. }
  215. else if (AZStd::string(key) == "rPadding")
  216. {
  217. rightPadding = AZStd::stof(AZStd::string(value));
  218. }
  219. else
  220. {
  221. // Unexpected font tag attribute
  222. return false;
  223. }
  224. }
  225. }
  226. if (imagePathname.empty())
  227. {
  228. // Need at least a path to a texture
  229. return false;
  230. }
  231. TextMarkup::ImageTag* imageTag = new TextMarkup::ImageTag();
  232. newTag = imageTag;
  233. // Add an extension if it's not there
  234. const char* extension = PathUtil::GetExt(imagePathname.c_str());
  235. if (!extension || extension[0] == '\0')
  236. {
  237. // Empty path - add the texture extension
  238. const AZStd::string textureExtension(".dds");
  239. imagePathname += textureExtension;
  240. }
  241. imageTag->m_imagePathname = imagePathname;
  242. imageTag->m_height = height;
  243. imageTag->m_scale = scale;
  244. imageTag->m_vAlign = vAlign;
  245. imageTag->m_yOffset = yOffset;
  246. imageTag->m_leftPadding = leftPadding;
  247. imageTag->m_rightPadding = rightPadding;
  248. }
  249. else if (AZStd::string(node->getTag()) == "ch")
  250. {
  251. AZStd::string nodeContent;
  252. const char* key = "";
  253. const char* value = "";
  254. if (!node->getAttributeByIndex(0, &key, &value))
  255. {
  256. return false;
  257. }
  258. if (AZStd::string(key) == "value")
  259. {
  260. nodeContent = value;
  261. }
  262. else
  263. {
  264. // Unexpected attribute
  265. return false;
  266. }
  267. TextMarkup::TextTag* textTag = new TextMarkup::TextTag();
  268. newTag = textTag;
  269. textTag->text = nodeContent;
  270. }
  271. else if (AZStd::string(node->getTag()) == "root")
  272. {
  273. // Ignore
  274. }
  275. else
  276. {
  277. return false;
  278. }
  279. // Guard against a tag adding itself as its own child
  280. if (newTag != &markupTag)
  281. {
  282. markupTag.children.push_back(newTag);
  283. }
  284. for (int i = 0, count = node->getChildCount(); i < count; ++i)
  285. {
  286. XmlNodeRef child = node->getChild(i);
  287. if (!PopulateTagTreeFromXml(child, *newTag))
  288. {
  289. return false;
  290. }
  291. }
  292. return true;
  293. }
  294. }
  295. ////////////////////////////////////////////////////////////////////////////////////////////////////
  296. bool TextMarkup::ParseMarkupBuffer(const AZStd::string& sourceBuffer, TextMarkup::Tag& markupTag, bool suppressWarnings)
  297. {
  298. // First, wrap up the source text to make it parseable XML
  299. AZStd::string wrappedSourceText(sourceBuffer);
  300. InsertMarkup(sourceBuffer, wrappedSourceText);
  301. // Parse the wrapped text as XML
  302. XmlNodeRef xmlRoot = GetISystem()->LoadXmlFromBuffer(wrappedSourceText.c_str(), wrappedSourceText.length(), false, suppressWarnings);
  303. if (xmlRoot)
  304. {
  305. return PopulateTagTreeFromXml(xmlRoot, markupTag);
  306. }
  307. return false;
  308. }
  309. ////////////////////////////////////////////////////////////////////////////////////////////////////
  310. void TextMarkup::CopyCharData(const AZStd::string& sourceBuffer, AZStd::string& targetBuffer)
  311. {
  312. TextMarkup::Tag markupRootTag;
  313. if (ParseMarkupBuffer(sourceBuffer, markupRootTag))
  314. {
  315. DumpCharData(markupRootTag, targetBuffer);
  316. }
  317. if (targetBuffer.empty())
  318. {
  319. // If, for some reason we couldn't parse the text as XML, we simply
  320. // assign the source buffer
  321. targetBuffer = sourceBuffer;
  322. }
  323. }
  324. #include "Tests/internal/test_TextMarkup.cpp"