summaryrefslogtreecommitdiffstats
path: root/lib/kformula/fontstyle.cc
blob: 0d1792e05e00efe67d6338f67535bcad3277e428 (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
/* This file is part of the KDE project
   Copyright (C) 2003 Ulrich Kuettler <ulrich.kuettler@gmx.de>
   Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqpainter.h>
#include <tqpen.h>
#include <tqfontdatabase.h>
#include <tqapplication.h>

#include <kstaticdeleter.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kio/netaccess.h>
#include <kio/job.h>
#include <kmessagebox.h>

#include "fontstyle.h"


KFORMULA_NAMESPACE_BEGIN

#include "unicodetable.cc"

bool FontStyle::m_installed = false;

bool FontStyle::init( ContextStyle* style, bool install )
{
    if (!m_installed && install)
        installFonts();
    m_symbolTable.init( style->getMathFont() );

    return true;
}

// Cache the family list from TQFontDatabase after fixing it up (no foundry, lowercase)
class FontList {
public:
    FontList() {
        TQFontDatabase db;
        const TQStringList lst = db.tqfamilies();
        for ( TQStringList::const_iterator it = lst.begin(), end = lst.end() ; it != end ; ++it ) {
            const TQString name = *it;
            int i = name.tqfind('[');
            TQString family = name;
            // Remove foundry
            if ( i > -1 ) {
                const int li = name.tqfindRev(']');
                if (i < li) {
                    if (name[i - 1] == ' ')
                        i--;
                    family = name.left(i);
                }
            }
            m_fontNames.append( family.lower() );
        }
    }
    bool hasFont( const TQString& fontName ) const {
        return m_fontNames.tqfind( fontName ) != m_fontNames.end();
    }
    TQStringList m_fontNames;
};
static FontList* s_fontList = 0;
static KStaticDeleter<FontList> s_fontList_sd;

void FontStyle::testFont( TQStringList& missing, const TQString& fontName ) {
    if ( !s_fontList )
        s_fontList_sd.setObject( s_fontList, new FontList() );
    if ( !s_fontList->hasFont( fontName ) )  {
        kdWarning(39001) << "Font '" << fontName << "' not found" << endl;
        missing.append( fontName );
    }
}


TQStringList FontStyle::missingFonts( bool install )
{
    if (!m_installed && install)
        installFonts();

    TQStringList missing = missingFontsInternal();
    return missing;
}

TQStringList FontStyle::missingFontsInternal()
{
    TQStringList missing;

    testFont( missing, "cmex10" );
    testFont( missing, "arev sans");

    return missing;
}

void FontStyle::installFonts()
{
    if (m_installed)
        return;
    TQStringList missing = missingFontsInternal();
    if (!missing.isEmpty())
    {
        TQStringList urlList;
        for (TQStringList::iterator it = missing.begin(); it != missing.end(); ++it)
        {
            if ( *it == "arev sans" ) {
                if (!KIO::NetAccess::exists("fonts:/Personal/Arev.ttf", true, NULL))
                    urlList.append(locate("data", "kformula/fonts/Arev.ttf"));
                if (!KIO::NetAccess::exists("fonts:/Personal/ArevIt.ttf", true, NULL))
                    urlList.append(locate("data", "kformula/fonts/ArevIt.ttf"));
                if (!KIO::NetAccess::exists("fonts:/Personal/ArevBd.ttf", true, NULL))
                    urlList.append(locate("data", "kformula/fonts/ArevBd.ttf"));
                if (!KIO::NetAccess::exists("fonts:/Personal/ArevBI.ttf", true, NULL))
                    urlList.append(locate("data", "kformula/fonts/ArevBI.ttf"));
            }
            else {
                if (!KIO::NetAccess::exists("fonts:/Personal/" + *it + ".ttf", true, NULL))
                    urlList.append(locate("data", "kformula/fonts/" + *it + ".ttf"));
            }
        }
        KIO::copy(urlList, "fonts:/Personal/", false);
        KMessageBox::information(tqApp->mainWidget(), 
                                 i18n("Some fonts have been installed to assure that symbols in formulas are properly visualized. You must restart the application in order so that changes take effect"));
    }
    m_installed = true;
}

Artwork* FontStyle::createArtwork( SymbolType type ) const
{
    return new Artwork( type );
}

// We claim that all chars come from the same font.
// It's up to the font tables to ensure this.
const TQChar leftRoundBracket[] = {
    0x30, // uppercorner
    0x40, // lowercorner
    0x42  // line
};
const TQChar leftSquareBracket[] = {
    0x32, // uppercorner
    0x34, // lowercorner
    0x36  // line
};
const TQChar leftCurlyBracket[] = {
    0x38, // uppercorner
    0x3A, // lowercorner
    0x3E, // line
    0x3C  // middle
};

const TQChar leftLineBracket[] = {
    0x36, // line
    0x36, // line
    0x36  // line
};
const TQChar rightLineBracket[] = {
    0x37, // line
    0x37, // line
    0x37  // line
};

const TQChar rightRoundBracket[] = {
    0x31, // uppercorner
    0x41, // lowercorner
    0x43  // line
};
const TQChar rightSquareBracket[] = {
    0x33, // uppercorner
    0x35, // lowercorner
    0x37  // line
};
const TQChar rightCurlyBracket[] = {
    0x39, // uppercorner
    0x3B, // lowercorner
    0x3E, // line
    0x3D  // middle
};


static const char cmex_LeftSquareBracket = 163;
static const char cmex_RightSquareBracket = 164;
static const char cmex_LeftCurlyBracket = 169;
static const char cmex_RightCurlyBracket = 170;
static const char cmex_LeftCornerBracket = 173;
static const char cmex_RightCornerBracket = 174;
static const char cmex_LeftRoundBracket = 161;
static const char cmex_RightRoundBracket = 162;
static const char cmex_SlashBracket = 177;
static const char cmex_BackSlashBracket = 178;
//static const char cmex_LeftLineBracket = 0x4b;
//static const char cmex_RightLineBracket = 0x4b;

// use the big symbols here
static const char cmex_Int = 90;
static const char cmex_Sum = 88;
static const char cmex_Prod = 89;


// cmex is a special font with symbols in four sizes.
static short cmex_nextchar( short ch )
{
    switch ( ch ) {
    case 161: return 179;
    case 162: return 180;
    case 163: return 104;
    case 164: return 105;
    case 169: return 110;
    case 170: return 111;
    case 165: return 106;
    case 166: return 107;
    case 167: return 108;
    case 168: return 109;
    case 173: return 68;
    case 174: return 69;
    case 177: return 46;
    case 178: return 47;

    case 179: return 181;
    case 180: return 182;
    case 104: return 183;
    case 105: return 184;
    case 110: return 189;
    case 111: return 190;
    case 106: return 185;
    case 107: return 186;
    case 108: return 187;
    case 109: return 188;
    case 68: return 191;
    case 69: return 192;
    case 46: return 193;
    case 47: return 194;

    case 181: return 195;
    case 182: return 33;
    case 183: return 34;
    case 184: return 35;
    case 189: return 40;
    case 190: return 41;
    case 185: return 36;
    case 186: return 37;
    case 187: return 38;
    case 188: return 39;
    case 191: return 42;
    case 192: return 43;
    case 193: return 44;
    case 194: return 45;
    }
    return 0;
}

bool Artwork::calcCMDelimiterSize( const ContextStyle& context,
                                     uchar c,
                                     luPt fontSize,
                                     luPt tqparentSize )
{
    TQFont f( "cmex10" );
    f.setPointSizeFloat( context.tqlayoutUnitPtToPt( fontSize ) );
    TQFontMetrics fm( f );

    for ( char i=1; c != 0; ++i ) {
        LuPixelRect bound = fm.boundingRect( c );

        luPt height = context.ptToLayoutUnitPt( bound.height() );
        if ( height >= tqparentSize ) {
            luPt width = context.ptToLayoutUnitPt( fm.width( c ) );
            luPt baseline = context.ptToLayoutUnitPt( -bound.top() );

            cmChar = c;

            setHeight( height );
            setWidth( width );
            setBaseline( baseline );

            return true;
        }
        c = cmex_nextchar( c );
    }

    // Build it up from pieces.
    return false;
}


void Artwork::calcLargest( const ContextStyle& context,
                             uchar c, luPt fontSize )
{
    TQFont f( "cmex10" );
    f.setPointSizeFloat( context.tqlayoutUnitPtToPt( fontSize ) );
    TQFontMetrics fm( f );

    cmChar = c;
    for ( ;; ) {
        c = cmex_nextchar( c );
        if ( c == 0 ) {
            break;
        }
        cmChar = c;
    }

    LuPixelRect bound = fm.boundingRect( cmChar );

    luPt height = context.ptToLayoutUnitPt( bound.height() );
    luPt width = context.ptToLayoutUnitPt( fm.width( cmChar ) );
    luPt baseline = context.ptToLayoutUnitPt( -bound.top() );

    setHeight( height );
    setWidth( width );
    setBaseline( baseline );
}


void Artwork::drawCMDelimiter( TQPainter& painter, const ContextStyle& style,
                                 luPixel x, luPixel y,
                                 luPt height )
{
    TQFont f( "cmex10" );
    f.setPointSizeFloat( style.tqlayoutUnitToFontSize( height, false ) );

    painter.setFont( f );
    painter.drawText( style.tqlayoutUnitToPixelX( x ),
                      style.tqlayoutUnitToPixelY( y + getBaseline() ),
                      TQString( TQChar( cmChar ) ) );

    // Debug
#if 0
    TQFontMetrics fm( f );
    LuPixelRect bound = fm.boundingRect( cmChar );
    painter.setBrush(TQt::NoBrush);
    painter.setPen(TQt::green);
    painter.drawRect( style.tqlayoutUnitToPixelX( x ),
                      style.tqlayoutUnitToPixelY( y ),
                      fm.width( cmChar ),
                      bound.height() );
#endif
}


Artwork::Artwork(SymbolType t)
    : baseline( -1 ), type(t)
{
}


void Artwork::calcSizes( const ContextStyle& style,
                           ContextStyle::TextStyle tstyle,
                           double factor,
                           luPt tqparentSize )
{
    setBaseline( -1 );
    cmChar = -1;
    luPt mySize = style.getAdjustedSize( tstyle, factor );
    switch (getType()) {
    case LeftSquareBracket:
        if ( calcCMDelimiterSize( style, cmex_LeftSquareBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcRoundBracket( style, leftSquareBracket, tqparentSize, mySize );
        break;
    case RightSquareBracket:
        if ( calcCMDelimiterSize( style, cmex_RightSquareBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcRoundBracket( style, rightSquareBracket, tqparentSize, mySize );
        break;
    case LeftLineBracket:
        calcRoundBracket( style, leftLineBracket, tqparentSize, mySize );
        setWidth( getWidth()/2 );
        break;
    case RightLineBracket:
        calcRoundBracket( style, rightLineBracket, tqparentSize, mySize );
        setWidth( getWidth()/2 );
        break;
    case SlashBracket:
        if ( calcCMDelimiterSize( style, cmex_SlashBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcLargest( style, cmex_SlashBracket, mySize );
        break;
    case BackSlashBracket:
        if ( calcCMDelimiterSize( style, cmex_BackSlashBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcLargest( style, cmex_BackSlashBracket, mySize );
        break;
    case LeftCornerBracket:
        if ( calcCMDelimiterSize( style, cmex_LeftCornerBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcLargest( style, cmex_LeftCornerBracket, mySize );
        break;
    case RightCornerBracket:
        if ( calcCMDelimiterSize( style, cmex_RightCornerBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcLargest( style, cmex_RightCornerBracket, mySize );
        break;
    case LeftRoundBracket:
        if ( calcCMDelimiterSize( style, cmex_LeftRoundBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcRoundBracket( style, leftRoundBracket, tqparentSize, mySize );
        break;
    case RightRoundBracket:
        if ( calcCMDelimiterSize( style, cmex_RightRoundBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcRoundBracket( style, rightRoundBracket, tqparentSize, mySize );
        break;
    case EmptyBracket:
        setHeight(tqparentSize);
        //setWidth(style.getEmptyRectWidth());
        setWidth(0);
        break;
    case LeftCurlyBracket:
        if ( calcCMDelimiterSize( style, cmex_LeftCurlyBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcCurlyBracket( style, leftCurlyBracket, tqparentSize, mySize );
        break;
    case RightCurlyBracket:
        if ( calcCMDelimiterSize( style, cmex_RightCurlyBracket,
                                  mySize, tqparentSize ) ) {
            return;
        }
        calcCurlyBracket( style, rightCurlyBracket, tqparentSize, mySize );
        break;
    case Integral:
        calcCharSize( style, style.getBracketFont(), mySize, cmex_Int );
        break;
    case Sum:
        calcCharSize( style, style.getBracketFont(), mySize, cmex_Sum );
        break;
    case Product:
        calcCharSize( style, style.getBracketFont(), mySize, cmex_Prod );
        break;
    }
}

void Artwork::calcSizes( const ContextStyle& style,
                         ContextStyle::TextStyle tstyle,
                         double factor )
{
    luPt mySize = style.getAdjustedSize( tstyle, factor );
    switch (type) {
    case LeftSquareBracket:
        calcCharSize(style, mySize, leftSquareBracketChar);
        break;
    case RightSquareBracket:
        calcCharSize(style, mySize, rightSquareBracketChar);
        break;
    case LeftLineBracket:
    case RightLineBracket:
        calcCharSize(style, mySize, verticalLineChar);
        break;
    case SlashBracket:
        calcCharSize(style, mySize, slashChar);
        break;
    case BackSlashBracket:
        calcCharSize(style, mySize, backSlashChar);
        break;
    case LeftCornerBracket:
        calcCharSize(style, mySize, leftAngleBracketChar);
        break;
    case RightCornerBracket:
        calcCharSize(style, mySize, rightAngleBracketChar);
        break;
    case LeftRoundBracket:
        calcCharSize(style, mySize, leftParenthesisChar);
        break;
    case RightRoundBracket:
        calcCharSize(style, mySize, rightParenthesisChar);
        break;
    case EmptyBracket:
        //calcCharSize(style, mySize, spaceChar);
        setHeight(0);
        //setWidth(style.getEmptyRectWidth());
        setWidth(0);
        break;
    case LeftCurlyBracket:
        calcCharSize(style, mySize, leftCurlyBracketChar);
        break;
    case RightCurlyBracket:
        calcCharSize(style, mySize, rightCurlyBracketChar);
        break;
    case Integral:
    case Sum:
    case Product:
        break;
    }
}


void Artwork::draw(TQPainter& painter, const LuPixelRect& /*r*/,
                   const ContextStyle& context, ContextStyle::TextStyle tstyle,
                   StyleAttributes& style, const LuPixelPoint& tqparentOrigin)
{
    luPt mySize = context.getAdjustedSize( tstyle, style.sizeFactor() );
    luPixel myX = tqparentOrigin.x() + getX();
    luPixel myY = tqparentOrigin.y() + getY();
    /*
    if ( !LuPixelRect( myX, myY, getWidth(), getHeight() ).intersects( r ) )
        return;
    */

    painter.setPen(context.getDefaultColor());

    switch (type) {
    case LeftSquareBracket:
        drawCharacter(painter, context, myX, myY, mySize, leftSquareBracketChar);
        break;
    case RightSquareBracket:
        drawCharacter(painter, context, myX, myY, mySize, rightSquareBracketChar);
        break;
    case LeftCurlyBracket:
        drawCharacter(painter, context, myX, myY, mySize, leftCurlyBracketChar);
        break;
    case RightCurlyBracket:
        drawCharacter(painter, context, myX, myY, mySize, rightCurlyBracketChar);
        break;
    case LeftLineBracket:
    case RightLineBracket:
        drawCharacter(painter, context, myX, myY, mySize, verticalLineChar);
        break;
    case SlashBracket:
        drawCharacter(painter, context, myX, myY, mySize, slashChar);
        break;
    case BackSlashBracket:
        drawCharacter(painter, context, myX, myY, mySize, backSlashChar);
        break;
    case LeftCornerBracket:
        drawCharacter(painter, context, myX, myY, mySize, leftAngleBracketChar);
        break;
    case RightCornerBracket:
        drawCharacter(painter, context, myX, myY, mySize, rightAngleBracketChar);
        break;
    case LeftRoundBracket:
        drawCharacter(painter, context, myX, myY, mySize, leftParenthesisChar);
        break;
    case RightRoundBracket:
        drawCharacter(painter, context, myX, myY, mySize, rightParenthesisChar);
        break;
    case EmptyBracket:
        break;
    case Integral:
    case Sum:
    case Product:
        break;
    }
}

