PropertyParserNumber.cpp 3.3 KB

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