diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 49e4ac68c3..e3f920d634 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -470,7 +470,6 @@ class StorageWrapper; class SubtleCryptoWrapper; class TextDecoderWrapper; class TextEncoderWrapper; -class TextMetricsWrapper; class URLSearchParamsIteratorWrapper; class URLSearchParamsWrapper; class URLWrapper; diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.h b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.h index d49e07df10..b84a188ab1 100644 --- a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.h +++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasText.h @@ -19,7 +19,7 @@ public: virtual void fill_text(String const&, float x, float y, Optional max_width) = 0; virtual void stroke_text(String const&, float x, float y, Optional max_width) = 0; - virtual RefPtr measure_text(String const& text) = 0; + virtual JS::NonnullGCPtr measure_text(String const& text) = 0; protected: CanvasText() = default; diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 5136a4c997..d0c5e2ff03 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -396,7 +396,7 @@ void CanvasRenderingContext2D::reset_to_default_state() } // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-measuretext -RefPtr CanvasRenderingContext2D::measure_text(String const& text) +JS::NonnullGCPtr CanvasRenderingContext2D::measure_text(String const& text) { // The measureText(text) method steps are to run the text preparation // algorithm, passing it text and the object implementing the CanvasText @@ -404,7 +404,7 @@ RefPtr CanvasRenderingContext2D::measure_text(String const& text) // TextMetrics object with members behaving as described in the following // list: auto prepared_text = prepare_text(text); - auto metrics = TextMetrics::create(); + auto metrics = TextMetrics::create(global_object()); // FIXME: Use the font that was used to create the glyphs in prepared_text. auto& font = Gfx::FontDatabase::default_font(); diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index 8c5e7987ea..45ca738db6 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -78,7 +78,7 @@ public: JS::NonnullGCPtr canvas_for_binding() const; - virtual RefPtr measure_text(String const& text) override; + virtual JS::NonnullGCPtr measure_text(String const& text) override; virtual void clip() override; diff --git a/Userland/Libraries/LibWeb/HTML/TextMetrics.cpp b/Userland/Libraries/LibWeb/HTML/TextMetrics.cpp index ed1b25497a..289ede7bba 100644 --- a/Userland/Libraries/LibWeb/HTML/TextMetrics.cpp +++ b/Userland/Libraries/LibWeb/HTML/TextMetrics.cpp @@ -4,13 +4,23 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "TextMetrics.h" +#include +#include +#include namespace Web::HTML { -RefPtr TextMetrics::create() +JS::NonnullGCPtr TextMetrics::create(HTML::Window& window) { - return adopt_ref(*new TextMetrics()); + return *window.heap().allocate(window.realm(), window); } +TextMetrics::TextMetrics(HTML::Window& window) + : PlatformObject(window.realm()) +{ + set_prototype(&window.ensure_web_prototype("TextMetrics")); +} + +TextMetrics::~TextMetrics() = default; + } diff --git a/Userland/Libraries/LibWeb/HTML/TextMetrics.h b/Userland/Libraries/LibWeb/HTML/TextMetrics.h index 13943b4ead..fad818b9db 100644 --- a/Userland/Libraries/LibWeb/HTML/TextMetrics.h +++ b/Userland/Libraries/LibWeb/HTML/TextMetrics.h @@ -6,18 +6,17 @@ #pragma once -#include -#include +#include namespace Web::HTML { -class TextMetrics - : public RefCounted - , public Bindings::Wrappable { -public: - using WrapperType = Bindings::TextMetricsWrapper; +class TextMetrics : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(TextMetrics, Bindings::PlatformObject); - static RefPtr create(); +public: + static JS::NonnullGCPtr create(HTML::Window&); + + virtual ~TextMetrics() override; double width() const { return m_width; } double actual_bounding_box_left() const { return m_actual_bounding_box_left; } @@ -46,7 +45,7 @@ public: void set_ideographic_baseline(double baseline) { m_ideographic_baseline = baseline; } private: - explicit TextMetrics() = default; + explicit TextMetrics(HTML::Window&); double m_width { 0 }; double m_actual_bounding_box_left { 0 }; @@ -64,3 +63,5 @@ private: }; } + +WRAPPER_HACK(TextMetrics, Web::HTML) diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 915c240e6e..85fe394a75 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -149,7 +149,7 @@ libweb_js_wrapper(HTML/Path2D NO_INSTANCE) libweb_js_wrapper(HTML/PromiseRejectionEvent NO_INSTANCE) libweb_js_wrapper(HTML/Storage) libweb_js_wrapper(HTML/SubmitEvent NO_INSTANCE) -libweb_js_wrapper(HTML/TextMetrics) +libweb_js_wrapper(HTML/TextMetrics NO_INSTANCE) libweb_js_wrapper(HTML/Worker NO_INSTANCE) libweb_js_wrapper(HTML/WorkerGlobalScope NO_INSTANCE) libweb_js_wrapper(HTML/WorkerLocation)