CanvasRenderingContext2D.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/OwnPtr.h>
  27. #include <LibGfx/Painter.h>
  28. #include <LibWeb/DOM/CanvasRenderingContext2D.h>
  29. #include <LibWeb/DOM/HTMLCanvasElement.h>
  30. #include <LibWeb/DOM/HTMLImageElement.h>
  31. #include <LibWeb/DOM/ImageData.h>
  32. namespace Web {
  33. CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement& element)
  34. : m_element(element.make_weak_ptr())
  35. {
  36. }
  37. CanvasRenderingContext2D::~CanvasRenderingContext2D()
  38. {
  39. }
  40. void CanvasRenderingContext2D::set_fill_style(String style)
  41. {
  42. m_fill_style = Gfx::Color::from_string(style).value_or(Color::Black);
  43. }
  44. String CanvasRenderingContext2D::fill_style() const
  45. {
  46. return m_fill_style.to_string();
  47. }
  48. void CanvasRenderingContext2D::fill_rect(float x, float y, float width, float height)
  49. {
  50. auto painter = this->painter();
  51. if (!painter)
  52. return;
  53. auto rect = m_transform.map(Gfx::FloatRect(x, y, width, height));
  54. painter->fill_rect(enclosing_int_rect(rect), m_fill_style);
  55. did_draw(rect);
  56. }
  57. void CanvasRenderingContext2D::set_stroke_style(String style)
  58. {
  59. m_stroke_style = Gfx::Color::from_string(style).value_or(Color::Black);
  60. }
  61. String CanvasRenderingContext2D::stroke_style() const
  62. {
  63. return m_fill_style.to_string();
  64. }
  65. void CanvasRenderingContext2D::stroke_rect(float x, float y, float width, float height)
  66. {
  67. auto painter = this->painter();
  68. if (!painter)
  69. return;
  70. auto rect = m_transform.map(Gfx::FloatRect(x, y, width, height));
  71. auto top_left = m_transform.map(Gfx::FloatPoint(x, y)).to_int_point();
  72. auto top_right = m_transform.map(Gfx::FloatPoint(x + width - 1, y)).to_int_point();
  73. auto bottom_left = m_transform.map(Gfx::FloatPoint(x, y + height - 1)).to_int_point();
  74. auto bottom_right = m_transform.map(Gfx::FloatPoint(x + width - 1, y + height - 1)).to_int_point();
  75. painter->draw_line(top_left, top_right, m_stroke_style, m_line_width);
  76. painter->draw_line(top_right, bottom_right, m_stroke_style, m_line_width);
  77. painter->draw_line(bottom_right, bottom_left, m_stroke_style, m_line_width);
  78. painter->draw_line(bottom_left, top_left, m_stroke_style, m_line_width);
  79. did_draw(rect);
  80. }
  81. void CanvasRenderingContext2D::draw_image(const HTMLImageElement& image_element, float x, float y)
  82. {
  83. if (!image_element.bitmap())
  84. return;
  85. auto painter = this->painter();
  86. if (!painter)
  87. return;
  88. auto src_rect = image_element.bitmap()->rect();
  89. Gfx::FloatRect dst_rect = { x, y, (float)image_element.bitmap()->width(), (float)image_element.bitmap()->height() };
  90. auto rect = m_transform.map(dst_rect);
  91. painter->draw_scaled_bitmap(enclosing_int_rect(rect), *image_element.bitmap(), src_rect);
  92. }
  93. void CanvasRenderingContext2D::scale(float sx, float sy)
  94. {
  95. dbg() << "CanvasRenderingContext2D::scale(): " << sx << ", " << sy;
  96. m_transform.scale(sx, sy);
  97. }
  98. void CanvasRenderingContext2D::translate(float tx, float ty)
  99. {
  100. dbg() << "CanvasRenderingContext2D::translate(): " << tx << ", " << ty;
  101. m_transform.translate(tx, ty);
  102. }
  103. void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&)
  104. {
  105. // FIXME: Make use of the rect to reduce the invalidated area when possible.
  106. if (!m_element)
  107. return;
  108. if (!m_element->layout_node())
  109. return;
  110. m_element->layout_node()->set_needs_display();
  111. }
  112. OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
  113. {
  114. if (!m_element)
  115. return nullptr;
  116. if (!m_element->bitmap()) {
  117. if (!m_element->create_bitmap())
  118. return nullptr;
  119. }
  120. return make<Gfx::Painter>(*m_element->bitmap());
  121. }
  122. void CanvasRenderingContext2D::begin_path()
  123. {
  124. m_path = Gfx::Path();
  125. }
  126. void CanvasRenderingContext2D::close_path()
  127. {
  128. m_path.close();
  129. }
  130. void CanvasRenderingContext2D::move_to(float x, float y)
  131. {
  132. m_path.move_to({ x, y });
  133. }
  134. void CanvasRenderingContext2D::line_to(float x, float y)
  135. {
  136. m_path.line_to({ x, y });
  137. }
  138. void CanvasRenderingContext2D::quadratic_curve_to(float cx, float cy, float x, float y)
  139. {
  140. m_path.quadratic_bezier_curve_to({ cx, cy }, { x, y });
  141. }
  142. void CanvasRenderingContext2D::stroke()
  143. {
  144. auto painter = this->painter();
  145. if (!painter)
  146. return;
  147. painter->stroke_path(m_path, m_stroke_style, m_line_width);
  148. }
  149. void CanvasRenderingContext2D::fill(Gfx::Painter::WindingRule winding)
  150. {
  151. auto painter = this->painter();
  152. if (!painter)
  153. return;
  154. auto path = m_path;
  155. path.close_all_subpaths();
  156. painter->fill_path(path, m_fill_style, winding);
  157. }
  158. RefPtr<ImageData> CanvasRenderingContext2D::create_image_data(JS::GlobalObject& global_object, int width, int height) const
  159. {
  160. return ImageData::create_with_size(global_object, width, height);
  161. }
  162. void CanvasRenderingContext2D::put_image_data(const ImageData& image_data, float x, float y)
  163. {
  164. auto painter = this->painter();
  165. if (!painter)
  166. return;
  167. painter->blit(Gfx::IntPoint(x, y), image_data.bitmap(), image_data.bitmap().rect());
  168. did_draw(Gfx::FloatRect(x, y, image_data.width(), image_data.height()));
  169. }
  170. }