PropertyParserTransform.cpp 7.7 KB

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