ID.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. namespace Rocket {
  29. namespace Core {
  30. template <typename ID>
  31. class IdNameMap {
  32. std::vector<String> name_map; // IDs are indices into the NameMap
  33. UnorderedMap<String, ID> reverse_map;
  34. protected:
  35. IdNameMap(ID id_num_to_initialize) {
  36. name_map.resize((size_t)id_num_to_initialize);
  37. reverse_map.reserve((size_t)id_num_to_initialize);
  38. }
  39. void add(ID id, const String& name) {
  40. name_map[(size_t)id] = name;
  41. reverse_map.emplace(name, id);
  42. }
  43. void assert_all_inserted(ID id_num_to_initialize) const {
  44. ROCKET_ASSERT(name_map.size() == (size_t)id_num_to_initialize && reverse_map.size() == (size_t)id_num_to_initialize);
  45. }
  46. public:
  47. ID GetId(const String& name)
  48. {
  49. if (auto it = reverse_map.find(ToLower(name)); it != reverse_map.end())
  50. return it->second;
  51. return ID::Invalid;
  52. }
  53. const String& GetName(ID id)
  54. {
  55. if (static_cast<size_t>(id) < name_map.size())
  56. return name_map[static_cast<size_t>(id)];
  57. return name_map[static_cast<size_t>(ID::Invalid)];
  58. }
  59. ID CreateIdFromName(const String& name)
  60. {
  61. String lower = ToLower(name);
  62. ID next_id = static_cast<ID>(name_map.size());
  63. // Only insert if not already in list
  64. auto [it, inserted] = reverse_map.emplace(lower, next_id);
  65. if (inserted)
  66. name_map.push_back(lower);
  67. // Return the property id that already existed, or the new one if inserted
  68. return it->second;
  69. }
  70. };
  71. class PropertyIdNameMap : public IdNameMap<PropertyId>
  72. {
  73. public:
  74. PropertyIdNameMap();
  75. };
  76. inline PropertyIdNameMap::PropertyIdNameMap() : IdNameMap(PropertyId::NumDefinedIds)
  77. {
  78. add(PropertyId::Invalid, "invalid_property");
  79. add(PropertyId::MarginTop, "margin-top");
  80. add(PropertyId::MarginRight, "margin-right");
  81. add(PropertyId::MarginBottom, "margin-bottom");
  82. add(PropertyId::MarginLeft, "margin-left");
  83. add(PropertyId::Margin, "margin");
  84. add(PropertyId::PaddingTop, "padding-top");
  85. add(PropertyId::PaddingRight, "padding-right");
  86. add(PropertyId::PaddingBottom, "padding-bottom");
  87. add(PropertyId::PaddingLeft, "padding-left");
  88. add(PropertyId::Padding, "padding");
  89. add(PropertyId::BorderTopWidth, "border-top-width");
  90. add(PropertyId::BorderRightWidth, "border-right-width");
  91. add(PropertyId::BorderBottomWidth, "border-bottom-width");
  92. add(PropertyId::BorderLeftWidth, "border-left-width");
  93. add(PropertyId::BorderWidth, "border-width");
  94. add(PropertyId::BorderTopColor, "border-top-color");
  95. add(PropertyId::BorderRightColor, "border-right-color");
  96. add(PropertyId::BorderBottomColor, "border-bottom-color");
  97. add(PropertyId::BorderLeftColor, "border-left-color");
  98. add(PropertyId::BorderColor, "border-color");
  99. add(PropertyId::BorderTop, "border-top");
  100. add(PropertyId::BorderRight, "border-right");
  101. add(PropertyId::BorderBottom, "border-bottom");
  102. add(PropertyId::BorderLeft, "border-left");
  103. add(PropertyId::Border, "border");
  104. add(PropertyId::Display, "display");
  105. add(PropertyId::Position, "position");
  106. add(PropertyId::Top, "top");
  107. add(PropertyId::Right, "right");
  108. add(PropertyId::Bottom, "bottom");
  109. add(PropertyId::Left, "left");
  110. add(PropertyId::Float, "float");
  111. add(PropertyId::Clear, "clear");
  112. add(PropertyId::ZIndex, "z-index");
  113. add(PropertyId::Width, "width");
  114. add(PropertyId::MinWidth, "min-width");
  115. add(PropertyId::MaxWidth, "max-width");
  116. add(PropertyId::Height, "height");
  117. add(PropertyId::MinHeight, "min-height");
  118. add(PropertyId::MaxHeight, "max-height");
  119. add(PropertyId::LineHeight, "line-height");
  120. add(PropertyId::VerticalAlign, "vertical-align");
  121. add(PropertyId::Overflow, "overflow");
  122. add(PropertyId::OverflowX, "overflow-x");
  123. add(PropertyId::OverflowY, "overflow-y");
  124. add(PropertyId::Clip, "clip");
  125. add(PropertyId::Visibility, "visibility");
  126. add(PropertyId::BackgroundColor, "background-color");
  127. add(PropertyId::Background, "background");
  128. add(PropertyId::Color, "color");
  129. add(PropertyId::ImageColor, "image-color");
  130. add(PropertyId::FontFamily, "font-family");
  131. add(PropertyId::FontCharset, "font-charset");
  132. add(PropertyId::FontStyle, "font-style");
  133. add(PropertyId::FontWeight, "font-weight");
  134. add(PropertyId::FontSize, "font-size");
  135. add(PropertyId::Font, "font");
  136. add(PropertyId::TextAlign, "text-align");
  137. add(PropertyId::TextDecoration, "text-decoration");
  138. add(PropertyId::TextTransform, "text-transform");
  139. add(PropertyId::WhiteSpace, "white-space");
  140. add(PropertyId::Cursor, "cursor");
  141. add(PropertyId::Drag, "drag");
  142. add(PropertyId::TabIndex, "tab-index");
  143. add(PropertyId::ScrollbarMargin, "scrollbar-margin");
  144. add(PropertyId::Perspective, "perspective");
  145. add(PropertyId::PerspectiveOrigin, "perspective-origin");
  146. add(PropertyId::PerspectiveOriginX, "perspective-origin-x");
  147. add(PropertyId::PerspectiveOriginY, "perspective-origin-y");
  148. add(PropertyId::Transform, "transform");
  149. add(PropertyId::TransformOrigin, "transform-origin");
  150. add(PropertyId::TransformOriginX, "transform-origin-x");
  151. add(PropertyId::TransformOriginY, "transform-origin-y");
  152. add(PropertyId::TransformOriginZ, "transform-origin-z");
  153. add(PropertyId::None, "none");
  154. add(PropertyId::All, "all");
  155. add(PropertyId::Transition, "transition");
  156. add(PropertyId::Animation, "animation");
  157. add(PropertyId::Keyframes, "keyframes");
  158. add(PropertyId::ScrollDefaultStepSize, "scroll-default-step-size");
  159. add(PropertyId::Opacity, "opacity");
  160. add(PropertyId::PointerEvents, "pointer-events");
  161. add(PropertyId::Focus, "focus");
  162. assert_all_inserted(PropertyId::NumDefinedIds);
  163. }
  164. class EventIdNameMap : public IdNameMap<EventId>
  165. {
  166. public:
  167. EventIdNameMap();
  168. };
  169. inline EventIdNameMap::EventIdNameMap() : IdNameMap(EventId::NumDefinedIds)
  170. {
  171. add(EventId::Invalid, "invalid_event");
  172. add(EventId::Mousedown, "mousedown");
  173. add(EventId::Mousescroll, "mousescroll");
  174. add(EventId::Mouseover, "mouseover");
  175. add(EventId::Mouseout, "mouseout");
  176. add(EventId::Focus, "focus");
  177. add(EventId::Blur, "blur");
  178. add(EventId::Keydown, "keydown");
  179. add(EventId::Mouseup, "mouseup");
  180. add(EventId::Click, "click");
  181. add(EventId::Load, "load");
  182. add(EventId::Unload, "unload");
  183. add(EventId::Show, "show");
  184. add(EventId::Hide, "hide");
  185. add(EventId::Keyup, "keyup");
  186. add(EventId::Textinput, "textinput");
  187. add(EventId::Mousemove, "mousemove");
  188. add(EventId::Dragmove, "dragmove");
  189. add(EventId::Dblclick, "dblclick");
  190. add(EventId::Drag, "drag");
  191. add(EventId::Dragstart, "dragstart");
  192. add(EventId::Dragover, "dragover");
  193. add(EventId::Dragdrop, "dragdrop");
  194. add(EventId::Dragout, "dragout");
  195. add(EventId::Dragend, "dragend");
  196. add(EventId::Handledrag, "handledrag");
  197. add(EventId::Resize, "resize");
  198. add(EventId::Scroll, "scroll");
  199. add(EventId::Scrollchange, "scrollchange");
  200. add(EventId::Animationend, "animationend");
  201. add(EventId::Transitionend, "transitionend");
  202. assert_all_inserted(EventId::NumDefinedIds);
  203. }
  204. static PropertyIdNameMap property_map;
  205. PropertyId GetPropertyId(const String& property_name) {
  206. return property_map.GetId(property_name);
  207. }
  208. PropertyId CreateOrGetPropertyId(const String& name) {
  209. return property_map.CreateIdFromName(name);
  210. }
  211. const String& GetName(PropertyId property_id) {
  212. return property_map.GetName(property_id);
  213. }
  214. static EventIdNameMap event_map;
  215. EventId GetEventId(const String& event_name) {
  216. return event_map.GetId(event_name);
  217. }
  218. EventId CreateOrGetEventId(const String& name) {
  219. return event_map.CreateIdFromName(name);
  220. }
  221. const String& GetName(EventId event_id) {
  222. return event_map.GetName(event_id);
  223. }
  224. //
  225. //class PseudoIdNameMap : public IdNameMap<PseudoId>
  226. //{
  227. //public:
  228. // PseudoIdNameMap();
  229. //};
  230. //
  231. //inline PseudoIdNameMap::PseudoIdNameMap() : IdNameMap(PseudoId::NumDefinedIds)
  232. //{
  233. // add(PseudoId::Invalid, "invalid_pseudo_class");
  234. // add(PseudoId::Hover, "hover");
  235. // add(PseudoId::Active, "active");
  236. // add(PseudoId::Focus, "focus");
  237. // add(PseudoId::Dragged, "dragged");
  238. //
  239. // add(PseudoId::Disabled, "disabled");
  240. // add(PseudoId::Selected, "selected");
  241. // add(PseudoId::Checked, "checked");
  242. //
  243. // assert_all_inserted(PseudoId::NumDefinedIds);
  244. //}
  245. //
  246. //
  247. //static PseudoIdNameMap pseudo_map;
  248. //
  249. //PseudoId GetPseudoId(const String& event_name) {
  250. // return pseudo_map.GetId(event_name);
  251. //}
  252. //PseudoId GetOrCreatePseudoId(const String& name) {
  253. // return pseudo_map.CreateIdFromName(name);
  254. //}
  255. //const String& GetName(PseudoId pseudo_id) {
  256. // return pseudo_map.GetName(pseudo_id);
  257. //}
  258. }
  259. }