PropertyParserNumber.cpp 3.2 KB

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