add more spacing
[personal-kdebase.git] / apps / konsole / src / Character.h
blobeba6b7afc0a803e7dcd126bf441f528b8556547e
1 /*
2 This file is part of Konsole, KDE's terminal.
4 Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
5 Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
23 #ifndef CHARACTER_H
24 #define CHARACTER_H
26 // Qt
27 #include <QtCore/QHash>
29 // Local
30 #include "CharacterColor.h"
32 namespace Konsole
35 typedef unsigned char LineProperty;
37 static const int LINE_DEFAULT = 0;
38 static const int LINE_WRAPPED = (1 << 0);
39 static const int LINE_DOUBLEWIDTH = (1 << 1);
40 static const int LINE_DOUBLEHEIGHT = (1 << 2);
42 #define DEFAULT_RENDITION 0
43 #define RE_BOLD (1 << 0)
44 #define RE_BLINK (1 << 1)
45 #define RE_UNDERLINE (1 << 2)
46 #define RE_REVERSE (1 << 3) // Screen only
47 #define RE_INTENSIVE (1 << 3) // Widget only
48 #define RE_CURSOR (1 << 4)
49 #define RE_EXTENDED_CHAR (1 << 5)
51 /**
52 * A single character in the terminal which consists of a unicode character
53 * value, foreground and background colors and a set of rendition attributes
54 * which specify how it should be drawn.
56 class Character
58 public:
59 /**
60 * Constructs a new character.
62 * @param _c The unicode character value of this character.
63 * @param _f The foreground color used to draw the character.
64 * @param _b The color used to draw the character's background.
65 * @param _r A set of rendition flags which specify how this character is to be drawn.
67 inline Character(quint16 _c = ' ',
68 CharacterColor _f = CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_FORE_COLOR),
69 CharacterColor _b = CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_BACK_COLOR),
70 quint8 _r = DEFAULT_RENDITION)
71 : character(_c), rendition(_r), foregroundColor(_f), backgroundColor(_b) {}
73 union
75 /** The unicode character value for this character. */
76 quint16 character;
77 /**
78 * Experimental addition which allows a single Character instance to contain more than
79 * one unicode character.
81 * charSequence is a hash code which can be used to look up the unicode
82 * character sequence in the ExtendedCharTable used to create the sequence.
84 quint16 charSequence;
87 /** A combination of RENDITION flags which specify options for drawing the character. */
88 quint8 rendition;
90 /** The foreground color used to draw this character. */
91 CharacterColor foregroundColor;
92 /** The color used to draw this character's background. */
93 CharacterColor backgroundColor;
95 /**
96 * Returns true if this character has a transparent background when
97 * it is drawn with the specified @p palette.
99 bool isTransparent(const ColorEntry* palette) const;
101 * Returns true if this character should always be drawn in bold when
102 * it is drawn with the specified @p palette, independent of whether
103 * or not the character has the RE_BOLD rendition flag.
105 ColorEntry::FontWeight fontWeight(const ColorEntry* base) const;
107 /**
108 * Compares two characters and returns true if they have the same unicode character value,
109 * rendition and colors.
111 friend bool operator == (const Character& a, const Character& b);
113 * Compares two characters and returns true if they have different unicode character values,
114 * renditions or colors.
116 friend bool operator != (const Character& a, const Character& b);
119 inline bool operator == (const Character& a, const Character& b)
121 return a.character == b.character &&
122 a.rendition == b.rendition &&
123 a.foregroundColor == b.foregroundColor &&
124 a.backgroundColor == b.backgroundColor;
127 inline bool operator != (const Character& a, const Character& b)
129 return a.character != b.character ||
130 a.rendition != b.rendition ||
131 a.foregroundColor != b.foregroundColor ||
132 a.backgroundColor != b.backgroundColor;
135 inline bool Character::isTransparent(const ColorEntry* base) const
137 return ((backgroundColor._colorSpace == COLOR_SPACE_DEFAULT) &&
138 base[backgroundColor._u+0+(backgroundColor._v?BASE_COLORS:0)].transparent)
139 || ((backgroundColor._colorSpace == COLOR_SPACE_SYSTEM) &&
140 base[backgroundColor._u+2+(backgroundColor._v?BASE_COLORS:0)].transparent);
143 inline ColorEntry::FontWeight Character::fontWeight(const ColorEntry* base) const
145 if (backgroundColor._colorSpace == COLOR_SPACE_DEFAULT)
146 return base[backgroundColor._u+0+(backgroundColor._v?BASE_COLORS:0)].fontWeight;
147 else if (backgroundColor._colorSpace == COLOR_SPACE_SYSTEM)
148 return base[backgroundColor._u+2+(backgroundColor._v?BASE_COLORS:0)].fontWeight;
149 else
150 return ColorEntry::UseCurrentFormat;
153 extern unsigned short vt100_graphics[32];
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.
162 class ExtendedCharTable
164 public:
165 /** Constructs a new character table. */
166 ExtendedCharTable();
167 ~ExtendedCharTable();
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()
174 * If the same sequence already exists in the table, the hash
175 * of the existing sequence will be returned.
177 * @param unicodePoints An array of unicode character points
178 * @param length Length of @p unicodePoints
180 ushort createExtendedChar(ushort* unicodePoints , ushort length);
182 * Looks up and returns a pointer to a sequence of unicode characters
183 * which was added to the table using createExtendedChar().
185 * @param hash The hash key returned by createExtendedChar()
186 * @param length This variable is set to the length of the
187 * character sequence.
189 * @return A unicode character sequence of size @p length.
191 ushort* lookupExtendedChar(ushort hash , ushort& length) const;
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;
208 Q_DECLARE_TYPEINFO(Konsole::Character, Q_MOVABLE_TYPE);
210 #endif // CHARACTER_H