void TextRendererPrivate::clearBitmapCache() { #ifdef NATRON_TEXT_RENDERER_USE_CACHE for (BitmapCache::iterator it = _bitmapsCache.begin(); it!= _bitmapsCache.end(); ++it) { delete it.value(); } _bitmapsCache.clear(); #endif }
CharBitmap * TextRendererPrivate::createCharacter(QChar c) { #ifdef NATRON_TEXT_RENDERER_USE_CACHE ushort unic = c.unicode(); //c is already in the cache BitmapCache::iterator it = _bitmapsCache.find(unic); if ( it != _bitmapsCache.end() ) { return it.value(); } #endif if ( _usedTextures.empty() ) { newTransparentTexture(); } GLuint texture = _usedTextures.back(); GLsizei width = _fontMetrics.width(c); GLsizei height = _fontMetrics.height(); //render into a new transparant pixmap using QPainter QImage image(width, height, QImage::Format_ARGB32_Premultiplied); image.fill(Qt::transparent); QPainter painter; painter.begin(&image); painter.setRenderHints(QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing); painter.setFont(_font); painter.setPen(Qt::white); painter.drawText(0, _fontMetrics.ascent(), c); painter.end(); //fill the texture with the QImage image = QGLWidget::convertToGLFormat(image); glCheckError(); GLuint savedTexture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&savedTexture); glBindTexture(GL_TEXTURE_2D, texture); assert( glIsTexture(texture) ); glTexSubImage2D( GL_TEXTURE_2D, 0, _xOffset, _yOffset, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image.bits() ); glCheckError(); glBindTexture(GL_TEXTURE_2D, savedTexture); CharBitmap *character = new CharBitmap; character->texID = texture; character->w = width; character->h = height; character->xTexCoords[0] = (GLfloat)_xOffset / TEXTURE_SIZE; character->xTexCoords[1] = (GLfloat)(_xOffset + width) / TEXTURE_SIZE; character->yTexCoords[0] = (GLfloat)_yOffset / TEXTURE_SIZE; character->yTexCoords[1] = (GLfloat)(_yOffset + height) / TEXTURE_SIZE; #ifdef NATRON_TEXT_RENDERER_USE_CACHE BitmapCache::iterator retIt = _bitmapsCache.insert(unic, character); // insert a new charactr assert( retIt != _bitmapsCache.end() ); #endif _xOffset += width; if (_xOffset + width >= TEXTURE_SIZE) { _xOffset = 1; _yOffset += height; } if (_yOffset + height >= TEXTURE_SIZE) { newTransparentTexture(); _yOffset = 1; } #ifdef NATRON_TEXT_RENDERER_USE_CACHE return retIt.value(); #else return character; #endif } // createCharacter