31 #include <QApplication>
36 #define INC_HIT_COUNT { ++HitCount; }
37 #define INC_MISS_COUNT { ++MissCount; }
38 #define DUMP_CACHE_STATS \
39 if ( HitCount != 0 && MissCount != 0 ) { \
40 int total = HitCount + MissCount; \
41 qreal hitQuote = ( 1.0 * HitCount ) / total; \
42 qDebug() << "PrerenderedLabel dtor: hits/misses/total:" \
43 << HitCount << "/" << MissCount << "/" << total \
44 << "(" << 100 * hitQuote << "% hits)"; \
48 #define INC_MISS_COUNT
49 #define DUMP_CACHE_STATS
69 m_referencePoint = point;
74 return m_referencePoint;
80 , m_font( qApp->font() )
81 , m_brush( Qt::black )
179 void PrerenderedLabel::paint()
const
184 const int Width = 1000;
185 const int Height = Width;
188 const QColor FullTransparent( 255, 255, 255, 0 );
190 QImage
pixmap( Width, Height, QImage::Format_ARGB32_Premultiplied );
191 qWarning() <<
"PrerenderedLabel::paint: using QImage for prerendered labels "
192 <<
"to work around XRender/Qt4 bug.";
194 QPixmap
pixmap( Width, Height );
198 static const QPointF Center ( 0.0, 0.0 );
199 QPointF textBottomRight;
200 QPainter painter( &
pixmap );
201 painter.setRenderHint(QPainter::TextAntialiasing,
true );
202 painter.setRenderHint(QPainter::Antialiasing,
true );
205 painter.setPen( FullTransparent );
206 painter.setBrush( FullTransparent );
207 QPainter::CompositionMode mode = painter.compositionMode();
208 painter.setCompositionMode( QPainter::CompositionMode_Clear );
209 painter.drawRect( 0, 0, Width, Height );
210 painter.setCompositionMode( mode );
213 matrix.translate( 0.5 * Width, 0.5 * Height );
214 matrix.rotate( m_angle );
215 painter.setWorldMatrix( matrix );
217 painter.setPen( m_pen );
218 painter.setBrush( m_brush );
219 painter.setFont( m_font );
220 QRectF container( -0.5 * Width, -0.5 * Height, Width, 0.5 * Height );
221 painter.drawText( container, Qt::AlignHCenter | Qt::AlignBottom,
222 m_text, &boundingRect );
223 m_referenceBottomLeft = QPointF( boundingRect.bottomLeft().x(), 0.0 );
224 textBottomRight = QPointF( boundingRect.bottomRight().x(), 0.0 );
225 m_textAscendVector = boundingRect.topRight() - textBottomRight;
226 m_textBaseLineVector = textBottomRight - m_referenceBottomLeft;
229 boundingRect = matrix.mapRect( boundingRect );
230 m_referenceBottomLeft = matrix.map( m_referenceBottomLeft )
231 - boundingRect.topLeft();
232 textBottomRight = matrix.map( textBottomRight )
233 - boundingRect.topLeft();
234 m_textAscendVector = matrix.map( m_textAscendVector )
235 - matrix.map( Center );
236 m_textBaseLineVector = matrix.map( m_textBaseLineVector )
237 - matrix.map( Center );
242 QPixmap temp(
static_cast<int>( boundingRect.width() ),
243 static_cast<int>( boundingRect.height() ) );
245 temp.fill( FullTransparent );
246 QPainter painter( &temp );
248 painter.drawImage( QPointF( 0.0, 0.0 ),
pixmap, boundingRect );
250 painter.drawPixmap( QPointF( 0.0, 0.0 ),
pixmap, boundingRect );
253 #ifdef PRERENDEREDLABEL_DEBUG
254 painter.setPen( QPen( Qt::red, 2 ) );
255 painter.setBrush( Qt::red );
268 static const double Radius = 0.5;
269 static const double Diameter = 2 * Radius;
272 painter.drawEllipse( QRectF( point - QPointF( Radius, Radius ),
273 QSizeF( Diameter, Diameter ) ) );
297 return m_referenceBottomLeft + 0.5 * m_textBaseLineVector + 0.5 * m_textAscendVector;
299 return m_referenceBottomLeft + m_textAscendVector;
301 return m_referenceBottomLeft + 0.5 * m_textBaseLineVector + m_textAscendVector;
303 return m_referenceBottomLeft + m_textBaseLineVector + m_textAscendVector;
305 return m_referenceBottomLeft + 0.5 * m_textAscendVector;
307 return m_referenceBottomLeft + m_textBaseLineVector;
309 return m_referenceBottomLeft + 0.5 * m_textBaseLineVector;
311 return m_referenceBottomLeft;
313 return m_referenceBottomLeft + m_textBaseLineVector + 0.5 * m_textAscendVector;