PropertyParserTransform.cpp 7.8 KB

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