Qt 4.8
Classes | Public Functions | Public Variables | Protected Functions | Protected Variables | List of all members
QTextureGlyphCache Class Referenceabstract

#include <qtextureglyphcache_p.h>

Inheritance diagram for QTextureGlyphCache:
QFontEngineGlyphCache QSharedData QImageTextureGlyphCache QGLTextureGlyphCache

Classes

struct  Coord
 
struct  GlyphAndSubPixelPosition
 

Public Functions

void createCache (int width, int height)
 
virtual void createTextureData (int width, int height)=0
 
void fillInPendingGlyphs ()
 
virtual void fillTexture (const Coord &coord, glyph_t glyph, QFixed subPixelPosition)=0
 
virtual int glyphMargin () const
 
virtual int glyphPadding () const
 
bool isNull () const
 
virtual int maxTextureHeight () const
 
virtual int maxTextureWidth () const
 
bool populate (QFontEngine *fontEngine, int numGlyphs, const glyph_t *glyphs, const QFixedPoint *positions)
 
 QTextureGlyphCache (QFontEngineGlyphCache::Type type, const QTransform &matrix)
 
void resizeCache (int width, int height)
 
virtual void resizeTextureData (int width, int height)=0
 
QFixed subPixelPositionForX (QFixed x) const
 
QImage textureMapForGlyph (glyph_t g, QFixed subPixelPosition) const
 
virtual ~QTextureGlyphCache ()
 
- Public Functions inherited from QFontEngineGlyphCache
Type cacheType () const
 
 QFontEngineGlyphCache (const QTransform &matrix, Type type)
 
virtual ~QFontEngineGlyphCache ()
 
- Public Functions inherited from QSharedData
 QSharedData ()
 Constructs a QSharedData object with a reference count of 0. More...
 
 QSharedData (const QSharedData &)
 Constructs a QSharedData object with reference count 0. More...
 

Public Variables

QHash< GlyphAndSubPixelPosition, Coordcoords
 
- Public Variables inherited from QFontEngineGlyphCache
QTransform m_transform
 
QFontEngineGlyphCache::Type m_type
 
- Public Variables inherited from QSharedData
QAtomicInt ref
 

Protected Functions

int calculateSubPixelPositionCount (glyph_t) const
 

Protected Variables

QFontEnginem_current_fontengine
 
int m_currentRowHeight
 
int m_cx
 
int m_cy
 
int m_h
 
QHash< GlyphAndSubPixelPosition, Coordm_pendingGlyphs
 
int m_subPixelPositionCount
 
int m_w
 

Additional Inherited Members

- Public Types inherited from QFontEngineGlyphCache
enum  Type { Raster_RGBMask, Raster_A8, Raster_Mono }
 

Detailed Description

Definition at line 79 of file qtextureglyphcache_p.h.

Constructors and Destructors

◆ QTextureGlyphCache()

QTextureGlyphCache::QTextureGlyphCache ( QFontEngineGlyphCache::Type  type,
const QTransform matrix 
)
inline

◆ ~QTextureGlyphCache()

virtual QTextureGlyphCache::~QTextureGlyphCache ( )
inlinevirtual

Definition at line 87 of file qtextureglyphcache_p.h.

87 { }

Functions

◆ calculateSubPixelPositionCount()

int QTextureGlyphCache::calculateSubPixelPositionCount ( glyph_t  glyph) const
protected

Definition at line 68 of file qtextureglyphcache.cpp.

Referenced by populate().

69 {
70  // Test 12 different subpixel positions since it factors into 3*4 so it gives
71  // the coverage we need.
72 
73  QList<QImage> images;
74  for (int i=0; i<12; ++i) {
75  QImage img = textureMapForGlyph(glyph, QFixed::fromReal(i / 12.0));
76 
77  if (images.isEmpty()) {
78  QPainterPath path;
79  QFixedPoint point;
80  m_current_fontengine->addGlyphsToPath(&glyph, &point, 1, &path, QTextItem::RenderFlags());
81 
82  // Glyph is space, return 0 to indicate that we need to keep trying
83  if (path.isEmpty())
84  break;
85 
86  images.append(img);
87  } else {
88  bool found = false;
89  for (int j=0; j<images.size(); ++j) {
90  if (images.at(j) == img) {
91  found = true;
92  break;
93  }
94  }
95  if (!found)
96  images.append(img);
97  }
98  }
99 
100  return images.size();
101 }
bool isEmpty() const
Returns true if either there are no elements in this path, or if the only element is a MoveToElement;...
Definition: qpainterpath.h:392
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
static QFixed fromReal(qreal r)
Definition: qfixed_p.h:70
QImage textureMapForGlyph(glyph_t g, QFixed subPixelPosition) const
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs, QPainterPath *path, QTextItem::RenderFlags flags)
QFontEngine * m_current_fontengine
int size() const
Returns the number of items in the list.
Definition: qlist.h:137

