PropertyParserNumber.cpp 4.1 KB

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