From f536d8479e37b1515ffb5737a9f276976bef3cf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Mon, 4 Mar 2019 00:51:03 +0100 Subject: Add word-break instead of naive character-break in TextEdit. --- plugingui/textedit.cc | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/plugingui/textedit.cc b/plugingui/textedit.cc index b6945de..9688b82 100644 --- a/plugingui/textedit.cc +++ b/plugingui/textedit.cc @@ -115,21 +115,37 @@ void TextEdit::preprocessText() } while(pos != std::string::npos); // Wrap long lines - for(auto it = lines.begin(); it != lines.end(); ++it) + auto const max_width = width() - 2*x_border - 10 - scroll.width(); + for(auto const& line: lines) { - auto line = *it; - - for(std::size_t i = 0; i < line.length(); ++i) + std::string valid; + std::string current; + for(auto c: line) { - auto linewidth = font.textWidth(line.substr(0, i)); - if(linewidth >= width() - 2*x_border - 10 - scroll.width()) + current += c; + if(c == ' ') + { + valid.append(current.substr(valid.size())); + } + + if(font.textWidth(current) >= max_width) { - preprocessed_text.push_back(line.substr(0, i)); - line = line.substr(i); - i = 0; + if(valid.empty()) + { + current.pop_back(); + preprocessed_text.push_back(current); + current = c; + } + else + { + current = current.substr(valid.size()); + valid.pop_back(); + preprocessed_text.push_back(valid); + valid.clear(); + } } } - preprocessed_text.push_back(line); + preprocessed_text.push_back(current); } } -- cgit v1.2.3