PropertyParserTransform.cpp 7.8 KB

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