1 | /* |
---|
2 | This file is part of Konsole, KDE's terminal. |
---|
3 | |
---|
4 | Copyright (C) 2007 by Robert Knight <robertknight@gmail.com> |
---|
5 | Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de> |
---|
6 | |
---|
7 | Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008 |
---|
8 | |
---|
9 | This program is free software; you can redistribute it and/or modify |
---|
10 | it under the terms of the GNU General Public License as published by |
---|
11 | the Free Software Foundation; either version 2 of the License, or |
---|
12 | (at your option) any later version. |
---|
13 | |
---|
14 | This program is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | GNU General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
22 | 02110-1301 USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef CHARACTER_H |
---|
26 | #define CHARACTER_H |
---|
27 | |
---|
28 | // Qt |
---|
29 | #include <QtCore/QHash> |
---|
30 | |
---|
31 | // Local |
---|
32 | #include "CharacterColor.h" |
---|
33 | |
---|
34 | namespace Konsole |
---|
35 | { |
---|
36 | |
---|
37 | typedef unsigned char LineProperty; |
---|
38 | |
---|
39 | static const int LINE_DEFAULT = 0; |
---|
40 | static const int LINE_WRAPPED = (1 << 0); |
---|
41 | static const int LINE_DOUBLEWIDTH = (1 << 1); |
---|
42 | static const int LINE_DOUBLEHEIGHT = (1 << 2); |
---|
43 | |
---|
44 | #define DEFAULT_RENDITION 0 |
---|
45 | #define RE_BOLD (1 << 0) |
---|
46 | #define RE_BLINK (1 << 1) |
---|
47 | #define RE_UNDERLINE (1 << 2) |
---|
48 | #define RE_REVERSE (1 << 3) // Screen only |
---|
49 | #define RE_INTENSIVE (1 << 3) // Widget only |
---|
50 | #define RE_CURSOR (1 << 4) |
---|
51 | #define RE_EXTENDED_CHAR (1 << 5) |
---|
52 | |
---|
53 | /** |
---|
54 | * A single character in the terminal which consists of a unicode character |
---|
55 | * value, foreground and background colors and a set of rendition attributes |
---|
56 | * which specify how it should be drawn. |
---|
57 | */ |
---|
58 | class Character |
---|
59 | { |
---|
60 | public: |
---|
61 | /** |
---|
62 | * Constructs a new character. |
---|
63 | * |
---|
64 | * @param _c The unicode character value of this character. |
---|
65 | * @param _f The foreground color used to draw the character. |
---|
66 | * @param _b The color used to draw the character's background. |
---|
67 | * @param _r A set of rendition flags which specify how this character is to be drawn. |
---|
68 | */ |
---|
69 | inline Character(quint16 _c = ' ', |
---|
70 | CharacterColor _f = CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_FORE_COLOR), |
---|
71 | CharacterColor _b = CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_BACK_COLOR), |
---|
72 | quint8 _r = DEFAULT_RENDITION) |
---|
73 | : character(_c), rendition(_r), foregroundColor(_f), backgroundColor(_b) {} |
---|
74 | |
---|
75 | union |
---|
76 | { |
---|
77 | /** The unicode character value for this character. */ |
---|
78 | quint16 character; |
---|
79 | /** |
---|
80 | * Experimental addition which allows a single Character instance to contain more than |
---|
81 | * one unicode character. |
---|
82 | * |
---|
83 | * charSequence is a hash code which can be used to look up the unicode |
---|
84 | * character sequence in the ExtendedCharTable used to create the sequence. |
---|
85 | */ |
---|
86 | quint16 charSequence; |
---|
87 | }; |
---|
88 | |
---|
89 | /** A combination of RENDITION flags which specify options for drawing the character. */ |
---|
90 | quint8 rendition; |
---|
91 | |
---|
92 | /** The foreground color used to draw this character. */ |
---|
93 | CharacterColor foregroundColor; |
---|
94 | /** The color used to draw this character's background. */ |
---|
95 | CharacterColor backgroundColor; |
---|
96 | |
---|
97 | /** |
---|
98 | * Returns true if this character has a transparent background when |
---|
99 | * it is drawn with the specified @p palette. |
---|
100 | */ |
---|
101 | bool isTransparent(const ColorEntry* palette) const; |
---|
102 | /** |
---|
103 | * Returns true if this character should always be drawn in bold when |
---|
104 | * it is drawn with the specified @p palette, independent of whether |
---|
105 | * or not the character has the RE_BOLD rendition flag. |
---|
106 | */ |
---|
107 | bool isBold(const ColorEntry* base) const; |
---|
108 | |
---|
109 | /** |
---|
110 | * Compares two characters and returns true if they have the same unicode character value, |
---|
111 | * rendition and colors. |
---|
112 | */ |
---|
113 | friend bool operator == (const Character& a, const Character& b); |
---|
114 | /** |
---|
115 | * Compares two characters and returns true if they have different unicode character values, |
---|
116 | * renditions or colors. |
---|
117 | */ |
---|
118 | friend bool operator != (const Character& a, const Character& b); |
---|
119 | }; |
---|
120 | |
---|
121 | inline bool operator == (const Character& a, const Character& b) |
---|
122 | { |
---|
123 | return a.character == b.character && |
---|
124 | a.rendition == b.rendition && |
---|
125 | a.foregroundColor == b.foregroundColor && |
---|
126 | a.backgroundColor == b.backgroundColor; |
---|
127 | } |
---|
128 | |
---|
129 | inline bool operator != (const Character& a, const Character& b) |
---|
130 | { |
---|
131 | return a.character != b.character || |
---|
132 | a.rendition != b.rendition || |
---|
133 | a.foregroundColor != b.foregroundColor || |
---|
134 | a.backgroundColor != b.backgroundColor; |
---|
135 | } |
---|
136 | |
---|
137 | inline bool Character::isTransparent(const ColorEntry* base) const |
---|
138 | { |
---|
139 | return ((backgroundColor._colorSpace == COLOR_SPACE_DEFAULT) && |
---|
140 | base[backgroundColor._u+0+(backgroundColor._v?BASE_COLORS:0)].transparent) |
---|
141 | || ((backgroundColor._colorSpace == COLOR_SPACE_SYSTEM) && |
---|
142 | base[backgroundColor._u+2+(backgroundColor._v?BASE_COLORS:0)].transparent); |
---|
143 | } |
---|
144 | |
---|
145 | inline bool Character::isBold(const ColorEntry* base) const |
---|
146 | { |
---|
147 | return ((backgroundColor._colorSpace == COLOR_SPACE_DEFAULT) && |
---|
148 | base[backgroundColor._u+0+(backgroundColor._v?BASE_COLORS:0)].bold) |
---|
149 | || ((backgroundColor._colorSpace == COLOR_SPACE_SYSTEM) && |
---|
150 | base[backgroundColor._u+2+(backgroundColor._v?BASE_COLORS:0)].bold); |
---|
151 | } |
---|
152 | |
---|
153 | extern unsigned short vt100_graphics[32]; |
---|
154 | |
---|
155 | |
---|
156 | /** |
---|
157 | * A table which stores sequences of unicode characters, referenced |
---|
158 | * by hash keys. The hash key itself is the same size as a unicode |
---|
159 | * character ( ushort ) so that it can occupy the same space in |
---|
160 | * a structure. |
---|
161 | */ |
---|
162 | class ExtendedCharTable |
---|
163 | { |
---|
164 | public: |
---|
165 | /** Constructs a new character table. */ |
---|
166 | ExtendedCharTable(); |
---|
167 | ~ExtendedCharTable(); |
---|
168 | |
---|
169 | /** |
---|
170 | * Adds a sequences of unicode characters to the table and returns |
---|
171 | * a hash code which can be used later to look up the sequence |
---|
172 | * using lookupExtendedChar() |
---|
173 | * |
---|
174 | * If the same sequence already exists in the table, the hash |
---|
175 | * of the existing sequence will be returned. |
---|
176 | * |
---|
177 | * @param unicodePoints An array of unicode character points |
---|
178 | * @param length Length of @p unicodePoints |
---|
179 | */ |
---|
180 | ushort createExtendedChar(ushort* unicodePoints , ushort length); |
---|
181 | /** |
---|
182 | * Looks up and returns a pointer to a sequence of unicode characters |
---|
183 | * which was added to the table using createExtendedChar(). |
---|
184 | * |
---|
185 | * @param hash The hash key returned by createExtendedChar() |
---|
186 | * @param length This variable is set to the length of the |
---|
187 | * character sequence. |
---|
188 | * |
---|
189 | * @return A unicode character sequence of size @p length. |
---|
190 | */ |
---|
191 | ushort* lookupExtendedChar(ushort hash , ushort& length) const; |
---|
192 | |
---|
193 | /** The global ExtendedCharTable instance. */ |
---|
194 | static ExtendedCharTable instance; |
---|
195 | private: |
---|
196 | // calculates the hash key of a sequence of unicode points of size 'length' |
---|
197 | ushort extendedCharHash(ushort* unicodePoints , ushort length) const; |
---|
198 | // tests whether the entry in the table specified by 'hash' matches the |
---|
199 | // character sequence 'unicodePoints' of size 'length' |
---|
200 | bool extendedCharMatch(ushort hash , ushort* unicodePoints , ushort length) const; |
---|
201 | // internal, maps hash keys to character sequence buffers. The first ushort |
---|
202 | // in each value is the length of the buffer, followed by the ushorts in the buffer |
---|
203 | // themselves. |
---|
204 | QHash<ushort,ushort*> extendedCharTable; |
---|
205 | }; |
---|
206 | |
---|
207 | } |
---|
208 | |
---|
209 | #endif // CHARACTER_H |
---|
210 | |
---|