22int QTextureGlyphCache::calculateSubPixelPositionCount(glyph_t glyph)
const
27 const int NumSubpixelPositions = 12;
29 QImage images[NumSubpixelPositions];
31 for (
int i = 0; i < NumSubpixelPositions; ++i) {
32 QImage img = textureMapForGlyph(glyph, QFixedPoint(QFixed::fromReal(i / 12.0), 0));
37 m_current_fontengine->addGlyphsToPath(&glyph, &point, 1, &path, QTextItem::RenderFlags());
43 images[numImages++] = std::move(img);
46 for (
int j = 0; j < numImages; ++j) {
47 if (images[j] == img) {
53 images[numImages++] = std::move(img);
60bool QTextureGlyphCache::populate(QFontEngine *fontEngine,
62 const glyph_t *glyphs,
63 const QFixedPoint *positions,
64 QPainter::RenderHints renderHints,
65 bool includeGlyphCacheScale)
68 printf(
"Populating with %lld glyphs\n",
static_cast<
long long>(numGlyphs));
69 qDebug() <<
" -> current transformation: " << m_transform;
72 m_current_fontengine = fontEngine;
73 const int padding = glyphPadding();
74 const int paddingDoubled = padding * 2;
76 bool supportsSubPixelPositions = fontEngine->supportsSubPixelPositions();
77 bool verticalSubPixelPositions = fontEngine->supportsVerticalSubPixelPositions()
78 && (renderHints & QPainter::VerticalSubpixelPositioning) != 0;
79 if (fontEngine->m_subPixelPositionCount == 0) {
80 if (!supportsSubPixelPositions) {
81 fontEngine->m_subPixelPositionCount = 1;
84 while (fontEngine->m_subPixelPositionCount == 0 && i < numGlyphs)
85 fontEngine->m_subPixelPositionCount = calculateSubPixelPositionCount(glyphs[i++]);
89 if (m_cx == 0 && m_cy == 0) {
94 qreal glyphCacheScaleX = transform().m11();
95 qreal glyphCacheScaleY = transform().m22();
97 QHash<GlyphAndSubPixelPosition, Coord> listItemCoordinates;
101 for (qsizetype i = 0; i < numGlyphs; ++i) {
102 const glyph_t glyph = glyphs[i];
104 QFixedPoint subPixelPosition;
105 if (supportsSubPixelPositions) {
106 QFixedPoint pos = positions !=
nullptr ? positions[i] : QFixedPoint();
107 if (includeGlyphCacheScale) {
108 pos = QFixedPoint(QFixed::fromReal(pos.x.toReal() * glyphCacheScaleX),
109 QFixed::fromReal(pos.y.toReal() * glyphCacheScaleY));
111 subPixelPosition = fontEngine->subPixelPositionFor(pos);
112 if (!verticalSubPixelPositions)
113 subPixelPosition.y = 0;
116 if (coords.contains(GlyphAndSubPixelPosition(glyph, subPixelPosition)))
118 if (listItemCoordinates.contains(GlyphAndSubPixelPosition(glyph, subPixelPosition)))
121 glyph_metrics_t metrics = fontEngine->alphaMapBoundingBox(glyph, subPixelPosition, m_transform, m_format);
124 printf(
"(%4x): w=%.2f, h=%.2f, xoff=%.2f, yoff=%.2f, x=%.2f, y=%.2f\n",
126 metrics.width.toReal(),
127 metrics.height.toReal(),
128 metrics.xoff.toReal(),
129 metrics.yoff.toReal(),
133 GlyphAndSubPixelPosition key(glyph, subPixelPosition);
134 int glyph_width = metrics.width.ceil().toInt();
135 int glyph_height = metrics.height.ceil().toInt();
136 if (glyph_height == 0 || glyph_width == 0
137 || QFontEngine::glyphBoundingBoxExceedsLimit(
138 metrics.width.toReal(), metrics.height.toReal(),
139 fontEngine->effectivePixelSizeSquared(m_transform))) {
141 Coord c = { 0, 0, 0, 0, 0, 0 };
142 coords.insert(key, c);
146 if (m_format == QFontEngine::Format_Mono)
147 glyph_width = (glyph_width+7)&~7;
152 metrics.x.truncate(),
153 -metrics.y.truncate() };
155 listItemCoordinates.insert(key, c);
156 rowHeight = qMax(rowHeight, glyph_height);
158 if (listItemCoordinates.isEmpty())
161 rowHeight += paddingDoubled;
167 m_w = qNextPowerOfTwo(qCeil(fontEngine->maxCharWidth()) - 1);
171 QHash<GlyphAndSubPixelPosition, Coord>::iterator iter = listItemCoordinates.begin();
172 int requiredWidth = m_w;
173 while (iter != listItemCoordinates.end()) {
174 Coord c = iter.value();
176 m_currentRowHeight = qMax(m_currentRowHeight, c.h);
178 if (m_cx + c.w + padding > requiredWidth) {
179 int new_width = requiredWidth*2;
180 while (new_width < m_cx + c.w + padding)
182 if (new_width <= maxTextureWidth()) {
183 requiredWidth = new_width;
187 m_cy += m_currentRowHeight + paddingDoubled;
188 m_currentRowHeight = c.h;
192 if (maxTextureHeight() > 0 && m_cy + c.h + padding > maxTextureHeight()) {
200 coords.insert(iter.key(), c);
201 m_pendingGlyphs.insert(iter.key(), c);
203 m_cx += c.w + paddingDoubled;
210void QTextureGlyphCache::fillInPendingGlyphs()
212 if (!hasPendingGlyphs())
215 int requiredHeight = m_h;
216 int requiredWidth = m_w;
217 const int padding = glyphPadding();
219 QHash<GlyphAndSubPixelPosition, Coord>::iterator iter = m_pendingGlyphs.begin();
220 while (iter != m_pendingGlyphs.end()) {
221 Coord c = iter.value();
222 requiredHeight = qMax(requiredHeight, c.y + c.h + padding);
223 requiredWidth = qMax(requiredWidth, c.x + c.w + padding);
228 if (isNull() || requiredHeight > m_h || requiredWidth > m_w) {
230 createCache(qNextPowerOfTwo(requiredWidth - 1), qNextPowerOfTwo(requiredHeight - 1));
232 resizeCache(qNextPowerOfTwo(requiredWidth - 1), qNextPowerOfTwo(requiredHeight - 1));
237 QHash<GlyphAndSubPixelPosition, Coord>::iterator iter = m_pendingGlyphs.begin();
238 while (iter != m_pendingGlyphs.end()) {
239 GlyphAndSubPixelPosition key = iter.key();
240 fillTexture(iter.value(), key.glyph, key.subPixelPosition);
247 m_pendingGlyphs.clear();
304void QImageTextureGlyphCache::fillTexture(
const Coord &c,
306 const QFixedPoint &subPixelPosition)
308 QImage mask = textureMapForGlyph(g, subPixelPosition);
313 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());
314 if (mask.width() > c.w || mask.height() > c.h) {
315 printf(
" ERROR; mask is bigger than reserved space! %dx%d instead of %dx%d\n", mask.width(), mask.height(), c.w,c.h);
319 if (m_format == QFontEngine::Format_A32
320 || m_format == QFontEngine::Format_ARGB) {
321 QImage ref(m_image.bits() + (c.x * 4 + c.y * m_image.bytesPerLine()),
322 qMin(mask.width(), c.w), qMin(mask.height(), c.h), m_image.bytesPerLine(),
325 p.setCompositionMode(QPainter::CompositionMode_Source);
326 p.fillRect(0, 0, c.w, c.h, QColor(0,0,0,0));
327 p.drawImage(0, 0, mask);
329 }
else if (m_format == QFontEngine::Format_Mono) {
330 if (mask.depth() > 1) {
332 mask.convertTo(QImage::Format_Alpha8);
333 mask.reinterpretAsFormat(QImage::Format_Grayscale8);
335 mask.convertTo(QImage::Format_Mono, Qt::ThresholdDither);
338 int mw = qMin(mask.width(), c.w);
339 int mh = qMin(mask.height(), c.h);
340 uchar *d = m_image.bits();
341 qsizetype dbpl = m_image.bytesPerLine();
343 for (
int y = 0; y < c.h; ++y) {
344 uchar *dest = d + (c.y + y) *dbpl + c.x/8;
347 const uchar *src = mask.constScanLine(y);
348 for (
int x = 0; x < c.w/8; ++x) {
355 for (
int x = 0; x < c.w/8; ++x)
360 int mw = qMin(mask.width(), c.w);
361 int mh = qMin(mask.height(), c.h);
362 uchar *d = m_image.bits();
363 qsizetype dbpl = m_image.bytesPerLine();
365 if (mask.depth() == 1) {
366 for (
int y = 0; y < c.h; ++y) {
367 uchar *dest = d + (c.y + y) *dbpl + c.x;
369 const uchar *src = mask.constScanLine(y);
370 for (
int x = 0; x < c.w; ++x) {
372 dest[x] = (src[x >> 3] & (1 << (7 - (x & 7)))) > 0 ? 255 : 0;
376 }
else if (mask.depth() == 8) {
377 for (
int y = 0; y < c.h; ++y) {
378 uchar *dest = d + (c.y + y) *dbpl + c.x;
380 const uchar *src = mask.constScanLine(y);
381 for (
int x = 0; x < c.w; ++x) {
393 int margin = m_current_fontengine ? m_current_fontengine->glyphMargin(m_format) : 0;
394 QPoint base(c.x + margin, c.y + margin + c.baseLineY-1);
395 if (m_image.rect().contains(base))
396 m_image.setPixel(base, 255);
397 m_image.save(QString::fromLatin1(
"cache-%1.png").arg(qint64(
this)));