Factory.cpp 14 KB

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