PropertyParserTransform.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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) 2014 Markus Schöngart
  7. * Copyright (c) 2019-2023 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 "PropertyParserTransform.h"
  29. #include "../../Include/RmlUi/Core/NumericValue.h"
  30. #include "../../Include/RmlUi/Core/Transform.h"
  31. #include "../../Include/RmlUi/Core/TransformPrimitive.h"
  32. #include <string.h>
  33. namespace Rml {
  34. PropertyParserTransform::PropertyParserTransform() :
  35. number(Unit::NUMBER), length(Unit::LENGTH, Unit::PX), length_pct(Unit::LENGTH_PERCENT, Unit::PX), angle(Unit::ANGLE, Unit::RAD)
  36. {}
  37. PropertyParserTransform::~PropertyParserTransform() {}
  38. bool PropertyParserTransform::ParseValue(Property& property, const String& value, const ParameterMap& /*parameters*/) const
  39. {
  40. if (value == "none")
  41. {
  42. property.value = Variant(TransformPtr());
  43. property.unit = Unit::TRANSFORM;
  44. return true;
  45. }
  46. TransformPtr transform = MakeShared<Transform>();
  47. char const* next = value.c_str();
  48. NumericValue args[16];
  49. const PropertyParser* number16[] = {&number, &number, &number, &number, &number, &number, &number, &number, &number, &number, &number, &number,
  50. &number, &number, &number, &number};
  51. const PropertyParser* lengthpct2_length1[] = {&length_pct, &length_pct, &length};
  52. const PropertyParser* number3angle1[] = {&number, &number, &number, &angle};
  53. const PropertyParser* angle2[] = {&angle, &angle};
  54. const PropertyParser* length1[] = {&length};
  55. // For semantic purposes, define subsets of the above parsers when scanning primitives below.
  56. auto lengthpct1 = lengthpct2_length1;
  57. auto lengthpct2 = lengthpct2_length1;
  58. auto angle1 = angle2;
  59. auto number1 = number16;
  60. auto number2 = number16;
  61. auto number3 = number16;
  62. auto number6 = number16;
  63. while (*next)
  64. {
  65. using namespace Transforms;
  66. int bytes_read = 0;
  67. if (Scan(bytes_read, next, "perspective", length1, args, 1))
  68. {
  69. transform->AddPrimitive({Perspective(args)});
  70. }
  71. else if (Scan(bytes_read, next, "matrix", number6, args, 6))
  72. {
  73. transform->AddPrimitive({Matrix2D(args)});
  74. }
  75. else if (Scan(bytes_read, next, "matrix3d", number16, args, 16))
  76. {
  77. transform->AddPrimitive({Matrix3D(args)});
  78. }
  79. else if (Scan(bytes_read, next, "translateX", lengthpct1, args, 1))
  80. {
  81. transform->AddPrimitive({TranslateX(args)});
  82. }
  83. else if (Scan(bytes_read, next, "translateY", lengthpct1, args, 1))
  84. {
  85. transform->AddPrimitive({TranslateY(args)});
  86. }
  87. else if (Scan(bytes_read, next, "translateZ", length1, args, 1))
  88. {
  89. transform->AddPrimitive({TranslateZ(args)});
  90. }
  91. else if (Scan(bytes_read, next, "translate", lengthpct2, args, 2))
  92. {
  93. transform->AddPrimitive({Translate2D(args)});
  94. }
  95. else if (Scan(bytes_read, next, "translate3d", lengthpct2_length1, args, 3))
  96. {
  97. transform->AddPrimitive({Translate3D(args)});
  98. }
  99. else if (Scan(bytes_read, next, "scaleX", number1, args, 1))
  100. {
  101. transform->AddPrimitive({ScaleX(args)});
  102. }
  103. else if (Scan(bytes_read, next, "scaleY", number1, args, 1))
  104. {
  105. transform->AddPrimitive({ScaleY(args)});
  106. }
  107. else if (Scan(bytes_read, next, "scaleZ", number1, args, 1))
  108. {
  109. transform->AddPrimitive({ScaleZ(args)});
  110. }
  111. else if (Scan(bytes_read, next, "scale", number2, args, 2))
  112. {
  113. transform->AddPrimitive({Scale2D(args)});
  114. }
  115. else if (Scan(bytes_read, next, "scale", number1, args, 1))
  116. {
  117. args[1] = args[0];
  118. transform->AddPrimitive({Scale2D(args)});
  119. }
  120. else if (Scan(bytes_read, next, "scale3d", number3, args, 3))
  121. {
  122. transform->AddPrimitive({Scale3D(args)});
  123. }
  124. else if (Scan(bytes_read, next, "rotateX", angle1, args, 1))
  125. {
  126. transform->AddPrimitive({RotateX(args)});
  127. }
  128. else if (Scan(bytes_read, next, "rotateY", angle1, args, 1))
  129. {
  130. transform->AddPrimitive({RotateY(args)});
  131. }
  132. else if (Scan(bytes_read, next, "rotateZ", angle1, args, 1))
  133. {
  134. transform->AddPrimitive({RotateZ(args)});
  135. }
  136. else if (Scan(bytes_read, next, "rotate", angle1, args, 1))
  137. {
  138. transform->AddPrimitive({Rotate2D(args)});
  139. }
  140. else if (Scan(bytes_read, next, "rotate3d", number3angle1, args, 4))
  141. {
  142. transform->AddPrimitive({Rotate3D(args)});
  143. }
  144. else if (Scan(bytes_read, next, "skewX", angle1, args, 1))
  145. {
  146. transform->AddPrimitive({SkewX(args)});
  147. }
  148. else if (Scan(bytes_read, next, "skewY", angle1, args, 1))
  149. {
  150. transform->AddPrimitive({SkewY(args)});
  151. }
  152. else if (Scan(bytes_read, next, "skew", angle2, args, 2))
  153. {
  154. transform->AddPrimitive({Skew2D(args)});
  155. }
  156. if (bytes_read > 0)
  157. {
  158. next += bytes_read;
  159. }
  160. else
  161. {
  162. return false;
  163. }
  164. }
  165. property.value = Variant(std::move(transform));
  166. property.unit = Unit::TRANSFORM;
  167. return true;
  168. }
  169. bool PropertyParserTransform::Scan(int& out_bytes_read, const char* str, const char* keyword, const PropertyParser** parsers, NumericValue* args,
  170. int nargs) const
  171. {
  172. out_bytes_read = 0;
  173. int total_bytes_read = 0, bytes_read = 0;
  174. /* skip leading white space */
  175. bytes_read = 0;
  176. sscanf(str, " %n", &bytes_read);
  177. str += bytes_read;
  178. total_bytes_read += bytes_read;
  179. /* find the keyword */
  180. if (!memcmp(str, keyword, strlen(keyword)))
  181. {
  182. bytes_read = (int)strlen(keyword);
  183. str += bytes_read;
  184. total_bytes_read += bytes_read;
  185. }
  186. else
  187. {
  188. return false;
  189. }
  190. /* skip any white space */
  191. bytes_read = 0;
  192. sscanf(str, " %n", &bytes_read);
  193. str += bytes_read;
  194. total_bytes_read += bytes_read;
  195. /* find the opening brace */
  196. bytes_read = 0;
  197. if (sscanf(str, " ( %n", &bytes_read), bytes_read)
  198. {
  199. str += bytes_read;
  200. total_bytes_read += bytes_read;
  201. }
  202. else
  203. {
  204. return false;
  205. }
  206. /* use the quicker stack-based argument buffer, if possible */
  207. char* arg = nullptr;
  208. char arg_stack[1024];
  209. String arg_heap;
  210. if (strlen(str) < sizeof(arg_stack))
  211. {
  212. arg = arg_stack;
  213. }
  214. else
  215. {
  216. arg_heap = str;
  217. arg = &arg_heap[0];
  218. }
  219. /* parse the arguments */
  220. for (int i = 0; i < nargs; ++i)
  221. {
  222. Property prop;
  223. bytes_read = 0;
  224. if (sscanf(str, " %[^,)] %n", arg, &bytes_read), bytes_read && parsers[i]->ParseValue(prop, String(arg), ParameterMap()))
  225. {
  226. args[i].number = prop.value.Get<float>();
  227. args[i].unit = prop.unit;
  228. str += bytes_read;
  229. total_bytes_read += bytes_read;
  230. }
  231. else
  232. {
  233. return false;
  234. }
  235. /* find the comma */
  236. if (i < nargs - 1)
  237. {
  238. bytes_read = 0;
  239. if (sscanf(str, " , %n", &bytes_read), bytes_read)
  240. {
  241. str += bytes_read;
  242. total_bytes_read += bytes_read;
  243. }
  244. else
  245. {
  246. return false;
  247. }
  248. }
  249. }
  250. /* find the closing brace */
  251. bytes_read = 0;
  252. if (sscanf(str, " ) %n", &bytes_read), bytes_read)
  253. {
  254. str += bytes_read;
  255. total_bytes_read += bytes_read;
  256. }
  257. else
  258. {
  259. return false;
  260. }
  261. out_bytes_read = total_bytes_read;
  262. return total_bytes_read > 0;
  263. }
  264. } // namespace Rml