PropertyParserNumber.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "PropertyParserNumber.h"
  29. namespace Rocket {
  30. namespace Core {
  31. PropertyParserNumber::PropertyParserNumber(int units)
  32. : units(units)
  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::EM)
  43. {
  44. unit_suffixes.push_back(UnitSuffix(Property::EM, "em"));
  45. }
  46. if (units & Property::INCH)
  47. {
  48. unit_suffixes.push_back(UnitSuffix(Property::INCH, "in"));
  49. }
  50. if (units & Property::CM)
  51. {
  52. unit_suffixes.push_back(UnitSuffix(Property::CM, "cm"));
  53. }
  54. if (units & Property::MM)
  55. {
  56. unit_suffixes.push_back(UnitSuffix(Property::MM, "mm"));
  57. }
  58. if (units & Property::PT)
  59. {
  60. unit_suffixes.push_back(UnitSuffix(Property::PT, "pt"));
  61. }
  62. if (units & Property::PC)
  63. {
  64. unit_suffixes.push_back(UnitSuffix(Property::PC, "pc"));
  65. }
  66. if (units & Property::DEG)
  67. {
  68. unit_suffixes.push_back(UnitSuffix(Property::DEG, "deg"));
  69. }
  70. if (units & Property::RAD)
  71. {
  72. unit_suffixes.push_back(UnitSuffix(Property::RAD, "rad"));
  73. }
  74. }
  75. PropertyParserNumber::~PropertyParserNumber()
  76. {
  77. }
  78. // Called to parse a RCSS number declaration.
  79. bool PropertyParserNumber::ParseValue(Property& property, const String& value, const ParameterMap& ROCKET_UNUSED(parameters)) const
  80. {
  81. // Default to a simple number.
  82. property.unit = Property::NUMBER;
  83. // Check for a unit declaration at the end of the number.
  84. for (size_t i = 0; i < unit_suffixes.size(); i++)
  85. {
  86. const UnitSuffix& unit_suffix = unit_suffixes[i];
  87. if (value.Length() < unit_suffix.second.Length())
  88. continue;
  89. if (strcasecmp(value.CString() + (value.Length() - unit_suffix.second.Length()), unit_suffix.second.CString()) == 0)
  90. {
  91. property.unit = unit_suffix.first;
  92. break;
  93. }
  94. }
  95. if ((units & property.unit) == 0)
  96. {
  97. // Detected unit not allowed (this can only apply to NUMBER,
  98. // i.e., when no unit was found but one is required).
  99. return false;
  100. }
  101. float float_value;
  102. if (sscanf(value.CString(), "%f", &float_value) == 1)
  103. {
  104. property.value = Variant(float_value);
  105. return true;
  106. }
  107. return false;
  108. }
  109. // Destroys the parser.
  110. void PropertyParserNumber::Release()
  111. {
  112. delete this;
  113. }
  114. }
  115. }