PropertyParserNumber.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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-2023 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 "PropertyParserNumber.h"
  29. #include <stdlib.h>
  30. namespace Rml {
  31. struct PropertyParserNumberData {
  32. const UnorderedMap<String, Unit> unit_string_map = {
  33. {"", Unit::NUMBER},
  34. {"%", Unit::PERCENT},
  35. {"px", Unit::PX},
  36. {"dp", Unit::DP},
  37. {"x", Unit::X},
  38. {"vw", Unit::VW},
  39. {"vh", Unit::VH},
  40. {"em", Unit::EM},
  41. {"rem", Unit::REM},
  42. {"in", Unit::INCH},
  43. {"cm", Unit::CM},
  44. {"mm", Unit::MM},
  45. {"pt", Unit::PT},
  46. {"pc", Unit::PC},
  47. {"deg", Unit::DEG},
  48. {"rad", Unit::RAD},
  49. };
  50. };
  51. ControlledLifetimeResource<PropertyParserNumberData> PropertyParserNumber::parser_data;
  52. void PropertyParserNumber::Initialize()
  53. {
  54. parser_data.Initialize();
  55. }
  56. void PropertyParserNumber::Shutdown()
  57. {
  58. parser_data.Shutdown();
  59. }
  60. PropertyParserNumber::PropertyParserNumber(Units units, Unit zero_unit) : units(units), zero_unit(zero_unit) {}
  61. PropertyParserNumber::~PropertyParserNumber() {}
  62. bool PropertyParserNumber::ParseValue(Property& property, const String& value, const ParameterMap& /*parameters*/) const
  63. {
  64. // Find the beginning of the unit string in 'value'.
  65. size_t unit_pos = 0;
  66. for (size_t i = value.size(); i--;)
  67. {
  68. const char c = value[i];
  69. if ((c >= '0' && c <= '9') || StringUtilities::IsWhitespace(c))
  70. {
  71. unit_pos = i + 1;
  72. break;
  73. }
  74. }
  75. String str_number = value.substr(0, unit_pos);
  76. String str_unit = StringUtilities::ToLower(value.substr(unit_pos));
  77. char* str_end = nullptr;
  78. float float_value = strtof(str_number.c_str(), &str_end);
  79. if (str_number.c_str() == str_end)
  80. {
  81. // Number conversion failed
  82. return false;
  83. }
  84. const auto it = parser_data->unit_string_map.find(str_unit);
  85. if (it == parser_data->unit_string_map.end())
  86. {
  87. // Invalid unit name
  88. return false;
  89. }
  90. const Unit unit = it->second;
  91. if (Any(unit & units))
  92. {
  93. property.value = float_value;
  94. property.unit = unit;
  95. return true;
  96. }
  97. // Detected unit not allowed.
  98. // However, we allow a value of "0" if zero_unit is set and no unit specified (that is, unit is a pure NUMBER).
  99. if (unit == Unit::NUMBER)
  100. {
  101. if (zero_unit != Unit::UNKNOWN && float_value == 0.0f)
  102. {
  103. property.unit = zero_unit;
  104. property.value = Variant(0.0f);
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. } // namespace Rml