PropertySpecification.cpp 16 KB

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