1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <qmath.h>
|
---|
43 |
|
---|
44 | #include "qtextureglyphcache_p.h"
|
---|
45 |
|
---|
46 | #include "private/qnumeric_p.h"
|
---|
47 | #include "private/qnativeimage_p.h"
|
---|
48 | #include "private/qfontengine_ft_p.h"
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | // #define CACHE_DEBUG
|
---|
53 |
|
---|
54 | // returns the highest number closest to v, which is a power of 2
|
---|
55 | // NB! assumes 32 bit ints
|
---|
56 | static inline int qt_next_power_of_two(int v)
|
---|
57 | {
|
---|
58 | v--;
|
---|
59 | v |= v >> 1;
|
---|
60 | v |= v >> 2;
|
---|
61 | v |= v >> 4;
|
---|
62 | v |= v >> 8;
|
---|
63 | v |= v >> 16;
|
---|
64 | ++v;
|
---|
65 | return v;
|
---|
66 | }
|
---|
67 |
|
---|
68 | bool QTextureGlyphCache::populate(QFontEngine *fontEngine, int numGlyphs, const glyph_t *glyphs,
|
---|
69 | const QFixedPoint *)
|
---|
70 | {
|
---|
71 | #ifdef CACHE_DEBUG
|
---|
72 | printf("Populating with %d glyphs\n", numGlyphs);
|
---|
73 | qDebug() << " -> current transformation: " << m_transform;
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | m_current_fontengine = fontEngine;
|
---|
77 | const int margin = glyphMargin();
|
---|
78 | const int paddingDoubled = glyphPadding() * 2;
|
---|
79 |
|
---|
80 | QHash<glyph_t, Coord> listItemCoordinates;
|
---|
81 | int rowHeight = 0;
|
---|
82 |
|
---|
83 | // check each glyph for its metrics and get the required rowHeight.
|
---|
84 | for (int i=0; i < numGlyphs; ++i) {
|
---|
85 | const glyph_t glyph = glyphs[i];
|
---|
86 | if (coords.contains(glyph))
|
---|
87 | continue;
|
---|
88 | if (listItemCoordinates.contains(glyph))
|
---|
89 | continue;
|
---|
90 | glyph_metrics_t metrics = fontEngine->boundingBox(glyph, m_transform);
|
---|
91 |
|
---|
92 | #ifdef CACHE_DEBUG
|
---|
93 | printf("(%4x): w=%.2f, h=%.2f, xoff=%.2f, yoff=%.2f, x=%.2f, y=%.2f\n",
|
---|
94 | glyph,
|
---|
95 | metrics.width.toReal(),
|
---|
96 | metrics.height.toReal(),
|
---|
97 | metrics.xoff.toReal(),
|
---|
98 | metrics.yoff.toReal(),
|
---|
99 | metrics.x.toReal(),
|
---|
100 | metrics.y.toReal());
|
---|
101 | #endif
|
---|
102 | int glyph_width = metrics.width.ceil().toInt();
|
---|
103 | int glyph_height = metrics.height.ceil().toInt();
|
---|
104 | if (glyph_height == 0 || glyph_width == 0)
|
---|
105 | continue;
|
---|
106 | glyph_width += margin * 2 + 4;
|
---|
107 | glyph_height += margin * 2 + 4;
|
---|
108 | // align to 8-bit boundary
|
---|
109 | if (m_type == QFontEngineGlyphCache::Raster_Mono)
|
---|
110 | glyph_width = (glyph_width+7)&~7;
|
---|
111 |
|
---|
112 | Coord c = { 0, 0, // will be filled in later
|
---|
113 | glyph_width,
|
---|
114 | glyph_height, // texture coords
|
---|
115 | metrics.x.round().truncate(),
|
---|
116 | -metrics.y.truncate() }; // baseline for horizontal scripts
|
---|
117 |
|
---|
118 | listItemCoordinates.insert(glyph, c);
|
---|
119 | rowHeight = qMax(rowHeight, glyph_height);
|
---|
120 | }
|
---|
121 | if (listItemCoordinates.isEmpty())
|
---|
122 | return true;
|
---|
123 |
|
---|
124 | rowHeight += margin * 2 + paddingDoubled;
|
---|
125 | if (isNull())
|
---|
126 | createCache(QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH, qt_next_power_of_two(rowHeight));
|
---|
127 |
|
---|
128 | // now actually use the coords and paint the wanted glyps into cache.
|
---|
129 | QHash<glyph_t, Coord>::iterator iter = listItemCoordinates.begin();
|
---|
130 | while (iter != listItemCoordinates.end()) {
|
---|
131 | Coord c = iter.value();
|
---|
132 |
|
---|
133 | m_currentRowHeight = qMax(m_currentRowHeight, c.h + margin * 2);
|
---|
134 |
|
---|
135 | if (m_cx + c.w > m_w) {
|
---|
136 | int new_width = m_w*2;
|
---|
137 | while (new_width < m_cx + c.w)
|
---|
138 | new_width *= 2;
|
---|
139 | if (new_width <= maxTextureWidth()) {
|
---|
140 | resizeTextureData(new_width, m_h);
|
---|
141 | m_w = new_width;
|
---|
142 | } else {
|
---|
143 | // no room on the current line, start new glyph strip
|
---|
144 | m_cx = 0;
|
---|
145 | m_cy += m_currentRowHeight + paddingDoubled;
|
---|
146 | m_currentRowHeight = c.h + margin * 2; // New row
|
---|
147 | }
|
---|
148 | }
|
---|
149 | if (m_cy + c.h > m_h) {
|
---|
150 | int new_height = m_h*2;
|
---|
151 | while (new_height < m_cy + c.h)
|
---|
152 | new_height *= 2;
|
---|
153 |
|
---|
154 | if (maxTextureHeight() > 0 && new_height > maxTextureHeight()) {
|
---|
155 | // We can't make a new texture of the required size, so
|
---|
156 | // bail out
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 |
|
---|
160 | // if no room in the current texture - realloc a larger texture
|
---|
161 | resizeTextureData(m_w, new_height);
|
---|
162 | m_h = new_height;
|
---|
163 | }
|
---|
164 |
|
---|
165 | c.x = m_cx;
|
---|
166 | c.y = m_cy;
|
---|
167 |
|
---|
168 | fillTexture(c, iter.key());
|
---|
169 | coords.insert(iter.key(), c);
|
---|
170 |
|
---|
171 | m_cx += c.w + paddingDoubled;
|
---|
172 | ++iter;
|
---|
173 | }
|
---|
174 |
|
---|
175 | return true;
|
---|
176 | }
|
---|
177 |
|
---|
178 | QImage QTextureGlyphCache::textureMapForGlyph(glyph_t g) const
|
---|
179 | {
|
---|
180 | #if defined(Q_WS_X11)
|
---|
181 | if (m_transform.type() > QTransform::TxTranslate) {
|
---|
182 | QFontEngineFT::GlyphFormat format = QFontEngineFT::Format_None;
|
---|
183 | QImage::Format imageFormat = QImage::Format_Invalid;
|
---|
184 | switch (m_type) {
|
---|
185 | case Raster_RGBMask:
|
---|
186 | format = QFontEngineFT::Format_A32;
|
---|
187 | imageFormat = QImage::Format_RGB32;
|
---|
188 | break;
|
---|
189 | case Raster_A8:
|
---|
190 | format = QFontEngineFT::Format_A8;
|
---|
191 | imageFormat = QImage::Format_Indexed8;
|
---|
192 | break;
|
---|
193 | case Raster_Mono:
|
---|
194 | format = QFontEngineFT::Format_Mono;
|
---|
195 | imageFormat = QImage::Format_Mono;
|
---|
196 | break;
|
---|
197 | };
|
---|
198 |
|
---|
199 | QFontEngineFT *ft = static_cast<QFontEngineFT*> (m_current_fontengine);
|
---|
200 | QFontEngineFT::QGlyphSet *gset = ft->loadTransformedGlyphSet(m_transform);
|
---|
201 |
|
---|
202 | if (gset && ft->loadGlyphs(gset, &g, 1, format)) {
|
---|
203 | QFontEngineFT::Glyph *glyph = gset->getGlyph(g);
|
---|
204 | const int bytesPerLine = (format == QFontEngineFT::Format_Mono ? ((glyph->width + 31) & ~31) >> 3
|
---|
205 | : (glyph->width + 3) & ~3);
|
---|
206 | return QImage(glyph->data, glyph->width, glyph->height, bytesPerLine, imageFormat);
|
---|
207 | }
|
---|
208 | } else
|
---|
209 | #endif
|
---|
210 | if (m_type == QFontEngineGlyphCache::Raster_RGBMask)
|
---|
211 | return m_current_fontengine->alphaRGBMapForGlyph(g, glyphMargin(), m_transform);
|
---|
212 | else
|
---|
213 | return m_current_fontengine->alphaMapForGlyph(g, m_transform);
|
---|
214 |
|
---|
215 | return QImage();
|
---|
216 | }
|
---|
217 |
|
---|
218 | /************************************************************************
|
---|
219 | * QImageTextureGlyphCache
|
---|
220 | */
|
---|
221 |
|
---|
222 | void QImageTextureGlyphCache::resizeTextureData(int width, int height)
|
---|
223 | {
|
---|
224 | m_image = m_image.copy(0, 0, width, height);
|
---|
225 | }
|
---|
226 |
|
---|
227 | void QImageTextureGlyphCache::createTextureData(int width, int height)
|
---|
228 | {
|
---|
229 | switch (m_type) {
|
---|
230 | case QFontEngineGlyphCache::Raster_Mono:
|
---|
231 | m_image = QImage(width, height, QImage::Format_Mono);
|
---|
232 | break;
|
---|
233 | case QFontEngineGlyphCache::Raster_A8: {
|
---|
234 | m_image = QImage(width, height, QImage::Format_Indexed8);
|
---|
235 | m_image.fill(0);
|
---|
236 | QVector<QRgb> colors(256);
|
---|
237 | QRgb *it = colors.data();
|
---|
238 | for (int i=0; i<256; ++i, ++it)
|
---|
239 | *it = 0xff000000 | i | (i<<8) | (i<<16);
|
---|
240 | m_image.setColorTable(colors);
|
---|
241 | break; }
|
---|
242 | case QFontEngineGlyphCache::Raster_RGBMask:
|
---|
243 | m_image = QImage(width, height, QImage::Format_RGB32);
|
---|
244 | break;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | int QImageTextureGlyphCache::glyphMargin() const
|
---|
249 | {
|
---|
250 | #if defined(Q_WS_MAC) && defined(QT_MAC_USE_COCOA)
|
---|
251 | return 0;
|
---|
252 | #else
|
---|
253 | return m_type == QFontEngineGlyphCache::Raster_RGBMask ? 2 : 0;
|
---|
254 | #endif
|
---|
255 | }
|
---|
256 |
|
---|
257 | void QImageTextureGlyphCache::fillTexture(const Coord &c, glyph_t g)
|
---|
258 | {
|
---|
259 | QImage mask = textureMapForGlyph(g);
|
---|
260 |
|
---|
261 | #ifdef CACHE_DEBUG
|
---|
262 | printf("fillTexture of %dx%d at %d,%d in the cache of %dx%d\n", c.w, c.h, c.x, c.y, m_image.width(), m_image.height());
|
---|
263 | if (mask.width() > c.w || mask.height() > c.h) {
|
---|
264 | printf(" ERROR; mask is bigger than reserved space! %dx%d instead of %dx%d\n", mask.width(), mask.height(), c.w,c.h);
|
---|
265 | return;
|
---|
266 | }
|
---|
267 | #endif
|
---|
268 |
|
---|
269 | if (m_type == QFontEngineGlyphCache::Raster_RGBMask) {
|
---|
270 | QImage ref(m_image.bits() + (c.x * 4 + c.y * m_image.bytesPerLine()),
|
---|
271 | qMax(mask.width(), c.w), qMax(mask.height(), c.h), m_image.bytesPerLine(),
|
---|
272 | m_image.format());
|
---|
273 | QPainter p(&ref);
|
---|
274 | p.setCompositionMode(QPainter::CompositionMode_Source);
|
---|
275 | p.fillRect(0, 0, c.w, c.h, QColor(0,0,0,0)); // TODO optimize this
|
---|
276 | p.drawImage(0, 0, mask);
|
---|
277 | p.end();
|
---|
278 | } else if (m_type == QFontEngineGlyphCache::Raster_Mono) {
|
---|
279 | if (mask.depth() > 1) {
|
---|
280 | // TODO optimize this
|
---|
281 | mask = mask.alphaChannel();
|
---|
282 | mask.invertPixels();
|
---|
283 | mask = mask.convertToFormat(QImage::Format_Mono);
|
---|
284 | }
|
---|
285 |
|
---|
286 | int mw = qMin(mask.width(), c.w);
|
---|
287 | int mh = qMin(mask.height(), c.h);
|
---|
288 | uchar *d = m_image.bits();
|
---|
289 | int dbpl = m_image.bytesPerLine();
|
---|
290 |
|
---|
291 | for (int y = 0; y < c.h; ++y) {
|
---|
292 | uchar *dest = d + (c.y + y) *dbpl + c.x/8;
|
---|
293 |
|
---|
294 | if (y < mh) {
|
---|
295 | uchar *src = mask.scanLine(y);
|
---|
296 | for (int x = 0; x < c.w/8; ++x) {
|
---|
297 | if (x < (mw+7)/8)
|
---|
298 | dest[x] = src[x];
|
---|
299 | else
|
---|
300 | dest[x] = 0;
|
---|
301 | }
|
---|
302 | } else {
|
---|
303 | for (int x = 0; x < c.w/8; ++x)
|
---|
304 | dest[x] = 0;
|
---|
305 | }
|
---|
306 | }
|
---|
307 | } else { // A8
|
---|
308 | int mw = qMin(mask.width(), c.w);
|
---|
309 | int mh = qMin(mask.height(), c.h);
|
---|
310 | uchar *d = m_image.bits();
|
---|
311 | int dbpl = m_image.bytesPerLine();
|
---|
312 |
|
---|
313 | if (mask.depth() == 1) {
|
---|
314 | for (int y = 0; y < c.h; ++y) {
|
---|
315 | uchar *dest = d + (c.y + y) *dbpl + c.x;
|
---|
316 | if (y < mh) {
|
---|
317 | uchar *src = (uchar *) mask.scanLine(y);
|
---|
318 | for (int x = 0; x < c.w; ++x) {
|
---|
319 | if (x < mw)
|
---|
320 | dest[x] = (src[x >> 3] & (1 << (7 - (x & 7)))) > 0 ? 255 : 0;
|
---|
321 | }
|
---|
322 | }
|
---|
323 | }
|
---|
324 | } else if (mask.depth() == 8) {
|
---|
325 | for (int y = 0; y < c.h; ++y) {
|
---|
326 | uchar *dest = d + (c.y + y) *dbpl + c.x;
|
---|
327 | if (y < mh) {
|
---|
328 | uchar *src = (uchar *) mask.scanLine(y);
|
---|
329 | for (int x = 0; x < c.w; ++x) {
|
---|
330 | if (x < mw)
|
---|
331 | dest[x] = src[x];
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | #ifdef CACHE_DEBUG
|
---|
339 | // QPainter p(&m_image);
|
---|
340 | // p.drawLine(
|
---|
341 | QPoint base(c.x + glyphMargin(), c.y + glyphMargin() + c.baseLineY-1);
|
---|
342 | if (m_image.rect().contains(base))
|
---|
343 | m_image.setPixel(base, 255);
|
---|
344 | m_image.save(QString::fromLatin1("cache-%1.png").arg(int(this)));
|
---|
345 | #endif
|
---|
346 | }
|
---|
347 |
|
---|
348 | QT_END_NAMESPACE
|
---|