mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-08 06:09:58 +00:00
LibWeb: Use Utf16String in CanvasText APIs
This is prep work for getting rid of UTF-8 text shaping.
This commit is contained in:
parent
5bd867f1dc
commit
434bf30cda
Notes:
github-actions[bot]
2025-09-21 11:24:04 +00:00
Author: https://github.com/awesomekling
Commit: 434bf30cda
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6254
Reviewed-by: https://github.com/tcl3
6 changed files with 26 additions and 26 deletions
|
|
@ -17,9 +17,9 @@ class CanvasText {
|
|||
public:
|
||||
virtual ~CanvasText() = default;
|
||||
|
||||
virtual void fill_text(StringView, float x, float y, Optional<double> max_width) = 0;
|
||||
virtual void stroke_text(StringView, float x, float y, Optional<double> max_width) = 0;
|
||||
virtual GC::Ref<TextMetrics> measure_text(StringView text) = 0;
|
||||
virtual void fill_text(Utf16String const&, float x, float y, Optional<double> max_width) = 0;
|
||||
virtual void stroke_text(Utf16String const&, float x, float y, Optional<double> max_width) = 0;
|
||||
virtual GC::Ref<TextMetrics> measure_text(Utf16String const&) = 0;
|
||||
|
||||
protected:
|
||||
CanvasText() = default;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
// https://html.spec.whatwg.org/multipage/canvas.html#canvastext
|
||||
interface mixin CanvasText {
|
||||
undefined fillText(DOMString text, unrestricted double x, unrestricted double y, optional unrestricted double maxWidth);
|
||||
undefined strokeText(DOMString text, unrestricted double x, unrestricted double y, optional unrestricted double maxWidth);
|
||||
TextMetrics measureText(DOMString text);
|
||||
undefined fillText(Utf16DOMString text, unrestricted double x, unrestricted double y, optional unrestricted double maxWidth);
|
||||
undefined strokeText(Utf16DOMString text, unrestricted double x, unrestricted double y, optional unrestricted double maxWidth);
|
||||
TextMetrics measureText(Utf16DOMString text);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -250,7 +250,7 @@ void CanvasRenderingContext2D::allocate_painting_surface_if_needed()
|
|||
}
|
||||
}
|
||||
|
||||
Gfx::Path CanvasRenderingContext2D::text_path(StringView text, float x, float y, Optional<double> max_width)
|
||||
Gfx::Path CanvasRenderingContext2D::text_path(Utf16String const& text, float x, float y, Optional<double> max_width)
|
||||
{
|
||||
if (max_width.has_value() && max_width.value() <= 0)
|
||||
return {};
|
||||
|
|
@ -259,7 +259,7 @@ Gfx::Path CanvasRenderingContext2D::text_path(StringView text, float x, float y,
|
|||
|
||||
auto const& font_cascade_list = this->font_cascade_list();
|
||||
auto const& font = font_cascade_list->first();
|
||||
auto glyph_runs = Gfx::shape_text({ x, y }, Utf8View(text), *font_cascade_list);
|
||||
auto glyph_runs = Gfx::shape_text({ x, y }, text.utf16_view(), *font_cascade_list);
|
||||
Gfx::Path path;
|
||||
for (auto const& glyph_run : glyph_runs) {
|
||||
path.glyph_run(glyph_run);
|
||||
|
|
@ -305,12 +305,12 @@ Gfx::Path CanvasRenderingContext2D::text_path(StringView text, float x, float y,
|
|||
return path.copy_transformed(transform);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2D::fill_text(StringView text, float x, float y, Optional<double> max_width)
|
||||
void CanvasRenderingContext2D::fill_text(Utf16String const& text, float x, float y, Optional<double> max_width)
|
||||
{
|
||||
fill_internal(text_path(text, x, y, max_width), Gfx::WindingRule::Nonzero);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2D::stroke_text(StringView text, float x, float y, Optional<double> max_width)
|
||||
void CanvasRenderingContext2D::stroke_text(Utf16String const& text, float x, float y, Optional<double> max_width)
|
||||
{
|
||||
stroke_internal(text_path(text, x, y, max_width));
|
||||
}
|
||||
|
|
@ -554,7 +554,7 @@ void CanvasRenderingContext2D::reset_to_default_state()
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-measuretext
|
||||
GC::Ref<TextMetrics> CanvasRenderingContext2D::measure_text(StringView text)
|
||||
GC::Ref<TextMetrics> CanvasRenderingContext2D::measure_text(Utf16String const& text)
|
||||
{
|
||||
// The measureText(text) method steps are to run the text preparation
|
||||
// algorithm, passing it text and the object implementing the CanvasText
|
||||
|
|
@ -606,7 +606,7 @@ RefPtr<Gfx::FontCascadeList const> CanvasRenderingContext2D::font_cascade_list()
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/canvas.html#text-preparation-algorithm
|
||||
CanvasRenderingContext2D::PreparedText CanvasRenderingContext2D::prepare_text(ByteString const& text, float max_width)
|
||||
CanvasRenderingContext2D::PreparedText CanvasRenderingContext2D::prepare_text(Utf16String const& text, float max_width)
|
||||
{
|
||||
// 1. If maxWidth was provided but is less than or equal to zero or equal to NaN, then return an empty array.
|
||||
if (max_width <= 0 || max_width != max_width) {
|
||||
|
|
@ -614,14 +614,14 @@ CanvasRenderingContext2D::PreparedText CanvasRenderingContext2D::prepare_text(By
|
|||
}
|
||||
|
||||
// 2. Replace all ASCII whitespace in text with U+0020 SPACE characters.
|
||||
StringBuilder builder { text.length() };
|
||||
StringBuilder builder { StringBuilder::Mode::UTF16, text.length_in_code_units() };
|
||||
for (auto c : text) {
|
||||
builder.append(Infra::is_ascii_whitespace(c) ? ' ' : c);
|
||||
}
|
||||
auto replaced_text = MUST(builder.to_string());
|
||||
auto replaced_text = builder.to_utf16_string();
|
||||
|
||||
// 3. Let font be the current font of target, as given by that object's font attribute.
|
||||
auto glyph_runs = Gfx::shape_text({ 0, 0 }, Utf8View(replaced_text), *font_cascade_list());
|
||||
auto glyph_runs = Gfx::shape_text({ 0, 0 }, replaced_text.utf16_view(), *font_cascade_list());
|
||||
|
||||
// FIXME: 4. Let language be the target's language.
|
||||
// FIXME: 5. If language is "inherit":
|
||||
|
|
|
|||
|
|
@ -70,8 +70,8 @@ public:
|
|||
virtual void stroke() override;
|
||||
virtual void stroke(Path2D const& path) override;
|
||||
|
||||
virtual void fill_text(StringView, 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_text(Utf16String const&, float x, float y, Optional<double> max_width) override;
|
||||
virtual void stroke_text(Utf16String const&, float x, float y, Optional<double> max_width) override;
|
||||
|
||||
virtual void fill(StringView fill_rule) override;
|
||||
virtual void fill(Path2D& path, StringView fill_rule) override;
|
||||
|
|
@ -87,7 +87,7 @@ public:
|
|||
|
||||
virtual CanvasRenderingContext2DSettings get_context_attributes() const override { return m_context_attributes; }
|
||||
|
||||
virtual GC::Ref<TextMetrics> measure_text(StringView text) override;
|
||||
virtual GC::Ref<TextMetrics> measure_text(Utf16String const&) override;
|
||||
|
||||
virtual void clip(StringView fill_rule) override;
|
||||
virtual void clip(Path2D& path, StringView fill_rule) override;
|
||||
|
|
@ -147,10 +147,10 @@ private:
|
|||
|
||||
RefPtr<Gfx::FontCascadeList const> font_cascade_list();
|
||||
|
||||
PreparedText prepare_text(ByteString const& text, float max_width = INFINITY);
|
||||
PreparedText prepare_text(Utf16String const&, float max_width = INFINITY);
|
||||
|
||||
[[nodiscard]] Gfx::Path rect_path(float x, float y, float width, float height);
|
||||
[[nodiscard]] Gfx::Path text_path(StringView text, float x, float y, Optional<double> max_width);
|
||||
[[nodiscard]] Gfx::Path text_path(Utf16String const&, float x, float y, Optional<double> max_width);
|
||||
|
||||
Gfx::Color clear_color() const;
|
||||
|
||||
|
|
|
|||
|
|
@ -119,12 +119,12 @@ void OffscreenCanvasRenderingContext2D::stroke(Path2D const&)
|
|||
dbgln("(STUBBED) OffscreenCanvasRenderingContext2D::stroke(Path2D)");
|
||||
}
|
||||
|
||||
void OffscreenCanvasRenderingContext2D::fill_text(StringView, float, float, Optional<double>)
|
||||
void OffscreenCanvasRenderingContext2D::fill_text(Utf16String const&, float, float, Optional<double>)
|
||||
{
|
||||
dbgln("(STUBBED) OffscreenCanvasRenderingContext2D::fill_text()");
|
||||
}
|
||||
|
||||
void OffscreenCanvasRenderingContext2D::stroke_text(StringView, float, float, Optional<double>)
|
||||
void OffscreenCanvasRenderingContext2D::stroke_text(Utf16String const&, float, float, Optional<double>)
|
||||
{
|
||||
dbgln("(STUBBED) OffscreenCanvasRenderingContext2D::stroke_text()");
|
||||
}
|
||||
|
|
@ -166,7 +166,7 @@ void OffscreenCanvasRenderingContext2D::reset_to_default_state()
|
|||
dbgln("(STUBBED) OffscreenCanvasRenderingContext2D::reset_to_default_state()");
|
||||
}
|
||||
|
||||
GC::Ref<TextMetrics> OffscreenCanvasRenderingContext2D::measure_text(StringView)
|
||||
GC::Ref<TextMetrics> OffscreenCanvasRenderingContext2D::measure_text(Utf16String const&)
|
||||
{
|
||||
dbgln("(STUBBED) OffscreenCanvasRenderingContext2D::measure_text()");
|
||||
|
||||
|
|
|
|||
|
|
@ -75,8 +75,8 @@ public:
|
|||
virtual void stroke() override;
|
||||
virtual void stroke(Path2D const& path) override;
|
||||
|
||||
virtual void fill_text(StringView, 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_text(Utf16String const&, float x, float y, Optional<double> max_width) override;
|
||||
virtual void stroke_text(Utf16String const&, float x, float y, Optional<double> max_width) override;
|
||||
|
||||
virtual void fill(StringView fill_rule) override;
|
||||
virtual void fill(Path2D& path, StringView fill_rule) override;
|
||||
|
|
@ -90,7 +90,7 @@ public:
|
|||
|
||||
virtual CanvasRenderingContext2DSettings get_context_attributes() const override { return m_context_attributes; }
|
||||
|
||||
virtual GC::Ref<TextMetrics> measure_text(StringView text) override;
|
||||
virtual GC::Ref<TextMetrics> measure_text(Utf16String const&) override;
|
||||
|
||||
virtual void clip(StringView fill_rule) override;
|
||||
virtual void clip(Path2D& path, StringView fill_rule) override;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue