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 }
00229
00230 luPixel ContextStyle::getSpace( TextStyle tstyle, SpaceWidth space, double factor ) const
00231 {
00232 switch ( space ) {
00233 case NEGTHIN: return -getThinSpace( tstyle, factor );
00234 case THIN: return getThinSpace( tstyle, factor );
00235 case MEDIUM: return getMediumSpace( tstyle, factor );
00236 case THICK: return getThickSpace( tstyle, factor );
00237 case QUAD: return getQuadSpace( tstyle, factor );
00238 }
00239 return 0;
00240 }
00241
00242 luPixel ContextStyle::getThinSpace( TextStyle tstyle, double factor ) const
00243 {
00244 return ptToPixelX( m_sizeFactor
00245 * textStyleValues[ tstyle ].thinSpace( quad )
00246 * factor );
00247 }
00248
00249 luPixel ContextStyle::getMediumSpace( TextStyle tstyle, double factor ) const
00250 {
00251 return ptToPixelX( m_sizeFactor
00252 * textStyleValues[ tstyle ].mediumSpace( quad )
00253 * factor );
00254 }
00255
00256 luPixel ContextStyle::getThickSpace( TextStyle tstyle, double factor ) const
00257 {
00258 return ptToPixelX( m_sizeFactor
00259 * textStyleValues[ tstyle ].thickSpace( quad )
00260 * factor );
00261 }
00262
00263 luPixel ContextStyle::getQuadSpace( TextStyle tstyle, double factor ) const
00264 {
00265 return ptToPixelX( m_sizeFactor
00266 * textStyleValues[ tstyle ].quadSpace( quad )
00267 * factor );
00268 }
00269
00270 luPixel ContextStyle::axisHeight( TextStyle tstyle, double factor ) const
00271 {
00272
00273 return static_cast<luPixel>( m_sizeFactor
00274 * textStyleValues[ tstyle ].axisHeight( m_axisHeight )
00275 * factor );
00276 }
00277
00278 luPt ContextStyle::getBaseSize() const
00279 {
00280 return static_cast<luPt>( ptToLayoutUnitPt( m_sizeFactor*m_baseSize ) );
00281 }
00282
00283 void ContextStyle::setBaseSize( int size )
00284 {
00285
00286 if ( size != m_baseSize ) {
00287 m_baseSize = size;
00288 setup();
00289 }
00290 }
00291
00292 void ContextStyle::setSizeFactor( double factor )
00293 {
00294 m_sizeFactor = factor;
00295 }
00296
00297
00298 luPixel ContextStyle::getLineWidth( double factor ) const
00299 {
00300 return ptToLayoutUnitPixX( m_sizeFactor*lineWidth*factor );
00301 }
00302
00303 luPixel ContextStyle::getEmptyRectWidth( double factor ) const
00304 {
00305 return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize*factor/1.8 );
00306 }
00307
00308 luPixel ContextStyle::getEmptyRectHeight( double factor ) const
00309 {
00310 return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize*factor/1.8 );
00311 }
00312
00313
00314 ContextStyle::TextStyle ContextStyle::convertTextStyleFraction( TextStyle tstyle ) const
00315 {
00316 TextStyle result;
00317
00318 switch ( tstyle ){
00319 case displayStyle:
00320 result = textStyle;
00321 break;
00322 case textStyle:
00323 result = scriptStyle;
00324 break;
00325 default:
00326 result = scriptScriptStyle;
00327 break;
00328 }
00329
00330 return result;
00331 }
00332
00333
00334 ContextStyle::TextStyle ContextStyle::convertTextStyleIndex( TextStyle tstyle ) const
00335 {
00336 TextStyle result;
00337
00338 switch ( tstyle ){
00339 case displayStyle:
00340 result = scriptStyle;
00341 break;
00342 case textStyle:
00343 result = scriptStyle;
00344 break;
00345 default:
00346 result = scriptScriptStyle;
00347 break;
00348 }
00349
00350 return result;
00351 }
00352
00353
00354 void ContextStyle::setup()
00355 {
00356 luPt size = static_cast<luPt>( m_baseSize );
00357 QFont font = symbolFont;
00358 font.setPointSize( size );
00359 QFontMetrics fm( font );
00360
00361
00362 quad = ptToLayoutUnitPt( fm.width( 'M' ) );
00363
00364 font = QFont(defaultFont);
00365 font.setPointSize( size );
00366 QFontMetrics fm2( font );
00367
00368
00369
00370 m_axisHeight = ptToLayoutUnitPixY( pixelYToPt( fm2.strikeOutPos() ) );
00371 }
00372
00373
00374 double StyleAttributes::sizeFactor() const
00375 {
00376 if ( m_size.empty() ) {
00377
00378 return 1.0;
00379 }
00380 return m_size.top();
00381 }
00382
00383 bool StyleAttributes::customMathVariant() const
00384 {
00385 if ( m_customMathVariant.empty() ) {
00386 return false;
00387 }
00388 return m_customMathVariant.top();
00389 }
00390
00391 CharStyle StyleAttributes::charStyle() const
00392 {
00393 if ( m_charStyle.empty() ) {
00394
00395 return anyChar;
00396 }
00397 return m_charStyle.top();
00398 }
00399
00400 CharFamily StyleAttributes::charFamily() const
00401 {
00402 if ( m_charFamily.empty() ) {
00403
00404 return anyFamily;
00405 }
00406 return m_charFamily.top();
00407 }
00408
00409 QColor StyleAttributes::color() const
00410 {
00411 if ( m_color.empty() ) {
00412
00413 return QColor( Qt::black );
00414
00415 }
00416 return m_color.top();
00417 }
00418
00419 QColor StyleAttributes::background() const
00420 {
00421 if ( m_background.empty() ) {
00422
00423 return QColor( Qt::color0 );
00424 }
00425 return m_background.top();
00426 }
00427
00428 QFont StyleAttributes::font() const
00429 {
00430 if ( m_font.empty() ) {
00431 return QFont();
00432 }
00433 return m_font.top();
00434 }
00435
00436 bool StyleAttributes::fontWeight() const
00437 {
00438 if ( m_fontWeight.empty() ) {
00439 return false;
00440 }
00441 return m_fontWeight.top();
00442 }
00443
00444 bool StyleAttributes::customFontWeight() const
00445 {
00446 if ( m_customFontWeight.empty() ) {
00447 return false;
00448 }
00449 return m_customFontWeight.top();
00450 }
00451
00452 bool StyleAttributes::fontStyle() const
00453 {
00454 if ( m_fontStyle.empty() ) {
00455 return false;
00456 }
00457 return m_fontStyle.top();
00458 }
00459
00460 bool StyleAttributes::customFontStyle() const
00461 {
00462 if ( m_customFontStyle.empty() ) {
00463 return false;
00464 }
00465 return m_customFontStyle.top();
00466 }
00467
00468 bool StyleAttributes::customFont() const
00469 {
00470 if ( m_customFontFamily.empty() ) {
00471 return false;
00472 }
00473 return m_customFontFamily.top();
00474 }
00475
00476 int StyleAttributes::scriptLevel() const
00477 {
00478 if ( m_scriptLevel.empty() ) {
00479 return 0;
00480 }
00481 return m_scriptLevel.top();
00482 }
00483
00484 double StyleAttributes::scriptSizeMultiplier() const
00485 {
00486 if ( m_scriptSizeMultiplier.empty() ) {
00487 return scriptsizemultiplier;
00488 }
00489 return m_scriptSizeMultiplier.top();
00490 }
00491
00492 double StyleAttributes::scriptMinSize() const
00493 {
00494 if ( m_scriptMinSize.empty() ) {
00495 return scriptminsize;
00496 }
00497 return m_scriptMinSize.top();
00498 }
00499
00500 double StyleAttributes::veryVeryThinMathSpace() const
00501 {
00502 if ( m_veryVeryThinMathSpace.empty() ) {
00503 return veryverythinmathspace;
00504 }
00505 return m_veryVeryThinMathSpace.top();
00506 }
00507
00508 double StyleAttributes::veryThinMathSpace() const
00509 {
00510 if ( m_veryThinMathSpace.empty() ) {
00511 return verythinmathspace;
00512 }
00513 return m_veryThinMathSpace.top();
00514 }
00515
00516 double StyleAttributes::thinMathSpace() const
00517 {
00518 if ( m_thinMathSpace.empty() ) {
00519 return thinmathspace;
00520 }
00521 return m_thinMathSpace.top();
00522 }
00523
00524 double StyleAttributes::mediumMathSpace() const
00525 {
00526 if ( m_mediumMathSpace.empty() ) {
00527 return mediummathspace;
00528 }
00529 return m_mediumMathSpace.top();
00530 }
00531
00532 double StyleAttributes::thickMathSpace() const
00533 {
00534 if ( m_thickMathSpace.empty() ) {
00535 return thickmathspace;
00536 }
00537 return m_thickMathSpace.top();
00538 }
00539
00540 double StyleAttributes::veryThickMathSpace() const
00541 {
00542 if ( m_veryThickMathSpace.empty() ) {
00543 return verythickmathspace;
00544 }
00545 return m_veryThickMathSpace.top();
00546 }
00547
00548 double StyleAttributes::veryVeryThickMathSpace() const
00549 {
00550 if ( m_veryVeryThickMathSpace.empty() ) {
00551 return veryverythickmathspace;
00552 }
00553 return m_veryVeryThickMathSpace.top();
00554 }
00555
00556 bool StyleAttributes::displayStyle() const
00557 {
00558 if ( m_displayStyle.empty() ) {
00559 return true;
00560 }
00561 return m_displayStyle.top();
00562 }
00563
00564 bool StyleAttributes::customDisplayStyle() const
00565 {
00566 if ( m_customDisplayStyle.empty() ) {
00567 return false;
00568 }
00569 return m_customDisplayStyle.top();
00570 }
00571
00572 double StyleAttributes::getSpace( SizeType type, double length ) const
00573 {
00574 switch ( type ) {
00575 case NegativeVeryVeryThinMathSpace:
00576 return - veryVeryThinMathSpace();
00577 case NegativeVeryThinMathSpace:
00578 return - veryThinMathSpace();
00579 case NegativeThinMathSpace:
00580 return - thinMathSpace();
00581 case NegativeMediumMathSpace:
00582 return - mediumMathSpace();
00583 case NegativeThickMathSpace:
00584 return - thickMathSpace();
00585 case NegativeVeryThickMathSpace:
00586 return - veryThickMathSpace();
00587 case NegativeVeryVeryThickMathSpace:
00588 return - veryVeryThickMathSpace();
00589 case VeryVeryThinMathSpace:
00590 return veryVeryThinMathSpace();
00591 case VeryThinMathSpace:
00592 return veryThinMathSpace();
00593 case ThinMathSpace:
00594 return thinMathSpace();
00595 case MediumMathSpace:
00596 return mediumMathSpace();
00597 case ThickMathSpace:
00598 return thickMathSpace();
00599 case VeryThickMathSpace:
00600 return veryThickMathSpace();
00601 case VeryVeryThickMathSpace:
00602 return veryVeryThickMathSpace();
00603 default:
00604 break;
00605 }
00606 return length;
00607 }
00608
00609 void StyleAttributes::resetSize()
00610 {
00611 if ( ! m_size.empty() ) {
00612 m_size.pop();
00613 }
00614 }
00615
00616 void StyleAttributes::resetCharStyle()
00617 {
00618 if ( ! m_charStyle.empty() ) {
00619 m_charStyle.pop();
00620 }
00621 }
00622
00623 void StyleAttributes::resetCharFamily()
00624 {
00625 if ( ! m_charFamily.empty() ) {
00626 m_charFamily.pop();
00627 }
00628 }
00629
00630 void StyleAttributes::resetColor()
00631 {
00632 if ( ! m_color.empty() ) {
00633 m_color.pop();
00634 }
00635 }
00636
00637 void StyleAttributes::resetBackground()
00638 {
00639 if ( ! m_background.empty() ) {
00640 m_background.pop();
00641 }
00642 }
00643
00644 void StyleAttributes::resetFontFamily()
00645 {
00646 if ( ! m_customFontFamily.empty() ) {
00647 if ( m_customFontFamily.pop() ) {
00648 if ( ! m_font.empty() ) {
00649 m_font.pop();
00650 }
00651 }
00652 }
00653 }
00654
00655 void StyleAttributes::resetFontWeight()
00656 {
00657 if ( ! m_customFontWeight.empty() ) {
00658 if ( m_customFontWeight.pop() ) {
00659 if ( ! m_fontWeight.empty() ) {
00660 m_fontWeight.pop();
00661 }
00662 }
00663 }
00664 }
00665
00666 void StyleAttributes::resetFontStyle()
00667 {
00668 if ( ! m_customFontStyle.empty() ) {
00669 if ( m_customFontStyle.pop() ) {
00670 if ( ! m_fontStyle.empty() ) {
00671 m_fontStyle.pop();
00672 }
00673 }
00674 }
00675 }
00676
00677 void StyleAttributes::resetScriptLevel()
00678 {
00679 if ( ! m_scriptLevel.empty() ) {
00680 m_scriptLevel.pop();
00681 }
00682 }
00683
00684 void StyleAttributes::resetScriptSizeMultiplier()
00685 {
00686 if ( ! m_scriptSizeMultiplier.empty() ) {
00687 m_scriptSizeMultiplier.pop();
00688 }
00689 }
00690
00691 void StyleAttributes::resetScriptMinSize()
00692 {
00693 if ( ! m_scriptMinSize.empty() ) {
00694 m_scriptMinSize.pop();
00695 }
00696 }
00697
00698 void StyleAttributes::resetVeryVeryThinMathSpace()
00699 {
00700 if ( ! m_veryVeryThinMathSpace.empty() ) {
00701 m_veryVeryThinMathSpace.pop();
00702 }
00703 }
00704
00705 void StyleAttributes::resetVeryThinMathSpace()
00706 {
00707 if ( ! m_veryThinMathSpace.empty() ) {
00708 m_veryThinMathSpace.pop();
00709 }
00710 }
00711
00712 void StyleAttributes::resetThinMathSpace()
00713 {
00714 if ( ! m_thinMathSpace.empty() ) {
00715 m_thinMathSpace.pop();
00716 }
00717 }
00718
00719 void StyleAttributes::resetMediumMathSpace()
00720 {
00721 if ( ! m_mediumMathSpace.empty() ) {
00722 m_mediumMathSpace.pop();
00723 }
00724 }
00725
00726 void StyleAttributes::resetThickMathSpace()
00727 {
00728 if ( ! m_thickMathSpace.empty() ) {
00729 m_thickMathSpace.pop();
00730 }
00731 }
00732
00733 void StyleAttributes::resetVeryThickMathSpace()
00734 {
00735 if ( ! m_veryThickMathSpace.empty() ) {
00736 m_veryThickMathSpace.pop();
00737 }
00738 }
00739
00740 void StyleAttributes::resetVeryVeryThickMathSpace()
00741 {
00742 if ( ! m_veryVeryThickMathSpace.empty() ) {
00743 m_veryVeryThickMathSpace.pop();
00744 }
00745 }
00746
00747 void StyleAttributes::resetDisplayStyle()
00748 {
00749 if ( ! m_customDisplayStyle.empty() ) {
00750 if ( m_customDisplayStyle.pop() ) {
00751 if ( ! m_displayStyle.empty() ) {
00752 m_displayStyle.pop();
00753 }
00754 }
00755 }
00756 }
00757
00758
00759 KFORMULA_NAMESPACE_END