◆ createCache()

void QTextureGlyphCache::createCache ( int  width,
int  height 
)
inline

Definition at line 128 of file qtextureglyphcache_p.h.

Referenced by fillInPendingGlyphs().

128  {
129  m_w = width;
130  m_h = height;
131  createTextureData(width, height);
132  }
virtual void createTextureData(int width, int height)=0

◆ createTextureData()

virtual void QTextureGlyphCache::createTextureData ( int  width,
int  height 
)
pure virtual

◆ fillInPendingGlyphs()

void QTextureGlyphCache::fillInPendingGlyphs ( )

Definition at line 259 of file qtextureglyphcache.cpp.

Referenced by QGL2PaintEngineExPrivate::drawCachedGlyphs(), and QRasterPaintEngine::drawCachedGlyphs().

260 {
261  if (m_pendingGlyphs.isEmpty())
262  return;
263 
264  int requiredHeight = m_h;
265  int requiredWidth = m_w; // Use a minimum size to avoid a lot of initial reallocations
266  {
268  while (iter != m_pendingGlyphs.end()) {
269  Coord c = iter.value();
270  requiredHeight = qMax(requiredHeight, c.y + c.h);
271  requiredWidth = qMax(requiredWidth, c.x + c.w);
272  ++iter;
273  }
274  }
275 
276  if (isNull() || requiredHeight > m_h || requiredWidth > m_w) {
277  if (isNull())
278  createCache(qt_next_power_of_two(requiredWidth), qt_next_power_of_two(requiredHeight));
279  else
280  resizeCache(qt_next_power_of_two(requiredWidth), qt_next_power_of_two(requiredHeight));
281  }
282 
283  {
285  while (iter != m_pendingGlyphs.end()) {
286  GlyphAndSubPixelPosition key = iter.key();
287  fillTexture(iter.value(), key.glyph, key.subPixelPosition);
288 
289  ++iter;
290  }
291  }
292 
293  m_pendingGlyphs.clear();
294 }
unsigned char c[8]
Definition: qnumeric_p.h:62
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qhash.h:348
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
const Key & key() const
Returns the current item&#39;s key as a const reference.
Definition: qhash.h:347
void resizeCache(int width, int height)
int key
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
virtual void fillTexture(const Coord &coord, glyph_t glyph, QFixed subPixelPosition)=0
void createCache(int width, int height)
static int qt_next_power_of_two(int v)
QHash< GlyphAndSubPixelPosition, Coord > m_pendingGlyphs

◆ fillTexture()

virtual void QTextureGlyphCache::fillTexture ( const Coord coord,
glyph_t  glyph,
QFixed  subPixelPosition 
)
pure virtual

◆ glyphMargin()

virtual int QTextureGlyphCache::glyphMargin ( ) const
inlinevirtual

Reimplemented in QImageTextureGlyphCache.

Definition at line 123 of file qtextureglyphcache_p.h.

Referenced by QImageTextureGlyphCache::fillTexture(), populate(), and textureMapForGlyph().

123 { return 0; }

◆ glyphPadding()

virtual int QTextureGlyphCache::glyphPadding ( ) const
inlinevirtual

Reimplemented in QGLTextureGlyphCache.

Definition at line 124 of file qtextureglyphcache_p.h.

Referenced by populate().

124 { return 0; }

◆ isNull()

bool QTextureGlyphCache::isNull ( ) const
inline

Definition at line 141 of file qtextureglyphcache_p.h.

Referenced by QGLTextureGlyphCache::createTextureData(), and fillInPendingGlyphs().

141 { return m_h == 0; }

◆ maxTextureHeight()

virtual int QTextureGlyphCache::maxTextureHeight ( ) const
inlinevirtual

Reimplemented in QGLTextureGlyphCache.

Definition at line 145 of file qtextureglyphcache_p.h.

