MauiKit Terminal

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

KDE's Doxygen guidelines are available online.