PropertySpecification.cpp 16 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/PropertySpecification.h"
  30. #include "../../Include/RmlUi/Core/Log.h"
  31. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  32. #include "../../Include/RmlUi/Core/PropertyDictionary.h"
  33. #include "PropertyShorthandDefinition.h"
  34. #include "IdNameMap.h"
  35. namespace Rml {
  36. namespace Core {
  37. PropertySpecification::PropertySpecification(size_t reserve_num_properties, size_t reserve_num_shorthands) :
  38. // Increment reserve numbers by one because the 'invalid' property occupies the first element
  39. properties(reserve_num_properties + 1), shorthands(reserve_num_shorthands + 1),
  40. property_map(std::make_unique<PropertyIdNameMap>(reserve_num_properties + 1)), shorthand_map(std::make_unique<ShorthandIdNameMap>(reserve_num_shorthands + 1))
  41. {
  42. }
  43. PropertySpecification::~PropertySpecification()
  44. {
  45. }
  46. // Registers a property with a new definition.
  47. PropertyDefinition& PropertySpecification::RegisterProperty(const String& property_name, const String& default_value, bool inherited, bool forces_layout, PropertyId id)
  48. {
  49. if (id == PropertyId::Invalid)
  50. id = property_map->GetOrCreateId(property_name);
  51. else
  52. property_map->AddPair(id, property_name);
  53. size_t index = (size_t)id;
  54. RMLUI_ASSERT(index < (size_t)INT16_MAX);
  55. if (index < properties.size())
  56. {
  57. // We don't want to owerwrite an existing entry.
  58. if (properties[index])
  59. {
  60. Log::Message(Log::LT_ERROR, "While registering property '%s': The property is already registered, ignoring.", property_name.c_str());
  61. return *properties[index];
  62. }
  63. }
  64. else
  65. {
  66. // Resize vector to hold the new index
  67. properties.resize((index*3)/2 + 1);
  68. }
  69. // Create and insert the new property
  70. properties[index] = std::make_unique<PropertyDefinition>(id, default_value, inherited, forces_layout);
  71. property_names.Insert(id);
  72. if (inherited)
  73. inherited_property_names.Insert(id);
  74. if (forces_layout)
  75. properties_forcing_layout.Insert(id);
  76. return *properties[index];
  77. }
  78. // Returns a property definition.
  79. const PropertyDefinition* PropertySpecification::GetProperty(PropertyId id) const
  80. {
  81. if (id == PropertyId::Invalid || (size_t)id >= properties.size())
  82. return nullptr;
  83. return properties[(size_t)id].get();
  84. }
  85. const PropertyDefinition* PropertySpecification::GetProperty(const String& property_name) const
  86. {
  87. return GetProperty(property_map->GetId(property_name));
  88. }
  89. // Fetches a list of the names of all registered property definitions.
  90. const PropertyIdSet& PropertySpecification::GetRegisteredProperties(void) const
  91. {
  92. return property_names;
  93. }
  94. // Fetches a list of the names of all registered property definitions.
  95. const PropertyIdSet& PropertySpecification::GetRegisteredInheritedProperties(void) const
  96. {
  97. return inherited_property_names;
  98. }
  99. const PropertyIdSet& PropertySpecification::GetRegisteredPropertiesForcingLayout() const
  100. {
  101. return properties_forcing_layout;
  102. }
  103. // Registers a shorthand property definition.
  104. ShorthandId PropertySpecification::RegisterShorthand(const String& shorthand_name, const String& property_names, ShorthandType type, ShorthandId id)
  105. {
  106. if (id == ShorthandId::Invalid)
  107. id = shorthand_map->GetOrCreateId(shorthand_name);
  108. else
  109. shorthand_map->AddPair(id, shorthand_name);
  110. StringList property_list;
  111. StringUtilities::ExpandString(property_list, StringUtilities::ToLower(property_names));
  112. // Construct the new shorthand definition and resolve its properties.
  113. UniquePtr<ShorthandDefinition> property_shorthand(new ShorthandDefinition());
  114. for (const String& name : property_list)
  115. {
  116. ShorthandItem item;
  117. PropertyId property_id = property_map->GetId(name);
  118. if (property_id != PropertyId::Invalid)
  119. {
  120. // We have a valid property
  121. if (const PropertyDefinition* property = GetProperty(property_id))
  122. item = ShorthandItem(property_id, property);
  123. }
  124. else
  125. {
  126. // Otherwise, we must be a shorthand
  127. ShorthandId shorthand_id = shorthand_map->GetId(name);
  128. // Test for valid shorthand id. The recursive types (and only those) can hold other shorthands.
  129. if (shorthand_id != ShorthandId::Invalid && (type == ShorthandType::Recursive || type == ShorthandType::RecursiveCommaSeparated))
  130. {
  131. if (const ShorthandDefinition * shorthand = GetShorthand(shorthand_id))
  132. item = ShorthandItem(shorthand_id, shorthand);
  133. }
  134. }
  135. if (item.type == ShorthandItemType::Invalid)
  136. {
  137. Log::Message(Log::LT_ERROR, "Shorthand property '%s' was registered with invalid property '%s'.", shorthand_name.c_str(), name.c_str());
  138. return ShorthandId::Invalid;
  139. }
  140. property_shorthand->items.push_back(item);
  141. }
  142. property_shorthand->id = id;
  143. property_shorthand->type = type;
  144. const size_t index = (size_t)id;
  145. RMLUI_ASSERT(index < (size_t)INT16_MAX);
  146. if (index < shorthands.size())
  147. {
  148. // We don't want to owerwrite an existing entry.
  149. if (shorthands[index])
  150. {
  151. Log::Message(Log::LT_ERROR, "The shorthand '%s' already exists, ignoring.", shorthand_name.c_str());
  152. return ShorthandId::Invalid;
  153. }
  154. }
  155. else
  156. {
  157. // Resize vector to hold the new index
  158. shorthands.resize((index * 3) / 2 + 1);
  159. }
  160. shorthands[index] = std::move(property_shorthand);
  161. return id;
  162. }
  163. // Returns a shorthand definition.
  164. const ShorthandDefinition* PropertySpecification::GetShorthand(ShorthandId id) const
  165. {
  166. if (id == ShorthandId::Invalid || (size_t)id >= shorthands.size())
  167. return nullptr;
  168. return shorthands[(size_t)id].get();
  169. }
  170. const ShorthandDefinition* PropertySpecification::GetShorthand(const String& shorthand_name) const
  171. {
  172. return GetShorthand(shorthand_map->GetId(shorthand_name));
  173. }
  174. bool PropertySpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, const String& property_name, const String& property_value) const
  175. {
  176. // Try as a property first
  177. PropertyId property_id = property_map->GetId(property_name);
  178. if (property_id != PropertyId::Invalid)
  179. return ParsePropertyDeclaration(dictionary, property_id, property_value);
  180. // Then, as a shorthand
  181. ShorthandId shorthand_id = shorthand_map->GetId(property_name);
  182. if (shorthand_id != ShorthandId::Invalid)
  183. return ParseShorthandDeclaration(dictionary, shorthand_id, property_value);
  184. return false;
  185. }
  186. bool PropertySpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, PropertyId property_id, const String& property_value) const
  187. {
  188. // Parse as a single property.
  189. const PropertyDefinition* property_definition = GetProperty(property_id);
  190. if (!property_definition)
  191. return false;
  192. StringList property_values;
  193. if (!ParsePropertyValues(property_values, property_value, false) || property_values.size() == 0)
  194. return false;
  195. Property new_property;
  196. if (!property_definition->ParseValue(new_property, property_values[0]))
  197. return false;
  198. dictionary.SetProperty(property_id, new_property);
  199. return true;
  200. }
  201. // Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  202. bool PropertySpecification::ParseShorthandDeclaration(PropertyDictionary& dictionary, ShorthandId shorthand_id, const String& property_value) const
  203. {
  204. StringList property_values;
  205. if (!ParsePropertyValues(property_values, property_value, true) || property_values.size() == 0)
  206. return false;
  207. // Parse as a shorthand.
  208. const ShorthandDefinition* shorthand_definition = GetShorthand(shorthand_id);
  209. if (!shorthand_definition)
  210. return false;
  211. // If this definition is a 'box'-style shorthand (x-top, x-right, x-bottom, x-left, etc) and there are fewer
  212. // than four values
  213. if (shorthand_definition->type == ShorthandType::Box &&
  214. property_values.size() < 4)
  215. {
  216. // This array tells which property index each side is parsed from
  217. std::array<int, 4> box_side_to_value_index = { 0,0,0,0 };
  218. switch (property_values.size())
  219. {
  220. case 1:
  221. // Only one value is defined, so it is parsed onto all four sides.
  222. box_side_to_value_index = { 0,0,0,0 };
  223. break;
  224. case 2:
  225. // Two values are defined, so the first one is parsed onto the top and bottom value, the second onto
  226. // the left and right.
  227. box_side_to_value_index = { 0,1,0,1 };
  228. break;
  229. case 3:
  230. // Three values are defined, so the first is parsed into the top value, the second onto the left and
  231. // right, and the third onto the bottom.
  232. box_side_to_value_index = { 0,1,2,1 };
  233. break;
  234. default:
  235. RMLUI_ERROR;
  236. break;
  237. }
  238. for (int i = 0; i < 4; i++)
  239. {
  240. RMLUI_ASSERT(shorthand_definition->items[i].type == ShorthandItemType::Property);
  241. Property new_property;
  242. int value_index = box_side_to_value_index[i];
  243. if (!shorthand_definition->items[i].property_definition->ParseValue(new_property, property_values[value_index]))
  244. return false;
  245. dictionary.SetProperty(shorthand_definition->items[i].property_definition->GetId(), new_property);
  246. }
  247. }
  248. else if (shorthand_definition->type == ShorthandType::Recursive)
  249. {
  250. bool result = true;
  251. for (size_t i = 0; i < shorthand_definition->items.size(); i++)
  252. {
  253. const ShorthandItem& item = shorthand_definition->items[i];
  254. if (item.type == ShorthandItemType::Property)
  255. result &= ParsePropertyDeclaration(dictionary, item.property_id, property_value);
  256. else if (item.type == ShorthandItemType::Shorthand)
  257. result &= ParseShorthandDeclaration(dictionary, item.shorthand_id, property_value);
  258. else
  259. result = false;
  260. }
  261. if (!result)
  262. return false;
  263. }
  264. else if (shorthand_definition->type == ShorthandType::RecursiveCommaSeparated)
  265. {
  266. bool result = true;
  267. StringList subvalues;
  268. StringUtilities::ExpandString(subvalues, property_value);
  269. if (shorthand_definition->items.size() != subvalues.size())
  270. {
  271. // We must declare all subvalues
  272. return false;
  273. }
  274. for (size_t i = 0; i < shorthand_definition->items.size(); i++)
  275. {
  276. const ShorthandItem& item = shorthand_definition->items[i];
  277. if (item.type == ShorthandItemType::Property)
  278. result &= ParsePropertyDeclaration(dictionary, item.property_id, subvalues[i]);
  279. else if (item.type == ShorthandItemType::Shorthand)
  280. result &= ParseShorthandDeclaration(dictionary, item.shorthand_id, subvalues[i]);
  281. else
  282. result = false;
  283. }
  284. if (!result)
  285. return false;
  286. }
  287. else
  288. {
  289. size_t value_index = 0;
  290. size_t property_index = 0;
  291. for (; value_index < property_values.size() && property_index < shorthand_definition->items.size(); property_index++)
  292. {
  293. Property new_property;
  294. if (!shorthand_definition->items[property_index].property_definition->ParseValue(new_property, property_values[value_index]))
  295. {
  296. // This definition failed to parse; if we're falling through, try the next property. If there is no
  297. // next property, then abort!
  298. if (shorthand_definition->type == ShorthandType::FallThrough)
  299. {
  300. if (property_index + 1 < shorthand_definition->items.size())
  301. continue;
  302. }
  303. return false;
  304. }
  305. dictionary.SetProperty(shorthand_definition->items[property_index].property_id, new_property);
  306. // Increment the value index, unless we're replicating the last value and we're up to the last value.
  307. if (shorthand_definition->type != ShorthandType::Replicate ||
  308. value_index < property_values.size() - 1)
  309. value_index++;
  310. }
  311. }
  312. return true;
  313. }
  314. // Sets all undefined properties in the dictionary to their defaults.
  315. void PropertySpecification::SetPropertyDefaults(PropertyDictionary& dictionary) const
  316. {
  317. for (const auto& property : properties)
  318. {
  319. if (property && dictionary.GetProperty(property->GetId()) == nullptr)
  320. dictionary.SetProperty(property->GetId(), *property->GetDefaultValue());
  321. }
  322. }
  323. String PropertySpecification::PropertiesToString(const PropertyDictionary& dictionary) const
  324. {
  325. String result;
  326. for (auto& pair : dictionary.GetProperties())
  327. {
  328. result += property_map->GetName(pair.first) + ": " + pair.second.ToString() + '\n';
  329. }
  330. return result;
  331. }
  332. bool PropertySpecification::ParsePropertyValues(StringList& values_list, const String& values, bool split_values) const
  333. {
  334. String value;
  335. enum ParseState { VALUE, VALUE_PARENTHESIS, VALUE_QUOTE };
  336. ParseState state = VALUE;
  337. int open_parentheses = 0;
  338. size_t character_index = 0;
  339. char previous_character = 0;
  340. while (character_index < values.size())
  341. {
  342. char character = values[character_index];
  343. character_index++;
  344. switch (state)
  345. {
  346. case VALUE:
  347. {
  348. if (character == ';')
  349. {
  350. value = StringUtilities::StripWhitespace(value);
  351. if (value.size() > 0)
  352. {
  353. values_list.push_back(value);
  354. value.clear();
  355. }
  356. }
  357. else if (StringUtilities::IsWhitespace(character))
  358. {
  359. if (split_values)
  360. {
  361. value = StringUtilities::StripWhitespace(value);
  362. if (value.size() > 0)
  363. {
  364. values_list.push_back(value);
  365. value.clear();
  366. }
  367. }
  368. else
  369. value += character;
  370. }
  371. else if (character == '"')
  372. {
  373. if (split_values)
  374. {
  375. value = StringUtilities::StripWhitespace(value);
  376. if (value.size() > 0)
  377. {
  378. values_list.push_back(value);
  379. value.clear();
  380. }
  381. state = VALUE_QUOTE;
  382. }
  383. else
  384. {
  385. value += ' ';
  386. state = VALUE_QUOTE;
  387. }
  388. }
  389. else if (character == '(')
  390. {
  391. open_parentheses = 1;
  392. value += character;
  393. state = VALUE_PARENTHESIS;
  394. }
  395. else
  396. {
  397. value += character;
  398. }
  399. }
  400. break;
  401. case VALUE_PARENTHESIS:
  402. {
  403. if (previous_character == '/')
  404. {
  405. if (character == ')' || character == '(')
  406. value += character;
  407. else
  408. {
  409. value += '/';
  410. value += character;
  411. }
  412. }
  413. else
  414. {
  415. if (character == '(')
  416. {
  417. open_parentheses++;
  418. value += character;
  419. }
  420. else if (character == ')')
  421. {
  422. open_parentheses--;
  423. value += character;
  424. if (open_parentheses == 0)
  425. state = VALUE;
  426. }
  427. else if (character != '/')
  428. {
  429. value += character;
  430. }
  431. }
  432. }
  433. break;
  434. case VALUE_QUOTE:
  435. {
  436. if (previous_character == '/')
  437. {
  438. if (character == '"')
  439. value += character;
  440. else
  441. {
  442. value += '/';
  443. value += character;
  444. }
  445. }
  446. else
  447. {
  448. if (character == '"')
  449. {
  450. if (split_values)
  451. {
  452. value = StringUtilities::StripWhitespace(value);
  453. if (value.size() > 0)
  454. {
  455. values_list.push_back(value);
  456. value.clear();
  457. }
  458. }
  459. else
  460. value += ' ';
  461. state = VALUE;
  462. }
  463. else if (character != '/')
  464. {
  465. value += character;
  466. }
  467. }
  468. }
  469. }
  470. previous_character = character;
  471. }
  472. if (state == VALUE)
  473. {
  474. value = StringUtilities::StripWhitespace(value);
  475. if (value.size() > 0)
  476. values_list.push_back(value);
  477. }
  478. return true;
  479. }
  480. }
  481. }