wrap.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #include <string>
5 #include <vector>
6 #include <sstream>
7 #include "wrap.h"
8 #include "../common/utilities/string/split.h"
9 #include "../third-party/imgui/imgui.h"
10 
11 namespace utilities {
12 namespace imgui {
13 
14 void trim_leading_spaces( std::string &remaining_paragraph )
15 {
16  auto non_space_index = remaining_paragraph.find_first_not_of( ' ' );
17 
18  if( non_space_index != std::string::npos )
19  {
20  // Remove trailing spaces
21  remaining_paragraph = remaining_paragraph.substr( non_space_index );
22  }
23 }
24 
25 std::string wrap_paragraph( const std::string & paragraph, int wrap_pixels_width )
26 {
27  float space_width = ImGui::CalcTextSize( " " ).x; // Calculate space width in pixels
28  std::string wrapped_line; // The line that is wrapped in this iteration
29  std::string wrapped_paragraph; // The output wrapped paragraph
30  auto remaining_paragraph
31  = paragraph; // Holds the remaining unwrapped part of the input paragraph
32 
33  // Handle a case when the paragraph starts with spaces
34  trim_leading_spaces(remaining_paragraph);
35 
36  auto next_word = remaining_paragraph.substr(
37  0,
38  remaining_paragraph.find( ' ' ) ); // The next word to add to the current line
39  bool first_word = true;
40 
41  while( ! next_word.empty() )
42  {
43  float next_x = 0.0f;
44  // If this is the first word we try to place it first in line,
45  // if not we concatenate it to the last word after adding a space
46  if( ! first_word )
47  {
48  next_x = ImGui::CalcTextSize( wrapped_line.c_str() ).x + space_width;
49  }
50 
51  if( next_x + ImGui::CalcTextSize( next_word.c_str() ).x <= wrap_pixels_width )
52  {
53  if( ! first_word )
54  wrapped_line += " "; // First word should not start with " "
55  wrapped_line += next_word;
56  }
57  else
58  { // Current line cannot feat new word so we wrap the line and
59  // start building the new line
60 
61  wrapped_paragraph += wrapped_line; // copy line build by now
62  if( ! first_word )
63  wrapped_paragraph += '\n'; // break the previous line if exist
64  wrapped_line = next_word; // add next work to new line
65  }
66 
67  first_word = false;
68 
69  // If we have more characters left other then the current word, prepare inputs for next
70  // iteration, If not add the current line built so far to the output and finish current
71  // line wrap.
72  if( remaining_paragraph.size() > next_word.size() )
73  {
74  remaining_paragraph = remaining_paragraph.substr( next_word.size() + 1 );
75 
76  // Handle a case when the paragraph starts with spaces
77  trim_leading_spaces(remaining_paragraph);
78 
79  next_word = remaining_paragraph.substr( 0, remaining_paragraph.find( ' ' ) );
80 
81  // If no more words exist, copy the current wrapped line to output and stop
82  if( next_word.empty() )
83  {
84  wrapped_paragraph += wrapped_line;
85  break;
86  }
87  }
88  else
89  {
90  wrapped_paragraph += wrapped_line;
91  break;
92  }
93  }
94 
95  return wrapped_paragraph;
96 }
97 
98 std::string wrap( const std::string & text, int wrap_pixels_width )
99 {
100  // Do not wrap if the wrap is smaller then 32 pixels (~2 characters on font 16)
101  if( wrap_pixels_width < 32 )
102  return text;
103 
104  // Split text into paragraphs
105  auto paragraphs_vector = string::split( text, '\n' );
106 
107  std::string wrapped_text;
108  size_t line_number = 1;
109  // Wrap each line according to the requested wrap width
110  for( auto paragraph : paragraphs_vector )
111  {
112  wrapped_text += wrap_paragraph( paragraph, wrap_pixels_width );
113  // Each paragraph except the last one ends with a new line
114  if( line_number++ != paragraphs_vector.size() )
115  wrapped_text += '\n';
116  }
117 
118  return wrapped_text;
119 }
120 
121 } // namespace imgui
122 } // namespace utilities
GLsizei const GLchar *const * string
IMGUI_API ImVec2 CalcTextSize(const char *text, const char *text_end=NULL, bool hide_text_after_double_hash=false, float wrap_width=-1.0f)
Definition: test-wrap.cpp:15
GLdouble x
void trim_leading_spaces(std::string &remaining_paragraph)
Definition: wrap.cpp:14
std::string wrap_paragraph(const std::string &paragraph, int wrap_pixels_width)
Definition: wrap.cpp:25
std::vector< std::string > split(const std::string &str, char delimiter)
Definition: split.h:26
std::string wrap(const std::string &text, int wrap_pixels_width)
Definition: wrap.cpp:98
float x
Definition: imgui.h:90


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:23