CanvasRenderingContext2DWrapper.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/FlyString.h>
  27. #include <AK/Function.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/GlobalObject.h>
  31. #include <LibJS/Runtime/PrimitiveString.h>
  32. #include <LibJS/Runtime/Value.h>
  33. #include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
  34. #include <LibWeb/Bindings/HTMLImageElementWrapper.h>
  35. #include <LibWeb/Bindings/ImageDataWrapper.h>
  36. #include <LibWeb/DOM/CanvasRenderingContext2D.h>
  37. #include <LibWeb/DOM/HTMLCanvasElement.h>
  38. #include <LibWeb/DOM/HTMLImageElement.h>
  39. #include <LibWeb/DOM/ImageData.h>
  40. namespace Web {
  41. namespace Bindings {
  42. CanvasRenderingContext2DWrapper* wrap(JS::Heap& heap, CanvasRenderingContext2D& impl)
  43. {
  44. return static_cast<CanvasRenderingContext2DWrapper*>(wrap_impl(heap, impl));
  45. }
  46. CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(JS::GlobalObject& global_object, CanvasRenderingContext2D& impl)
  47. : Wrapper(*global_object.object_prototype())
  48. , m_impl(impl)
  49. {
  50. }
  51. void CanvasRenderingContext2DWrapper::initialize(JS::Interpreter&, JS::GlobalObject&)
  52. {
  53. define_native_function("fillRect", fill_rect, 4);
  54. define_native_function("scale", scale, 2);
  55. define_native_function("translate", translate, 2);
  56. define_native_function("strokeRect", stroke_rect, 4);
  57. define_native_function("drawImage", draw_image, 3);
  58. define_native_function("beginPath", begin_path, 0);
  59. define_native_function("closePath", close_path, 0);
  60. define_native_function("stroke", stroke, 0);
  61. define_native_function("fill", fill, 0);
  62. define_native_function("moveTo", move_to, 2);
  63. define_native_function("lineTo", line_to, 2);
  64. define_native_function("quadraticCurveTo", quadratic_curve_to, 4);
  65. define_native_function("createImageData", create_image_data, 1);
  66. define_native_function("putImageData", put_image_data, 3);
  67. define_native_property("fillStyle", fill_style_getter, fill_style_setter);
  68. define_native_property("strokeStyle", stroke_style_getter, stroke_style_setter);
  69. define_native_property("lineWidth", line_width_getter, line_width_setter);
  70. define_native_property("canvas", canvas_getter, nullptr);
  71. }
  72. CanvasRenderingContext2DWrapper::~CanvasRenderingContext2DWrapper()
  73. {
  74. }
  75. static CanvasRenderingContext2D* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
  76. {
  77. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  78. if (!this_object)
  79. return nullptr;
  80. // FIXME: Verify that it's a CanvasRenderingContext2DWrapper somehow!
  81. return &static_cast<CanvasRenderingContext2DWrapper*>(this_object)->impl();
  82. }
  83. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::fill_rect)
  84. {
  85. auto* impl = impl_from(interpreter, global_object);
  86. if (!impl)
  87. return {};
  88. if (interpreter.argument_count() >= 4) {
  89. auto x = interpreter.argument(0).to_double(interpreter);
  90. if (interpreter.exception())
  91. return {};
  92. auto y = interpreter.argument(1).to_double(interpreter);
  93. if (interpreter.exception())
  94. return {};
  95. auto width = interpreter.argument(2).to_double(interpreter);
  96. if (interpreter.exception())
  97. return {};
  98. auto height = interpreter.argument(3).to_double(interpreter);
  99. if (interpreter.exception())
  100. return {};
  101. impl->fill_rect(x, y, width, height);
  102. }
  103. return JS::js_undefined();
  104. }
  105. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::stroke_rect)
  106. {
  107. auto* impl = impl_from(interpreter, global_object);
  108. if (!impl)
  109. return {};
  110. if (interpreter.argument_count() >= 4) {
  111. auto x = interpreter.argument(0).to_double(interpreter);
  112. if (interpreter.exception())
  113. return {};
  114. auto y = interpreter.argument(1).to_double(interpreter);
  115. if (interpreter.exception())
  116. return {};
  117. auto width = interpreter.argument(2).to_double(interpreter);
  118. if (interpreter.exception())
  119. return {};
  120. auto height = interpreter.argument(3).to_double(interpreter);
  121. if (interpreter.exception())
  122. return {};
  123. impl->stroke_rect(x, y, width, height);
  124. }
  125. return JS::js_undefined();
  126. }
  127. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::draw_image)
  128. {
  129. auto* impl = impl_from(interpreter, global_object);
  130. if (!impl)
  131. return {};
  132. if (interpreter.argument_count() < 3)
  133. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::DrawImageArgumentCount);
  134. auto* image_argument = interpreter.argument(0).to_object(interpreter, global_object);
  135. if (!image_argument)
  136. return {};
  137. if (StringView(image_argument->class_name()) != "HTMLImageElementWrapper")
  138. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::ImageIsAn, image_argument->class_name());
  139. auto x = interpreter.argument(1).to_double(interpreter);
  140. if (interpreter.exception())
  141. return {};
  142. auto y = interpreter.argument(2).to_double(interpreter);
  143. if (interpreter.exception())
  144. return {};
  145. impl->draw_image(static_cast<const HTMLImageElementWrapper&>(*image_argument).node(), x, y);
  146. return JS::js_undefined();
  147. }
  148. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::scale)
  149. {
  150. auto* impl = impl_from(interpreter, global_object);
  151. if (!impl)
  152. return {};
  153. if (interpreter.argument_count() >= 2) {
  154. auto sx = interpreter.argument(0).to_number(interpreter);
  155. if (interpreter.exception())
  156. return {};
  157. auto sy = interpreter.argument(1).to_number(interpreter);
  158. if (interpreter.exception())
  159. return {};
  160. if (sx.is_finite_number() && sy.is_finite_number())
  161. impl->scale(sx.as_double(), sy.as_double());
  162. }
  163. return JS::js_undefined();
  164. }
  165. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::translate)
  166. {
  167. auto* impl = impl_from(interpreter, global_object);
  168. if (!impl)
  169. return {};
  170. if (interpreter.argument_count() >= 2) {
  171. auto tx = interpreter.argument(0).to_number(interpreter);
  172. if (interpreter.exception())
  173. return {};
  174. auto ty = interpreter.argument(1).to_number(interpreter);
  175. if (interpreter.exception())
  176. return {};
  177. if (tx.is_finite_number() && ty.is_finite_number())
  178. impl->translate(tx.as_double(), ty.as_double());
  179. }
  180. return JS::js_undefined();
  181. }
  182. JS_DEFINE_NATIVE_GETTER(CanvasRenderingContext2DWrapper::fill_style_getter)
  183. {
  184. auto* impl = impl_from(interpreter, global_object);
  185. if (!impl)
  186. return {};
  187. return JS::js_string(interpreter, impl->fill_style());
  188. }
  189. JS_DEFINE_NATIVE_SETTER(CanvasRenderingContext2DWrapper::fill_style_setter)
  190. {
  191. auto* impl = impl_from(interpreter, global_object);
  192. if (!impl)
  193. return;
  194. auto string = value.to_string(interpreter);
  195. if (interpreter.exception())
  196. return;
  197. impl->set_fill_style(string);
  198. }
  199. JS_DEFINE_NATIVE_GETTER(CanvasRenderingContext2DWrapper::stroke_style_getter)
  200. {
  201. auto* impl = impl_from(interpreter, global_object);
  202. if (!impl)
  203. return {};
  204. return JS::js_string(interpreter, impl->stroke_style());
  205. }
  206. JS_DEFINE_NATIVE_SETTER(CanvasRenderingContext2DWrapper::stroke_style_setter)
  207. {
  208. auto* impl = impl_from(interpreter, global_object);
  209. if (!impl)
  210. return;
  211. auto string = value.to_string(interpreter);
  212. if (interpreter.exception())
  213. return;
  214. impl->set_stroke_style(string);
  215. }
  216. JS_DEFINE_NATIVE_GETTER(CanvasRenderingContext2DWrapper::line_width_getter)
  217. {
  218. auto* impl = impl_from(interpreter, global_object);
  219. if (!impl)
  220. return {};
  221. return JS::Value(impl->line_width());
  222. }
  223. JS_DEFINE_NATIVE_SETTER(CanvasRenderingContext2DWrapper::line_width_setter)
  224. {
  225. auto* impl = impl_from(interpreter, global_object);
  226. if (!impl)
  227. return;
  228. auto line_width = value.to_double(interpreter);
  229. if (interpreter.exception())
  230. return;
  231. impl->set_line_width(line_width);
  232. }
  233. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::begin_path)
  234. {
  235. auto* impl = impl_from(interpreter, global_object);
  236. if (!impl)
  237. return {};
  238. impl->begin_path();
  239. return JS::js_undefined();
  240. }
  241. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::close_path)
  242. {
  243. auto* impl = impl_from(interpreter, global_object);
  244. if (!impl)
  245. return {};
  246. impl->close_path();
  247. return JS::js_undefined();
  248. }
  249. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::stroke)
  250. {
  251. auto* impl = impl_from(interpreter, global_object);
  252. if (!impl)
  253. return {};
  254. impl->stroke();
  255. return JS::js_undefined();
  256. }
  257. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::fill)
  258. {
  259. auto* impl = impl_from(interpreter, global_object);
  260. if (!impl)
  261. return {};
  262. auto winding = Gfx::Painter::WindingRule::Nonzero;
  263. if (interpreter.argument_count() == 1) {
  264. auto arg0 = interpreter.argument(0);
  265. if (arg0.is_string()) {
  266. const auto& winding_name = arg0.as_string().string();
  267. if (winding_name == "evenodd") {
  268. winding = Gfx::Painter::WindingRule::EvenOdd;
  269. } else if (winding_name != "nonzero") {
  270. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::FillBadWindingRule);
  271. }
  272. } else {
  273. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::FillNonString);
  274. }
  275. } else {
  276. // FIXME: Path2D object
  277. return JS::js_undefined();
  278. }
  279. impl->fill(winding);
  280. return JS::js_undefined();
  281. }
  282. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::move_to)
  283. {
  284. auto* impl = impl_from(interpreter, global_object);
  285. if (!impl)
  286. return {};
  287. auto x = interpreter.argument(0).to_double(interpreter);
  288. if (interpreter.exception())
  289. return {};
  290. auto y = interpreter.argument(1).to_double(interpreter);
  291. if (interpreter.exception())
  292. return {};
  293. impl->move_to(x, y);
  294. return JS::js_undefined();
  295. }
  296. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::line_to)
  297. {
  298. auto* impl = impl_from(interpreter, global_object);
  299. if (!impl)
  300. return {};
  301. auto x = interpreter.argument(0).to_double(interpreter);
  302. if (interpreter.exception())
  303. return {};
  304. auto y = interpreter.argument(1).to_double(interpreter);
  305. if (interpreter.exception())
  306. return {};
  307. impl->line_to(x, y);
  308. return JS::js_undefined();
  309. }
  310. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::quadratic_curve_to)
  311. {
  312. auto* impl = impl_from(interpreter, global_object);
  313. if (!impl)
  314. return {};
  315. auto cx = interpreter.argument(0).to_double(interpreter);
  316. if (interpreter.exception())
  317. return {};
  318. auto cy = interpreter.argument(1).to_double(interpreter);
  319. if (interpreter.exception())
  320. return {};
  321. auto x = interpreter.argument(2).to_double(interpreter);
  322. if (interpreter.exception())
  323. return {};
  324. auto y = interpreter.argument(3).to_double(interpreter);
  325. if (interpreter.exception())
  326. return {};
  327. impl->quadratic_curve_to(cx, cy, x, y);
  328. return JS::js_undefined();
  329. }
  330. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::create_image_data)
  331. {
  332. auto* impl = impl_from(interpreter, global_object);
  333. if (!impl)
  334. return {};
  335. auto width = interpreter.argument(0).to_i32(interpreter);
  336. if (interpreter.exception())
  337. return {};
  338. auto height = interpreter.argument(1).to_i32(interpreter);
  339. if (interpreter.exception())
  340. return {};
  341. auto image_data = impl->create_image_data(global_object, width, height);
  342. return wrap(interpreter.heap(), *image_data);
  343. }
  344. JS_DEFINE_NATIVE_FUNCTION(CanvasRenderingContext2DWrapper::put_image_data)
  345. {
  346. auto* impl = impl_from(interpreter, global_object);
  347. if (!impl)
  348. return {};
  349. auto* image_data_object = interpreter.argument(0).to_object(interpreter, global_object);
  350. if (!image_data_object)
  351. return {};
  352. if (StringView(image_data_object->class_name()) != "ImageDataWrapper") {
  353. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::PutImageDataBadCall);
  354. }
  355. auto& image_data = static_cast<ImageDataWrapper*>(image_data_object)->impl();
  356. auto x = interpreter.argument(1).to_double(interpreter);
  357. if (interpreter.exception())
  358. return {};
  359. auto y = interpreter.argument(2).to_double(interpreter);
  360. if (interpreter.exception())
  361. return {};
  362. impl->put_image_data(image_data, x, y);
  363. return JS::js_undefined();
  364. }
  365. JS_DEFINE_NATIVE_GETTER(CanvasRenderingContext2DWrapper::canvas_getter)
  366. {
  367. auto* impl = impl_from(interpreter, global_object);
  368. if (!impl)
  369. return {};
  370. auto* element = impl->element();
  371. if (!element)
  372. return JS::js_null();
  373. return wrap(interpreter.heap(), *element);
  374. }
  375. }
  376. }