Factory.cpp 13 KB

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