glyphelement.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqfontmetrics.h>
21#include <tqpainter.h>
22
23#include "fontstyle.h"
24#include "glyphelement.h"
25
26KFORMULA_NAMESPACE_BEGIN
27
28GlyphElement::GlyphElement( BasicElement* parent ) : TextElement( ' ', false, parent ) {
29}
30
31bool GlyphElement::readAttributesFromMathMLDom( const TQDomElement& element )
32{
34 return false;
35 }
36
37 // MathML Section 3.2.9.2
38 m_fontFamily = element.attribute( "fontfamily" );
39 if ( m_fontFamily.isNull() ) {
40 kdWarning( DEBUGID ) << "Required attribute fontfamily not found in glyph element\n";
41 return false;
42 }
43 TQString indexStr = element.attribute( "index" );
44 if ( indexStr.isNull() ) {
45 kdWarning( DEBUGID ) << "Required attribute index not found in glyph element\n";
46 return false;
47 }
48 bool ok;
49 ushort index = indexStr.toUShort( &ok );
50 if ( ! ok ) {
51 kdWarning( DEBUGID ) << "Invalid index value in glyph element\n";
52 return false;
53 }
54 m_char = TQChar( index );
55
56 m_alt = element.attribute( "alt" );
57 if ( m_alt.isNull() ) {
58 kdWarning( DEBUGID ) << "Required attribute alt not found in glyph element\n";
59 return false;
60 }
61
62 TQStringList missing;
63 FontStyle::testFont( missing, m_fontFamily.lower() );
64 m_hasFont = missing.isEmpty();
65
66 return true;
67}
68
69
74void GlyphElement::calcSizes( const ContextStyle& context,
76 ContextStyle::IndexStyle /*istyle*/,
77 StyleAttributes& style )
78{
79 double factor = style.sizeFactor();
80 luPt mySize = context.getAdjustedSize( tstyle, factor );
81 TQRect bound;
82
83 if ( m_hasFont ) {
84 TQFont font( m_fontFamily );
85 font.setPointSizeFloat( context.layoutUnitPtToPt( mySize ) );
86 TQFontMetrics fm ( font );
87 bound = fm.boundingRect( m_char );
88 setWidth( context.ptToLayoutUnitPt( fm.width( m_char ) ) );
89 }
90 else {
91 TQFont font( context.getDefaultFont() );
92 font.setPointSizeFloat( context.layoutUnitPtToPt( mySize ) );
93 TQFontMetrics fm ( font );
94 bound = fm.boundingRect( m_alt );
95 setWidth( context.ptToLayoutUnitPt( fm.width( m_alt ) ) );
96 }
97 setHeight( context.ptToLayoutUnitPt( bound.height() ) );
98 setBaseline( context.ptToLayoutUnitPt( -bound.top() ) );
99
100 // There are some glyphs in TeX that have
101 // baseline==0. (\int, \sum, \prod)
102 if ( getBaseline() == 0 ) {
103 setBaseline( -1 );
104 }
105}
106
112void GlyphElement::draw( TQPainter& painter, const LuPixelRect& /*r*/,
113 const ContextStyle& context,
115 ContextStyle::IndexStyle /*istyle*/,
116 StyleAttributes& style,
117 const LuPixelPoint& parentOrigin )
118{
119 LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
120 //if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
121 // return;
122
123 double factor = style.sizeFactor();
124 luPt mySize = context.getAdjustedSize( tstyle, factor );
125 TQFont font;
126 TQString text;
127
128 if ( m_hasFont ) {
129 painter.setPen( style.color() );
130 setCharStyle( style.charStyle() );
131 setCharFamily( style.charFamily() );
132 font = TQFont( m_fontFamily );
133 text = m_char;
134 painter.fillRect( context.layoutUnitToPixelX( myPos.x() ),
135 context.layoutUnitToPixelY( myPos.y() ),
136 context.layoutUnitToPixelX( getWidth() ),
137 context.layoutUnitToPixelY( getHeight() ),
138 style.background() );
139 }
140 else {
141 painter.setPen( context.getErrorColor() );
142 font = context.getDefaultFont();
143 text = m_alt;
144 }
145 font.setPointSizeFloat( context.layoutUnitToFontSize( mySize, false ) );
146 painter.setFont( font );
147 painter.drawText( context.layoutUnitToPixelX( myPos.x() ),
148 context.layoutUnitToPixelY( myPos.y()+getBaseline() ),
149 text );
150}
151
152void GlyphElement::writeMathMLAttributes( TQDomElement& element ) const
153{
154 element.setAttribute( "fontfamily", m_fontFamily );
155 element.setAttribute( "index", m_char.unicode() );
156 element.setAttribute( "alt", m_alt );
157}
158
159KFORMULA_NAMESPACE_END
Basis of every formula element.
virtual bool readAttributesFromMathMLDom(const TQDomElement &element)
Reads our attributes from the MathML element.
Contains all the style information for the formela.
luPt getAdjustedSize(TextStyle tstyle, double factor) const
Calculates the font size corresponding to the given TextStyle.
TextStyle
Textstyles like in TeX.
double layoutUnitToFontSize(int luSize, bool) const
Given the font size for the font in layout units, in pt (use pointSize()) this returns the font size ...
static double ptToLayoutUnitPt(double pt)
Not zoom dependent.
int layoutUnitToPixelX(int lupix) const
The "layout unit pixel" -> "[zoomed] view pixel" conversions.
An element that represents one char.
Definition textelement.h:37