QPixmap shadowText(QString text, const QFont &font, QColor textColor, QColor shadowColor, QPoint offset, int radius) { //don't try to paint stuff on a future null pixmap because the text is empty if (text.isEmpty()) { return QPixmap(); } // Draw text QFontMetrics fm(font); QRect textRect = fm.boundingRect(text); QPixmap textPixmap(textRect.width(), fm.height()); textPixmap.fill(Qt::transparent); QPainter p(&textPixmap); p.setPen(textColor); p.setFont(font); // FIXME: the center alignment here is odd: the rect should be the size needed by // the text, but for some fonts and configurations this is off by a pixel or so // and "centering" the text painting 'fixes' that. Need to research why // this is the case and determine if we should be painting it differently here, // doing soething different with the boundingRect call or if it's a problem // in Qt itself p.drawText(textPixmap.rect(), Qt::AlignCenter, text); p.end(); //Draw blurred shadow QImage img(textRect.size() + QSize(radius * 2, radius * 2), QImage::Format_ARGB32_Premultiplied); img.fill(0); p.begin(&img); p.drawImage(QPoint(radius, radius), textPixmap.toImage()); p.end(); shadowBlur(img, radius, shadowColor); //Compose text and shadow int addSizeX = qMax(0, qAbs(offset.x()) - radius); int addSizeY = qMax(0, qAbs(offset.y()) - radius); QPixmap finalPixmap(img.size() + QSize(addSizeX, addSizeY)); finalPixmap.fill(Qt::transparent); p.begin(&finalPixmap); p.drawImage(qMax(0, offset.x()), qMax(0, offset.y()), img); p.drawPixmap(radius + qMax(0, -offset.x()), radius + qMax(0, -offset.y()), textPixmap); p.end(); return finalPixmap; }
TQImage *KFileIVIDesktop::buildShadow( TQPainter *p, const int align, TQColor &shadowColor ) { TQPainter pixPainter; int spread = shadowThickness(); TQPixmap textPixmap(textRect( FALSE ).width() + spread * 2 + 2, textRect( FALSE ).height() + spread * 2 + 2); textPixmap.fill(TQColor(0,0,0)); textPixmap.setMask( textPixmap.createHeuristicMask(TRUE) ); pixPainter.begin(&textPixmap); pixPainter.setPen(white); // get the pen from the root painter pixPainter.setFont(p->font()); // get the font from the root painter wordWrap()->drawText( &pixPainter, spread, spread, align | KWordWrap::Truncate ); pixPainter.end(); return new TQImage(m_shadow->makeShadow(textPixmap, shadowColor)); }
void Flags::drawLabel(QPainter& painter, const QString& layoutText, bool flagShown) { QFont font = painter.font(); QRect rect = painter.window(); // int fontSize = layoutText.length() == 2 // ? height * 7 / 10 // : height * 5 / 10; int fontSize = rect.height();// * 7 /10; font.setPixelSize(fontSize); font.setWeight(QFont::DemiBold); QFontMetrics fm = painter.fontMetrics(); int width = fm.width(layoutText); if( width > rect.width() * 2 / 3 ) { fontSize = round( (double)fontSize * ((double)rect.width()*2/3) / width ); } int smallestReadableSize = KGlobalSettings::smallestReadableFont().pixelSize(); if( fontSize < smallestReadableSize ) { fontSize = smallestReadableSize; } font.setPixelSize(fontSize); #ifdef DONT_USE_PLASMA painter.setFont(font); painter.setPen(Qt::white); painter.drawText(QRect(rect).adust(1,1,0,0), Qt::AlignCenter | Qt::AlignHCenter, layoutText); painter.setPen(Qt::black); painter.drawText(rect, Qt::AlignCenter | Qt::AlignHCenter, layoutText); #else // we init svg so that we get notification about theme change getSvg(); Plasma::Theme theme; QColor textColor = flagShown ? Qt::black : theme.color(Plasma::Theme::TextColor); QPoint offset = QPoint(0, 0); auto shadowText = [&font, &textColor, &offset](QString text) { //don't try to paint stuff on a future null pixmap because the text is empty if (text.isEmpty()) { return QPixmap(); } // Draw text QFontMetrics fm(font); QRect textRect = fm.boundingRect(text); QPixmap textPixmap(textRect.width(), fm.height()); textPixmap.fill(Qt::transparent); QPainter p(&textPixmap); p.setPen(textColor); p.setFont(font); // FIXME: the center alignment here is odd: the rect should be the size needed by // the text, but for some fonts and configurations this is off by a pixel or so // and "centering" the text painting 'fixes' that. Need to research why // this is the case and determine if we should be painting it differently here, // doing soething different with the boundingRect call or if it's a problem // in Qt itself p.drawText(textPixmap.rect(), Qt::AlignCenter, text); p.end(); return textPixmap; }; // QPixmap pixmap = Plasma::PaintUtils::texturedText(layoutText, font, svg); QPixmap labelPixmap = shadowText(layoutText); int y = round((rect.height() - labelPixmap.height()) / 2.0); int x = round((rect.width() - labelPixmap.width()) / 2.0); painter.drawPixmap(QPoint(x, y), labelPixmap); #endif }
void PanelButton::drawButtonLabel(QPainter *p) { QPixmap icon = labelIcon(); bool active = isDown() || isOn(); if (active) { icon = icon.convertToImage().smoothScale(icon.width() - 2, icon.height() - 2); } if (!m_buttonText.isEmpty() && orientation() == Horizontal) { int h = height(); int w = width(); int y = (h - icon.height())/2; p->save(); QFont f = font(); double fontPercent = m_fontPercent; if (active) { fontPercent *= .8; } f.setPixelSize(KMIN(h, KMAX(int(float(h) * m_fontPercent), 16))); QFontMetrics fm(f); p->setFont(f); /* Draw shadowed text */ bool reverse = QApplication::reverseLayout(); QPainter::TextDirection rtl = reverse ? QPainter::RTL : QPainter::LTR; if (!reverse && !icon.isNull()) { /* Draw icon */ p->drawPixmap(3, y, icon); } int tX = reverse ? 3 : icon.width() + KMIN(25, KMAX(5, fm.width('m') / 2)); int tY = fm.ascent() + ((h - fm.height()) / 2); QColor shadCol = KickerLib::shadowColor(m_textColor); // get a transparent pixmap QPainter pixPainter; QPixmap textPixmap(w, h); textPixmap.fill(QColor(0,0,0)); textPixmap.setMask(textPixmap.createHeuristicMask(true)); // draw text pixPainter.begin(&textPixmap); pixPainter.setPen(m_textColor); pixPainter.setFont(p->font()); // get the font from the root painter pixPainter.drawText(tX, tY, m_buttonText, -1, rtl); pixPainter.end(); if (!s_textShadowEngine) { KShadowSettings* shadset = new KShadowSettings(); shadset->setOffsetX(0); shadset->setOffsetY(0); shadset->setThickness(1); shadset->setMaxOpacity(96); s_textShadowEngine = new KShadowEngine(shadset); } // draw shadow QImage img = s_textShadowEngine->makeShadow(textPixmap, shadCol); p->drawImage(0, 0, img); p->save(); p->setPen(m_textColor); p->drawText(tX, tY, m_buttonText, -1, rtl); p->restore(); if (reverse && !icon.isNull()) { p->drawPixmap(w - icon.width() - 3, y, icon); } p->restore(); } else if (!icon.isNull()) { int y = (height() - icon.height()) / 2; int x = (width() - icon.width()) / 2; p->drawPixmap(x, y, icon); } if (m_drawArrow && (m_highlight || active)) { QStyle::PrimitiveElement e = QStyle::PE_ArrowUp; int arrowSize = style().pixelMetric(QStyle::PM_MenuButtonIndicator); QRect r((width() - arrowSize)/2, 0, arrowSize, arrowSize); switch (m_arrowDirection) { case KPanelExtension::Top: e = QStyle::PE_ArrowUp; break; case KPanelExtension::Bottom: e = QStyle::PE_ArrowDown; r.moveBy(0, height() - arrowSize); break; case KPanelExtension::Right: e = QStyle::PE_ArrowRight; r = QRect(width() - arrowSize, (height() - arrowSize)/2, arrowSize, arrowSize); break; case KPanelExtension::Left: e = QStyle::PE_ArrowLeft; r = QRect(0, (height() - arrowSize)/2, arrowSize, arrowSize); break; case KPanelExtension::Floating: if (orientation() == Horizontal) { e = QStyle::PE_ArrowDown; r.moveBy(0, height() - arrowSize); } else if (QApplication::reverseLayout()) { e = QStyle::PE_ArrowLeft; r = QRect(0, (height() - arrowSize)/2, arrowSize, arrowSize); } else { e = QStyle::PE_ArrowRight; r = QRect(width() - arrowSize, (height() - arrowSize)/2, arrowSize, arrowSize); } break; } int flags = QStyle::Style_Enabled; if (active) { flags |= QStyle::Style_Down; } style().drawPrimitive(e, p, r, colorGroup(), flags); } }
/** Take data from image, draw text at x|y with specified parameters. If destPainter is null, draw to image, if destPainter is not null, draw directly using the painter. Returns modified area of image. */ QRect InsertTextWidget::composeImage(DImg* const image, QPainter* const destPainter, int x, int y, QFont font, float pointSize, int textRotation, QColor textColor, int textOpacity, int alignMode, const QString& textString, bool transparentBackground, QColor backgroundColor, BorderMode borderMode, int borderWidth, int spacing, float fontScale) { /* The problem we have to solve is that we have no pixel access to font rendering, we have to let Qt do the drawing. On the other hand we need to support 16 bit, which cannot be done with QPixmap. The current solution cuts out the text area, lets Qt do its drawing, converts back and blits to original. */ int maxWidth, maxHeight; if (x == -1 && y == -1) { maxWidth = image->width(); maxHeight = image->height(); } else { maxWidth = image->width() - x; maxHeight = image->height() - y; } fontScale = qMax(0.01f, fontScale); // find out size of the area that we are drawing to font.setPointSizeF(pointSize); QFontMetrics fontMt(font); QRect fontRect = fontMt.boundingRect(0, 0, qRound(maxWidth / fontScale), qRound(maxHeight / fontScale), alignMode, textString); fontRect.setWidth(qRound(fontRect.width() * fontScale)); fontRect.setHeight(qRound(fontRect.height() * fontScale)); if (!fontRect.isValid()) { return QRect(); } int fontWidth, fontHeight; switch (textRotation) { case ROTATION_NONE: case ROTATION_180: default: fontWidth = fontRect.width(); fontHeight = fontRect.height(); break; case ROTATION_90: case ROTATION_270: fontWidth = fontRect.height(); fontHeight = fontRect.width(); break; } // x, y == -1 means that we have to find a good initial position for the text here if (x == -1 && y == -1) { int boxWidth = fontWidth + 2 * borderWidth + 2 * spacing; int boxHeight = fontHeight + 2 * borderWidth + 2 * spacing; // was a valid position hint stored from last use? if (d->positionHint.isValid()) { // We assume that people tend to orient text along the edges, // so we do some guessing so that positions such as "in the lower right corner" // will be remembered across different image sizes. // get relative positions float fromTop = (float)d->positionHint.top() / 10000.0; float fromBottom = 1.0 - (float)d->positionHint.bottom() / 10000.0; float fromLeft = (float)d->positionHint.left() / 10000.0; float fromRight = 1.0 - (float)d->positionHint.right() / 10000.0; // calculate horizontal position if (fromLeft < fromRight) { x = qRound(fromLeft * maxWidth); // we are placing from the smaller distance, // so if now the larger distance is actually too small, // fall back to standard placement, nothing to lose. if (x + boxWidth > maxWidth) { x = qMax( (maxWidth - boxWidth) / 2, 0); } } else { x = maxWidth - qRound(fromRight * maxWidth) - boxWidth; if ( x < 0 ) { x = qMax( (maxWidth - boxWidth) / 2, 0); } } // calculate vertical position if (fromTop < fromBottom) { y = qRound(fromTop * maxHeight); if (y + boxHeight > maxHeight) { y = qMax( (maxHeight - boxHeight) / 2, 0); } } else { y = maxHeight - qRound(fromBottom * maxHeight) - boxHeight; if ( y < 0 ) { y = qMax( (maxHeight - boxHeight) / 2, 0); } } if (! QRect(x, y, boxWidth, boxHeight). intersects(QRect(0, 0, maxWidth, maxHeight)) ) { // emergency fallback - nothing is visible x = qMax( (maxWidth - boxWidth) / 2, 0); y = qMax( (maxHeight - boxHeight) / 2, 0); } // invalidate position hint, use only once d->positionHint = QRect(); } else { // use standard position x = qMax( (maxWidth - boxWidth) / 2, 0); y = qMax( (maxHeight - boxHeight) / 2, 0); } } // create a rectangle relative to image QRect drawRect( x, y, fontWidth + 2 * borderWidth + 2 * spacing, fontHeight + 2 * borderWidth + 2 * spacing); // create a rectangle relative to textArea, excluding the border QRect textAreaBackgroundRect( borderWidth, borderWidth, fontWidth + 2 * spacing, fontHeight + 2 * spacing); // create a rectangle relative to textArea, excluding the border and spacing QRect textAreaTextRect( borderWidth + spacing, borderWidth + spacing, fontWidth, fontHeight ); // create a rectangle relative to textArea, including the border, // for drawing the rectangle, taking into account that the width of the QPen goes in and out in equal parts QRect textAreaDrawRect( borderWidth / 2, borderWidth / 2, fontWidth + borderWidth + 2 * spacing, fontHeight + borderWidth + 2 * spacing ); // cut out the text area DImg textArea = image->copy(drawRect); if (textArea.isNull()) { return QRect(); } // compose semi-transparent background over textArea DColorComposer* composer = DColorComposer::getComposer(DColorComposer::PorterDuffNone); if (transparentBackground) { DImg transparentLayer(textAreaBackgroundRect.width(), textAreaBackgroundRect.height(), textArea.sixteenBit(), true); DColor transparent(backgroundColor); transparent.setAlpha(d->transparency); if (image->sixteenBit()) { transparent.convertToSixteenBit(); } transparentLayer.fill(transparent); textArea.bitBlendImage(composer, &transparentLayer, 0, 0, transparentLayer.width(), transparentLayer.height(), textAreaBackgroundRect.x(), textAreaBackgroundRect.y()); } DImg textNotDrawn; if (textArea.sixteenBit()) { textNotDrawn = textArea.copy(); textNotDrawn.convertToEightBit(); } else { textNotDrawn = textArea; } // We have no direct pixel access to font rendering, so now we need to use Qt/X11 for the drawing // convert text area to pixmap QPixmap pixmap; if (destPainter) { // We working on tool preview, deal with CM as well pixmap = d->iface->convertToPixmap(textNotDrawn); } else { // We working on target image. Do no apply double CM adjustment here. pixmap = textNotDrawn.convertToPixmap(); } int fontScaleWidth = qRound(fontWidth / fontScale); int fontScaleHeight = qRound(fontHeight / fontScale); QPixmap textPixmap(fontScaleWidth, fontScaleHeight); textPixmap.fill(Qt::transparent); QPainter tp(&textPixmap); tp.setOpacity((qreal)textOpacity / 100.0); tp.setPen(QPen(textColor, 1)); tp.setFont(font); switch (textRotation) { case ROTATION_NONE: tp.drawText(0, 0, fontScaleWidth, fontScaleHeight, alignMode, textString); break; case ROTATION_90: tp.translate(fontScaleWidth, 0); tp.rotate(90.0); tp.drawText(0, 0, fontScaleHeight, fontScaleWidth, alignMode, textString); break; case ROTATION_180: tp.translate(fontScaleWidth, fontScaleHeight); tp.rotate(180.0); tp.drawText(0, 0, fontScaleWidth, fontScaleHeight, alignMode, textString); break; case ROTATION_270: tp.translate(0, fontScaleHeight); tp.rotate(270.0); tp.drawText(0, 0, fontScaleHeight, fontScaleWidth, alignMode, textString); break; } tp.end(); // paint on pixmap QPainter p(&pixmap); p.drawPixmap(textAreaTextRect, textPixmap.scaled(fontWidth, fontHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); // Drawing rectangle around text. if (borderMode == BORDER_NORMAL) // Decorative border using text color. { p.setPen( QPen(textColor, borderWidth, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin) ) ; p.drawRect(textAreaDrawRect); } else if (borderMode == BORDER_SUPPORT) // Make simple dot line border to help user. { p.setPen(QPen(Qt::white, 1, Qt::SolidLine)); p.drawRect(textAreaDrawRect); p.setPen(QPen(Qt::red, 1, Qt::DotLine)); p.drawRect(textAreaDrawRect); } p.end(); if (!destPainter) { // convert to QImage, then to DImg QImage pixmapImage = pixmap.toImage(); DImg textDrawn(pixmapImage.width(), pixmapImage.height(), false, true, pixmapImage.bits()); // This does not work: during the conversion, colors are altered significantly (diffs of 1 to 10 in each component), // so we cannot find out which pixels have actually been touched. /* // Compare the result of drawing with the previous version. // Set all unchanged pixels to transparent DColor color, ncolor; uchar *ptr, *nptr; ptr = textDrawn.bits(); nptr = textNotDrawn.bits(); int bytesDepth = textDrawn.bytesDepth(); int numPixels = textDrawn.width() * textDrawn.height(); for (int i = 0; i < numPixels; ++i, ptr+= bytesDepth, nptr += bytesDepth) { color.setColor(ptr, false); ncolor.setColor(nptr, false); if ( color.red() == ncolor.red() && color.green() == ncolor.green() && color.blue() == ncolor.blue()) { color.setAlpha(0); color.setPixel(ptr); } } // convert to 16 bit if needed */ textDrawn.convertToDepthOfImage(&textArea); // now compose to original: only pixels affected by drawing text and border are changed, not whole area textArea.bitBlendImage(composer, &textDrawn, 0, 0, textDrawn.width(), textDrawn.height(), 0, 0); // copy result to original image image->bitBltImage(&textArea, drawRect.x(), drawRect.y()); } else { destPainter->drawPixmap(drawRect.x(), drawRect.y(), pixmap, 0, 0, pixmap.width(), pixmap.height()); } delete composer; return drawRect; }