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