SVGContext.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Vector.h>
  8. #include <LibGfx/Color.h>
  9. #include <LibGfx/Rect.h>
  10. namespace Web {
  11. class SVGContext {
  12. public:
  13. SVGContext(Gfx::FloatRect svg_element_bounds)
  14. : m_svg_element_bounds(svg_element_bounds)
  15. {
  16. m_states.append(State());
  17. }
  18. Gfx::Color fill_color() const { return state().fill_color; }
  19. Gfx::Color stroke_color() const { return state().stroke_color; }
  20. float stroke_width() const { return state().stroke_width; }
  21. void set_fill_color(Gfx::Color color) { state().fill_color = color; }
  22. void set_stroke_color(Gfx::Color color) { state().stroke_color = color; }
  23. void set_stroke_width(float width) { state().stroke_width = width; }
  24. Gfx::FloatPoint svg_element_position() const { return m_svg_element_bounds.top_left(); }
  25. void save() { m_states.append(m_states.last()); }
  26. void restore() { m_states.take_last(); }
  27. private:
  28. struct State {
  29. Gfx::Color fill_color { Gfx::Color::Transparent };
  30. Gfx::Color stroke_color { Gfx::Color::Transparent };
  31. float stroke_width { 1.0 };
  32. };
  33. State const& state() const { return m_states.last(); }
  34. State& state() { return m_states.last(); }
  35. Gfx::FloatRect m_svg_element_bounds;
  36. Vector<State> m_states;
  37. };
  38. }