-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathFontSupport.h
More file actions
369 lines (296 loc) · 9.79 KB
/
FontSupport.h
File metadata and controls
369 lines (296 loc) · 9.79 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2011 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef FONT_SUPPORT_H_
#define FONT_SUPPORT_H_
#include <list>
#include "Wt/WDllDefs.h"
#ifdef WT_TARGET_JAVA
#define WT_FONTSUPPORT_SIMPLE
#endif // WT_TARGET_JAVA
#ifdef WT_FONTSUPPORT_SIMPLE
#include <string>
#endif // WT_FONTSUPPORT_SIMPLE
#ifdef WT_FONTSUPPORT_PANGO
#include <pango/pango.h>
#endif // WT_FONTSUPPORT_PANGO
#ifdef WT_FONTSUPPORT_DIRECTWRITE
#include <dwrite.h>
#endif // WT_FONTSUPPORT_DIRECTWRITE
#include "Wt/WFont.h"
#include "Wt/WPaintDevice.h"
namespace Wt {
/*
* A private utility class that provides improved font matching.
*
* This utility class uses libpango for font selection and layout if
* available, otherwise uses a simple font matching which choses a
* single font and does not take into account its supported glyphs.
*/
class FontSupport
{
public:
#ifndef WT_TARGET_JAVA
class Bitmap {
public:
Bitmap(int width, int height);
~Bitmap();
int width() const { return width_; }
int height() const { return height_; }
int pitch() const { return pitch_; }
unsigned char value(int x, int y) const { return buffer_[y * pitch_ + x]; }
unsigned char *buffer() { return buffer_; }
private:
int width_, height_, pitch_;
unsigned char *buffer_;
};
#endif
/* For limiting font formats in FontMatch constructor
*/
enum EnabledFontFormats { AnyFont, // Any available font format
TrueTypeOnly // Only .ttf or .ttc fonts
};
class FontMatch {
public:
#ifdef WT_FONTSUPPORT_SIMPLE
FontMatch();
FontMatch(const std::string& fileName, double quality);
bool matched() const { return quality_ > 0; }
std::string fileName() const { return file_; }
void setFileName(const std::string &file) { file_ = file; }
double quality() const { return quality_; }
void setQuality(double quality) { quality_ = quality; }
#endif // WT_FONTSUPPORT_SIMPLE
#ifdef WT_FONTSUPPORT_PANGO
FontMatch(PangoFont *font, PangoFontDescription *desc);
bool matched() const { return true; }
std::string fileName() const;
PangoFont *pangoFont() const { return font_; }
PangoFontDescription *pangoFontDescription() const { return desc_; }
#endif // WT_FONTSUPPORT_PANGO
#ifdef WT_FONTSUPPORT_DIRECTWRITE
FontMatch();
FontMatch(IDWriteTextFormat *textFormat,
IDWriteFontFamily *fontFamily,
IDWriteFont *font,
std::string fontFamilyFileName);
~FontMatch();
FontMatch(const FontMatch &other);
FontMatch &operator=(const FontMatch &other);
#ifdef WT_CXX11
FontMatch(FontMatch &&other);
FontMatch &operator=(FontMatch &&other);
#endif // WT_CXX11
bool matched() const;
std::string fileName() const;
std::wstring fontFamilyName() const;
// For use in WRasterImage-d2d1.C
IDWriteTextFormat *textFormat() { return textFormat_; }
IDWriteFontFamily *fontFamily() { return fontFamily_; }
IDWriteFont *font() { return font_; }
#endif // WT_FONTSUPPORT_DIRECTWRITE
private:
#ifdef WT_FONTSUPPORT_SIMPLE
std::string file_;
double quality_;
#endif // WT_FONTSUPPORT_SIMPLE
#ifdef WT_FONTSUPPORT_PANGO
mutable PangoFont *font_;
PangoFontDescription *desc_;
#endif // WT_FONTSUPPORT_PANGO
#ifdef WT_FONTSUPPORT_DIRECTWRITE
IDWriteTextFormat *textFormat_;
IDWriteFontFamily *fontFamily_;
IDWriteFont *font_;
mutable std::string fontFamilyFileName_;
#endif // WT_FONTSUPPORT_DIRECTWRITE
};
FontSupport(WPaintDevice *device, EnabledFontFormats enabledFontFormats=AnyFont);
~FontSupport();
void setDevice(WPaintDevice *device);
/*
* Returns the best matching true type font.
*/
FontMatch matchFont(const WFont& f) const;
/*
* Returns font metrics
*/
WFontMetrics fontMetrics(const WFont& f);
/*
* Measures the text, taking into account actual font choices.
*
* When there is a device_, the actual measurements are delegated to the
* device. Otherwise, pango is used for measurements.
*/
WTextItem measureText(const WFont& f, const WString& text, double maxWidth,
bool wordWrap);
/*
* Draws the text, using pango for font choices, but delegating the actual
* drawing to the device
*
* Precondition: device_ != nullptr
*/
void drawText(const WFont& f,
const WRectF& rect,
WFlags<AlignmentFlag> alignmentFlags, const WString& text);
/*
* Returns true while in measureText() or drawText()
*/
bool busy() const;
/*
* Returns the currently matched font path (while busy()).
*/
std::string drawingFontPath() const;
/*
* Returns whether the implementation can actually render (which is only
* the case for pango
*/
bool canRender() const;
#ifndef WT_TARGET_JAVA
/*
* Draws the text, using pango and libfreetype to do the actual rendering
*
* Precondition: device_ == nullptr
*/
void drawText(const WFont& f,
const WRectF& rect,
const WTransform& transform,
Bitmap& bitmap,
WFlags<AlignmentFlag> alignmentFlags, const WString& text);
#endif
/*
* If libpango support is available, this is a no-op.
*/
void addFontCollection(const std::string& directory, bool recursive = true);
#ifdef WT_FONTSUPPORT_DIRECTWRITE
/*
* Direct access to IDWriteFactory for WRasterImage-d2d1
*/
IDWriteFactory *writeFactory() { return writeFactory_; }
#endif // WT_FONTSUPPORT_DIRECTWRITE
private:
WPaintDevice *device_;
static const std::size_t FONT_CACHE_MAX_SIZE = 10;
#ifdef WT_FONTSUPPORT_SIMPLE
struct FontCollection {
std::string directory;
bool recursive;
};
std::vector<FontCollection> fontCollections_;
struct Matched {
WFont font;
FontMatch match;
Matched() : font(), match() { }
};
typedef std::list<Matched> MatchCache;
// The cache that tracks the used fonts, and limits the size of the used
// fonts to a maximum of 10 fonts.
// This cache keeps the fonts alive for as long as the cache lives, or
// when a new match would exceed the current cache's limit.
mutable MatchCache lruCache_;
const WFont *font_;
FontMatch matchFont(const WFont& font, const std::string& directory,
bool recursive) const;
void matchFont(const WFont& font,
const std::vector<std::string>& fontNames,
const std::string& path,
bool recursive,
FontMatch& match) const;
void matchFont(const WFont& font,
const std::vector<std::string>& fontNames,
const std::string& path,
FontMatch& match) const;
#endif // WT_FONTSUPPORT_SIMPLE
#ifdef WT_FONTSUPPORT_PANGO
struct PangoContextDeleter {
void operator()(PangoContext* context)
{
g_object_unref(context);
}
};
struct PangoFontDeleter {
void operator()(PangoFont* font)
{
g_object_unref(font);
}
};
struct PangoFontDescriptionDeleter {
void operator()(PangoFontDescription* fontDescription)
{
pango_font_description_free(fontDescription);
}
};
// Context for Pango for each font support call
std::unique_ptr<PangoContext, PangoContextDeleter> context_;
// Raw pointer to currently used font.
PangoFont* currentFont_;
EnabledFontFormats enabledFontFormats_;
/* Singular instance of a match, containing the Pango font and its
* description. This struct manages the lifetime of the font and
* description.
*/
struct Matched {
WFont font;
std::unique_ptr<PangoFont, PangoFontDeleter> match;
std::unique_ptr<PangoFontDescription, PangoFontDescriptionDeleter> desc;
Matched() : font(), match(nullptr), desc(nullptr) { }
Matched(const WFont& f, std::unique_ptr<PangoFont, PangoFontDeleter> m, std::unique_ptr<PangoFontDescription, PangoFontDescriptionDeleter> d) : font(f), match(std::move(m)), desc(std::move(d)) { }
};
typedef std::list<Matched> MatchCache;
// The cache that tracks the used fonts, and limits the size of the used
// fonts to a maximum of 10 fonts.
// This cache keeps the fonts and descriptions alive for as long as it
// lives, or when a new match would exceed the current cache's limit.
mutable MatchCache lruCache_;
PangoFontDescription *createFontDescription(const WFont& f) const;
static std::string fontPath(PangoFont *font);
GList *layoutText(const WFont& font, const std::string& utf8,
std::vector<PangoGlyphString *>& glyphs, int& width);
friend class FontMatch;
#endif // WT_FONTSUPPORT_PANGO
#ifdef WT_FONTSUPPORT_DIRECTWRITE
EnabledFontFormats enabledFontFormats_;
IDWriteFactory *writeFactory_;
const WFont *font_;
std::string drawingFontPath_;
struct Matched {
WFont font;
FontMatch match;
Matched()
: font(), match()
{ }
Matched(const WFont &f,
FontMatch &&m)
: font(f), match(m)
{ }
};
typedef std::list<Matched> MatchCache;
// The cache that tracks the used fonts, and limits the size of the used
// fonts to a maximum of 10 fonts.
// This cache keeps the fonts alive for as long as the cache lives, or
// when a new match would exceed the current cache's limit.
mutable MatchCache lruCache_;
struct TextFragment {
TextFragment(std::string fontPath,
std::size_t start,
std::size_t length,
double width)
: fontPath(fontPath),
start(start),
length(length),
width(width)
{ }
std::string fontPath;
std::size_t start;
std::size_t length;
double width;
};
class TextFragmentRenderer;
void layoutText(const WFont &f, std::vector<TextFragment> &v, const std::wstring &s, double &width, double &nextWidth, double maxWidth, bool wordWrap);
#endif // WT_FONTSUPPORT_DIRECTWRITE
};
}
#endif // FONT_SUPPORT_H_