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

Last change on this file since 262 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 11.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
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 [email protected].
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
50QT_BEGIN_NAMESPACE
51
52// #define CACHE_DEBUG
53
54void 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
163void QImageTextureGlyphCache::resizeTextureData(int width, int height)
164{
165 m_image = m_image.copy(0, 0, width, height);
166}
167
168void 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
189int 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