Referenced by QGLTextureGlyphCache::maxTextureHeight(), and populate().

145 { return -1; }

◆ maxTextureWidth()

virtual int QTextureGlyphCache::maxTextureWidth ( ) const
inlinevirtual

Reimplemented in QGLTextureGlyphCache.

Definition at line 144 of file qtextureglyphcache_p.h.

Referenced by QGLTextureGlyphCache::maxTextureWidth(), and populate().

#define QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH

◆ populate()

bool QTextureGlyphCache::populate ( QFontEngine fontEngine,
int  numGlyphs,
const glyph_t glyphs,
const QFixedPoint positions 
)

Definition at line 120 of file qtextureglyphcache.cpp.

Referenced by QGL2PaintEngineExPrivate::drawCachedGlyphs(), and QRasterPaintEngine::drawCachedGlyphs().

122 {
123 #ifdef CACHE_DEBUG
124  printf("Populating with %d glyphs\n", numGlyphs);
125  qDebug() << " -> current transformation: " << m_transform;
126 #endif
127 
128  m_current_fontengine = fontEngine;
129  const int margin = glyphMargin();
130  const int paddingDoubled = glyphPadding() * 2;
131 
132  bool supportsSubPixelPositions = fontEngine->supportsSubPixelPositions();
133  if (m_subPixelPositionCount == 0) {
134  if (!supportsSubPixelPositions) {
136  } else {
137 #if !defined(Q_WS_X11)
138  int i = 0;
139  while (m_subPixelPositionCount == 0 && i < numGlyphs)
141 #else
143 #endif
144  }
145  }
146 
147  QHash<GlyphAndSubPixelPosition, Coord> listItemCoordinates;
148  int rowHeight = 0;
149 
151  switch (m_type) {
152  case Raster_A8: format = QFontEngine::Format_A8; break;
153  case Raster_RGBMask: format = QFontEngine::Format_A32; break;
154  default: format = QFontEngine::Format_Mono; break;
155  }
156 
157  // check each glyph for its metrics and get the required rowHeight.
158  for (int i=0; i < numGlyphs; ++i) {
159  const glyph_t glyph = glyphs[i];
160 
161  QFixed subPixelPosition;
162  if (supportsSubPixelPositions) {
163  QFixed x = positions != 0 ? positions[i].x : QFixed();
164  subPixelPosition = subPixelPositionForX(x);
165  }
166 
167  if (coords.contains(GlyphAndSubPixelPosition(glyph, subPixelPosition)))
168  continue;
169  if (listItemCoordinates.contains(GlyphAndSubPixelPosition(glyph, subPixelPosition)))
170  continue;
171  glyph_metrics_t metrics = fontEngine->alphaMapBoundingBox(glyph, subPixelPosition, m_transform, format);
172 
173 #ifdef CACHE_DEBUG
174  printf("(%4x): w=%.2f, h=%.2f, xoff=%.2f, yoff=%.2f, x=%.2f, y=%.2f\n",
175  glyph,
176  metrics.width.toReal(),
177  metrics.height.toReal(),
178  metrics.xoff.toReal(),
179  metrics.yoff.toReal(),
180  metrics.x.toReal(),
181  metrics.y.toReal());
182 #endif
183  GlyphAndSubPixelPosition key(glyph, subPixelPosition);
184  int glyph_width = metrics.width.ceil().toInt();
185  int glyph_height = metrics.height.ceil().toInt();
186  if (glyph_height == 0 || glyph_width == 0) {
187  // Avoid multiple calls to boundingBox() for non-printable characters
188  Coord c = { 0, 0, 0, 0, 0, 0 };
189  coords.insert(key, c);
190  continue;
191  }
192  glyph_width += margin * 2 + 4;
193  glyph_height += margin * 2 + 4;
194  // align to 8-bit boundary
196  glyph_width = (glyph_width+7)&~7;
197 
198  Coord c = { 0, 0, // will be filled in later
199  glyph_width,
200  glyph_height, // texture coords
201  metrics.x.truncate(),
202  -metrics.y.truncate() }; // baseline for horizontal scripts
203 
204  listItemCoordinates.insert(key, c);
205  rowHeight = qMax(rowHeight, glyph_height);
206  }
207  if (listItemCoordinates.isEmpty())
208  return true;
209 
210  rowHeight += margin * 2 + paddingDoubled;
211 
212  if (m_w == 0) {
215  else
216  m_w = qt_next_power_of_two(fontEngine->maxCharWidth());
217  }
218 
219  // now actually use the coords and paint the wanted glyps into cache.
220  QHash<GlyphAndSubPixelPosition, Coord>::iterator iter = listItemCoordinates.begin();
221  int requiredWidth = m_w;
222  while (iter != listItemCoordinates.end()) {
223  Coord c = iter.value();
224 
225  m_currentRowHeight = qMax(m_currentRowHeight, c.h + margin * 2);
226 
227  if (m_cx + c.w > requiredWidth) {
228  int new_width = requiredWidth*2;
229  while (new_width < m_cx + c.w)
230  new_width *= 2;
231  if (new_width <= maxTextureWidth()) {
232  requiredWidth = new_width;
233  } else {
234  // no room on the current line, start new glyph strip
235  m_cx = 0;
236  m_cy += m_currentRowHeight + paddingDoubled;
237  m_currentRowHeight = c.h + margin * 2; // New row
238  }
239  }
240 
241  if (maxTextureHeight() > 0 && m_cy + c.h > maxTextureHeight()) {
242  // We can't make a cache of the required size, so we bail out
243  return false;
244  }
245 
246  c.x = m_cx;
247  c.y = m_cy;
248 
249  coords.insert(iter.key(), c);
250  m_pendingGlyphs.insert(iter.key(), c);
251 
252  m_cx += c.w + paddingDoubled;
253  ++iter;
254  }
255  return true;
256 
257 }
virtual int maxTextureWidth() const
unsigned char c[8]
Definition: qnumeric_p.h:62
QHash< GlyphAndSubPixelPosition, Coord > coords
QFixed subPixelPositionForX(QFixed x) const
#define QT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
virtual int maxTextureHeight() const
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
QFontEngineGlyphCache::Type m_type
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
virtual qreal maxCharWidth() const =0
Q_CORE_EXPORT void qDebug(const char *,...)
virtual int glyphPadding() const
bool isEmpty() const
Returns true if the hash contains no items; otherwise returns false.
Definition: qhash.h:297
int toInt() const
Definition: qfixed_p.h:76
QFixed x
Definition: qfixed_p.h:190
int truncate() const
Definition: qfixed_p.h:79
virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed, const QTransform &matrix, GlyphFormat)
QFontEngine * m_current_fontengine
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
int calculateSubPixelPositionCount(glyph_t) const
int key
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
virtual int glyphMargin() const
iterator begin()
Returns an STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:464
qreal toReal() const
Definition: qfixed_p.h:77
static int qt_next_power_of_two(int v)
QHash< GlyphAndSubPixelPosition, Coord > m_pendingGlyphs
QFixed ceil() const
Definition: qfixed_p.h:82
unsigned int glyph_t
virtual bool supportsSubPixelPositions() const

