PropertyParserNumber.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. namespace Rml {
  30. namespace Core {
  31. PropertyParserNumber::PropertyParserNumber(int units, Property::Unit zero_unit)
  32. : units(units), zero_unit(zero_unit)
  33. {
  34. if (units & Property::PERCENT)
  35. {
  36. unit_suffixes.push_back(UnitSuffix(Property::PERCENT, "%"));
  37. }
  38. if (units & Property::PX)
  39. {
  40. unit_suffixes.push_back(UnitSuffix(Property::PX, "px"));
  41. }
  42. if (units & Property::DP)
  43. {
  44. unit_suffixes.push_back(UnitSuffix(Property::DP, "dp"));
  45. }
  46. if (units & Property::EM)
  47. {
  48. unit_suffixes.push_back(UnitSuffix(Property::EM, "em"));
  49. }
  50. if (units & Property::REM)
  51. {
  52. unit_suffixes.push_back(UnitSuffix(Property::REM, "rem"));
  53. }
  54. if (units & Property::INCH)
  55. {
  56. unit_suffixes.push_back(UnitSuffix(Property::INCH, "in"));
  57. }
  58. if (units & Property::CM)
  59. {
  60. unit_suffixes.push_back(UnitSuffix(Property::CM, "cm"));
  61. }
  62. if (units & Property::MM)
  63. {
  64. unit_suffixes.push_back(UnitSuffix(Property::MM, "mm"));
  65. }
  66. if (units & Property::PT)
  67. {
  68. unit_suffixes.push_back(UnitSuffix(Property::PT, "pt"));
  69. }
  70. if (units & Property::PC)
  71. {
  72. unit_suffixes.push_back(UnitSuffix(Property::PC, "pc"));
  73. }
  74. if (units & Property::DEG)
  75. {
  76. unit_suffixes.push_back(UnitSuffix(Property::DEG, "deg"));
  77. }
  78. if (units & Property::RAD)
  79. {
  80. unit_suffixes.push_back(UnitSuffix(Property::RAD, "rad"));
  81. }
  82. }
  83. PropertyParserNumber::~PropertyParserNumber()
  84. {
  85. }
  86. // Called to parse a RCSS number declaration.
  87. bool PropertyParserNumber::ParseValue(Property& property, const String& value, const ParameterMap& RMLUI_UNUSED_PARAMETER(parameters)) const
  88. {
  89. RMLUI_UNUSED(parameters);
  90. // Default to a simple number.
  91. property.unit = Property::NUMBER;
  92. // Check for a unit declaration at the end of the number.
  93. size_t unit_pos = value.size();
  94. for (size_t i = 0; i < unit_suffixes.size(); i++)
  95. {
  96. const UnitSuffix& unit_suffix = unit_suffixes[i];
  97. if (value.size() < unit_suffix.second.size())
  98. continue;
  99. const size_t test_unit_pos = value.size() - unit_suffix.second.size();
  100. if (StringUtilities::StringCompareCaseInsensitive(StringView(value, test_unit_pos), StringView(unit_suffix.second)))
  101. {
  102. unit_pos = test_unit_pos;
  103. property.unit = unit_suffix.first;
  104. break;
  105. }
  106. }
  107. if ((units & property.unit) == 0)
  108. {
  109. // Detected unit not allowed (this can only apply to NUMBER, i.e., when no unit was found but one is required).
  110. // However, we allow values of "0" if zero_unit is set.
  111. bool result = (zero_unit != Property::UNKNOWN && (value.size() == 1 && value[0] == '0'));
  112. if(result)
  113. {
  114. property.unit = zero_unit;
  115. property.value = Variant(0.0f);
  116. }
  117. return result;
  118. }
  119. float float_value;
  120. String str_value( value.c_str(), value.c_str() + unit_pos );
  121. if (sscanf(str_value.c_str(), "%f", &float_value) == 1)
  122. {
  123. property.value = Variant(float_value);
  124. return true;
  125. }
  126. return false;
  127. }
  128. }
  129. }