2
0

Factory.cpp 15 KB

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