فهرست منبع

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.
Idan Horowitz 4 سال پیش
والد
کامیت
00114ab01d

+ 16 - 0
Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp

@@ -163,6 +163,22 @@ OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
     return make<Gfx::Painter>(*m_element->bitmap());
     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()
 void CanvasRenderingContext2D::begin_path()
 {
 {
     m_path = Gfx::Path();
     m_path = Gfx::Path();

+ 2 - 0
Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h

@@ -80,6 +80,8 @@ public:
     void rect(float x, float y, float width, float height);
     void rect(float x, float y, float width, float height);
     void stroke();
     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!
     // FIXME: We should only have one fill(), really. Fix the wrapper generator!
     void fill(Gfx::Painter::WindingRule);
     void fill(Gfx::Painter::WindingRule);
     void fill(const String& fill_rule);
     void fill(const String& fill_rule);

+ 2 - 0
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 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 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);
     undefined drawImage(HTMLImageElement image, double dx, double dy);
 
 
     attribute DOMString fillStyle;
     attribute DOMString fillStyle;