Core.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/Core.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/Factory.h"
  31. #include "../../Include/RmlUi/Core/FileInterface.h"
  32. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  33. #include "../../Include/RmlUi/Core/Plugin.h"
  34. #include "../../Include/RmlUi/Core/RenderInterface.h"
  35. #include "../../Include/RmlUi/Core/SystemInterface.h"
  36. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  37. #include "../../Include/RmlUi/Core/Types.h"
  38. #include "EventSpecification.h"
  39. #include "FileInterfaceDefault.h"
  40. #include "GeometryDatabase.h"
  41. #include "PluginRegistry.h"
  42. #include "StyleSheetFactory.h"
  43. #include "StyleSheetParser.h"
  44. #include "TemplateCache.h"
  45. #include "TextureDatabase.h"
  46. #include "EventSpecification.h"
  47. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  48. #include "FontEngineDefault/FontEngineInterfaceDefault.h"
  49. #endif
  50. #ifdef RMLUI_ENABLE_LOTTIE_PLUGIN
  51. #include "../Lottie/LottiePlugin.h"
  52. #endif
  53. #ifdef RMLUI_ENABLE_SVG_PLUGIN
  54. #include "../SVG/SVGPlugin.h"
  55. #endif
  56. namespace Rml {
  57. // RmlUi's renderer interface.
  58. static RenderInterface* render_interface = nullptr;
  59. /// RmlUi's system interface.
  60. static SystemInterface* system_interface = nullptr;
  61. // RmlUi's file I/O interface.
  62. static FileInterface* file_interface = nullptr;
  63. // RmlUi's font engine interface.
  64. static FontEngineInterface* font_interface = nullptr;
  65. // Default interfaces should be created and destroyed on Initialise and Shutdown, respectively.
  66. static UniquePtr<FileInterface> default_file_interface;
  67. static UniquePtr<FontEngineInterface> default_font_interface;
  68. static bool initialised = false;
  69. using ContextMap = UnorderedMap< String, ContextPtr >;
  70. static ContextMap contexts;
  71. #ifndef RMLUI_VERSION
  72. #define RMLUI_VERSION "custom"
  73. #endif
  74. bool Initialise()
  75. {
  76. RMLUI_ASSERTMSG(!initialised, "Rml::Initialise() called, but RmlUi is already initialised!");
  77. Log::Initialise();
  78. // Check for valid interfaces, or install default interfaces as appropriate.
  79. if (!system_interface)
  80. {
  81. Log::Message(Log::LT_ERROR, "No system interface set!");
  82. return false;
  83. }
  84. if (!file_interface)
  85. {
  86. #ifndef RMLUI_NO_FILE_INTERFACE_DEFAULT
  87. default_file_interface = MakeUnique<FileInterfaceDefault>();
  88. file_interface = default_file_interface.get();
  89. #else
  90. Log::Message(Log::LT_ERROR, "No file interface set!");
  91. return false;
  92. #endif
  93. }
  94. EventSpecificationInterface::Initialize();
  95. TextureDatabase::Initialise();
  96. if (!font_interface)
  97. {
  98. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  99. default_font_interface = MakeUnique<FontEngineInterfaceDefault>();
  100. font_interface = default_font_interface.get();
  101. #else
  102. Log::Message(Log::LT_ERROR, "No font interface set!");
  103. return false;
  104. #endif
  105. }
  106. StyleSheetSpecification::Initialise();
  107. StyleSheetParser::Initialise();
  108. StyleSheetFactory::Initialise();
  109. TemplateCache::Initialise();
  110. Factory::Initialise();
  111. // Initialise plugins integrated with Core.
  112. #ifdef RMLUI_ENABLE_LOTTIE_PLUGIN
  113. Lottie::Initialise();
  114. #endif
  115. #ifdef RMLUI_ENABLE_SVG_PLUGIN
  116. SVG::Initialise();
  117. #endif
  118. // Notify all plugins we're starting up.
  119. PluginRegistry::NotifyInitialise();
  120. initialised = true;
  121. return true;
  122. }
  123. void Shutdown()
  124. {
  125. RMLUI_ASSERTMSG(initialised, "Rml::Shutdown() called, but RmlUi is not initialised!");
  126. // Clear out all contexts, which should also clean up all attached elements.
  127. contexts.clear();
  128. // Notify all plugins we're being shutdown.
  129. PluginRegistry::NotifyShutdown();
  130. Factory::Shutdown();
  131. TemplateCache::Shutdown();
  132. StyleSheetFactory::Shutdown();
  133. StyleSheetParser::Shutdown();
  134. StyleSheetSpecification::Shutdown();
  135. font_interface = nullptr;
  136. default_font_interface.reset();
  137. TextureDatabase::Shutdown();
  138. initialised = false;
  139. render_interface = nullptr;
  140. file_interface = nullptr;
  141. system_interface = nullptr;
  142. default_file_interface.reset();
  143. Log::Shutdown();
  144. }
  145. // Returns the version of this RmlUi library.
  146. String GetVersion()
  147. {
  148. return RMLUI_VERSION;
  149. }
  150. // Sets the interface through which all RmlUi messages will be routed.
  151. void SetSystemInterface(SystemInterface* _system_interface)
  152. {
  153. system_interface = _system_interface;
  154. }
  155. // Returns RmlUi's system interface.
  156. SystemInterface* GetSystemInterface()
  157. {
  158. return system_interface;
  159. }
  160. // Sets the interface through which all rendering requests are made.
  161. void SetRenderInterface(RenderInterface* _render_interface)
  162. {
  163. render_interface = _render_interface;
  164. }
  165. // Returns RmlUi's render interface.
  166. RenderInterface* GetRenderInterface()
  167. {
  168. return render_interface;
  169. }
  170. // Sets the interface through which all file I/O requests are made.
  171. void SetFileInterface(FileInterface* _file_interface)
  172. {
  173. file_interface = _file_interface;
  174. }
  175. // Returns RmlUi's file interface.
  176. FileInterface* GetFileInterface()
  177. {
  178. return file_interface;
  179. }
  180. // Sets the interface through which all font requests are made.
  181. void SetFontEngineInterface(FontEngineInterface* _font_interface)
  182. {
  183. font_interface = _font_interface;
  184. }
  185. // Returns RmlUi's file interface.
  186. FontEngineInterface* GetFontEngineInterface()
  187. {
  188. return font_interface;
  189. }
  190. // Creates a new element context.
  191. Context* CreateContext(const String& name, const Vector2i dimensions, RenderInterface* custom_render_interface)
  192. {
  193. if (!initialised)
  194. return nullptr;
  195. if (!custom_render_interface && !render_interface)
  196. {
  197. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.", name.c_str());
  198. return nullptr;
  199. }
  200. if (GetContext(name))
  201. {
  202. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.c_str());
  203. return nullptr;
  204. }
  205. ContextPtr new_context = Factory::InstanceContext(name);
  206. if (!new_context)
  207. {
  208. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned nullptr.", name.c_str());
  209. return nullptr;
  210. }
  211. // Set the render interface on the context, and add a reference onto it.
  212. if (custom_render_interface)
  213. new_context->render_interface = custom_render_interface;
  214. else
  215. new_context->render_interface = render_interface;
  216. new_context->SetDimensions(dimensions);
  217. Context* new_context_raw = new_context.get();
  218. contexts[name] = std::move(new_context);
  219. PluginRegistry::NotifyContextCreate(new_context_raw);
  220. return new_context_raw;
  221. }
  222. bool RemoveContext(const String& name)
  223. {
  224. auto it = contexts.find(name);
  225. if (it != contexts.end())
  226. {
  227. contexts.erase(it);
  228. return true;
  229. }
  230. return false;
  231. }
  232. // Fetches a previously constructed context by name.
  233. Context* GetContext(const String& name)
  234. {
  235. ContextMap::iterator i = contexts.find(name);
  236. if (i == contexts.end())
  237. return nullptr;
  238. return i->second.get();
  239. }
  240. // Fetches a context by index.
  241. Context* GetContext(int index)
  242. {
  243. ContextMap::iterator i = contexts.begin();
  244. int count = 0;
  245. if (index < 0 || index >= GetNumContexts())
  246. return nullptr;
  247. while (count < index)
  248. {
  249. ++i;
  250. ++count;
  251. }
  252. if (i == contexts.end())
  253. return nullptr;
  254. return i->second.get();
  255. }
  256. // Returns the number of active contexts.
  257. int GetNumContexts()
  258. {
  259. return (int) contexts.size();
  260. }
  261. bool LoadFontFace(const String& file_name, bool fallback_face)
  262. {
  263. return font_interface->LoadFontFace(file_name, fallback_face);
  264. }
  265. bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face)
  266. {
  267. return font_interface->LoadFontFace(data, data_size, font_family, style, weight, fallback_face);
  268. }
  269. // Registers a generic rmlui plugin
  270. void RegisterPlugin(Plugin* plugin)
  271. {
  272. if (initialised)
  273. plugin->OnInitialise();
  274. PluginRegistry::RegisterPlugin(plugin);
  275. }
  276. EventId RegisterEventType(const String& type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  277. {
  278. return EventSpecificationInterface::InsertOrReplaceCustom(type, interruptible, bubbles, default_action_phase);
  279. }
  280. StringList GetTextureSourceList()
  281. {
  282. return TextureDatabase::GetSourceList();
  283. }
  284. void ReleaseTextures()
  285. {
  286. TextureDatabase::ReleaseTextures();
  287. }
  288. void ReleaseCompiledGeometry()
  289. {
  290. return GeometryDatabase::ReleaseAll();
  291. }
  292. } // namespace Rml