summaryrefslogtreecommitdiffstats
path: root/kchart/kdchart/KDDrawText.cpp
blob: 09c0bb7856716db1151650d26c3f8c3afa711664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* -*- Mode: C++ -*-
   KDChart - a multi-platform charting engine
   */

/****************************************************************************
 ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB.  All rights reserved.
 **
 ** This file is part of the KDChart library.
 **
 ** This file may be distributed and/or modified under the terms of the
 ** GNU General Public License version 2 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.
 **
 ** Licensees holding valid commercial KDChart licenses may use this file in
 ** accordance with the KDChart Commercial License Agreement provided with
 ** the Software.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ** See http://www.klaralvdalens-datakonsult.se/?page=products for
 **   information about KDChart Commercial License Agreements.
 **
 ** Contact [email protected] if any conditions of this
 ** licensing are not clear to you.
 **
 **********************************************************************/
#include <tqpainter.h>
#include <tqbitmap.h>
#include <tqpixmap.h>
#include <math.h>
#include <limits.h>

#include <KDDrawText.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

void KDDrawText::drawRotatedText( TQPainter* painter,
        float  degrees,
        TQPoint anchor,
        const TQString& text,
        const TQFont* font,
        int align,
        bool showAnchor,
        const TQFontMetrics* fontMet,
        bool noFirstrotate,
        bool noBackrotate,
        KDDrawTextRegionAndTrueRect* infos,
        bool optimizeOutputForScreen )
{
    drawRotatedTxt( painter,
            optimizeOutputForScreen,
            degrees,
            anchor,
            text,
            font,
            align,
            showAnchor,
            INT_MAX,
            INT_MAX,
            fontMet,
            false,
            0 != infos,
            noFirstrotate,
            noBackrotate,
            infos );
}


KDDrawTextRegionAndTrueRect KDDrawText::measureRotatedText(
        TQPainter* painter,
        float  degrees,
        TQPoint anchor,
        const TQString& text,
        const TQFont* font,
        int align,
        const TQFontMetrics* fontMet,
        bool noFirstrotate,
        bool noBackrotate,
        int addPercentOfHeightToRegion )
{
    KDDrawTextRegionAndTrueRect infos;
    drawRotatedTxt( painter,
            false,
            degrees,
            anchor,
            text,
            font,
            align,
            false,
            INT_MAX,
            INT_MAX,
            fontMet,
            true,
            false,
            noFirstrotate,
            noBackrotate,
            &infos,
            addPercentOfHeightToRegion );
    return infos;
}


