/
fontlinewrapping.h
152 lines (128 loc) · 4.38 KB
/
fontlinewrapping.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
/** @file fontlinewrapping.h Font line wrapping.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBAPPFW_FONTLINEWRAPPING_H
#define LIBAPPFW_FONTLINEWRAPPING_H
#include "../libappfw.h"
#include <de/String>
#include <de/Font>
#include <de/Image>
#include <de/Lockable>
#include <de/shell/ILineWrapping>
namespace de {
/**
* Line wrapper that uses a particular font and calculates widths in pixels.
* Height is still in lines, though. The wrapper cannot be used before the
* font has been defined.
*
* Supports indentation of lines, as marked in the RichFormat.
*
* @par Thread-safety
*
* FontLineWrapping locks itself automatically when any of its methods are
* being executed. Instances can be used from multiple threads.
*
* @ingroup appfw
*/
class LIBAPPFW_PUBLIC FontLineWrapping : public Lockable, public shell::ILineWrapping
{
public:
FontLineWrapping();
void setFont(Font const &font);
Font const &font() const;
bool hasFont() const;
/**
* Clears the wrapping completely. The text is also cleared.
*/
void clear();
/**
* Resets the existing wrapping (isEmpty() will return @c true) but does not
* clear the text.
*/
void reset();
void wrapTextToWidth(String const &text, int maxWidth);
void wrapTextToWidth(String const &text, Font::RichFormat const &format, int maxWidth);
void rasterizeLines(Rangei const &lineRange);
void clearRasterizedLines() const;
/**
* Cancels the ongoing wrapping operation. This is useful when doing long wrapping
* operations in the background. An exception is thrown from the ongoing
* wrapTextToWidth() call.
*/
void cancel();
bool isEmpty() const;
String const &text() const;
shell::WrappedLine line(int index) const;
int width() const;
int height() const;
int rangeWidth(Rangei const &range) const;
int indexAtWidth(Rangei const &range, int width) const;
/**
* Calculates the total height of the wrapped lined in pixels. If there are
* multiple lines, takes into consideration the appropriate leading between
* lines.
*/
int totalHeightInPixels() const;
/**
* Returns the maximum width given to wrapTextToWidth().
*/
int maximumWidth() const;
/**
* Determines the coordinates of a character's top left corner in pixels.
*
* @param line Wrapped line number.
* @param charIndex Index of the character on the wrapped line. Each
* wrapped line is indexed starting from zero.
*
* @return XY coordinates of the character.
*/
Vector2i charTopLeftInPixels(int line, int charIndex);
struct LineInfo
{
struct Segment {
Rangei range;
int tabStop;
int width;
Segment(Rangei const &r, int tab = 0) : range(r), tabStop(tab), width(0)
{}
};
typedef QVector<Segment> Segments;
Segments segs;
int indent; ///< Left indentation to apply to the entire line.
public:
int highestTabStop() const;
};
/**
* Returns the left indentation for a wrapped line.
*
* @param index Wrapped line number.
*/
LineInfo const &lineInfo(int index) const;
/**
* Returns a rasterized version of a segment.
* Before calling this, segments must first be rasterized by calling
* rasterizeAllSegments().
*
* @param line Line index.
* @param segment Segment on the line.
*/
Image rasterizedSegment(int line, int segment) const;
private:
DENG2_PRIVATE(d)
};
} // namespace de
#endif // LIBAPPFW_FONTLINEWRAPPING_H