◆ resizeCache()

void QTextureGlyphCache::resizeCache ( int  width,
int  height 
)
inline

Definition at line 134 of file qtextureglyphcache_p.h.

Referenced by fillInPendingGlyphs().

135  {
136  resizeTextureData(width, height);
137  m_w = width;
138  m_h = height;
139  }
virtual void resizeTextureData(int width, int height)=0

◆ resizeTextureData()

virtual void QTextureGlyphCache::resizeTextureData ( int  width,
int  height 
)
pure virtual

◆ subPixelPositionForX()

QFixed QTextureGlyphCache::subPixelPositionForX ( QFixed  x) const

Definition at line 103 of file qtextureglyphcache.cpp.

Referenced by QGL2PaintEngineExPrivate::drawCachedGlyphs(), QRasterPaintEngine::drawCachedGlyphs(), and populate().

104 {
105  if (m_subPixelPositionCount <= 1)
106  return QFixed();
107 
108  QFixed subPixelPosition;
109  if (x != 0) {
110  subPixelPosition = x - x.floor();
111  QFixed fraction = (subPixelPosition / QFixed::fromReal(1.0 / m_subPixelPositionCount)).floor();
112 
113  // Compensate for precision loss in fixed point to make sure we are always drawing at a subpixel position over
114  // the lower boundary for the selected rasterization by adding 1/64.
115  subPixelPosition = fraction / QFixed(m_subPixelPositionCount) + QFixed::fromReal(0.015625);
116  }
117  return subPixelPosition;
118 }
static QFixed fromReal(qreal r)
Definition: qfixed_p.h:70
QFixed floor() const
Definition: qfixed_p.h:81
T floor(const T &a)

