35#include <KDDrawText.h>
38#define M_PI 3.14159265358979323846
41void KDDrawText::drawRotatedText( TQPainter* painter,
48 const TQFontMetrics* fontMet,
51 KDDrawTextRegionAndTrueRect* infos,
52 bool optimizeOutputForScreen )
54 drawRotatedTxt( painter,
55 optimizeOutputForScreen,
73KDDrawTextRegionAndTrueRect KDDrawText::measureRotatedText(
80 const TQFontMetrics* fontMet,
83 int addPercentOfHeightToRegion )
85 KDDrawTextRegionAndTrueRect infos;
86 drawRotatedTxt( painter,
102 addPercentOfHeightToRegion );
107void KDDrawText::drawRotatedTxt( TQPainter* painter,
108 bool optimizeOutputForScreen,
111 const TQString& text,
117 const TQFontMetrics* fontMet,
122 KDDrawTextRegionAndTrueRect* infos,
123 int addPercentOfHeightToRegion )
129 bool useInfos = doNotCalculate && infos;
130 bool fontChanged = ( 0 != font );
133 oldFont = painter->font();
134 painter->setFont( *font );
137 font =
static_cast<const TQFont*
>(&painter->font());
139 bool mustBackrotate =
false;
140 if( !optimizeOutputForScreen && !noFirstrotate ){
141 painter->rotate( degrees );
143 mustBackrotate =
true;
146 TQPoint pos = useInfos ? infos->pos : painter->xFormDev( anchor );
150 txtWidth = infos->width;
151 txtHeight = infos->height;
159 TQFontMetrics* pFM=0;
161 pFM =
new TQFontMetrics( painter->fontMetrics() );
163 pFM =
const_cast<TQFontMetrics*
>(fontMet);
166 int nLF = text.contains(
'\n');
167 if( INT_MAX == txtWidth ) {
172 int iLF = text.find(
'\n');
174 const TQRect r(pFM->boundingRect( text.mid(i0, iLF-i0) ));
176 newHeight = r.height();
180 iLF = text.find(
'\n', i0);
182 if( iLF < (
int)text.length() ){
183 const TQRect r(pFM->boundingRect( text.mid( i0 ) ));
185 newHeight = r.height();
191 const TQRect r(painter->boundingRect( 0,0,1,1, TQt::AlignAuto, text ));
194 txtWidth = r.width()+2;
195 newHeight = r.height()+2;
198 if( INT_MAX == txtWidth || INT_MAX == txtHeight ) {
199 txtHeight = newHeight ? newHeight : pFM->height() * (1+nLF);
206 infos->width = txtWidth;
207 infos->height = txtHeight;
212 TQPen savePen = painter->pen();
213 painter->setPen( TQColor( TQt::darkRed ) );
214 painter->drawLine( pos.x(), pos.y()-d,
215 pos.x(), pos.y()+d );
216 painter->drawLine( pos.x()-d, pos.y(),
217 pos.x()+d, pos.y() );
218 painter->setPen( savePen );
220 int x = useInfos ? infos->x : pos.x();
221 int y = useInfos ? infos->y : pos.y();
229 if( !useInfos && !optimizeOutputForScreen ) {
230 switch( align & ( TQt::AlignLeft | TQt::AlignRight | TQt::AlignHCenter ) ) {
233 case TQt::AlignRight:
237 case TQt::AlignHCenter:
238 x -= txtWidth - txtWidth/2;
241 switch( align & ( TQt::AlignTop | TQt::AlignBottom | TQt::AlignVCenter ) ) {
244 case TQt::AlignBottom:
247 case TQt::AlignVCenter:
252 if( infos && !useInfos ) {
253 painter->xForm( pos );
263 TQRect rect( painter->boundingRect( x, y,
265 TQt::AlignLeft + TQt::AlignTop,
269 TQPoint topLeft( painter->xForm( rect.topLeft() ) );
270 TQPoint topRight( painter->xForm( rect.topRight() ) );
271 TQPoint bottomRight( painter->xForm( rect.bottomRight() ) );
272 TQPoint bottomLeft( painter->xForm( rect.bottomLeft() ) );
274 int additor = addPercentOfHeightToRegion * txtHeight / 100;
276 points.setPoints( 4, topLeft.x()-additor, topLeft.y()-additor,
277 topRight.x()+additor, topRight.y()-additor,
278 bottomRight.x()+additor, bottomRight.y()+additor,
279 bottomLeft.x()-additor, bottomLeft.y()+additor );
280 infos->region = TQRegion( points );
287 TQPen savePen = painter->pen();
289 painter->setPen( TQColor( TQt::blue ) );
290 painter->drawLine( x, y-d,
292 painter->drawLine( x-d, y,
294 painter->setPen( TQColor( TQt::darkGreen ) );
295 painter->drawRect(x,y,txtWidth,txtHeight);
306 painter->setPen( savePen );
309 if( mustBackrotate && optimizeOutputForScreen ){
310 painter->rotate( -degrees );
311 mustBackrotate =
false;
314 if( !calculateOnly ){
316 if( !optimizeOutputForScreen ){
323 painter->drawText( x, y,
325 TQt::AlignLeft + TQt::AlignTop,
335 TQPixmap pm( txtWidth+2, txtHeight+2, 1 );
339 pm.fill(TQt::color0);
343 p.drawRect(0,0, txtWidth,txtHeight);
344 p.drawLine(0,0, txtWidth,txtHeight);
345 p.drawLine(0,txtHeight, txtWidth,0);
347 p.setFont(painter->font());
349 p.drawText( 0, 0, txtWidth, txtHeight,
350 TQt::AlignLeft + TQt::AlignTop,
364 TQPixmap theRotatedPixmap = pm.xForm(m);
367 double degreesRad = degrees;
368 while( degreesRad > 360 )
370 degreesRad *= M_PI / 180.0;
371 double cosA = cos( degreesRad );
372 double sinA = sin( degreesRad );
375 TQPoint pBotLeft(
static_cast < int > ( 0 * cosA - txtHeight * sinA ),
376 static_cast < int > ( txtHeight * cosA + 0 * sinA ) );
377 TQPoint pTopRight(
static_cast < int > ( txtWidth * cosA - 0 * sinA ),
378 static_cast < int > ( 0 * cosA + txtWidth * sinA ) );
379 TQPoint pBotRight(
static_cast < int > ( txtWidth * cosA - txtHeight * sinA ),
380 static_cast < int > ( txtHeight * cosA + txtWidth * sinA ) );
385 TQPoint pDeltaTL( TQMIN(0, TQMIN(pBotLeft.x(), TQMIN(pTopRight.x(), pBotRight.x()))),
386 TQMIN(0, TQMIN(pBotLeft.y(), TQMIN(pTopRight.y(), pBotRight.y()))) );
387 pTopLeft -= pDeltaTL;
388 pBotLeft -= pDeltaTL;
389 pTopRight -= pDeltaTL;
390 pBotRight -= pDeltaTL;
413 int hAlign = align & ( TQt::AlignLeft | TQt::AlignRight | TQt::AlignHCenter );
414 int vAlign = align & ( TQt::AlignTop | TQt::AlignBottom | TQt::AlignVCenter );
423 case TQt::AlignBottom:
426 case TQt::AlignVCenter:
428 pixPoint = TQPoint( (pTopLeft.x() + pBotLeft.x()) / 2,
429 (pTopLeft.y() + pBotLeft.y()) / 2 );
433 case TQt::AlignRight:
436 pixPoint = pTopRight;
438 case TQt::AlignBottom:
439 pixPoint = pBotRight;
441 case TQt::AlignVCenter:
443 pixPoint = TQPoint( (pTopRight.x() + pBotRight.x()) / 2,
444 (pTopRight.y() + pBotRight.y()) / 2 );
448 case TQt::AlignHCenter:
452 pixPoint = TQPoint( (pTopLeft.x() + pTopRight.x()) / 2,
453 (pTopLeft.y() + pTopRight.y()) / 2 );
455 case TQt::AlignBottom:
456 pixPoint = TQPoint( (pBotLeft.x() + pBotRight.x()) / 2,
457 (pBotLeft.y() + pBotRight.y()) / 2 );
459 case TQt::AlignVCenter:
461 pixPoint = TQPoint( (pTopLeft.x() + pBotRight.x()) / 2,
462 (pTopLeft.y() + pBotRight.y()) / 2 );
469 painter->drawPixmap( TQPoint( x - pixPoint.x(),
477 painter->rotate( -degrees );
480 painter->setFont( oldFont );