lib Library API Documentation

contextstyle.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
00003                   Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
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 //     kdDebug() << "ContextStyle::ContextStyle" << endl
00044 //               << "defaultFont: " << defaultFont.rawName() << endl
00045 //               << "nameFont: " << nameFont.rawName() << endl
00046 //               << "numberFont: " << numberFont.rawName() << endl
00047 //               << "operatorFont: " << operatorFont.rawName() << endl
00048 //               << "symbolFont: " << symbolFont.rawName() << endl;
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         // The SymbolFontStyle is always expected to work.
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", "symbol" );
00122 
00123 #if 0
00124     m_requestedFonts = config->readListEntry( "usedMathFonts" );
00125     if ( m_requestedFonts.size() == 0 ) {
00126         m_requestedFonts.push_back( "esstixone" );
00127         m_requestedFonts.push_back( "esstixtwo" );
00128         m_requestedFonts.push_back( "esstixthree" );
00129         m_requestedFonts.push_back( "esstixfour" );
00130         m_requestedFonts.push_back( "esstixfive" );
00131         m_requestedFonts.push_back( "esstixsix" );
00132         m_requestedFonts.push_back( "esstixseven" );
00133         m_requestedFonts.push_back( "esstixeight" );
00134         m_requestedFonts.push_back( "esstixnine" );
00135         m_requestedFonts.push_back( "esstixten" );
00136         m_requestedFonts.push_back( "esstixeleven" );
00137         m_requestedFonts.push_back( "esstixtwelve" );
00138         m_requestedFonts.push_back( "esstixthirteen" );
00139         m_requestedFonts.push_back( "esstixfourteen" );
00140         m_requestedFonts.push_back( "esstixfifteen" );
00141         m_requestedFonts.push_back( "esstixsixteen" );
00142         m_requestedFonts.push_back( "esstixseventeen" );
00143 //         m_requestedFonts.push_back( "mt extra" );
00144 //         m_requestedFonts.push_back( "mt symbol" );
00145 //         m_requestedFonts.push_back( "euclid math one" );
00146 //         m_requestedFonts.push_back( "euclid math two" );
00147 //         m_requestedFonts.push_back( "euclid symbol" );
00148     }
00149 #endif
00150 
00151     // There's no gui right anymore but I'll leave it here...
00152     config->setGroup( "kformula Color" );
00153     defaultColor  = config->readColorEntry( "defaultColor",  &defaultColor );
00154     numberColor   = config->readColorEntry( "numberColor",   &numberColor );
00155     operatorColor = config->readColorEntry( "operatorColor", &operatorColor );
00156     emptyColor    = config->readColorEntry( "emptyColor",    &emptyColor );
00157     errorColor    = config->readColorEntry( "errorColor",    &errorColor );
00158     helpColor     = config->readColorEntry( "helpColor",     &helpColor );
00159 
00160     m_syntaxHighlighting = config->readBoolEntry( "syntaxHighlighting", true );
00161 }
00162 
00163 void ContextStyle::setZoomAndResolution( int zoom, int dpiX, int dpiY )
00164 {
00165     KoZoomHandler::setZoomAndResolution( zoom, dpiX, dpiY );
00166 }
00167 
00168 bool ContextStyle::setZoomAndResolution( int zoom, double zoomX, double zoomY, bool, bool )
00169 {
00170     bool changes = m_zoom != zoom || m_zoomedResolutionX != zoomX || m_zoomedResolutionY != zoomY;
00171     m_zoom = zoom;
00172     m_zoomedResolutionX = zoomX;
00173     m_zoomedResolutionY = zoomY;
00174     return changes;
00175 }
00176 
00177 QColor ContextStyle::getNumberColor()   const
00178 {
00179     if ( edit() && syntaxHighlighting() ) {
00180         return numberColor;
00181     }
00182     return getDefaultColor();
00183 }
00184 
00185 QColor ContextStyle::getOperatorColor() const
00186 {
00187     if ( edit() && syntaxHighlighting() ) {
00188         return operatorColor;
00189     }
00190     return getDefaultColor();
00191 }
00192 
00193 QColor ContextStyle::getErrorColor()    const
00194 {
00195     if ( edit() && syntaxHighlighting() ) {
00196         return errorColor;
00197     }
00198     return getDefaultColor();
00199 }
00200 
00201 QColor ContextStyle::getEmptyColor()    const
00202 {
00203     if ( edit() && syntaxHighlighting() ) {
00204         return emptyColor;
00205     }
00206     return getDefaultColor();
00207 }
00208 
00209 QColor ContextStyle::getHelpColor()     const
00210 {
00211     if ( edit() && syntaxHighlighting() ) {
00212         return helpColor;
00213     }
00214     return getDefaultColor();
00215 }
00216 
00217 void ContextStyle::setDefaultColor( const QColor& color )
00218 {
00219     defaultColor = color;
00220 }
00221 void ContextStyle::setNumberColor( const QColor& color )
00222 {
00223     numberColor = color;
00224 }
00225 void ContextStyle::setOperatorColor( const QColor& color )
00226 {
00227     operatorColor = color;
00228 }
00229 void ContextStyle::setErrorColor( const QColor& color )
00230 {
00231     errorColor = color;
00232 }
00233 void ContextStyle::setEmptyColor( const QColor& color )
00234 {
00235     emptyColor = color;
00236 }
00237 void ContextStyle::setHelpColor( const QColor& color )
00238 {
00239     helpColor = color;
00240 }
00241 
00242 #if 0
00243 const QStringList& ContextStyle::requestedFonts() const
00244 {
00245     return m_requestedFonts;
00246 }
00247 
00248 void ContextStyle::setRequestedFonts( const QStringList& list )
00249 {
00250     m_requestedFonts = list;
00251     //table.init( this );
00252 }
00253 #endif
00254 
00255 double ContextStyle::getReductionFactor( TextStyle tstyle ) const
00256 {
00257     return textStyleValues[ tstyle ].reductionFactor;
00258 }
00259 
00260 luPt ContextStyle::getAdjustedSize( TextStyle tstyle ) const
00261 {
00262     return qRound( ptToLayoutUnitPt( m_sizeFactor*m_baseSize*getReductionFactor( tstyle ) ) );
00263 }
00264 
00265 luPixel ContextStyle::getSpace( TextStyle tstyle, SpaceWidth space ) const
00266 {
00267     switch ( space ) {
00268     case NEGTHIN: return -getThinSpace( tstyle );
00269     case THIN:    return getThinSpace( tstyle );
00270     case MEDIUM:  return getMediumSpace( tstyle );
00271     case THICK:   return getThickSpace( tstyle );
00272     case QUAD:    return getQuadSpace( tstyle );
00273     }
00274     return 0;
00275 }
00276 
00277 luPixel ContextStyle::getThinSpace( TextStyle tstyle ) const
00278 {
00279     return ptToPixelX( m_sizeFactor*textStyleValues[ tstyle ].thinSpace( quad ) );
00280 }
00281 
00282 luPixel ContextStyle::getMediumSpace( TextStyle tstyle ) const
00283 {
00284     return ptToPixelX( m_sizeFactor*textStyleValues[ tstyle ].mediumSpace( quad ) );
00285 }
00286 
00287 luPixel ContextStyle::getThickSpace( TextStyle tstyle ) const
00288 {
00289     return ptToPixelX( m_sizeFactor*textStyleValues[ tstyle ].thickSpace( quad ) );
00290 }
00291 
00292 luPixel ContextStyle::getQuadSpace( TextStyle tstyle ) const
00293 {
00294     return ptToPixelX( m_sizeFactor*textStyleValues[ tstyle ].quadSpace( quad ) );
00295 }
00296 
00297 luPixel ContextStyle::axisHeight( TextStyle tstyle ) const
00298 {
00299     //return ptToPixelY( textStyleValues[ tstyle ].axisHeight( m_axisHeight ) );
00300     return static_cast<luPixel>( m_sizeFactor*textStyleValues[ tstyle ].axisHeight( m_axisHeight ) );
00301 }
00302 
00303 luPt ContextStyle::getBaseSize() const
00304 {
00305     return static_cast<luPt>( ptToLayoutUnitPt( m_sizeFactor*m_baseSize ) );
00306 }
00307 
00308 void ContextStyle::setBaseSize( int size )
00309 {
00310     //kdDebug( 40000 ) << "ContextStyle::setBaseSize" << endl;
00311     if ( size != m_baseSize ) {
00312         m_baseSize = size;
00313         setup();
00314     }
00315 }
00316 
00317 void ContextStyle::setSizeFactor( double factor )
00318 {
00319     m_sizeFactor = factor;
00320 }
00321 
00322 
00323 luPixel ContextStyle::getLineWidth() const
00324 {
00325     return ptToLayoutUnitPixX( m_sizeFactor*lineWidth );
00326 }
00327 
00328 luPixel ContextStyle::getEmptyRectWidth() const
00329 {
00330     return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize/1.8 );
00331 }
00332 
00333 luPixel ContextStyle::getEmptyRectHeight() const
00334 {
00335     return ptToLayoutUnitPixX( m_sizeFactor*m_baseSize/1.8 );
00336 }
00337 
00338 
00339 ContextStyle::TextStyle ContextStyle::convertTextStyleFraction( TextStyle tstyle ) const
00340 {
00341     TextStyle result;
00342 
00343     switch ( tstyle ){
00344     case displayStyle:
00345     result = textStyle;
00346     break;
00347     case textStyle:
00348     result = scriptStyle;
00349     break;
00350     default:
00351     result = scriptScriptStyle;
00352     break;
00353     }
00354 
00355     return result;
00356 }
00357 
00358 
00359 ContextStyle::TextStyle ContextStyle::convertTextStyleIndex( TextStyle tstyle ) const
00360 {
00361     TextStyle result;
00362 
00363     switch ( tstyle ){
00364     case displayStyle:
00365     result = scriptStyle;
00366     break;
00367     case textStyle:
00368     result = scriptStyle;
00369     break;
00370     default:
00371     result = scriptScriptStyle;
00372     break;
00373     }
00374 
00375     return result;
00376 }
00377 
00378 
00379 void ContextStyle::setup()
00380 {
00381     luPt size = static_cast<luPt>( m_baseSize );
00382     QFont font = symbolFont;
00383     font.setPointSize( size );
00384     QFontMetrics fm( font );
00385 
00386     // Or better the real space required? ( boundingRect )
00387     quad = ptToLayoutUnitPt( fm.width( 'M' ) );
00388 
00389     font = QFont(defaultFont);
00390     font.setPointSize( size );
00391     QFontMetrics fm2( font );
00392     //m_axisHeight = ptToLayoutUnitPt( fm2.strikeOutPos() );
00393     //ptToLayoutUnitPixY
00394     //m_axisHeight = ptToLayoutUnitPt( pixelYToPt( fm2.strikeOutPos() ) );
00395     m_axisHeight = ptToLayoutUnitPixY( pixelYToPt( fm2.strikeOutPos() ) );
00396 }
00397 
00398 KFORMULA_NAMESPACE_END
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Mar 20 14:25:23 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003