2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
24 #include <boost/algorithm/string/replace.hpp>
25 #include <boost/algorithm/string/split.hpp>
26 #include <boost/algorithm/string/classification.hpp>
32 namespace caspar { namespace core {
34 void wordwrap_line(const std::wstring& line, int width, std::wostream& result)
36 std::list<std::wstring> words;
37 boost::split(words, line, boost::is_any_of(L" "), boost::algorithm::token_compress_on);
39 size_t current_width = 0;
42 while (!words.empty())
44 auto word = std::move(words.front());
49 current_width = word.length();
50 result << std::move(word);
53 else if (current_width + 1 + word.length() > width)
56 current_width = word.length();
57 result << std::move(word);
61 current_width += 1 + word.length();
62 result << L" " << std::move(word);
67 std::wstring wordwrap(const std::wstring& text, int width)
69 std::vector<std::wstring> lines;
70 boost::split(lines, text, boost::is_any_of(L"\n"));
72 std::wstringstream result;
74 for (auto& line : lines)
76 wordwrap_line(line, width, result);
83 std::wstring indent(std::wstring text, const std::wstring& indent)
85 text.insert(0, indent);
86 bool last_is_newline = text.at(text.length() - 1) == L'\n';
89 text.erase(text.length() - 1);
91 boost::replace_all(text, L"\n", L"\n" + indent);