You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
5.5 KiB
160 lines
5.5 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
14 years ago
|
#include <tqfontmetrics.h>
|
||
|
#include <tqpainter.h>
|
||
15 years ago
|
|
||
|
#include "fontstyle.h"
|
||
|
#include "glyphelement.h"
|
||
|
|
||
|
KFORMULA_NAMESPACE_BEGIN
|
||
|
|
||
14 years ago
|
GlyphElement::GlyphElement( BasicElement* parent ) : TextElement( ' ', false, parent ) {
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
bool GlyphElement::readAttributesFromMathMLDom( const TQDomElement& element )
|
||
15 years ago
|
{
|
||
|
if ( !BasicElement::readAttributesFromMathMLDom( element ) ) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// MathML Section 3.2.9.2
|
||
|
m_fontFamily = element.attribute( "fontfamily" );
|
||
|
if ( m_fontFamily.isNull() ) {
|
||
|
kdWarning( DEBUGID ) << "Required attribute fontfamily not found in glyph element\n";
|
||
|
return false;
|
||
|
}
|
||
14 years ago
|
TQString indexStr = element.attribute( "index" );
|
||
15 years ago
|
if ( indexStr.isNull() ) {
|
||
|
kdWarning( DEBUGID ) << "Required attribute index not found in glyph element\n";
|
||
|
return false;
|
||
|
}
|
||
|
bool ok;
|
||
|
ushort index = indexStr.toUShort( &ok );
|
||
|
if ( ! ok ) {
|
||
|
kdWarning( DEBUGID ) << "Invalid index value in glyph element\n";
|
||
|
return false;
|
||
|
}
|
||
14 years ago
|
m_char = TQChar( index );
|
||
15 years ago
|
|
||
|
m_alt = element.attribute( "alt" );
|
||
|
if ( m_alt.isNull() ) {
|
||
|
kdWarning( DEBUGID ) << "Required attribute alt not found in glyph element\n";
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQStringList missing;
|
||
15 years ago
|
FontStyle::testFont( missing, m_fontFamily.lower() );
|
||
|
m_hasFont = missing.isEmpty();
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Calculates our width and height and
|
||
13 years ago
|
* our children's parentPosition.
|
||
15 years ago
|
*/
|
||
|
void GlyphElement::calcSizes( const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle /*istyle*/,
|
||
|
StyleAttributes& style )
|
||
|
{
|
||
|
double factor = style.sizeFactor();
|
||
|
luPt mySize = context.getAdjustedSize( tstyle, factor );
|
||
14 years ago
|
TQRect bound;
|
||
15 years ago
|
|
||
|
if ( m_hasFont ) {
|
||
14 years ago
|
TQFont font( m_fontFamily );
|
||
14 years ago
|
font.setPointSizeFloat( context.layoutUnitPtToPt( mySize ) );
|
||
14 years ago
|
TQFontMetrics fm ( font );
|
||
15 years ago
|
bound = fm.boundingRect( m_char );
|
||
|
setWidth( context.ptToLayoutUnitPt( fm.width( m_char ) ) );
|
||
|
}
|
||
|
else {
|
||
14 years ago
|
TQFont font( context.getDefaultFont() );
|
||
14 years ago
|
font.setPointSizeFloat( context.layoutUnitPtToPt( mySize ) );
|
||
14 years ago
|
TQFontMetrics fm ( font );
|
||
15 years ago
|
bound = fm.boundingRect( m_alt );
|
||
|
setWidth( context.ptToLayoutUnitPt( fm.width( m_alt ) ) );
|
||
|
}
|
||
|
setHeight( context.ptToLayoutUnitPt( bound.height() ) );
|
||
|
setBaseline( context.ptToLayoutUnitPt( -bound.top() ) );
|
||
|
|
||
|
// There are some glyphs in TeX that have
|
||
|
// baseline==0. (\int, \sum, \prod)
|
||
|
if ( getBaseline() == 0 ) {
|
||
|
setBaseline( -1 );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Draws the whole element including its children.
|
||
14 years ago
|
* The `parentOrigin' is the point this element's parent starts.
|
||
14 years ago
|
* We can use our parentPosition to get our own origin then.
|
||
15 years ago
|
*/
|
||
14 years ago
|
void GlyphElement::draw( TQPainter& painter, const LuPixelRect& /*r*/,
|
||
15 years ago
|
const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle /*istyle*/,
|
||
|
StyleAttributes& style,
|
||
14 years ago
|
const LuPixelPoint& parentOrigin )
|
||
15 years ago
|
{
|
||
14 years ago
|
LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
|
||
15 years ago
|
//if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
|
||
|
// return;
|
||
|
|
||
|
double factor = style.sizeFactor();
|
||
|
luPt mySize = context.getAdjustedSize( tstyle, factor );
|
||
14 years ago
|
TQFont font;
|
||
|
TQString text;
|
||
15 years ago
|
|
||
|
if ( m_hasFont ) {
|
||
|
painter.setPen( style.color() );
|
||
|
setCharStyle( style.charStyle() );
|
||
|
setCharFamily( style.charFamily() );
|
||
14 years ago
|
font = TQFont( m_fontFamily );
|
||
15 years ago
|
text = m_char;
|
||
14 years ago
|
painter.fillRect( context.layoutUnitToPixelX( myPos.x() ),
|
||
|
context.layoutUnitToPixelY( myPos.y() ),
|
||
|
context.layoutUnitToPixelX( getWidth() ),
|
||
|
context.layoutUnitToPixelY( getHeight() ),
|
||
15 years ago
|
style.background() );
|
||
|
}
|
||
|
else {
|
||
|
painter.setPen( context.getErrorColor() );
|
||
|
font = context.getDefaultFont();
|
||
|
text = m_alt;
|
||
|
}
|
||
14 years ago
|
font.setPointSizeFloat( context.layoutUnitToFontSize( mySize, false ) );
|
||
15 years ago
|
painter.setFont( font );
|
||
14 years ago
|
painter.drawText( context.layoutUnitToPixelX( myPos.x() ),
|
||
|
context.layoutUnitToPixelY( myPos.y()+getBaseline() ),
|
||
15 years ago
|
text );
|
||
|
}
|
||
|
|
||
14 years ago
|
void GlyphElement::writeMathMLAttributes( TQDomElement& element ) const
|
||
15 years ago
|
{
|
||
|
element.setAttribute( "fontfamily", m_fontFamily );
|
||
13 years ago
|
element.setAttribute( "index", m_char.unicode() );
|
||
15 years ago
|
element.setAttribute( "alt", m_alt );
|
||
|
}
|
||
|
|
||
|
KFORMULA_NAMESPACE_END
|