contextstyle.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
3 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
4 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include <tqfontmetrics.h>
23#include <tqstring.h>
24
25#include <kdebug.h>
26#include <KoGlobal.h>
27
28#include "contextstyle.h"
29#include "fontstyle.h"
30
31
32KFORMULA_NAMESPACE_BEGIN
33
34
36 : symbolFont( "Symbol" ),
37 defaultColor(TQt::black), numberColor(TQt::blue),
38 operatorColor(TQt::darkGreen), errorColor(TQt::darkRed),
39 emptyColor(TQt::blue), helpColor( TQt::gray ), m_sizeFactor( 0 )
40{
41// kdDebug() << "ContextStyle::ContextStyle" << endl
42// << "defaultFont: " << defaultFont.rawName() << endl
43// << "nameFont: " << nameFont.rawName() << endl
44// << "numberFont: " << numberFont.rawName() << endl
45// << "operatorFont: " << operatorFont.rawName() << endl
46// << "symbolFont: " << symbolFont.rawName() << endl;
47
48 textStyleValues[ displayStyle ].setup( 1. );
49 textStyleValues[ textStyle ].setup( 1. );
50 textStyleValues[ scriptStyle ].setup( .7 );
51 textStyleValues[ scriptScriptStyle ].setup( .49 );
52
53 m_baseTextStyle = displayStyle;
54
55 lineWidth = 1;
56 linearMovement = false;
57 centerSymbol = true;
58 m_syntaxHighlighting = true;
59
60 m_fontStyle = 0;
61}
62
63
64ContextStyle::~ContextStyle()
65{
66 delete m_fontStyle;
67}
68
69
70void ContextStyle::init( bool init )
71{
72 setup();
73 setFontStyle( m_fontStyleName, init );
74}
75
76
77void ContextStyle::setFontStyle( const TQString& fontStyle, bool init )
78{
79 delete m_fontStyle;
80 m_fontStyleName = fontStyle;
81 m_fontStyle = new FontStyle();
82 m_fontStyle->init( this, init );
83}
84
85
87{
88 return *( m_fontStyle->symbolTable() );
89}
90
91
92void ContextStyle::readConfig( TDEConfig* config, bool init )
93{
94 config->setGroup( "kformula Font" );
95 TQString fontName = config->readEntry( "defaultFont", "Times,12,-1,5,50,1,0,0,0,0" );
96 defaultFont.fromString( fontName );
97 fontName = config->readEntry( "nameFont", "Times,12,-1,5,50,0,0,0,0,0" );
98 nameFont.fromString( fontName );
99 fontName = config->readEntry( "numberFont", "Times,12,-1,5,50,0,0,0,0,0" );
100 numberFont.fromString( fontName );
101 fontName = config->readEntry( "operatorFont", "Times,12,-1,5,50,0,0,0,0,0" );
102 operatorFont.fromString( fontName );
103 TQString baseSize = config->readEntry( "baseSize", "20" );
104 m_baseSize = baseSize.toInt();
105
106 if ( ! FontStyle::missingFonts( init ).isEmpty() ) {
107 kdWarning( DEBUGID) << "Not all basic fonts found\n";
108 }
109 mathFont.fromString("Arev Sans");
110 bracketFont.fromString("cmex10");
111
112
113 // There's no gui right anymore but I'll leave it here...
114 config->setGroup( "kformula Color" );
115 defaultColor = config->readColorEntry( "defaultColor", &defaultColor );
116 numberColor = config->readColorEntry( "numberColor", &numberColor );
117 operatorColor = config->readColorEntry( "operatorColor", &operatorColor );
118 emptyColor = config->readColorEntry( "emptyColor", &emptyColor );
119 errorColor = config->readColorEntry( "errorColor", &errorColor );
120 helpColor = config->readColorEntry( "helpColor", &helpColor );
121
122 m_syntaxHighlighting = config->readBoolEntry( "syntaxHighlighting", true );
123}
124
125void ContextStyle::setZoomAndResolution( int zoom, int dpiX, int dpiY )
126{
128}
129
130bool ContextStyle::setZoomAndResolution( int zoom, double zoomX, double zoomY, bool, bool )
131{
132 bool changes = m_zoom != zoom || m_zoomedResolutionX != zoomX || m_zoomedResolutionY != zoomY;
133 m_zoom = zoom;
134 m_zoomedResolutionX = zoomX;
135 m_zoomedResolutionY = zoomY;
136 return changes;
137}
138
139TQColor ContextStyle::getNumberColor() const
140{
141 if ( edit() && syntaxHighlighting() ) {
142 return numberColor;
143 }
144 return getDefaultColor();
145}
146
147TQColor ContextStyle::getOperatorColor() const
148{
149 if ( edit() && syntaxHighlighting() ) {
150 return operatorColor;
151 }
152 return getDefaultColor();
153}
154
155TQColor ContextStyle::getErrorColor() const
156{
157 if ( edit() && syntaxHighlighting() ) {
158 return errorColor;
159 }
160 return getDefaultColor();
161}
162
163TQColor ContextStyle::getEmptyColor() const
164{
165 if ( edit() && syntaxHighlighting() ) {
166 return emptyColor;
167 }
168 return getDefaultColor();
169}
170
171TQColor ContextStyle::getHelpColor() const
172{
173 if ( edit() && syntaxHighlighting() ) {
174 return helpColor;
175 }
176 return getDefaultColor();
177}
178
179void ContextStyle::setDefaultColor( const TQColor& color )
180{
181 defaultColor = color;
182}
183void ContextStyle::setNumberColor( const TQColor& color )
184{
185 numberColor = color;
186}
187void ContextStyle::setOperatorColor( const TQColor& color )
188{
189 operatorColor = color;
190}
191void ContextStyle::setErrorColor( const TQColor& color )
192{
193 errorColor = color;
194}
195void ContextStyle::setEmptyColor( const TQColor& color )
196{
197 emptyColor = color;
198}
199void ContextStyle::setHelpColor( const TQColor& color )
200{
201 helpColor = color;
202}
203
204#if 0
205const TQStringList& ContextStyle::requestedFonts() const
206{
207 return m_requestedFonts;
208}
209
210void ContextStyle::setRequestedFonts( const TQStringList& list )
211{
212 m_requestedFonts = list;
213 //table.init( this );
214}
215#endif
216
217double ContextStyle::getReductionFactor( TextStyle tstyle ) const
218{
219 return textStyleValues[ tstyle ].reductionFactor;
220}
221
222luPt ContextStyle::getAdjustedSize( TextStyle tstyle, double factor ) const
223{
224 return tqRound( ptToLayoutUnitPt( m_sizeFactor
225 * m_baseSize
226 * getReductionFactor( tstyle )
227 * factor
228 * zoom() / 100.0 ) );
229}
230
231luPixel ContextStyle::getSpace( TextStyle tstyle, SpaceWidth space, double factor ) const
232{
233 switch ( space ) {
234 case NEGTHIN: return -getThinSpace( tstyle, factor );
235 case THIN: return getThinSpace( tstyle, factor );
236 case MEDIUM: return getMediumSpace( tstyle, factor );
237 case THICK: return getThickSpace( tstyle, factor );
238 case QUAD: return getQuadSpace( tstyle, factor );
239 }
240 return 0;
241}
242
243luPixel ContextStyle::getThinSpace( TextStyle tstyle, double factor ) const
244{
245 return ptToPixelX( m_sizeFactor
246 * textStyleValues[ tstyle ].thinSpace( quad )
247 * factor
248 * zoom() / 100.0 );
249}
250
251luPixel ContextStyle::getMediumSpace( TextStyle tstyle, double factor ) const
252{
253 return ptToPixelX( m_sizeFactor
254 * textStyleValues[ tstyle ].mediumSpace( quad )
255 * factor
256 * zoom() / 100.0 );
257}
258
259luPixel ContextStyle::getThickSpace( TextStyle tstyle, double factor ) const
260{
261 return ptToPixelX( m_sizeFactor
262 * textStyleValues[ tstyle ].thickSpace( quad )
263 * factor
264 * zoom() / 100.0 );
265}
266
267luPixel ContextStyle::getQuadSpace( TextStyle tstyle, double factor ) const
268{
269 return ptToPixelX( m_sizeFactor
270 * textStyleValues[ tstyle ].quadSpace( quad )
271 * factor
272 * zoom() / 100.0 );
273}
274
275luPixel ContextStyle::axisHeight( TextStyle tstyle, double factor ) const
276{
277 //return ptToPixelY( textStyleValues[ tstyle ].axisHeight( m_axisHeight ) );
278 return static_cast<luPixel>( m_sizeFactor
279 * textStyleValues[ tstyle ].axisHeight( m_axisHeight )
280 * factor
281 * zoom() / 100.0 );
282}
283
284luPt ContextStyle::getBaseSize() const
285{
286 return static_cast<luPt>( ptToLayoutUnitPt( m_sizeFactor*m_baseSize ) );
287}
288
289void ContextStyle::setBaseSize( int size )
290{
291 //kdDebug( 40000 ) << "ContextStyle::setBaseSize" << endl;
292 if ( size != m_baseSize ) {
293 m_baseSize = size;
294 setup();
295 }
296}
297
298void ContextStyle::setSizeFactor( double factor )
299{
300 m_sizeFactor = factor;
301}
302
303
304luPixel ContextStyle::getLineWidth( double factor ) const
305{
306 return ptToLayoutUnitPixX( m_sizeFactor*lineWidth*factor*zoom() / 100.0 );
307}
308
309luPixel ContextStyle::getEmptyRectWidth( double factor ) const
310{
311 return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize*factor*(zoom() / 100.0)/1.8 );
312}
313
314luPixel ContextStyle::getEmptyRectHeight( double factor ) const
315{
316 return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize*factor*(zoom() / 100.0)/1.8 );
317}
318
319
321{
322 TextStyle result;
323
324 switch ( tstyle ){
325 case displayStyle:
326 result = textStyle;
327 break;
328 case textStyle:
329 result = scriptStyle;
330 break;
331 default:
332 result = scriptScriptStyle;
333 break;
334 }
335
336 return result;
337}
338
339
341{
342 TextStyle result;
343
344 switch ( tstyle ){
345 case displayStyle:
346 result = scriptStyle;
347 break;
348 case textStyle:
349 result = scriptStyle;
350 break;
351 default:
352 result = scriptScriptStyle;
353 break;
354 }
355
356 return result;
357}
358
359
360void ContextStyle::setup()
361{
362 luPt size = static_cast<luPt>( m_baseSize );
363 TQFont font = symbolFont;
364 font.setPointSize( size );
365 TQFontMetrics fm( font );
366
367 // Or better the real space required? ( boundingRect )
368 quad = ptToLayoutUnitPt( fm.width( 'M' ) );
369
370 font = TQFont(defaultFont);
371 font.setPointSize( size );
372 TQFontMetrics fm2( font );
373 //m_axisHeight = ptToLayoutUnitPt( fm2.strikeOutPos() );
374 //ptToLayoutUnitPixY
375 //m_axisHeight = ptToLayoutUnitPt( pixelYToPt( fm2.strikeOutPos() ) );
376 m_axisHeight = ptToLayoutUnitPixY( pixelYToPt( fm2.strikeOutPos() ) );
377}
378
379
380double StyleAttributes::sizeFactor() const
381{
382 if ( m_size.empty() ) {
383// kdWarning( DEBUGID ) << "SizeFactor stack is empty.\n";
384 return 1.0;
385 }
386 return m_size.top();
387}
388
389bool StyleAttributes::customMathVariant() const
390{
391 if ( m_customMathVariant.empty() ) {
392 return false;
393 }
394 return m_customMathVariant.top();
395}
396
397CharStyle StyleAttributes::charStyle() const
398{
399 if ( m_charStyle.empty() ) {
400// kdWarning( DEBUGID ) << "CharStyle stack is empty.\n";
401 return anyChar;
402 }
403 return m_charStyle.top();
404}
405
406CharFamily StyleAttributes::charFamily() const
407{
408 if ( m_charFamily.empty() ) {
409// kdWarning( DEBUGID ) << "CharFamily stack is empty.\n";
410 return anyFamily;
411 }
412 return m_charFamily.top();
413}
414
415TQColor StyleAttributes::color() const
416{
417 if ( m_color.empty() ) {
418// kdWarning( DEBUGID ) << "Color stack is empty.\n";
419 return TQColor( TQt::black );
420 //return getDefaultColor();
421 }
422 return m_color.top();
423}
424
425TQColor StyleAttributes::background() const
426{
427 if ( m_background.empty() ) {
428// kdWarning( DEBUGID ) << "Background stack is empty.\n";
429 return TQColor( TQt::color0 );
430 }
431 return m_background.top();
432}
433
434TQFont StyleAttributes::font() const
435{
436 if ( m_font.empty() ) {
437 return TQFont();
438 }
439 return m_font.top();
440}
441
442bool StyleAttributes::fontWeight() const
443{
444 if ( m_fontWeight.empty() ) {
445 return false;
446 }
447 return m_fontWeight.top();
448}
449
450bool StyleAttributes::customFontWeight() const
451{
452 if ( m_customFontWeight.empty() ) {
453 return false;
454 }
455 return m_customFontWeight.top();
456}
457
458bool StyleAttributes::fontStyle() const
459{
460 if ( m_fontStyle.empty() ) {
461 return false;
462 }
463 return m_fontStyle.top();
464}
465
466bool StyleAttributes::customFontStyle() const
467{
468 if ( m_customFontStyle.empty() ) {
469 return false;
470 }
471 return m_customFontStyle.top();
472}
473
474bool StyleAttributes::customFont() const
475{
476 if ( m_customFontFamily.empty() ) {
477 return false;
478 }
479 return m_customFontFamily.top();
480}
481
482int StyleAttributes::scriptLevel() const
483{
484 if ( m_scriptLevel.empty() ) {
485 return 0;
486 }
487 return m_scriptLevel.top();
488}
489
490double StyleAttributes::scriptSizeMultiplier() const
491{
492 if ( m_scriptSizeMultiplier.empty() ) {
493 return scriptsizemultiplier;
494 }
495 return m_scriptSizeMultiplier.top();
496}
497
498double StyleAttributes::scriptMinSize() const
499{
500 if ( m_scriptMinSize.empty() ) {
501 return scriptminsize;
502 }
503 return m_scriptMinSize.top();
504}
505
506double StyleAttributes::veryVeryThinMathSpace() const
507{
508 if ( m_veryVeryThinMathSpace.empty() ) {
509 return veryverythinmathspace;
510 }
511 return m_veryVeryThinMathSpace.top();
512}
513
514double StyleAttributes::veryThinMathSpace() const
515{
516 if ( m_veryThinMathSpace.empty() ) {
517 return verythinmathspace;
518 }
519 return m_veryThinMathSpace.top();
520}
521
522double StyleAttributes::thinMathSpace() const
523{
524 if ( m_thinMathSpace.empty() ) {
525 return thinmathspace;
526 }
527 return m_thinMathSpace.top();
528}
529
530double StyleAttributes::mediumMathSpace() const
531{
532 if ( m_mediumMathSpace.empty() ) {
533 return mediummathspace;
534 }
535 return m_mediumMathSpace.top();
536}
537
538double StyleAttributes::thickMathSpace() const
539{
540 if ( m_thickMathSpace.empty() ) {
541 return thickmathspace;
542 }
543 return m_thickMathSpace.top();
544}
545
546double StyleAttributes::veryThickMathSpace() const
547{
548 if ( m_veryThickMathSpace.empty() ) {
549 return verythickmathspace;
550 }
551 return m_veryThickMathSpace.top();
552}
553
554double StyleAttributes::veryVeryThickMathSpace() const
555{
556 if ( m_veryVeryThickMathSpace.empty() ) {
557 return veryverythickmathspace;
558 }
559 return m_veryVeryThickMathSpace.top();
560}
561
562bool StyleAttributes::displayStyle() const
563{
564 if ( m_displayStyle.empty() ) {
565 return true;
566 }
567 return m_displayStyle.top();
568}
569
570bool StyleAttributes::customDisplayStyle() const
571{
572 if ( m_customDisplayStyle.empty() ) {
573 return false;
574 }
575 return m_customDisplayStyle.top();
576}
577
578double StyleAttributes::getSpace( SizeType type, double length ) const
579{
580 switch ( type ) {
581 case NegativeVeryVeryThinMathSpace:
582 return - veryVeryThinMathSpace();
583 case NegativeVeryThinMathSpace:
584 return - veryThinMathSpace();
585 case NegativeThinMathSpace:
586 return - thinMathSpace();
587 case NegativeMediumMathSpace:
588 return - mediumMathSpace();
589 case NegativeThickMathSpace:
590 return - thickMathSpace();
591 case NegativeVeryThickMathSpace:
592 return - veryThickMathSpace();
593 case NegativeVeryVeryThickMathSpace:
594 return - veryVeryThickMathSpace();
595 case VeryVeryThinMathSpace:
596 return veryVeryThinMathSpace();
597 case VeryThinMathSpace:
598 return veryThinMathSpace();
599 case ThinMathSpace:
600 return thinMathSpace();
601 case MediumMathSpace:
602 return mediumMathSpace();
603 case ThickMathSpace:
604 return thickMathSpace();
605 case VeryThickMathSpace:
606 return veryThickMathSpace();
607 case VeryVeryThickMathSpace:
608 return veryVeryThickMathSpace();
609 default:
610 break;
611 }
612 return length;
613}
614
615void StyleAttributes::resetSize()
616{
617 if ( ! m_size.empty() ) {
618 m_size.pop();
619 }
620}
621
622void StyleAttributes::resetCharStyle()
623{
624 if ( ! m_charStyle.empty() ) {
625 m_charStyle.pop();
626 }
627}
628
629void StyleAttributes::resetCharFamily()
630{
631 if ( ! m_charFamily.empty() ) {
632 m_charFamily.pop();
633 }
634}
635
636void StyleAttributes::resetColor()
637{
638 if ( ! m_color.empty() ) {
639 m_color.pop();
640 }
641}
642
643void StyleAttributes::resetBackground()
644{
645 if ( ! m_background.empty() ) {
646 m_background.pop();
647 }
648}
649
650void StyleAttributes::resetFontFamily()
651{
652 if ( ! m_customFontFamily.empty() ) {
653 if ( m_customFontFamily.pop() ) {
654 if ( ! m_font.empty() ) {
655 m_font.pop();
656 }
657 }
658 }
659}
660
661void StyleAttributes::resetFontWeight()
662{
663 if ( ! m_customFontWeight.empty() ) {
664 if ( m_customFontWeight.pop() ) {
665 if ( ! m_fontWeight.empty() ) {
666 m_fontWeight.pop();
667 }
668 }
669 }
670}
671
672void StyleAttributes::resetFontStyle()
673{
674 if ( ! m_customFontStyle.empty() ) {
675 if ( m_customFontStyle.pop() ) {
676 if ( ! m_fontStyle.empty() ) {
677 m_fontStyle.pop();
678 }
679 }
680 }
681}
682
683void StyleAttributes::resetScriptLevel()
684{
685 if ( ! m_scriptLevel.empty() ) {
686 m_scriptLevel.pop();
687 }
688}
689
690void StyleAttributes::resetScriptSizeMultiplier()
691{
692 if ( ! m_scriptSizeMultiplier.empty() ) {
693 m_scriptSizeMultiplier.pop();
694 }
695}
696
697void StyleAttributes::resetScriptMinSize()
698{
699 if ( ! m_scriptMinSize.empty() ) {
700 m_scriptMinSize.pop();
701 }
702}
703
704void StyleAttributes::resetVeryVeryThinMathSpace()
705{
706 if ( ! m_veryVeryThinMathSpace.empty() ) {
707 m_veryVeryThinMathSpace.pop();
708 }
709}
710
711void StyleAttributes::resetVeryThinMathSpace()
712{
713 if ( ! m_veryThinMathSpace.empty() ) {
714 m_veryThinMathSpace.pop();
715 }
716}
717
718void StyleAttributes::resetThinMathSpace()
719{
720 if ( ! m_thinMathSpace.empty() ) {
721 m_thinMathSpace.pop();
722 }
723}
724
725void StyleAttributes::resetMediumMathSpace()
726{
727 if ( ! m_mediumMathSpace.empty() ) {
728 m_mediumMathSpace.pop();
729 }
730}
731
732void StyleAttributes::resetThickMathSpace()
733{
734 if ( ! m_thickMathSpace.empty() ) {
735 m_thickMathSpace.pop();
736 }
737}
738
739void StyleAttributes::resetVeryThickMathSpace()
740{
741 if ( ! m_veryThickMathSpace.empty() ) {
742 m_veryThickMathSpace.pop();
743 }
744}
745
746void StyleAttributes::resetVeryVeryThickMathSpace()
747{
748 if ( ! m_veryVeryThickMathSpace.empty() ) {
749 m_veryVeryThickMathSpace.pop();
750 }
751}
752
753void StyleAttributes::resetDisplayStyle()
754{
755 if ( ! m_customDisplayStyle.empty() ) {
756 if ( m_customDisplayStyle.pop() ) {
757 if ( ! m_displayStyle.empty() ) {
758 m_displayStyle.pop();
759 }
760 }
761 }
762}
763
764
765KFORMULA_NAMESPACE_END
TextStyle convertTextStyleIndex(TextStyle tstyle) const
Font-conversions a la TeX.
luPt getAdjustedSize(TextStyle tstyle, double factor) const
Calculates the font size corresponding to the given TextStyle.
TextStyle
Textstyles like in TeX.
luPixel getLineWidth(double factor) const
All simple lines like the one that makes up a fraction.
void init(bool init=true)
ContextStyle()
Build a default context style.
void setZoomAndResolution(int zoom, int dpiX, int dpiY)
Change the zoom factor to z (e.g.
void readConfig(TDEConfig *config, bool init=true)
luPixel getSpace(TextStyle tstyle, SpaceWidth space, double factor) const
TeX like spacings.
const SymbolTable & symbolTable() const
TextStyle convertTextStyleFraction(TextStyle tstyle) const
Font-conversions a la TeX.
Base class for all supported font styles.
Definition fontstyle.h:41
const SymbolTable * symbolTable() const
the table for ordinary symbols (those that have a unicode value)
Definition fontstyle.h:52
bool init(ContextStyle *context, bool install=true)
lazy init support.
Definition fontstyle.cpp:42
int ptToPixelX(double pt) const
Basic pt to pixel and pixel to pt conversions, valid at any zoom level, as well as at the Layout Unit...
static double ptToLayoutUnitPt(double pt)
Not zoom dependent.
int ptToLayoutUnitPixX(double x_pt) const
The "document pt" -> "Layout Unit pixels" conversions, for convenience.
int zoom() const
virtual void setZoomAndResolution(int zoom, int dpiX, int dpiY)
Change the zoom factor to z (e.g.
The symbol table.
Definition symboltable.h:77