/
font.h
351 lines (302 loc) · 11.4 KB
/
font.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/** @file font.h Font with metrics.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBGUI_FONT_H
#define LIBGUI_FONT_H
#include <de/libdeng2.h>
#include <de/Rule>
#include <de/Rectangle>
#include <de/String>
#include <de/Vector>
#include <de/Range>
#include <de/EscapeParser>
#include <QFont>
#include <QImage>
#include <QList>
#include "libgui.h"
namespace de {
class NativeFont;
/**
* Font with metrics.
*/
class LIBGUI_PUBLIC Font
{
public:
typedef QVector<int> TabStops;
/**
* Rich formatting instructions for a string of plain text.
*
* The formatting instructions are composed of a sequence of ranges that
* specify various modifications to the original font. It is important to
* note that a RichFormat instance always needs to be set up for a specific
* source text string. Also, RichFormat is out-of-band data: when operating
* on a piece of rich text (using the methods of Font), the formatting is
* always provided in parallel to the plain version of the text.
*
* Use RichFormat::fromPlainText() to set up a RichFormat for a string of
* plain text.
*
* Use RichFormat::initFromStyledText() to set up a RichFormat for a string
* of text that contains style information (as escape sequences that start
* with the Esc ASCII code 0x1b).
*
* @par Indentation and tab stops
*
* The escape sequences ">" and "T" can be used to define indentation
* points and tab stops for visual alignment of text.
*
* The indentation mark ">" determines the width at which the following
* lines are left-indented. It does not affect the line the mark itself is
* on. Newlines (\n) do not reset the indentation. If multiple marks are
* used in the same text content, each mark is applied cumulatively on the
* previous indentation. Also note that placing an indentation mark at the
* beginning of a line or immediately after a new line does nothing (a
* width of zero does not increase the indentation).
*
* Tab stops are used to divide text on a line into smaller segments that
* can then be freely moved when drawn. All segments with the same tab stop
* number are aligned which the same stops at other lines of the content,
* providing there is enough horizontal space available. If tab stop
* alignment runs out of space, width of the rightmost segments is shrunk
* slightly to fit.
*
* The escape sequence "Ta" sets the tab stop to zero for the following
* text. The default tab stop is zero. "Tb" sets the tab stop to 1, "Tc" to
* 2, etc.
*/
class LIBGUI_PUBLIC RichFormat
{
public:
enum ContentStyle {
NormalStyle = 0,
MajorStyle = 1,
MinorStyle = 2,
MetaStyle = 3,
MajorMetaStyle = 4,
MinorMetaStyle = 5,
AuxMetaStyle = 6
};
enum Weight {
OriginalWeight = -1,
Normal = 0,
Light = 1,
Bold = 2
};
enum Style {
OriginalStyle = -1,
Regular = 0,
Italic = 1,
Monospace = 2
};
enum Color {
OriginalColor = -1,
NormalColor = 0,
HighlightColor = 1,
DimmedColor = 2,
AccentColor = 3,
DimAccentColor = 4
};
/**
* Interface for an object providing style information: fonts and
* colors.
*/
class LIBGUI_PUBLIC IStyle
{
public:
typedef Vector4ub Color;
virtual ~IStyle() {}
/**
* Returns a color from the style's palette.
* @param index Color index (see RichFormat::Color).
* @return Color values (RGBA 0...255).
*/
virtual Color richStyleColor(int index) const = 0;
virtual void richStyleFormat(int contentStyle, float &sizeFactor, Weight &fontWeight,
Style &fontStyle, int &colorIndex) const = 0;
/**
* Returns a font to be used with a particular style.
* @param fontStyle Style.
* @return @c NULL to use the default font.
*/
virtual Font const *richStyleFont(Style fontStyle) const {
DENG2_UNUSED(fontStyle);
return NULL; // Use default.
}
};
/**
* Reference to a (portion) of an existing RichFormat instance.
*/
class LIBGUI_PUBLIC Ref
{
public:
Ref(Ref const &ref);
Ref(Ref const &ref, Rangei const &subSpan);
Ref(RichFormat const &richFormat);
Ref(RichFormat const &richFormat, Rangei const &subSpan);
Ref subRef(Rangei const &subSpan) const;
/// Returns the original referred RichFormat instance.
RichFormat const &format() const;
int rangeCount() const;
Rangei range(int index) const;
Rangei rangeIndices() const { return _indices; }
private:
void updateIndices();
RichFormat const &_ref;
Rangei _span;
Rangei _indices; ///< Applicable indices in the referred format's ranges list.
};
public:
RichFormat();
RichFormat(IStyle const &style);
RichFormat(RichFormat const &other);
RichFormat &operator = (RichFormat const &other);
void clear();
bool hasStyle() const;
void setStyle(IStyle const &style);
IStyle const &style() const;
/**
* Constructs a RichFormat that specifies no formatting instructions.
*
* @param plainText Plain text.
*
* @return RichFormat instance with a single range that uses the
* default formatting.
*/
static RichFormat fromPlainText(String const &plainText);
/**
* Initializes this RichFormat instance with the styles found in the
* provided styled text (using escape sequences).
*
* @param styledText Text with style markup.
*
* @return Corresponding plain text for use with the methods of Font.
*/
String initFromStyledText(String const &styledText);
/**
* Clips this RichFormat so that it covers only the specified range.
* The indices are translated to be relative to the provided range.
*
* @param range Target range for clipping.
*
* @return RichFormat with only those ranges covering @a range.
*/
Ref subRange(Rangei const &range) const;
TabStops const &tabStops() const;
int tabStopXWidth(int stop) const;
/**
* Iterates the rich format ranges of a RichFormat.
*
* @note Iterator::next() must be called at least once after
* constructing the instance to move the iterator onto the first range.
*/
struct LIBGUI_PUBLIC Iterator
{
Ref format;
int index;
public:
Iterator(Ref const &ref);
int size() const;
bool hasNext() const;
void next();
/// Determines if all the style parameters are the same as the default ones.
bool isDefault() const;
Rangei range() const;
float sizeFactor() const;
Weight weight() const;
Style style() const;
int colorIndex() const;
IStyle::Color color() const;
bool markIndent() const;
bool resetIndent() const;
int tabStop() const;
bool isTabless() const; ///< Tabstop < 0.
};
private:
DENG2_PRIVATE(d)
};
typedef RichFormat::Ref RichFormatRef;
public:
Font();
Font(Font const &other);
//Font(NativeFont const &font);
Font(QFont const &font);
//QFont toQFont() const;
/**
* Sets the font family name to be used if the font weight is changed. By default it
* is assumed that the same family name provides all weights.
*
* @param weight Weight for the alternative family.
* @param familyName Name of the alternative family.
*/
//void setAltFamily(RichFormat::Weight weight, String const &familyName);
/**
* Determines the size of the given line of text, i.e., how large an area
* is covered by the glyphs. (0,0) is at the baseline, left edge of the
* line. The rectangle may extend into negative coordinates.
*
* @param textLine Text to measure.
*
* @return Rectangle covered by the text.
*/
Rectanglei measure(String const &textLine) const;
/**
* Determines the size of the given line of rich text, i.e., how large an
* area is covered by the glyphs. (0,0) is at the baseline, left edge of
* the line. The rectangle may extend into negative coordinates.
*
* @param textLine Text to measure.
* @param format Rich formatting for @a textLine.
*
* @return Rectangle covered by the text.
*/
Rectanglei measure(String const &textLine, RichFormatRef const &format) const;
/**
* Returns the advance width of a line of text. This may not be the same as
* the width of the rectangle returned by measure().
*
* @param textLine Text to measure.
*
* @return Width of the line of text (including non-visible parts like
* whitespace).
*/
int advanceWidth(String const &textLine) const;
int advanceWidth(String const &textLine, RichFormatRef const &format) const;
/**
* Rasterizes a line of text onto a 32-bit RGBA image.
*
* @param textLine Text to rasterize.
* @param foreground Text foreground color.
* @param background Background color.
*
* @return Image containing the rasterized text.
*/
QImage rasterize(String const &textLine,
Vector4ub const &foreground = Vector4ub(255, 255, 255, 255),
Vector4ub const &background = Vector4ub(255, 255, 255, 0)) const;
QImage rasterize(String const &textLine,
RichFormatRef const &format,
Vector4ub const &foreground = Vector4ub(255, 255, 255, 255),
Vector4ub const &background = Vector4ub(255, 255, 255, 0)) const;
Rule const &height() const;
Rule const &ascent() const;
Rule const &descent() const;
Rule const &lineSpacing() const;
private:
DENG2_PRIVATE(d)
};
} // namespace de
#endif // LIBGUI_FONT_H