void Artwork::draw(TQPainter& painter, const LuPixelRect& ,
                     const ContextStyle& context, ContextStyle::TextStyle tstyle,
                     StyleAttributes& style, luPt , const LuPixelPoint& origin)
{
    luPt mySize = context.getAdjustedSize( tstyle, style.sizeFactor() );
    luPixel myX = origin.x() + getX();
    luPixel myY = origin.y() + getY();
    /*
    if ( !LuPixelRect( myX, myY, getWidth(), getHeight() ).intersects( r ) )
        return;
    */

    painter.setPen(context.getDefaultColor());

    switch (getType()) {
    case LeftSquareBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        else {
            drawBigRoundBracket( painter, context, leftSquareBracket, myX, myY, mySize );
        }
        break;
    case RightSquareBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        else {
            drawBigRoundBracket( painter, context, rightSquareBracket, myX, myY, mySize );
        }
        break;
    case LeftCurlyBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        else {
            drawBigCurlyBracket( painter, context, leftCurlyBracket, myX, myY, mySize );
        }
        break;
    case RightCurlyBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        else {
            drawBigCurlyBracket( painter, context, rightCurlyBracket, myX, myY, mySize );
        }
        break;
    case LeftLineBracket: {
        luPixel halfWidth = getWidth()/2;
        drawBigRoundBracket( painter, context, leftLineBracket,
                             myX-halfWidth, myY, mySize );
    }
        break;
    case RightLineBracket: {
        luPixel halfWidth = getWidth()/2;
        drawBigRoundBracket( painter, context, rightLineBracket,
                             myX-halfWidth, myY, mySize );
    }
        break;
    case SlashBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        break;
    case BackSlashBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        break;
    case LeftCornerBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        else drawCharacter(painter, context, myX, myY, mySize, leftAngleBracketChar);
        break;
    case RightCornerBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        else drawCharacter(painter, context, myX, myY, mySize, rightAngleBracketChar);
        break;
    case LeftRoundBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        else {
            drawBigRoundBracket( painter, context, leftRoundBracket, myX, myY, mySize );
        }
        break;
    case RightRoundBracket:
        if ( cmChar != -1 ) {
            drawCMDelimiter( painter, context, myX, myY, mySize );
        }
        else {
            drawBigRoundBracket( painter, context, rightRoundBracket, myX, myY, mySize );
        }
        break;
    case EmptyBracket:
        break;
    case Integral:
        drawCharacter(painter, context, TQFont( "cmex10" ), myX, myY, mySize, cmex_Int);
        break;
    case Sum:
        drawCharacter(painter, context, TQFont( "cmex10" ), myX, myY, mySize, cmex_Sum);
        break;
    case Product:
        drawCharacter(painter, context, TQFont( "cmex10" ), myX, myY, mySize, cmex_Prod);
        break;
    }

    // debug
//     painter.setBrush(TQt::NoBrush);
//     painter.setPen(TQt::green);
//     painter.drawRect( context.tqlayoutUnitToPixelX( myX ),
//                       context.tqlayoutUnitToPixelY( myY ),
//                       context.tqlayoutUnitToPixelX( getWidth() ),
//                       context.tqlayoutUnitToPixelY( getHeight() ) );
}

