summaryrefslogtreecommitdiffstats
path: root/lib/kformula/contextstyle.cc
blob: a744e02dd24cdc503c35158473539d8ecfe968f4 (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
/* This file is part of the KDE project
   Copyright (C) 2001 Andrea Rizzi <[email protected]>
	              Ulrich Kuettler <[email protected]>
                 2006 Alfredo Beaumont Sainz <[email protected]>

   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 <tqfontmetrics.h>
#include <tqstring.h>

#include <kdebug.h>
#include <KoGlobal.h>

#include "contextstyle.h"
#include "fontstyle.h"


KFORMULA_NAMESPACE_BEGIN


ContextStyle::ContextStyle()
    : symbolFont( "Symbol" ),
      defaultColor(TQt::black), numberColor(TQt::blue),
      operatorColor(TQt::darkGreen), errorColor(TQt::darkRed),
      emptyColor(TQt::blue), helpColor( TQt::gray ), m_sizeFactor( 0 )
{
//     kdDebug() << "ContextStyle::ContextStyle" << endl
//               << "defaultFont: " << defaultFont.rawName() << endl
//               << "nameFont: " << nameFont.rawName() << endl
//               << "numberFont: " << numberFont.rawName() << endl
//               << "operatorFont: " << operatorFont.rawName() << endl
//               << "symbolFont: " << symbolFont.rawName() << endl;

    textStyleValues[ displayStyle      ].setup( 1. );
    textStyleValues[ textStyle         ].setup( 1. );
    textStyleValues[ scriptStyle       ].setup( .7 );
    textStyleValues[ scriptScriptStyle ].setup( .49 );

    m_baseTextStyle = displayStyle;

    lineWidth = 1;
    linearMovement = false;
    centerSymbol = true;
    m_syntaxHighlighting = true;

    m_fontStyle = 0;
}


ContextStyle::~ContextStyle()
{
    delete m_fontStyle;
}


void ContextStyle::init( bool init )
{
    setup();
    setFontStyle( m_fontStyleName, init );
}


void ContextStyle::setFontStyle( const TQString& fontStyle, bool init )
{
    delete m_fontStyle;
    m_fontStyleName = fontStyle;
    m_fontStyle = new FontStyle();
    m_fontStyle->init( this, init );
}


const SymbolTable& ContextStyle::symbolTable() const
{
    return *( m_fontStyle->symbolTable() );
}


void ContextStyle::readConfig( TDEConfig* config, bool init )
{
    config->setGroup( "kformula Font" );
    TQString fontName = config->readEntry( "defaultFont", "Times,12,-1,5,50,1,0,0,0,0" );
    defaultFont.fromString( fontName );
    fontName = config->readEntry( "nameFont", "Times,12,-1,5,50,0,0,0,0,0" );
    nameFont.fromString( fontName );
    fontName = config->readEntry( "numberFont", "Times,12,-1,5,50,0,0,0,0,0" );
    numberFont.fromString( fontName );
    fontName = config->readEntry( "operatorFont", "Times,12,-1,5,50,0,0,0,0,0" );
    operatorFont.fromString( fontName );
    TQString baseSize = config->readEntry( "baseSize", "20" );
    m_baseSize = baseSize.toInt();

    if ( ! FontStyle::missingFonts( init ).isEmpty() ) {
        kdWarning( DEBUGID) << "Not all basic fonts found\n";
    }
    mathFont.fromString("Arev Sans");
    bracketFont.fromString("cmex10");


    // There's no gui right anymore but I'll leave it here...
    config->setGroup( "kformula Color" );
    defaultColor  = config->readColorEntry( "defaultColor",  &defaultColor );
    numberColor   = config->readColorEntry( "numberColor",   &numberColor );
    operatorColor = config->readColorEntry( "operatorColor", &operatorColor );
    emptyColor    = config->readColorEntry( "emptyColor",    &emptyColor );
    errorColor    = config->readColorEntry( "errorColor",    &errorColor );
    helpColor     = config->readColorEntry( "helpColor",     &helpColor );

    m_syntaxHighlighting = config->readBoolEntry( "syntaxHighlighting", true );
}

void ContextStyle::setZoomAndResolution( int zoom, int dpiX, int dpiY )
{
    KoZoomHandler::setZoomAndResolution( zoom, dpiX, dpiY );
}

bool ContextStyle::setZoomAndResolution( int zoom, double zoomX, double zoomY, bool, bool )
{
    bool changes = m_zoom != zoom || m_zoomedResolutionX != zoomX || m_zoomedResolutionY != zoomY;
    m_zoom = zoom;
    m_zoomedResolutionX = zoomX;
    m_zoomedResolutionY = zoomY;
    return changes;
}

TQColor ContextStyle::getNumberColor()   const
{
    if ( edit() && syntaxHighlighting() ) {
        return numberColor;
    }
    return getDefaultColor();
}

TQColor ContextStyle::getOperatorColor() const
{
    if ( edit() && syntaxHighlighting() ) {
        return operatorColor;
    }
    return getDefaultColor();
}

TQColor ContextStyle::getErrorColor()    const
{
    if ( edit() && syntaxHighlighting() ) {
        return errorColor;
    }
    return getDefaultColor();
}

TQColor ContextStyle::getEmptyColor()    const
{
    if ( edit() && syntaxHighlighting() ) {
        return emptyColor;
    }
    return getDefaultColor();
}

TQColor ContextStyle::getHelpColor()     const
{
    if ( edit() && syntaxHighlighting() ) {
        return helpColor;
    }
    return getDefaultColor();
}

void ContextStyle::setDefaultColor( const TQColor& color )
{
    defaultColor = color;
}
void ContextStyle::setNumberColor( const TQColor& color )
{
    numberColor = color;
}
void ContextStyle::setOperatorColor( const TQColor& color )
{
    operatorColor = color;
}
void ContextStyle::setErrorColor( const TQColor& color )
{
    errorColor = color;
}
void ContextStyle::setEmptyColor( const TQColor& color )
{
    emptyColor = color;
}
void ContextStyle::setHelpColor( const TQColor& color )
{
    helpColor = color;
}

#if 0
const TQStringList& ContextStyle::requestedFonts() const
{
    return m_requestedFonts;
}

void ContextStyle::setRequestedFonts( const TQStringList& list )
{
    m_requestedFonts = list;
    //table.init( this );
}
#endif

double ContextStyle::getReductionFactor( TextStyle tstyle ) const
{
    return textStyleValues[ tstyle ].reductionFactor;
}

luPt ContextStyle::getAdjustedSize( TextStyle tstyle, double factor ) const
{
    return tqRound( ptToLayoutUnitPt( m_sizeFactor 
                                     * m_baseSize 
                                     * getReductionFactor( tstyle )
                                     * factor
                                     * zoom() / 100.0 ) );
}

luPixel ContextStyle::getSpace( TextStyle tstyle, SpaceWidth space, double factor ) const
{
    switch ( space ) {
    case NEGTHIN: return -getThinSpace( tstyle, factor );
    case THIN:    return getThinSpace( tstyle, factor );
    case MEDIUM:  return getMediumSpace( tstyle, factor );
    case THICK:   return getThickSpace( tstyle, factor );
    case QUAD:    return getQuadSpace( tstyle, factor );
    }
    return 0;
}

luPixel ContextStyle::getThinSpace( TextStyle tstyle, double factor ) const
{
    return ptToPixelX( m_sizeFactor
                       * textStyleValues[ tstyle ].thinSpace( quad )
                       * factor
                       * zoom() / 100.0 );
}

luPixel ContextStyle::getMediumSpace( TextStyle tstyle, double factor ) const
{
    return ptToPixelX( m_sizeFactor
                       * textStyleValues[ tstyle ].mediumSpace( quad ) 
                       * factor
                       * zoom() / 100.0 );
}

luPixel ContextStyle::getThickSpace( TextStyle tstyle, double factor ) const
{
    return ptToPixelX( m_sizeFactor
                       * textStyleValues[ tstyle ].thickSpace( quad ) 
                       * factor
                       * zoom() / 100.0 );
}

luPixel ContextStyle::getQuadSpace( TextStyle tstyle, double factor ) const
{
    return ptToPixelX( m_sizeFactor
                       * textStyleValues[ tstyle ].quadSpace( quad ) 
                       * factor
                       * zoom() / 100.0 );
}

luPixel ContextStyle::axisHeight( TextStyle tstyle, double factor ) const
{
    //return ptToPixelY( textStyleValues[ tstyle ].axisHeight( m_axisHeight ) );
    return static_cast<luPixel>( m_sizeFactor
                                 * textStyleValues[ tstyle ].axisHeight( m_axisHeight ) 
                                 * factor
                                 * zoom() / 100.0 );
}

luPt ContextStyle::getBaseSize() const
{
    return static_cast<luPt>( ptToLayoutUnitPt( m_sizeFactor*m_baseSize ) );
}

void ContextStyle::setBaseSize( int size )
{
    //kdDebug( 40000 ) << "ContextStyle::setBaseSize" << endl;
    if ( size != m_baseSize ) {
        m_baseSize = size;
        setup();
    }
}

void ContextStyle::setSizeFactor( double factor )
{
    m_sizeFactor = factor;
}


luPixel ContextStyle::getLineWidth( double factor ) const
{
    return ptToLayoutUnitPixX( m_sizeFactor*lineWidth*factor*zoom() / 100.0 );
}

luPixel ContextStyle::getEmptyRectWidth( double factor ) const
{
    return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize*factor*(zoom() / 100.0)/1.8 );
}

luPixel ContextStyle::getEmptyRectHeight( double factor ) const
{
    return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize*factor*(zoom() / 100.0)/1.8 );
}


ContextStyle::TextStyle ContextStyle::convertTextStyleFraction( TextStyle tstyle ) const
{
    TextStyle result;

    switch ( tstyle ){
    case displayStyle:
	result = textStyle;
	break;
    case textStyle:
	result = scriptStyle;
	break;
    default:
	result = scriptScriptStyle;
	break;
    }

    return result;
}


ContextStyle::TextStyle ContextStyle::convertTextStyleIndex( TextStyle tstyle ) const
{
    TextStyle result;

    switch ( tstyle ){
    case displayStyle:
	result = scriptStyle;
	break;
    case textStyle:
	result = scriptStyle;
	break;
    default:
	result = scriptScriptStyle;
	break;
    }

    return result;
}


void ContextStyle::setup()
{
    luPt size = static_cast<luPt>( m_baseSize );
    TQFont font = symbolFont;
    font.setPointSize( size );
    TQFontMetrics fm( font );

    // Or better the real space required? ( boundingRect )
    quad = ptToLayoutUnitPt( fm.width( 'M' ) );

    font = TQFont(defaultFont);
    font.setPointSize( size );
    TQFontMetrics fm2( font );
    //m_axisHeight = ptToLayoutUnitPt( fm2.strikeOutPos() );
    //ptToLayoutUnitPixY
    //m_axisHeight = ptToLayoutUnitPt( pixelYToPt( fm2.strikeOutPos() ) );
    m_axisHeight = ptToLayoutUnitPixY( pixelYToPt( fm2.strikeOutPos() ) );
}


double StyleAttributes::sizeFactor() const
{
    if ( m_size.empty() ) {
//        kdWarning( DEBUGID ) << "SizeFactor stack is empty.\n";
        return 1.0;
    }
    return m_size.top();
}

bool StyleAttributes::customMathVariant() const
{
    if ( m_customMathVariant.empty() ) {
        return false;
    }
    return m_customMathVariant.top();
}

CharStyle StyleAttributes::charStyle() const
{
    if ( m_charStyle.empty() ) {
//        kdWarning( DEBUGID ) << "CharStyle stack is empty.\n";
        return anyChar;
    }
    return m_charStyle.top();
}

CharFamily StyleAttributes::charFamily() const
{
    if ( m_charFamily.empty() ) {
//        kdWarning( DEBUGID ) << "CharFamily stack is empty.\n";
        return anyFamily;
    }
    return m_charFamily.top();
}

TQColor StyleAttributes::color() const
{
    if ( m_color.empty() ) {
//        kdWarning( DEBUGID ) << "Color stack is empty.\n";
        return TQColor( TQt::black );
        //return getDefaultColor();
    }
    return m_color.top();
}

TQColor StyleAttributes::background() const
{
    if ( m_background.empty() ) {
//        kdWarning( DEBUGID ) << "Background stack is empty.\n";
        return TQColor( TQt::color0 );
    }
    return m_background.top();
}

TQFont StyleAttributes::font() const
{
    if ( m_font.empty() ) {
        return TQFont();
    }
    return m_font.top();
}

bool StyleAttributes::fontWeight() const
{
    if ( m_fontWeight.empty() ) {
        return false;
    }
    return m_fontWeight.top();
}

bool StyleAttributes::customFontWeight() const
{
    if ( m_customFontWeight.empty() ) {
        return false;
    }
    return m_customFontWeight.top();
}

bool StyleAttributes::fontStyle() const
{
    if ( m_fontStyle.empty() ) {
        return false;
    }
    return m_fontStyle.top();
}

bool StyleAttributes::customFontStyle() const
{
    if ( m_customFontStyle.empty() ) {
        return false;
    }
    return m_customFontStyle.top();
}

bool StyleAttributes::customFont() const
{
    if ( m_customFontFamily.empty() ) {
        return false;
    }
    return m_customFontFamily.top();
}

int StyleAttributes::scriptLevel() const
{
    if ( m_scriptLevel.empty() ) {
        return 0;
    }
    return m_scriptLevel.top();
}

double StyleAttributes::scriptSizeMultiplier() const
{
    if ( m_scriptSizeMultiplier.empty() ) {
        return scriptsizemultiplier;
    }
    return m_scriptSizeMultiplier.top();
}

double StyleAttributes::scriptMinSize() const
{
    if ( m_scriptMinSize.empty() ) {
        return scriptminsize;
    }
    return m_scriptMinSize.top();
}

double StyleAttributes::veryVeryThinMathSpace() const
{
    if ( m_veryVeryThinMathSpace.empty() ) {
        return veryverythinmathspace;
    }
    return m_veryVeryThinMathSpace.top();
}

double StyleAttributes::veryThinMathSpace() const
{
    if ( m_veryThinMathSpace.empty() ) {
        return verythinmathspace;
    }
    return m_veryThinMathSpace.top();
}

double StyleAttributes::thinMathSpace() const
{
    if ( m_thinMathSpace.empty() ) {
        return thinmathspace;
    }
    return m_thinMathSpace.top();
}

double StyleAttributes::mediumMathSpace() const
{
    if ( m_mediumMathSpace.empty() ) {
        return mediummathspace;
    }
    return m_mediumMathSpace.top();
}

double StyleAttributes::thickMathSpace() const
{
    if ( m_thickMathSpace.empty() ) {
        return thickmathspace;
    }
    return m_thickMathSpace.top();
}

double StyleAttributes::veryThickMathSpace() const
{
    if ( m_veryThickMathSpace.empty() ) {
        return verythickmathspace;
    }
    return m_veryThickMathSpace.top();
}

double StyleAttributes::veryVeryThickMathSpace() const
{
    if ( m_veryVeryThickMathSpace.empty() ) {
        return veryverythickmathspace;
    }
    return m_veryVeryThickMathSpace.top();
}

bool StyleAttributes::displayStyle() const
{
    if ( m_displayStyle.empty() ) {
        return true;
    }
    return m_displayStyle.top();
}

bool StyleAttributes::customDisplayStyle() const
{
    if ( m_customDisplayStyle.empty() ) {
        return false;
    }
    return m_customDisplayStyle.top();
}

double StyleAttributes::getSpace( SizeType type, double length ) const
{
    switch ( type ) {
    case NegativeVeryVeryThinMathSpace:
        return - veryVeryThinMathSpace();
    case NegativeVeryThinMathSpace:
        return - veryThinMathSpace();
    case NegativeThinMathSpace:
        return - thinMathSpace();
    case NegativeMediumMathSpace:
        return - mediumMathSpace();
    case NegativeThickMathSpace:
        return - thickMathSpace();
    case NegativeVeryThickMathSpace:
        return - veryThickMathSpace();
    case NegativeVeryVeryThickMathSpace:
        return - veryVeryThickMathSpace();
    case VeryVeryThinMathSpace:
        return veryVeryThinMathSpace();
    case VeryThinMathSpace:
        return veryThinMathSpace();
    case ThinMathSpace:
        return thinMathSpace();
    case MediumMathSpace:
        return mediumMathSpace();
    case ThickMathSpace:
        return thickMathSpace();
    case VeryThickMathSpace:
        return veryThickMathSpace();
    case VeryVeryThickMathSpace:
        return veryVeryThickMathSpace();
    default:
        break;
    }
    return length;
}

void StyleAttributes::resetSize()
{
    if ( ! m_size.empty() ) {
        m_size.pop();
    }
}

void StyleAttributes::resetCharStyle()
{
    if ( ! m_charStyle.empty() ) {
        m_charStyle.pop();
    }
}

void StyleAttributes::resetCharFamily()
{
    if ( ! m_charFamily.empty() ) {
        m_charFamily.pop();
    }
}

void StyleAttributes::resetColor()
{
    if ( ! m_color.empty() ) {
        m_color.pop();
    }
}

void StyleAttributes::resetBackground()
{
    if ( ! m_background.empty() ) {
        m_background.pop();
    }
}

void StyleAttributes::resetFontFamily()
{
    if ( ! m_customFontFamily.empty() ) {
        if ( m_customFontFamily.pop() ) {
            if ( ! m_font.empty() ) {
                m_font.pop();
            }
        }
    }
}

void StyleAttributes::resetFontWeight()
{
    if ( ! m_customFontWeight.empty() ) {
        if ( m_customFontWeight.pop() ) {
            if ( ! m_fontWeight.empty() ) {
                m_fontWeight.pop();
            }
        }
    }
}

void StyleAttributes::resetFontStyle()
{
    if ( ! m_customFontStyle.empty() ) {
        if ( m_customFontStyle.pop() ) {
            if ( ! m_fontStyle.empty() ) {
                m_fontStyle.pop();
            }
        }
    }
}

void StyleAttributes::resetScriptLevel()
{
    if ( ! m_scriptLevel.empty() ) {
        m_scriptLevel.pop();
    }
}

void StyleAttributes::resetScriptSizeMultiplier()
{
    if ( ! m_scriptSizeMultiplier.empty() ) {
        m_scriptSizeMultiplier.pop();
    }
}

void StyleAttributes::resetScriptMinSize()
{
    if ( ! m_scriptMinSize.empty() ) {
        m_scriptMinSize.pop();
    }
}

void StyleAttributes::resetVeryVeryThinMathSpace()
{
    if ( ! m_veryVeryThinMathSpace.empty() ) {
        m_veryVeryThinMathSpace.pop();
    }
}

void StyleAttributes::resetVeryThinMathSpace()
{
    if ( ! m_veryThinMathSpace.empty() ) {
        m_veryThinMathSpace.pop();
    }
}

void StyleAttributes::resetThinMathSpace()
{
    if ( ! m_thinMathSpace.empty() ) {
        m_thinMathSpace.pop();
    }
}

void StyleAttributes::resetMediumMathSpace()
{
    if ( ! m_mediumMathSpace.empty() ) {
        m_mediumMathSpace.pop();
    }
}

void StyleAttributes::resetThickMathSpace()
{
    if ( ! m_thickMathSpace.empty() ) {
        m_thickMathSpace.pop();
    }
}

void StyleAttributes::resetVeryThickMathSpace()
{
    if ( ! m_veryThickMathSpace.empty() ) {
        m_veryThickMathSpace.pop();
    }
}

void StyleAttributes::resetVeryVeryThickMathSpace()
{
    if ( ! m_veryVeryThickMathSpace.empty() ) {
        m_veryVeryThickMathSpace.pop();
    }
}

void StyleAttributes::resetDisplayStyle()
{
    if ( ! m_customDisplayStyle.empty() ) {
        if ( m_customDisplayStyle.pop() ) {
            if ( ! m_displayStyle.empty() ) {
                m_displayStyle.pop();
            }
        }
    }
}


KFORMULA_NAMESPACE_END