~verzegnassi-stefano/+junk/ubuntu-terminal-app-uitk13

« back to all changes in this revision

Viewing changes to src/plugin/qmltermwidget/qtermwidget/lib/Character.h

  • Committer: Filippo Scognamiglio
  • Date: 2014-10-25 04:42:31 UTC
  • Revision ID: flscogna@gmail.com-20141025044231-javjhusbqa171127
Initial reboot commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
32
namespace Konsole
 
33
{
 
34
 
 
35
typedef unsigned char LineProperty;
 
36
 
 
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);
 
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_CURSOR          (1 << 4)
 
49
#define RE_EXTENDED_CHAR   (1 << 5)
 
50
 
 
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.
 
55
 */
 
56
class Character
 
57
{
 
58
public:
 
59
  /** 
 
60
   * Constructs a new character.
 
61
   *
 
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.
 
66
   */
 
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) {}
 
72
 
 
73
  union
 
74
  {
 
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.
 
80
     *
 
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.
 
83
     */
 
84
    quint16 charSequence; 
 
85
  };
 
86
 
 
87
  /** A combination of RENDITION flags which specify options for drawing the character. */
 
88
  quint8  rendition;
 
89
 
 
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;
 
94
 
 
95
  /** 
 
96
   * Returns true if this character has a transparent background when
 
97
   * it is drawn with the specified @p palette.
 
98
   */
 
99
  bool   isTransparent(const ColorEntry* palette) const;
 
100
  /**
 
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. 
 
104
   */
 
105
  ColorEntry::FontWeight fontWeight(const ColorEntry* base) const;
 
106
  
 
107
  /** 
 
108
   * returns true if the format (color, rendition flag) of the compared characters is equal
 
109
   */
 
110
  bool equalsFormat(const Character &other) const;
 
111
 
 
112
  /** 
 
113
   * Compares two characters and returns true if they have the same unicode character value,
 
114
   * rendition and colors.
 
115
   */
 
116
  friend bool operator == (const Character& a, const Character& b);
 
117
  /**
 
118
   * Compares two characters and returns true if they have different unicode character values,
 
119
   * renditions or colors.
 
120
   */
 
121
  friend bool operator != (const Character& a, const Character& b);
 
122
};
 
123
 
 
124
inline bool operator == (const Character& a, const Character& b)
 
125
 
126
  return a.character == b.character && 
 
127
         a.rendition == b.rendition && 
 
128
         a.foregroundColor == b.foregroundColor && 
 
129
         a.backgroundColor == b.backgroundColor;
 
130
}
 
131
 
 
132
inline bool operator != (const Character& a, const Character& b)
 
133
{
 
134
  return    a.character != b.character || 
 
135
            a.rendition != b.rendition || 
 
136
            a.foregroundColor != b.foregroundColor || 
 
137
            a.backgroundColor != b.backgroundColor;
 
138
}
 
139
 
 
140
inline bool Character::isTransparent(const ColorEntry* base) const
 
141
{
 
142
  return ((backgroundColor._colorSpace == COLOR_SPACE_DEFAULT) && 
 
143
          base[backgroundColor._u+0+(backgroundColor._v?BASE_COLORS:0)].transparent)
 
144
      || ((backgroundColor._colorSpace == COLOR_SPACE_SYSTEM) && 
 
145
          base[backgroundColor._u+2+(backgroundColor._v?BASE_COLORS:0)].transparent);
 
146
}
 
147
 
 
148
inline bool Character::equalsFormat(const Character& other) const
 
149
{
 
150
  return 
 
151
    backgroundColor==other.backgroundColor &&
 
152
    foregroundColor==other.foregroundColor &&
 
153
    rendition==other.rendition;
 
154
}       
 
155
 
 
156
inline ColorEntry::FontWeight Character::fontWeight(const ColorEntry* base) const
 
157
{
 
158
    if (backgroundColor._colorSpace == COLOR_SPACE_DEFAULT)
 
159
        return base[backgroundColor._u+0+(backgroundColor._v?BASE_COLORS:0)].fontWeight;
 
160
    else if (backgroundColor._colorSpace == COLOR_SPACE_SYSTEM)
 
161
        return base[backgroundColor._u+2+(backgroundColor._v?BASE_COLORS:0)].fontWeight;
 
162
    else
 
163
        return ColorEntry::UseCurrentFormat;
 
164
}
 
165
 
 
166
extern unsigned short vt100_graphics[32];
 
167
 
 
168
 
 
169
/**
 
170
 * A table which stores sequences of unicode characters, referenced
 
171
 * by hash keys.  The hash key itself is the same size as a unicode
 
172
 * character ( ushort ) so that it can occupy the same space in
 
173
 * a structure.
 
174
 */
 
175
class ExtendedCharTable
 
176
{
 
177
public:
 
178
    /** Constructs a new character table. */
 
179
    ExtendedCharTable();
 
180
    ~ExtendedCharTable();
 
181
 
 
182
    /**
 
183
     * Adds a sequences of unicode characters to the table and returns
 
184
     * a hash code which can be used later to look up the sequence
 
185
     * using lookupExtendedChar()
 
186
     *
 
187
     * If the same sequence already exists in the table, the hash
 
188
     * of the existing sequence will be returned.
 
189
     *
 
190
     * @param unicodePoints An array of unicode character points
 
191
     * @param length Length of @p unicodePoints
 
192
     */
 
193
    ushort createExtendedChar(ushort* unicodePoints , ushort length);
 
194
    /**
 
195
     * Looks up and returns a pointer to a sequence of unicode characters
 
196
     * which was added to the table using createExtendedChar().
 
197
     *
 
198
     * @param hash The hash key returned by createExtendedChar()
 
199
     * @param length This variable is set to the length of the 
 
200
     * character sequence.
 
201
     *
 
202
     * @return A unicode character sequence of size @p length.
 
203
     */
 
204
    ushort* lookupExtendedChar(ushort hash , ushort& length) const;
 
205
 
 
206
    /** The global ExtendedCharTable instance. */
 
207
    static ExtendedCharTable instance;
 
208
private:
 
209
    // calculates the hash key of a sequence of unicode points of size 'length'
 
210
    ushort extendedCharHash(ushort* unicodePoints , ushort length) const;
 
211
    // tests whether the entry in the table specified by 'hash' matches the 
 
212
    // character sequence 'unicodePoints' of size 'length'
 
213
    bool extendedCharMatch(ushort hash , ushort* unicodePoints , ushort length) const;
 
214
    // internal, maps hash keys to character sequence buffers.  The first ushort
 
215
    // in each value is the length of the buffer, followed by the ushorts in the buffer
 
216
    // themselves.
 
217
    QHash<ushort,ushort*> extendedCharTable;
 
218
};
 
219
 
 
220
}
 
221
Q_DECLARE_TYPEINFO(Konsole::Character, Q_MOVABLE_TYPE);
 
222
 
 
223
#endif // CHARACTER_H
 
224