void BitmapFont::calculateGlyphsWidthsAutomatically(const ImagePtr& image, const Size& glyphSize) { if(!image) return; int numHorizontalGlyphs = image->getSize().width() / glyphSize.width(); auto texturePixels = image->getPixels(); // small AI to auto calculate pixels widths for(int glyph = m_firstGlyph; glyph< 256; ++glyph) { Rect glyphCoords(((glyph - m_firstGlyph) % numHorizontalGlyphs) * glyphSize.width(), ((glyph - m_firstGlyph) / numHorizontalGlyphs) * glyphSize.height(), glyphSize.width(), m_glyphHeight); int width = glyphSize.width(); for(int x = glyphCoords.left(); x <= glyphCoords.right(); ++x) { int filledPixels = 0; // check if all vertical pixels are alpha for(int y = glyphCoords.top(); y <= glyphCoords.bottom(); ++y) { if(texturePixels[(y * image->getSize().width() * 4) + (x*4) + 3] != 0) filledPixels++; } if(filledPixels > 0) width = x - glyphCoords.left() + 1; } // store glyph size m_glyphsSize[glyph].resize(width, m_glyphHeight); } }
bool VCard_LDIFCreator::createImageSmall() { text = name + '\n' + text; if ( !mFont ) { QString pixmap = KStandardDirs::locate( "data", "kio_thumbnail/pics/thumbnailfont_7x4.png" ); if ( pixmap.isEmpty() ) { kWarning() <<"VCard_LDIFCreator: Font image \"thumbnailfont_7x4.png\" not found!"; return false; } mFont = new QPixmap( pixmap ); } QSize chSize(4, 7); // the size of one char int xOffset = chSize.width(); int yOffset = chSize.height(); // calculate a better border so that the text is centered int canvasWidth = pixmapSize.width() - 2 * xborder; int canvasHeight = pixmapSize.height() - 2 * yborder; int numCharsPerLine = (int) (canvasWidth / chSize.width()); int numLines = (int) (canvasHeight / chSize.height()); // render the information QRect rect; int rest = mPixmap.width() - (numCharsPerLine * chSize.width()); xborder = qMax( xborder, rest / 2 ); // center horizontally rest = mPixmap.height() - (numLines * chSize.height()); yborder = qMax( yborder, rest / 2 ); // center vertically // end centering int x = xborder, y = yborder; // where to paint the characters int posNewLine = mPixmap.width() - (chSize.width() + xborder); int posLastLine = mPixmap.height() - (chSize.height() + yborder); bool newLine = false; Q_ASSERT( posNewLine > 0 ); for ( int i = 0; i < text.length(); ++i ) { if ( x > posNewLine || newLine ) { // start a new line? x = xborder; y += yOffset; if ( y > posLastLine ) // more text than space break; // after starting a new line, we also jump to the next // physical newline in the file if we don't come from one if ( !newLine ) { int pos = text.indexOf( '\n', i ); if ( pos > (int) i ) i = pos +1; } newLine = false; } // check for newlines in the text (unix,dos) QChar ch = text.at( i ); if ( ch == '\n' ) { newLine = true; continue; } else if ( ch == '\r' && text.at(i+1) == '\n' ) { newLine = true; i++; // skip the next character (\n) as well continue; } rect = glyphCoords( (unsigned char)ch.toLatin1(), mFont->width() ); if ( !rect.isEmpty() ) { QPainter p( &mPixmap ); p.drawPixmap( QPoint( x, y ), *mFont, rect ); // bitBlt( &mPixmap, QPoint(x,y), mFont, rect ); } x += xOffset; // next character } return true; }