void KDDrawText::drawRotatedTxt( TQPainter* painter,
        bool optimizeOutputForScreen,
        float  degrees,
        TQPoint anchor,
        const TQString& text,
        const TQFont* font,
        int align,
        bool showAnchor,
        int txtWidth,
        int txtHeight,
        const TQFontMetrics* fontMet,
        bool calculateOnly,
        bool doNotCalculate,
        bool noFirstrotate,
        bool noBackrotate,
        KDDrawTextRegionAndTrueRect* infos,
        int addPercentOfHeightToRegion )
{
 
//    showAnchor=true;
  //tqDebug("\nanchor: "+ text + " / "+TQString::number(anchor.x())
  //         +" / "+TQString::number(anchor.y()));
    bool useInfos = doNotCalculate && infos;
    bool fontChanged = ( 0 != font );
    TQFont oldFont;
    if( fontChanged ) {
        oldFont = painter->font();
        painter->setFont( *font );
    }
    else
        font = static_cast<const TQFont*>(&painter->font());

    bool mustBackrotate = false;
    if( !optimizeOutputForScreen && !noFirstrotate ){
        painter->rotate( degrees );
        if( !noBackrotate )
            mustBackrotate = true;
    }

    TQPoint pos = useInfos ? infos->pos : painter->xFormDev( anchor );

    if( useInfos )
    {
        txtWidth  = infos->width;
        txtHeight = infos->height;
    }
    else
    {
        int newHeight=0;

        // a bug in the AIX 5.2 compiler means using (?:) syntax doesn't work here
        // therefor we do it the following way:
        TQFontMetrics* pFM=0;
        if( ! pFM ) {
            pFM = new TQFontMetrics( painter->fontMetrics() );
        } else {
            pFM = const_cast<TQFontMetrics*>(fontMet);
        }

        int nLF = text.contains('\n');
        if( INT_MAX == txtWidth ) {
            if( nLF ){
                int tw;
                txtWidth = 0;
                int i0  = 0;
                int iLF = text.find('\n');
                while( -1 != iLF ){
                    const TQRect r(pFM->boundingRect( text.mid(i0, iLF-i0) ));
                    tw = r.width()+ 2;
                    newHeight = r.height();
                    if( tw > txtWidth )
                        txtWidth = tw;
                    i0 = iLF+1;
                    iLF = text.find('\n', i0);
                }
                if( iLF < (int)text.length() ){
                    const TQRect r(pFM->boundingRect( text.mid( i0 ) ));
                    tw = r.width()+2;
                    newHeight = r.height();
                    if( tw > txtWidth )
                        txtWidth = tw;
                    i0 = iLF+1;
                }
            }else{
                const TQRect r(painter->boundingRect( 0,0,1,1, TQt::AlignAuto, text ));
                // correct width and height before painting with 2 unit to avoid truncating.
                // PENDING Michel - improve
                txtWidth  = r.width()+2;
                newHeight = r.height()+2;
            }
        }
        if( INT_MAX == txtWidth || INT_MAX == txtHeight ) {
            txtHeight = newHeight ? newHeight : pFM->height() * (1+nLF);
        }
        if( pFM != fontMet )
            delete pFM;
        if( infos ) {
            infos->pos    = pos;
            // PENDING infos
            infos->width = txtWidth;
            infos->height = txtHeight;
        }
    }
    if( showAnchor ) {
        int d = txtHeight/4;
        TQPen savePen = painter->pen();
        painter->setPen( TQColor( TQt::darkRed ) );
        painter->drawLine( pos.x(),   pos.y()-d,
                           pos.x(),   pos.y()+d );
        painter->drawLine( pos.x()-d, pos.y(),
                           pos.x()+d, pos.y() );
        painter->setPen( savePen );
    }
    int x = useInfos ? infos->x : pos.x();
    int y = useInfos ? infos->y : pos.y();
    //tqDebug("1.:     (x / y) :" +  text + " / "+TQString::number(x)
    //       +" / "+TQString::number(y));
    //tqDebug("2.:     (posx / posy) :" +  text );
    // tqDebug ( "%d", pos.x() ); tqDebug ( "%d", pos.y() );
    //tqDebug("3.:     (infosx / infosy) :" +  text + " / "+TQString::number(infos->x)
    //           +" / "+TQString::number(infos->y));

    if( !useInfos && !optimizeOutputForScreen ) {
        switch( align & ( TQt::AlignLeft | TQt::AlignRight | TQt::AlignHCenter ) ) {
              case TQt::AlignLeft:
                break;
            case TQt::AlignRight:
//tqDebug( TQPaintDeviceMetrics::logicalDpiX() );
                x -= txtWidth;
                break;
            case TQt::AlignHCenter:
                x -= txtWidth - txtWidth/2;
                break;
        }
        switch( align & ( TQt::AlignTop | TQt::AlignBottom | TQt::AlignVCenter ) ) {
            case TQt::AlignTop:
                break;
            case TQt::AlignBottom:
                y -= txtHeight;
                break;
            case TQt::AlignVCenter:
                y -= txtHeight/2;
                break;
        }
    }
    if( infos && !useInfos ) {
         painter->xForm( pos );
        infos->x = x - 4;
        infos->y = y - 4;
        //PENDING Michel updating info using x , y from pos 
        //tqDebug("4.:     (infosx / infosy) :" +  text + " / "+TQString::number(infos->x)
	//+" / "+TQString::number(infos->y));
        //tqDebug("5.:  (x / y) :" +  text + " / "+TQString::number(x)
	//   +" / "+TQString::number(y));
	//tqDebug("6.:  (anchorx /anchory) :" +  text + " / "+TQString::number(x)
	//   +" / "+TQString::number(y));
        TQRect rect( painter->boundingRect( x, y,
                    txtWidth, txtHeight,
                    TQt::AlignLeft + TQt::AlignTop,
                    text ) );
        //painter->fillRect (rect, TQt::blue );
        
        TQPoint topLeft(     painter->xForm( rect.topLeft()     ) );
        TQPoint topRight(    painter->xForm( rect.topRight()    ) );
        TQPoint bottomRight( painter->xForm( rect.bottomRight() ) );
        TQPoint bottomLeft(  painter->xForm( rect.bottomLeft()  ) );
      
        int additor = addPercentOfHeightToRegion * txtHeight / 100;
        TQPointArray points;
        points.setPoints( 4, topLeft.x()-additor,     topLeft.y()-additor,
                topRight.x()+additor,    topRight.y()-additor,
                bottomRight.x()+additor, bottomRight.y()+additor,
                bottomLeft.x()-additor,  bottomLeft.y()+additor );
        infos->region = TQRegion( points );
    }

    // When the TQt initialization bug is fixed the following scope
    // will be put into an "if( showAnchor )" entirely.
    {
        int d = txtHeight/4;
        TQPen savePen = painter->pen();
        if( showAnchor ) {
            painter->setPen( TQColor( TQt::blue ) );
            painter->drawLine( x,   y-d,
                               x,   y+d );
            painter->drawLine( x-d, y,
                               x+d, y );
            painter->setPen( TQColor( TQt::darkGreen ) );
            painter->drawRect(x,y,txtWidth,txtHeight);
            //painter->drawText( x, y-d, text);           
        
/*
        }else{
            // Working around a strange TQt bug: Rotated painter must be
            // initialized by drawing before text can be painted there.
            painter->setPen( TQColor( TQt::white ) );
            painter->drawLine( 30000,0,30001,0 );
*/
        }
        painter->setPen( savePen );
    }

    if( mustBackrotate && optimizeOutputForScreen ){
        painter->rotate( -degrees );
        mustBackrotate = false;
    }

    if( !calculateOnly ){
        //tqDebug("txtWidth: %i  txtHeight: %i", txtWidth, txtHeight);
        if( !optimizeOutputForScreen ){
/*
            painter->drawText( x, y,
                               txtWidth, txtHeight,
                               TQt::AlignLeft + TQt::AlignTop,
                               text );
*/
            painter->drawText( x, y,
                               txtWidth, txtHeight,
                               TQt::AlignLeft + TQt::AlignTop,
                               text );
/*
            painter->drawText( x, y,
                               text,
                               -1,
                               TQt::AlignRight + TQt::AlignTop );
*/
        }else{
            // new code (rotating the text ourselves for better quality on screens)
            TQPixmap pm( txtWidth+2, txtHeight+2, 1 );
            // note: When using colored axis labels it will be necessary
            //       to change this code and use a 256 color pixmap instead
            //       of a monochrome one.                 (khz, 2002/08/15)
            pm.fill(TQt::color0);
            TQPainter p;
            p.begin( &pm );
            if( showAnchor ){
                p.drawRect(0,0, txtWidth,txtHeight);
                p.drawLine(0,0, txtWidth,txtHeight);
                p.drawLine(0,txtHeight, txtWidth,0);
            }
            p.setFont(painter->font());

            p.drawText( 0, 0, txtWidth, txtHeight,
                        TQt::AlignLeft + TQt::AlignTop,
                        text );
/*
            p.drawText( 0,0,
                        text,
                       -1,
                        TQt::AlignLeft + TQt::AlignTop );
*/

            TQBitmap mask;
            mask = pm;
            pm.setMask( mask );
            TQWMatrix m;
            m.rotate( degrees );
            TQPixmap theRotatedPixmap = pm.xForm(m);

            // where are our four corner points now:
            double degreesRad = degrees;
            while( degreesRad > 360 )
                degreesRad -= 360;
            degreesRad *= M_PI / 180.0;
            double cosA = cos( degreesRad );
            double sinA = sin( degreesRad );
            TQPoint pTopLeft(  0,
                              0 );
            TQPoint pBotLeft(  static_cast < int > ( 0         * cosA  -  txtHeight * sinA ),
                              static_cast < int > ( txtHeight * cosA  +  0         * sinA  ) );
            TQPoint pTopRight( static_cast < int > ( txtWidth  * cosA  -  0         * sinA ),
                              static_cast < int > ( 0         * cosA  +  txtWidth  * sinA  ) );
            TQPoint pBotRight( static_cast < int > ( txtWidth  * cosA  -  txtHeight * sinA ),
                              static_cast < int > ( txtHeight * cosA  +  txtWidth  * sinA  ) );

            // make our four corner points relative
            // to the bounding rect of the rotated pixmap
            {
                TQPoint pDeltaTL( TQMIN(0, TQMIN(pBotLeft.x(), TQMIN(pTopRight.x(), pBotRight.x()))),
                                 TQMIN(0, TQMIN(pBotLeft.y(), TQMIN(pTopRight.y(), pBotRight.y()))) );
                pTopLeft  -= pDeltaTL;
                pBotLeft  -= pDeltaTL;
                pTopRight -= pDeltaTL;
                pBotRight -= pDeltaTL;
            }

            /*
            painter->setPen( TQColor( TQt::black ) );
            painter->drawLine( x-13,  y,    x+13,  y    );
            painter->drawLine( x,     y-13, x,     y+13 );
            painter->setPen( TQColor( TQt::blue ) );
            painter->drawLine( x+pTopLeft.x()-3,   y+pTopLeft.y(),   x+pTopLeft.x()+3,   y+pTopLeft.y()   );
            painter->drawLine( x+pTopLeft.x(),     y+pTopLeft.y()-3, x+pTopLeft.x(),     y+pTopLeft.y()+3 );
            painter->setPen( TQColor( TQt::red ) );
            painter->drawLine( x+pTopRight.x()-3,   y+pTopRight.y(),   x+pTopRight.x()+3,   y+pTopRight.y()   );
            painter->drawLine( x+pTopRight.x(),     y+pTopRight.y()-3, x+pTopRight.x(),     y+pTopRight.y()+3 );
            painter->setPen( TQColor( TQt::green ) );
            painter->drawLine( x+pBotLeft.x()-3,   y+pBotLeft.y(),   x+pBotLeft.x()+3,   y+pBotLeft.y()   );
            painter->drawLine( x+pBotLeft.x(),     y+pBotLeft.y()-3, x+pBotLeft.x(),     y+pBotLeft.y()+3 );
            painter->setPen( TQColor( TQt::yellow ) );
            painter->drawLine( x+pBotRight.x()-3,   y+pBotRight.y(),   x+pBotRight.x()+3,   y+pBotRight.y()   );
            painter->drawLine( x+pBotRight.x(),     y+pBotRight.y()-3, x+pBotRight.x(),     y+pBotRight.y()+3 );
            */

            // The horizontal and vertical alignment together define one of
            // NINE possible points: this point must be moved on the anchor.
            int hAlign = align & ( TQt::AlignLeft | TQt::AlignRight  | TQt::AlignHCenter );
            int vAlign = align & ( TQt::AlignTop  | TQt::AlignBottom | TQt::AlignVCenter );

            TQPoint pixPoint;
            switch( hAlign ) {
                case TQt::AlignLeft:
                    switch( vAlign ) {
                        case TQt::AlignTop:
                            pixPoint = pTopLeft;
                            break;
                        case TQt::AlignBottom:
                            pixPoint = pBotLeft;
                            break;
                        case TQt::AlignVCenter:
                        default:
                            pixPoint = TQPoint( (pTopLeft.x() + pBotLeft.x()) / 2,
                                               (pTopLeft.y() + pBotLeft.y()) / 2 );
                            break;
                    }
                    break;
                case TQt::AlignRight:
                    switch( vAlign ) {
                        case TQt::AlignTop:
                            pixPoint = pTopRight;
                            break;
                        case TQt::AlignBottom:
                            pixPoint = pBotRight;
                            break;
                        case TQt::AlignVCenter:
                        default:
                            pixPoint = TQPoint( (pTopRight.x() + pBotRight.x()) / 2,
                                               (pTopRight.y() + pBotRight.y()) / 2 );
                            break;
                    }
                    break;
                case TQt::AlignHCenter:
                default:
                    switch( vAlign ) {
                        case TQt::AlignTop:
                            pixPoint = TQPoint( (pTopLeft.x() + pTopRight.x()) / 2,
                                               (pTopLeft.y() + pTopRight.y()) / 2 );
                            break;
                        case TQt::AlignBottom:
                            pixPoint = TQPoint( (pBotLeft.x() + pBotRight.x()) / 2,
                                               (pBotLeft.y() + pBotRight.y()) / 2 );
                            break;
                        case TQt::AlignVCenter:
                        default:
                            pixPoint = TQPoint( (pTopLeft.x() + pBotRight.x()) / 2,
                                               (pTopLeft.y() + pBotRight.y()) / 2 );
                            break;
                    }
                    break;
            }
	    //tqDebug("2.:     (x / y) : "+TQString::number(x)
	    //             +" / "+TQString::number(y));
            painter->drawPixmap( TQPoint( x - pixPoint.x(),
                                         y - pixPoint.y() ),
                                 theRotatedPixmap );
            p.end();
        }
    }

    if( mustBackrotate )
        painter->rotate( -degrees );

    if( fontChanged )
        painter->setFont( oldFont );
}