void Artwork::calcCharSize( const ContextStyle& style, luPt height, TQChar ch )
{
    calcCharSize( style, style.getMathFont(), height, ch );
}


void Artwork::drawCharacter( TQPainter& painter, const ContextStyle& style,
                             luPixel x, luPixel y,
                             luPt height, TQChar ch )
{
    drawCharacter( painter, style, style.getMathFont(), x, y, height, ch );
}


void Artwork::calcCharSize( const ContextStyle& style, TQFont f,
                            luPt height, TQChar c )
{
    f.setPointSizeFloat( style.tqlayoutUnitPtToPt( height ) );
    //f.setPointSize( height );
    TQFontMetrics fm(f);
    setWidth( style.ptToLayoutUnitPt( fm.width( c ) ) );
    LuPixelRect bound = fm.boundingRect( c );
    setHeight( style.ptToLayoutUnitPt( bound.height() ) );
    setBaseline( style.ptToLayoutUnitPt( -bound.top() ) );
}


void Artwork::drawCharacter( TQPainter& painter, const ContextStyle& style,
                             TQFont f,
                             luPixel x, luPixel y, luPt height, uchar c )
{
    f.setPointSizeFloat( style.tqlayoutUnitToFontSize( height, false ) );

    painter.setFont( f );
    painter.drawText( style.tqlayoutUnitToPixelX( x ),
                      style.tqlayoutUnitToPixelY( y+getBaseline() ),
                      TQString( TQChar( c ) ) );
}


