summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-04-15 20:36:10 +0300
committerAndreas Kling <kling@serenityos.org>2021-04-15 20:22:08 +0200
commit00114ab01dff8a0916840c89f0fc3bc288b7de15 (patch)
tree899648d51a50448a515de5ea902d97dbd85d7f52 /Userland/Libraries/LibWeb/HTML
parenta257ef0f35c1a744e5c284c6752efc69db006e91 (diff)
downloadserenity-00114ab01dff8a0916840c89f0fc3bc288b7de15.zip
LibWeb: Add a naive implemention of CanvasRenderingContext2D::fill_text
This doesnt actually account for several unimplemented attributes (like ltr/rtl, alignment, etc) yet, so this should be expanded in the future.
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML')
-rw-r--r--Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp16
-rw-r--r--Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h2
-rw-r--r--Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl2
3 files changed, 20 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
index 00bc5029f2..0a8bfa7ab2 100644
--- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
+++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
@@ -163,6 +163,22 @@ OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
return make<Gfx::Painter>(*m_element->bitmap());
}
+void CanvasRenderingContext2D::fill_text(const String& text, float x, float y, Optional<double> max_width)
+{
+ if (max_width.has_value() && max_width.value() <= 0)
+ return;
+
+ auto painter = this->painter();
+ if (!painter)
+ return;
+
+ // FIXME: painter only supports integer rects for text right now, so this effectively chops off any fractional position
+ auto text_rect = Gfx::IntRect(x, y, max_width.has_value() ? max_width.value() : painter->font().width(text), painter->font().glyph_height());
+ auto transformed_rect = m_transform.map(text_rect);
+ painter->draw_text(transformed_rect, text, Gfx::TextAlignment::TopLeft, m_fill_style);
+ did_draw(transformed_rect.to<float>());
+}
+
void CanvasRenderingContext2D::begin_path()
{
m_path = Gfx::Path();
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
index c083f1b574..53f5674b52 100644
--- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
+++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
@@ -80,6 +80,8 @@ public:
void rect(float x, float y, float width, float height);
void stroke();
+ void fill_text(const String&, float x, float y, Optional<double> max_width);
+
// FIXME: We should only have one fill(), really. Fix the wrapper generator!
void fill(Gfx::Painter::WindingRule);
void fill(const String& fill_rule);
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl
index 25b87241fb..c5a5b7bf6f 100644
--- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl
+++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl
@@ -20,6 +20,8 @@ interface CanvasRenderingContext2D {
undefined ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, optional boolean counterclockwise = false);
undefined rect(double x, double y, double width, double height);
+ undefined fillText(DOMString text, double x, double y, optional double maxWidth);
+
undefined drawImage(HTMLImageElement image, double dx, double dy);
attribute DOMString fillStyle;