Factory.cpp 19 KB

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