1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:47:44 +00:00

LibVT: Switch VT::Line to being backed by 32-bit codepoints

This will allow us to have much better Unicode support. It does incur
a memory usage regression which we'll have to optimize to cover.
This commit is contained in:
Andreas Kling 2020-05-16 19:21:53 +02:00
parent cd29844632
commit c4edc4c550
5 changed files with 71 additions and 100 deletions

View file

@ -36,7 +36,7 @@ Line::Line(u16 length)
Line::~Line() Line::~Line()
{ {
delete[] m_characters; delete[] m_codepoints;
delete[] m_attributes; delete[] m_attributes;
} }
@ -44,17 +44,19 @@ void Line::set_length(u16 new_length)
{ {
if (m_length == new_length) if (m_length == new_length)
return; return;
auto* new_characters = new u8[new_length]; auto* new_codepoints = new u32[new_length];
auto* new_attributes = new Attribute[new_length]; auto* new_attributes = new Attribute[new_length];
memset(new_characters, ' ', new_length); for (size_t i = 0; i < new_length; ++i)
if (m_characters && m_attributes) { new_codepoints[i] = ' ';
memcpy(new_characters, m_characters, min(m_length, new_length)); if (m_codepoints && m_attributes) {
for (size_t i = 0; i < min(m_length, new_length); ++i) for (size_t i = 0; i < min(m_length, new_length); ++i) {
new_codepoints[i] = m_codepoints[i];
new_attributes[i] = m_attributes[i]; new_attributes[i] = m_attributes[i];
}
} }
delete[] m_characters; delete[] m_codepoints;
delete[] m_attributes; delete[] m_attributes;
m_characters = new_characters; m_codepoints = new_codepoints;
m_attributes = new_attributes; m_attributes = new_attributes;
m_length = new_length; m_length = new_length;
} }
@ -62,15 +64,16 @@ void Line::set_length(u16 new_length)
void Line::clear(Attribute attribute) void Line::clear(Attribute attribute)
{ {
if (m_dirty) { if (m_dirty) {
memset(m_characters, ' ', m_length); for (u16 i = 0; i < m_length; ++i) {
for (u16 i = 0; i < m_length; ++i) m_codepoints[i] = ' ';
m_attributes[i] = attribute; m_attributes[i] = attribute;
}
return; return;
} }
for (unsigned i = 0; i < m_length; ++i) { for (unsigned i = 0; i < m_length; ++i) {
if (m_characters[i] != ' ') if (m_codepoints[i] != ' ')
m_dirty = true; m_dirty = true;
m_characters[i] = ' '; m_codepoints[i] = ' ';
} }
for (unsigned i = 0; i < m_length; ++i) { for (unsigned i = 0; i < m_length; ++i) {
if (m_attributes[i] != attribute) if (m_attributes[i] != attribute)

View file

@ -88,12 +88,10 @@ public:
bool has_only_one_background_color() const; bool has_only_one_background_color() const;
void set_length(u16); void set_length(u16);
StringView text() const { return { m_characters, m_length }; }
u16 length() const { return m_length; } u16 length() const { return m_length; }
const u8* characters() const { return m_characters; } const u32* codepoints() const { return m_codepoints; }
u8* characters() { return m_characters; } u32* codepoints() { return m_codepoints; }
bool is_dirty() const { return m_dirty; } bool is_dirty() const { return m_dirty; }
void set_dirty(bool b) { m_dirty = b; } void set_dirty(bool b) { m_dirty = b; }
@ -102,7 +100,7 @@ public:
Attribute* attributes() { return m_attributes; } Attribute* attributes() { return m_attributes; }
private: private:
u8* m_characters { nullptr }; u32* m_codepoints { nullptr };
Attribute* m_attributes { nullptr }; Attribute* m_attributes { nullptr };
bool m_dirty { false }; bool m_dirty { false };
u16 m_length { 0 }; u16 m_length { 0 };

View file

@ -353,7 +353,7 @@ void Terminal::escape$b(const ParamVector& params)
return; return;
for (unsigned i = 0; i < params[0]; ++i) for (unsigned i = 0; i < params[0]; ++i)
put_character_at(m_cursor_row, m_cursor_column++, m_last_char); put_character_at(m_cursor_row, m_cursor_column++, m_last_codepoint);
} }
void Terminal::escape$d(const ParamVector& params) void Terminal::escape$d(const ParamVector& params)
@ -529,11 +529,11 @@ void Terminal::escape$P(const ParamVector& params)
// Move n characters of line to the left // Move n characters of line to the left
for (int i = m_cursor_column; i < line.length() - num; i++) for (int i = m_cursor_column; i < line.length() - num; i++)
line.characters()[i] = line.characters()[i + num]; line.codepoints()[i] = line.codepoints()[i + num];
// Fill remainder of line with blanks // Fill remainder of line with blanks
for (int i = line.length() - num; i < line.length(); i++) for (int i = line.length() - num; i < line.length(); i++)
line.characters()[i] = ' '; line.codepoints()[i] = ' ';
line.set_dirty(true); line.set_dirty(true);
} }
@ -761,17 +761,17 @@ void Terminal::set_cursor(unsigned a_row, unsigned a_column)
invalidate_cursor(); invalidate_cursor();
} }
void Terminal::put_character_at(unsigned row, unsigned column, u8 ch) void Terminal::put_character_at(unsigned row, unsigned column, u32 ch)
{ {
ASSERT(row < rows()); ASSERT(row < rows());
ASSERT(column < columns()); ASSERT(column < columns());
auto& line = m_lines[row]; auto& line = m_lines[row];
line.characters()[column] = ch; line.codepoints()[column] = ch;
line.attributes()[column] = m_current_attribute; line.attributes()[column] = m_current_attribute;
line.attributes()[column].flags |= Attribute::Touched; line.attributes()[column].flags |= Attribute::Touched;
line.set_dirty(true); line.set_dirty(true);
m_last_char = ch; m_last_codepoint = ch;
} }
void Terminal::NEL() void Terminal::NEL()

