/src/serenity/Userland/Libraries/LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibWeb/CSS/Parser/Parser.h> |
10 | | #include <LibWeb/CSS/StyleComputer.h> |
11 | | #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h> |
12 | | #include <LibWeb/DOM/Document.h> |
13 | | #include <LibWeb/HTML/Canvas/CanvasState.h> |
14 | | |
15 | | namespace Web::HTML { |
16 | | |
17 | | // https://html.spec.whatwg.org/multipage/canvas.html#canvastextdrawingstyles |
18 | | template<typename IncludingClass> |
19 | | class CanvasTextDrawingStyles { |
20 | | public: |
21 | | ~CanvasTextDrawingStyles() = default; |
22 | | |
23 | | ByteString font() const |
24 | 0 | { |
25 | | // When font style value is empty return default string |
26 | 0 | if (!my_drawing_state().font_style_value) { |
27 | 0 | return "10px sans-serif"; |
28 | 0 | } |
29 | | |
30 | | // On getting, the font attribute must return the serialized form of the current font of the context (with no 'line-height' component). |
31 | 0 | auto const& font_style_value = my_drawing_state().font_style_value->as_shorthand(); |
32 | 0 | auto font_style = font_style_value.longhand(CSS::PropertyID::FontStyle); |
33 | 0 | auto font_weight = font_style_value.longhand(CSS::PropertyID::FontWeight); |
34 | 0 | auto font_size = font_style_value.longhand(CSS::PropertyID::FontSize); |
35 | 0 | auto font_family = font_style_value.longhand(CSS::PropertyID::FontFamily); |
36 | 0 | return ByteString::formatted("{} {} {} {}", font_style->to_string(), font_weight->to_string(), font_size->to_string(), font_family->to_string()); |
37 | 0 | } |
38 | | |
39 | | void set_font(StringView font) |
40 | 0 | { |
41 | | // The font IDL attribute, on setting, must be parsed as a CSS <'font'> value (but without supporting property-independent style sheet syntax like 'inherit'), |
42 | | // 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, |
43 | | // and with system fonts being computed to explicit values. |
44 | | // FIXME: with the 'line-height' component forced to 'normal' |
45 | | // FIXME: with the 'font-size' component converted to CSS pixels |
46 | 0 | auto parsing_context = CSS::Parser::ParsingContext { reinterpret_cast<IncludingClass&>(*this).realm() }; |
47 | 0 | auto font_style_value_result = parse_css_value(parsing_context, font, CSS::PropertyID::Font); |
48 | | |
49 | | // 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. |
50 | | // NOTE: ShorthandStyleValue should be the only valid option here. We implicitly VERIFY this below. |
51 | 0 | if (!font_style_value_result || !font_style_value_result->is_shorthand()) { |
52 | 0 | return; |
53 | 0 | } |
54 | 0 | my_drawing_state().font_style_value = font_style_value_result.release_nonnull(); |
55 | | |
56 | | // Load font with font style value properties |
57 | 0 | auto const& font_style_value = my_drawing_state().font_style_value->as_shorthand(); |
58 | 0 | auto& canvas_element = reinterpret_cast<IncludingClass&>(*this).canvas_element(); |
59 | 0 | auto& font_style = *font_style_value.longhand(CSS::PropertyID::FontStyle); |
60 | 0 | auto& font_weight = *font_style_value.longhand(CSS::PropertyID::FontWeight); |
61 | 0 | auto& font_width = *font_style_value.longhand(CSS::PropertyID::FontWidth); |
62 | 0 | auto& font_size = *font_style_value.longhand(CSS::PropertyID::FontSize); |
63 | 0 | auto& font_family = *font_style_value.longhand(CSS::PropertyID::FontFamily); |
64 | 0 | 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); |
65 | 0 | my_drawing_state().current_font = font_list->first(); |
66 | 0 | } |
67 | | |
68 | 0 | Bindings::CanvasTextAlign text_align() const { return my_drawing_state().text_align; } |
69 | 0 | void set_text_align(Bindings::CanvasTextAlign text_align) { my_drawing_state().text_align = text_align; } |
70 | | |
71 | 0 | Bindings::CanvasTextBaseline text_baseline() const { return my_drawing_state().text_baseline; } |
72 | 0 | void set_text_baseline(Bindings::CanvasTextBaseline text_baseline) { my_drawing_state().text_baseline = text_baseline; } |
73 | | |
74 | | protected: |
75 | | CanvasTextDrawingStyles() = default; |
76 | | |
77 | | private: |
78 | 0 | CanvasState::DrawingState& my_drawing_state() { return reinterpret_cast<IncludingClass&>(*this).drawing_state(); } |
79 | 0 | CanvasState::DrawingState const& my_drawing_state() const { return reinterpret_cast<IncludingClass const&>(*this).drawing_state(); } |
80 | | }; |
81 | | |
82 | | } |