PropertyParserTransform.cpp 7.6 KB

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