CanvasRenderingContext2D.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. namespace Web {
  32. CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement& element)
  33. : m_element(element.make_weak_ptr())
  34. {
  35. }
  36. CanvasRenderingContext2D::~CanvasRenderingContext2D()
  37. {
  38. }
  39. void CanvasRenderingContext2D::set_fill_style(String style)
  40. {
  41. m_fill_style = Gfx::Color::from_string(style).value_or(Color::Black);
  42. }
  43. String CanvasRenderingContext2D::fill_style() const
  44. {
  45. return m_fill_style.to_string();
  46. }
  47. void CanvasRenderingContext2D::fill_rect(float x, float y, float width, float height)
  48. {
  49. auto painter = this->painter();
  50. if (!painter)
  51. return;
  52. auto rect = m_transform.map(Gfx::FloatRect(x, y, width, height));
  53. painter->fill_rect(enclosing_int_rect(rect), m_fill_style);
  54. did_draw(rect);
  55. }
  56. void CanvasRenderingContext2D::set_stroke_style(String style)
  57. {
  58. m_stroke_style = Gfx::Color::from_string(style).value_or(Color::Black);
  59. }
  60. String CanvasRenderingContext2D::stroke_style() const
  61. {
  62. return m_fill_style.to_string();
  63. }
  64. void CanvasRenderingContext2D::stroke_rect(float x, float y, float width, float height)
  65. {
  66. auto painter = this->painter();
  67. if (!painter)
  68. return;
  69. auto rect = m_transform.map(Gfx::FloatRect(x, y, width, height));
  70. painter->draw_rect(enclosing_int_rect(rect), m_stroke_style);
  71. did_draw(rect);
  72. }
  73. void CanvasRenderingContext2D::draw_image(const HTMLImageElement& image_element, float x, float y)
  74. {
  75. if (!image_element.bitmap())
  76. return;
  77. auto painter = this->painter();
  78. if (!painter)
  79. return;
  80. auto src_rect = image_element.bitmap()->rect();
  81. Gfx::FloatRect dst_rect = { x, y, (float)image_element.bitmap()->width(), (float)image_element.bitmap()->height() };
  82. auto rect = m_transform.map(dst_rect);
  83. painter->draw_scaled_bitmap(enclosing_int_rect(rect), *image_element.bitmap(), src_rect);
  84. }
  85. void CanvasRenderingContext2D::scale(float sx, float sy)
  86. {
  87. dbg() << "CanvasRenderingContext2D::scale(): " << sx << ", " << sy;
  88. m_transform.scale(sx, sy);
  89. }
  90. void CanvasRenderingContext2D::translate(float tx, float ty)
  91. {
  92. dbg() << "CanvasRenderingContext2D::translate(): " << tx << ", " << ty;
  93. m_transform.translate(tx, ty);
  94. }
  95. void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&)
  96. {
  97. // FIXME: Make use of the rect to reduce the invalidated area when possible.
  98. if (!m_element)
  99. return;
  100. if (!m_element->layout_node())
  101. return;
  102. m_element->layout_node()->set_needs_display();
  103. }
  104. OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
  105. {
  106. if (!m_element)
  107. return nullptr;
  108. return make<Gfx::Painter>(m_element->ensure_bitmap());
  109. }
  110. }