void Artwork::calcRoundBracket( const ContextStyle& style, const TQChar chars[],
                                luPt height, luPt charHeight )
{
    uchar uppercorner = chars[0];
    uchar lowercorner = chars[1];
    //uchar line = style.symbolTable().character( chars[2] );

    TQFont f = style.getBracketFont();
    f.setPointSizeFloat( style.tqlayoutUnitPtToPt( charHeight ) );
    TQFontMetrics fm( f );
    LuPtRect upperBound = fm.boundingRect( uppercorner );
    LuPtRect lowerBound = fm.boundingRect( lowercorner );
    //LuPtRect lineBound = fm.boundingRect( line );

    setWidth( style.ptToLayoutUnitPt( fm.width( TQChar( uppercorner ) ) ) );
    luPt edgeHeight = style.ptToLayoutUnitPt( upperBound.height()+lowerBound.height() );
    //luPt lineHeight = style.ptToLayoutUnitPt( lineBound.height() );

    //setHeight( edgeHeight + ( ( height-edgeHeight-1 ) / lineHeight + 1 ) * lineHeight );
    setHeight( TQMAX( edgeHeight, height ) );
}

void Artwork::drawBigRoundBracket( TQPainter& p, const ContextStyle& style, const TQChar chars[],
                                   luPixel x, luPixel y, luPt charHeight )
{
    uchar uppercorner = chars[0];
    uchar lowercorner = chars[1];
    uchar line = chars[2];

    TQFont f = style.getBracketFont();
    f.setPointSizeFloat( style.tqlayoutUnitToFontSize( charHeight, false ) );
    p.setFont(f);

    TQFontMetrics fm(f);
    TQRect upperBound = fm.boundingRect(uppercorner);
    TQRect lowerBound = fm.boundingRect(lowercorner);
    TQRect lineBound = fm.boundingRect(line);

    pixel ptX = style.tqlayoutUnitToPixelX( x );
    pixel ptY = style.tqlayoutUnitToPixelY( y );
    pixel height = style.tqlayoutUnitToPixelY( getHeight() );

//     p.setPen( TQt::red );
//     //p.drawRect( ptX, ptY, upperBound.width(), upperBound.height() + lowerBound.height() );
//     p.drawRect( ptX, ptY, style.tqlayoutUnitToPixelX( getWidth() ),
//                 style.tqlayoutUnitToPixelY( getHeight() ) );

//     p.setPen( TQt::black );
    p.drawText( ptX, ptY-upperBound.top(), TQString( TQChar( uppercorner ) ) );
    p.drawText( ptX, ptY+height-lowerBound.top()-lowerBound.height(),
                TQString( TQChar( lowercorner ) ) );

    // for printing
    //pt safety = lineBound.height() / 10.0;
    pixel safety = 0;

    pixel gap = height - upperBound.height() - lowerBound.height();
    pixel lineHeight = lineBound.height() - safety;
    int lineCount = tqRound( static_cast<double>( gap ) / lineHeight );
    pixel start = upperBound.height()-lineBound.top() - safety;

    for (int i = 0; i < lineCount; i++) {
        p.drawText( ptX, ptY+start+i*lineHeight, TQString(TQChar(line)));
    }
    pixel remaining = gap - lineCount*lineHeight;
    pixel dist = ( lineHeight - remaining ) / 2;
    p.drawText( ptX, ptY+height-upperBound.height()+dist-lineBound.height()-lineBound.top(),
                TQString( TQChar( line ) ) );
}

