StyleSheetFactory.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "StyleSheetFactory.h"
  29. #include "../../Include/Rocket/Core/StyleSheet.h"
  30. #include "StreamFile.h"
  31. #include "StyleSheetNodeSelectorNthChild.h"
  32. #include "StyleSheetNodeSelectorNthLastChild.h"
  33. #include "StyleSheetNodeSelectorNthOfType.h"
  34. #include "StyleSheetNodeSelectorNthLastOfType.h"
  35. #include "StyleSheetNodeSelectorFirstChild.h"
  36. #include "StyleSheetNodeSelectorLastChild.h"
  37. #include "StyleSheetNodeSelectorFirstOfType.h"
  38. #include "StyleSheetNodeSelectorLastOfType.h"
  39. #include "StyleSheetNodeSelectorOnlyChild.h"
  40. #include "StyleSheetNodeSelectorOnlyOfType.h"
  41. #include "StyleSheetNodeSelectorEmpty.h"
  42. #include "../../Include/Rocket/Core/Log.h"
  43. namespace Rocket {
  44. namespace Core {
  45. static StyleSheetFactory* instance = NULL;
  46. StyleSheetFactory::StyleSheetFactory()
  47. {
  48. ROCKET_ASSERT(instance == NULL);
  49. instance = this;
  50. }
  51. StyleSheetFactory::~StyleSheetFactory()
  52. {
  53. instance = NULL;
  54. }
  55. bool StyleSheetFactory::Initialise()
  56. {
  57. new StyleSheetFactory();
  58. instance->selectors["nth-child"] = new StyleSheetNodeSelectorNthChild();
  59. instance->selectors["nth-last-child"] = new StyleSheetNodeSelectorNthLastChild();
  60. instance->selectors["nth-of-type"] = new StyleSheetNodeSelectorNthOfType();
  61. instance->selectors["nth-last-of-type"] = new StyleSheetNodeSelectorNthLastOfType();
  62. instance->selectors["first-child"] = new StyleSheetNodeSelectorFirstChild();
  63. instance->selectors["last-child"] = new StyleSheetNodeSelectorLastChild();
  64. instance->selectors["first-of-type"] = new StyleSheetNodeSelectorFirstOfType();
  65. instance->selectors["last-of-type"] = new StyleSheetNodeSelectorLastOfType();
  66. instance->selectors["only-child"] = new StyleSheetNodeSelectorOnlyChild();
  67. instance->selectors["only-of-type"] = new StyleSheetNodeSelectorOnlyOfType();
  68. instance->selectors["empty"] = new StyleSheetNodeSelectorEmpty();
  69. return true;
  70. }
  71. void StyleSheetFactory::Shutdown()
  72. {
  73. if (instance != NULL)
  74. {
  75. ClearStyleSheetCache();
  76. for (SelectorMap::iterator i = instance->selectors.begin(); i != instance->selectors.end(); ++i)
  77. delete (*i).second;
  78. delete instance;
  79. }
  80. }
  81. StyleSheet* StyleSheetFactory::GetStyleSheet(const String& sheet_name)
  82. {
  83. // Look up the sheet definition in the cache
  84. StyleSheets::iterator itr = instance->stylesheets.find(sheet_name);
  85. if (itr != instance->stylesheets.end())
  86. {
  87. (*itr).second->AddReference();
  88. return (*itr).second;
  89. }
  90. // Don't currently have the sheet, attempt to load it
  91. StyleSheet* sheet = instance->LoadStyleSheet(sheet_name);
  92. if (sheet == NULL)
  93. return NULL;
  94. // Add it to the cache, and add a reference count so the cache will keep hold of it.
  95. instance->stylesheets[sheet_name] = sheet;
  96. sheet->AddReference();
  97. return sheet;
  98. }
  99. StyleSheet* StyleSheetFactory::GetStyleSheet(const StringList& sheets)
  100. {
  101. // Generate a unique key for these sheets
  102. String combined_key;
  103. for (size_t i = 0; i < sheets.size(); i++)
  104. {
  105. URL path(sheets[i]);
  106. combined_key += path.GetFileName();
  107. }
  108. // Look up the sheet definition in the cache.
  109. StyleSheets::iterator itr = instance->stylesheet_cache.find(combined_key);
  110. if (itr != instance->stylesheet_cache.end())
  111. {
  112. (*itr).second->AddReference();
  113. return (*itr).second;
  114. }
  115. // Load and combine the sheets.
  116. StyleSheet* sheet = NULL;
  117. for (size_t i = 0; i < sheets.size(); i++)
  118. {
  119. StyleSheet* sub_sheet = GetStyleSheet(sheets[i]);
  120. if (sub_sheet)
  121. {
  122. if (sheet)
  123. {
  124. StyleSheet* new_sheet = sheet->CombineStyleSheet(sub_sheet);
  125. sheet->RemoveReference();
  126. sub_sheet->RemoveReference();
  127. sheet = new_sheet;
  128. }
  129. else
  130. sheet = sub_sheet;
  131. }
  132. else
  133. Log::Message(Log::LT_ERROR, "Failed to load style sheet %s.", sheets[i].CString());
  134. }
  135. if (sheet == NULL)
  136. return NULL;
  137. // Add to cache, and a reference to the sheet to hold it in the cache.
  138. instance->stylesheet_cache[combined_key] = sheet;
  139. sheet->AddReference();
  140. return sheet;
  141. }
  142. // Clear the style sheet cache.
  143. void StyleSheetFactory::ClearStyleSheetCache()
  144. {
  145. for (StyleSheets::iterator i = instance->stylesheets.begin(); i != instance->stylesheets.end(); ++i)
  146. (*i).second->RemoveReference();
  147. for (StyleSheets::iterator i = instance->stylesheet_cache.begin(); i != instance->stylesheet_cache.end(); ++i)
  148. (*i).second->RemoveReference();
  149. instance->stylesheets.clear();
  150. instance->stylesheet_cache.clear();
  151. }
  152. // Returns one of the available node selectors.
  153. StyleSheetNodeSelector* StyleSheetFactory::GetSelector(const String& name)
  154. {
  155. size_t index = name.Find("(");
  156. SelectorMap::iterator i = instance->selectors.find(name.Substring(0, index));
  157. if (i == instance->selectors.end())
  158. return NULL;
  159. return (*i).second;
  160. }
  161. StyleSheet* StyleSheetFactory::LoadStyleSheet(const String& sheet)
  162. {
  163. StyleSheet* new_style_sheet = NULL;
  164. // Open stream, construct new sheet and pass the stream into the sheet
  165. // TODO: Make this support ASYNC
  166. StreamFile* stream = new StreamFile();
  167. if (stream->Open(sheet))
  168. {
  169. new_style_sheet = new StyleSheet();
  170. if (!new_style_sheet->LoadStyleSheet(stream))
  171. {
  172. new_style_sheet->RemoveReference();
  173. new_style_sheet = NULL;
  174. }
  175. }
  176. stream->RemoveReference();
  177. return new_style_sheet;
  178. }
  179. }
  180. }