00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qfontmetrics.h>
00022 #include <qstring.h>
00023
00024 #include <kdebug.h>
00025 #include <koGlobal.h>
00026
00027 #include "cmstyle.h"
00028 #include "contextstyle.h"
00029 #include "esstixfontstyle.h"
00030 #include "symbolfontstyle.h"
00031
00032
00033 KFORMULA_NAMESPACE_BEGIN
00034
00035
00036 ContextStyle::ContextStyle()
00037 : symbolFont( "Symbol" ),
00038 defaultColor(Qt::black), numberColor(Qt::blue),
00039 operatorColor(Qt::darkGreen), errorColor(Qt::darkRed),
00040 emptyColor(Qt::blue), helpColor( Qt::gray ),
00041 m_sizeFactor( 0 )
00042 {
00043
00044
00045
00046
00047
00048
00049
00050 textStyleValues[ displayStyle ].setup( 1. );
00051 textStyleValues[ textStyle ].setup( 1. );
00052 textStyleValues[ scriptStyle ].setup( .7 );
00053 textStyleValues[ scriptScriptStyle ].setup( .49 );
00054
00055 m_baseTextStyle = displayStyle;
00056
00057 lineWidth = 1;
00058 linearMovement = false;
00059 centerSymbol = true;
00060 m_syntaxHighlighting = true;
00061
00062 m_fontStyle = 0;
00063 }
00064
00065
00066 ContextStyle::~ContextStyle()
00067 {
00068 delete m_fontStyle;
00069 }
00070
00071
00072 void ContextStyle::init()
00073 {
00074 setup();
00075 setFontStyle( m_fontStyleName );
00076 }
00077
00078
00079 void ContextStyle::setFontStyle( const QString& fontStyle )
00080 {
00081 delete m_fontStyle;
00082 m_fontStyleName = fontStyle;
00083 if ( m_fontStyleName == "tex" ) {
00084 m_fontStyle = new CMStyle();
00085 if ( !m_fontStyle->init( this ) ) {
00086 }
00087 }
00088 else if ( m_fontStyleName == "esstix" ) {
00089 m_fontStyle = new EsstixFontStyle();
00090 if ( !m_fontStyle->init( this ) ) {
00091 }
00092 }
00093 else {
00094
00095 m_fontStyle = new SymbolFontStyle();
00096 m_fontStyle->init( this );
00097 }
00098 }
00099
00100
00101 const SymbolTable& ContextStyle::symbolTable() const
00102 {
00103 return *( m_fontStyle->symbolTable() );
00104 }
00105
00106
00107 void ContextStyle::readConfig( KConfig* config )
00108 {
00109 config->setGroup( "kformula Font" );
00110 QString fontName = config->readEntry( "defaultFont", "Times,12,-1,5,50,1,0,0,0,0" );
00111 defaultFont.fromString( fontName );
00112 fontName = config->readEntry( "nameFont", "Times,12,-1,5,50,0,0,0,0,0" );
00113 nameFont.fromString( fontName );
00114 fontName = config->readEntry( "numberFont", "Times,12,-1,5,50,0,0,0,0,0" );
00115 numberFont.fromString( fontName );
00116 fontName = config->readEntry( "operatorFont", "Times,12,-1,5,50,0,0,0,0,0" );
00117 operatorFont.fromString( fontName );
00118 QString baseSize = config->readEntry( "baseSize", "20" );
00119 m_baseSize = baseSize.toInt();
00120
00121 m_fontStyleName = config->readEntry( "fontStyle" );
00122
00123 if ( m_fontStyleName.isEmpty() ) {
00124 if (CMStyle::missingFonts().isEmpty())
00125 m_fontStyleName = "tex";
00126 else if (EsstixFontStyle::missingFonts().isEmpty())
00127 m_fontStyleName = "esstix";
00128 else
00129 m_fontStyleName = "symbol";
00130 }
00131
00132 #if 0
00133 m_requestedFonts = config->readListEntry( "usedMathFonts" );
00134 if ( m_requestedFonts.size() == 0 ) {
00135 m_requestedFonts.push_back( "esstixone" );
00136 m_requestedFonts.push_back( "esstixtwo" );
00137 m_requestedFonts.push_back( "esstixthree" );
00138 m_requestedFonts.push_back( "esstixfour" );
00139 m_requestedFonts.push_back( "esstixfive" );
00140 m_requestedFonts.push_back( "esstixsix" );
00141 m_requestedFonts.push_back( "esstixseven" );
00142 m_requestedFonts.push_back( "esstixeight" );
00143 m_requestedFonts.push_back( "esstixnine" );
00144 m_requestedFonts.push_back( "esstixten" );
00145 m_requestedFonts.push_back( "esstixeleven" );
00146 m_requestedFonts.push_back( "esstixtwelve" );
00147 m_requestedFonts.push_back( "esstixthirteen" );
00148 m_requestedFonts.push_back( "esstixfourteen" );
00149 m_requestedFonts.push_back( "esstixfifteen" );
00150 m_requestedFonts.push_back( "esstixsixteen" );
00151 m_requestedFonts.push_back( "esstixseventeen" );
00152
00153
00154
00155
00156
00157 }
00158 #endif
00159
00160
00161 config->setGroup( "kformula Color" );
00162 defaultColor = config->readColorEntry( "defaultColor", &defaultColor );
00163 numberColor = config->readColorEntry( "numberColor", &numberColor );
00164 operatorColor = config->readColorEntry( "operatorColor", &operatorColor );
00165 emptyColor = config->readColorEntry( "emptyColor", &emptyColor );
00166 errorColor = config->readColorEntry( "errorColor", &errorColor );
00167 helpColor = config->readColorEntry( "helpColor", &helpColor );
00168
00169 m_syntaxHighlighting = config->readBoolEntry( "syntaxHighlighting", true );
00170 }
00171
00172 void ContextStyle::setZoomAndResolution( int zoom, int dpiX, int dpiY )
00173 {
00174 KoZoomHandler::setZoomAndResolution( zoom, dpiX, dpiY );
00175 }
00176
00177 bool ContextStyle::setZoomAndResolution( int zoom, double zoomX, double zoomY, bool, bool )
00178 {
00179 bool changes = m_zoom != zoom || m_zoomedResolutionX != zoomX || m_zoomedResolutionY != zoomY;
00180 m_zoom = zoom;
00181 m_zoomedResolutionX = zoomX;
00182 m_zoomedResolutionY = zoomY;
00183 return changes;
00184 }
00185
00186 QColor ContextStyle::getNumberColor() const
00187 {
00188 if ( edit() && syntaxHighlighting() ) {
00189 return numberColor;
00190 }
00191 return getDefaultColor();
00192 }
00193
00194 QColor ContextStyle::getOperatorColor() const
00195 {
00196 if ( edit() && syntaxHighlighting() ) {
00197 return operatorColor;
00198 }
00199 return getDefaultColor();
00200 }
00201
00202 QColor ContextStyle::getErrorColor() const
00203 {
00204 if ( edit() && syntaxHighlighting() ) {
00205 return errorColor;
00206 }
00207 return getDefaultColor();
00208 }
00209
00210 QColor ContextStyle::getEmptyColor() const
00211 {
00212 if ( edit() && syntaxHighlighting() ) {
00213 return emptyColor;
00214 }
00215 return getDefaultColor();
00216 }
00217
00218 QColor ContextStyle::getHelpColor() const
00219 {
00220 if ( edit() && syntaxHighlighting() ) {
00221 return helpColor;
00222 }
00223 return getDefaultColor();
00224 }
00225
00226 void ContextStyle::setDefaultColor( const QColor& color )
00227 {
00228 defaultColor = color;
00229 }
00230 void ContextStyle::setNumberColor( const QColor& color )
00231 {
00232 numberColor = color;
00233 }
00234 void ContextStyle::setOperatorColor( const QColor& color )
00235 {
00236 operatorColor = color;
00237 }
00238 void ContextStyle::setErrorColor( const QColor& color )
00239 {
00240 errorColor = color;
00241 }
00242 void ContextStyle::setEmptyColor( const QColor& color )
00243 {
00244 emptyColor = color;
00245 }
00246 void ContextStyle::setHelpColor( const QColor& color )
00247 {
00248 helpColor = color;
00249 }
00250
00251 #if 0
00252 const QStringList& ContextStyle::requestedFonts() const
00253 {
00254 return m_requestedFonts;
00255 }
00256
00257 void ContextStyle::setRequestedFonts( const QStringList& list )
00258 {
00259 m_requestedFonts = list;
00260
00261 }
00262 #endif
00263
00264 double ContextStyle::getReductionFactor( TextStyle tstyle ) const
00265 {
00266 return textStyleValues[ tstyle ].reductionFactor;
00267 }
00268
00269 luPt ContextStyle::getAdjustedSize( TextStyle tstyle ) const
00270 {
00271 return qRound( ptToLayoutUnitPt( m_sizeFactor*m_baseSize*getReductionFactor( tstyle ) ) );
00272 }
00273
00274 luPixel ContextStyle::getSpace( TextStyle tstyle, SpaceWidth space ) const
00275 {
00276 switch ( space ) {
00277 case NEGTHIN: return -getThinSpace( tstyle );
00278 case THIN: return getThinSpace( tstyle );
00279 case MEDIUM: return getMediumSpace( tstyle );
00280 case THICK: return getThickSpace( tstyle );
00281 case QUAD: return getQuadSpace( tstyle );
00282 }
00283 return 0;
00284 }
00285
00286 luPixel ContextStyle::getThinSpace( TextStyle tstyle ) const
00287 {
00288 return ptToPixelX( m_sizeFactor*textStyleValues[ tstyle ].thinSpace( quad ) );
00289 }
00290
00291 luPixel ContextStyle::getMediumSpace( TextStyle tstyle ) const
00292 {
00293 return ptToPixelX( m_sizeFactor*textStyleValues[ tstyle ].mediumSpace( quad ) );
00294 }
00295
00296 luPixel ContextStyle::getThickSpace( TextStyle tstyle ) const
00297 {
00298 return ptToPixelX( m_sizeFactor*textStyleValues[ tstyle ].thickSpace( quad ) );
00299 }
00300
00301 luPixel ContextStyle::getQuadSpace( TextStyle tstyle ) const
00302 {
00303 return ptToPixelX( m_sizeFactor*textStyleValues[ tstyle ].quadSpace( quad ) );
00304 }
00305
00306 luPixel ContextStyle::axisHeight( TextStyle tstyle ) const
00307 {
00308
00309 return static_cast<luPixel>( m_sizeFactor*textStyleValues[ tstyle ].axisHeight( m_axisHeight ) );
00310 }
00311
00312 luPt ContextStyle::getBaseSize() const
00313 {
00314 return static_cast<luPt>( ptToLayoutUnitPt( m_sizeFactor*m_baseSize ) );
00315 }
00316
00317 void ContextStyle::setBaseSize( int size )
00318 {
00319
00320 if ( size != m_baseSize ) {
00321 m_baseSize = size;
00322 setup();
00323 }
00324 }
00325
00326 void ContextStyle::setSizeFactor( double factor )
00327 {
00328 m_sizeFactor = factor;
00329 }
00330
00331
00332 luPixel ContextStyle::getLineWidth() const
00333 {
00334 return ptToLayoutUnitPixX( m_sizeFactor*lineWidth );
00335 }
00336
00337 luPixel ContextStyle::getEmptyRectWidth() const
00338 {
00339 return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize/1.8 );
00340 }
00341
00342 luPixel ContextStyle::getEmptyRectHeight() const
00343 {
00344 return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize/1.8 );
00345 }
00346
00347
00348 ContextStyle::TextStyle ContextStyle::convertTextStyleFraction( TextStyle tstyle ) const
00349 {
00350 TextStyle result;
00351
00352 switch ( tstyle ){
00353 case displayStyle:
00354 result = textStyle;
00355 break;
00356 case textStyle:
00357 result = scriptStyle;
00358 break;
00359 default:
00360 result = scriptScriptStyle;
00361 break;
00362 }
00363
00364 return result;
00365 }
00366
00367
00368 ContextStyle::TextStyle ContextStyle::convertTextStyleIndex( TextStyle tstyle ) const
00369 {
00370 TextStyle result;
00371
00372 switch ( tstyle ){
00373 case displayStyle:
00374 result = scriptStyle;
00375 break;
00376 case textStyle:
00377 result = scriptStyle;
00378 break;
00379 default:
00380 result = scriptScriptStyle;
00381 break;
00382 }
00383
00384 return result;
00385 }
00386
00387
00388 void ContextStyle::setup()
00389 {
00390 luPt size = static_cast<luPt>( m_baseSize );
00391 QFont font = symbolFont;
00392 font.setPointSize( size );
00393 QFontMetrics fm( font );
00394
00395
00396 quad = ptToLayoutUnitPt( fm.width( 'M' ) );
00397
00398 font = QFont(defaultFont);
00399 font.setPointSize( size );
00400 QFontMetrics fm2( font );
00401
00402
00403
00404 m_axisHeight = ptToLayoutUnitPixY( pixelYToPt( fm2.strikeOutPos() ) );
00405 }
00406
00407 KFORMULA_NAMESPACE_END