source: trunk/src/gui/painting/qtextureglyphcache.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 11.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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#ifndef QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH
51#define QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH 256
52#endif
53
54QT_BEGIN_NAMESPACE
55
56// #define CACHE_DEBUG
57
58void QTextureGlyphCache::populate(const QTextItemInt &ti,
59 const QVarLengthArray<glyph_t> &glyphs,
60 const QVarLengthArray<QFixedPoint> &)
61{
62#ifdef CACHE_DEBUG
63 printf("Populating with '%s'\n", QString::fromRawData(ti.chars, ti.num_chars).toLatin1().data());
64 qDebug() << " -> current transformation: " << m_transform;
65#endif
66
67 m_current_textitem = &ti;
68 const int margin = glyphMargin();
69
70 QHash<glyph_t, Coord> listItemCoordinates;
71 int rowHeight = 0;
72
73 // check each glyph for its metrics and get the required rowHeight.
74 for (int i=0; i < glyphs.size(); ++i) {
75 const glyph_t glyph = glyphs[i];
76 if (coords.contains(glyph))
77 continue;
78 if (listItemCoordinates.contains(glyph))
79 continue;
80 glyph_metrics_t metrics = ti.fontEngine->boundingBox(glyph, m_transform);
81
82#ifdef CACHE_DEBUG
83 printf("'%c' (%4x): w=%.2f, h=%.2f, xoff=%.2f, yoff=%.2f, x=%.2f, y=%.2f, ti.ascent=%.2f, ti.descent=%.2f\n",
84 ti.chars[i].toLatin1(),
85 glyph,
86 metrics.width.toReal(),
87 metrics.height.toReal(),
88 metrics.xoff.toReal(),
89 metrics.yoff.toReal(),
90 metrics.x.toReal(),
91 metrics.y.toReal(),
92 ti.ascent.toReal(),
93 ti.descent.toReal());
94#endif
95 int glyph_width = metrics.width.ceil().toInt();
96 int glyph_height = metrics.height.ceil().toInt();
97 if (glyph_height == 0 || glyph_width == 0)
98 continue;
99 glyph_width += margin * 2 + 4;
100 glyph_height += margin * 2 + 4;
101 // align to 8-bit boundary
102 if (m_type == QFontEngineGlyphCache::Raster_Mono)
103 glyph_width = (glyph_width+7)&~7;
104
105 Coord c = { 0, 0, // will be filled in later
106 glyph_width,
107 glyph_height, // texture coords
108 metrics.x.round().truncate(),
109 -metrics.y.truncate() }; // baseline for horizontal scripts
110
111 listItemCoordinates.insert(glyph, c);
112 rowHeight = qMax(rowHeight, glyph_height);
113 }
114 if (listItemCoordinates.isEmpty())
115 return;
116
117 rowHeight += margin * 2;
118 if (isNull())
119 createCache(QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH, rowHeight);
120
121 // now actually use the coords and paint the wanted glyps into cache.
122 QHash<glyph_t, Coord>::iterator iter = listItemCoordinates.begin();
123 while (iter != listItemCoordinates.end()) {
124 Coord c = iter.value();
125
126 if (m_cx + c.w > m_w) {
127 // no room on the current line, start new glyph strip
128 m_cx = 0;
129 m_cy = m_h;
130 }
131 if (m_cy + c.h > m_h) {
132 int new_height;
133 if (m_cx == 0) { // add a whole row
134 new_height = m_h + rowHeight;
135 m_cy = m_h;
136 } else { // just extend row
137 new_height = m_cy + rowHeight;
138 }
139 // if no room in the current texture - realloc a larger texture
140 resizeTextureData(m_w, new_height);
141 m_h = new_height;
142 }
143
144 c.x = m_cx;
145 c.y = m_cy;
146
147 fillTexture(c, iter.key());
148 coords.insert(iter.key(), c);
149
150 if (m_cx + c.w > m_w) {
151 m_cx = 0;
152 m_cy += rowHeight;
153 } else {
154 // for the Mono case, glyph_width is 8-bit aligned,
155 // and therefore so will m_cx
156 m_cx += c.w;
157 }
158 ++iter;
159 }
160
161
162}
163
164QImage QTextureGlyphCache::textureMapForGlyph(glyph_t g) const
165{
166#if defined(Q_WS_X11)
167 if (m_transform.type() > QTransform::TxTranslate) {
168 QFontEngineFT::GlyphFormat format = QFontEngineFT::Format_None;
169 QImage::Format imageFormat = QImage::Format_Invalid;
170 switch (m_type) {
171 case Raster_RGBMask:
172 format = QFontEngineFT::Format_A32;
173 imageFormat = QImage::Format_RGB32;
174 break;
175 case Raster_A8:
176 format = QFontEngineFT::Format_A8;
177 imageFormat = QImage::Format_Indexed8;
178 break;
179 case Raster_Mono:
180 format = QFontEngineFT::Format_Mono;
181 imageFormat = QImage::Format_Mono;
182 break;
183 };
184
185 QFontEngineFT *ft = static_cast<QFontEngineFT*> (m_current_textitem->fontEngine);
186 QFontEngineFT::QGlyphSet *gset = ft->loadTransformedGlyphSet(m_transform);
187
188 if (gset && ft->loadGlyphs(gset, &g, 1, format)) {
189 QFontEngineFT::Glyph *glyph = gset->glyph_data.value(g);
190 const int bytesPerLine = (format == QFontEngineFT::Format_Mono ? ((glyph->width + 31) & ~31) >> 3
191 : (glyph->width + 3) & ~3);
192 return QImage(glyph->data, glyph->width, glyph->height, bytesPerLine, imageFormat);
193 }
194 } else
195#endif
196 if (m_type == QFontEngineGlyphCache::Raster_RGBMask)
197 return m_current_textitem->fontEngine->alphaRGBMapForGlyph(g, glyphMargin(), m_transform);
198 else
199 return m_current_textitem->fontEngine->alphaMapForGlyph(g, m_transform);
200
201 return QImage();
202}
203
204/************************************************************************
205 * QImageTextureGlyphCache
206 */
207
208void QImageTextureGlyphCache::resizeTextureData(int width, int height)
209{
210 m_image = m_image.copy(0, 0, width, height);
211}
212
213void QImageTextureGlyphCache::createTextureData(int width, int height)
214{
215 switch (m_type) {
216 case QFontEngineGlyphCache::Raster_Mono:
217 m_image = QImage(width, height, QImage::Format_Mono);
218 break;
219 case QFontEngineGlyphCache::Raster_A8: {
220 m_image = QImage(width, height, QImage::Format_Indexed8);
221 m_image.fill(0);
222 QVector<QRgb> colors(256);
223 QRgb *it = colors.data();
224 for (int i=0; i<256; ++i, ++it)
225 *it = 0xff000000 | i | (i<<8) | (i<<16);
226 m_image.setColorTable(colors);
227 break; }
228 case QFontEngineGlyphCache::Raster_RGBMask:
229 m_image = QImage(width, height, QImage::Format_RGB32);
230 break;
231 }
232}
233
234int QImageTextureGlyphCache::glyphMargin() const
235{
236#if defined(Q_WS_MAC) && defined(QT_MAC_USE_COCOA)
237 return 0;
238#else
239 return m_type == QFontEngineGlyphCache::Raster_RGBMask ? 2 : 0;
240#endif
241}
242
243void QImageTextureGlyphCache::fillTexture(const Coord &c, glyph_t g)
244{
245 QImage mask = textureMapForGlyph(g);
246
247#ifdef CACHE_DEBUG
248 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());
249 if (mask.width() > c.w || mask.height() > c.h) {
250 printf(" ERROR; mask is bigger than reserved space! %dx%d instead of %dx%d\n", mask.width(), mask.height(), c.w,c.h);
251 return;
252 }
253#endif
254
255 if (m_type == QFontEngineGlyphCache::Raster_RGBMask) {
256 QPainter p(&m_image);
257 p.setCompositionMode(QPainter::CompositionMode_Source);
258 p.fillRect(c.x, c.y, c.w, c.h, QColor(0,0,0,0)); // TODO optimize this
259 p.drawImage(c.x, c.y, mask);
260 p.end();
261 } else if (m_type == QFontEngineGlyphCache::Raster_Mono) {
262 if (mask.depth() > 1) {
263 // TODO optimize this
264 mask = mask.alphaChannel();
265 mask.invertPixels();
266 mask = mask.convertToFormat(QImage::Format_Mono);
267 }
268
269 int mw = qMin(mask.width(), c.w);
270 int mh = qMin(mask.height(), c.h);
271 uchar *d = m_image.bits();
272 int dbpl = m_image.bytesPerLine();
273
274 for (int y = 0; y < c.h; ++y) {
275 uchar *dest = d + (c.y + y) *dbpl + c.x/8;
276
277 if (y < mh) {
278 uchar *src = mask.scanLine(y);
279 for (int x = 0; x < c.w/8; ++x) {
280 if (x < (mw+7)/8)
281 dest[x] = src[x];
282 else
283 dest[x] = 0;
284 }
285 } else {
286 for (int x = 0; x < c.w/8; ++x)
287 dest[x] = 0;
288 }
289 }
290 } else { // A8
291 int mw = qMin(mask.width(), c.w);
292 int mh = qMin(mask.height(), c.h);
293 uchar *d = m_image.bits();
294 int dbpl = m_image.bytesPerLine();
295
296 if (mask.depth() == 1) {
297 for (int y = 0; y < c.h; ++y) {
298 uchar *dest = d + (c.y + y) *dbpl + c.x;
299 if (y < mh) {
300 uchar *src = (uchar *) mask.scanLine(y);
301 for (int x = 0; x < c.w; ++x) {
302 if (x < mw)
303 dest[x] = (src[x >> 3] & (1 << (7 - (x & 7)))) > 0 ? 255 : 0;
304 }
305 }
306 }
307 } else if (mask.depth() == 8) {
308 for (int y = 0; y < c.h; ++y) {
309 uchar *dest = d + (c.y + y) *dbpl + c.x;
310 if (y < mh) {
311 uchar *src = (uchar *) mask.scanLine(y);
312 for (int x = 0; x < c.w; ++x) {
313 if (x < mw)
314 dest[x] = src[x];
315 }
316 }
317 }
318 }
319 }
320
321#ifdef CACHE_DEBUG
322// QPainter p(&m_image);
323// p.drawLine(
324 QPoint base(c.x + glyphMargin(), c.y + glyphMargin() + c.baseLineY-1);
325 if (m_image.rect().contains(base))
326 m_image.setPixel(base, 255);
327 m_image.save(QString::fromLatin1("cache-%1-%2-%3.png")
328 .arg(m_current_textitem->font().family())
329 .arg(m_current_textitem->font().pointSize())
330 .arg(m_transform.type()));
331#endif
332}
333
334QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.