ScummVM API documentation
UI_TextParser.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
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 3 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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef QDENGINE_SYSTEM_GRAPHICS_UI_TEXT_PARSER_H
23 #define QDENGINE_SYSTEM_GRAPHICS_UI_TEXT_PARSER_H
24 
25 
26 #include "qdengine/system/graphics/gr_font.h"
27 
28 
29 namespace QDEngine {
30 
31 struct OutNode {
32  enum {
33  NEW_LINE,
34  TEXT,
35  COLOR
36  } type;
37  int width;
38  union {
39  struct {
40  const char *begin;
41  const char *end;
42  } nl;
43  struct {
44  int style;
45  } t;
46  int color;
47  };
48  OutNode() : type(NEW_LINE), width(0) { nl.begin = 0; nl.end = 0; }
49  OutNode(const char *b, const char *e, int wd) : type(TEXT), width(wd) { nl.begin = b; nl.end = e; }
50  OutNode(int clr) : type(COLOR), width(0), color(clr) {}
51 };
52 
54 
56 public:
57  UI_TextParser(const grFont *font = 0);
58  UI_TextParser(const UI_TextParser &src);
59 
60  void operator= (const UI_TextParser &src);
61 
62  void setFont(const grFont *font);
63 
64  void parseString(const char *text, int color = 0, int fitIn = -1);
65 
66  const OutNodes &outNodes() const {
67  return _outNodes;
68  }
69 
70  int fontHeight() const {
71  return _font ? _font->size_y() : 1;
72  }
73  const Vect2i &size() const {
74  return _size;
75  }
76 
77  int lineCount() const {
78  return _lineCount;
79  }
80  OutNodes::const_iterator getLineBegin(int lineNum) const;
81 
82 private:
83  void init();
84 
85  inline int fromHex(char a) {
86  if (a >= '0' && a <= '9')
87  return a - '0';
88  if (a >= 'A' && a <= 'F')
89  return a - 'A' + 10;
90  if (a >= 'a' && a <= 'f')
91  return a - 'a' + 10;
92  return -1;
93  }
94 
95  inline void addChar(byte cc) {
96  int width = _font->char_width(cc);
97  if (testWidth(width) || cc != ' ')
98  _tagWidth += width;
99  ++_pstr;
100  }
101 
102  inline void skipNode() {
103  _lineBegin = _pstr;
104  _lastSpace = _lineBegin;
105  _lastTagWidth = 0;
106  _tagWidth = 0;
107  }
108 
109  inline void putNode(OutNode &node) {
110  _outNodes.push_back(node);
111  skipNode();
112  }
113 
114  void putText() {
115  if (_pstr == _lineBegin)
116  return;
117  _lineWidth += _tagWidth;
118  OutNode node(_lineBegin, _pstr, _tagWidth);
119  putNode(node);
120  }
121 
122  void endLine() {
123  _size.x = MAX(_size.x, _lineWidth);
124 
125  _outNodes[_prevLineIndex].width = _lineWidth;
126  _lineWidth = 0;
127 
128  _outNodes.push_back(OutNode());
129  _prevLineIndex = _outNodes.size() - 1;
130 
131  ++_lineCount;
132  }
133 
134  void getColor(int defColor);
135  int getStyle(const char *styleptr, const char *end);
136  int getToken();
137  bool testWidth(int width);
138 
139  OutNodes _outNodes;
140 
141  int _prevLineIndex;
142  const char *_lastSpace;
143  int _lastTagWidth;
144 
145  const char *_lineBegin;
146  const char *_pstr;
147  int _tagWidth;
148  int _lineWidth;
149 
150  int _fitIn;
151 
152  Vect2i _size;
153  int _lineCount;
154 
155  const grFont *_font;
156 
157 };
158 
159 } // namespace QDEngine
160 
161 #endif // QDENGINE_SYSTEM_GRAPHICS_UI_TEXT_PARSER_H
Definition: UI_TextParser.h:31
Базовый класс для игровых ресурсов.
Definition: console.h:28
Definition: xmath.h:268
Definition: UI_TextParser.h:55
T MAX(T a, T b)
Definition: util.h:62
Definition: gr_font.h:34