1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qsgdistancefieldglyphcache_p.h"
45 #include <private/qsgpathsimplifier_p.h>
46 #include <private/qdeclarativeglobal_p.h>
47 #include <qglshaderprogram.h>
48 #include <private/qglengineshadersource_p.h>
49 #include <private/qsgcontext_p.h>
50 #include <private/qrawfont_p.h>
51 #include <qglfunctions.h>
52 #include <qglyphrun.h>
58 #define QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE 54
59 #define QT_DISTANCEFIELD_DEFAULT_TILESIZE 64
60 #define QT_DISTANCEFIELD_DEFAULT_SCALE 16
61 #define QT_DISTANCEFIELD_DEFAULT_RADIUS 80
62 #define QT_DISTANCEFIELD_HIGHGLYPHCOUNT 2000
64 #define QT_DISTANCEFIELD_BASEFONTSIZE \
65 (m_textureData->doubleGlyphResolution ? QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE * 2 : \
66 QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE)
67 #define QT_DISTANCEFIELD_TILESIZE \
68 (m_textureData->doubleGlyphResolution ? QT_DISTANCEFIELD_DEFAULT_TILESIZE * 2 : \
69 QT_DISTANCEFIELD_DEFAULT_TILESIZE)
70 #define QT_DISTANCEFIELD_SCALE \
71 (m_textureData->doubleGlyphResolution ? QT_DISTANCEFIELD_DEFAULT_SCALE / 2 : \
72 QT_DISTANCEFIELD_DEFAULT_SCALE)
73 #define QT_DISTANCEFIELD_RADIUS \
74 (m_textureData->doubleGlyphResolution ? QT_DISTANCEFIELD_DEFAULT_RADIUS / 2 : \
75 QT_DISTANCEFIELD_DEFAULT_RADIUS)
77 static inline int qt_next_power_of_two(int v)
89 static float defaultThresholdFunc(float glyphScale)
91 static float base = qgetenv("QT_DF_BASE").isEmpty() ? 0.5f : qgetenv("QT_DF_BASE").toFloat();
92 static float baseDev = qgetenv("QT_DF_BASEDEVIATION").isEmpty() ? 0.065f : qgetenv("QT_DF_BASEDEVIATION").toFloat();
93 static float devScaleMin = qgetenv("QT_DF_SCALEFORMAXDEV").isEmpty() ? 0.15f : qgetenv("QT_DF_SCALEFORMAXDEV").toFloat();
94 static float devScaleMax = qgetenv("QT_DF_SCALEFORNODEV").isEmpty() ? 0.3f : qgetenv("QT_DF_SCALEFORNODEV").toFloat();
95 return base - ((qBound(devScaleMin, glyphScale, devScaleMax) - devScaleMin) / (devScaleMax - devScaleMin) * -baseDev + baseDev);
98 static float defaultAntialiasingSpreadFunc(float glyphScale)
100 static float range = qgetenv("QT_DF_RANGE").isEmpty() ? 0.06f : qgetenv("QT_DF_RANGE").toFloat();
101 return range / glyphScale;
125 template <FillClip clip, FillHDir dir>
126 inline void fillLine(qint32 *, int, int, int, qint32, qint32)
131 inline void fillLine<Clip, LeftToRight>(qint32 *line, int width, int lx, int rx, qint32 d, qint32 dd)
133 int fromX = qMax(0, lx >> 8);
134 int toX = qMin(width, rx >> 8);
138 qint32 val = d + (((fromX << 8) + 0xff - lx) * dd >> 8);
141 *line = abs(val) < abs(*line) ? val : *line;
148 inline void fillLine<Clip, RightToLeft>(qint32 *line, int width, int lx, int rx, qint32 d, qint32 dd)
150 int fromX = qMax(0, lx >> 8);
151 int toX = qMin(width, rx >> 8);
155 qint32 val = d + (((toX << 8) + 0xff - rx) * dd >> 8);
160 *line = abs(val) < abs(*line) ? val : *line;
165 inline void fillLine<NoClip, LeftToRight>(qint32 *line, int, int lx, int rx, qint32 d, qint32 dd)
172 qint32 val = d + ((~lx & 0xff) * dd >> 8);
175 *line = abs(val) < abs(*line) ? val : *line;
182 inline void fillLine<NoClip, RightToLeft>(qint32 *line, int, int lx, int rx, qint32 d, qint32 dd)
189 qint32 val = d + ((~rx & 0xff) * dd >> 8);
194 *line = abs(val) < abs(*line) ? val : *line;
198 template <FillClip clip, FillVDir vDir, FillHDir hDir>
199 inline void fillLines(qint32 *bits, int width, int height, int upperY, int lowerY,
200 int &lx, int ldx, int &rx, int rdx, qint32 &d, qint32 ddy, qint32 ddx)
203 Q_ASSERT(upperY < lowerY);
204 int y = lowerY - upperY;
205 if (vDir == TopDown) {
206 qint32 *line = bits + upperY * width;
208 fillLine<clip, hDir>(line, width, lx, rx, d, ddx);
215 qint32 *line = bits + lowerY * width;
221 fillLine<clip, hDir>(line, width, lx, rx, d, ddx);
226 template <FillClip clip>
227 void drawTriangle(qint32 *bits, int width, int height, const QPoint *center,
228 const QPoint *v1, const QPoint *v2, qint32 value)
230 const int y1 = clip == Clip ? qBound(0, v1->y() >> 8, height) : v1->y() >> 8;
231 const int y2 = clip == Clip ? qBound(0, v2->y() >> 8, height) : v2->y() >> 8;
232 const int yC = clip == Clip ? qBound(0, center->y() >> 8, height) : center->y() >> 8;
234 const int v1Frac = clip == Clip ? (y1 << 8) + 0xff - v1->y() : ~v2->y() & 0xff;
235 const int v2Frac = clip == Clip ? (y2 << 8) + 0xff - v2->y() : ~v1->y() & 0xff;
236 const int centerFrac = clip == Clip ? (yC << 8) + 0xff - center->y() : ~center->y() & 0xff;
238 int dx1, x1, dx2, x2;
239 qint32 dd1, d1, dd2, d2;
240 if (v1->y() != center->y()) {
241 dx1 = ((v1->x() - center->x()) << 8) / (v1->y() - center->y());
242 x1 = center->x() + centerFrac * (v1->x() - center->x()) / (v1->y() - center->y());
244 if (v2->y() != center->y()) {
245 dx2 = ((v2->x() - center->x()) << 8) / (v2->y() - center->y());
246 x2 = center->x() + centerFrac * (v2->x() - center->x()) / (v2->y() - center->y());
249 const qint32 div = (v2->x() - center->x()) * (v1->y() - center->y())
250 - (v2->y() - center->y()) * (v1->x() - center->x());
251 const qint32 dd = div ? qint32((qint64(value * (v1->y() - v2->y())) << 8) / div) : 0;
255 // Center at the bottom.
259 d1 = centerFrac * value / (v1->y() - center->y());
260 dd1 = ((value << 8) / (v1->y() - center->y()));
261 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y1, yC, x1, dx1,
262 x2, dx2, d1, dd1, dd);
263 dx1 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
264 x1 = v1->x() + v1Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
265 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y2, y1, x1, dx1,
266 x2, dx2, value, 0, dd);
270 d2 = centerFrac * value / (v2->y() - center->y());
271 dd2 = ((value << 8) / (v2->y() - center->y()));
272 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y2, yC, x1, dx1,
273 x2, dx2, d2, dd2, dd);
275 dx2 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
276 x2 = v2->x() + v2Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
277 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y1, y2, x1, dx1,
278 x2, dx2, value, 0, dd);
283 // Center to the right.
284 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
286 xUp = xDn = v2->x() + (clip == Clip ? (yC << 8) + 0xff - v2->y()
287 : (center->y() | 0xff) - v2->y())
288 * (v1->x() - v2->x()) / (v1->y() - v2->y());
289 fillLines<clip, BottomUp, LeftToRight>(bits, width, height, y2, yC, xUp, dx,
290 x2, dx2, value, 0, dd);
292 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yC, y1, xDn, dx,
293 x1, dx1, value, 0, dd);
298 // Center to the left.
299 int dx = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
301 xUp = xDn = v1->x() + (clip == Clip ? (yC << 8) + 0xff - v1->y()
302 : (center->y() | 0xff) - v1->y())
303 * (v1->x() - v2->x()) / (v1->y() - v2->y());
304 fillLines<clip, BottomUp, RightToLeft>(bits, width, height, y1, yC, x1, dx1,
305 xUp, dx, value, 0, dd);
307 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yC, y2, x2, dx2,
308 xDn, dx, value, 0, dd);
310 // Center at the top.
315 d2 = centerFrac * value / (v2->y() - center->y());
316 dd2 = ((value << 8) / (v2->y() - center->y()));
317 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yC, y2, x2, dx2,
318 x1, dx1, d2, dd2, dd);
320 dx2 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
321 x2 = v2->x() + v2Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
322 fillLines<clip, TopDown, LeftToRight>(bits, width, height, y2, y1, x2, dx2,
323 x1, dx1, value, 0, dd);
328 d1 = centerFrac * value / (v1->y() - center->y());
329 dd1 = ((value << 8) / (v1->y() - center->y()));
330 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yC, y1, x2, dx2,
331 x1, dx1, d1, dd1, dd);
334 dx1 = ((v1->x() - v2->x()) << 8) / (v1->y() - v2->y());
335 x1 = v1->x() + v1Frac * (v1->x() - v2->x()) / (v1->y() - v2->y());
336 fillLines<clip, TopDown, RightToLeft>(bits, width, height, y1, y2, x2, dx2,
337 x1, dx1, value, 0, dd);
344 template <FillClip clip>
345 void drawRectangle(qint32 *bits, int width, int height,
346 const QPoint *int1, const QPoint *center1, const QPoint *ext1,
347 const QPoint *int2, const QPoint *center2, const QPoint *ext2,
350 if (center1->y() > center2->y()) {
351 qSwap(center1, center2);
354 extValue = -extValue;
357 Q_ASSERT(ext1->x() - center1->x() == center1->x() - int1->x());
358 Q_ASSERT(ext1->y() - center1->y() == center1->y() - int1->y());
359 Q_ASSERT(ext2->x() - center2->x() == center2->x() - int2->x());
360 Q_ASSERT(ext2->y() - center2->y() == center2->y() - int2->y());
362 const int yc1 = clip == Clip ? qBound(0, center1->y() >> 8, height) : center1->y() >> 8;
363 const int yc2 = clip == Clip ? qBound(0, center2->y() >> 8, height) : center2->y() >> 8;
364 const int yi1 = clip == Clip ? qBound(0, int1->y() >> 8, height) : int1->y() >> 8;
365 const int yi2 = clip == Clip ? qBound(0, int2->y() >> 8, height) : int2->y() >> 8;
366 const int ye1 = clip == Clip ? qBound(0, ext1->y() >> 8, height) : ext1->y() >> 8;
367 const int ye2 = clip == Clip ? qBound(0, ext2->y() >> 8, height) : ext2->y() >> 8;
369 const int center1Frac = clip == Clip ? (yc1 << 8) + 0xff - center1->y() : ~center1->y() & 0xff;
370 const int center2Frac = clip == Clip ? (yc2 << 8) + 0xff - center2->y() : ~center2->y() & 0xff;
371 const int int1Frac = clip == Clip ? (yi1 << 8) + 0xff - int1->y() : ~int1->y() & 0xff;
372 const int ext1Frac = clip == Clip ? (ye1 << 8) + 0xff - ext1->y() : ~ext1->y() & 0xff;
374 int dxC, dxE; // cap slope, edge slope
376 if (ext1->y() != int1->y()) {
377 dxC = ((ext1->x() - int1->x()) << 8) / (ext1->y() - int1->y());
378 ddC = (extValue << 9) / (ext1->y() - int1->y());
380 if (ext1->y() != ext2->y())
381 dxE = ((ext1->x() - ext2->x()) << 8) / (ext1->y() - ext2->y());
383 const qint32 div = (ext1->x() - int1->x()) * (ext2->y() - int1->y())
384 - (ext1->y() - int1->y()) * (ext2->x() - int1->x());
385 const qint32 dd = div ? qint32((qint64(extValue * (ext2->y() - ext1->y())) << 9) / div) : 0;
387 int xe1, xe2, xc1, xc2;
390 qint32 intValue = -extValue;
392 if (center2->x() < center1->x()) {
393 // Leaning to the right. '/'
394 if (int1->y() < ext2->y()) {
396 Q_ASSERT(ext1->y() != ext2->y());
397 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
398 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
400 xc2 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
401 xc2 += (ye1 - yc1) * dxC;
402 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, yi1, xe1, dxE,
403 xc2, dxC, extValue, 0, dd);
406 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yi1, ye2, xe1, dxE,
407 xe2, dxE, extValue, 0, dd);
409 xc1 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
410 xc1 += (ye2 - yc2) * dxC;
411 fillLines<clip, TopDown, RightToLeft>(bits, width, height, ye2, yi2, xc1, dxC,
412 xe2, dxE, intValue, 0, dd);
415 // Mostly horizontal.
416 Q_ASSERT(ext1->y() != int1->y());
417 xc1 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
418 xc2 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
419 xc1 += (ye2 - yc2) * dxC;
420 xc2 += (ye1 - yc1) * dxC;
422 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
423 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, ye2, xe1, dxE,
424 xc2, dxC, extValue, 0, dd);
427 d = (clip == Clip ? (ye2 << 8) + 0xff - center2->y()
428 : (ext2->y() | 0xff) - center2->y())
429 * 2 * extValue / (ext1->y() - int1->y());
430 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye2, yi1, xc1, dxC,
431 xc2, dxC, d, ddC, dd);
434 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
435 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, yi2, xc1, dxC,
436 xe2, dxE, intValue, 0, dd);
440 // Leaning to the left. '\'
441 if (ext1->y() < int2->y()) {
443 Q_ASSERT(ext1->y() != ext2->y());
444 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
445 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
447 xc1 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
448 xc1 += (yi1 - yc1) * dxC;
449 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, ye1, xc1, dxC,
450 xe2, dxE, intValue, 0, dd);
453 fillLines<clip, TopDown, RightToLeft>(bits, width, height, ye1, yi2, xe1, dxE,
454 xe2, dxE, intValue, 0, dd);
456 xc2 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
457 xc2 += (yi2 - yc2) * dxC;
458 fillLines<clip, TopDown, LeftToRight>(bits, width, height, yi2, ye2, xe1, dxE,
459 xc2, dxC, extValue, 0, dd);
462 // Mostly horizontal.
463 Q_ASSERT(ext1->y() != int1->y());
464 xc1 = center1->x() + center1Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
465 xc2 = center2->x() + center2Frac * (ext1->x() - int1->x()) / (ext1->y() - int1->y());
466 xc1 += (yi1 - yc1) * dxC;
467 xc2 += (yi2 - yc2) * dxC;
469 xe2 = int1->x() + int1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
470 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi1, yi2, xc1, dxC,
471 xe2, dxE, intValue, 0, dd);
474 d = (clip == Clip ? (yi2 << 8) + 0xff - center2->y()
475 : (int2->y() | 0xff) - center2->y())
476 * 2 * extValue / (ext1->y() - int1->y());
477 fillLines<clip, TopDown, RightToLeft>(bits, width, height, yi2, ye1, xc1, dxC,
478 xc2, dxC, d, ddC, dd);
481 xe1 = ext1->x() + ext1Frac * (ext1->x() - ext2->x()) / (ext1->y() - ext2->y());
482 fillLines<clip, TopDown, LeftToRight>(bits, width, height, ye1, ye2, xe1, dxE,
483 xc2, dxC, extValue, 0, dd);
489 static void drawPolygons(qint32 *bits, int width, int height, const QPoint *vertices,
490 const quint32 *indices, int indexCount, qint32 value)
492 Q_ASSERT(indexCount != 0);
493 Q_ASSERT(height <= 128);
494 QVarLengthArray<quint8, 16> scans[128];
496 for (int i = 1; i < indexCount; ++i) {
497 quint32 idx1 = indices[i - 1];
498 quint32 idx2 = indices[i];
499 Q_ASSERT(idx1 != quint32(-1));
500 if (idx2 == quint32(-1)) {
501 idx2 = indices[first];
502 Q_ASSERT(idx2 != quint32(-1));
505 const QPoint *v1 = &vertices[idx1];
506 const QPoint *v2 = &vertices[idx2];
507 if (v2->y() < v1->y())
509 int fromY = qMax(0, v1->y() >> 8);
510 int toY = qMin(height, v2->y() >> 8);
513 int dx = ((v2->x() - v1->x()) << 8) / (v2->y() - v1->y());
514 int x = v1->x() + ((fromY << 8) + 0xff - v1->y()) * (v2->x() - v1->x()) / (v2->y() - v1->y());
515 for (int y = fromY; y < toY; ++y) {
516 quint32 c = quint32(x >> 8);
517 if (c < quint32(width))
518 scans[y].append(quint8(c));
522 for (int i = 0; i < height; ++i) {
523 quint8 *scanline = scans[i].data();
524 int size = scans[i].size();
525 for (int j = 1; j < size; ++j) {
527 quint8 value = scanline[k];
528 for (; k != 0 && value < scanline[k - 1]; --k)
529 scanline[k] = scanline[k - 1];
532 qint32 *line = bits + i * width;
534 for (; j + 1 < size; j += 2) {
535 for (quint8 x = scanline[j]; x < scanline[j + 1]; ++x)
539 for (int x = scanline[j]; x < width; ++x)
545 static QImage makeDistanceField(int imgSize, const QPainterPath &path, int dfScale, int offs)
547 QImage image(imgSize, imgSize, QImage::Format_Indexed8);
549 if (path.isEmpty()) {
554 QTransform transform;
555 transform.translate(offs, offs);
556 transform.scale(qreal(1) / dfScale, qreal(1) / dfScale);
558 QDataBuffer<quint32> pathIndices(0);
559 QDataBuffer<QPoint> pathVertices(0);
560 qSimplifyPath(path, pathVertices, pathIndices, transform);
562 const qint32 interiorColor = -0x7f80; // 8:8 signed format, -127.5
563 const qint32 exteriorColor = 0x7f80; // 8:8 signed format, 127.5
565 QScopedArrayPointer<qint32> bits(new qint32[imgSize * imgSize]);
566 for (int i = 0; i < imgSize * imgSize; ++i)
567 bits[i] = exteriorColor;
569 const qreal angleStep = qreal(15 * 3.141592653589793238 / 180);
570 const QPoint rotation(qRound(cos(angleStep) * 0x4000),
571 qRound(sin(angleStep) * 0x4000)); // 2:14 signed
573 const quint32 *indices = pathIndices.data();
574 QVarLengthArray<QPoint> normals;
575 QVarLengthArray<QPoint> vertices;
576 QVarLengthArray<bool> isConvex;
577 QVarLengthArray<bool> needsClipping;
579 drawPolygons(bits.data(), imgSize, imgSize, pathVertices.data(), indices, pathIndices.size(),
584 while (index < pathIndices.size()) {
587 needsClipping.clear();
589 // Find end of polygon.
591 while (indices[end] != quint32(-1))
594 // Calculate vertex normals.
595 for (int next = index, prev = end - 1; next < end; prev = next++) {
596 quint32 fromVertexIndex = indices[prev];
597 quint32 toVertexIndex = indices[next];
599 const QPoint &from = pathVertices.at(fromVertexIndex);
600 const QPoint &to = pathVertices.at(toVertexIndex);
602 QPoint n(to.y() - from.y(), from.x() - to.x());
603 if (n.x() == 0 && n.y() == 0)
605 int scale = qRound((offs << 16) / sqrt(qreal(n.x() * n.x() + n.y() * n.y()))); // 8:16
606 n.rx() = n.x() * scale >> 8;
607 n.ry() = n.y() * scale >> 8;
609 QPoint v(to.x() + 0x7f, to.y() + 0x7f);
611 needsClipping.append((to.x() < offs << 8) || (to.x() >= (imgSize - offs) << 8)
612 || (to.y() < offs << 8) || (to.y() >= (imgSize - offs) << 8));
615 isConvex.resize(normals.count());
616 for (int next = 0, prev = normals.count() - 1; next < normals.count(); prev = next++) {
617 isConvex[prev] = normals.at(prev).x() * normals.at(next).y()
618 - normals.at(prev).y() * normals.at(next).x() < 0;
622 for (int next = 0, prev = normals.count() - 1; next < normals.count(); prev = next++) {
623 QPoint n = normals.at(next);
624 QPoint intPrev = vertices.at(prev);
625 QPoint extPrev = vertices.at(prev);
626 QPoint intNext = vertices.at(next);
627 QPoint extNext = vertices.at(next);
629 extPrev.rx() -= n.x();
630 extPrev.ry() -= n.y();
631 intPrev.rx() += n.x();
632 intPrev.ry() += n.y();
633 extNext.rx() -= n.x();
634 extNext.ry() -= n.y();
635 intNext.rx() += n.x();
636 intNext.ry() += n.y();
638 if (needsClipping[prev] || needsClipping[next]) {
639 drawRectangle<Clip>(bits.data(), imgSize, imgSize,
640 &intPrev, &vertices.at(prev), &extPrev,
641 &intNext, &vertices.at(next), &extNext,
644 drawRectangle<NoClip>(bits.data(), imgSize, imgSize,
645 &intPrev, &vertices.at(prev), &extPrev,
646 &intNext, &vertices.at(next), &extNext,
650 if (isConvex.at(prev)) {
652 if (needsClipping[prev]) {
654 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
655 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
657 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
658 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
659 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
660 drawTriangle<Clip>(bits.data(), imgSize, imgSize, &vertices.at(prev),
661 &extPrev, &p, exteriorColor);
665 p.rx() = vertices.at(prev).x() - n.x();
666 p.ry() = vertices.at(prev).y() - n.y();
667 drawTriangle<Clip>(bits.data(), imgSize, imgSize, &vertices.at(prev),
668 &extPrev, &p, exteriorColor);
673 QPoint rn((n.x() * rotation.x() - n.y() * rotation.y()) >> 14,
674 (n.y() * rotation.x() + n.x() * rotation.y()) >> 14);
676 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() <= 0) {
677 p.rx() = vertices.at(prev).x() - normals.at(prev).x();
678 p.ry() = vertices.at(prev).y() - normals.at(prev).y();
679 drawTriangle<NoClip>(bits.data(), imgSize, imgSize, &vertices.at(prev),
680 &extPrev, &p, exteriorColor);
684 p.rx() = vertices.at(prev).x() - n.x();
685 p.ry() = vertices.at(prev).y() - n.y();
686 drawTriangle<NoClip>(bits.data(), imgSize, imgSize, &vertices.at(prev),
687 &extPrev, &p, exteriorColor);
693 if (needsClipping[prev]) {
695 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
696 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
698 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
699 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
700 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
701 drawTriangle<Clip>(bits.data(), imgSize, imgSize, &vertices.at(prev),
702 &p, &intPrev, interiorColor);
706 p.rx() = vertices.at(prev).x() + n.x();
707 p.ry() = vertices.at(prev).y() + n.y();
708 drawTriangle<Clip>(bits.data(), imgSize, imgSize, &vertices.at(prev),
709 &p, &intPrev, interiorColor);
714 QPoint rn((n.x() * rotation.x() + n.y() * rotation.y()) >> 14,
715 (n.y() * rotation.x() - n.x() * rotation.y()) >> 14);
717 if (n.x() * normals.at(prev).y() - n.y() * normals.at(prev).x() >= 0) {
718 p.rx() = vertices.at(prev).x() + normals.at(prev).x();
719 p.ry() = vertices.at(prev).y() + normals.at(prev).y();
720 drawTriangle<NoClip>(bits.data(), imgSize, imgSize, &vertices.at(prev),
721 &p, &intPrev, interiorColor);
725 p.rx() = vertices.at(prev).x() + n.x();
726 p.ry() = vertices.at(prev).y() + n.y();
727 drawTriangle<NoClip>(bits.data(), imgSize, imgSize, &vertices.at(prev),
728 &p, &intPrev, interiorColor);
738 const qint32 *inLine = bits.data();
739 uchar *outLine = image.bits();
740 int padding = image.bytesPerLine() - image.width();
741 for (int y = 0; y < imgSize; ++y) {
742 for (int x = 0; x < imgSize; ++x, ++inLine, ++outLine)
743 *outLine = uchar((0x7f80 - *inLine) >> 8);
750 static bool fontHasNarrowOutlines(const QRawFont &f)
753 font.setPixelSize(QT_DISTANCEFIELD_DEFAULT_BASEFONTSIZE);
754 Q_ASSERT(font.isValid());
756 QVector<quint32> glyphIndices = font.glyphIndexesForString(QLatin1String("O"));
757 if (glyphIndices.size() < 1)
760 QImage im = font.alphaMapForGlyph(glyphIndices.at(0), QRawFont::PixelAntialiasing);
769 int y = (im.height() + 1) / 2;
770 for (int x = 0; x < im.width(); ++x) {
771 int a = qAlpha(im.pixel(x, y));
777 minHThick = qMin(minHThick, thick);
784 int x = (im.width() + 1) / 2;
785 for (int y = 0; y < im.height(); ++y) {
786 int a = qAlpha(im.pixel(x, y));
792 minVThick = qMin(minVThick, thick);
797 return minHThick == 1 || minVThick == 1;
800 DEFINE_BOOL_CONFIG_OPTION(disableDistanceField, QML_DISABLE_DISTANCEFIELD)
802 QHash<QPair<const QGLContext *, QFontEngine *>, QSGDistanceFieldGlyphCache *> QSGDistanceFieldGlyphCache::m_caches;
803 QHash<QFontEngine *, QGLContextGroupResource<QSGDistanceFieldGlyphCache::DistanceFieldTextureData> > QSGDistanceFieldGlyphCache::m_textures_data;
805 QSGDistanceFieldGlyphCache *QSGDistanceFieldGlyphCache::get(const QGLContext *ctx, const QRawFont &font)
807 QRawFontPrivate *fontD = QRawFontPrivate::get(font);
808 QPair<const QGLContext *, QFontEngine *> key(ctx, fontD->fontEngine);
809 QHash<QPair<const QGLContext *, QFontEngine *>, QSGDistanceFieldGlyphCache *>::iterator atlas = m_caches.find(key);
810 if (atlas == m_caches.end())
811 atlas = m_caches.insert(key, new QSGDistanceFieldGlyphCache(ctx, font));
813 return atlas.value();
816 QSGDistanceFieldGlyphCache::DistanceFieldTextureData *QSGDistanceFieldGlyphCache::textureData()
818 return m_textures_data[QRawFontPrivate::get(m_font)->fontEngine].value(ctx);
821 QSGDistanceFieldGlyphCache::QSGDistanceFieldGlyphCache(const QGLContext *c, const QRawFont &font)
823 , m_maxTextureSize(0)
826 , m_threshold_func(defaultThresholdFunc)
827 , m_antialiasingSpread_func(defaultAntialiasingSpreadFunc)
829 Q_ASSERT(font.isValid());
832 m_textureData = textureData();
834 QRawFontPrivate *fontD = QRawFontPrivate::get(m_font);
835 m_glyphCount = fontD->fontEngine->glyphCount();
837 m_textureData->doubleGlyphResolution = fontHasNarrowOutlines(font) && m_glyphCount < QT_DISTANCEFIELD_HIGHGLYPHCOUNT;
839 m_referenceFont = m_font;
840 m_referenceFont.setPixelSize(QT_DISTANCEFIELD_BASEFONTSIZE);
841 Q_ASSERT(m_referenceFont.isValid());
843 m_vertexCoordinateArray[0] = -1.0f;
844 m_vertexCoordinateArray[1] = -1.0f;
845 m_vertexCoordinateArray[2] = 1.0f;
846 m_vertexCoordinateArray[3] = -1.0f;
847 m_vertexCoordinateArray[4] = 1.0f;
848 m_vertexCoordinateArray[5] = 1.0f;
849 m_vertexCoordinateArray[6] = -1.0f;
850 m_vertexCoordinateArray[7] = 1.0f;
852 m_textureCoordinateArray[0] = 0.0f;
853 m_textureCoordinateArray[1] = 0.0f;
854 m_textureCoordinateArray[2] = 1.0f;
855 m_textureCoordinateArray[3] = 0.0f;
856 m_textureCoordinateArray[4] = 1.0f;
857 m_textureCoordinateArray[5] = 1.0f;
858 m_textureCoordinateArray[6] = 0.0f;
859 m_textureCoordinateArray[7] = 1.0f;
861 connect(QGLSignalProxy::instance(), SIGNAL(aboutToDestroyContext(const QGLContext*)),
862 this, SLOT(onContextDestroyed(const QGLContext*)));
865 QSGDistanceFieldGlyphCache::~QSGDistanceFieldGlyphCache()
867 delete m_blitProgram;
870 void QSGDistanceFieldGlyphCache::onContextDestroyed(const QGLContext *context)
875 QRawFontPrivate *fontD = QRawFontPrivate::get(m_font);
876 QPair<const QGLContext *, QFontEngine *> key(context, fontD->fontEngine);
877 m_caches.remove(key);
881 GLuint QSGDistanceFieldGlyphCache::texture()
883 return m_textureData->texture;
886 QSize QSGDistanceFieldGlyphCache::textureSize() const
888 return m_textureData->size;
891 int QSGDistanceFieldGlyphCache::maxTextureSize() const
893 if (!m_maxTextureSize)
894 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_maxTextureSize);
895 return m_maxTextureSize;
898 QSGDistanceFieldGlyphCache::Metrics QSGDistanceFieldGlyphCache::glyphMetrics(glyph_t glyph)
900 QHash<glyph_t, Metrics>::iterator metric = m_metrics.find(glyph);
901 if (metric == m_metrics.end()) {
902 QPainterPath path = m_font.pathForGlyph(glyph);
903 QRectF br = path.boundingRect();
906 m.width = br.width();
907 m.height = br.height();
908 m.baselineX = br.x();
909 m.baselineY = -br.y();
911 metric = m_metrics.insert(glyph, m);
914 return metric.value();
917 QSGDistanceFieldGlyphCache::TexCoord QSGDistanceFieldGlyphCache::glyphTexCoord(glyph_t glyph)
919 return m_textureData->texCoords.value(glyph);
922 QImage QSGDistanceFieldGlyphCache::renderDistanceFieldGlyph(glyph_t glyph) const
924 QRawFont renderFont = m_font;
925 renderFont.setPixelSize(QT_DISTANCEFIELD_BASEFONTSIZE * QT_DISTANCEFIELD_SCALE);
927 QPainterPath path = renderFont.pathForGlyph(glyph);
928 path.translate(-path.boundingRect().topLeft());
929 path.setFillRule(Qt::WindingFill);
931 QImage im = makeDistanceField(QT_DISTANCEFIELD_TILESIZE,
933 QT_DISTANCEFIELD_SCALE,
934 QT_DISTANCEFIELD_RADIUS / QT_DISTANCEFIELD_SCALE);
938 qreal QSGDistanceFieldGlyphCache::fontScale() const
940 return qreal(m_font.pixelSize()) / QT_DISTANCEFIELD_BASEFONTSIZE;
943 int QSGDistanceFieldGlyphCache::distanceFieldRadius() const
945 return QT_DISTANCEFIELD_DEFAULT_RADIUS / QT_DISTANCEFIELD_SCALE;
948 void QSGDistanceFieldGlyphCache::populate(int count, const glyph_t *glyphs)
950 // Avoid useless and costly glyph re-generation
951 if (cacheIsFull() && !m_textureData->unusedGlyphs.isEmpty()) {
952 for (int i = 0; i < count; ++i) {
953 glyph_t glyphIndex = glyphs[i];
954 if (m_textureData->texCoords.contains(glyphIndex) && m_textureData->unusedGlyphs.contains(glyphIndex))
955 m_textureData->unusedGlyphs.remove(glyphIndex);
959 for (int i = 0; i < count; ++i) {
960 glyph_t glyphIndex = glyphs[i];
961 if ((int) glyphIndex >= glyphCount()) {
962 qWarning("Warning: distance-field glyph is not available with index %d", glyphIndex);
966 if (++m_textureData->glyphRefCount[glyphIndex] == 1)
967 m_textureData->unusedGlyphs.remove(glyphIndex);
969 if (m_textureData->texCoords.contains(glyphIndex)
970 || (cacheIsFull() && m_textureData->unusedGlyphs.isEmpty()))
973 QPainterPath path = m_referenceFont.pathForGlyph(glyphIndex);
974 if (path.isEmpty()) {
975 m_textureData->texCoords.insert(glyphIndex, TexCoord());
978 QRectF br = path.boundingRect();
981 c.xMargin = QT_DISTANCEFIELD_RADIUS / qreal(QT_DISTANCEFIELD_SCALE);
982 c.yMargin = QT_DISTANCEFIELD_RADIUS / qreal(QT_DISTANCEFIELD_SCALE);
983 c.x = m_textureData->currX;
984 c.y = m_textureData->currY;
985 c.width = br.width();
986 c.height = br.height();
988 if (!cacheIsFull()) {
989 m_textureData->currX += QT_DISTANCEFIELD_TILESIZE;
990 if (m_textureData->currX >= maxTextureSize()) {
991 m_textureData->currX = 0;
992 m_textureData->currY += QT_DISTANCEFIELD_TILESIZE;
996 if (!m_textureData->unusedGlyphs.isEmpty()) {
997 glyph_t unusedGlyph = *m_textureData->unusedGlyphs.constBegin();
998 TexCoord unusedCoord = glyphTexCoord(unusedGlyph);
1000 c.y = unusedCoord.y;
1001 m_textureData->unusedGlyphs.remove(unusedGlyph);
1002 m_textureData->texCoords.remove(unusedGlyph);
1006 if (c.y < maxTextureSize()) {
1007 m_textureData->texCoords.insert(glyphIndex, c);
1008 m_textureData->pendingGlyphs.add(glyphIndex);
1013 void QSGDistanceFieldGlyphCache::derefGlyphs(int count, const glyph_t *glyphs)
1015 for (int i = 0; i < count; ++i)
1016 if (--m_textureData->glyphRefCount[glyphs[i]] == 0 && !glyphTexCoord(glyphs[i]).isNull())
1017 m_textureData->unusedGlyphs.insert(glyphs[i]);
1020 void QSGDistanceFieldGlyphCache::createTexture(int width, int height)
1022 if (ctx->d_ptr->workaround_brokenFBOReadBack && m_textureData->image.isNull())
1023 m_textureData->image = QImage(width, height, QImage::Format_Indexed8);
1025 while (glGetError() != GL_NO_ERROR) { }
1027 glGenTextures(1, &m_textureData->texture);
1028 glBindTexture(GL_TEXTURE_2D, m_textureData->texture);
1030 QVarLengthArray<uchar> data(width * height);
1031 for (int i = 0; i < data.size(); ++i)
1033 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, &data[0]);
1034 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1035 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1036 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1037 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1039 m_textureData->size = QSize(width, height);
1041 GLuint error = glGetError();
1042 if (error != GL_NO_ERROR) {
1043 glBindTexture(GL_TEXTURE_2D, 0);
1044 glDeleteTextures(1, &m_textureData->texture);
1045 m_textureData->texture = 0;
1050 void QSGDistanceFieldGlyphCache::resizeTexture(int width, int height)
1052 int oldWidth = m_textureData->size.width();
1053 int oldHeight = m_textureData->size.height();
1054 if (width == oldWidth && height == oldHeight)
1057 GLuint oldTexture = m_textureData->texture;
1058 createTexture(width, height);
1063 if (ctx->d_ptr->workaround_brokenFBOReadBack) {
1064 m_textureData->image = m_textureData->image.copy(0, 0, width, height);
1065 QImage copy = m_textureData->image.copy(0, 0, oldWidth, oldHeight);
1066 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, oldWidth, oldHeight, GL_ALPHA, GL_UNSIGNED_BYTE, copy.constBits());
1067 glDeleteTextures(1, &oldTexture);
1071 if (!m_textureData->fbo)
1072 ctx->functions()->glGenFramebuffers(1, &m_textureData->fbo);
1073 ctx->functions()->glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_textureData->fbo);
1076 glGenTextures(1, &tmp_texture);
1077 glBindTexture(GL_TEXTURE_2D, tmp_texture);
1078 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, oldWidth, oldHeight, 0,
1079 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1080 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1081 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1082 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1083 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1084 glBindTexture(GL_TEXTURE_2D, 0);
1085 ctx->functions()->glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
1086 GL_TEXTURE_2D, tmp_texture, 0);
1088 ctx->functions()->glActiveTexture(GL_TEXTURE0);
1089 glBindTexture(GL_TEXTURE_2D, oldTexture);
1091 // save current render states
1092 GLboolean stencilTestEnabled;
1093 GLboolean depthTestEnabled;
1094 GLboolean scissorTestEnabled;
1095 GLboolean blendEnabled;
1097 glGetBooleanv(GL_STENCIL_TEST, &stencilTestEnabled);
1098 glGetBooleanv(GL_DEPTH_TEST, &depthTestEnabled);
1099 glGetBooleanv(GL_SCISSOR_TEST, &scissorTestEnabled);
1100 glGetBooleanv(GL_BLEND, &blendEnabled);
1101 glGetIntegerv(GL_VIEWPORT, &viewport[0]);
1103 glDisable(GL_STENCIL_TEST);
1104 glDisable(GL_DEPTH_TEST);
1105 glDisable(GL_SCISSOR_TEST);
1106 glDisable(GL_BLEND);
1108 glViewport(0, 0, oldWidth, oldHeight);
1110 if (m_blitProgram == 0) {
1111 m_blitProgram = new QGLShaderProgram;
1115 source.append(QLatin1String(qglslMainWithTexCoordsVertexShader));
1116 source.append(QLatin1String(qglslUntransformedPositionVertexShader));
1118 QGLShader *vertexShader = new QGLShader(QGLShader::Vertex, m_blitProgram);
1119 vertexShader->compileSourceCode(source);
1121 m_blitProgram->addShader(vertexShader);
1126 source.append(QLatin1String(qglslMainFragmentShader));
1127 source.append(QLatin1String(qglslImageSrcFragmentShader));
1129 QGLShader *fragmentShader = new QGLShader(QGLShader::Fragment, m_blitProgram);
1130 fragmentShader->compileSourceCode(source);
1132 m_blitProgram->addShader(fragmentShader);
1135 m_blitProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR);
1136 m_blitProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR);
1138 m_blitProgram->link();
1141 ctx->functions()->glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, m_vertexCoordinateArray);
1142 ctx->functions()->glVertexAttribPointer(QT_TEXTURE_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, m_textureCoordinateArray);
1144 m_blitProgram->bind();
1145 m_blitProgram->enableAttributeArray(int(QT_VERTEX_COORDS_ATTR));
1146 m_blitProgram->enableAttributeArray(int(QT_TEXTURE_COORDS_ATTR));
1147 m_blitProgram->disableAttributeArray(int(QT_OPACITY_ATTR));
1148 m_blitProgram->setUniformValue("imageTexture", GLuint(0));
1150 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1152 glBindTexture(GL_TEXTURE_2D, m_textureData->texture);
1154 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, oldWidth, oldHeight);
1156 ctx->functions()->glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
1157 GL_RENDERBUFFER_EXT, 0);
1158 glDeleteTextures(1, &tmp_texture);
1159 glDeleteTextures(1, &oldTexture);
1161 ctx->functions()->glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
1163 // restore render states
1164 if (stencilTestEnabled)
1165 glEnable(GL_STENCIL_TEST);
1166 if (depthTestEnabled)
1167 glEnable(GL_DEPTH_TEST);
1168 if (scissorTestEnabled)
1169 glEnable(GL_SCISSOR_TEST);
1172 glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
1175 void QSGDistanceFieldGlyphCache::updateCache()
1177 if (m_textureData->pendingGlyphs.isEmpty())
1180 int requiredWidth = maxTextureSize();
1181 int rows = 128 / (requiredWidth / QT_DISTANCEFIELD_TILESIZE); // Enough rows to fill the latin1 set by default..
1182 int requiredHeight = qMin(maxTextureSize(), qMax(m_textureData->currY + QT_DISTANCEFIELD_TILESIZE, QT_DISTANCEFIELD_TILESIZE * rows));
1184 resizeTexture((requiredWidth), (requiredHeight));
1185 glBindTexture(GL_TEXTURE_2D, m_textureData->texture);
1187 // ### Remove before final release
1188 static bool cacheDistanceFields = QApplication::arguments().contains("--cache-distance-fields");
1190 // #define QSGDISTANCEFIELDS_TIME_CREATION
1191 #ifdef QSGDISTANCEFIELDS_TIME_CREATION
1196 QString tmpPath = QString::fromLatin1("%1/.qt/").arg(QDir::tempPath());
1197 QString keyBase = QString::fromLatin1("%1%2%3_%4_%5_%6.fontblob")
1199 .arg(m_font.familyName())
1200 .arg(m_font.styleName())
1201 .arg(m_font.weight())
1202 .arg(m_font.style());
1204 if (cacheDistanceFields && !QFile::exists(tmpPath))
1205 QDir(tmpPath).mkpath(tmpPath);
1207 for (int i = 0; i < m_textureData->pendingGlyphs.size(); ++i) {
1208 glyph_t glyphIndex = m_textureData->pendingGlyphs.at(i);
1209 TexCoord c = m_textureData->texCoords.value(glyphIndex);
1211 if (cacheDistanceFields) {
1212 QString key = keyBase.arg(glyphIndex);
1214 if (file.open(QFile::ReadOnly)) {
1215 int fileSize = file.size();
1216 int dim = sqrt(float(fileSize));
1217 QByteArray blob = file.readAll();
1218 glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y, dim, dim, GL_ALPHA, GL_UNSIGNED_BYTE, blob.constData());
1223 QImage glyph = renderDistanceFieldGlyph(glyphIndex);
1225 if (ctx->d_ptr->workaround_brokenFBOReadBack) {
1226 uchar *inBits = glyph.scanLine(0);
1227 uchar *outBits = m_textureData->image.scanLine(int(c.y)) + int(c.x);
1228 for (int y = 0; y < glyph.height(); ++y) {
1229 qMemCopy(outBits, inBits, glyph.width());
1230 inBits += glyph.bytesPerLine();
1231 outBits += m_textureData->image.bytesPerLine();
1235 glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y, glyph.width(), glyph.height(), GL_ALPHA, GL_UNSIGNED_BYTE, glyph.constBits());
1237 if (cacheDistanceFields) {
1238 QString key = keyBase.arg(glyphIndex);
1240 file.open(QFile::WriteOnly);
1241 file.write((const char *) glyph.constBits(), glyph.width() * glyph.height());
1245 #ifdef QSGDISTANCEFIELDS_TIME_CREATION
1246 static int totalTime;
1247 totalTime += time.elapsed();
1248 printf("time: %d\n", totalTime);
1251 m_textureData->pendingGlyphs.reset();
1254 bool QSGDistanceFieldGlyphCache::useWorkaroundBrokenFBOReadback() const
1256 return ctx->d_ptr->workaround_brokenFBOReadBack;
1259 bool QSGDistanceFieldGlyphCache::distanceFieldEnabled()
1261 return !disableDistanceField();
1264 int QSGDistanceFieldGlyphCache::glyphCount() const
1266 return m_glyphCount;