void Artwork::calcCurlyBracket( const ContextStyle& style, const TQChar chars[],
                                luPt height, luPt charHeight )
{
    uchar uppercorner = chars[0];
    uchar lowercorner = chars[1];
    //uchar line = style.symbolTable().character( chars[2] );
    uchar middle = chars[3];

    TQFont f = style.getBracketFont();
    f.setPointSizeFloat( style.tqlayoutUnitPtToPt( charHeight ) );
    TQFontMetrics fm( f );
    LuPtRect upperBound = fm.boundingRect( uppercorner );
    LuPtRect lowerBound = fm.boundingRect( lowercorner );
    //LuPtRect lineBound = fm.boundingRect( line );
    LuPtRect middleBound = fm.boundingRect( middle );

    setWidth( style.ptToLayoutUnitPt( fm.width( TQChar( uppercorner ) ) ) );
    luPt edgeHeight = style.ptToLayoutUnitPt( upperBound.height()+
                                              lowerBound.height()+
                                              middleBound.height() );
    //luPt lineHeight = style.ptToLayoutUnitPt( lineBound.height() );

    //setHeight( edgeHeight + ( ( height-edgeHeight-1 ) / lineHeight + 1 ) * lineHeight );
    setHeight( TQMAX( edgeHeight, height ) );
}

