F:/KPlato/koffice/libs/kformula/GlyphElement.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <QFontMetrics>
00021 #include <QPainter>
00022 
00023 #include "fontstyle.h"
00024 #include "GlyphElement.h"
00025 
00026 namespace KFormula {
00027 
00028 GlyphElement::GlyphElement( BasicElement* parent ) : TextElement( ' ', false, parent ) {
00029 }
00030 
00031 bool GlyphElement::readAttributesFromMathMLDom( const QDomElement& element )
00032 {
00033     if ( !BasicElement::readAttributesFromMathMLDom( element ) ) {
00034         return false;
00035     }
00036 
00037     // MathML Section 3.2.9.2
00038     m_fontFamily = element.attribute( "fontfamily" );
00039     if ( m_fontFamily.isNull() ) {
00040         kWarning( DEBUGID ) << "Required attribute fontfamily not found in glyph element\n";
00041         return false;
00042     }
00043     QString indexStr = element.attribute( "index" );
00044     if ( indexStr.isNull() ) {
00045         kWarning( DEBUGID ) << "Required attribute index not found in glyph element\n";
00046         return false;
00047     }
00048     bool ok;
00049     ushort index = indexStr.toUShort( &ok );
00050     if ( ! ok ) {
00051         kWarning( DEBUGID ) << "Invalid index value in glyph element\n";
00052         return false;
00053     }
00054     m_char = QChar( index );
00055 
00056     m_alt = element.attribute( "alt" );
00057     if ( m_alt.isNull() ) {
00058         kWarning( DEBUGID ) << "Required attribute alt not found in glyph element\n";
00059         return false;
00060     }
00061 
00062     QStringList missing;
00063     FontStyle::testFont( missing, m_fontFamily.lower() );
00064     m_hasFont = missing.isEmpty();
00065 
00066     return true;
00067 }
00068 
00069 
00074 void GlyphElement::calcSizes( const ContextStyle& context, 
00075                               ContextStyle::TextStyle tstyle, 
00076                               ContextStyle::IndexStyle /*istyle*/,
00077                               StyleAttributes& style )
00078 {
00079     double factor = style.sizeFactor();
00080     luPt mySize = context.getAdjustedSize( tstyle, factor );
00081     QRect bound;
00082 
00083     if ( m_hasFont ) {
00084         QFont font( m_fontFamily );
00085         font.setPointSizeFloat( context.layoutUnitPtToPt( mySize ) );
00086         QFontMetrics fm ( font );
00087         bound = fm.boundingRect( m_char );
00088         setWidth( context.ptToLayoutUnitPt( fm.width( m_char ) ) );
00089     }
00090     else {
00091         QFont font( context.getDefaultFont() );
00092         font.setPointSizeFloat( context.layoutUnitPtToPt( mySize ) );
00093         QFontMetrics fm ( font );
00094         bound = fm.boundingRect( m_alt );
00095         setWidth( context.ptToLayoutUnitPt( fm.width( m_alt ) ) );
00096     }
00097     setHeight( context.ptToLayoutUnitPt( bound.height() ) );
00098     setBaseline( context.ptToLayoutUnitPt( -bound.top() ) );
00099     
00100     // There are some glyphs in TeX that have
00101     // baseline==0. (\int, \sum, \prod)
00102     if ( getBaseline() == 0 ) {
00103         setBaseline( -1 );
00104     }
00105 }
00106 
00112 void GlyphElement::draw( QPainter& painter, const LuPixelRect& /*r*/,
00113                         const ContextStyle& context,
00114                         ContextStyle::TextStyle tstyle,
00115                         ContextStyle::IndexStyle /*istyle*/,
00116                         StyleAttributes& style,
00117                         const LuPixelPoint& parentOrigin )
00118 {
00119     LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
00120     //if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
00121     //    return;
00122 
00123     double factor = style.sizeFactor();
00124     luPt mySize = context.getAdjustedSize( tstyle, factor );
00125     QFont font;
00126     QString text;
00127     
00128     if ( m_hasFont ) {
00129         painter.setPen( style.color() );
00130         setCharStyle( style.charStyle() );
00131         setCharFamily( style.charFamily() );
00132         font = QFont( m_fontFamily );
00133         text = m_char;
00134         painter.fillRect( context.layoutUnitToPixelX( myPos.x() ),
00135                           context.layoutUnitToPixelY( myPos.y() ),
00136                           context.layoutUnitToPixelX( getWidth() ),
00137                           context.layoutUnitToPixelY( getHeight() ),
00138                           style.background() );
00139     }
00140     else {
00141         painter.setPen( context.getErrorColor() );
00142         font = context.getDefaultFont();
00143         text = m_alt;
00144     }
00145     font.setPointSizeFloat( context.layoutUnitToFontSize( mySize, false ) );
00146     painter.setFont( font );
00147     painter.drawText( context.layoutUnitToPixelX( myPos.x() ),
00148                       context.layoutUnitToPixelY( myPos.y()+getBaseline() ),
00149                       text );
00150 }
00151     
00152 void GlyphElement::writeMathMLAttributes( QDomElement& element ) const
00153 {
00154     element.setAttribute( "fontfamily", m_fontFamily );
00155     element.setAttribute( "index", m_char.unicode() );
00156     element.setAttribute( "alt", m_alt );
00157 }
00158 
00159 } // namespace KFormula

Généré le Wed Nov 22 23:41:01 2006 pour KPlato par  doxygen 1.5.1-p1