From 31422709e8a192f0e9422c8b1d3c43271e9fe00b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 24 Jul 2016 20:35:01 -0400 Subject: [PATCH] Wrap lines --- src/gui/Window.cc | 21 ++++++++++++++++++--- src/gui/Window.h | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/gui/Window.cc b/src/gui/Window.cc index a7fef76..a24da92 100644 --- a/src/gui/Window.cc +++ b/src/gui/Window.cc @@ -247,6 +247,9 @@ void Window::resize() glViewport(0, 0, m_width, m_height); m_programs.text->use(); m_programs.text->set_viewport_size(m_width, m_height); + m_chars_per_line = m_width / m_font.get_advance(); + if (m_chars_per_line < 1) + m_chars_per_line = 1; } void Window::redraw() @@ -258,21 +261,33 @@ void Window::redraw() int advance = m_font.get_advance(); int line_height = m_font.get_line_height(); + int x = 0; int y = m_height - line_height; for (PieceTable::PieceDescriptor * pd = m_start_piece; pd != m_buffer->piece_table->end_descriptor; pd = pd->next) { - int x = 0; for (int i = 0, len = pd->length; i < len; i++) { uint8_t c = pd->start[i]; auto g = m_font.get_glyph(c); m_programs.text->set_position(x, y); g->render(); - x += advance; + if ((i + 1) % m_chars_per_line == 0) + { + y -= line_height; + x = 0; + } + else + { + x += advance; + } + } + if (pd->eol()) + { + y -= line_height; + x = 0; } - y -= line_height; if (y + line_height < 0) { break; diff --git a/src/gui/Window.h b/src/gui/Window.h index de0d100..e2eedd8 100644 --- a/src/gui/Window.h +++ b/src/gui/Window.h @@ -29,6 +29,7 @@ protected: } m_programs; Font m_font; + int m_chars_per_line; std::shared_ptr m_buffer;