PropertySpecification.cpp 18 KB

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