mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 18:18:12 +00:00
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.
This commit is contained in:
parent
1024dfa81a
commit
ab004f73bf
3 changed files with 19 additions and 31 deletions
|
@ -301,7 +301,7 @@ void GTextEditor::paint_event(GPaintEvent& event)
|
||||||
//line_rect.set_width(exposed_width);
|
//line_rect.set_width(exposed_width);
|
||||||
if (is_multi_line() && i == m_cursor.line())
|
if (is_multi_line() && i == m_cursor.line())
|
||||||
painter.fill_rect(line_rect, Color(230, 230, 230));
|
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();
|
bool line_has_selection = has_selection && i >= selection.start().line() && i <= selection.end().line();
|
||||||
if (line_has_selection) {
|
if (line_has_selection) {
|
||||||
int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
|
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() };
|
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.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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
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;
|
String elided_text;
|
||||||
if (elision == TextElision::Right) {
|
if (elision == TextElision::Right) {
|
||||||
int text_width = font.width(StringView(text, length));
|
int text_width = font.width(final_text);
|
||||||
if (font.width(StringView(text, length)) > rect.width()) {
|
if (font.width(final_text) > rect.width()) {
|
||||||
int glyph_spacing = font.glyph_spacing();
|
int glyph_spacing = font.glyph_spacing();
|
||||||
int new_length = 0;
|
int new_length = 0;
|
||||||
int new_width = font.width("...");
|
int new_width = font.width("...");
|
||||||
if (new_width < text_width) {
|
if (new_width < text_width) {
|
||||||
for (int i = 0; i < length; ++i) {
|
for (int i = 0; i < final_text.length(); ++i) {
|
||||||
int glyph_width = font.glyph_width(text[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,
|
// 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,
|
// but since we are here because the last glyph does not actually fit on the line,
|
||||||
// we don't have to worry about spacing.
|
// 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;
|
new_width += glyph_width + glyph_spacing;
|
||||||
}
|
}
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append(text, new_length);
|
builder.append(StringView(final_text.characters(), new_length));
|
||||||
builder.append("...");
|
builder.append("...");
|
||||||
elided_text = builder.to_string();
|
elided_text = builder.to_string();
|
||||||
text = elided_text.characters();
|
final_text = elided_text;
|
||||||
length = elided_text.length();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -546,10 +551,10 @@ void Painter::draw_text(const Rect& rect, const char* text, int length, const Fo
|
||||||
} else if (alignment == TextAlignment::CenterLeft) {
|
} else if (alignment == TextAlignment::CenterLeft) {
|
||||||
point = { rect.x(), rect.center().y() - (font.glyph_height() / 2) };
|
point = { rect.x(), rect.center().y() - (font.glyph_height() / 2) };
|
||||||
} else if (alignment == TextAlignment::CenterRight) {
|
} 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) };
|
point = { rect.right() - text_width, rect.center().y() - (font.glyph_height() / 2) };
|
||||||
} else if (alignment == TextAlignment::Center) {
|
} else if (alignment == TextAlignment::Center) {
|
||||||
int text_width = font.width(StringView(text, length));
|
int text_width = font.width(final_text);
|
||||||
point = rect.center();
|
point = rect.center();
|
||||||
point.move_by(-(text_width / 2), -(font.glyph_height() / 2));
|
point.move_by(-(text_width / 2), -(font.glyph_height() / 2));
|
||||||
} else {
|
} 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();
|
int space_width = font.glyph_width(' ') + font.glyph_spacing();
|
||||||
for (ssize_t i = 0; i < length; ++i) {
|
for (ssize_t i = 0; i < final_text.length(); ++i) {
|
||||||
char ch = text[i];
|
char ch = final_text.characters()[i];
|
||||||
if (ch == ' ') {
|
if (ch == ' ') {
|
||||||
point.move_by(space_width, 0);
|
point.move_by(space_width, 0);
|
||||||
continue;
|
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)
|
void Painter::set_pixel(const Point& p, Color color)
|
||||||
{
|
{
|
||||||
auto point = p;
|
auto point = p;
|
||||||
|
|
|
@ -30,8 +30,6 @@ public:
|
||||||
void blit_tiled(const Point&, const GraphicsBitmap&, const Rect& src_rect);
|
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_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 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&, 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_text(const Rect&, const StringView&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
|
||||||
void draw_glyph(const Point&, char, Color);
|
void draw_glyph(const Point&, char, Color);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue