PropertySpecification.cpp 16 KB

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