00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <qfontmetrics.h>
00023 #include <qstring.h>
00024
00025 #include <kdebug.h>
00026 #include <KoGlobal.h>
00027
00028 #include "contextstyle.h"
00029 #include "fontstyle.h"
00030
00031
00032 KFORMULA_NAMESPACE_BEGIN
00033
00034
00035 ContextStyle::ContextStyle()
00036 : symbolFont( "Symbol" ),
00037 defaultColor(Qt::black), numberColor(Qt::blue),
00038 operatorColor(Qt::darkGreen), errorColor(Qt::darkRed),
00039 emptyColor(Qt::blue), helpColor( Qt::gray ), m_sizeFactor( 0 )
00040 {
00041
00042
00043
00044
00045
00046
00047
00048 textStyleValues[ displayStyle ].setup( 1. );
00049 textStyleValues[ textStyle ].setup( 1. );
00050 textStyleValues[ scriptStyle ].setup( .7 );
00051 textStyleValues[ scriptScriptStyle ].setup( .49 );
00052
00053 m_baseTextStyle = displayStyle;
00054
00055 lineWidth = 1;
00056 linearMovement = false;
00057 centerSymbol = true;
00058 m_syntaxHighlighting = true;
00059
00060 m_fontStyle = 0;
00061 }
00062
00063
00064 ContextStyle::~ContextStyle()
00065 {
00066 delete m_fontStyle;
00067 }
00068
00069
00070 void ContextStyle::init( bool init )
00071 {
00072 setup();
00073 setFontStyle( m_fontStyleName, init );
00074 }
00075
00076
00077 void ContextStyle::setFontStyle( const QString& fontStyle, bool init )
00078 {
00079 delete m_fontStyle;
00080 m_fontStyleName = fontStyle;
00081 m_fontStyle = new FontStyle();
00082 m_fontStyle->init( this, init );
00083 }
00084
00085
00086 const SymbolTable& ContextStyle::symbolTable() const
00087 {
00088 return *( m_fontStyle->symbolTable() );
00089 }
00090
00091
00092 void ContextStyle::readConfig( KConfig* config, bool init )
00093 {
00094 config->setGroup( "kformula Font" );
00095 QString fontName = config->readEntry( "defaultFont", "Times,12,-1,5,50,1,0,0,0,0" );
00096 defaultFont.fromString( fontName );
00097 fontName = config->readEntry( "nameFont", "Times,12,-1,5,50,0,0,0,0,0" );
00098 nameFont.fromString( fontName );
00099 fontName = config->readEntry( "numberFont", "Times,12,-1,5,50,0,0,0,0,0" );
00100 numberFont.fromString( fontName );
00101 fontName = config->readEntry( "operatorFont", "Times,12,-1,5,50,0,0,0,0,0" );
00102 operatorFont.fromString( fontName );
00103 QString baseSize = config->readEntry( "baseSize", "20" );
00104 m_baseSize = baseSize.toInt();
00105
00106 if ( ! FontStyle::missingFonts( init ).isEmpty() ) {
00107 kdWarning( DEBUGID) << "Not all basic fonts found\n";
00108 }
00109 mathFont.fromString("Arev Sans");
00110 bracketFont.fromString("cmex10");
00111
00112
00113
00114 config->setGroup( "kformula Color" );
00115 defaultColor = config->readColorEntry( "defaultColor", &defaultColor );
00116 numberColor = config->readColorEntry( "numberColor", &numberColor );
00117 operatorColor = config->readColorEntry( "operatorColor", &operatorColor );
00118 emptyColor = config->readColorEntry( "emptyColor", &emptyColor );
00119 errorColor = config->readColorEntry( "errorColor", &errorColor );
00120 helpColor = config->readColorEntry( "helpColor", &helpColor );
00121
00122 m_syntaxHighlighting = config->readBoolEntry( "syntaxHighlighting", true );
00123 }
00124
00125 void ContextStyle::setZoomAndResolution( int zoom, int dpiX, int dpiY )
00126 {
00127 KoZoomHandler::setZoomAndResolution( zoom, dpiX, dpiY );
00128 }
00129
00130 bool ContextStyle::setZoomAndResolution( int zoom, double zoomX, double zoomY, bool, bool )
00131 {
00132 bool changes = m_zoom != zoom || m_zoomedResolutionX != zoomX || m_zoomedResolutionY != zoomY;
00133 m_zoom = zoom;
00134 m_zoomedResolutionX = zoomX;
00135 m_zoomedResolutionY = zoomY;
00136 return changes;
00137 }
00138
00139 QColor ContextStyle::getNumberColor() const
00140 {
00141 if ( edit() && syntaxHighlighting() ) {
00142 return numberColor;
00143 }
00144 return getDefaultColor();
00145 }
00146
00147 QColor ContextStyle::getOperatorColor() const
00148 {
00149 if ( edit() && syntaxHighlighting() ) {
00150 return operatorColor;
00151 }
00152 return getDefaultColor();
00153 }
00154
00155 QColor ContextStyle::getErrorColor() const
00156 {
00157 if ( edit() && syntaxHighlighting() ) {
00158 return errorColor;
00159 }
00160 return getDefaultColor();
00161 }
00162
00163 QColor ContextStyle::getEmptyColor() const
00164 {
00165 if ( edit() && syntaxHighlighting() ) {
00166 return emptyColor;
00167 }
00168 return getDefaultColor();
00169 }
00170
00171 QColor ContextStyle::getHelpColor() const
00172 {
00173 if ( edit() && syntaxHighlighting() ) {
00174 return helpColor;
00175 }
00176 return getDefaultColor();
00177 }
00178
00179 void ContextStyle::setDefaultColor( const QColor& color )
00180 {
00181 defaultColor = color;
00182 }
00183 void ContextStyle::setNumberColor( const QColor& color )
00184 {
00185 numberColor = color;
00186 }
00187 void ContextStyle::setOperatorColor( const QColor& color )
00188 {
00189 operatorColor = color;
00190 }
00191 void ContextStyle::setErrorColor( const QColor& color )
00192 {
00193 errorColor = color;
00194 }
00195 void ContextStyle::setEmptyColor( const QColor& color )
00196 {
00197 emptyColor = color;
00198 }
00199 void ContextStyle::setHelpColor( const QColor& color )
00200 {
00201 helpColor = color;
00202 }
00203
00204 #if 0
00205 const QStringList& ContextStyle::requestedFonts() const
00206 {
00207 return m_requestedFonts;
00208 }
00209
00210 void ContextStyle::setRequestedFonts( const QStringList& list )
00211 {
00212 m_requestedFonts = list;
00213
00214 }
00215 #endif
00216
00217 double ContextStyle::getReductionFactor( TextStyle tstyle ) const
00218 {
00219 return textStyleValues[ tstyle ].reductionFactor;
00220 }
00221
00222 luPt ContextStyle::getAdjustedSize( TextStyle tstyle, double factor ) const
00223 {
00224 return qRound( ptToLayoutUnitPt( m_sizeFactor
00225 * m_baseSize
00226 * getReductionFactor( tstyle )
00227 * factor
00228 * zoom() / 100.0 ) );
00229 }
00230
00231 luPixel ContextStyle::getSpace( TextStyle tstyle, SpaceWidth space, double factor ) const
00232 {
00233 switch ( space ) {
00234 case NEGTHIN: return -getThinSpace( tstyle, factor );
00235 case THIN: return getThinSpace( tstyle, factor );
00236 case MEDIUM: return getMediumSpace( tstyle, factor );
00237 case THICK: return getThickSpace( tstyle, factor );
00238 case QUAD: return getQuadSpace( tstyle, factor );
00239 }
00240 return 0;
00241 }
00242
00243 luPixel ContextStyle::getThinSpace( TextStyle tstyle, double factor ) const
00244 {
00245 return ptToPixelX( m_sizeFactor
00246 * textStyleValues[ tstyle ].thinSpace( quad )
00247 * factor
00248 * zoom() / 100.0 );
00249 }
00250
00251 luPixel ContextStyle::getMediumSpace( TextStyle tstyle, double factor ) const
00252 {
00253 return ptToPixelX( m_sizeFactor
00254 * textStyleValues[ tstyle ].mediumSpace( quad )
00255 * factor
00256 * zoom() / 100.0 );
00257 }
00258
00259 luPixel ContextStyle::getThickSpace( TextStyle tstyle, double factor ) const
00260 {
00261 return ptToPixelX( m_sizeFactor
00262 * textStyleValues[ tstyle ].thickSpace( quad )
00263 * factor
00264 * zoom() / 100.0 );
00265 }
00266
00267 luPixel ContextStyle::getQuadSpace( TextStyle tstyle, double factor ) const
00268 {
00269 return ptToPixelX( m_sizeFactor
00270 * textStyleValues[ tstyle ].quadSpace( quad )
00271 * factor
00272 * zoom() / 100.0 );
00273 }
00274
00275 luPixel ContextStyle::axisHeight( TextStyle tstyle, double factor ) const
00276 {
00277
00278 return static_cast<luPixel>( m_sizeFactor
00279 * textStyleValues[ tstyle ].axisHeight( m_axisHeight )
00280 * factor
00281 * zoom() / 100.0 );
00282 }
00283
00284 luPt ContextStyle::getBaseSize() const
00285 {
00286 return static_cast<luPt>( ptToLayoutUnitPt( m_sizeFactor*m_baseSize ) );
00287 }
00288
00289 void ContextStyle::setBaseSize( int size )
00290 {
00291
00292 if ( size != m_baseSize ) {
00293 m_baseSize = size;
00294 setup();
00295 }
00296 }
00297
00298 void ContextStyle::setSizeFactor( double factor )
00299 {
00300 m_sizeFactor = factor;
00301 }
00302
00303
00304 luPixel ContextStyle::getLineWidth( double factor ) const
00305 {
00306 return ptToLayoutUnitPixX( m_sizeFactor*lineWidth*factor*zoom() / 100.0 );
00307 }
00308
00309 luPixel ContextStyle::getEmptyRectWidth( double factor ) const
00310 {
00311 return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize*factor*(zoom() / 100.0)/1.8 );
00312 }
00313
00314 luPixel ContextStyle::getEmptyRectHeight( double factor ) const
00315 {
00316 return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize*factor*(zoom() / 100.0)/1.8 );
00317 }
00318
00319
00320 ContextStyle::TextStyle ContextStyle::convertTextStyleFraction( TextStyle tstyle ) const
00321 {
00322 TextStyle result;
00323
00324 switch ( tstyle ){
00325 case displayStyle:
00326 result = textStyle;
00327 break;
00328 case textStyle:
00329 result = scriptStyle;
00330 break;
00331 default:
00332 result = scriptScriptStyle;
00333 break;
00334 }
00335
00336 return result;
00337 }
00338
00339
00340 ContextStyle::TextStyle ContextStyle::convertTextStyleIndex( TextStyle tstyle ) const
00341 {
00342 TextStyle result;
00343
00344 switch ( tstyle ){
00345 case displayStyle:
00346 result = scriptStyle;
00347 break;
00348 case textStyle:
00349 result = scriptStyle;
00350 break;
00351 default:
00352 result = scriptScriptStyle;
00353 break;
00354 }
00355
00356 return result;
00357 }
00358
00359
00360 void ContextStyle::setup()
00361 {
00362 luPt size = static_cast<luPt>( m_baseSize );
00363 QFont font = symbolFont;
00364 font.setPointSize( size );
00365 QFontMetrics fm( font );
00366
00367
00368 quad = ptToLayoutUnitPt( fm.width( 'M' ) );
00369
00370 font = QFont(defaultFont);
00371 font.setPointSize( size );
00372 QFontMetrics fm2( font );
00373
00374
00375
00376 m_axisHeight = ptToLayoutUnitPixY( pixelYToPt( fm2.strikeOutPos() ) );
00377 }
00378
00379
00380 double StyleAttributes::sizeFactor() const
00381 {
00382 if ( m_size.empty() ) {
00383
00384 return 1.0;
00385 }
00386 return m_size.top();
00387 }
00388
00389 bool StyleAttributes::customMathVariant() const
00390 {
00391 if ( m_customMathVariant.empty() ) {
00392 return false;
00393 }
00394 return m_customMathVariant.top();
00395 }
00396
00397 CharStyle StyleAttributes::charStyle() const
00398 {
00399 if ( m_charStyle.empty() ) {
00400
00401 return anyChar;
00402 }
00403 return m_charStyle.top();
00404 }
00405
00406 CharFamily StyleAttributes::charFamily() const
00407 {
00408 if ( m_charFamily.empty() ) {
00409
00410 return anyFamily;
00411 }
00412 return m_charFamily.top();
00413 }
00414
00415 QColor StyleAttributes::color() const
00416 {
00417 if ( m_color.empty() ) {
00418
00419 return QColor( Qt::black );
00420
00421 }
00422 return m_color.top();
00423 }
00424
00425 QColor StyleAttributes::background() const
00426 {
00427 if ( m_background.empty() ) {
00428
00429 return QColor( Qt::color0 );
00430 }
00431 return m_background.top();
00432 }
00433
00434 QFont StyleAttributes::font() const
00435 {
00436 if ( m_font.empty() ) {
00437 return QFont();
00438 }
00439 return m_font.top();
00440 }
00441
00442 bool StyleAttributes::fontWeight() const
00443 {
00444 if ( m_fontWeight.empty() ) {
00445 return false;
00446 }
00447 return m_fontWeight.top();
00448 }
00449
00450 bool StyleAttributes::customFontWeight() const
00451 {
00452 if ( m_customFontWeight.empty() ) {
00453 return false;
00454 }
00455 return m_customFontWeight.top();
00456 }
00457
00458 bool StyleAttributes::fontStyle() const
00459 {
00460 if ( m_fontStyle.empty() ) {
00461 return false;
00462 }
00463 return m_fontStyle.top();
00464 }
00465
00466 bool StyleAttributes::customFontStyle() const
00467 {
00468 if ( m_customFontStyle.empty() ) {
00469 return false;
00470 }
00471 return m_customFontStyle.top();
00472 }
00473
00474 bool StyleAttributes::customFont() const
00475 {
00476 if ( m_customFontFamily.empty() ) {
00477 return false;
00478 }
00479 return m_customFontFamily.top();
00480 }
00481
00482 int StyleAttributes::scriptLevel() const
00483 {
00484 if ( m_scriptLevel.empty() ) {
00485 return 0;
00486 }
00487 return m_scriptLevel.top();
00488 }
00489
00490 double StyleAttributes::scriptSizeMultiplier() const
00491 {
00492 if ( m_scriptSizeMultiplier.empty() ) {
00493 return scriptsizemultiplier;
00494 }
00495 return m_scriptSizeMultiplier.top();
00496 }
00497
00498 double StyleAttributes::scriptMinSize() const
00499 {
00500 if ( m_scriptMinSize.empty() ) {
00501 return scriptminsize;
00502 }
00503 return m_scriptMinSize.top();
00504 }
00505
00506 double StyleAttributes::veryVeryThinMathSpace() const
00507 {
00508 if ( m_veryVeryThinMathSpace.empty() ) {
00509 return veryverythinmathspace;
00510 }
00511 return m_veryVeryThinMathSpace.top();
00512 }
00513
00514 double StyleAttributes::veryThinMathSpace() const
00515 {
00516 if ( m_veryThinMathSpace.empty() ) {
00517 return verythinmathspace;
00518 }
00519 return m_veryThinMathSpace.top();
00520 }
00521
00522 double StyleAttributes::thinMathSpace() const
00523 {
00524 if ( m_thinMathSpace.empty() ) {
00525 return thinmathspace;
00526 }
00527 return m_thinMathSpace.top();
00528 }
00529
00530 double StyleAttributes::mediumMathSpace() const
00531 {
00532 if ( m_mediumMathSpace.empty() ) {
00533 return mediummathspace;
00534 }
00535 return m_mediumMathSpace.top();
00536 }
00537
00538 double StyleAttributes::thickMathSpace() const
00539 {
00540 if ( m_thickMathSpace.empty() ) {
00541 return thickmathspace;
00542 }
00543 return m_thickMathSpace.top();
00544 }
00545
00546 double StyleAttributes::veryThickMathSpace() const
00547 {
00548 if ( m_veryThickMathSpace.empty() ) {
00549 return verythickmathspace;
00550 }
00551 return m_veryThickMathSpace.top();
00552 }
00553
00554 double StyleAttributes::veryVeryThickMathSpace() const
00555 {
00556 if ( m_veryVeryThickMathSpace.empty() ) {
00557 return veryverythickmathspace;
00558 }
00559 return m_veryVeryThickMathSpace.top();
00560 }
00561
00562 bool StyleAttributes::displayStyle() const
00563 {
00564 if ( m_displayStyle.empty() ) {
00565 return true;
00566 }
00567 return m_displayStyle.top();
00568 }
00569
00570 bool StyleAttributes::customDisplayStyle() const
00571 {
00572 if ( m_customDisplayStyle.empty() ) {
00573 return false;
00574 }
00575 return m_customDisplayStyle.top();
00576 }
00577
00578 double StyleAttributes::getSpace( SizeType type, double length ) const
00579 {
00580 switch ( type ) {
00581 case NegativeVeryVeryThinMathSpace:
00582 return - veryVeryThinMathSpace();
00583 case NegativeVeryThinMathSpace:
00584 return - veryThinMathSpace();
00585 case NegativeThinMathSpace:
00586 return - thinMathSpace();
00587 case NegativeMediumMathSpace:
00588 return - mediumMathSpace();
00589 case NegativeThickMathSpace:
00590 return - thickMathSpace();
00591 case NegativeVeryThickMathSpace:
00592 return - veryThickMathSpace();
00593 case NegativeVeryVeryThickMathSpace:
00594 return - veryVeryThickMathSpace();
00595 case VeryVeryThinMathSpace:
00596 return veryVeryThinMathSpace();
00597 case VeryThinMathSpace:
00598 return veryThinMathSpace();
00599 case ThinMathSpace:
00600 return thinMathSpace();
00601 case MediumMathSpace:
00602 return mediumMathSpace();
00603 case ThickMathSpace:
00604 return thickMathSpace();
00605 case VeryThickMathSpace:
00606 return veryThickMathSpace();
00607 case VeryVeryThickMathSpace:
00608 return veryVeryThickMathSpace();
00609 default:
00610 break;
00611 }
00612 return length;
00613 }
00614
00615 void StyleAttributes::resetSize()
00616 {
00617 if ( ! m_size.empty() ) {
00618 m_size.pop();
00619 }
00620 }
00621
00622 void StyleAttributes::resetCharStyle()
00623 {
00624 if ( ! m_charStyle.empty() ) {
00625 m_charStyle.pop();
00626 }
00627 }
00628
00629 void StyleAttributes::resetCharFamily()
00630 {
00631 if ( ! m_charFamily.empty() ) {
00632 m_charFamily.pop();
00633 }
00634 }
00635
00636 void StyleAttributes::resetColor()
00637 {
00638 if ( ! m_color.empty() ) {
00639 m_color.pop();
00640 }
00641 }
00642
00643 void StyleAttributes::resetBackground()
00644 {
00645 if ( ! m_background.empty() ) {
00646 m_background.pop();
00647 }
00648 }
00649
00650 void StyleAttributes::resetFontFamily()
00651 {
00652 if ( ! m_customFontFamily.empty() ) {
00653 if ( m_customFontFamily.pop() ) {
00654 if ( ! m_font.empty() ) {
00655 m_font.pop();
00656 }
00657 }
00658 }
00659 }
00660
00661 void StyleAttributes::resetFontWeight()
00662 {
00663 if ( ! m_customFontWeight.empty() ) {
00664 if ( m_customFontWeight.pop() ) {
00665 if ( ! m_fontWeight.empty() ) {
00666 m_fontWeight.pop();
00667 }
00668 }
00669 }
00670 }
00671
00672 void StyleAttributes::resetFontStyle()
00673 {
00674 if ( ! m_customFontStyle.empty() ) {
00675 if ( m_customFontStyle.pop() ) {
00676 if ( ! m_fontStyle.empty() ) {
00677 m_fontStyle.pop();
00678 }
00679 }
00680 }
00681 }
00682
00683 void StyleAttributes::resetScriptLevel()
00684 {
00685 if ( ! m_scriptLevel.empty() ) {
00686 m_scriptLevel.pop();
00687 }
00688 }
00689
00690 void StyleAttributes::resetScriptSizeMultiplier()
00691 {
00692 if ( ! m_scriptSizeMultiplier.empty() ) {
00693 m_scriptSizeMultiplier.pop();
00694 }
00695 }
00696
00697 void StyleAttributes::resetScriptMinSize()
00698 {
00699 if ( ! m_scriptMinSize.empty() ) {
00700 m_scriptMinSize.pop();
00701 }
00702 }
00703
00704 void StyleAttributes::resetVeryVeryThinMathSpace()
00705 {
00706 if ( ! m_veryVeryThinMathSpace.empty() ) {
00707 m_veryVeryThinMathSpace.pop();
00708 }
00709 }
00710
00711 void StyleAttributes::resetVeryThinMathSpace()
00712 {
00713 if ( ! m_veryThinMathSpace.empty() ) {
00714 m_veryThinMathSpace.pop();
00715 }
00716 }
00717
00718 void StyleAttributes::resetThinMathSpace()
00719 {
00720 if ( ! m_thinMathSpace.empty() ) {
00721 m_thinMathSpace.pop();
00722 }
00723 }
00724
00725 void StyleAttributes::resetMediumMathSpace()
00726 {
00727 if ( ! m_mediumMathSpace.empty() ) {
00728 m_mediumMathSpace.pop();
00729 }
00730 }
00731
00732 void StyleAttributes::resetThickMathSpace()
00733 {
00734 if ( ! m_thickMathSpace.empty() ) {
00735 m_thickMathSpace.pop();
00736 }
00737 }
00738
00739 void StyleAttributes::resetVeryThickMathSpace()
00740 {
00741 if ( ! m_veryThickMathSpace.empty() ) {
00742 m_veryThickMathSpace.pop();
00743 }
00744 }
00745
00746 void StyleAttributes::resetVeryVeryThickMathSpace()
00747 {
00748 if ( ! m_veryVeryThickMathSpace.empty() ) {
00749 m_veryVeryThickMathSpace.pop();
00750 }
00751 }
00752
00753 void StyleAttributes::resetDisplayStyle()
00754 {
00755 if ( ! m_customDisplayStyle.empty() ) {
00756 if ( m_customDisplayStyle.pop() ) {
00757 if ( ! m_displayStyle.empty() ) {
00758 m_displayStyle.pop();
00759 }
00760 }
00761 }
00762 }
00763
00764
00765 KFORMULA_NAMESPACE_END