source: ogBrowser-Git/qtermwidget/lib/Character.h

jenkins
Last change on this file was fedf2a2, checked in by Vadim Troshchinskiy Shmelev <vtroshchinskiy@…>, 18 months ago

Update Qtermwidget to Qt6 version
Remove build files

  • Property mode set to 100644
File size: 8.0 KB
Line 
1/*
2    This file is part of Konsole, KDE's terminal.
3
4    Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
5    Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
6
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.
11
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.
16
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.
21*/
22
23#ifndef CHARACTER_H
24#define CHARACTER_H
25
26// Qt
27#include <QHash>
28
29// Local
30#include "CharacterColor.h"
31
32namespace Konsole
33{
34
35typedef unsigned char LineProperty;
36
37static const int LINE_DEFAULT        = 0;
38static const int LINE_WRAPPED          = (1 << 0);
39static const int LINE_DOUBLEWIDTH      = (1 << 1);
40static const int LINE_DOUBLEHEIGHT    = (1 << 2);
41
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_ITALIC          (1 << 4)
49#define RE_CURSOR          (1 << 5)
50#define RE_EXTENDED_CHAR   (1 << 6)
51#define RE_FAINT           (1 << 7)
52#define RE_STRIKEOUT       (1 << 8)
53#define RE_CONCEAL         (1 << 9)
54#define RE_OVERLINE        (1 << 10)
55
56/**
57 * A single character in the terminal which consists of a unicode character
58 * value, foreground and background colors and a set of rendition attributes
59 * which specify how it should be drawn.
60 */
61class Character
62{
63public:
64  /**
65   * Constructs a new character.
66   *
67   * @param _c The unicode character value of this character.
68   * @param _f The foreground color used to draw the character.
69   * @param _b The color used to draw the character's background.
70   * @param _r A set of rendition flags which specify how this character is to be drawn.
71   */
72  inline Character(quint16 _c = ' ',
73            CharacterColor  _f = CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_FORE_COLOR),
74            CharacterColor  _b = CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_BACK_COLOR),
75            quint8  _r = DEFAULT_RENDITION)
76       : character(_c), rendition(_r), foregroundColor(_f), backgroundColor(_b) {}
77
78  union
79  {
80    /** The unicode character value for this character. */
81#if QT_VERSION >= 0x060000
82    char16_t character;
83#else
84    wchar_t character;
85#endif
86    /**
87     * Experimental addition which allows a single Character instance to contain more than
88     * one unicode character.
89     *
90     * charSequence is a hash code which can be used to look up the unicode
91     * character sequence in the ExtendedCharTable used to create the sequence.
92     */
93    quint16 charSequence;
94  };
95
96  /** A combination of RENDITION flags which specify options for drawing the character. */
97  quint8  rendition;
98
99  /** The foreground color used to draw this character. */
100  CharacterColor  foregroundColor;
101  /** The color used to draw this character's background. */
102  CharacterColor  backgroundColor;
103
104  /**
105   * Returns true if this character has a transparent background when
106   * it is drawn with the specified @p palette.
107   */
108  bool   isTransparent(const ColorEntry* palette) const;
109  /**
110   * Returns true if this character should always be drawn in bold when
111   * it is drawn with the specified @p palette, independent of whether
112   * or not the character has the RE_BOLD rendition flag.
113   */
114  ColorEntry::FontWeight fontWeight(const ColorEntry* base) const;
115
116  /**
117   * returns true if the format (color, rendition flag) of the compared characters is equal
118   */
119  bool equalsFormat(const Character &other) const;
120
121  /**
122   * Compares two characters and returns true if they have the same unicode character value,
123   * rendition and colors.
124   */
125  friend bool operator == (const Character& a, const Character& b);
126  /**
127   * Compares two characters and returns true if they have different unicode character values,
128   * renditions or colors.
129   */
130  friend bool operator != (const Character& a, const Character& b);
131};
132
133inline bool operator == (const Character& a, const Character& b)
134{
135  return a.character == b.character &&
136         a.rendition == b.rendition &&
137         a.foregroundColor == b.foregroundColor &&
138         a.backgroundColor == b.backgroundColor;
139}
140
141inline bool operator != (const Character& a, const Character& b)
142{
143  return    a.character != b.character ||
144            a.rendition != b.rendition ||
145            a.foregroundColor != b.foregroundColor ||
146            a.backgroundColor != b.backgroundColor;
147}
148
149inline bool Character::isTransparent(const ColorEntry* base) const
150{
151  return ((backgroundColor._colorSpace == COLOR_SPACE_DEFAULT) &&
152          base[backgroundColor._u+0+(backgroundColor._v?BASE_COLORS:0)].transparent)
153      || ((backgroundColor._colorSpace == COLOR_SPACE_SYSTEM) &&
154          base[backgroundColor._u+2+(backgroundColor._v?BASE_COLORS:0)].transparent);
155}
156
157inline bool Character::equalsFormat(const Character& other) const
158{
159  return
160    backgroundColor==other.backgroundColor &&
161    foregroundColor==other.foregroundColor &&
162    rendition==other.rendition;
163}
164
165inline ColorEntry::FontWeight Character::fontWeight(const ColorEntry* base) const
166{
167    if (backgroundColor._colorSpace == COLOR_SPACE_DEFAULT)
168        return base[backgroundColor._u+0+(backgroundColor._v?BASE_COLORS:0)].fontWeight;
169    else if (backgroundColor._colorSpace == COLOR_SPACE_SYSTEM)
170        return base[backgroundColor._u+2+(backgroundColor._v?BASE_COLORS:0)].fontWeight;
171    else
172        return ColorEntry::UseCurrentFormat;
173}
174
175extern unsigned short vt100_graphics[32];
176
177
178/**
179 * A table which stores sequences of unicode characters, referenced
180 * by hash keys.  The hash key itself is the same size as a unicode
181 * character ( ushort ) so that it can occupy the same space in
182 * a structure.
183 */
184class ExtendedCharTable
185{
186public:
187    /** Constructs a new character table. */
188    ExtendedCharTable();
189    ~ExtendedCharTable();
190
191    /**
192     * Adds a sequences of unicode characters to the table and returns
193     * a hash code which can be used later to look up the sequence
194     * using lookupExtendedChar()
195     *
196     * If the same sequence already exists in the table, the hash
197     * of the existing sequence will be returned.
198     *
199     * @param unicodePoints An array of unicode character points
200     * @param length Length of @p unicodePoints
201     */
202    ushort createExtendedChar(ushort* unicodePoints , ushort length);
203    /**
204     * Looks up and returns a pointer to a sequence of unicode characters
205     * which was added to the table using createExtendedChar().
206     *
207     * @param hash The hash key returned by createExtendedChar()
208     * @param length This variable is set to the length of the
209     * character sequence.
210     *
211     * @return A unicode character sequence of size @p length.
212     */
213    ushort* lookupExtendedChar(ushort hash , ushort& length) const;
214
215    /** The global ExtendedCharTable instance. */
216    static ExtendedCharTable instance;
217private:
218    // calculates the hash key of a sequence of unicode points of size 'length'
219    ushort extendedCharHash(ushort* unicodePoints , ushort length) const;
220    // tests whether the entry in the table specified by 'hash' matches the
221    // character sequence 'unicodePoints' of size 'length'
222    bool extendedCharMatch(ushort hash , ushort* unicodePoints , ushort length) const;
223    // internal, maps hash keys to character sequence buffers.  The first ushort
224    // in each value is the length of the buffer, followed by the ushorts in the buffer
225    // themselves.
226    QHash<ushort,ushort*> extendedCharTable;
227};
228
229}
230Q_DECLARE_TYPEINFO(Konsole::Character, Q_MOVABLE_TYPE);
231
232#endif // CHARACTER_H
233
Note: See TracBrowser for help on using the repository browser.