|
| 1 | +/* |
| 2 | + This file is part of terminal, KDE's terminal. |
| 3 | +
|
| 4 | + Copyright 2007-2008 by Robert Knight <[email protected]> |
| 5 | + Copyright 1997,1998 by Lars Doelle <[email protected]> |
| 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 | +// terminal |
| 27 | +#include "CharacterColor.h" |
| 28 | +#include "CharacterWidth.h" |
| 29 | + |
| 30 | +// Qt |
| 31 | +#include <QVector> |
| 32 | + |
| 33 | +namespace terminal { |
| 34 | +typedef unsigned char LineProperty; |
| 35 | + |
| 36 | +typedef quint16 RenditionFlags; |
| 37 | + |
| 38 | +const int LINE_DEFAULT = 0; |
| 39 | +const int LINE_WRAPPED = (1 << 0); |
| 40 | +const int LINE_DOUBLEWIDTH = (1 << 1); |
| 41 | +const int LINE_DOUBLEHEIGHT = (1 << 2); |
| 42 | + |
| 43 | +const RenditionFlags DEFAULT_RENDITION = 0; |
| 44 | +const RenditionFlags RE_BOLD = (1 << 0); |
| 45 | +const RenditionFlags RE_BLINK = (1 << 1); |
| 46 | +const RenditionFlags RE_UNDERLINE = (1 << 2); |
| 47 | +const RenditionFlags RE_REVERSE = (1 << 3); // Screen only |
| 48 | +const RenditionFlags RE_ITALIC = (1 << 4); |
| 49 | +const RenditionFlags RE_CURSOR = (1 << 5); |
| 50 | +const RenditionFlags RE_EXTENDED_CHAR = (1 << 6); |
| 51 | +const RenditionFlags RE_FAINT = (1 << 7); |
| 52 | +const RenditionFlags RE_STRIKEOUT = (1 << 8); |
| 53 | +const RenditionFlags RE_CONCEAL = (1 << 9); |
| 54 | +const RenditionFlags 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 | + */ |
| 61 | +class Character |
| 62 | +{ |
| 63 | +public: |
| 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 |
| 71 | + * is to be drawn. |
| 72 | + * @param _real Indicate whether this character really exists, or exists |
| 73 | + * simply as place holder. |
| 74 | + */ |
| 75 | + explicit inline Character(uint _c = ' ', |
| 76 | + CharacterColor _f = CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_FORE_COLOR), |
| 77 | + CharacterColor _b = CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_BACK_COLOR), |
| 78 | + RenditionFlags _r = DEFAULT_RENDITION, |
| 79 | + bool _real = true) |
| 80 | + : character(_c) |
| 81 | + , rendition(_r) |
| 82 | + , foregroundColor(_f) |
| 83 | + , backgroundColor(_b) |
| 84 | + , isRealCharacter(_real) { } |
| 85 | + |
| 86 | + /** The unicode character value for this character. |
| 87 | + * |
| 88 | + * if RE_EXTENDED_CHAR is set, character is a hash code which can be used to |
| 89 | + * look up the unicode character sequence in the ExtendedCharTable used to |
| 90 | + * create the sequence. |
| 91 | + */ |
| 92 | + uint character; |
| 93 | + |
| 94 | + /** A combination of RENDITION flags which specify options for drawing the character. */ |
| 95 | + RenditionFlags rendition; |
| 96 | + |
| 97 | + /** The foreground color used to draw this character. */ |
| 98 | + CharacterColor foregroundColor; |
| 99 | + |
| 100 | + /** The color used to draw this character's background. */ |
| 101 | + CharacterColor backgroundColor; |
| 102 | + |
| 103 | + /** Indicate whether this character really exists, or exists simply as place holder. |
| 104 | + * |
| 105 | + * TODO: this boolean filed can be further improved to become a enum filed, which |
| 106 | + * indicates different roles: |
| 107 | + * |
| 108 | + * RealCharacter: a character which really exists |
| 109 | + * PlaceHolderCharacter: a character which exists as place holder |
| 110 | + * TabStopCharacter: a special place holder for HT("\t") |
| 111 | + */ |
| 112 | + bool isRealCharacter; |
| 113 | + |
| 114 | + /** |
| 115 | + * returns true if the format (color, rendition flag) of the compared characters is equal |
| 116 | + */ |
| 117 | + bool equalsFormat(const Character &other) const; |
| 118 | + |
| 119 | + /** |
| 120 | + * Compares two characters and returns true if they have the same unicode character value, |
| 121 | + * rendition and colors. |
| 122 | + */ |
| 123 | + friend bool operator ==(const Character &a, const Character &b); |
| 124 | + |
| 125 | + /** |
| 126 | + * Compares two characters and returns true if they have different unicode character values, |
| 127 | + * renditions or colors. |
| 128 | + */ |
| 129 | + friend bool operator !=(const Character &a, const Character &b); |
| 130 | + |
| 131 | + inline bool isSpace() const |
| 132 | + { |
| 133 | + if (rendition & RE_EXTENDED_CHAR) { |
| 134 | + return false; |
| 135 | + } else { |
| 136 | + return QChar(character).isSpace(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + inline int width() const { |
| 141 | + return width(character); |
| 142 | + } |
| 143 | + |
| 144 | + static int width(uint ucs4) { |
| 145 | + return characterWidth(ucs4); |
| 146 | + } |
| 147 | + |
| 148 | + static int stringWidth(const uint *ucs4Str, int len) { |
| 149 | + int w = 0; |
| 150 | + for (int i = 0; i < len; ++i) { |
| 151 | + w += width(ucs4Str[i]); |
| 152 | + } |
| 153 | + return w; |
| 154 | + } |
| 155 | + |
| 156 | + inline static int stringWidth(const QString &str) { |
| 157 | + QVector<uint> ucs4Str = str.toUcs4(); |
| 158 | + return stringWidth(ucs4Str.constData(), ucs4Str.length()); |
| 159 | + } |
| 160 | +}; |
| 161 | + |
| 162 | +inline bool operator ==(const Character &a, const Character &b) |
| 163 | +{ |
| 164 | + return a.character == b.character && a.equalsFormat(b); |
| 165 | +} |
| 166 | + |
| 167 | +inline bool operator !=(const Character &a, const Character &b) |
| 168 | +{ |
| 169 | + return !operator==(a, b); |
| 170 | +} |
| 171 | + |
| 172 | +inline bool Character::equalsFormat(const Character &other) const |
| 173 | +{ |
| 174 | + return backgroundColor == other.backgroundColor |
| 175 | + && foregroundColor == other.foregroundColor |
| 176 | + && rendition == other.rendition; |
| 177 | +} |
| 178 | +} |
| 179 | +Q_DECLARE_TYPEINFO(terminal::Character, Q_MOVABLE_TYPE); |
| 180 | + |
| 181 | +#endif // CHARACTER_H |
0 commit comments