View file

@ -105,7 +105,7 @@ private:
void scroll_down(); void scroll_down();
void newline(); void newline();
void set_cursor(unsigned row, unsigned column); void set_cursor(unsigned row, unsigned column);
void put_character_at(unsigned row, unsigned column, u8 ch); void put_character_at(unsigned row, unsigned column, u32 ch);
void set_window_title(const String&); void set_window_title(const String&);
void unimplemented_escape(); void unimplemented_escape();
@ -188,8 +188,7 @@ private:
Vector<u8> m_xterm_parameters; Vector<u8> m_xterm_parameters;
Vector<bool> m_horizontal_tabs; Vector<bool> m_horizontal_tabs;
u8 m_final { 0 }; u8 m_final { 0 };
u32 m_last_codepoint { 0 };
u8 m_last_char { 0 };
}; };
} }

View file

@ -30,7 +30,6 @@
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <Kernel/KeyCode.h> #include <Kernel/KeyCode.h>
#include <LibCore/ConfigFile.h> #include <LibCore/ConfigFile.h>
#include <LibCore/MimeData.h> #include <LibCore/MimeData.h>
@ -338,86 +337,58 @@ void TerminalWidget::paint_event(GUI::PaintEvent& event)
else if (has_only_one_background_color) else if (has_only_one_background_color)
painter.clear_rect(row_rect, color_from_rgb(line.attributes()[0].background_color).with_alpha(m_opacity)); painter.clear_rect(row_rect, color_from_rgb(line.attributes()[0].background_color).with_alpha(m_opacity));
// The terminal insists on thinking characters and for (size_t column = 0; column < line.length(); ++column) {
// bytes are the same thing. We want to still draw u32 codepoint = line.codepoints()[column];
// emojis in *some* way, but it won't be completely bool should_reverse_fill_for_cursor_or_selection = m_cursor_blink_state
// perfect. So what we do is we make multi-byte && m_has_logical_focus
// characters take up multiple columns, and render && visual_row == row_with_cursor
// the character itself in the center of the columns && column == m_terminal.cursor_column();
// its bytes take up as far as the terminal is concerned. should_reverse_fill_for_cursor_or_selection |= selection_contains({ first_row_from_history + visual_row, (int)column });
auto attribute = line.attributes()[column];
auto text_color = color_from_rgb(should_reverse_fill_for_cursor_or_selection ? attribute.background_color : attribute.foreground_color);
auto character_rect = glyph_rect(visual_row, column);
auto cell_rect = character_rect.inflated(0, m_line_spacing);
if (!has_only_one_background_color || should_reverse_fill_for_cursor_or_selection) {
painter.clear_rect(cell_rect, color_from_rgb(should_reverse_fill_for_cursor_or_selection ? attribute.foreground_color : attribute.background_color).with_alpha(m_opacity));
}
Utf8View utf8_view { line.text() }; enum class UnderlineStyle {
None,
Dotted,
Solid,
};
for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) { auto underline_style = UnderlineStyle::None;
u32 codepoint = *it;
int this_char_column = utf8_view.byte_offset_of(it);
AK::Utf8CodepointIterator it_copy = it;
int next_char_column = utf8_view.byte_offset_of(++it_copy);
// Columns from this_char_column up until next_char_column if (attribute.flags & VT::Attribute::Underline) {
// are logically taken up by this (possibly multi-byte) // Content has specified underline
// character. Iterate over these columns and draw background underline_style = UnderlineStyle::Solid;
// for each one of them separately. } else if (!attribute.href.is_empty()) {
// We're hovering a hyperlink
bool should_reverse_fill_for_cursor_or_selection = false; if (m_hovered_href_id == attribute.href_id || m_active_href_id == attribute.href_id)
VT::Attribute attribute;
Color text_color;
for (u16 column = this_char_column; column < next_char_column; ++column) {
should_reverse_fill_for_cursor_or_selection |= m_cursor_blink_state
&& m_has_logical_focus
&& visual_row == row_with_cursor
&& column == m_terminal.cursor_column();
should_reverse_fill_for_cursor_or_selection |= selection_contains({ first_row_from_history + visual_row, column });
attribute = line.attributes()[column];
text_color = color_from_rgb(should_reverse_fill_for_cursor_or_selection ? attribute.background_color : attribute.foreground_color);
auto character_rect = glyph_rect(visual_row, column);
auto cell_rect = character_rect.inflated(0, m_line_spacing);
if (!has_only_one_background_color || should_reverse_fill_for_cursor_or_selection) {
painter.clear_rect(cell_rect, color_from_rgb(should_reverse_fill_for_cursor_or_selection ? attribute.foreground_color : attribute.background_color).with_alpha(m_opacity));
}
enum class UnderlineStyle {
None,
Dotted,
Solid,
};
auto underline_style = UnderlineStyle::None;
if (attribute.flags & VT::Attribute::Underline) {
// Content has specified underline
underline_style = UnderlineStyle::Solid; underline_style = UnderlineStyle::Solid;
} else if (!attribute.href.is_empty()) { else
// We're hovering a hyperlink underline_style = UnderlineStyle::Dotted;
if (m_hovered_href_id == attribute.href_id || m_active_href_id == attribute.href_id) }
underline_style = UnderlineStyle::Solid;
else
underline_style = UnderlineStyle::Dotted;
}
if (underline_style == UnderlineStyle::Solid) { if (underline_style == UnderlineStyle::Solid) {
if (attribute.href_id == m_active_href_id && m_hovered_href_id == m_active_href_id) if (attribute.href_id == m_active_href_id && m_hovered_href_id == m_active_href_id)
text_color = palette().active_link(); text_color = palette().active_link();
painter.draw_line(cell_rect.bottom_left(), cell_rect.bottom_right(), text_color); painter.draw_line(cell_rect.bottom_left(), cell_rect.bottom_right(), text_color);
} else if (underline_style == UnderlineStyle::Dotted) { } else if (underline_style == UnderlineStyle::Dotted) {
auto dotted_line_color = text_color.darkened(0.6f); auto dotted_line_color = text_color.darkened(0.6f);
int x1 = cell_rect.bottom_left().x(); int x1 = cell_rect.bottom_left().x();
int x2 = cell_rect.bottom_right().x(); int x2 = cell_rect.bottom_right().x();
int y = cell_rect.bottom_left().y(); int y = cell_rect.bottom_left().y();
for (int x = x1; x <= x2; ++x) { for (int x = x1; x <= x2; ++x) {
if ((x % 3) == 0) if ((x % 3) == 0)
painter.set_pixel({ x, y }, dotted_line_color); painter.set_pixel({ x, y }, dotted_line_color);
}
} }
} }
if (codepoint == ' ') if (codepoint == ' ')
continue; continue;
auto character_rect = glyph_rect(visual_row, this_char_column);
auto num_columns = next_char_column - this_char_column;
character_rect.move_by((num_columns - 1) * font().glyph_width('x') / 2, 0);
painter.draw_glyph_or_emoji( painter.draw_glyph_or_emoji(
character_rect.location(), character_rect.location(),
codepoint, codepoint,
@ -583,16 +554,16 @@ void TerminalWidget::doubleclick_event(GUI::MouseEvent& event)
auto position = buffer_position_at(event.position()); auto position = buffer_position_at(event.position());
auto& line = m_terminal.line(position.row()); auto& line = m_terminal.line(position.row());
bool want_whitespace = line.characters()[position.column()] == ' '; bool want_whitespace = line.codepoints()[position.column()] == ' ';
int start_column = 0; int start_column = 0;
int end_column = 0; int end_column = 0;
for (int column = position.column(); column >= 0 && (line.characters()[column] == ' ') == want_whitespace; --column) { for (int column = position.column(); column >= 0 && (line.codepoints()[column] == ' ') == want_whitespace; --column) {
start_column = column; start_column = column;
} }
for (int column = position.column(); column < m_terminal.columns() && (line.characters()[column] == ' ') == want_whitespace; ++column) { for (int column = position.column(); column < m_terminal.columns() && (line.codepoints()[column] == ' ') == want_whitespace; ++column) {
end_column = column; end_column = column;
} }
@ -762,7 +733,7 @@ String TerminalWidget::selected_text() const
builder.append('\n'); builder.append('\n');
break; break;
} }
builder.append(line.characters()[column]); builder.append(line.codepoints()[column]);
if (column == line.length() - 1 || (m_rectangle_selection && column == last_column)) { if (column == line.length() - 1 || (m_rectangle_selection && column == last_column)) {
builder.append('\n'); builder.append('\n');
} }