CanvasPathDrawingStyles.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/HTML/Canvas/CanvasState.h>
  8. namespace Web::HTML {
  9. // https://html.spec.whatwg.org/multipage/canvas.html#canvaspathdrawingstyles
  10. template<typename IncludingClass>
  11. class CanvasPathDrawingStyles {
  12. public:
  13. ~CanvasPathDrawingStyles() = default;
  14. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-linewidth
  15. void set_line_width(float line_width)
  16. {
  17. // On setting, zero, negative, infinite, and NaN values must be ignored, leaving the value unchanged;
  18. if (line_width <= 0 || !isfinite(line_width))
  19. return;
  20. // other values must change the current value to the new value.
  21. my_drawing_state().line_width = line_width;
  22. }
  23. float line_width() const
  24. {
  25. // On getting, it must return the current value.
  26. return my_drawing_state().line_width;
  27. }
  28. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-linecap
  29. void set_line_cap(Bindings::CanvasLineCap line_cap)
  30. {
  31. // On setting, the current value must be changed to the new value.
  32. my_drawing_state().line_cap = line_cap;
  33. }
  34. Bindings::CanvasLineCap line_cap() const
  35. {
  36. // On getting, it must return the current value.
  37. return my_drawing_state().line_cap;
  38. }
  39. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-linejoin
  40. void set_line_join(Bindings::CanvasLineJoin line_join)
  41. {
  42. // On setting, the current value must be changed to the new value.
  43. my_drawing_state().line_join = line_join;
  44. }
  45. Bindings::CanvasLineJoin line_join() const
  46. {
  47. // On getting, it must return the current value.
  48. return my_drawing_state().line_join;
  49. }
  50. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-miterlimit
  51. void set_miter_limit(float miter_limit)
  52. {
  53. // On setting, zero, negative, infinite, and NaN values must be ignored, leaving the value unchanged;
  54. if (miter_limit <= 0 || !isfinite(miter_limit))
  55. return;
  56. // other values must change the current value to the new value.
  57. my_drawing_state().miter_limit = miter_limit;
  58. }
  59. float miter_limit() const
  60. {
  61. // On getting, it must return the current value.
  62. return my_drawing_state().miter_limit;
  63. }
  64. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-setlinedash
  65. void set_line_dash(Vector<double> segments)
  66. {
  67. // The setLineDash(segments) method, when invoked, must run these steps:
  68. // 1. If any value in segments is not finite (e.g. an Infinity or a NaN value), or if any value is negative (less than zero), then return
  69. // (without throwing an exception; user agents could show a message on a developer console, though, as that would be helpful for debugging).
  70. for (auto const& segment : segments) {
  71. if (!isfinite(segment) || segment < 0)
  72. return;
  73. }
  74. // 2. If the number of elements in segments is odd, then let segments be the concatenation of two copies of segments.
  75. if (segments.size() % 2 == 1)
  76. segments.extend(segments);
  77. // 3. Let the object's dash list be segments.
  78. my_drawing_state().dash_list = segments;
  79. }
  80. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-getlinedash
  81. Vector<double> get_line_dash()
  82. {
  83. // When the getLineDash() method is invoked, it must return a sequence whose values are the values of the object's dash list, in the same order.
  84. return my_drawing_state().dash_list;
  85. }
  86. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-linedashoffset
  87. void set_line_dash_offset(float line_dash_offset)
  88. {
  89. // On setting, infinite and NaN values must be ignored, leaving the value unchanged;
  90. if (!isfinite(line_dash_offset))
  91. return;
  92. // other values must change the current value to the new value.
  93. my_drawing_state().line_dash_offset = line_dash_offset;
  94. }
  95. float line_dash_offset() const
  96. {
  97. // On getting, it must return the current value.
  98. return my_drawing_state().line_dash_offset;
  99. }
  100. protected:
  101. CanvasPathDrawingStyles() = default;
  102. private:
  103. CanvasState::DrawingState& my_drawing_state() { return reinterpret_cast<IncludingClass&>(*this).drawing_state(); }
  104. CanvasState::DrawingState const& my_drawing_state() const { return reinterpret_cast<IncludingClass const&>(*this).drawing_state(); }
  105. };
  106. }