00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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 ,
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
00101
00102 if ( getBaseline() == 0 ) {
00103 setBaseline( -1 );
00104 }
00105 }
00106
00112 void GlyphElement::draw( QPainter& painter, const LuPixelRect& ,
00113 const ContextStyle& context,
00114 ContextStyle::TextStyle tstyle,
00115 ContextStyle::IndexStyle ,
00116 StyleAttributes& style,
00117 const LuPixelPoint& parentOrigin )
00118 {
00119 LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
00120
00121
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 }