Factory.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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-2023 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/ElementText.h"
  35. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  36. #include "../../Include/RmlUi/Core/Elements/ElementForm.h"
  37. #include "../../Include/RmlUi/Core/Elements/ElementFormControlInput.h"
  38. #include "../../Include/RmlUi/Core/Elements/ElementFormControlSelect.h"
  39. #include "../../Include/RmlUi/Core/Elements/ElementFormControlTextArea.h"
  40. #include "../../Include/RmlUi/Core/Elements/ElementProgress.h"
  41. #include "../../Include/RmlUi/Core/Elements/ElementTabSet.h"
  42. #include "../../Include/RmlUi/Core/EventListenerInstancer.h"
  43. #include "../../Include/RmlUi/Core/StreamMemory.h"
  44. #include "../../Include/RmlUi/Core/StyleSheet.h"
  45. #include "../../Include/RmlUi/Core/StyleSheetContainer.h"
  46. #include "../../Include/RmlUi/Core/SystemInterface.h"
  47. #include "ContextInstancerDefault.h"
  48. #include "DataControllerDefault.h"
  49. #include "DataViewDefault.h"
  50. #include "DecoratorGradient.h"
  51. #include "DecoratorNinePatch.h"
  52. #include "DecoratorTiledBox.h"
  53. #include "DecoratorTiledHorizontal.h"
  54. #include "DecoratorTiledImage.h"
  55. #include "DecoratorTiledVertical.h"
  56. #include "ElementHandle.h"
  57. #include "Elements/ElementImage.h"
  58. #include "Elements/ElementLabel.h"
  59. #include "Elements/ElementTextSelection.h"
  60. #include "Elements/XMLNodeHandlerSelect.h"
  61. #include "Elements/XMLNodeHandlerTabSet.h"
  62. #include "Elements/XMLNodeHandlerTextArea.h"
  63. #include "EventInstancerDefault.h"
  64. #include "FilterBasic.h"
  65. #include "FilterBlur.h"
  66. #include "FontEffectBlur.h"
  67. #include "FontEffectGlow.h"
  68. #include "FontEffectOutline.h"
  69. #include "FontEffectShadow.h"
  70. #include "PluginRegistry.h"
  71. #include "StreamFile.h"
  72. #include "StyleSheetFactory.h"
  73. #include "TemplateCache.h"
  74. #include "XMLNodeHandlerBody.h"
  75. #include "XMLNodeHandlerDefault.h"
  76. #include "XMLNodeHandlerHead.h"
  77. #include "XMLNodeHandlerTemplate.h"
  78. #include "XMLParseTools.h"
  79. #include <algorithm>
  80. namespace Rml {
  81. // Element instancers.
  82. using ElementInstancerMap = UnorderedMap<String, ElementInstancer*>;
  83. static ElementInstancerMap element_instancers;
  84. // Decorator instancers.
  85. using DecoratorInstancerMap = UnorderedMap<String, DecoratorInstancer*>;
  86. static DecoratorInstancerMap decorator_instancers;
  87. // Filter instancers.
  88. using FilterInstancerMap = UnorderedMap<String, FilterInstancer*>;
  89. static FilterInstancerMap filter_instancers;
  90. // Font effect instancers.
  91. using FontEffectInstancerMap = UnorderedMap<String, FontEffectInstancer*>;
  92. static FontEffectInstancerMap font_effect_instancers;
  93. // Data view instancers.
  94. using DataViewInstancerMap = UnorderedMap<String, DataViewInstancer*>;
  95. static DataViewInstancerMap data_view_instancers;
  96. // Data controller instancers.
  97. using DataControllerInstancerMap = UnorderedMap<String, DataControllerInstancer*>;
  98. static DataControllerInstancerMap data_controller_instancers;
  99. // Structural data view instancers.
  100. using StructuralDataViewInstancerMap = SmallUnorderedMap<String, DataViewInstancer*>;
  101. static StructuralDataViewInstancerMap structural_data_view_instancers;
  102. // Structural data view names.
  103. static StringList structural_data_view_attribute_names;
  104. // The context instancer.
  105. static ContextInstancer* context_instancer = nullptr;
  106. // The event instancer
  107. static EventInstancer* event_instancer = nullptr;
  108. // Event listener instancer.
  109. static EventListenerInstancer* event_listener_instancer = nullptr;
  110. // Default instancers are constructed and destroyed on Initialise and Shutdown, respectively.
  111. struct DefaultInstancers {
  112. UniquePtr<ContextInstancer> context_default;
  113. UniquePtr<EventInstancer> event_default;
  114. // Basic elements
  115. ElementInstancerElement element_default;
  116. ElementInstancerText element_text;
  117. ElementInstancerGeneric<ElementImage> element_img;
  118. ElementInstancerGeneric<ElementHandle> element_handle;
  119. ElementInstancerGeneric<ElementDocument> element_body;
  120. // Control elements
  121. ElementInstancerGeneric<ElementForm> form;
  122. ElementInstancerGeneric<ElementFormControlInput> input;
  123. ElementInstancerGeneric<ElementFormControlSelect> select;
  124. ElementInstancerGeneric<ElementLabel> element_label;
  125. ElementInstancerGeneric<ElementFormControlTextArea> textarea;
  126. ElementInstancerGeneric<ElementTextSelection> selection;
  127. ElementInstancerGeneric<ElementTabSet> tabset;
  128. ElementInstancerGeneric<ElementProgress> progress;
  129. // Decorators
  130. DecoratorTiledHorizontalInstancer decorator_tiled_horizontal;
  131. DecoratorTiledVerticalInstancer decorator_tiled_vertical;
  132. DecoratorTiledBoxInstancer decorator_tiled_box;
  133. DecoratorTiledImageInstancer decorator_image;
  134. DecoratorNinePatchInstancer decorator_ninepatch;
  135. DecoratorGradientInstancer decorator_gradient;
  136. // Filters
  137. FilterBasicInstancer filter_hue_rotate = {FilterBasicInstancer::ValueType::Angle, "0rad"};
  138. FilterBasicInstancer filter_basic_d0 = {FilterBasicInstancer::ValueType::NumberPercent, "0"};
  139. FilterBasicInstancer filter_basic_d1 = {FilterBasicInstancer::ValueType::NumberPercent, "1"};
  140. FilterBlurInstancer filter_blur;
  141. // Font effects
  142. FontEffectBlurInstancer font_effect_blur;
  143. FontEffectGlowInstancer font_effect_glow;
  144. FontEffectOutlineInstancer font_effect_outline;
  145. FontEffectShadowInstancer font_effect_shadow;
  146. // Data binding views
  147. DataViewInstancerDefault<DataViewAttribute> data_view_attribute;
  148. DataViewInstancerDefault<DataViewAttributeIf> data_view_attribute_if;
  149. DataViewInstancerDefault<DataViewClass> data_view_class;
  150. DataViewInstancerDefault<DataViewIf> data_view_if;
  151. DataViewInstancerDefault<DataViewVisible> data_view_visible;
  152. DataViewInstancerDefault<DataViewRml> data_view_rml;
  153. DataViewInstancerDefault<DataViewStyle> data_view_style;
  154. DataViewInstancerDefault<DataViewText> data_view_text;
  155. DataViewInstancerDefault<DataViewValue> data_view_value;
  156. DataViewInstancerDefault<DataViewChecked> data_view_checked;
  157. DataViewInstancerDefault<DataViewAlias> data_view_alias;
  158. DataViewInstancerDefault<DataViewFor> structural_data_view_for;
  159. // Data binding controllers
  160. DataControllerInstancerDefault<DataControllerEvent> data_controller_event;
  161. DataControllerInstancerDefault<DataControllerValue> data_controller_value;
  162. };
  163. static UniquePtr<DefaultInstancers> default_instancers;
  164. Factory::Factory() {}
  165. Factory::~Factory() {}
  166. bool Factory::Initialise()
  167. {
  168. default_instancers = MakeUnique<DefaultInstancers>();
  169. // Default context instancer
  170. if (!context_instancer)
  171. {
  172. default_instancers->context_default = MakeUnique<ContextInstancerDefault>();
  173. context_instancer = default_instancers->context_default.get();
  174. }
  175. // Default event instancer
  176. if (!event_instancer)
  177. {
  178. default_instancers->event_default = MakeUnique<EventInstancerDefault>();
  179. event_instancer = default_instancers->event_default.get();
  180. }
  181. // No default event listener instancer
  182. if (!event_listener_instancer)
  183. event_listener_instancer = nullptr;
  184. // Basic element instancers
  185. RegisterElementInstancer("*", &default_instancers->element_default);
  186. RegisterElementInstancer("img", &default_instancers->element_img);
  187. RegisterElementInstancer("#text", &default_instancers->element_text);
  188. RegisterElementInstancer("handle", &default_instancers->element_handle);
  189. RegisterElementInstancer("body", &default_instancers->element_body);
  190. // Control element instancers
  191. RegisterElementInstancer("form", &default_instancers->form);
  192. RegisterElementInstancer("input", &default_instancers->input);
  193. RegisterElementInstancer("select", &default_instancers->select);
  194. RegisterElementInstancer("label", &default_instancers->element_label);
  195. RegisterElementInstancer("textarea", &default_instancers->textarea);
  196. RegisterElementInstancer("#selection", &default_instancers->selection);
  197. RegisterElementInstancer("tabset", &default_instancers->tabset);
  198. RegisterElementInstancer("progress", &default_instancers->progress);
  199. RegisterElementInstancer("progressbar", &default_instancers->progress);
  200. // Decorator instancers
  201. RegisterDecoratorInstancer("tiled-horizontal", &default_instancers->decorator_tiled_horizontal);
  202. RegisterDecoratorInstancer("tiled-vertical", &default_instancers->decorator_tiled_vertical);
  203. RegisterDecoratorInstancer("tiled-box", &default_instancers->decorator_tiled_box);
  204. RegisterDecoratorInstancer("image", &default_instancers->decorator_image);
  205. RegisterDecoratorInstancer("ninepatch", &default_instancers->decorator_ninepatch);
  206. RegisterDecoratorInstancer("gradient", &default_instancers->decorator_gradient);
  207. // Filter instancers
  208. RegisterFilterInstancer("hue-rotate", &default_instancers->filter_hue_rotate);
  209. RegisterFilterInstancer("brightness", &default_instancers->filter_basic_d1);
  210. RegisterFilterInstancer("contrast", &default_instancers->filter_basic_d1);
  211. RegisterFilterInstancer("grayscale", &default_instancers->filter_basic_d0);
  212. RegisterFilterInstancer("invert", &default_instancers->filter_basic_d0);
  213. RegisterFilterInstancer("opacity", &default_instancers->filter_basic_d1);
  214. RegisterFilterInstancer("saturate", &default_instancers->filter_basic_d1);
  215. RegisterFilterInstancer("sepia", &default_instancers->filter_basic_d0);
  216. RegisterFilterInstancer("blur", &default_instancers->filter_blur);
  217. // Font effect instancers
  218. RegisterFontEffectInstancer("blur", &default_instancers->font_effect_blur);
  219. RegisterFontEffectInstancer("glow", &default_instancers->font_effect_glow);
  220. RegisterFontEffectInstancer("outline", &default_instancers->font_effect_outline);
  221. RegisterFontEffectInstancer("shadow", &default_instancers->font_effect_shadow);
  222. // Data binding views
  223. // clang-format off
  224. RegisterDataViewInstancer(&default_instancers->data_view_attribute, "attr", false);
  225. RegisterDataViewInstancer(&default_instancers->data_view_attribute_if, "attrif", false);
  226. RegisterDataViewInstancer(&default_instancers->data_view_class, "class", false);
  227. RegisterDataViewInstancer(&default_instancers->data_view_if, "if", false);
  228. RegisterDataViewInstancer(&default_instancers->data_view_visible, "visible", false);
  229. RegisterDataViewInstancer(&default_instancers->data_view_rml, "rml", false);
  230. RegisterDataViewInstancer(&default_instancers->data_view_style, "style", false);
  231. RegisterDataViewInstancer(&default_instancers->data_view_text, "text", false);
  232. RegisterDataViewInstancer(&default_instancers->data_view_value, "value", false);
  233. RegisterDataViewInstancer(&default_instancers->data_view_checked, "checked", false);
  234. RegisterDataViewInstancer(&default_instancers->data_view_alias, "alias", false);
  235. RegisterDataViewInstancer(&default_instancers->structural_data_view_for, "for", true );
  236. // clang-format on
  237. // Data binding controllers
  238. RegisterDataControllerInstancer(&default_instancers->data_controller_value, "checked");
  239. RegisterDataControllerInstancer(&default_instancers->data_controller_event, "event");
  240. RegisterDataControllerInstancer(&default_instancers->data_controller_value, "value");
  241. // XML node handlers
  242. XMLParser::RegisterNodeHandler("", MakeShared<XMLNodeHandlerDefault>());
  243. XMLParser::RegisterNodeHandler("body", MakeShared<XMLNodeHandlerBody>());
  244. XMLParser::RegisterNodeHandler("head", MakeShared<XMLNodeHandlerHead>());
  245. XMLParser::RegisterNodeHandler("template", MakeShared<XMLNodeHandlerTemplate>());
  246. // XML node handlers for control elements
  247. XMLParser::RegisterNodeHandler("tabset", MakeShared<XMLNodeHandlerTabSet>());
  248. XMLParser::RegisterNodeHandler("textarea", MakeShared<XMLNodeHandlerTextArea>());
  249. XMLParser::RegisterNodeHandler("select", MakeShared<XMLNodeHandlerSelect>());
  250. return true;
  251. }
  252. void Factory::Shutdown()
  253. {
  254. element_instancers.clear();
  255. decorator_instancers.clear();
  256. font_effect_instancers.clear();
  257. data_controller_instancers.clear();
  258. data_view_instancers.clear();
  259. structural_data_view_instancers.clear();
  260. structural_data_view_attribute_names.clear();
  261. context_instancer = nullptr;
  262. event_listener_instancer = nullptr;
  263. event_instancer = nullptr;
  264. XMLParser::ReleaseHandlers();
  265. default_instancers.reset();
  266. }
  267. void Factory::RegisterContextInstancer(ContextInstancer* instancer)
  268. {
  269. context_instancer = instancer;
  270. }
  271. ContextPtr Factory::InstanceContext(const String& name)
  272. {
  273. ContextPtr new_context = context_instancer->InstanceContext(name);
  274. if (new_context)
  275. new_context->SetInstancer(context_instancer);
  276. return new_context;
  277. }
  278. void Factory::RegisterElementInstancer(const String& name, ElementInstancer* instancer)
  279. {
  280. element_instancers[StringUtilities::ToLower(name)] = instancer;
  281. }
  282. ElementInstancer* Factory::GetElementInstancer(const String& tag)
  283. {
  284. ElementInstancerMap::iterator instancer_iterator = element_instancers.find(tag);
  285. if (instancer_iterator == element_instancers.end())
  286. {
  287. instancer_iterator = element_instancers.find("*");
  288. if (instancer_iterator == element_instancers.end())
  289. return nullptr;
  290. }
  291. return instancer_iterator->second;
  292. }
  293. ElementPtr Factory::InstanceElement(Element* parent, const String& instancer_name, const String& tag, const XMLAttributes& attributes)
  294. {
  295. if (ElementInstancer* instancer = GetElementInstancer(instancer_name))
  296. {
  297. if (ElementPtr element = instancer->InstanceElement(parent, tag, attributes))
  298. {
  299. element->SetInstancer(instancer);
  300. element->SetAttributes(attributes);
  301. PluginRegistry::NotifyElementCreate(element.get());
  302. return element;
  303. }
  304. }
  305. return nullptr;
  306. }
  307. bool Factory::InstanceElementText(Element* parent, const String& in_text)
  308. {
  309. RMLUI_ASSERT(parent);
  310. String text;
  311. if (SystemInterface* system_interface = GetSystemInterface())
  312. system_interface->TranslateString(text, in_text);
  313. // If this text node only contains white-space we don't want to construct it.
  314. const bool only_white_space = std::all_of(text.begin(), text.end(), &StringUtilities::IsWhitespace);
  315. if (only_white_space)
  316. return true;
  317. // See if we need to parse it as RML, and whether the text contains data expressions (curly brackets).
  318. bool parse_as_rml = false;
  319. bool has_data_expression = false;
  320. bool inside_brackets = false;
  321. bool inside_string = false;
  322. char previous = 0;
  323. for (const char c : text)
  324. {
  325. const char* error_str = XMLParseTools::ParseDataBrackets(inside_brackets, inside_string, c, previous);
  326. if (error_str)
  327. {
  328. Log::Message(Log::LT_WARNING, "Failed to instance text element '%s'. %s", text.c_str(), error_str);
  329. return false;
  330. }
  331. if (inside_brackets)
  332. has_data_expression = true;
  333. else if (c == '<')
  334. parse_as_rml = true;
  335. previous = c;
  336. }
  337. // If the text contains RML elements then run it through the XML parser again.
  338. if (parse_as_rml)
  339. {
  340. RMLUI_ZoneScopedNC("InstanceStream", 0xDC143C);
  341. auto stream = MakeUnique<StreamMemory>(text.size() + 32);
  342. Context* context = parent->GetContext();
  343. String tag = context ? context->GetDocumentsBaseTag() : "body";
  344. String open_tag = "<" + tag + ">";
  345. String close_tag = "</" + tag + ">";
  346. stream->Write(open_tag.c_str(), open_tag.size());
  347. stream->Write(text);
  348. stream->Write(close_tag.c_str(), close_tag.size());
  349. stream->Seek(0, SEEK_SET);
  350. InstanceElementStream(parent, stream.get());
  351. }
  352. else
  353. {
  354. RMLUI_ZoneScopedNC("InstanceText", 0x8FBC8F);
  355. // Attempt to instance the element.
  356. XMLAttributes attributes;
  357. // If we have curly brackets in the text, we tag the element so that the appropriate data view (DataViewText) is constructed.
  358. if (has_data_expression)
  359. attributes.emplace("data-text", Variant());
  360. ElementPtr element = Factory::InstanceElement(parent, "#text", "#text", attributes);
  361. if (!element)
  362. {
  363. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s', instancer returned nullptr.", text.c_str());
  364. return false;
  365. }
  366. // Assign the element its text value.
  367. ElementText* text_element = rmlui_dynamic_cast<ElementText*>(element.get());
  368. if (!text_element)
  369. {
  370. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s'. Found type '%s', was expecting a derivative of ElementText.",
  371. text.c_str(), rmlui_type_name(*element));
  372. return false;
  373. }
  374. // Unescape any escaped entities or unicode symbols
  375. text = StringUtilities::DecodeRml(text);
  376. text_element->SetText(text);
  377. // Add to active node.
  378. parent->AppendChild(std::move(element));
  379. }
  380. return true;
  381. }
  382. bool Factory::InstanceElementStream(Element* parent, Stream* stream)
  383. {
  384. XMLParser parser(parent);
  385. parser.Parse(stream);
  386. return true;
  387. }
  388. ElementPtr Factory::InstanceDocumentStream(Context* context, Stream* stream, const String& document_base_tag)
  389. {
  390. RMLUI_ZoneScoped;
  391. ElementPtr element = Factory::InstanceElement(nullptr, document_base_tag, document_base_tag, XMLAttributes());
  392. if (!element)
  393. {
  394. Log::Message(Log::LT_ERROR, "Failed to instance document, instancer returned nullptr.");
  395. return nullptr;
  396. }
  397. ElementDocument* document = rmlui_dynamic_cast<ElementDocument*>(element.get());
  398. if (!document)
  399. {
  400. Log::Message(Log::LT_ERROR, "Failed to instance document element. Found type '%s', was expecting derivative of ElementDocument.",
  401. rmlui_type_name(*element));
  402. return nullptr;
  403. }
  404. document->context = context;
  405. XMLParser parser(element.get());
  406. parser.Parse(stream);
  407. return element;
  408. }
  409. void Factory::RegisterDecoratorInstancer(const String& name, DecoratorInstancer* instancer)
  410. {
  411. RMLUI_ASSERT(instancer);
  412. decorator_instancers[StringUtilities::ToLower(name)] = instancer;
  413. }
  414. DecoratorInstancer* Factory::GetDecoratorInstancer(const String& name)
  415. {
  416. auto iterator = decorator_instancers.find(name);
  417. if (iterator == decorator_instancers.end())
  418. return nullptr;
  419. return iterator->second;
  420. }
  421. void Factory::RegisterFilterInstancer(const String& name, FilterInstancer* instancer)
  422. {
  423. RMLUI_ASSERT(instancer);
  424. filter_instancers[StringUtilities::ToLower(name)] = instancer;
  425. }
  426. FilterInstancer* Factory::GetFilterInstancer(const String& name)
  427. {
  428. auto iterator = filter_instancers.find(name);
  429. if (iterator == filter_instancers.end())
  430. return nullptr;
  431. return iterator->second;
  432. }
  433. void Factory::RegisterFontEffectInstancer(const String& name, FontEffectInstancer* instancer)
  434. {
  435. RMLUI_ASSERT(instancer);
  436. font_effect_instancers[StringUtilities::ToLower(name)] = instancer;
  437. }
  438. FontEffectInstancer* Factory::GetFontEffectInstancer(const String& name)
  439. {
  440. auto iterator = font_effect_instancers.find(name);
  441. if (iterator == font_effect_instancers.end())
  442. return nullptr;
  443. return iterator->second;
  444. }
  445. SharedPtr<StyleSheetContainer> Factory::InstanceStyleSheetString(const String& string)
  446. {
  447. auto memory_stream = MakeUnique<StreamMemory>((const byte*)string.c_str(), string.size());
  448. return InstanceStyleSheetStream(memory_stream.get());
  449. }
  450. SharedPtr<StyleSheetContainer> Factory::InstanceStyleSheetFile(const String& file_name)
  451. {
  452. auto file_stream = MakeUnique<StreamFile>();
  453. file_stream->Open(file_name);
  454. return InstanceStyleSheetStream(file_stream.get());
  455. }
  456. SharedPtr<StyleSheetContainer> Factory::InstanceStyleSheetStream(Stream* stream)
  457. {
  458. SharedPtr<StyleSheetContainer> style_sheet_container = MakeShared<StyleSheetContainer>();
  459. if (style_sheet_container->LoadStyleSheetContainer(stream))
  460. {
  461. return style_sheet_container;
  462. }
  463. return nullptr;
  464. }
  465. void Factory::ClearStyleSheetCache()
  466. {
  467. StyleSheetFactory::ClearStyleSheetCache();
  468. }
  469. void Factory::ClearTemplateCache()
  470. {
  471. TemplateCache::Clear();
  472. }
  473. void Factory::RegisterEventInstancer(EventInstancer* instancer)
  474. {
  475. event_instancer = instancer;
  476. }
  477. EventPtr Factory::InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible)
  478. {
  479. EventPtr event = event_instancer->InstanceEvent(target, id, type, parameters, interruptible);
  480. if (event)
  481. event->instancer = event_instancer;
  482. return event;
  483. }
  484. void Factory::RegisterEventListenerInstancer(EventListenerInstancer* instancer)
  485. {
  486. event_listener_instancer = instancer;
  487. }
  488. EventListener* Factory::InstanceEventListener(const String& value, Element* element)
  489. {
  490. // If we have an event listener instancer, use it
  491. if (event_listener_instancer)
  492. return event_listener_instancer->InstanceEventListener(value, element);
  493. return nullptr;
  494. }
  495. void Factory::RegisterDataViewInstancer(DataViewInstancer* instancer, const String& name, bool is_structural_view)
  496. {
  497. bool inserted = false;
  498. if (is_structural_view)
  499. {
  500. inserted = structural_data_view_instancers.emplace(name, instancer).second;
  501. if (inserted)
  502. structural_data_view_attribute_names.push_back(String("data-") + name);
  503. }
  504. else
  505. {
  506. inserted = data_view_instancers.emplace(name, instancer).second;
  507. }
  508. if (!inserted)
  509. Log::Message(Log::LT_WARNING, "Could not register data view instancer '%s'. The given name is already registered.", name.c_str());
  510. }
  511. void Factory::RegisterDataControllerInstancer(DataControllerInstancer* instancer, const String& name)
  512. {
  513. bool inserted = data_controller_instancers.emplace(name, instancer).second;
  514. if (!inserted)
  515. Log::Message(Log::LT_WARNING, "Could not register data controller instancer '%s'. The given name is already registered.", name.c_str());
  516. }
  517. DataViewPtr Factory::InstanceDataView(const String& type_name, Element* element, bool is_structural_view)
  518. {
  519. RMLUI_ASSERT(element);
  520. if (is_structural_view)
  521. {
  522. auto it = structural_data_view_instancers.find(type_name);
  523. if (it != structural_data_view_instancers.end())
  524. return it->second->InstanceView(element);
  525. }
  526. else
  527. {
  528. auto it = data_view_instancers.find(type_name);
  529. if (it != data_view_instancers.end())
  530. return it->second->InstanceView(element);
  531. }
  532. return nullptr;
  533. }
  534. DataControllerPtr Factory::InstanceDataController(const String& type_name, Element* element)
  535. {
  536. auto it = data_controller_instancers.find(type_name);
  537. if (it != data_controller_instancers.end())
  538. return it->second->InstanceController(element);
  539. return DataControllerPtr();
  540. }
  541. bool Factory::IsStructuralDataView(const String& type_name)
  542. {
  543. return structural_data_view_instancers.find(type_name) != structural_data_view_instancers.end();
  544. }
  545. const StringList& Factory::GetStructuralDataViewAttributeNames()
  546. {
  547. return structural_data_view_attribute_names;
  548. }
  549. } // namespace Rml