mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 01:02:45 +00:00 
			
		
		
		
	LibWeb: Make TextMetrics GC-allocated
This commit is contained in:
		
							parent
							
								
									2704bcdaaa
								
							
						
					
					
						commit
						6b7a1d13e9
					
				
					 7 changed files with 28 additions and 18 deletions
				
			
		|  | @ -470,7 +470,6 @@ class StorageWrapper; | ||||||
| class SubtleCryptoWrapper; | class SubtleCryptoWrapper; | ||||||
| class TextDecoderWrapper; | class TextDecoderWrapper; | ||||||
| class TextEncoderWrapper; | class TextEncoderWrapper; | ||||||
| class TextMetricsWrapper; |  | ||||||
| class URLSearchParamsIteratorWrapper; | class URLSearchParamsIteratorWrapper; | ||||||
| class URLSearchParamsWrapper; | class URLSearchParamsWrapper; | ||||||
| class URLWrapper; | class URLWrapper; | ||||||
|  |  | ||||||
|  | @ -19,7 +19,7 @@ public: | ||||||
| 
 | 
 | ||||||
|     virtual void fill_text(String const&, float x, float y, Optional<double> max_width) = 0; |     virtual void fill_text(String const&, float x, float y, Optional<double> max_width) = 0; | ||||||
|     virtual void stroke_text(String const&, float x, float y, Optional<double> max_width) = 0; |     virtual void stroke_text(String const&, float x, float y, Optional<double> max_width) = 0; | ||||||
|     virtual RefPtr<TextMetrics> measure_text(String const& text) = 0; |     virtual JS::NonnullGCPtr<TextMetrics> measure_text(String const& text) = 0; | ||||||
| 
 | 
 | ||||||
| protected: | protected: | ||||||
|     CanvasText() = default; |     CanvasText() = default; | ||||||
|  |  | ||||||
|  | @ -396,7 +396,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
 | ||||||
| RefPtr<TextMetrics> CanvasRenderingContext2D::measure_text(String const& text) | JS::NonnullGCPtr<TextMetrics> CanvasRenderingContext2D::measure_text(String const& 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
 | ||||||
|  | @ -404,7 +404,7 @@ RefPtr<TextMetrics> CanvasRenderingContext2D::measure_text(String const& text) | ||||||
|     // TextMetrics object with members behaving as described in the following
 |     // TextMetrics object with members behaving as described in the following
 | ||||||
|     // list:
 |     // list:
 | ||||||
|     auto prepared_text = prepare_text(text); |     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.
 |     // FIXME: Use the font that was used to create the glyphs in prepared_text.
 | ||||||
|     auto& font = Gfx::FontDatabase::default_font(); |     auto& font = Gfx::FontDatabase::default_font(); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -78,7 +78,7 @@ public: | ||||||
| 
 | 
 | ||||||
|     JS::NonnullGCPtr<HTMLCanvasElement> canvas_for_binding() const; |     JS::NonnullGCPtr<HTMLCanvasElement> canvas_for_binding() const; | ||||||
| 
 | 
 | ||||||
|     virtual RefPtr<TextMetrics> measure_text(String const& text) override; |     virtual JS::NonnullGCPtr<TextMetrics> measure_text(String const& text) override; | ||||||
| 
 | 
 | ||||||
|     virtual void clip() override; |     virtual void clip() override; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -4,13 +4,23 @@ | ||||||
|  * SPDX-License-Identifier: BSD-2-Clause |  * SPDX-License-Identifier: BSD-2-Clause | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #include "TextMetrics.h" | #include <LibWeb/Bindings/TextMetricsPrototype.h> | ||||||
|  | #include <LibWeb/HTML/TextMetrics.h> | ||||||
|  | #include <LibWeb/HTML/Window.h> | ||||||
| 
 | 
 | ||||||
| namespace Web::HTML { | namespace Web::HTML { | ||||||
| 
 | 
 | ||||||
| RefPtr<TextMetrics> TextMetrics::create() | JS::NonnullGCPtr<TextMetrics> TextMetrics::create(HTML::Window& window) | ||||||
| { | { | ||||||
|     return adopt_ref(*new TextMetrics()); |     return *window.heap().allocate<TextMetrics>(window.realm(), window); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | TextMetrics::TextMetrics(HTML::Window& window) | ||||||
|  |     : PlatformObject(window.realm()) | ||||||
|  | { | ||||||
|  |     set_prototype(&window.ensure_web_prototype<Bindings::TextMetricsPrototype>("TextMetrics")); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | TextMetrics::~TextMetrics() = default; | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -6,18 +6,17 @@ | ||||||
| 
 | 
 | ||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <LibJS/Heap/Handle.h> | #include <LibWeb/Bindings/PlatformObject.h> | ||||||
| #include <LibWeb/Bindings/Wrappable.h> |  | ||||||
| 
 | 
 | ||||||
| namespace Web::HTML { | namespace Web::HTML { | ||||||
| 
 | 
 | ||||||
| class TextMetrics | class TextMetrics : public Bindings::PlatformObject { | ||||||
|     : public RefCounted<TextMetrics> |     WEB_PLATFORM_OBJECT(TextMetrics, Bindings::PlatformObject); | ||||||
|     , public Bindings::Wrappable { |  | ||||||
| public: |  | ||||||
|     using WrapperType = Bindings::TextMetricsWrapper; |  | ||||||
| 
 | 
 | ||||||
|     static RefPtr<TextMetrics> create(); | public: | ||||||
|  |     static JS::NonnullGCPtr<TextMetrics> create(HTML::Window&); | ||||||
|  | 
 | ||||||
|  |     virtual ~TextMetrics() override; | ||||||
| 
 | 
 | ||||||
|     double width() const { return m_width; } |     double width() const { return m_width; } | ||||||
|     double actual_bounding_box_left() const { return m_actual_bounding_box_left; } |     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; } |     void set_ideographic_baseline(double baseline) { m_ideographic_baseline = baseline; } | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     explicit TextMetrics() = default; |     explicit TextMetrics(HTML::Window&); | ||||||
| 
 | 
 | ||||||
|     double m_width { 0 }; |     double m_width { 0 }; | ||||||
|     double m_actual_bounding_box_left { 0 }; |     double m_actual_bounding_box_left { 0 }; | ||||||
|  | @ -64,3 +63,5 @@ private: | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | WRAPPER_HACK(TextMetrics, Web::HTML) | ||||||
|  |  | ||||||
|  | @ -149,7 +149,7 @@ libweb_js_wrapper(HTML/Path2D NO_INSTANCE) | ||||||
| libweb_js_wrapper(HTML/PromiseRejectionEvent NO_INSTANCE) | libweb_js_wrapper(HTML/PromiseRejectionEvent NO_INSTANCE) | ||||||
| libweb_js_wrapper(HTML/Storage) | libweb_js_wrapper(HTML/Storage) | ||||||
| libweb_js_wrapper(HTML/SubmitEvent NO_INSTANCE) | 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/Worker NO_INSTANCE) | ||||||
| libweb_js_wrapper(HTML/WorkerGlobalScope NO_INSTANCE) | libweb_js_wrapper(HTML/WorkerGlobalScope NO_INSTANCE) | ||||||
| libweb_js_wrapper(HTML/WorkerLocation) | libweb_js_wrapper(HTML/WorkerLocation) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling