summaryrefslogtreecommitdiffstats
path: root/lib/kformula/contextstyle.h
blob: be4a8bcf034fcf3b3c4aae913e506b75e29e184b (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
/* This file is part of the KDE project
   Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
	              Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>

   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.
*/

#ifndef CONTEXTSTYLE_H
#define CONTEXTSTYLE_H

//TQt Include
#include <tqcolor.h>
#include <tqfont.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqvaluestack.h>

//KDE Include
#include <kconfig.h>
#include <KoTextZoomHandler.h>

//Formula include
#include "kformuladefs.h"


KFORMULA_NAMESPACE_BEGIN

class FontStyle;
class SymbolTable;


/**
 * Contains all the style information for the formela. The idea
 * is to change the values here (user configurable) and have
 * the elements paint themselves with this information.
 *
 * All distances are stored in point. Most methods return pixel
 * values.
 */
class ContextStyle : public KoTextZoomHandler
{
public:

    enum Alignment { left, center, right };

    /**
     * Textstyles like in TeX. In the remaining documentation, the
     * styles are abbreviated like this:
     *
     * displayStyle: D
     *
     * textStyle: T
     *
     * scriptStyle: S
     *
     * scriptScriptStyle: SS
     **/
    enum TextStyle {
        displayStyle = 0,
        textStyle = 1,
        scriptStyle = 2,
        scriptScriptStyle = 3
    };

    enum IndexStyle {normal, cramped};

    /**
     * Build a default context style
     */
    ContextStyle();
    ~ContextStyle();

    /**
     * @param init if true fonts may be installed if needed.
     */
    void init( bool init = true );

    /**
     * @param init true if initialization may take place. This may cause font
     * installation. Mark as false when this is not intended (i. e. creating
     * configuration dialog from another component)
     */
    void readConfig( KConfig* config, bool init = true );

    bool edit() const { return m_edit; }
    void setEdit( bool e ) { m_edit = e; }

    /**
     * @returns our symbol table.
     */
    const SymbolTable& symbolTable() const;

    const FontStyle& fontStyle() const { return *m_fontStyle; }


    void setZoomAndResolution( int zoom, int dpiX, int dpiY );

    /**
     * Sets the zoom by hand. This is to be used in <code>paintContent</code>.
     * @returns whether there was any change.
     */
    bool setZoomAndResolution( int zoom, double zoomX, double zoomY, bool updateViews, bool forPrint );

    bool syntaxHighlighting() const { return m_syntaxHighlighting; }
    void setSyntaxHighlighting( bool highlight ) { m_syntaxHighlighting = highlight; }

    TQColor getDefaultColor()  const { return defaultColor; }
    TQColor getNumberColorPlain()   const { return numberColor; }
    TQColor getOperatorColorPlain() const { return operatorColor; }
    TQColor getErrorColorPlain()    const { return errorColor; }
    TQColor getEmptyColorPlain()    const { return emptyColor; }
    TQColor getHelpColorPlain()     const { return helpColor; }
    TQColor getNumberColor()   const;
    TQColor getOperatorColor() const;
    TQColor getErrorColor()    const;
    TQColor getEmptyColor()    const;
    TQColor getHelpColor()     const;

    void setDefaultColor( const TQColor& );
    void setNumberColor( const TQColor& );
    void setOperatorColor( const TQColor& );
    void setErrorColor( const TQColor& );
    void setEmptyColor( const TQColor& );
    void setHelpColor( const TQColor& );

    TQString getFontStyle() const { return m_fontStyleName; }
    void setFontStyle( const TQString& fontStyle, bool init = true );

    TQFont getMathFont()       const { return mathFont; }
    TQFont getBracketFont()    const { return bracketFont; }
    TQFont getDefaultFont()    const { return defaultFont; }
    TQFont getNameFont()       const { return nameFont; }
    TQFont getNumberFont()     const { return numberFont; }
    TQFont getOperatorFont()   const { return operatorFont; }
    TQFont getSymbolFont()     const { return symbolFont; }

    void setMathFont( TQFont f )     { defaultFont = f; }
    void setBracketFont( TQFont f )  { bracketFont = f; }
    void setDefaultFont( TQFont f )  { defaultFont = f; }
    void setNameFont( TQFont f )     { nameFont = f; }
    void setNumberFont( TQFont f )   { numberFont = f; }
    void setOperatorFont( TQFont f ) { operatorFont = f; }

    //const TQStringList& requestedFonts() const;
    //void setRequestedFonts( const TQStringList& list );

    double getReductionFactor( TextStyle tstyle ) const;

    luPt getBaseSize() const;
    int baseSize() const { return m_baseSize; }
    void setBaseSize( int pointSize );
    void setSizeFactor( double factor );

    TextStyle getBaseTextStyle() const { return m_baseTextStyle; }
    bool isScript( TextStyle tstyle ) const { return ( tstyle == scriptStyle ) ||
                                                     ( tstyle == scriptScriptStyle ); }

    /**
     * TeX like spacings.
     */
    luPixel getSpace( TextStyle tstyle, SpaceWidth space, double factor ) const;
    luPixel getThinSpace( TextStyle tstyle, double factor ) const;
    luPixel getMediumSpace( TextStyle tstyle, double factor ) const;
    luPixel getThickSpace( TextStyle tstyle, double factor ) const;
    luPixel getQuadSpace( TextStyle tstyle, double factor ) const;

    luPixel axisHeight( TextStyle tstyle, double factor ) const;

    /**
     * Calculates the font size corresponding to the given TextStyle.
     */
    luPt getAdjustedSize( TextStyle tstyle, double factor ) const;

    /**
     * All simple lines like the one that makes up a fraction.
     */
    luPixel getLineWidth( double factor ) const;

    luPixel getEmptyRectWidth( double factor ) const;
    luPixel getEmptyRectHeight( double factor ) const;

    Alignment getMatrixAlignment() const { return center; }

    bool getCenterSymbol() const { return centerSymbol; }

    /**
     * Font-conversions a la TeX.
     *
     * For fractions (and also matrices), we have the following conversions:
     * D->T, T->S, S,SS->SS
     */
    TextStyle convertTextStyleFraction( TextStyle tstyle ) const;

    /**
     * Font-conversions a la TeX.
     *
     * For indices, we have the following conversions:
     * D->S, T->S, S,SS->SS
     */
    TextStyle convertTextStyleIndex( TextStyle tstyle ) const;

    /**
     * Index-style-conversions a la TeX.
     *
     * The function convertIndexStyleUpper is responsible for everything
     * that ends 'up', like nominators of fractions, or upper indices.
     *
     * We have the following rule:
     * normal->normal, cramped->cramped
     */
    IndexStyle convertIndexStyleUpper( IndexStyle istyle ) const {
	return istyle; }


    /**
     * Index-style-conversions a la TeX.
     *
     * The function convertIndexStyleLower is responsible for everything
     * that ends 'down', like nominators of fractions, or upper indices.
     *
     * We have the following rule:
     * normal->cramped, cramped->cramped
     */
    IndexStyle convertIndexStyleLower( IndexStyle /*istyle*/ ) const {
	return cramped; }

private:

    void setup();

    struct TextStyleValues {

        void setup( double reduction ) { reductionFactor = reduction; }

        luPt thinSpace( luPt quad ) const   { return static_cast<luPt>( reductionFactor*static_cast<double>( quad )/6. ); }
        luPt mediumSpace( luPt quad ) const { return static_cast<luPt>( reductionFactor*static_cast<double>( quad )*2./9. ); }
        luPt thickSpace( luPt quad ) const  { return static_cast<luPt>( reductionFactor*static_cast<double>( quad )*5./18. ); }
        luPt quadSpace( luPt quad ) const   { return quad; }

        luPixel axisHeight( luPixel height ) const { return static_cast<luPixel>( reductionFactor*height ); }
        double reductionFactor;
    };

    TextStyleValues textStyleValues[ 4 ];

    TQFont mathFont;
    TQFont bracketFont;
    TQFont defaultFont;
    TQFont nameFont;
    TQFont numberFont;
    TQFont operatorFont;
    TQFont symbolFont;

    //TQStringList m_requestedFonts;

    TQColor defaultColor;
    TQColor numberColor;
    TQColor operatorColor;
    TQColor errorColor;
    TQColor emptyColor;
    TQColor helpColor;

    /**
     * The cursors movement style. You need to notify each cursor
     * if you change this.
     */
    bool linearMovement;

    /**
     * The (font) size of the formula's main sequence.
     */
    int m_baseSize;

    /**
     * Hack! Each formula might set this to a value not too far from one
     * to get a size different from the default one.
     */
    double m_sizeFactor;

    /**
     * The base text style of the formula.
     **/
    TextStyle m_baseTextStyle;

    /**
     * The thickness of our lines.
     */
    pt lineWidth;

    /**
     * Size of one quad.
     */
    luPt quad;

    /**
     * Distance between base line and axis.
     */
    luPixel m_axisHeight;

    /**
     * true means to center the symbol between its indexes.
     * false means tqalignment to the right.
     */
    bool centerSymbol;

    /**
     * Whether we want coloured formulae.
     */
    bool m_syntaxHighlighting;

    /**
     * Whether we are in edit mode.
     */
    bool m_edit;

    /**
     * The symbols/names that are "known" to the system.
     */
    //SymbolTable table;

    FontStyle* m_fontStyle;
    TQString m_fontStyleName;
};

// Section 3.3.4.2, default values
const double scriptsizemultiplier   = 0.71;
const double scriptminsize          = 8;
const double veryverythinmathspace  = 0.0555556;
const double verythinmathspace      = 0.111111;
const double thinmathspace          = 0.166667;
const double mediummathspace        = 0.222222;
const double thickmathspace         = 0.277778;
const double verythickmathspace     = 0.333333;
const double veryverythickmathspace = 0.388889;

class StyleAttributes {
 public:
    double sizeFactor() const ;
    bool customMathVariant() const ;
    CharStyle charStyle() const ;
    CharFamily charFamily() const ;
    TQColor color() const ;
    TQColor background() const ;
    TQFont font() const ;
    bool fontWeight() const ;
    bool customFontWeight() const ;
    bool fontStyle() const ;
    bool customFontStyle() const ;
    bool customFont() const ;

    int scriptLevel() const ;
    double scriptSizeMultiplier() const ;
    double scriptMinSize() const ;
    double veryVeryThinMathSpace() const ;
    double veryThinMathSpace() const ;
    double thinMathSpace() const ;
    double mediumMathSpace() const ;
    double thickMathSpace() const ;
    double veryThickMathSpace() const ;
    double veryVeryThickMathSpace() const ;
    bool displayStyle() const ;
    bool customDisplayStyle() const ;

    double getSpace( SizeType type, double length ) const ;

    void setSizeFactor( double s ) { m_size.push( s ); }
    void setCustomMathVariant( bool cmv ) { m_customMathVariant.push( cmv ); }
    void setCharStyle( CharStyle cs ) { m_charStyle.push( cs ); }
    void setCharFamily( CharFamily cf ) { m_charFamily.push( cf ); }
    void setColor( const TQColor& c ) { m_color.push( c ); }
    void setBackground( const TQColor& bg ) { m_background.push( bg ); }
    void setFont( const TQFont& f ) { m_font.push( f ); }
    void setCustomFont( bool cf ) { m_customFontFamily.push ( cf ); }
    void setCustomFontWeight( bool cfw ) { m_customFontWeight.push( cfw ); }
    void setFontWeight( bool fw ) { m_fontWeight.push( fw ); }
    void setCustomFontStyle( bool cfs ) { m_customFontStyle.push( cfs ); }
    void setFontStyle( bool fs ) { m_fontStyle.push( fs ); }

    void setScriptLevel( int s ) { m_scriptLevel.push( s ); }
    void setScriptSizeMultiplier( double s ) { m_scriptSizeMultiplier.push( s ); }
    void setScriptMinSize( double s ) { m_scriptMinSize.push( s ); }
    void setVeryVeryThinMathSpace( double s ) { m_veryVeryThinMathSpace.push( s ); }
    void setVeryThinMathSpace( double s ) { m_veryThinMathSpace.push( s ); }
    void setThinMathSpace( double s ) { m_thinMathSpace.push( s ); }
    void setMediumMathSpace( double s ) { m_mediumMathSpace.push( s ); }
    void setThickMathSpace( double s ) { m_thickMathSpace.push( s ); }
    void setVeryThickMathSpace( double s ) { m_veryThickMathSpace.push( s ); }
    void setVeryVeryThickMathSpace( double s ) { m_veryVeryThickMathSpace.push( s ); }
    void setDisplayStyle( bool ds ) { m_displayStyle.push( ds ); }
    void setCustomDisplayStyle( bool cds ) { m_customDisplayStyle.push( cds ); }

    void reset();
    void resetSize();
    void resetCharStyle();
    void resetCharFamily();
    void resetColor();
    void resetBackground();
    void resetFontFamily();
    void resetFontWeight();
    void resetFontStyle();

    void resetScriptLevel();
    void resetScriptSizeMultiplier();
    void resetScriptMinSize();
    void resetVeryVeryThinMathSpace();
    void resetVeryThinMathSpace();
    void resetThinMathSpace();
    void resetMediumMathSpace();
    void resetThickMathSpace();
    void resetVeryThickMathSpace();
    void resetVeryVeryThickMathSpace();
    void resetDisplayStyle();

 private:
    // Size of the font in points (mathsize / fontsize)
    TQValueStack<double> m_size;

    // Whether a custom mathvariant attribute is in use
    TQValueStack<bool> m_customMathVariant;

    // Font style (mathvariant, fontweight, fontstyle)
    TQValueStack<CharStyle> m_charStyle;

    // Font family (mathvariant)
    TQValueStack<CharFamily> m_charFamily;

    // Foreground color (mathcolor, color)
    TQValueStack<TQColor> m_color;

    // Background color (mathbackground)
    TQValueStack<TQColor> m_background;

    // Font family (fontfamily)
    TQValueStack<TQFont> m_font;

    // Whether a custom fontfamily attribute is in use (instead of CharFamily)
    TQValueStack<bool> m_customFontFamily;

    // Font Weight (fontweight)
    TQValueStack<bool> m_fontWeight;

    // Whether a custom fontweight attribute is in use
    TQValueStack<bool> m_customFontWeight;

    // Font Style (fontstyle)
    TQValueStack<bool> m_fontStyle;

    // Whether a custom fontstyle attribute is in use
    TQValueStack<bool> m_customFontStyle;

    TQValueStack<int> m_scriptLevel;
    TQValueStack<double> m_scriptSizeMultiplier;
    TQValueStack<double> m_scriptMinSize;
    TQValueStack<double> m_veryVeryThinMathSpace;
    TQValueStack<double> m_veryThinMathSpace;
    TQValueStack<double> m_thinMathSpace;
    TQValueStack<double> m_mediumMathSpace;
    TQValueStack<double> m_thickMathSpace;
    TQValueStack<double> m_veryThickMathSpace;
    TQValueStack<double> m_veryVeryThickMathSpace;
    TQValueStack<bool> m_displayStyle;
    TQValueStack<bool> m_customDisplayStyle;
};

KFORMULA_NAMESPACE_END

#endif // CONTEXTSTYLE_H