PropertyParserNumber.cpp 3.4 KB

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