PropertyParserNumber.cpp 3.3 KB

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