From e7de7b353c8d9df9c0c49369fe65b9a1f311c0d7 Mon Sep 17 00:00:00 2001 From: NirAz Date: Thu, 5 Nov 2020 14:24:21 +0200 Subject: [PATCH 1/8] wrap viewer error pop up text by pixels width --- common/CMakeLists.txt | 8 +- common/utilities/string/wrap-text.h | 117 ++++++++++++++++++ common/viewer.cpp | 20 ++- .../utilities/string/test-wrap-text.cpp | 87 +++++++++++++ 4 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 common/utilities/string/wrap-text.h create mode 100644 unit-tests/utilities/string/test-wrap-text.cpp diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 22ff9b6b15..7ed0fcd0b5 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -56,12 +56,16 @@ set(COMMON_SRC "${CMAKE_CURRENT_LIST_DIR}/reflectivity/reflectivity.cpp" ) - set(UTILITIES + set(UTILITIES_FILES "${CMAKE_CURRENT_LIST_DIR}/utilities/number/stabilized-value.h" + "${CMAKE_CURRENT_LIST_DIR}/utilities/string/trim-newlines.h" + "${CMAKE_CURRENT_LIST_DIR}/utilities/string/wrap-text.h" ) + set(COMMON_SRC ${COMMON_SRC} ${SW_UPDATE_FILES} ${REFLECTIVITY_FILES} - ${UTILITIES}) + ${UTILITIES_FILES} + ) diff --git a/common/utilities/string/wrap-text.h b/common/utilities/string/wrap-text.h new file mode 100644 index 0000000000..f03f2d698d --- /dev/null +++ b/common/utilities/string/wrap-text.h @@ -0,0 +1,117 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +#pragma once + +#include +#include +#include + +#include "../third-party/imgui/imgui.h" + +namespace utilities { +namespace string { + + +// Split input test to lines +// - Input: std:string text +// - Output: a vector of (std::string) lines +// Example: +// Input: +// This is the first line\nThis is the second line\nThis is the last line +// Output: +// [0] this is the first line +// [1] this is the second line +// [2] this is the last line +inline std::vector< std::string > split_string_by_newline( const std::string & str ) +{ + auto result = std::vector< std::string >{}; + auto ss = std::stringstream{ str }; + + for( std::string line; std::getline( ss, line, '\n' ); ) + result.push_back( line ); + + return result; +} + +// Wrap text according to input width +// - Input: - text as a string +// - wrapping width +// - Output: - on success - wrapped text +// - on failure - an empty string +// Example: +// Input: +// This is the first line\nThis is the second line\nThis is the last line , wrap_width = 15 +// Output: +// this is the\nfirst line\nthis is the\nsecond line\nthis is the\nlast line +inline std::string wrap_text(const std::string & text, float wrap_pixels_width) +{ + // Do not wrap if the wrap is smaller then 32 pixels (~2 characters on font 16) + if (wrap_pixels_width < 32.0f) + return ""; + + // split text into paragraphs + auto lines_vector = split_string_by_newline(text); + + std::string wrapped_text; + float space_size = ImGui::CalcTextSize(" ").x; + bool first_line = true; + + // Wrap each line according to the requested wrap width + for (auto line : lines_vector) + { + std::string current_line; // The line that is built in this iteration + auto temp_line = line; // holds the input line and gets shorten each iteration + auto next_word = temp_line.substr(0, temp_line.find(" ")); // The next word to add to the current line + bool first_word = true; + + // each paragraph except the first one starts in a new line + if (!first_line) current_line += '\n'; + + while (!next_word.empty()) + { + float next_pos = 0.0f; + // If this is the first word we try to place it first in line, + // if not we concatenate it to the last word after adding a space + if (!first_word) + { + next_pos = ImGui::CalcTextSize(current_line.c_str()).x + space_size; + } + + if (next_pos + ImGui::CalcTextSize(next_word.c_str()).x <= wrap_pixels_width) + { + if (!first_word) current_line += " "; // first word should not start with " " + current_line += next_word; + } + else + { // current line cannot feat new word so we wrap the line and + // start building the new line + + wrapped_text += current_line; // copy line build by now + if (!first_word) wrapped_text += '\n'; // break the previous line if exist + current_line = next_word; // add next work to new line + } + + first_word = false; + first_line = false; + + // If we have more characters left other then the current word, prepare inputs for next iteration + // If not add the current line built so far to the output and finish current line wrap. + if (temp_line.size() > next_word.size()) + { + temp_line = temp_line.substr(next_word.size() + 1); + next_word = temp_line.substr(0, temp_line.find(" ")); + } + else + { + wrapped_text += current_line; + break; + } + } + } + + return wrapped_text; +} + +} // namespace string +} // namespace utilities diff --git a/common/viewer.cpp b/common/viewer.cpp index 50ad2cb559..8fef91120c 100644 --- a/common/viewer.cpp +++ b/common/viewer.cpp @@ -21,6 +21,8 @@ #define ARCBALL_CAMERA_IMPLEMENTATION #include +#include "../common/utilities/string/trim-newlines.h" +#include "../common/utilities/string/wrap-text.h" namespace rs2 { @@ -1067,9 +1069,25 @@ namespace rs2 auto custom_command = [&]() { auto msg = _active_popups.front().message; + + // Wrap the text to feet the error pop-up window + std::string wrapped_msg; + try + { + auto trimmed_msg = utilities::string::trim_newlines(msg); + wrapped_msg = utilities::string::wrap_text(trimmed_msg, 500); + if (wrapped_msg.empty()) wrapped_msg = msg; // Revert to original text on wrapping failure + } + catch (...) + { + wrapped_msg = msg; // Revert to original text on wrapping failure + not_model->output.add_log(RS2_LOG_SEVERITY_WARN, __FILE__, __LINE__, + to_string() << "Wrapping of error message text failed!"); + } + ImGui::Text("RealSense error calling:"); ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, regular_blue); - ImGui::InputTextMultiline("##error", const_cast(msg.c_str()), + ImGui::InputTextMultiline("##error", const_cast(wrapped_msg.c_str()), msg.size() + 1, { 500,95 }, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_ReadOnly); ImGui::PopStyleColor(); diff --git a/unit-tests/utilities/string/test-wrap-text.cpp b/unit-tests/utilities/string/test-wrap-text.cpp new file mode 100644 index 0000000000..d0d1ad4eb2 --- /dev/null +++ b/unit-tests/utilities/string/test-wrap-text.cpp @@ -0,0 +1,87 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +//#cmake:add-file ../../../third-party/imgui/imgui.h + + +#include "common.h" +#include "../../../common/utilities/string/wrap-text.h" + +namespace ImGui { +// Mock ImGui function for test +// all characters are at length of 10 pixels except 'i' and 'j' which is 5 pixels +ImVec2 CalcTextSize( const char * text, + const char * text_end, + bool hide_text_after_double_hash, + float wrap_width ) +{ + if (!text) return ImVec2(0.0f, 0.0f); + + float total_size = 0.0f; + while( *text ) + { + if( *text == 'i' || *text == 'j' ) + total_size += 5.0f; + else + total_size += 10.0f; + text++; + } + return ImVec2( total_size, 0.0f ); +} +} // namespace ImGui + +using namespace utilities::string; + +TEST_CASE("split_string_by_newline", "[string]") +{ + // split size check + CHECK(split_string_by_newline("").size() == 0); + CHECK(split_string_by_newline("abc").size() == 1); + CHECK(split_string_by_newline("abc\nabc").size() == 2); + CHECK(split_string_by_newline("a\nbc\nabc").size() == 3); + CHECK(split_string_by_newline("a\nbc\nabc\n").size() == 3); + + CHECK(split_string_by_newline("a\nbc\nabc")[0] == "a"); + CHECK(split_string_by_newline("a\nbc\nabc")[1] == "bc"); + CHECK(split_string_by_newline("a\nbc\nabc")[2] == "abc"); + CHECK(split_string_by_newline("a\nbc\nabc\n")[2] == "abc"); +} + + +TEST_CASE( "wrap-text", "[string]" ) +{ + // Verify illegal inputs return empty string + CHECK( wrap_text( "", 0 ) == "" ); + CHECK( wrap_text( "", 10 ) == "" ); + CHECK( wrap_text( "abc", 0 ) == "" ); + CHECK( wrap_text( "abc", 10 ) == "" ); + CHECK( wrap_text( "abc\nabc", 0 ) == "" ); + CHECK( wrap_text( "abc\nabc", 10 ) == "" ); + CHECK( wrap_text( "", 10 ) == "" ); + + // Verify no wrap if not needed + CHECK( wrap_text( "abc", 100 ) == "abc" ); + CHECK( wrap_text( "abc\nabc", 100 ) == "abc\nabc" ); + CHECK( wrap_text( "abc abc a", 100 ) == "abc abc a" ); + + // No wrapping possible, copy line until first space and continue wrapping + CHECK( wrap_text( "abcdefgh", 40 ) == "abcdefgh" ); + CHECK( wrap_text( "aabbccddff das ds fr", 50 ) == "aabbccddff\ndas\nds fr" ); + CHECK( wrap_text( "das aabbccddff ds fr", 50 ) == "das\naabbccddff\nds fr" ); + + // Exact wrap position test + CHECK( wrap_text( "abcde abcde", 50 ) == "abcde\nabcde" ); + + // Short letters test + CHECK(wrap_text("aaaa bbbb cc", 100) == "aaaa bbbb\ncc"); + // i and j are only 5 pixels so we get more characters inside the wrap + CHECK(wrap_text("aaaa iijj cc", 100) == "aaaa iijj cc"); + + + // Check wrapping of 3 paragraphs + CHECK( wrap_text( "this is the first line\nthis is the second line\nthis is the last line", 150 ) + == "this is the\nfirst line\nthis is the\nsecond line\nthis is the\nlast line" ); + + CHECK( wrap_text( "this is the first line\nthis is the second line\nthis is the last line", 60 ) + == "this is\nthe\nfirst\nline\nthis\nis the\nsecond\nline\nthis\nis the\nlast\nline" ); +} From 58b00d974aa808fa484e48fad04eff44563e1581 Mon Sep 17 00:00:00 2001 From: NirAz Date: Wed, 18 Nov 2020 14:37:20 +0200 Subject: [PATCH 2/8] updates after code review --- common/CMakeLists.txt | 4 +- common/utilities/string/split.h | 38 ++++++ common/utilities/string/wrap-text.h | 117 ------------------ common/utilities/string/wrap.cpp | 95 ++++++++++++++ common/utilities/string/wrap.h | 22 ++++ common/viewer.cpp | 5 +- unit-tests/utilities/string/test-split.cpp | 31 +++++ .../utilities/string/test-wrap-text.cpp | 87 ------------- unit-tests/utilities/string/test-wrap.cpp | 73 +++++++++++ 9 files changed, 264 insertions(+), 208 deletions(-) create mode 100644 common/utilities/string/split.h delete mode 100644 common/utilities/string/wrap-text.h create mode 100644 common/utilities/string/wrap.cpp create mode 100644 common/utilities/string/wrap.h create mode 100644 unit-tests/utilities/string/test-split.cpp delete mode 100644 unit-tests/utilities/string/test-wrap-text.cpp create mode 100644 unit-tests/utilities/string/test-wrap.cpp diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 7ed0fcd0b5..5f5f46b5b2 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -59,7 +59,9 @@ set(COMMON_SRC set(UTILITIES_FILES "${CMAKE_CURRENT_LIST_DIR}/utilities/number/stabilized-value.h" "${CMAKE_CURRENT_LIST_DIR}/utilities/string/trim-newlines.h" - "${CMAKE_CURRENT_LIST_DIR}/utilities/string/wrap-text.h" + "${CMAKE_CURRENT_LIST_DIR}/utilities/string/split.h" + "${CMAKE_CURRENT_LIST_DIR}/utilities/string/wrap.h" + "${CMAKE_CURRENT_LIST_DIR}/utilities/string/wrap.cpp" ) set(COMMON_SRC diff --git a/common/utilities/string/split.h b/common/utilities/string/split.h new file mode 100644 index 0000000000..2386588a03 --- /dev/null +++ b/common/utilities/string/split.h @@ -0,0 +1,38 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +#pragma once + +#include +#include +#include + +namespace utilities { +namespace string { + + +// Split input text to vector of strings according to input delimiter +// - Input: string to be split +// - delimiter +// - Output: a vector of strings +// Example: +// Input: +// Text: This is the first line\nThis is the second line\nThis is the last line +// Delimiter : '\n' +// Output: +// [0] this is the first line +// [1] this is the second line +// [2] this is the last line +inline std::vector< std::string > split( const std::string & str , char delimiter) +{ + auto result = std::vector< std::string >{}; + auto ss = std::stringstream{ str }; + + for( std::string line; std::getline( ss, line, delimiter); ) + result.push_back( line ); + + return result; +} + +} // namespace string +} // namespace utilities diff --git a/common/utilities/string/wrap-text.h b/common/utilities/string/wrap-text.h deleted file mode 100644 index f03f2d698d..0000000000 --- a/common/utilities/string/wrap-text.h +++ /dev/null @@ -1,117 +0,0 @@ -// License: Apache 2.0. See LICENSE file in root directory. -// Copyright(c) 2020 Intel Corporation. All Rights Reserved. - -#pragma once - -#include -#include -#include - -#include "../third-party/imgui/imgui.h" - -namespace utilities { -namespace string { - - -// Split input test to lines -// - Input: std:string text -// - Output: a vector of (std::string) lines -// Example: -// Input: -// This is the first line\nThis is the second line\nThis is the last line -// Output: -// [0] this is the first line -// [1] this is the second line -// [2] this is the last line -inline std::vector< std::string > split_string_by_newline( const std::string & str ) -{ - auto result = std::vector< std::string >{}; - auto ss = std::stringstream{ str }; - - for( std::string line; std::getline( ss, line, '\n' ); ) - result.push_back( line ); - - return result; -} - -// Wrap text according to input width -// - Input: - text as a string -// - wrapping width -// - Output: - on success - wrapped text -// - on failure - an empty string -// Example: -// Input: -// This is the first line\nThis is the second line\nThis is the last line , wrap_width = 15 -// Output: -// this is the\nfirst line\nthis is the\nsecond line\nthis is the\nlast line -inline std::string wrap_text(const std::string & text, float wrap_pixels_width) -{ - // Do not wrap if the wrap is smaller then 32 pixels (~2 characters on font 16) - if (wrap_pixels_width < 32.0f) - return ""; - - // split text into paragraphs - auto lines_vector = split_string_by_newline(text); - - std::string wrapped_text; - float space_size = ImGui::CalcTextSize(" ").x; - bool first_line = true; - - // Wrap each line according to the requested wrap width - for (auto line : lines_vector) - { - std::string current_line; // The line that is built in this iteration - auto temp_line = line; // holds the input line and gets shorten each iteration - auto next_word = temp_line.substr(0, temp_line.find(" ")); // The next word to add to the current line - bool first_word = true; - - // each paragraph except the first one starts in a new line - if (!first_line) current_line += '\n'; - - while (!next_word.empty()) - { - float next_pos = 0.0f; - // If this is the first word we try to place it first in line, - // if not we concatenate it to the last word after adding a space - if (!first_word) - { - next_pos = ImGui::CalcTextSize(current_line.c_str()).x + space_size; - } - - if (next_pos + ImGui::CalcTextSize(next_word.c_str()).x <= wrap_pixels_width) - { - if (!first_word) current_line += " "; // first word should not start with " " - current_line += next_word; - } - else - { // current line cannot feat new word so we wrap the line and - // start building the new line - - wrapped_text += current_line; // copy line build by now - if (!first_word) wrapped_text += '\n'; // break the previous line if exist - current_line = next_word; // add next work to new line - } - - first_word = false; - first_line = false; - - // If we have more characters left other then the current word, prepare inputs for next iteration - // If not add the current line built so far to the output and finish current line wrap. - if (temp_line.size() > next_word.size()) - { - temp_line = temp_line.substr(next_word.size() + 1); - next_word = temp_line.substr(0, temp_line.find(" ")); - } - else - { - wrapped_text += current_line; - break; - } - } - } - - return wrapped_text; -} - -} // namespace string -} // namespace utilities diff --git a/common/utilities/string/wrap.cpp b/common/utilities/string/wrap.cpp new file mode 100644 index 0000000000..15561c369b --- /dev/null +++ b/common/utilities/string/wrap.cpp @@ -0,0 +1,95 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +#include +#include +#include +#include "wrap.h" +#include "split.h" +#include "../third-party/imgui/imgui.h" + +namespace utilities { +namespace string { + +std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width ) +{ + float space_size = ImGui::CalcTextSize( " " ).x; // Calculate space width in pixels + std::string wrapped_line; // The line that is wrapped in this iteration + std::string wrapped_paragraph; // The output wrapped paragraph + auto remaining_paragraph = paragraph; // holds the remaining unwrapped part of the input paragraph + auto next_word = remaining_paragraph.substr( + 0, + remaining_paragraph.find( " " ) ); // The next word to add to the current line + bool first_word = true; + + while( ! next_word.empty() ) + { + float next_x = 0.0f; + // If this is the first word we try to place it first in line, + // if not we concatenate it to the last word after adding a space + if( ! first_word ) + { + next_x = ImGui::CalcTextSize( wrapped_line.c_str() ).x + space_size; + } + + if( next_x + ImGui::CalcTextSize( next_word.c_str() ).x <= wrap_pixels_width ) + { + if( ! first_word ) + wrapped_line += " "; // first word should not start with " " + wrapped_line += next_word; + } + else + { // current line cannot feat new word so we wrap the line and + // start building the new line + + wrapped_paragraph += wrapped_line; // copy line build by now + if( ! first_word ) + wrapped_paragraph += '\n'; // break the previous line if exist + wrapped_line = next_word; // add next work to new line + } + + first_word = false; + + // If we have more characters left other then the current word, prepare inputs for next + // iteration, If not add the current line built so far to the output and finish current + // line wrap. + if( remaining_paragraph.size() > next_word.size() ) + { + remaining_paragraph = remaining_paragraph.substr( next_word.size() + 1 ); + next_word = remaining_paragraph.substr( 0, remaining_paragraph.find( " " ) ); + } + else + { + wrapped_paragraph += wrapped_line; + break; + } + } + + return wrapped_paragraph; +} + +std::string wrap( const std::string & text, int wrap_pixels_width ) +{ + // Do not wrap if the wrap is smaller then 32 pixels (~2 characters on font 16) + if( wrap_pixels_width < 32 ) + return text; + + // split text into paragraphs + auto paragraphs_vector = split( text, '\n' ); + + std::string wrapped_text; + int line_number = 1; + // Wrap each line according to the requested wrap width + for( auto paragraph : paragraphs_vector ) + { + wrapped_text += wrap_paragraph( paragraph, wrap_pixels_width ); + // each paragraph except the last one ends with a new line + if( line_number++ != paragraphs_vector.size() ) + wrapped_text += '\n'; + } + + return wrapped_text; +} + +} // namespace string +} // namespace utilities diff --git a/common/utilities/string/wrap.h b/common/utilities/string/wrap.h new file mode 100644 index 0000000000..9b19cabe94 --- /dev/null +++ b/common/utilities/string/wrap.h @@ -0,0 +1,22 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +#pragma once + +namespace utilities { +namespace string { + +// Wrap text according to input width +// - Input: - text as a string +// - wrapping width +// - Output: - on success - wrapped text +// - on failure - an empty string +// Example: +// Input: +// this is the first line\nthis is the second line\nthis is the last line , wrap_width = 150 [pixels] +// Output: +// this is the\nfirst line\nthis is the\nsecond line\nthis is the last\nline +std::string wrap( const std::string & text, int wrap_pixels_width ); + +} // namespace string +} // namespace utilities diff --git a/common/viewer.cpp b/common/viewer.cpp index 8fef91120c..10a096d8bf 100644 --- a/common/viewer.cpp +++ b/common/viewer.cpp @@ -22,7 +22,7 @@ #define ARCBALL_CAMERA_IMPLEMENTATION #include #include "../common/utilities/string/trim-newlines.h" -#include "../common/utilities/string/wrap-text.h" +#include "../common/utilities/string/wrap.h" namespace rs2 { @@ -1075,8 +1075,7 @@ namespace rs2 try { auto trimmed_msg = utilities::string::trim_newlines(msg); - wrapped_msg = utilities::string::wrap_text(trimmed_msg, 500); - if (wrapped_msg.empty()) wrapped_msg = msg; // Revert to original text on wrapping failure + wrapped_msg = utilities::string::wrap(trimmed_msg, 500); } catch (...) { diff --git a/unit-tests/utilities/string/test-split.cpp b/unit-tests/utilities/string/test-split.cpp new file mode 100644 index 0000000000..8c14cfee36 --- /dev/null +++ b/unit-tests/utilities/string/test-split.cpp @@ -0,0 +1,31 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +//#cmake:add-file ../../../common/utilities/string/split.h + +#include "common.h" +#include "../../../common/utilities/string/split.h" + +using namespace utilities::string; + +TEST_CASE("split_string_by_newline", "[string]") +{ + // split size check + CHECK(split("" , '\n').size() == 0); + CHECK(split("abc", '\n').size() == 1); + CHECK(split("abc\nabc", '\n').size() == 2); + CHECK(split("a\nbc\nabc", '\n').size() == 3); + CHECK(split("a\nbc\nabc\n", '\n').size() == 3); + CHECK(split("1-12-123-1234", '-').size() == 4); + + CHECK(split("a\nbc\nabc", '\n')[0] == "a"); + CHECK(split("a\nbc\nabc", '\n')[1] == "bc"); + CHECK(split("a\nbc\nabc", '\n')[2] == "abc"); + CHECK(split("a\nbc\nabc\n", '\n')[2] == "abc"); + + + CHECK(split("1-12-123-1234", '-')[0] == "1"); + CHECK(split("1-12-123-1234", '-')[1] == "12"); + CHECK(split("1-12-123-1234", '-')[2] == "123"); + CHECK(split("1-12-123-1234", '-')[3] == "1234"); +} diff --git a/unit-tests/utilities/string/test-wrap-text.cpp b/unit-tests/utilities/string/test-wrap-text.cpp deleted file mode 100644 index d0d1ad4eb2..0000000000 --- a/unit-tests/utilities/string/test-wrap-text.cpp +++ /dev/null @@ -1,87 +0,0 @@ -// License: Apache 2.0. See LICENSE file in root directory. -// Copyright(c) 2020 Intel Corporation. All Rights Reserved. - -//#cmake:add-file ../../../third-party/imgui/imgui.h - - -#include "common.h" -#include "../../../common/utilities/string/wrap-text.h" - -namespace ImGui { -// Mock ImGui function for test -// all characters are at length of 10 pixels except 'i' and 'j' which is 5 pixels -ImVec2 CalcTextSize( const char * text, - const char * text_end, - bool hide_text_after_double_hash, - float wrap_width ) -{ - if (!text) return ImVec2(0.0f, 0.0f); - - float total_size = 0.0f; - while( *text ) - { - if( *text == 'i' || *text == 'j' ) - total_size += 5.0f; - else - total_size += 10.0f; - text++; - } - return ImVec2( total_size, 0.0f ); -} -} // namespace ImGui - -using namespace utilities::string; - -TEST_CASE("split_string_by_newline", "[string]") -{ - // split size check - CHECK(split_string_by_newline("").size() == 0); - CHECK(split_string_by_newline("abc").size() == 1); - CHECK(split_string_by_newline("abc\nabc").size() == 2); - CHECK(split_string_by_newline("a\nbc\nabc").size() == 3); - CHECK(split_string_by_newline("a\nbc\nabc\n").size() == 3); - - CHECK(split_string_by_newline("a\nbc\nabc")[0] == "a"); - CHECK(split_string_by_newline("a\nbc\nabc")[1] == "bc"); - CHECK(split_string_by_newline("a\nbc\nabc")[2] == "abc"); - CHECK(split_string_by_newline("a\nbc\nabc\n")[2] == "abc"); -} - - -TEST_CASE( "wrap-text", "[string]" ) -{ - // Verify illegal inputs return empty string - CHECK( wrap_text( "", 0 ) == "" ); - CHECK( wrap_text( "", 10 ) == "" ); - CHECK( wrap_text( "abc", 0 ) == "" ); - CHECK( wrap_text( "abc", 10 ) == "" ); - CHECK( wrap_text( "abc\nabc", 0 ) == "" ); - CHECK( wrap_text( "abc\nabc", 10 ) == "" ); - CHECK( wrap_text( "", 10 ) == "" ); - - // Verify no wrap if not needed - CHECK( wrap_text( "abc", 100 ) == "abc" ); - CHECK( wrap_text( "abc\nabc", 100 ) == "abc\nabc" ); - CHECK( wrap_text( "abc abc a", 100 ) == "abc abc a" ); - - // No wrapping possible, copy line until first space and continue wrapping - CHECK( wrap_text( "abcdefgh", 40 ) == "abcdefgh" ); - CHECK( wrap_text( "aabbccddff das ds fr", 50 ) == "aabbccddff\ndas\nds fr" ); - CHECK( wrap_text( "das aabbccddff ds fr", 50 ) == "das\naabbccddff\nds fr" ); - - // Exact wrap position test - CHECK( wrap_text( "abcde abcde", 50 ) == "abcde\nabcde" ); - - // Short letters test - CHECK(wrap_text("aaaa bbbb cc", 100) == "aaaa bbbb\ncc"); - // i and j are only 5 pixels so we get more characters inside the wrap - CHECK(wrap_text("aaaa iijj cc", 100) == "aaaa iijj cc"); - - - // Check wrapping of 3 paragraphs - CHECK( wrap_text( "this is the first line\nthis is the second line\nthis is the last line", 150 ) - == "this is the\nfirst line\nthis is the\nsecond line\nthis is the\nlast line" ); - - CHECK( wrap_text( "this is the first line\nthis is the second line\nthis is the last line", 60 ) - == "this is\nthe\nfirst\nline\nthis\nis the\nsecond\nline\nthis\nis the\nlast\nline" ); -} diff --git a/unit-tests/utilities/string/test-wrap.cpp b/unit-tests/utilities/string/test-wrap.cpp new file mode 100644 index 0000000000..9b9482c42e --- /dev/null +++ b/unit-tests/utilities/string/test-wrap.cpp @@ -0,0 +1,73 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +//#cmake:add-file ../../../common/utilities/string/wrap.h +//#cmake:add-file ../../../common/utilities/string/wrap.cpp +//#cmake:add-file ../../../third-party/imgui/imgui.h + +#include "common.h" +#include "../../../common/utilities/string/wrap.h" +#include "../../../third-party/imgui/imgui.h" + +namespace ImGui { +// Mock ImGui function for test +// all characters are at length of 10 pixels except 'i' and 'j' which is 5 pixels +ImVec2 CalcTextSize( const char * text, + const char * text_end, + bool hide_text_after_double_hash, + float wrap_width ) +{ + if (!text) return ImVec2(0.0f, 0.0f); + + float total_size = 0.0f; + while( *text ) + { + if( *text == 'i' || *text == 'j' ) + total_size += 5.0f; + else + total_size += 10.0f; + text++; + } + return ImVec2( total_size, 0.0f ); +} +} // namespace ImGui + +using namespace utilities::string; + +TEST_CASE( "wrap-text", "[string]" ) +{ + // Verify illegal inputs return empty string + CHECK( wrap( "", 0 ) == "" ); + CHECK( wrap( "", 10 ) == "" ); + CHECK( wrap( "abc", 0 ) == "abc" ); + CHECK( wrap( "abc", 10 ) == "abc" ); + CHECK( wrap( "abc\nabc", 0 ) == "abc\nabc" ); + CHECK( wrap( "abc abc", 5 ) == "abc abc" ); + CHECK( wrap( "", 10 ) == "" ); + + // Verify no wrap if not needed + CHECK( wrap( "abc", 100 ) == "abc" ); + CHECK( wrap( "abc\nabc", 100 ) == "abc\nabc" ); + CHECK( wrap( "abc abc a", 100 ) == "abc abc a" ); + + // No wrapping possible, copy line until first space and continue wrapping + CHECK( wrap( "abcdefgh", 40 ) == "abcdefgh" ); + CHECK( wrap( "aabbccddff das ds fr", 50 ) == "aabbccddff\ndas\nds fr" ); + CHECK( wrap( "das aabbccddff ds fr", 50 ) == "das\naabbccddff\nds fr" ); + + // Exact wrap position test + CHECK( wrap( "abcde abcde", 50 ) == "abcde\nabcde" ); + + // Short letters test + CHECK(wrap("aaaa bbbb cc", 100) == "aaaa bbbb\ncc"); + // i and j are only 5 pixels so we get more characters inside the wrap + CHECK(wrap("aaaa iijj cc", 100) == "aaaa iijj cc"); + + + // Check wrapping of 3 paragraphs + CHECK( wrap( "this is the first line\nthis is the second line\nthis is the last line", 150 ) + == "this is the\nfirst line\nthis is the\nsecond line\nthis is the last\nline" ); + + CHECK( wrap( "this is the first line\nthis is the second line\nthis is the last line", 60 ) + == "this is\nthe\nfirst\nline\nthis is\nthe\nsecond\nline\nthis is\nthe\nlast\nline" ); +} From b5d91e655a31df602f9856664ae150fe410018eb Mon Sep 17 00:00:00 2001 From: NirAz Date: Thu, 26 Nov 2020 15:16:50 +0200 Subject: [PATCH 3/8] fix issue with multiple spaces --- common/utilities/string/wrap.cpp | 63 ++++++++++++++++++----- unit-tests/utilities/string/test-wrap.cpp | 13 +++++ 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/common/utilities/string/wrap.cpp b/common/utilities/string/wrap.cpp index 15561c369b..0a02ddd628 100644 --- a/common/utilities/string/wrap.cpp +++ b/common/utilities/string/wrap.cpp @@ -13,39 +13,54 @@ namespace string { std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width ) { - float space_size = ImGui::CalcTextSize( " " ).x; // Calculate space width in pixels - std::string wrapped_line; // The line that is wrapped in this iteration - std::string wrapped_paragraph; // The output wrapped paragraph - auto remaining_paragraph = paragraph; // holds the remaining unwrapped part of the input paragraph + float space_width = ImGui::CalcTextSize( " " ).x; // Calculate space width in pixels + std::string wrapped_line; // The line that is wrapped in this iteration + std::string wrapped_paragraph; // The output wrapped paragraph + auto remaining_paragraph = paragraph; // Holds the remaining unwrapped part of the input paragraph + + // Handle a case when the paragraph starts with spaces + if( remaining_paragraph[0] == ' ' ) + { + auto non_space_index = remaining_paragraph.find_first_not_of( ' ' ); + // If non spaces characters exist + if( non_space_index != std::string::npos ) + { + // Remove trailing spaces + remaining_paragraph + = remaining_paragraph.substr( remaining_paragraph.find_first_not_of( ' ' ) ); + } + } + auto next_word = remaining_paragraph.substr( 0, - remaining_paragraph.find( " " ) ); // The next word to add to the current line + remaining_paragraph.find( ' ' ) ); // The next word to add to the current line bool first_word = true; while( ! next_word.empty() ) { + float next_x = 0.0f; // If this is the first word we try to place it first in line, // if not we concatenate it to the last word after adding a space if( ! first_word ) { - next_x = ImGui::CalcTextSize( wrapped_line.c_str() ).x + space_size; + next_x = ImGui::CalcTextSize( wrapped_line.c_str() ).x + space_width; } if( next_x + ImGui::CalcTextSize( next_word.c_str() ).x <= wrap_pixels_width ) { if( ! first_word ) - wrapped_line += " "; // first word should not start with " " + wrapped_line += " "; // First word should not start with " " wrapped_line += next_word; } else - { // current line cannot feat new word so we wrap the line and + { // Current line cannot feat new word so we wrap the line and // start building the new line wrapped_paragraph += wrapped_line; // copy line build by now if( ! first_word ) - wrapped_paragraph += '\n'; // break the previous line if exist - wrapped_line = next_word; // add next work to new line + wrapped_paragraph += '\n'; // break the previous line if exist + wrapped_line = next_word; // add next work to new line } first_word = false; @@ -56,7 +71,29 @@ std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width if( remaining_paragraph.size() > next_word.size() ) { remaining_paragraph = remaining_paragraph.substr( next_word.size() + 1 ); - next_word = remaining_paragraph.substr( 0, remaining_paragraph.find( " " ) ); + + + // Handle a case when the paragraph starts with spaces + if( remaining_paragraph[0] == ' ' ) + { + auto non_space_index = remaining_paragraph.find_first_not_of( ' ' ); + // If non spaces characters exist + if( non_space_index != std::string::npos ) + { + // Remove trailing spaces + remaining_paragraph = remaining_paragraph.substr( + remaining_paragraph.find_first_not_of( ' ' ) ); + } + } + + next_word = remaining_paragraph.substr( 0, remaining_paragraph.find( ' ' ) ); + + // If no more words exist, copy the current wrapped line to output and stop + if( next_word.empty() ) + { + wrapped_paragraph += wrapped_line; + break; + } } else { @@ -74,7 +111,7 @@ std::string wrap( const std::string & text, int wrap_pixels_width ) if( wrap_pixels_width < 32 ) return text; - // split text into paragraphs + // Split text into paragraphs auto paragraphs_vector = split( text, '\n' ); std::string wrapped_text; @@ -83,7 +120,7 @@ std::string wrap( const std::string & text, int wrap_pixels_width ) for( auto paragraph : paragraphs_vector ) { wrapped_text += wrap_paragraph( paragraph, wrap_pixels_width ); - // each paragraph except the last one ends with a new line + // Each paragraph except the last one ends with a new line if( line_number++ != paragraphs_vector.size() ) wrapped_text += '\n'; } diff --git a/unit-tests/utilities/string/test-wrap.cpp b/unit-tests/utilities/string/test-wrap.cpp index 9b9482c42e..b705770637 100644 --- a/unit-tests/utilities/string/test-wrap.cpp +++ b/unit-tests/utilities/string/test-wrap.cpp @@ -70,4 +70,17 @@ TEST_CASE( "wrap-text", "[string]" ) CHECK( wrap( "this is the first line\nthis is the second line\nthis is the last line", 60 ) == "this is\nthe\nfirst\nline\nthis is\nthe\nsecond\nline\nthis is\nthe\nlast\nline" ); + + CHECK(wrap("ab cd ", 32) == "ab\ncd"); + CHECK(wrap("ab cd", 32) == "ab\ncd"); // Multiple spaces + CHECK(wrap("ab cd ", 32) == "ab\ncd"); + CHECK(wrap(" ab cd ", 32) == "ab\ncd"); + CHECK(wrap(" ab cd ", 32) == "ab\ncd"); + CHECK(wrap(" ab ", 33) == "ab"); + CHECK(wrap("ab ", 33) == "ab"); + + CHECK(wrap(" ", 33) == ""); + CHECK(wrap(" ", 33) == ""); + CHECK(wrap(" ab", 33) == "ab"); + } From 6e8bdd31b95d611b26079254f84986821a299320 Mon Sep 17 00:00:00 2001 From: NirAz Date: Thu, 26 Nov 2020 15:26:53 +0200 Subject: [PATCH 4/8] cosmetic changes + rearrange UT --- common/utilities/string/wrap.cpp | 10 ++++------ unit-tests/utilities/string/test-wrap.cpp | 17 +++++++++-------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/common/utilities/string/wrap.cpp b/common/utilities/string/wrap.cpp index 0a02ddd628..9159e7fc5d 100644 --- a/common/utilities/string/wrap.cpp +++ b/common/utilities/string/wrap.cpp @@ -27,7 +27,7 @@ std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width { // Remove trailing spaces remaining_paragraph - = remaining_paragraph.substr( remaining_paragraph.find_first_not_of( ' ' ) ); + = remaining_paragraph.substr( non_space_index ); } } @@ -59,8 +59,8 @@ std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width wrapped_paragraph += wrapped_line; // copy line build by now if( ! first_word ) - wrapped_paragraph += '\n'; // break the previous line if exist - wrapped_line = next_word; // add next work to new line + wrapped_paragraph += '\n'; // break the previous line if exist + wrapped_line = next_word; // add next work to new line } first_word = false; @@ -72,7 +72,6 @@ std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width { remaining_paragraph = remaining_paragraph.substr( next_word.size() + 1 ); - // Handle a case when the paragraph starts with spaces if( remaining_paragraph[0] == ' ' ) { @@ -81,8 +80,7 @@ std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width if( non_space_index != std::string::npos ) { // Remove trailing spaces - remaining_paragraph = remaining_paragraph.substr( - remaining_paragraph.find_first_not_of( ' ' ) ); + remaining_paragraph = remaining_paragraph.substr( non_space_index ); } } diff --git a/unit-tests/utilities/string/test-wrap.cpp b/unit-tests/utilities/string/test-wrap.cpp index b705770637..d3a483b46e 100644 --- a/unit-tests/utilities/string/test-wrap.cpp +++ b/unit-tests/utilities/string/test-wrap.cpp @@ -71,16 +71,17 @@ TEST_CASE( "wrap-text", "[string]" ) CHECK( wrap( "this is the first line\nthis is the second line\nthis is the last line", 60 ) == "this is\nthe\nfirst\nline\nthis is\nthe\nsecond\nline\nthis is\nthe\nlast\nline" ); - CHECK(wrap("ab cd ", 32) == "ab\ncd"); - CHECK(wrap("ab cd", 32) == "ab\ncd"); // Multiple spaces - CHECK(wrap("ab cd ", 32) == "ab\ncd"); - CHECK(wrap(" ab cd ", 32) == "ab\ncd"); - CHECK(wrap(" ab cd ", 32) == "ab\ncd"); - CHECK(wrap(" ab ", 33) == "ab"); - CHECK(wrap("ab ", 33) == "ab"); + // Spaces checks + CHECK(wrap("ab cd ", 32) == "ab\ncd"); // Ending spaces + CHECK(wrap("ab cd", 32) == "ab\ncd"); // Middle spaces + CHECK(wrap(" ab cd ", 32) == "ab\ncd"); // Mixed multiple spaces + CHECK(wrap(" ab cd ", 32) == "ab\ncd"); // Mixed multiple spaces + CHECK(wrap(" ab ", 33) == "ab"); // Mixed multiple spaces + CHECK(wrap("ab ", 33) == "ab"); // Ending multiple spaces + CHECK(wrap(" ab", 33) == "ab"); + // Only spaces CHECK(wrap(" ", 33) == ""); CHECK(wrap(" ", 33) == ""); - CHECK(wrap(" ab", 33) == "ab"); } From 57847a31710f8ccac5f925d90542bf6314c87a13 Mon Sep 17 00:00:00 2001 From: NirAz Date: Sun, 6 Dec 2020 15:27:47 +0200 Subject: [PATCH 5/8] Updates after review II --- common/utilities/string/wrap.cpp | 38 +++++++++-------------- common/utilities/string/wrap.h | 1 + unit-tests/utilities/string/test-wrap.cpp | 3 ++ 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/common/utilities/string/wrap.cpp b/common/utilities/string/wrap.cpp index 9159e7fc5d..31ddac59fb 100644 --- a/common/utilities/string/wrap.cpp +++ b/common/utilities/string/wrap.cpp @@ -11,25 +11,27 @@ namespace utilities { namespace string { +void trim_trailing_spaces( std::string &remaining_paragraph ) +{ + auto non_space_index = remaining_paragraph.find_first_not_of( ' ' ); + + if( non_space_index != std::string::npos ) + { + // Remove trailing spaces + remaining_paragraph = remaining_paragraph.substr( non_space_index ); + } +} + std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width ) { float space_width = ImGui::CalcTextSize( " " ).x; // Calculate space width in pixels std::string wrapped_line; // The line that is wrapped in this iteration std::string wrapped_paragraph; // The output wrapped paragraph - auto remaining_paragraph = paragraph; // Holds the remaining unwrapped part of the input paragraph + auto remaining_paragraph + = paragraph; // Holds the remaining unwrapped part of the input paragraph // Handle a case when the paragraph starts with spaces - if( remaining_paragraph[0] == ' ' ) - { - auto non_space_index = remaining_paragraph.find_first_not_of( ' ' ); - // If non spaces characters exist - if( non_space_index != std::string::npos ) - { - // Remove trailing spaces - remaining_paragraph - = remaining_paragraph.substr( non_space_index ); - } - } + trim_trailing_spaces(remaining_paragraph); auto next_word = remaining_paragraph.substr( 0, @@ -38,7 +40,6 @@ std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width while( ! next_word.empty() ) { - float next_x = 0.0f; // If this is the first word we try to place it first in line, // if not we concatenate it to the last word after adding a space @@ -73,16 +74,7 @@ std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width remaining_paragraph = remaining_paragraph.substr( next_word.size() + 1 ); // Handle a case when the paragraph starts with spaces - if( remaining_paragraph[0] == ' ' ) - { - auto non_space_index = remaining_paragraph.find_first_not_of( ' ' ); - // If non spaces characters exist - if( non_space_index != std::string::npos ) - { - // Remove trailing spaces - remaining_paragraph = remaining_paragraph.substr( non_space_index ); - } - } + trim_trailing_spaces(remaining_paragraph); next_word = remaining_paragraph.substr( 0, remaining_paragraph.find( ' ' ) ); diff --git a/common/utilities/string/wrap.h b/common/utilities/string/wrap.h index 9b19cabe94..0e0d686247 100644 --- a/common/utilities/string/wrap.h +++ b/common/utilities/string/wrap.h @@ -16,6 +16,7 @@ namespace string { // this is the first line\nthis is the second line\nthis is the last line , wrap_width = 150 [pixels] // Output: // this is the\nfirst line\nthis is the\nsecond line\nthis is the last\nline +// Note: If the paragraph contain multiple spaces, it will be trimmed into a single space. std::string wrap( const std::string & text, int wrap_pixels_width ); } // namespace string diff --git a/unit-tests/utilities/string/test-wrap.cpp b/unit-tests/utilities/string/test-wrap.cpp index d3a483b46e..c5f1e9afc6 100644 --- a/unit-tests/utilities/string/test-wrap.cpp +++ b/unit-tests/utilities/string/test-wrap.cpp @@ -84,4 +84,7 @@ TEST_CASE( "wrap-text", "[string]" ) CHECK(wrap(" ", 33) == ""); CHECK(wrap(" ", 33) == ""); + CHECK(wrap("ab cd ", 100) == "ab cd"); + CHECK(wrap("ab cd", 100) == "ab cd"); // Known corner case - we trim multiple spaces + } From b939abbee529229f77422f249bc188ef2d7e00ef Mon Sep 17 00:00:00 2001 From: NirAz Date: Mon, 7 Dec 2020 19:55:14 +0200 Subject: [PATCH 6/8] move wrap to imgui utilities --- common/CMakeLists.txt | 4 ++-- common/utilities/{string => imgui}/wrap.cpp | 12 ++++++------ common/utilities/{string => imgui}/wrap.h | 2 +- common/viewer.cpp | 4 ++-- unit-tests/utilities/imgui/common.h | 15 +++++++++++++++ .../utilities/{string => imgui}/test-wrap.cpp | 8 ++++---- 6 files changed, 30 insertions(+), 15 deletions(-) rename common/utilities/{string => imgui}/wrap.cpp (93%) rename common/utilities/{string => imgui}/wrap.h (97%) create mode 100644 unit-tests/utilities/imgui/common.h rename unit-tests/utilities/{string => imgui}/test-wrap.cpp (93%) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 5f5f46b5b2..0f412adcf0 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -60,8 +60,8 @@ set(COMMON_SRC "${CMAKE_CURRENT_LIST_DIR}/utilities/number/stabilized-value.h" "${CMAKE_CURRENT_LIST_DIR}/utilities/string/trim-newlines.h" "${CMAKE_CURRENT_LIST_DIR}/utilities/string/split.h" - "${CMAKE_CURRENT_LIST_DIR}/utilities/string/wrap.h" - "${CMAKE_CURRENT_LIST_DIR}/utilities/string/wrap.cpp" + "${CMAKE_CURRENT_LIST_DIR}/utilities/imgui/wrap.h" + "${CMAKE_CURRENT_LIST_DIR}/utilities/imgui/wrap.cpp" ) set(COMMON_SRC diff --git a/common/utilities/string/wrap.cpp b/common/utilities/imgui/wrap.cpp similarity index 93% rename from common/utilities/string/wrap.cpp rename to common/utilities/imgui/wrap.cpp index 31ddac59fb..4f3bf50049 100644 --- a/common/utilities/string/wrap.cpp +++ b/common/utilities/imgui/wrap.cpp @@ -5,13 +5,13 @@ #include #include #include "wrap.h" -#include "split.h" +#include "../common/utilities/string/split.h" #include "../third-party/imgui/imgui.h" namespace utilities { -namespace string { +namespace imgui { -void trim_trailing_spaces( std::string &remaining_paragraph ) +void trim_leading_spaces( std::string &remaining_paragraph ) { auto non_space_index = remaining_paragraph.find_first_not_of( ' ' ); @@ -31,7 +31,7 @@ std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width = paragraph; // Holds the remaining unwrapped part of the input paragraph // Handle a case when the paragraph starts with spaces - trim_trailing_spaces(remaining_paragraph); + trim_leading_spaces(remaining_paragraph); auto next_word = remaining_paragraph.substr( 0, @@ -74,7 +74,7 @@ std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width remaining_paragraph = remaining_paragraph.substr( next_word.size() + 1 ); // Handle a case when the paragraph starts with spaces - trim_trailing_spaces(remaining_paragraph); + trim_leading_spaces(remaining_paragraph); next_word = remaining_paragraph.substr( 0, remaining_paragraph.find( ' ' ) ); @@ -102,7 +102,7 @@ std::string wrap( const std::string & text, int wrap_pixels_width ) return text; // Split text into paragraphs - auto paragraphs_vector = split( text, '\n' ); + auto paragraphs_vector = string::split( text, '\n' ); std::string wrapped_text; int line_number = 1; diff --git a/common/utilities/string/wrap.h b/common/utilities/imgui/wrap.h similarity index 97% rename from common/utilities/string/wrap.h rename to common/utilities/imgui/wrap.h index 0e0d686247..7afcb3ef12 100644 --- a/common/utilities/string/wrap.h +++ b/common/utilities/imgui/wrap.h @@ -4,7 +4,7 @@ #pragma once namespace utilities { -namespace string { +namespace imgui { // Wrap text according to input width // - Input: - text as a string diff --git a/common/viewer.cpp b/common/viewer.cpp index 10a096d8bf..722d21d2ca 100644 --- a/common/viewer.cpp +++ b/common/viewer.cpp @@ -22,7 +22,7 @@ #define ARCBALL_CAMERA_IMPLEMENTATION #include #include "../common/utilities/string/trim-newlines.h" -#include "../common/utilities/string/wrap.h" +#include "../common/utilities/imgui/wrap.h" namespace rs2 { @@ -1075,7 +1075,7 @@ namespace rs2 try { auto trimmed_msg = utilities::string::trim_newlines(msg); - wrapped_msg = utilities::string::wrap(trimmed_msg, 500); + wrapped_msg = utilities::imgui::wrap(trimmed_msg, 500); } catch (...) { diff --git a/unit-tests/utilities/imgui/common.h b/unit-tests/utilities/imgui/common.h new file mode 100644 index 0000000000..6274e5a819 --- /dev/null +++ b/unit-tests/utilities/imgui/common.h @@ -0,0 +1,15 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +#define CATCH_CONFIG_MAIN + +#include "../../catch.h" + +#include +#ifdef BUILD_SHARED_LIBS +// With static linkage, ELPP is initialized by librealsense, so doing it here will +// create errors. When we're using the shared .so/.dll, the two are separate and we have +// to initialize ours if we want to use the APIs! +INITIALIZE_EASYLOGGINGPP +#endif + diff --git a/unit-tests/utilities/string/test-wrap.cpp b/unit-tests/utilities/imgui/test-wrap.cpp similarity index 93% rename from unit-tests/utilities/string/test-wrap.cpp rename to unit-tests/utilities/imgui/test-wrap.cpp index c5f1e9afc6..74b5a35328 100644 --- a/unit-tests/utilities/string/test-wrap.cpp +++ b/unit-tests/utilities/imgui/test-wrap.cpp @@ -1,12 +1,12 @@ // License: Apache 2.0. See LICENSE file in root directory. // Copyright(c) 2020 Intel Corporation. All Rights Reserved. -//#cmake:add-file ../../../common/utilities/string/wrap.h -//#cmake:add-file ../../../common/utilities/string/wrap.cpp +//#cmake:add-file ../../../common/utilities/imgui/wrap.h +//#cmake:add-file ../../../common/utilities/imgui/wrap.cpp //#cmake:add-file ../../../third-party/imgui/imgui.h #include "common.h" -#include "../../../common/utilities/string/wrap.h" +#include "../../../common/utilities/imgui/wrap.h" #include "../../../third-party/imgui/imgui.h" namespace ImGui { @@ -32,7 +32,7 @@ ImVec2 CalcTextSize( const char * text, } } // namespace ImGui -using namespace utilities::string; +using namespace utilities::imgui; TEST_CASE( "wrap-text", "[string]" ) { From f06b9a851200976b37911773cc62f97348054db2 Mon Sep 17 00:00:00 2001 From: NirAz Date: Mon, 14 Dec 2020 13:27:40 +0200 Subject: [PATCH 7/8] fixes after review --- common/utilities/imgui/wrap.cpp | 4 ++-- common/utilities/imgui/wrap.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common/utilities/imgui/wrap.cpp b/common/utilities/imgui/wrap.cpp index 4f3bf50049..1d447e78d1 100644 --- a/common/utilities/imgui/wrap.cpp +++ b/common/utilities/imgui/wrap.cpp @@ -105,7 +105,7 @@ std::string wrap( const std::string & text, int wrap_pixels_width ) auto paragraphs_vector = string::split( text, '\n' ); std::string wrapped_text; - int line_number = 1; + size_t line_number = 1; // Wrap each line according to the requested wrap width for( auto paragraph : paragraphs_vector ) { @@ -118,5 +118,5 @@ std::string wrap( const std::string & text, int wrap_pixels_width ) return wrapped_text; } -} // namespace string +} // namespace imgui } // namespace utilities diff --git a/common/utilities/imgui/wrap.h b/common/utilities/imgui/wrap.h index 7afcb3ef12..4afd43b988 100644 --- a/common/utilities/imgui/wrap.h +++ b/common/utilities/imgui/wrap.h @@ -19,5 +19,5 @@ namespace imgui { // Note: If the paragraph contain multiple spaces, it will be trimmed into a single space. std::string wrap( const std::string & text, int wrap_pixels_width ); -} // namespace string +} // namespace imgui } // namespace utilities From f407ddbc7194039047103783fce35b3caf188167 Mon Sep 17 00:00:00 2001 From: NirAz Date: Mon, 14 Dec 2020 15:56:40 +0200 Subject: [PATCH 8/8] merge development --- common/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 0f412adcf0..585a37c608 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -63,11 +63,11 @@ set(COMMON_SRC "${CMAKE_CURRENT_LIST_DIR}/utilities/imgui/wrap.h" "${CMAKE_CURRENT_LIST_DIR}/utilities/imgui/wrap.cpp" ) - + set(COMMON_SRC ${COMMON_SRC} ${SW_UPDATE_FILES} ${REFLECTIVITY_FILES} - ${UTILITIES_FILES} - ) + ${UTILITIES_FILES} + )