Factory.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "../../Include/RmlUi/Core.h"
  30. #include "../../Include/RmlUi/Core/StreamMemory.h"
  31. #include "ContextInstancerDefault.h"
  32. #include "DecoratorTiledBoxInstancer.h"
  33. #include "DecoratorTiledHorizontalInstancer.h"
  34. #include "DecoratorTiledImageInstancer.h"
  35. #include "DecoratorTiledVerticalInstancer.h"
  36. #include "DecoratorNinePatch.h"
  37. #include "ElementHandle.h"
  38. #include "ElementImage.h"
  39. #include "ElementTextDefault.h"
  40. #include "EventInstancerDefault.h"
  41. #include "FontEffectOutlineInstancer.h"
  42. #include "FontEffectShadowInstancer.h"
  43. #include "PluginRegistry.h"
  44. #include "PropertyParserColour.h"
  45. #include "StreamFile.h"
  46. #include "StyleSheetFactory.h"
  47. #include "TemplateCache.h"
  48. #include "XMLNodeHandlerBody.h"
  49. #include "XMLNodeHandlerDefault.h"
  50. #include "XMLNodeHandlerHead.h"
  51. #include "XMLNodeHandlerTemplate.h"
  52. #include "XMLParseTools.h"
  53. namespace Rml {
  54. namespace Core {
  55. // Element instancers.
  56. typedef UnorderedMap< String, ElementInstancer* > ElementInstancerMap;
  57. static ElementInstancerMap element_instancers;
  58. // Decorator instancers.
  59. typedef UnorderedMap< String, DecoratorInstancer* > DecoratorInstancerMap;
  60. static DecoratorInstancerMap decorator_instancers;
  61. // Font effect instancers.
  62. typedef UnorderedMap< String, FontEffectInstancer* > FontEffectInstancerMap;
  63. static FontEffectInstancerMap font_effect_instancers;
  64. // The context instancer.
  65. static ContextInstancer* context_instancer = nullptr;;
  66. // The event instancer
  67. static EventInstancer* event_instancer = nullptr;;
  68. // Event listener instancer.
  69. static EventListenerInstancer* event_listener_instancer = nullptr;
  70. // Default instancers are constructed and destroyed on Initialise and Shutdown, respectively.
  71. struct DefaultInstancers {
  72. template<typename T> using Ptr = UniquePtr<T>;
  73. Ptr<ContextInstancer> context_default;
  74. Ptr<EventInstancer> event_default;
  75. Ptr<ElementInstancer> element_default = std::make_unique<ElementInstancerElement>();
  76. Ptr<ElementInstancer> element_text_default = std::make_unique<ElementInstancerTextDefault>();
  77. Ptr<ElementInstancer> element_img = std::make_unique<ElementInstancerGeneric<ElementImage>>();
  78. Ptr<ElementInstancer> element_handle = std::make_unique<ElementInstancerGeneric<ElementHandle>>();
  79. Ptr<ElementInstancer> element_body = std::make_unique<ElementInstancerGeneric<ElementDocument>>();
  80. Ptr<DecoratorInstancer> decorator_tiled_horizontal = std::make_unique<DecoratorTiledHorizontalInstancer>();
  81. Ptr<DecoratorInstancer> decorator_tiled_vertical = std::make_unique<DecoratorTiledVerticalInstancer>();
  82. Ptr<DecoratorInstancer> decorator_tiled_box = std::make_unique<DecoratorTiledBoxInstancer>();
  83. Ptr<DecoratorInstancer> decorator_image = std::make_unique<DecoratorTiledImageInstancer>();
  84. Ptr<DecoratorInstancer> decorator_ninepatch = std::make_unique<DecoratorNinePatchInstancer>();
  85. Ptr<FontEffectInstancer> font_effect_shadow = std::make_unique<FontEffectShadowInstancer>();
  86. Ptr<FontEffectInstancer> font_effect_outline = std::make_unique<FontEffectOutlineInstancer>();
  87. };
  88. static UniquePtr<DefaultInstancers> default_instancers;
  89. Factory::Factory()
  90. {
  91. }
  92. Factory::~Factory()
  93. {
  94. }
  95. bool Factory::Initialise()
  96. {
  97. default_instancers = std::make_unique<DefaultInstancers>();
  98. // Bind the default context instancer.
  99. if (!context_instancer)
  100. {
  101. default_instancers->context_default = std::make_unique<ContextInstancerDefault>();
  102. context_instancer = default_instancers->context_default.get();
  103. }
  104. // Bind default event instancer
  105. if (!event_instancer)
  106. {
  107. default_instancers->event_default = std::make_unique<EventInstancerDefault>();
  108. event_instancer = default_instancers->event_default.get();
  109. }
  110. // No default event listener instancer
  111. if (!event_listener_instancer)
  112. event_listener_instancer = nullptr;
  113. // Bind the default element instancers
  114. RegisterElementInstancer("*", default_instancers->element_default.get());
  115. RegisterElementInstancer("img", default_instancers->element_img.get());
  116. RegisterElementInstancer("#text", default_instancers->element_text_default.get());
  117. RegisterElementInstancer("handle", default_instancers->element_handle.get());
  118. RegisterElementInstancer("body", default_instancers->element_body.get());
  119. // Bind the default decorator instancers
  120. RegisterDecoratorInstancer("tiled-horizontal", default_instancers->decorator_tiled_horizontal.get());
  121. RegisterDecoratorInstancer("tiled-vertical", default_instancers->decorator_tiled_vertical.get());
  122. RegisterDecoratorInstancer("tiled-box", default_instancers->decorator_tiled_box.get());
  123. RegisterDecoratorInstancer("image", default_instancers->decorator_image.get());
  124. RegisterDecoratorInstancer("ninepatch", default_instancers->decorator_ninepatch.get());
  125. RegisterFontEffectInstancer("shadow", default_instancers->font_effect_shadow.get());
  126. RegisterFontEffectInstancer("outline", default_instancers->font_effect_outline.get());
  127. // Register the core XML node handlers.
  128. XMLParser::RegisterNodeHandler("", std::make_shared<XMLNodeHandlerDefault>());
  129. XMLParser::RegisterNodeHandler("body", std::make_shared<XMLNodeHandlerBody>());
  130. XMLParser::RegisterNodeHandler("head", std::make_shared<XMLNodeHandlerHead>());
  131. XMLParser::RegisterNodeHandler("template", std::make_shared<XMLNodeHandlerTemplate>());
  132. return true;
  133. }
  134. void Factory::Shutdown()
  135. {
  136. element_instancers.clear();
  137. decorator_instancers.clear();
  138. font_effect_instancers.clear();
  139. context_instancer = nullptr;
  140. event_listener_instancer = nullptr;
  141. event_instancer = nullptr;
  142. XMLParser::ReleaseHandlers();
  143. default_instancers.reset();
  144. }
  145. // Registers the instancer to use when instancing contexts.
  146. void Factory::RegisterContextInstancer(ContextInstancer* instancer)
  147. {
  148. context_instancer = instancer;
  149. }
  150. // Instances a new context.
  151. ContextPtr Factory::InstanceContext(const String& name)
  152. {
  153. ContextPtr new_context = context_instancer->InstanceContext(name);
  154. if (new_context)
  155. new_context->SetInstancer(context_instancer);
  156. return new_context;
  157. }
  158. void Factory::RegisterElementInstancer(const String& name, ElementInstancer* instancer)
  159. {
  160. element_instancers[StringUtilities::ToLower(name)] = instancer;
  161. }
  162. // Looks up the instancer for the given element
  163. ElementInstancer* Factory::GetElementInstancer(const String& tag)
  164. {
  165. ElementInstancerMap::iterator instancer_iterator = element_instancers.find(tag);
  166. if (instancer_iterator == element_instancers.end())
  167. {
  168. instancer_iterator = element_instancers.find("*");
  169. if (instancer_iterator == element_instancers.end())
  170. return nullptr;
  171. }
  172. return instancer_iterator->second;
  173. }
  174. // Instances a single element.
  175. ElementPtr Factory::InstanceElement(Element* parent, const String& instancer_name, const String& tag, const XMLAttributes& attributes)
  176. {
  177. ElementInstancer* instancer = GetElementInstancer(instancer_name);
  178. if (instancer)
  179. {
  180. ElementPtr element = instancer->InstanceElement(parent, tag, attributes);
  181. // Process the generic attributes and bind any events
  182. if (element)
  183. {
  184. element->SetInstancer(instancer);
  185. element->SetAttributes(attributes);
  186. ElementUtilities::BindEventAttributes(element.get());
  187. PluginRegistry::NotifyElementCreate(element.get());
  188. }
  189. return element;
  190. }
  191. return nullptr;
  192. }
  193. // Instances a single text element containing a string.
  194. bool Factory::InstanceElementText(Element* parent, const String& text)
  195. {
  196. SystemInterface* system_interface = GetSystemInterface();
  197. // Do any necessary translation. If any substitutions were made then new XML may have been introduced, so we'll
  198. // have to run the data through the XML parser again.
  199. String translated_data;
  200. if (system_interface != nullptr &&
  201. (system_interface->TranslateString(translated_data, text) > 0 ||
  202. translated_data.find("<") != String::npos))
  203. {
  204. RMLUI_ZoneScopedNC("InstanceStream", 0xDC143C);
  205. auto stream = std::make_unique<StreamMemory>(translated_data.size() + 32);
  206. stream->Write("<body>", 6);
  207. stream->Write(translated_data);
  208. stream->Write("</body>", 7);
  209. stream->Seek(0, SEEK_SET);
  210. InstanceElementStream(parent, stream.get());
  211. }
  212. else
  213. {
  214. RMLUI_ZoneScopedNC("InstanceText", 0x8FBC8F);
  215. // Check if this text node contains only white-space; if so, we don't want to construct it.
  216. bool only_white_space = true;
  217. for (size_t i = 0; i < translated_data.size(); ++i)
  218. {
  219. if (!StringUtilities::IsWhitespace(translated_data[i]))
  220. {
  221. only_white_space = false;
  222. break;
  223. }
  224. }
  225. if (only_white_space)
  226. return true;
  227. // Attempt to instance the element.
  228. XMLAttributes attributes;
  229. ElementPtr element = Factory::InstanceElement(parent, "#text", "#text", attributes);
  230. if (!element)
  231. {
  232. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s', instancer returned nullptr.", translated_data.c_str());
  233. return false;
  234. }
  235. // Assign the element its text value.
  236. ElementText* text_element = dynamic_cast< ElementText* >(element.get());
  237. if (!text_element)
  238. {
  239. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s'. Found type '%s', was expecting a derivative of ElementText.", translated_data.c_str(), typeid(element).name());
  240. return false;
  241. }
  242. text_element->SetText(translated_data);
  243. // Add to active node.
  244. parent->AppendChild(std::move(element));
  245. }
  246. return true;
  247. }
  248. // Instances a element tree based on the stream
  249. bool Factory::InstanceElementStream(Element* parent, Stream* stream)
  250. {
  251. XMLParser parser(parent);
  252. parser.Parse(stream);
  253. return true;
  254. }
  255. // Instances a element tree based on the stream
  256. ElementPtr Factory::InstanceDocumentStream(Rml::Core::Context* context, Stream* stream)
  257. {
  258. RMLUI_ZoneScoped;
  259. ElementPtr element = Factory::InstanceElement(nullptr, "body", "body", XMLAttributes());
  260. if (!element)
  261. {
  262. Log::Message(Log::LT_ERROR, "Failed to instance document, instancer returned nullptr.");
  263. return nullptr;
  264. }
  265. ElementDocument* document = dynamic_cast< ElementDocument* >(element.get());
  266. if (!document)
  267. {
  268. Log::Message(Log::LT_ERROR, "Failed to instance document element. Found type '%s', was expecting derivative of ElementDocument.", typeid(element).name());
  269. return nullptr;
  270. }
  271. document->context = context;
  272. XMLParser parser(element.get());
  273. parser.Parse(stream);
  274. return element;
  275. }
  276. // Registers an instancer that will be used to instance decorators.
  277. void Factory::RegisterDecoratorInstancer(const String& name, DecoratorInstancer* instancer)
  278. {
  279. RMLUI_ASSERT(instancer);
  280. decorator_instancers[StringUtilities::ToLower(name)] = instancer;
  281. }
  282. // Retrieves a decorator instancer registered with the factory.
  283. DecoratorInstancer* Factory::GetDecoratorInstancer(const String& name)
  284. {
  285. auto iterator = decorator_instancers.find(name);
  286. if (iterator == decorator_instancers.end())
  287. return nullptr;
  288. return iterator->second;
  289. }
  290. // Registers an instancer that will be used to instance font effects.
  291. void Factory::RegisterFontEffectInstancer(const String& name, FontEffectInstancer* instancer)
  292. {
  293. RMLUI_ASSERT(instancer);
  294. font_effect_instancers[StringUtilities::ToLower(name)] = instancer;
  295. }
  296. FontEffectInstancer* Factory::GetFontEffectInstancer(const String& name)
  297. {
  298. auto iterator = font_effect_instancers.find(name);
  299. if (iterator == font_effect_instancers.end())
  300. return nullptr;
  301. return iterator->second;
  302. }
  303. // Creates a style sheet containing the passed in styles.
  304. SharedPtr<StyleSheet> Factory::InstanceStyleSheetString(const String& string)
  305. {
  306. auto memory_stream = std::make_unique<StreamMemory>((const byte*) string.c_str(), string.size());
  307. return InstanceStyleSheetStream(memory_stream.get());
  308. }
  309. // Creates a style sheet from a file.
  310. SharedPtr<StyleSheet> Factory::InstanceStyleSheetFile(const String& file_name)
  311. {
  312. auto file_stream = std::make_unique<StreamFile>();
  313. file_stream->Open(file_name);
  314. return InstanceStyleSheetStream(file_stream.get());
  315. }
  316. // Creates a style sheet from an Stream.
  317. SharedPtr<StyleSheet> Factory::InstanceStyleSheetStream(Stream* stream)
  318. {
  319. SharedPtr<StyleSheet> style_sheet = std::make_shared<StyleSheet>();
  320. if (style_sheet->LoadStyleSheet(stream))
  321. {
  322. return style_sheet;
  323. }
  324. return nullptr;
  325. }
  326. // Clears the style sheet cache. This will force style sheets to be reloaded.
  327. void Factory::ClearStyleSheetCache()
  328. {
  329. StyleSheetFactory::ClearStyleSheetCache();
  330. }
  331. /// Clears the template cache. This will force templates to be reloaded.
  332. void Factory::ClearTemplateCache()
  333. {
  334. TemplateCache::Clear();
  335. }
  336. // Registers an instancer for all RmlEvents
  337. void Factory::RegisterEventInstancer(EventInstancer* instancer)
  338. {
  339. event_instancer = instancer;
  340. }
  341. // Instance an event object.
  342. EventPtr Factory::InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible)
  343. {
  344. EventPtr event = event_instancer->InstanceEvent(target, id, type, parameters, interruptible);
  345. if (event)
  346. event->instancer = event_instancer;
  347. return event;
  348. }
  349. // Register an instancer for all event listeners
  350. void Factory::RegisterEventListenerInstancer(EventListenerInstancer* instancer)
  351. {
  352. event_listener_instancer = instancer;
  353. }
  354. // Instance an event listener with the given string
  355. EventListener* Factory::InstanceEventListener(const String& value, Element* element)
  356. {
  357. // If we have an event listener instancer, use it
  358. if (event_listener_instancer)
  359. return event_listener_instancer->InstanceEventListener(value, element);
  360. return nullptr;
  361. }
  362. }
  363. }