1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:47:45 +00:00

LibWeb: Port CanvasRenderingContext2D from DeprecatedString to String

This commit is contained in:
Shannon Booth 2023-09-06 19:41:48 +12:00 committed by Andrew Kaster
parent da1f137967
commit d312fdc2d3
8 changed files with 29 additions and 29 deletions

View file

@ -18,14 +18,14 @@ public:
virtual void begin_path() = 0; virtual void begin_path() = 0;
virtual void fill(DeprecatedString const& fill_rule) = 0; virtual void fill(StringView fill_rule) = 0;
virtual void fill(Path2D& path, DeprecatedString const& fill_rule) = 0; virtual void fill(Path2D& path, StringView fill_rule) = 0;
virtual void stroke() = 0; virtual void stroke() = 0;
virtual void stroke(Path2D const& path) = 0; virtual void stroke(Path2D const& path) = 0;
virtual void clip(DeprecatedString const& fill_rule) = 0; virtual void clip(StringView fill_rule) = 0;
virtual void clip(Path2D& path, DeprecatedString const& fill_rule) = 0; virtual void clip(Path2D& path, StringView fill_rule) = 0;
protected: protected:
CanvasDrawPath() = default; CanvasDrawPath() = default;

View file

@ -9,7 +9,7 @@
#pragma once #pragma once
#include <AK/DeprecatedString.h> #include <AK/String.h>
#include <LibWeb/HTML/Canvas/CanvasState.h> #include <LibWeb/HTML/Canvas/CanvasState.h>
#include <LibWeb/HTML/CanvasGradient.h> #include <LibWeb/HTML/CanvasGradient.h>
#include <LibWeb/HTML/CanvasPattern.h> #include <LibWeb/HTML/CanvasPattern.h>
@ -21,12 +21,12 @@ template<typename IncludingClass>
class CanvasFillStrokeStyles { class CanvasFillStrokeStyles {
public: public:
~CanvasFillStrokeStyles() = default; ~CanvasFillStrokeStyles() = default;
using FillOrStrokeStyleVariant = Variant<DeprecatedString, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>; using FillOrStrokeStyleVariant = Variant<String, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>;
static CanvasState::FillOrStrokeStyle to_canvas_state_fill_or_stroke_style(auto const& style) static CanvasState::FillOrStrokeStyle to_canvas_state_fill_or_stroke_style(auto const& style)
{ {
return style.visit( return style.visit(
[&](DeprecatedString const& string) -> CanvasState::FillOrStrokeStyle { [&](String const& string) -> CanvasState::FillOrStrokeStyle {
return Gfx::Color::from_string(string).value_or(Color::Black); return Gfx::Color::from_string(string).value_or(Color::Black);
}, },
[&](auto fill_or_stroke_style) -> CanvasState::FillOrStrokeStyle { [&](auto fill_or_stroke_style) -> CanvasState::FillOrStrokeStyle {

View file

@ -52,13 +52,13 @@ public:
Optional<Gfx::Color> as_color() const; Optional<Gfx::Color> as_color() const;
Gfx::Color to_color_but_fixme_should_accept_any_paint_style() const; Gfx::Color to_color_but_fixme_should_accept_any_paint_style() const;
using JsFillOrStrokeStyle = Variant<DeprecatedString, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>; using JsFillOrStrokeStyle = Variant<String, JS::Handle<CanvasGradient>, JS::Handle<CanvasPattern>>;
JsFillOrStrokeStyle to_js_fill_or_stroke_style() const JsFillOrStrokeStyle to_js_fill_or_stroke_style() const
{ {
return m_fill_or_stroke_style.visit( return m_fill_or_stroke_style.visit(
[&](Gfx::Color color) -> JsFillOrStrokeStyle { [&](Gfx::Color color) -> JsFillOrStrokeStyle {
return color.to_deprecated_string(); return MUST(String::from_deprecated_string(color.to_deprecated_string()));
}, },
[&](auto handle) -> JsFillOrStrokeStyle { [&](auto handle) -> JsFillOrStrokeStyle {
return handle; return handle;

View file

@ -17,9 +17,9 @@ class CanvasText {
public: public:
virtual ~CanvasText() = default; virtual ~CanvasText() = default;
virtual void fill_text(DeprecatedString const&, float x, float y, Optional<double> max_width) = 0; virtual void fill_text(StringView, float x, float y, Optional<double> max_width) = 0;
virtual void stroke_text(DeprecatedString const&, float x, float y, Optional<double> max_width) = 0; virtual void stroke_text(StringView, float x, float y, Optional<double> max_width) = 0;
virtual JS::NonnullGCPtr<TextMetrics> measure_text(DeprecatedString const& text) = 0; virtual JS::NonnullGCPtr<TextMetrics> measure_text(StringView text) = 0;
protected: protected:
CanvasText() = default; CanvasText() = default;

View file

@ -36,7 +36,7 @@ public:
font_style_value.font_families()->to_string()); font_style_value.font_families()->to_string());
} }
void set_font(DeprecatedString const& font) void set_font(StringView font)
{ {
// The font IDL attribute, on setting, must be parsed as a CSS <'font'> value (but without supporting property-independent style sheet syntax like 'inherit'), // The font IDL attribute, on setting, must be parsed as a CSS <'font'> value (but without supporting property-independent style sheet syntax like 'inherit'),
// 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, // 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,

View file

@ -195,7 +195,7 @@ Optional<Gfx::AntiAliasingPainter> CanvasRenderingContext2D::antialiased_painter
return {}; return {};
} }
void CanvasRenderingContext2D::fill_text(DeprecatedString const& text, float x, float y, Optional<double> max_width) void CanvasRenderingContext2D::fill_text(StringView text, float x, float y, Optional<double> max_width)
{ {
if (max_width.has_value() && max_width.value() <= 0) if (max_width.has_value() && max_width.value() <= 0)
return; return;
@ -239,7 +239,7 @@ void CanvasRenderingContext2D::fill_text(DeprecatedString const& text, float x,
}); });
} }
void CanvasRenderingContext2D::stroke_text(DeprecatedString const& text, float x, float y, Optional<double> max_width) void CanvasRenderingContext2D::stroke_text(StringView text, float x, float y, Optional<double> max_width)
{ {
// FIXME: Stroke the text instead of filling it. // FIXME: Stroke the text instead of filling it.
fill_text(text, x, y, max_width); fill_text(text, x, y, max_width);
@ -299,12 +299,12 @@ void CanvasRenderingContext2D::fill_internal(Gfx::Path const& path, Gfx::Painter
}); });
} }
void CanvasRenderingContext2D::fill(DeprecatedString const& fill_rule) void CanvasRenderingContext2D::fill(StringView fill_rule)
{ {
return fill_internal(path(), parse_fill_rule(fill_rule)); return fill_internal(path(), parse_fill_rule(fill_rule));
} }
void CanvasRenderingContext2D::fill(Path2D& path, DeprecatedString const& fill_rule) void CanvasRenderingContext2D::fill(Path2D& path, StringView fill_rule)
{ {
auto transformed_path = path.path().copy_transformed(drawing_state().transform); auto transformed_path = path.path().copy_transformed(drawing_state().transform);
return fill_internal(transformed_path, parse_fill_rule(fill_rule)); return fill_internal(transformed_path, parse_fill_rule(fill_rule));
@ -386,7 +386,7 @@ void CanvasRenderingContext2D::reset_to_default_state()
} }
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-measuretext // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-measuretext
JS::NonnullGCPtr<TextMetrics> CanvasRenderingContext2D::measure_text(DeprecatedString const& text) JS::NonnullGCPtr<TextMetrics> CanvasRenderingContext2D::measure_text(StringView text)
{ {
// The measureText(text) method steps are to run the text preparation // The measureText(text) method steps are to run the text preparation
// algorithm, passing it text and the object implementing the CanvasText // algorithm, passing it text and the object implementing the CanvasText
@ -430,7 +430,7 @@ RefPtr<Gfx::Font const> CanvasRenderingContext2D::current_font()
{ {
// When font style value is empty load default font // When font style value is empty load default font
if (!drawing_state().font_style_value) { if (!drawing_state().font_style_value) {
set_font("10px sans-serif"); set_font("10px sans-serif"sv);
} }
// Get current loaded font // Get current loaded font
@ -533,13 +533,13 @@ void CanvasRenderingContext2D::clip_internal(Gfx::Path& path, Gfx::Painter::Wind
drawing_state().clip = CanvasClip { path, winding_rule }; drawing_state().clip = CanvasClip { path, winding_rule };
} }
void CanvasRenderingContext2D::clip(DeprecatedString const& fill_rule) void CanvasRenderingContext2D::clip(StringView fill_rule)
{ {
auto transformed_path = path().copy_transformed(drawing_state().transform); auto transformed_path = path().copy_transformed(drawing_state().transform);
return clip_internal(transformed_path, parse_fill_rule(fill_rule)); return clip_internal(transformed_path, parse_fill_rule(fill_rule));
} }
void CanvasRenderingContext2D::clip(Path2D& path, DeprecatedString const& fill_rule) void CanvasRenderingContext2D::clip(Path2D& path, StringView fill_rule)
{ {
auto transformed_path = path.path().copy_transformed(drawing_state().transform); auto transformed_path = path.path().copy_transformed(drawing_state().transform);
return clip_internal(transformed_path, parse_fill_rule(fill_rule)); return clip_internal(transformed_path, parse_fill_rule(fill_rule));

View file

@ -73,11 +73,11 @@ public:
virtual void stroke() override; virtual void stroke() override;
virtual void stroke(Path2D const& path) override; virtual void stroke(Path2D const& path) override;
virtual void fill_text(DeprecatedString const&, float x, float y, Optional<double> max_width) override; virtual void fill_text(StringView, float x, float y, Optional<double> max_width) override;
virtual void stroke_text(DeprecatedString const&, float x, float y, Optional<double> max_width) override; virtual void stroke_text(StringView, float x, float y, Optional<double> max_width) override;
virtual void fill(DeprecatedString const& fill_rule) override; virtual void fill(StringView fill_rule) override;
virtual void fill(Path2D& path, DeprecatedString const& fill_rule) override; virtual void fill(Path2D& path, StringView fill_rule) override;
virtual JS::GCPtr<ImageData> create_image_data(int width, int height) const override; virtual JS::GCPtr<ImageData> create_image_data(int width, int height) const override;
virtual WebIDL::ExceptionOr<JS::GCPtr<ImageData>> get_image_data(int x, int y, int width, int height) const override; virtual WebIDL::ExceptionOr<JS::GCPtr<ImageData>> get_image_data(int x, int y, int width, int height) const override;
@ -87,10 +87,10 @@ public:
JS::NonnullGCPtr<HTMLCanvasElement> canvas_for_binding() const; JS::NonnullGCPtr<HTMLCanvasElement> canvas_for_binding() const;
virtual JS::NonnullGCPtr<TextMetrics> measure_text(DeprecatedString const& text) override; virtual JS::NonnullGCPtr<TextMetrics> measure_text(StringView text) override;
virtual void clip(DeprecatedString const& fill_rule) override; virtual void clip(StringView fill_rule) override;
virtual void clip(Path2D& path, DeprecatedString const& fill_rule) override; virtual void clip(Path2D& path, StringView fill_rule) override;
virtual bool image_smoothing_enabled() const override; virtual bool image_smoothing_enabled() const override;
virtual void set_image_smoothing_enabled(bool) override; virtual void set_image_smoothing_enabled(bool) override;

View file

@ -20,7 +20,7 @@ enum CanvasTextAlign { "start", "end", "left", "right", "center" };
enum CanvasTextBaseline { "top", "hanging", "middle", "alphabetic", "ideographic", "bottom" }; enum CanvasTextBaseline { "top", "hanging", "middle", "alphabetic", "ideographic", "bottom" };
// https://html.spec.whatwg.org/multipage/canvas.html#canvasrenderingcontext2d // https://html.spec.whatwg.org/multipage/canvas.html#canvasrenderingcontext2d
[Exposed=Window, UseDeprecatedAKString] [Exposed=Window]
interface CanvasRenderingContext2D { interface CanvasRenderingContext2D {
[ImplementedAs=canvas_for_binding] readonly attribute HTMLCanvasElement canvas; [ImplementedAs=canvas_for_binding] readonly attribute HTMLCanvasElement canvas;
}; };