PropertyParserTransform.cpp 7.8 KB

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