CanvasTextDrawingStyles.h 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/CSS/Parser/Parser.h>
  8. #include <LibWeb/CSS/StyleComputer.h>
  9. #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/HTML/Canvas/CanvasState.h>
  12. namespace Web::HTML {
  13. // https://html.spec.whatwg.org/multipage/canvas.html#canvastextdrawingstyles
  14. template<typename IncludingClass>
  15. class CanvasTextDrawingStyles {
  16. public:
  17. ~CanvasTextDrawingStyles() = default;
  18. ByteString font() const
  19. {
  20. // When font style value is empty return default string
  21. if (!my_drawing_state().font_style_value) {
  22. return "10px sans-serif";
  23. }
  24. // On getting, the font attribute must return the serialized form of the current font of the context (with no 'line-height' component).
  25. auto const& font_style_value = my_drawing_state().font_style_value->as_shorthand();
  26. auto font_style = font_style_value.longhand(CSS::PropertyID::FontStyle);
  27. auto font_weight = font_style_value.longhand(CSS::PropertyID::FontWeight);
  28. auto font_size = font_style_value.longhand(CSS::PropertyID::FontSize);
  29. auto font_family = font_style_value.longhand(CSS::PropertyID::FontFamily);
  30. return ByteString::formatted("{} {} {} {}",
  31. font_style->to_string(CSS::CSSStyleValue::SerializationMode::Normal),
  32. font_weight->to_string(CSS::CSSStyleValue::SerializationMode::Normal),
  33. font_size->to_string(CSS::CSSStyleValue::SerializationMode::Normal),
  34. font_family->to_string(CSS::CSSStyleValue::SerializationMode::Normal));
  35. }
  36. void set_font(StringView font)
  37. {
  38. // The font IDL attribute, on setting, must be parsed as a CSS <'font'> value (but without supporting property-independent style sheet syntax like 'inherit'),
  39. // and the resulting font must be assigned to the context, with the 'line-height' component forced to 'normal', with the 'font-size' component converted to CSS pixels,
  40. // and with system fonts being computed to explicit values.
  41. // FIXME: with the 'line-height' component forced to 'normal'
  42. // FIXME: with the 'font-size' component converted to CSS pixels
  43. auto font_style_value_result = parse_css_value(CSS::Parser::ParsingContext {}, font, CSS::PropertyID::Font);
  44. // If the new value is syntactically incorrect (including using property-independent style sheet syntax like 'inherit' or 'initial'), then it must be ignored, without assigning a new font value.
  45. // NOTE: ShorthandStyleValue should be the only valid option here. We implicitly VERIFY this below.
  46. if (!font_style_value_result || !font_style_value_result->is_shorthand()) {
  47. return;
  48. }
  49. my_drawing_state().font_style_value = font_style_value_result.release_nonnull();
  50. // Load font with font style value properties
  51. auto const& font_style_value = my_drawing_state().font_style_value->as_shorthand();
  52. auto& canvas_element = reinterpret_cast<IncludingClass&>(*this).canvas_element();
  53. auto& font_style = *font_style_value.longhand(CSS::PropertyID::FontStyle);
  54. auto& font_weight = *font_style_value.longhand(CSS::PropertyID::FontWeight);
  55. auto& font_width = *font_style_value.longhand(CSS::PropertyID::FontWidth);
  56. auto& font_size = *font_style_value.longhand(CSS::PropertyID::FontSize);
  57. auto& font_family = *font_style_value.longhand(CSS::PropertyID::FontFamily);
  58. auto font_list = canvas_element.document().style_computer().compute_font_for_style_values(&canvas_element, {}, font_family, font_size, font_style, font_weight, font_width);
  59. my_drawing_state().current_font = font_list->first();
  60. }
  61. Bindings::CanvasTextAlign text_align() const { return my_drawing_state().text_align; }
  62. void set_text_align(Bindings::CanvasTextAlign text_align) { my_drawing_state().text_align = text_align; }
  63. Bindings::CanvasTextBaseline text_baseline() const { return my_drawing_state().text_baseline; }
  64. void set_text_baseline(Bindings::CanvasTextBaseline text_baseline) { my_drawing_state().text_baseline = text_baseline; }
  65. protected:
  66. CanvasTextDrawingStyles() = default;
  67. private:
  68. CanvasState::DrawingState& my_drawing_state() { return reinterpret_cast<IncludingClass&>(*this).drawing_state(); }
  69. CanvasState::DrawingState const& my_drawing_state() const { return reinterpret_cast<IncludingClass const&>(*this).drawing_state(); }
  70. };
  71. }