]> git.armaanb.net Git - gen-shell.git/blob - src/libshared/src/Composite.cpp
added install instructions
[gen-shell.git] / src / libshared / src / Composite.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 - 2017, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #include <cmake.h>
28 #include <Composite.h>
29 #include <utf8.h>
30 #include <sstream>
31 #include <stack>
32
33 ////////////////////////////////////////////////////////////////////////////////
34 // Initially assume no text, but infinite virtual space.
35 //
36 // Ã…llow overlay placement of arbitrary text at any offset, real or virtual, and
37 // using a specific color.
38 //
39 // For example:
40 //   Composite c;
41 //   c.add ("aaaaaaaaaa",  2, Color ("..."));    // Layer 1
42 //   c.add ("bbbbb",       5, Color ("..."));    // Layer 2
43 //   c.add ("c",          15, Color ("..."));    // Layer 3
44 //
45 //   _layers = { std::make_tuple ("aaaaaaaaaa",  2, Color ("...")),
46 //               std::make_tuple ("bbbbb",       5, Color ("...")),
47 //               std::make_tuple ("c",          15, Color ("..."))};
48 //
49 void Composite::add (
50   const std::string& text,
51   std::string::size_type offset,
52   const Color& color)
53 {
54   _layers.push_back (std::make_tuple (text, offset, color));
55 }
56
57 ////////////////////////////////////////////////////////////////////////////////
58 // Merge the layers of text and color into one string.
59 //
60 // For example:
61 //   Composite c;
62 //   c.add ("aaaaaaaaaa",  2, Color ("..."));    // Layer 1
63 //   c.add ("bbbbb",       5, Color ("..."));    // Layer 2
64 //   c.add ("c",          15, Color ("..."));    // Layer 3
65 //
66 //   _layers = { std::make_tuple ("aaaaaaaaaa",  2, Color ("...")),
67 //               std::make_tuple ("bbbbb",       5, Color ("...")),
68 //               std::make_tuple ("c",          15, Color ("..."))};
69 //
70 // Arrange strings conceptually:
71 //              111111
72 //    0123456789012345     // Position
73 //
74 //      aaaaaaaaaa         // Layer 1
75 //         bbbbb           // Layer 2
76 //                   c     // Layer 3
77 //
78 // Walk all strings left to right, selecting the character and color from the
79 // highest numbered layer. Emit color codes only on edge detection.
80 //
81 std::string Composite::str () const
82 {
83   // The strings are broken into a vector of int, for UTF8 support.
84   std::vector <int> characters;
85   std::vector <int> colors;
86   for (unsigned int layer = 0; layer < _layers.size (); ++layer)
87   {
88     auto text   = std::get <0> (_layers[layer]);
89     auto offset = std::get <1> (_layers[layer]);
90     auto len    = utf8_text_length (text);
91
92     // Make sure the vectors are large enough to support a write operator[].
93     if (characters.size () < offset + len)
94     {
95       characters.resize (offset + len, 32);
96       colors.resize     (offset + len, 0);
97     }
98
99     // Copy in the layer characters and color indexes.
100     std::string::size_type cursor = 0;
101     int character;
102     int count = 0;
103     while ((character = utf8_next_char (text, cursor)))
104     {
105       characters[offset + count] = character;
106       colors    [offset + count] = layer + 1;
107       ++count;
108     }
109   }
110
111   // Now walk the character and color vector, emitting every character and
112   // every detected color change.
113   std::stringstream out;
114   int prev_color = 0;
115   for (unsigned int i = 0; i < characters.size (); ++i)
116   {
117     // A change in color triggers a code emit.
118     if (prev_color != colors[i])
119     {
120       if (prev_color)
121         out << std::get <2> (_layers[prev_color - 1]).end ();
122
123       if (colors[i])
124         out << std::get <2> (_layers[colors[i] - 1]).code ();
125       else
126         out << std::get <2> (_layers[prev_color - 1]).end ();
127
128       prev_color = colors[i];
129     }
130
131     out << utf8_character (characters[i]);
132   }
133
134   // Terminate the color codes, if necessary.
135   if (prev_color)
136     out << std::get <2> (_layers[prev_color - 1]).end ();
137
138   return out.str ();
139 }
140
141 ////////////////////////////////////////////////////////////////////////////////
142 // So the same instance can be reused.
143 void Composite::clear ()
144 {
145   _layers.clear ();
146 }
147
148 ////////////////////////////////////////////////////////////////////////////////