◆ textureMapForGlyph()

QImage QTextureGlyphCache::textureMapForGlyph ( glyph_t  g,
QFixed  subPixelPosition 
) const

Definition at line 296 of file qtextureglyphcache.cpp.

Referenced by calculateSubPixelPositionCount(), QGLTextureGlyphCache::fillTexture(), and QImageTextureGlyphCache::fillTexture().

297 {
298 #if defined(Q_WS_X11)
302  switch (m_type) {
303  case Raster_A8:
304  format = QFontEngineFT::Format_A8;
305  imageFormat = QImage::Format_Indexed8;
306  break;
307  case Raster_Mono:
309  imageFormat = QImage::Format_Mono;
310  break;
311  case Raster_RGBMask:
312  // impossible condition (see the if-clause above)
313  // this option is here only to silence a compiler warning
314  break;
315  };
316 
317  QFontEngineFT *ft = static_cast<QFontEngineFT*> (m_current_fontengine);
320  positions[0].x = subPixelPosition;
321 
322  if (gset && ft->loadGlyphs(gset, &g, 1, positions, format)) {
323  QFontEngineFT::Glyph *glyph = gset->getGlyph(g, subPixelPosition);
324  const int bytesPerLine = (format == QFontEngineFT::Format_Mono ? ((glyph->width + 31) & ~31) >> 3
325  : (glyph->width + 3) & ~3);
326  return QImage(glyph->data, glyph->width, glyph->height, bytesPerLine, imageFormat);
327  }
328  } else
329 #endif
331  return m_current_fontengine->alphaRGBMapForGlyph(g, subPixelPosition, glyphMargin(), m_transform);
332  else
333  return m_current_fontengine->alphaMapForGlyph(g, subPixelPosition, m_transform);
334 
335  return QImage();
336 }
Format
The following image formats are available in Qt.
Definition: qimage.h:91
virtual QImage alphaMapForGlyph(glyph_t)
TransformationType type() const
Returns the transformation type of this matrix.
virtual Type type() const =0
QFontEngineGlyphCache::Type m_type
virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t)
Glyph * getGlyph(glyph_t index, QFixed subPixelPosition=0) const
static const QCssKnownValue positions[NumKnownPositionModes - 1]
Definition: qcssparser.cpp:329
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
bool loadGlyphs(QGlyphSet *gs, const glyph_t *glyphs, int num_glyphs, const QFixedPoint *positions, GlyphFormat format=Format_Render)
QFixed x
Definition: qfixed_p.h:190
QFontEngine * m_current_fontengine
virtual int glyphMargin() const
QGlyphSet * loadTransformedGlyphSet(const QTransform &matrix)

Properties

◆ coords

QHash<GlyphAndSubPixelPosition, Coord> QTextureGlyphCache::coords

◆ m_current_fontengine

QFontEngine* QTextureGlyphCache::m_current_fontengine
protected

◆ m_currentRowHeight

int QTextureGlyphCache::m_currentRowHeight
protected

Definition at line 161 of file qtextureglyphcache_p.h.

Referenced by QGLTextureGlyphCache::clear(), and populate().

◆ m_cx

int QTextureGlyphCache::m_cx
protected

Definition at line 159 of file qtextureglyphcache_p.h.

Referenced by QGLTextureGlyphCache::clear(), and populate().

◆ m_cy

int QTextureGlyphCache::m_cy
protected

Definition at line 160 of file qtextureglyphcache_p.h.

Referenced by QGLTextureGlyphCache::clear(), and populate().

◆ m_h

int QTextureGlyphCache::m_h
protected

◆ m_pendingGlyphs

QHash<GlyphAndSubPixelPosition, Coord> QTextureGlyphCache::m_pendingGlyphs
protected

Definition at line 155 of file qtextureglyphcache_p.h.

Referenced by fillInPendingGlyphs(), and populate().

◆ m_subPixelPositionCount

int QTextureGlyphCache::m_subPixelPositionCount
protected

Definition at line 162 of file qtextureglyphcache_p.h.

Referenced by populate(), and subPixelPositionForX().

◆ m_w

int QTextureGlyphCache::m_w
protected

The documentation for this class was generated from the following files: