kformula/flake

GlyphElement.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2006-2009 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 "GlyphElement.h"
00021 #include "AttributeManager.h"
00022 
00023 #include <KoXmlWriter.h>
00024 #include <KoXmlReader.h>
00025 
00026 #include <QFontDatabase>
00027 #include <QFontMetricsF>
00028 
00029 #include <kdebug.h>
00030 
00031 GlyphElement::GlyphElement( BasicElement* parent ) : TokenElement( parent )
00032 {}
00033 
00034 QRectF GlyphElement::renderToPath( const QString& raw, QPainterPath& path ) const
00035 {
00036     Q_UNUSED( raw )
00037     Q_UNUSED( path )
00038     // try to lookup the char in the font database
00039     AttributeManager am;
00040     QString fontFamily = am.stringOf( "fontfamily", this );
00041     QFontDatabase db;
00042     QFont tmpFont;
00043 
00044     // determine if the specified font and glyph can be found
00045     if( db.families().contains( fontFamily ) )
00046     {
00047         tmpFont.setFamily( fontFamily );
00048         path.addText( path.currentPosition(), tmpFont,
00049                       QChar( am.stringOf( "index", this ).toInt() ) ); 
00050         QFontMetricsF fm(tmpFont);
00051         return fm.boundingRect(QChar( am.stringOf( "index", this ).toInt() ) );
00052     }
00053     else { // if not found use alt text
00054         path.addText( path.currentPosition(), font(), am.stringOf( "alt", this ) );
00055         QFontMetricsF fm(font());
00056         return fm.boundingRect(am.stringOf( "alt", this ));
00057     }
00058 }
00059 
00060 ElementType GlyphElement::elementType() const
00061 {
00062     return Glyph;
00063 }
00064 
00065 bool GlyphElement::readMathMLAttributes( const KoXmlElement& element )
00066 {
00067     // MathML Section 3.2.9.2
00068     m_fontFamily = element.attribute( "fontfamily" );
00069     if ( m_fontFamily.isNull() ) {
00070         kWarning( DEBUGID ) << "Required attribute fontfamily not found in glyph element";
00071         return false;
00072     }
00073     QString indexStr = element.attribute( "index" );
00074     if ( indexStr.isNull() ) {
00075         kWarning( DEBUGID ) << "Required attribute index not found in glyph element";
00076         return false;
00077     }
00078     bool ok;
00079     ushort index = indexStr.toUShort( &ok );
00080     if ( ! ok ) {
00081         kWarning( DEBUGID ) << "Invalid index value in glyph element";
00082         return false;
00083     }
00084     m_char = QChar( index );
00085 
00086     m_alt = element.attribute( "alt" );
00087     if ( m_alt.isNull() ) {
00088         kWarning( DEBUGID ) << "Required attribute alt not found in glyph element";
00089         return false;
00090     }
00091 
00092     // TODO: Check whether we have needed fontfamily
00093     return true;
00094 }
00095 
00096 void GlyphElement::writeMathMLAttributes( KoXmlWriter* writer ) const
00097 {
00098     writer->addAttribute( "fontfamily", m_fontFamily );
00099     writer->addAttribute( "index", m_char.unicode() );
00100     writer->addAttribute( "alt", m_alt );
00101 }
00102 
00103 void GlyphElement::writeMathMLContent( KoXmlWriter* writer ) const
00104 {
00105     Q_UNUSED( writer )
00106 }