sources/coloredline.cpp

changeset 46
19be47c9bab7
child 47
35b968619b0c
equal deleted inserted replaced
45:87b180260a5d 46:19be47c9bab7
1 /*
2 Copyright 2014 Teemu Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the copyright holder nor the names of its
15 contributors may be used to endorse or promote products derived from
16 this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "coloredline.h"
32
33 static const struct { Color color; bool bold; } g_colorCodes['v' - 'a' + 1] =
34 {
35 { RED, true }, // a - brick
36 { YELLOW, true }, // b - tan
37 { WHITE, false }, // c - gray
38 { GREEN, true }, // d - light green
39 { YELLOW, false }, // e - brown
40 { YELLOW, true }, // f - gold yellow
41 { RED, true }, // g - bright red
42 { BLUE, false }, // h - dark blue
43 { YELLOW, false }, // i - orange
44 { WHITE, true }, // j - white
45 { YELLOW, true }, // k - fire yellow
46 { DEFAULT, false }, // l - untranslated
47 { BLACK, false }, // m - black
48 { BLUE, true }, // n - light blue
49 { YELLOW, true }, // o - cream
50 { GREEN, true }, // p - olive green
51 { GREEN, false }, // q - dark green
52 { RED, false }, // r - dark red
53 { YELLOW, false }, // s - dark brown
54 { MAGENTA, false }, // t - purple
55 { BLACK, true }, // u - dark gray
56 { CYAN, true }, // v - cyan
57 };
58
59 // -------------------------------------------------------------------------------------------------
60 //
61 METHOD
62 ColoredLine::finalize() -> void
63 {
64 if (m_activeColor != DEFAULT)
65 this->set_color (m_activeColor, false);
66
67 if (m_boldActive)
68 m_data << RLINE_OFF_BOLD;
69
70 m_final = true;
71 }
72
73 // -------------------------------------------------------------------------------------------------
74 //
75 METHOD
76 ColoredLine::add_char (char ch) -> void
77 {
78 if (m_final)
79 return; // Don't touch finalized lines.
80
81 if (ch == '\x1C' and m_colorCodeStage == 0)
82 {
83 m_colorCodeStage = 1;
84 return;
85 }
86
87 if (m_colorCodeStage == 1)
88 {
89 if (m_activeColor != DEFAULT)
90 this->set_color (m_activeColor, false);
91
92 if (m_boldActive)
93 m_data << RLINE_OFF_BOLD;
94
95 if (ch >= 'a' and ch <= 'v' and ch != 'l')
96 {
97 auto colorInfo = g_colorCodes[ch - 'a'];
98 m_activeColor = colorInfo.color;
99 m_boldActive = colorInfo.bold;
100 assert (m_activeColor < 8);
101 this->set_color (m_activeColor, true);
102
103 if (m_boldActive)
104 m_data << RLINE_ON_BOLD;
105 }
106
107 m_colorCodeStage = 0;
108 return;
109 }
110
111 if (isprint (ch))
112 {
113 m_string += ch;
114 m_data << int (ch);
115 ++m_length;
116 }
117 }
118
119 // -------------------------------------------------------------------------------------------------
120 //
121 METHOD
122 ColoredLine::set_color (Color a, bool on) -> void
123 {
124 switch (a)
125 {
126 case BLACK: m_data << (on ? RLINE_ON_BLACK : RLINE_OFF_BLACK); break;
127 case RED: m_data << (on ? RLINE_ON_RED : RLINE_OFF_RED); break;
128 case GREEN: m_data << (on ? RLINE_ON_GREEN : RLINE_OFF_GREEN); break;
129 case YELLOW: m_data << (on ? RLINE_ON_YELLOW : RLINE_OFF_YELLOW); break;
130 case BLUE: m_data << (on ? RLINE_ON_BLUE : RLINE_OFF_BLUE); break;
131 case MAGENTA: m_data << (on ? RLINE_ON_MAGENTA : RLINE_OFF_MAGENTA); break;
132 case CYAN: m_data << (on ? RLINE_ON_CYAN : RLINE_OFF_CYAN); break;
133 case WHITE: m_data << (on ? RLINE_ON_WHITE : RLINE_OFF_WHITE); break;
134 case NUM_COLORS:
135 case DEFAULT: assert (false); break;
136 }
137 }
138
139 // -------------------------------------------------------------------------------------------------
140 // How many rows does this line take up?
141 //
142 METHOD
143 ColoredLine::rows (int cols) const -> int
144 {
145 int rows = length() / cols;
146
147 if (length() % cols != 0)
148 rows++;
149
150 return max (rows, 1);
151 }

mercurial