Factory.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include <Rocket/Core.h>
  29. #include <Rocket/Core/StreamMemory.h>
  30. #include "ContextInstancerDefault.h"
  31. #include "DecoratorNoneInstancer.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 "XMLNodeHandlerBody.h"
  48. #include "XMLNodeHandlerDefault.h"
  49. #include "XMLNodeHandlerHead.h"
  50. #include "XMLNodeHandlerTemplate.h"
  51. #include "XMLParseTools.h"
  52. namespace Rocket {
  53. namespace Core {
  54. // Element instancers.
  55. typedef std::map< String, ElementInstancer* > ElementInstancerMap;
  56. static ElementInstancerMap element_instancers;
  57. // Decorator instancers.
  58. typedef std::map< String, DecoratorInstancer* > DecoratorInstancerMap;
  59. static DecoratorInstancerMap decorator_instancers;
  60. // Font effect instancers.
  61. typedef std::map< String, FontEffectInstancer* > FontEffectInstancerMap;
  62. static FontEffectInstancerMap font_effect_instancers;
  63. // The context instancer.
  64. static ContextInstancer* context_instancer = NULL;
  65. // The event instancer
  66. static EventInstancer* event_instancer = NULL;
  67. // Event listener instancer.
  68. static EventListenerInstancer* event_listener_instancer = NULL;
  69. Factory::Factory()
  70. {
  71. }
  72. Factory::~Factory()
  73. {
  74. }
  75. bool Factory::Initialise()
  76. {
  77. // Bind the default context instancer.
  78. if (context_instancer == NULL)
  79. context_instancer = new ContextInstancerDefault();
  80. // Bind default event instancer
  81. if (event_instancer == NULL)
  82. event_instancer = new EventInstancerDefault();
  83. // No default event listener instancer
  84. if (event_listener_instancer == NULL)
  85. event_listener_instancer = NULL;
  86. // Bind the default element instancers
  87. RegisterElementInstancer("*", new ElementInstancerGeneric< Element >())->RemoveReference();
  88. RegisterElementInstancer("img", new ElementInstancerGeneric< ElementImage >())->RemoveReference();
  89. RegisterElementInstancer("#text", new ElementInstancerGeneric< ElementTextDefault >())->RemoveReference();
  90. RegisterElementInstancer("handle", new ElementInstancerGeneric< ElementHandle >())->RemoveReference();
  91. RegisterElementInstancer("body", new ElementInstancerGeneric< ElementDocument >())->RemoveReference();
  92. // Bind the default decorator instancers
  93. RegisterDecoratorInstancer("tiled-horizontal", new DecoratorTiledHorizontalInstancer())->RemoveReference();
  94. RegisterDecoratorInstancer("tiled-vertical", new DecoratorTiledVerticalInstancer())->RemoveReference();
  95. RegisterDecoratorInstancer("tiled-box", new DecoratorTiledBoxInstancer())->RemoveReference();
  96. RegisterDecoratorInstancer("image", new DecoratorTiledImageInstancer())->RemoveReference();
  97. RegisterDecoratorInstancer("none", new DecoratorNoneInstancer())->RemoveReference();
  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. for (DecoratorInstancerMap::iterator i = decorator_instancers.begin(); i != decorator_instancers.end(); ++i)
  113. (*i).second->RemoveReference();
  114. for (FontEffectInstancerMap::iterator i = font_effect_instancers.begin(); i != font_effect_instancers.end(); ++i)
  115. (*i).second->RemoveReference();
  116. if (context_instancer)
  117. context_instancer->RemoveReference();
  118. if (event_listener_instancer)
  119. event_listener_instancer->RemoveReference();
  120. if (event_instancer)
  121. event_instancer->RemoveReference();
  122. XMLParser::ReleaseHandlers();
  123. }
  124. // Registers the instancer to use when instancing contexts.
  125. ContextInstancer* Factory::RegisterContextInstancer(ContextInstancer* instancer)
  126. {
  127. instancer->AddReference();
  128. if (context_instancer != NULL)
  129. context_instancer->RemoveReference();
  130. context_instancer = instancer;
  131. return instancer;
  132. }
  133. // Instances a new context.
  134. Context* Factory::InstanceContext(const String& name)
  135. {
  136. Context* new_context = context_instancer->InstanceContext(name);
  137. if (new_context != NULL)
  138. new_context->SetInstancer(context_instancer);
  139. return new_context;
  140. }
  141. ElementInstancer* Factory::RegisterElementInstancer(const String& name, ElementInstancer* instancer)
  142. {
  143. String lower_case_name = name.ToLower();
  144. instancer->AddReference();
  145. // Check if an instancer for this tag is already defined, if so release it
  146. ElementInstancerMap::iterator itr = element_instancers.find(lower_case_name);
  147. if (itr != element_instancers.end())
  148. {
  149. (*itr).second->RemoveReference();
  150. }
  151. element_instancers[lower_case_name] = instancer;
  152. return instancer;
  153. }
  154. // Looks up the instancer for the given element
  155. ElementInstancer* Factory::GetElementInstancer(const String& tag)
  156. {
  157. ElementInstancerMap::iterator instancer_iterator = element_instancers.find(tag);
  158. if (instancer_iterator == element_instancers.end())
  159. {
  160. instancer_iterator = element_instancers.find("*");
  161. if (instancer_iterator == element_instancers.end())
  162. return NULL;
  163. }
  164. return (*instancer_iterator).second;
  165. }
  166. // Instances a single element.
  167. Element* Factory::InstanceElement(Element* parent, const String& instancer_name, const String& tag, const XMLAttributes& attributes)
  168. {
  169. ElementInstancer* instancer = GetElementInstancer(instancer_name);
  170. if (instancer)
  171. {
  172. Element* element = instancer->InstanceElement(parent, tag, attributes);
  173. // Process the generic attributes and bind any events
  174. if (element)
  175. {
  176. element->SetInstancer(instancer);
  177. element->SetAttributes(&attributes);
  178. ElementUtilities::BindEventAttributes(element);
  179. PluginRegistry::NotifyElementCreate(element);
  180. }
  181. return element;
  182. }
  183. return NULL;
  184. }
  185. // Instances a single text element containing a string.
  186. bool Factory::InstanceElementText(Element* parent, const String& text)
  187. {
  188. SystemInterface* system_interface = GetSystemInterface();
  189. // Do any necessary translation. If any substitutions were made then new XML may have been introduced, so we'll
  190. // have to run the data through the XML parser again.
  191. String translated_data;
  192. if (system_interface != NULL &&
  193. (system_interface->TranslateString(translated_data, text) > 0 ||
  194. translated_data.Find("<") != String::npos))
  195. {
  196. StreamMemory* stream = new StreamMemory(translated_data.Length() + 32);
  197. stream->Write("<body>", 6);
  198. stream->Write(translated_data);
  199. stream->Write("</body>", 7);
  200. stream->Seek(0, SEEK_SET);
  201. InstanceElementStream(parent, stream);
  202. stream->RemoveReference();
  203. }
  204. else
  205. {
  206. // Check if this text node contains only white-space; if so, we don't want to construct it.
  207. bool only_white_space = true;
  208. for (size_t i = 0; i < translated_data.Length(); ++i)
  209. {
  210. if (!StringUtilities::IsWhitespace(translated_data[i]))
  211. {
  212. only_white_space = false;
  213. break;
  214. }
  215. }
  216. if (only_white_space)
  217. return true;
  218. // Attempt to instance the element.
  219. XMLAttributes attributes;
  220. Element* element = Factory::InstanceElement(parent, "#text", "#text", attributes);
  221. if (!element)
  222. {
  223. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s', instancer returned NULL.", translated_data.CString());
  224. return false;
  225. }
  226. // Assign the element its text value.
  227. ElementText* text_element = dynamic_cast< ElementText* >(element);
  228. if (text_element == NULL)
  229. {
  230. 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());
  231. element->RemoveReference();
  232. return false;
  233. }
  234. text_element->SetText(translated_data);
  235. // Add to active node.
  236. parent->AppendChild(element);
  237. element->RemoveReference();
  238. }
  239. return true;
  240. }
  241. // Instances a element tree based on the stream
  242. bool Factory::InstanceElementStream(Element* parent, Stream* stream)
  243. {
  244. XMLParser parser(parent);
  245. parser.Parse(stream);
  246. return true;
  247. }
  248. // Instances a element tree based on the stream
  249. ElementDocument* Factory::InstanceDocumentStream(Rocket::Core::Context* context, Stream* stream)
  250. {
  251. Element* element = Factory::InstanceElement(NULL, "body", "body", XMLAttributes());
  252. if (!element)
  253. {
  254. Log::Message(Log::LT_ERROR, "Failed to instance document, instancer returned NULL.");
  255. return NULL;
  256. }
  257. ElementDocument* document = dynamic_cast< ElementDocument* >(element);
  258. if (!document)
  259. {
  260. Log::Message(Log::LT_ERROR, "Failed to instance document element. Found type '%s', was expecting derivative of ElementDocument.", typeid(element).name());
  261. return NULL;
  262. }
  263. document->lock_layout = true;
  264. document->context = context;
  265. XMLParser parser(element);
  266. parser.Parse(stream);
  267. document->lock_layout = false;
  268. return document;
  269. }
  270. // Registers an instancer that will be used to instance decorators.
  271. DecoratorInstancer* Factory::RegisterDecoratorInstancer(const String& name, DecoratorInstancer* instancer)
  272. {
  273. String lower_case_name = name.ToLower();
  274. instancer->AddReference();
  275. // Check if an instancer for this tag is already defined. If so, release it.
  276. DecoratorInstancerMap::iterator iterator = decorator_instancers.find(lower_case_name);
  277. if (iterator != decorator_instancers.end())
  278. (*iterator).second->RemoveReference();
  279. decorator_instancers[lower_case_name] = instancer;
  280. return instancer;
  281. }
  282. // Attempts to instance a decorator from an instancer registered with the factory.
  283. Decorator* Factory::InstanceDecorator(const String& name, const PropertyDictionary& properties)
  284. {
  285. float z_index = 0;
  286. int specificity = -1;
  287. DecoratorInstancerMap::iterator iterator = decorator_instancers.find(name);
  288. if (iterator == decorator_instancers.end())
  289. return NULL;
  290. // Turn the generic, un-parsed properties we've got into a properly parsed dictionary.
  291. const PropertySpecification& property_specification = (*iterator).second->GetPropertySpecification();
  292. PropertyDictionary parsed_properties;
  293. for (PropertyMap::const_iterator i = properties.GetProperties().begin(); i != properties.GetProperties().end(); ++i)
  294. {
  295. specificity = Math::Max(specificity, (*i).second.specificity);
  296. // Check for the 'z-index' property; we don't want to send this through.
  297. if ((*i).first == Z_INDEX)
  298. z_index = (*i).second.value.Get< float >();
  299. else
  300. property_specification.ParsePropertyDeclaration(parsed_properties, (*i).first, (*i).second.value.Get< String >(), (*i).second.source, (*i).second.source_line_number);
  301. }
  302. // Set the property defaults for all unset properties.
  303. property_specification.SetPropertyDefaults(parsed_properties);
  304. Decorator* decorator = (*iterator).second->InstanceDecorator(name, parsed_properties);
  305. if (decorator == NULL)
  306. return NULL;
  307. decorator->SetZIndex(z_index);
  308. decorator->SetSpecificity(specificity);
  309. decorator->instancer = (*iterator).second;
  310. return decorator;
  311. }
  312. // Registers an instancer that will be used to instance font effects.
  313. FontEffectInstancer* Factory::RegisterFontEffectInstancer(const String& name, FontEffectInstancer* instancer)
  314. {
  315. String lower_case_name = name.ToLower();
  316. instancer->AddReference();
  317. // Check if an instancer for this tag is already defined. If so, release it.
  318. FontEffectInstancerMap::iterator iterator = font_effect_instancers.find(lower_case_name);
  319. if (iterator != font_effect_instancers.end())
  320. (*iterator).second->RemoveReference();
  321. font_effect_instancers[lower_case_name] = instancer;
  322. return instancer;
  323. }
  324. // Attempts to instance a font effect from an instancer registered with the factory.
  325. FontEffect* Factory::InstanceFontEffect(const String& name, const PropertyDictionary& properties)
  326. {
  327. bool set_colour = false;
  328. Colourb colour(255, 255, 255);
  329. bool set_z_index = false;
  330. float z_index = 0;
  331. int specificity = -1;
  332. FontEffectInstancerMap::iterator iterator = font_effect_instancers.find(name);
  333. if (iterator == font_effect_instancers.end())
  334. return NULL;
  335. FontEffectInstancer* instancer = iterator->second;
  336. // Turn the generic, un-parsed properties we've got into a properly parsed dictionary.
  337. const PropertySpecification& property_specification = (*iterator).second->GetPropertySpecification();
  338. PropertyDictionary parsed_properties;
  339. for (PropertyMap::const_iterator i = properties.GetProperties().begin(); i != properties.GetProperties().end(); ++i)
  340. {
  341. specificity = Math::Max(specificity, i->second.specificity);
  342. // Check for the 'z-index' property; we don't want to send this through.
  343. if (i->first == Z_INDEX)
  344. {
  345. set_z_index = true;
  346. z_index = i->second.value.Get< float >();
  347. }
  348. else if (i->first == COLOR)
  349. {
  350. static PropertyParserColour colour_parser;
  351. Property colour_property;
  352. if (colour_parser.ParseValue(colour_property, i->second.value.Get< String >(), ParameterMap()))
  353. {
  354. colour = colour_property.value.Get< Colourb >();
  355. set_colour = true;
  356. }
  357. }
  358. else
  359. {
  360. property_specification.ParsePropertyDeclaration(parsed_properties, (*i).first, (*i).second.value.Get< String >(), (*i).second.source, (*i).second.source_line_number);
  361. }
  362. }
  363. // Set the property defaults for all unset properties.
  364. property_specification.SetPropertyDefaults(parsed_properties);
  365. // Compile an ordered list of the values of the properties used to generate the effect's
  366. // textures and geometry.
  367. typedef std::list< std::pair< String, String > > GenerationPropertyList;
  368. GenerationPropertyList generation_properties;
  369. for (PropertyMap::const_iterator i = parsed_properties.GetProperties().begin(); i != parsed_properties.GetProperties().end(); ++i)
  370. {
  371. if (instancer->volatile_properties.find(i->first) != instancer->volatile_properties.end())
  372. {
  373. GenerationPropertyList::iterator j = generation_properties.begin();
  374. while (j != generation_properties.end() &&
  375. j->first < i->first)
  376. ++j;
  377. generation_properties.insert(j, GenerationPropertyList::value_type(i->first, i->second.value.Get< String >()));
  378. }
  379. }
  380. String generation_key;
  381. for (GenerationPropertyList::iterator i = generation_properties.begin(); i != generation_properties.end(); ++i)
  382. {
  383. generation_key += i->second;
  384. generation_key += ";";
  385. }
  386. // Now we can actually instance the effect!
  387. FontEffect* font_effect = (*iterator).second->InstanceFontEffect(name, parsed_properties);
  388. if (font_effect == NULL)
  389. return NULL;
  390. font_effect->name = name;
  391. font_effect->generation_key = generation_key;
  392. if (set_z_index)
  393. font_effect->SetZIndex(z_index);
  394. if (set_colour)
  395. font_effect->SetColour(colour);
  396. font_effect->SetSpecificity(specificity);
  397. font_effect->instancer = (*iterator).second;
  398. return font_effect;
  399. }
  400. // Creates a style sheet containing the passed in styles.
  401. StyleSheet* Factory::InstanceStyleSheetString(const String& string)
  402. {
  403. StreamMemory* memory_stream = new StreamMemory((const byte*) string.CString(), string.Length());
  404. StyleSheet* style_sheet = InstanceStyleSheetStream(memory_stream);
  405. memory_stream->RemoveReference();
  406. return style_sheet;
  407. }
  408. // Creates a style sheet from a file.
  409. StyleSheet* Factory::InstanceStyleSheetFile(const String& file_name)
  410. {
  411. StreamFile* file_stream = new StreamFile();
  412. file_stream->Open(file_name);
  413. StyleSheet* style_sheet = InstanceStyleSheetStream(file_stream);
  414. file_stream->RemoveReference();
  415. return style_sheet;
  416. }
  417. // Creates a style sheet from an Stream.
  418. StyleSheet* Factory::InstanceStyleSheetStream(Stream* stream)
  419. {
  420. StyleSheet* style_sheet = new StyleSheet();
  421. if (style_sheet->LoadStyleSheet(stream))
  422. {
  423. return style_sheet;
  424. }
  425. style_sheet->RemoveReference();
  426. return NULL;
  427. }
  428. // Clears the style sheet cache. This will force style sheets to be reloaded.
  429. void Factory::ClearStyleSheetCache()
  430. {
  431. StyleSheetFactory::ClearStyleSheetCache();
  432. }
  433. // Registers an instancer for all RKTEvents
  434. EventInstancer* Factory::RegisterEventInstancer(EventInstancer* instancer)
  435. {
  436. instancer->AddReference();
  437. if (event_instancer)
  438. event_instancer->RemoveReference();
  439. event_instancer = instancer;
  440. return instancer;
  441. }
  442. // Instance an event object.
  443. Event* Factory::InstanceEvent(Element* target, const String& name, const Dictionary& parameters, bool interruptible)
  444. {
  445. Event* event = event_instancer->InstanceEvent(target, name, parameters, interruptible);
  446. if (event != NULL)
  447. event->instancer = event_instancer;
  448. return event;
  449. }
  450. // Register an instancer for all event listeners
  451. EventListenerInstancer* Factory::RegisterEventListenerInstancer(EventListenerInstancer* instancer)
  452. {
  453. instancer->AddReference();
  454. if (event_listener_instancer)
  455. event_listener_instancer->RemoveReference();
  456. event_listener_instancer = instancer;
  457. return instancer;
  458. }
  459. // Instance an event listener with the given string
  460. EventListener* Factory::InstanceEventListener(const String& value)
  461. {
  462. // If we have an event listener instancer, use it
  463. if (event_listener_instancer)
  464. return event_listener_instancer->InstanceEventListener(value);
  465. return NULL;
  466. }
  467. }
  468. }