void Artwork::drawBigCurlyBracket( TQPainter& p, const ContextStyle& style, const TQChar chars[],
                                   luPixel x, luPixel y, luPt charHeight )
{
    //TQFont f = style.getSymbolFont();
    TQFont f = style.getBracketFont();
    f.setPointSizeFloat( style.tqlayoutUnitToFontSize( charHeight, false ) );
    p.setFont(f);

    uchar uppercorner = chars[0];
    uchar lowercorner = chars[1];
    uchar line = chars[2];
    uchar middle = chars[3];

    TQFontMetrics fm(p.fontMetrics());
    TQRect upperBound = fm.boundingRect(uppercorner);
    TQRect lowerBound = fm.boundingRect(lowercorner);
    TQRect middleBound = fm.boundingRect(middle);
    TQRect lineBound = fm.boundingRect(line);

    pixel ptX = style.tqlayoutUnitToPixelX( x );
    pixel ptY = style.tqlayoutUnitToPixelY( y );
    pixel height = style.tqlayoutUnitToPixelY( getHeight() );

    //p.setPen(TQt::gray);
    //p.drawRect(x, y, upperBound.width() + offset, height);

    p.drawText( ptX, ptY-upperBound.top(), TQString( TQChar( uppercorner ) ) );
    p.drawText( ptX, ptY+(height-middleBound.height())/2-middleBound.top(),
                TQString( TQChar( middle ) ) );
    p.drawText( ptX, ptY+height-lowerBound.top()-lowerBound.height(),
                TQString( TQChar( lowercorner ) ) );

    // for printing
    // If the world was perfect and the urw-symbol font correct
    // this could be 0.
    //lu safety = lineBound.height() / 10;
    pixel safety = 0;

    pixel lineHeight = lineBound.height() - safety;
    pixel gap = height/2 - upperBound.height() - middleBound.height() / 2;

    if (gap > 0) {
        TQString ch = TQString(TQChar(line));
        int lineCount = tqRound( gap / lineHeight ) + 1;

        pixel start = (height - middleBound.height()) / 2 + safety;
        for (int i = 0; i < lineCount; i++) {
            p.drawText( ptX, ptY-lineBound.top()+TQMAX( start-(i+1)*lineHeight,
                                                       upperBound.width() ),
                        ch );
        }

        start = (height + middleBound.height()) / 2 - safety;
        for (int i = 0; i < lineCount; i++) {
            p.drawText( ptX, ptY-lineBound.top()+TQMIN( start+i*lineHeight,
                                                       height-upperBound.width()-lineBound.height() ),
                        ch );
        }
    }
}

KFORMULA_NAMESPACE_END