PropertyParserTransform.cpp 7.8 KB

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