summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Burchell <robin+git@viroteck.net>2019-06-02 15:56:33 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-06-03 20:27:05 +0200
commitab004f73bf042d118da2ff326640c912703192af (patch)
tree59ca8d1a774620b67aa25a7d4fa69d09a1d7b249
parent1024dfa81ab391765eb8838cfc7e58b03e2cb5c6 (diff)
downloadserenity-ab004f73bf042d118da2ff326640c912703192af.zip
Painter: Reduce the number of draw_text overloads to only involve StringView
No more char + int sequences, as that's literally what StringView is for.
-rw-r--r--LibGUI/GTextEditor.cpp4
-rw-r--r--SharedGraphics/Painter.cpp44
-rw-r--r--SharedGraphics/Painter.h2
3 files changed, 19 insertions, 31 deletions
diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp
index 2402c38519..001e99d497 100644
--- a/LibGUI/GTextEditor.cpp
+++ b/LibGUI/GTextEditor.cpp
@@ -301,7 +301,7 @@ void GTextEditor::paint_event(GPaintEvent& event)
//line_rect.set_width(exposed_width);
if (is_multi_line() && i == m_cursor.line())
painter.fill_rect(line_rect, Color(230, 230, 230));
- painter.draw_text(line_rect, line.characters(), line.length(), m_text_alignment, Color::Black);
+ painter.draw_text(line_rect, StringView(line.characters(), line.length()), m_text_alignment, Color::Black);
bool line_has_selection = has_selection && i >= selection.start().line() && i <= selection.end().line();
if (line_has_selection) {
int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
@@ -312,7 +312,7 @@ void GTextEditor::paint_event(GPaintEvent& event)
Rect selection_rect { selection_left, line_rect.y(), selection_right - selection_left, line_rect.height() };
painter.fill_rect(selection_rect, Color::from_rgb(0x955233));
- painter.draw_text(selection_rect, line.characters() + selection_start_column_on_line, line.length() - selection_start_column_on_line - (line.length() - selection_end_column_on_line), TextAlignment::CenterLeft, Color::White);
+ painter.draw_text(selection_rect, StringView(line.characters() + selection_start_column_on_line, line.length() - selection_start_column_on_line - (line.length() - selection_end_column_on_line)), TextAlignment::CenterLeft, Color::White);
}
}
diff --git a/SharedGraphics/Painter.cpp b/SharedGraphics/Painter.cpp
index 889558f322..5061fefb2c 100644
--- a/SharedGraphics/Painter.cpp
+++ b/SharedGraphics/Painter.cpp
@@ -508,18 +508,24 @@ void Painter::draw_scaled_bitmap(const Rect& a_dst_rect, const GraphicsBitmap& s
draw_bitmap(point, font.glyph_bitmap(ch), color);
}
-void Painter::draw_text(const Rect& rect, const char* text, int length, const Font& font, TextAlignment alignment, Color color, TextElision elision)
+void Painter::draw_text(const Rect& rect, const StringView& text, TextAlignment alignment, Color color, TextElision elision)
+{
+ draw_text(rect, text, font(), alignment, color, elision);
+}
+
+void Painter::draw_text(const Rect& rect, const StringView& text, const Font& font, TextAlignment alignment, Color color, TextElision elision)
{
+ StringView final_text(text);
String elided_text;
if (elision == TextElision::Right) {
- int text_width = font.width(StringView(text, length));
- if (font.width(StringView(text, length)) > rect.width()) {
+ int text_width = font.width(final_text);
+ if (font.width(final_text) > rect.width()) {
int glyph_spacing = font.glyph_spacing();
int new_length = 0;
int new_width = font.width("...");
if (new_width < text_width) {
- for (int i = 0; i < length; ++i) {
- int glyph_width = font.glyph_width(text[i]);
+ for (int i = 0; i < final_text.length(); ++i) {
+ int glyph_width = font.glyph_width(final_text.characters()[i]);
// NOTE: Glyph spacing should not be added after the last glyph on the line,
// but since we are here because the last glyph does not actually fit on the line,
// we don't have to worry about spacing.
@@ -530,11 +536,10 @@ void Painter::draw_text(const Rect& rect, const char* text, int length, const Fo
new_width += glyph_width + glyph_spacing;
}
StringBuilder builder;
- builder.append(text, new_length);
+ builder.append(StringView(final_text.characters(), new_length));
builder.append("...");
elided_text = builder.to_string();
- text = elided_text.characters();
- length = elided_text.length();
+ final_text = elided_text;
}
}
}
@@ -546,10 +551,10 @@ void Painter::draw_text(const Rect& rect, const char* text, int length, const Fo
} else if (alignment == TextAlignment::CenterLeft) {
point = { rect.x(), rect.center().y() - (font.glyph_height() / 2) };
} else if (alignment == TextAlignment::CenterRight) {
- int text_width = font.width(StringView(text, length));
+ int text_width = font.width(final_text);
point = { rect.right() - text_width, rect.center().y() - (font.glyph_height() / 2) };
} else if (alignment == TextAlignment::Center) {
- int text_width = font.width(StringView(text, length));
+ int text_width = font.width(final_text);
point = rect.center();
point.move_by(-(text_width / 2), -(font.glyph_height() / 2));
} else {
@@ -557,8 +562,8 @@ void Painter::draw_text(const Rect& rect, const char* text, int length, const Fo
}
int space_width = font.glyph_width(' ') + font.glyph_spacing();
- for (ssize_t i = 0; i < length; ++i) {
- char ch = text[i];
+ for (ssize_t i = 0; i < final_text.length(); ++i) {
+ char ch = final_text.characters()[i];
if (ch == ' ') {
point.move_by(space_width, 0);
continue;
@@ -568,21 +573,6 @@ void Painter::draw_text(const Rect& rect, const char* text, int length, const Fo
}
}
-void Painter::draw_text(const Rect& rect, const StringView& text, TextAlignment alignment, Color color, TextElision elision)
-{
- draw_text(rect, text.characters(), text.length(), alignment, color, elision);
-}
-
-void Painter::draw_text(const Rect& rect, const StringView& text, const Font& font, TextAlignment alignment, Color color, TextElision elision)
-{
- draw_text(rect, text.characters(), text.length(), font, alignment, color, elision);
-}
-
-void Painter::draw_text(const Rect& rect, const char* text, int length, TextAlignment alignment, Color color, TextElision elision)
-{
- draw_text(rect, text, length, font(), alignment, color, elision);
-}
-
void Painter::set_pixel(const Point& p, Color color)
{
auto point = p;
diff --git a/SharedGraphics/Painter.h b/SharedGraphics/Painter.h
index 1008d287ab..593474e3c3 100644
--- a/SharedGraphics/Painter.h
+++ b/SharedGraphics/Painter.h
@@ -30,8 +30,6 @@ public:
void blit_tiled(const Point&, const GraphicsBitmap&, const Rect& src_rect);
void blit_offset(const Point&, const GraphicsBitmap&, const Rect& src_rect, const Point&);
void blit_scaled(const Point&, const GraphicsBitmap&, const Rect& src_rect, const Size&);
- void draw_text(const Rect&, const char* text, int length, const Font&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
- void draw_text(const Rect&, const char* text, int length, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
void draw_text(const Rect&, const StringView&, const Font&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
void draw_text(const Rect&, const StringView&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
void draw_glyph(const Point&, char, Color);