Bladeren bron

LibGfx: Replace 'bool dotted' with a LineStyle::{Solid,Dotted} enum

Just a bool is insufficient as we'll have to support dashed lines as well.
Linus Groh 5 jaren geleden
bovenliggende
commit
59d00e5df6
3 gewijzigde bestanden met toevoegingen van 21 en 11 verwijderingen
  1. 7 7
      Libraries/LibGfx/Painter.cpp
  2. 8 2
      Libraries/LibGfx/Painter.h
  3. 6 2
      Libraries/LibWeb/Layout/LayoutBox.cpp

+ 7 - 7
Libraries/LibGfx/Painter.cpp

@@ -966,7 +966,7 @@ void Painter::draw_pixel(const Point& position, Color color, int thickness)
     fill_rect(rect.translated(-state().translation), color);
 }
 
-void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thickness, bool dotted)
+void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thickness, LineStyle style)
 {
     auto clip_rect = this->clip_rect();
 
@@ -989,7 +989,7 @@ void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thick
             return;
         int min_y = max(point1.y(), clip_rect.top());
         int max_y = min(point2.y(), clip_rect.bottom());
-        if (dotted) {
+        if (style == LineStyle::Dotted) {
             for (int y = min_y; y <= max_y; y += thickness * 2)
                 draw_pixel({ x, y }, color, thickness);
         } else {
@@ -1012,7 +1012,7 @@ void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thick
             return;
         int min_x = max(point1.x(), clip_rect.left());
         int max_x = min(point2.x(), clip_rect.right());
-        if (dotted) {
+        if (style == LineStyle::Dotted) {
             for (int x = min_x; x <= max_x; x += thickness * 2)
                 draw_pixel({ x, y }, color, thickness);
         } else {
@@ -1023,7 +1023,7 @@ void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thick
     }
 
     // FIXME: Implement dotted diagonal lines.
-    ASSERT(!dotted);
+    ASSERT(style == LineStyle::Solid);
 
     const double adx = abs(point2.x() - point1.x());
     const double ady = abs(point2.y() - point1.y());
@@ -1116,10 +1116,10 @@ void Painter::for_each_line_segment_on_bezier_curve(const FloatPoint& control_po
     for_each_line_segment_on_bezier_curve(control_point, p1, p2, callback);
 }
 
-void Painter::draw_quadratic_bezier_curve(const Point& control_point, const Point& p1, const Point& p2, Color color, int thickness, bool dotted)
+void Painter::draw_quadratic_bezier_curve(const Point& control_point, const Point& p1, const Point& p2, Color color, int thickness, LineStyle style)
 {
     for_each_line_segment_on_bezier_curve(FloatPoint(control_point.x(), control_point.y()), FloatPoint(p1.x(), p1.y()), FloatPoint(p2.x(), p2.y()), [&](const FloatPoint& p1, const FloatPoint& p2) {
-        draw_line(Point(p1.x(), p1.y()), Point(p2.x(), p2.y()), color, thickness, dotted);
+        draw_line(Point(p1.x(), p1.y()), Point(p2.x(), p2.y()), color, thickness, style);
     });
 }
 
@@ -1263,7 +1263,7 @@ void Painter::fill_path(Path& path, Color color, WindingRule winding_rule)
 #ifdef FILL_PATH_DEBUG
                         dbg() << "y=" << scanline << ": " << winding_number << " at " << i << ": " << from << " -- " << to;
 #endif
-                        draw_line(from, to, color, 1, false);
+                        draw_line(from, to, color, 1);
                     }
 
                 skip_drawing:;

+ 8 - 2
Libraries/LibGfx/Painter.h

@@ -43,6 +43,12 @@ class Painter {
 public:
     explicit Painter(Gfx::Bitmap&);
     ~Painter();
+
+    enum class LineStyle {
+        Solid,
+        Dotted,
+    };
+
     void clear_rect(const Rect&, Color);
     void fill_rect(const Rect&, Color);
     void fill_rect_with_dither_pattern(const Rect&, Color, Color);
@@ -55,8 +61,8 @@ public:
     void draw_triangle(const Point&, const Point&, const Point&, Color);
     void draw_ellipse_intersecting(const Rect&, Color, int thickness = 1);
     void set_pixel(const Point&, Color);
-    void draw_line(const Point&, const Point&, Color, int thickness = 1, bool dotted = false);
-    void draw_quadratic_bezier_curve(const Point& control_point, const Point&, const Point&, Color, int thickness = 1, bool dotted = false);
+    void draw_line(const Point&, const Point&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
+    void draw_quadratic_bezier_curve(const Point& control_point, const Point&, const Point&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
     void draw_scaled_bitmap(const Rect& dst_rect, const Gfx::Bitmap&, const Rect& src_rect);
     void blit(const Point&, const Gfx::Bitmap&, const Rect& src_rect, float opacity = 1.0f);
     void blit_dimmed(const Point&, const Gfx::Bitmap&, const Rect& src_rect);

+ 6 - 2
Libraries/LibWeb/Layout/LayoutBox.cpp

@@ -102,10 +102,14 @@ void LayoutBox::paint_border(RenderingContext& context, Edge edge, const Gfx::Fl
         color = (edge == Edge::Left || edge == Edge::Top) ? top_left_color : bottom_right_color;
     }
 
-    bool dotted = border_style.has_value() && border_style.value()->to_string() == "dotted";
+    auto line_style = Gfx::Painter::LineStyle::Solid;
+    if (border_style.has_value()) {
+        if (border_style.value()->to_string() == "dotted")
+            line_style = Gfx::Painter::LineStyle::Dotted;
+    }
 
     auto draw_line = [&](auto& p1, auto& p2) {
-        context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, 1, dotted);
+        context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, 1, line_style);
     };
 
     auto width_for = [&](CSS::PropertyID property_id) -> float {