Factory.cpp 13 KB

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