Repository navigation
Expand file tree
/
Copy pathDisplayString.h
More file actions
291 lines (256 loc) · 7.4 KB
/
Copy pathDisplayString.h
File metadata and controls
291 lines (256 loc) · 7.4 KB
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
#ifndef _GDISPLAY_STRING_H_
#define _GDISPLAY_STRING_H_
#include "lgi/common/Font.h"
#if defined(LGI_SDL)
#elif defined(LINUX)
namespace Pango
{
#include "pango/pango.h"
#include "pango/pangocairo.h"
}
#endif
#if !defined(_MSC_VER) || defined(__GTK_H__)
#define LGI_DSP_STR_CACHE 1
#endif
/// \brief Cache for text measuring, glyph substitution and painting
///
/// To paint text onto the screen several stages need to be implemented to
/// properly support unicode on multiple platforms. This class addresses all
/// of those needs and then allows you to cache the results to reduce text
/// related workload.
///
/// The first stage is converting text into the native format for the
/// OS's API. This usually involved converting the text to wide characters for
/// Linux or Windows, or Utf-8 for Haiku. Then the text is converted into runs of
/// characters that can be rendered in the same font. If glyph substitution is
/// required to render the characters a separate run is used with a different
/// font ID. Finally you can measure or paint these runs of text. Also tab characters
/// are expanded to the current tab size setting.
class LgiClass LDisplayString
{
protected:
LSurface *pDC = NULL;
LFont *Font = NULL;
uint32_t x = 0, y = 0;
int xf = 0, yf = 0;
int DrawOffsetF = 0;
// Flags
uint8_t LaidOut : 1;
uint8_t AppendDots : 1;
uint8_t VisibleTab : 1;
/* String data:
Str Wide
Windows: utf-16 utf-16
MacCocoa: utf-16 utf-32
Gtk3: utf-8 utf-32
Haiku: utf-8 utf-32
*/
OsChar *Str = NULL;
ssize_t StrWords = 0;
#if LGI_DSP_STR_CACHE
wchar_t *Wide = NULL;
ssize_t WideWords = 0;
#endif
#if defined(LGI_SDL)
LAutoPtr<LMemDC> Img;
#elif defined(__GTK_H__)
struct LDisplayStringPriv *d;
friend struct LDisplayStringPriv;
#elif defined(MAC)
CTLineRef Hnd = nullptr;
CFAttributedStringRef AttrStr = nullptr;
void CreateAttrStr();
#elif defined(WINNATIVE) || defined(HAIKU)
class CharInfo
{
public:
OsChar *Str = NULL;
int Len = 0;
int X = 0;
uint8_t FontId = 0;
int8_t SizeDelta = 0;
uint8_t Missing = false;
uint32_t First()
{
auto s = (const uint16*)Str;
ssize_t l = Len * sizeof(*Str);
return LgiUtf16To32(s, l);
}
};
LArray<CharInfo> Info;
#endif
void Layout(bool Debug = false);
void DrawWhiteSpace(LSurface *pDC, char Ch, LRect &r);
public:
// Turn on debug logging...
bool _debug = false;
/// Constructor
LDisplayString
(
/// The base font. Must not be destroyed during the lifetime of this object.
LFont *f,
/// Utf-8 input string
const char *s,
/// Number of bytes in the input string or -1 for NULL terminated.
ssize_t l = -1,
LSurface *pdc = 0
);
/// Constructor
LDisplayString
(
/// The base font. Must not be destroyed during the lifetime of this object.
LFont *f,
/// A wide character input string
const char16 *s,
/// The number of characters in the input string (NOT the number of bytes) or -1 for NULL terminated
ssize_t l = -1,
LSurface *pdc = 0
);
#ifdef _MSC_VER
/// Constructor
LDisplayString
(
/// The base font. Must not be destroyed during the lifetime of this object.
LFont *f,
/// A wide character input string
const uint32_t *s,
/// The number of characters in the input string (NOT the number of bytes) or -1 for NULL terminated
ssize_t l = -1,
LSurface *pdc = 0
);
#endif
virtual ~LDisplayString();
LDisplayString &operator=(const LDisplayString &s)
{
LAssert(0);
return *this;
}
/// \returns the draw offset used to calculate tab spacing (in Px)
int GetDrawOffset();
/// Sets the draw offset used to calculate tab spacing (in Px)
void SetDrawOffset(int Px);
/// Returns the ShowVisibleTab setting.
/// Treats Unicode-2192 (left arrow) as a tab char
bool ShowVisibleTab();
/// Sets the ShowVisibleTab setting.
/// Treats Unicode-2192 (left arrow) as a tab char
void ShowVisibleTab(bool i);
/// \returns the font used to create the layout
LFont *GetFont() { return Font; };
/// Fits string into 'width' pixels, truncating with '...' if it's not going to fit
void TruncateWithDots
(
/// The number of pixels the string has to fit
int Width
);
/// Returns true if the string is trucated
bool IsTruncated();
/// Returns the words (not chars) in the OsChar string
ssize_t Length();
/// Returns the pointer to the native string
operator const char16*()
{
#if LGI_DSP_STR_CACHE
return Wide;
#else
return Str;
#endif
}
/// \returns the number of words in the wide string
ssize_t WideLength()
{
#if LGI_DSP_STR_CACHE
return WideWords;
#else
return StrWords;
#endif
}
// API that use full pixel sizes:
/// Returns the width of the whole string
int X();
/// Returns the height of the whole string
int Y();
/// Returns the width and height of the whole string
LPoint Size();
/// Returns the number of characters that fit in 'x' pixels.
ssize_t CharAt(int x, LPxToIndexType Type = LgiTruncate);
/// Draws the string onto a device surface
///
/// Windows note: drawing text on a 32bit memory context will
/// not set the alpha correctly (it'll be 0). You can call
/// LSurface::MakeOpaque afterwards to fix that.
void Draw
(
/// The output device
LSurface *pDC,
/// The x coordinate of the top left corner of the output box
int x,
/// The y coordinate of the top left corner of the output box
int y,
/// An optional clipping rectangle. If the font is not transparent this rectangle will be filled with the background colour.
LRect *r = NULL,
/// Turn on debug logging
bool Debug = false
);
/// Draws the string onto a device surface
void DrawCenter
(
/// The output device
LSurface *pDC,
/// An rectangle to center in. If the font is not transparent this rectangle will be filled with the background colour.
LRect *r
)
{
if (r)
Draw(pDC, r->x1 + ((r->X() - X()) >> 1), r->y1 + ((r->Y() - Y()) >> 1), r);
}
// API that uses fractional pixel sizes.
enum
{
#if defined LGI_SDL
FShift = 6,
FScale = 1 << FShift,
#elif defined __GTK_H__
/// This is the value for 1 pixel.
FScale = PANGO_SCALE,
/// This is bitwise shift to convert between integer and fractional
FShift = 10
#elif defined MAC
/// This is the value for 1 pixel.
FScale = 0x10000,
/// This is bitwise shift to convert between integer and fractional
FShift = 16
#else
/// This is the value for 1 pixel.
FScale = 1,
/// This is bitwise shift to convert between integer and fractional
FShift = 0
#endif
};
/// \returns the draw offset used to calculate tab spacing (in fractional units)
int GetDrawOffsetF();
/// Sets the draw offset used to calculate tab spacing (in fractional units)
void SetDrawOffsetF(int Fpx);
/// \returns the fractional width of the string
int FX();
/// \returns the fractional height of the string
int FY();
/// \returns both the fractional width and height of the string
LPoint FSize();
/// Draws the string using fractional co-ordinates.
void FDraw
(
/// The output device
LSurface *pDC,
/// The fractional x coordinate of the top left corner of the output box
int fx,
/// The fractional y coordinate of the top left corner of the output box
int fy,
/// [Optional] fractional clipping rectangle. If the font is not transparent
/// this rectangle will be filled with the background colour.
LRect *frc = NULL,
/// [Optional] print debug info
bool Debug = false
);
};
#endif