PropertySpecification.cpp 17 KB

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