-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathFontSupport.h
More file actions
192 lines (148 loc) · 4.24 KB
/
FontSupport.h
File metadata and controls
192 lines (148 loc) · 4.24 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
// This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2011 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef FONT_SUPPORT_H_
#define FONT_SUPPORT_H_
#ifndef HAVE_PANGO
#include <boost/filesystem/path.hpp>
#else
#include <pango/pango.h>
#endif // HAVE_PANGO
#include <Wt/WPaintDevice>
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:
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_;
};
class FontMatch {
public:
#ifdef HAVE_PANGO
FontMatch(PangoFont *font);
bool matched() const { return true; }
std::string fileName() const;
PangoFont *pangoFont() const { return font_; }
#else
FontMatch();
FontMatch(const std::string& fileName, double quality);
bool matched() const { return quality_ > 0; }
std::string fileName() const { return file_; }
double quality() const { return quality_; }
#endif
private:
#ifdef HAVE_PANGO
mutable PangoFont *font_;
#else
std::string file_;
double quality_;
#endif
};
FontSupport(WPaintDevice *device);
~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_ != 0
*/
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;
/*
* Draws the text, using pango and libfreetype to do the actual rendering
*
* Precondition: device_ == 0
*/
void drawText(const WFont& f,
const WRectF& rect,
const WTransform& transform,
Bitmap& bitmap,
WFlags<AlignmentFlag> alignmentFlags, const WString& text);
/*
* If libpango support is available, this is a no-op.
*/
void addFontCollection(const std::string& directory, bool recursive = true);
private:
WPaintDevice *device_;
#ifdef HAVE_PANGO
PangoContext *context_;
PangoFont *currentFont_;
mutable PangoFont *matchFont_;
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;
#else
struct FontCollection {
std::string directory;
bool recursive;
};
std::vector<FontCollection> fontCollections_;
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 boost::filesystem::path& path,
bool recursive,
FontMatch& match) const;
void matchFont(const WFont& font,
const std::vector<std::string>& fontNames,
const boost::filesystem::path& path,
FontMatch& match) const;
#endif // HAVE_PANGO
};
}
#endif // FONT_SUPPORT_H_