Factory.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 "FontEffectNoneInstancer.h"
  41. #include "FontEffectOutlineInstancer.h"
  42. #include "FontEffectShadowInstancer.h"
  43. #include "PluginRegistry.h"
  44. #include "PropertyParserColour.h"
  45. #include "StreamFile.h"
  46. #include "StyleSheetFactory.h"
  47. #include "TemplateCache.h"
  48. #include "XMLNodeHandlerBody.h"
  49. #include "XMLNodeHandlerDefault.h"
  50. #include "XMLNodeHandlerHead.h"
  51. #include "XMLNodeHandlerTemplate.h"
  52. #include "XMLParseTools.h"
  53. namespace Rml {
  54. namespace Core {
  55. // Element instancers.
  56. typedef UnorderedMap< String, ElementInstancer* > ElementInstancerMap;
  57. static ElementInstancerMap element_instancers;
  58. // Decorator instancers.
  59. typedef UnorderedMap< String, std::unique_ptr<DecoratorInstancer> > DecoratorInstancerMap;
  60. static DecoratorInstancerMap decorator_instancers;
  61. // Font effect instancers.
  62. typedef UnorderedMap< String, FontEffectInstancer* > FontEffectInstancerMap;
  63. static FontEffectInstancerMap font_effect_instancers;
  64. // The context instancer.
  65. static ContextInstancer* context_instancer = NULL;
  66. // The event instancer
  67. static EventInstancer* event_instancer = NULL;
  68. // Event listener instancer.
  69. static EventListenerInstancer* event_listener_instancer = NULL;
  70. Factory::Factory()
  71. {
  72. }
  73. Factory::~Factory()
  74. {
  75. }
  76. bool Factory::Initialise()
  77. {
  78. // Bind the default context instancer.
  79. if (context_instancer == NULL)
  80. context_instancer = new ContextInstancerDefault();
  81. // Bind default event instancer
  82. if (event_instancer == NULL)
  83. event_instancer = new EventInstancerDefault();
  84. // No default event listener instancer
  85. if (event_listener_instancer == NULL)
  86. event_listener_instancer = NULL;
  87. // Bind the default element instancers
  88. RegisterElementInstancer("*", new ElementInstancerGeneric< Element >())->RemoveReference();
  89. RegisterElementInstancer("img", new ElementInstancerGeneric< ElementImage >())->RemoveReference();
  90. RegisterElementInstancer("#text", new ElementInstancerGeneric< ElementTextDefault >())->RemoveReference();
  91. RegisterElementInstancer("handle", new ElementInstancerGeneric< ElementHandle >())->RemoveReference();
  92. RegisterElementInstancer("body", new ElementInstancerGeneric< ElementDocument >())->RemoveReference();
  93. // Bind the default decorator instancers
  94. RegisterDecoratorInstancer("tiled-horizontal", std::make_unique<DecoratorTiledHorizontalInstancer>());
  95. RegisterDecoratorInstancer("tiled-vertical", std::make_unique<DecoratorTiledVerticalInstancer>());
  96. RegisterDecoratorInstancer("tiled-box", std::make_unique<DecoratorTiledBoxInstancer>());
  97. RegisterDecoratorInstancer("image", std::make_unique<DecoratorTiledImageInstancer>());
  98. RegisterFontEffectInstancer("shadow", new FontEffectShadowInstancer())->RemoveReference();
  99. RegisterFontEffectInstancer("outline", new FontEffectOutlineInstancer())->RemoveReference();
  100. RegisterFontEffectInstancer("none", new FontEffectNoneInstancer())->RemoveReference();
  101. // Register the core XML node handlers.
  102. XMLParser::RegisterNodeHandler("", new XMLNodeHandlerDefault())->RemoveReference();
  103. XMLParser::RegisterNodeHandler("body", new XMLNodeHandlerBody())->RemoveReference();
  104. XMLParser::RegisterNodeHandler("head", new XMLNodeHandlerHead())->RemoveReference();
  105. XMLParser::RegisterNodeHandler("template", new XMLNodeHandlerTemplate())->RemoveReference();
  106. return true;
  107. }
  108. void Factory::Shutdown()
  109. {
  110. for (ElementInstancerMap::iterator i = element_instancers.begin(); i != element_instancers.end(); ++i)
  111. (*i).second->RemoveReference();
  112. element_instancers.clear();
  113. decorator_instancers.clear();
  114. for (FontEffectInstancerMap::iterator i = font_effect_instancers.begin(); i != font_effect_instancers.end(); ++i)
  115. (*i).second->RemoveReference();
  116. font_effect_instancers.clear();
  117. if (context_instancer)
  118. context_instancer->RemoveReference();
  119. context_instancer = NULL;
  120. if (event_listener_instancer)
  121. event_listener_instancer->RemoveReference();
  122. event_listener_instancer = NULL;
  123. if (event_instancer)
  124. event_instancer->RemoveReference();
  125. event_instancer = NULL;
  126. XMLParser::ReleaseHandlers();
  127. }
  128. // Registers the instancer to use when instancing contexts.
  129. ContextInstancer* Factory::RegisterContextInstancer(ContextInstancer* instancer)
  130. {
  131. instancer->AddReference();
  132. if (context_instancer != NULL)
  133. context_instancer->RemoveReference();
  134. context_instancer = instancer;
  135. return instancer;
  136. }
  137. // Instances a new context.
  138. Context* Factory::InstanceContext(const String& name)
  139. {
  140. Context* new_context = context_instancer->InstanceContext(name);
  141. if (new_context != NULL)
  142. new_context->SetInstancer(context_instancer);
  143. return new_context;
  144. }
  145. ElementInstancer* Factory::RegisterElementInstancer(const String& name, ElementInstancer* instancer)
  146. {
  147. String lower_case_name = ToLower(name);
  148. instancer->AddReference();
  149. // Check if an instancer for this tag is already defined, if so release it
  150. ElementInstancerMap::iterator itr = element_instancers.find(lower_case_name);
  151. if (itr != element_instancers.end())
  152. {
  153. (*itr).second->RemoveReference();
  154. }
  155. element_instancers[lower_case_name] = instancer;
  156. return instancer;
  157. }
  158. // Looks up the instancer for the given element
  159. ElementInstancer* Factory::GetElementInstancer(const String& tag)
  160. {
  161. ElementInstancerMap::iterator instancer_iterator = element_instancers.find(tag);
  162. if (instancer_iterator == element_instancers.end())
  163. {
  164. instancer_iterator = element_instancers.find("*");
  165. if (instancer_iterator == element_instancers.end())
  166. return NULL;
  167. }
  168. return (*instancer_iterator).second;
  169. }
  170. // Instances a single element.
  171. Element* Factory::InstanceElement(Element* parent, const String& instancer_name, const String& tag, const XMLAttributes& attributes)
  172. {
  173. ElementInstancer* instancer = GetElementInstancer(instancer_name);
  174. if (instancer)
  175. {
  176. Element* element = instancer->InstanceElement(parent, tag, attributes);
  177. // Process the generic attributes and bind any events
  178. if (element)
  179. {
  180. element->SetInstancer(instancer);
  181. element->SetAttributes(attributes);
  182. ElementUtilities::BindEventAttributes(element);
  183. PluginRegistry::NotifyElementCreate(element);
  184. }
  185. return element;
  186. }
  187. return NULL;
  188. }
  189. // Instances a single text element containing a string.
  190. bool Factory::InstanceElementText(Element* parent, const String& text)
  191. {
  192. SystemInterface* system_interface = GetSystemInterface();
  193. // Do any necessary translation. If any substitutions were made then new XML may have been introduced, so we'll
  194. // have to run the data through the XML parser again.
  195. String translated_data;
  196. if (system_interface != NULL &&
  197. (system_interface->TranslateString(translated_data, text) > 0 ||
  198. translated_data.find("<") != String::npos))
  199. {
  200. StreamMemory* stream = new StreamMemory(translated_data.size() + 32);
  201. stream->Write("<body>", 6);
  202. stream->Write(translated_data);
  203. stream->Write("</body>", 7);
  204. stream->Seek(0, SEEK_SET);
  205. InstanceElementStream(parent, stream);
  206. stream->RemoveReference();
  207. }
  208. else
  209. {
  210. // Check if this text node contains only white-space; if so, we don't want to construct it.
  211. bool only_white_space = true;
  212. for (size_t i = 0; i < translated_data.size(); ++i)
  213. {
  214. if (!StringUtilities::IsWhitespace(translated_data[i]))
  215. {
  216. only_white_space = false;
  217. break;
  218. }
  219. }
  220. if (only_white_space)
  221. return true;
  222. // Attempt to instance the element.
  223. XMLAttributes attributes;
  224. Element* element = Factory::InstanceElement(parent, "#text", "#text", attributes);
  225. if (!element)
  226. {
  227. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s', instancer returned NULL.", translated_data.c_str());
  228. return false;
  229. }
  230. // Assign the element its text value.
  231. ElementText* text_element = dynamic_cast< ElementText* >(element);
  232. if (text_element == NULL)
  233. {
  234. 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());
  235. element->RemoveReference();
  236. return false;
  237. }
  238. text_element->SetText(ToWideString(translated_data));
  239. // Add to active node.
  240. parent->AppendChild(element);
  241. element->RemoveReference();
  242. }
  243. return true;
  244. }
  245. // Instances a element tree based on the stream
  246. bool Factory::InstanceElementStream(Element* parent, Stream* stream)
  247. {
  248. XMLParser parser(parent);
  249. parser.Parse(stream);
  250. return true;
  251. }
  252. // Instances a element tree based on the stream
  253. ElementDocument* Factory::InstanceDocumentStream(Rml::Core::Context* context, Stream* stream)
  254. {
  255. Element* element = Factory::InstanceElement(NULL, "body", "body", XMLAttributes());
  256. if (!element)
  257. {
  258. Log::Message(Log::LT_ERROR, "Failed to instance document, instancer returned NULL.");
  259. return NULL;
  260. }
  261. ElementDocument* document = dynamic_cast< ElementDocument* >(element);
  262. if (!document)
  263. {
  264. Log::Message(Log::LT_ERROR, "Failed to instance document element. Found type '%s', was expecting derivative of ElementDocument.", typeid(element).name());
  265. return NULL;
  266. }
  267. document->context = context;
  268. XMLParser parser(element);
  269. parser.Parse(stream);
  270. return document;
  271. }
  272. // Registers an instancer that will be used to instance decorators.
  273. void Factory::RegisterDecoratorInstancer(const String& name, std::unique_ptr<DecoratorInstancer> instancer)
  274. {
  275. if (!instancer)
  276. return;
  277. String lower_case_name = ToLower(name);
  278. decorator_instancers[lower_case_name] = std::move(instancer);
  279. }
  280. // Retrieves a decorator instancer registered with the factory.
  281. DecoratorInstancer* Factory::GetDecoratorInstancer(const String& name)
  282. {
  283. auto iterator = decorator_instancers.find(name);
  284. if (iterator == decorator_instancers.end())
  285. return nullptr;
  286. return iterator->second.get();
  287. }
  288. // Registers an instancer that will be used to instance font effects.
  289. FontEffectInstancer* Factory::RegisterFontEffectInstancer(const String& name, FontEffectInstancer* instancer)
  290. {
  291. String lower_case_name = ToLower(name);
  292. instancer->AddReference();
  293. // Check if an instancer for this tag is already defined. If so, release it.
  294. FontEffectInstancerMap::iterator iterator = font_effect_instancers.find(lower_case_name);
  295. if (iterator != font_effect_instancers.end())
  296. (*iterator).second->RemoveReference();
  297. font_effect_instancers[lower_case_name] = instancer;
  298. return instancer;
  299. }
  300. // Attempts to instance a font effect from an instancer registered with the factory.
  301. FontEffect* Factory::InstanceFontEffect(const String& name, const PropertyDictionary& properties)
  302. {
  303. bool set_colour = false;
  304. Colourb colour(255, 255, 255);
  305. bool set_z_index = false;
  306. float z_index = 0;
  307. int specificity = -1;
  308. FontEffectInstancerMap::iterator iterator = font_effect_instancers.find(name);
  309. if (iterator == font_effect_instancers.end())
  310. return NULL;
  311. FontEffectInstancer* instancer = iterator->second;
  312. // Turn the generic, un-parsed properties we've got into a properly parsed dictionary.
  313. const PropertySpecification& property_specification = (*iterator).second->GetPropertySpecification();
  314. PropertyDictionary parsed_properties;
  315. for (PropertyMap::const_iterator i = properties.GetProperties().begin(); i != properties.GetProperties().end(); ++i)
  316. {
  317. specificity = Math::Max(specificity, i->second.specificity);
  318. // Check for the 'z-index' property; we don't want to send this through.
  319. //if (i->first == Z_INDEX)
  320. //{
  321. // set_z_index = true;
  322. // z_index = i->second.value.Get< float >();
  323. //}
  324. //else if (i->first == COLOR)
  325. //{
  326. // static PropertyParserColour colour_parser;
  327. // Property colour_property;
  328. // if (colour_parser.ParseValue(colour_property, i->second.value.Get< String >(), ParameterMap()))
  329. // {
  330. // colour = colour_property.value.Get< Colourb >();
  331. // set_colour = true;
  332. // }
  333. //}
  334. //else
  335. {
  336. property_specification.ParsePropertyDeclaration(parsed_properties, (*i).first, (*i).second.value.Get< String >(), (*i).second.source, (*i).second.source_line_number);
  337. }
  338. }
  339. // Set the property defaults for all unset properties.
  340. property_specification.SetPropertyDefaults(parsed_properties);
  341. // Compile an ordered list of the values of the properties used to generate the effect's
  342. // textures and geometry.
  343. typedef std::list< std::pair< PropertyId, String > > GenerationPropertyList;
  344. GenerationPropertyList generation_properties;
  345. for (PropertyMap::const_iterator i = parsed_properties.GetProperties().begin(); i != parsed_properties.GetProperties().end(); ++i)
  346. {
  347. if (instancer->volatile_properties.find(i->first) != instancer->volatile_properties.end())
  348. {
  349. GenerationPropertyList::iterator j = generation_properties.begin();
  350. while (j != generation_properties.end() &&
  351. j->first < i->first)
  352. ++j;
  353. generation_properties.insert(j, GenerationPropertyList::value_type(i->first, i->second.value.Get< String >()));
  354. }
  355. }
  356. String generation_key;
  357. for (GenerationPropertyList::iterator i = generation_properties.begin(); i != generation_properties.end(); ++i)
  358. {
  359. generation_key += i->second;
  360. generation_key += ";";
  361. }
  362. // Now we can actually instance the effect!
  363. FontEffect* font_effect = (*iterator).second->InstanceFontEffect(name, parsed_properties);
  364. if (font_effect == NULL)
  365. return NULL;
  366. font_effect->name = name;
  367. font_effect->generation_key = generation_key;
  368. if (set_z_index)
  369. font_effect->SetZIndex(z_index);
  370. if (set_colour)
  371. font_effect->SetColour(colour);
  372. font_effect->SetSpecificity(specificity);
  373. font_effect->instancer = (*iterator).second;
  374. return font_effect;
  375. }
  376. // Creates a style sheet containing the passed in styles.
  377. StyleSheet* Factory::InstanceStyleSheetString(const String& string)
  378. {
  379. StreamMemory* memory_stream = new StreamMemory((const byte*) string.c_str(), string.size());
  380. StyleSheet* style_sheet = InstanceStyleSheetStream(memory_stream);
  381. memory_stream->RemoveReference();
  382. return style_sheet;
  383. }
  384. // Creates a style sheet from a file.
  385. StyleSheet* Factory::InstanceStyleSheetFile(const String& file_name)
  386. {
  387. StreamFile* file_stream = new StreamFile();
  388. file_stream->Open(file_name);
  389. StyleSheet* style_sheet = InstanceStyleSheetStream(file_stream);
  390. file_stream->RemoveReference();
  391. return style_sheet;
  392. }
  393. // Creates a style sheet from an Stream.
  394. StyleSheet* Factory::InstanceStyleSheetStream(Stream* stream)
  395. {
  396. StyleSheet* style_sheet = new StyleSheet();
  397. if (style_sheet->LoadStyleSheet(stream))
  398. {
  399. return style_sheet;
  400. }
  401. style_sheet->RemoveReference();
  402. return NULL;
  403. }
  404. // Clears the style sheet cache. This will force style sheets to be reloaded.
  405. void Factory::ClearStyleSheetCache()
  406. {
  407. StyleSheetFactory::ClearStyleSheetCache();
  408. }
  409. /// Clears the template cache. This will force templates to be reloaded.
  410. void Factory::ClearTemplateCache()
  411. {
  412. TemplateCache::Clear();
  413. }
  414. // Registers an instancer for all RmlEvents
  415. EventInstancer* Factory::RegisterEventInstancer(EventInstancer* instancer)
  416. {
  417. instancer->AddReference();
  418. if (event_instancer)
  419. event_instancer->RemoveReference();
  420. event_instancer = instancer;
  421. return instancer;
  422. }
  423. // Instance an event object.
  424. Event* Factory::InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible)
  425. {
  426. Event* event = event_instancer->InstanceEvent(target, id, type, parameters, interruptible);
  427. if (event != NULL)
  428. event->instancer = event_instancer;
  429. return event;
  430. }
  431. // Register an instancer for all event listeners
  432. EventListenerInstancer* Factory::RegisterEventListenerInstancer(EventListenerInstancer* instancer)
  433. {
  434. instancer->AddReference();
  435. if (event_listener_instancer)
  436. event_listener_instancer->RemoveReference();
  437. event_listener_instancer = instancer;
  438. return instancer;
  439. }
  440. // Instance an event listener with the given string
  441. EventListener* Factory::InstanceEventListener(const String& value, Element* element)
  442. {
  443. // If we have an event listener instancer, use it
  444. if (event_listener_instancer)
  445. return event_listener_instancer->InstanceEventListener(value, element);
  446. return NULL;
  447. }
  448. }
  449. }