summaryrefslogtreecommitdiffstats
path: root/lib/kformula/glyphelement.cpp
blob: 709609cf181c834c8250876ce3ae7e1cf70a66d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* This file is part of the KDE project
   Copyright (C) 2006 Alfredo Beaumont Sainz <[email protected]>

   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.
*/

#include <tqfontmetrics.h>
#include <tqpainter.h>

#include "fontstyle.h"
#include "glyphelement.h"

KFORMULA_NAMESPACE_BEGIN

GlyphElement::GlyphElement( BasicElement* parent ) : TextElement( ' ', false, parent ) {
}

bool GlyphElement::readAttributesFromMathMLDom( const TQDomElement& element )
{
    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;
    }
    TQString indexStr = element.attribute( "index" );
    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;
    }
    m_char = TQChar( index );

    m_alt = element.attribute( "alt" );
    if ( m_alt.isNull() ) {
        kdWarning( DEBUGID ) << "Required attribute alt not found in glyph element\n";
        return false;
    }

    TQStringList missing;
    FontStyle::testFont( missing, m_fontFamily.lower() );
    m_hasFont = missing.isEmpty();

    return true;
}


/**
 * Calculates our width and height and
 * our children's parentPosition.
 */
void GlyphElement::calcSizes( const ContextStyle& context, 
                              ContextStyle::TextStyle tstyle, 
                              ContextStyle::IndexStyle /*istyle*/,
                              StyleAttributes& style )
{
    double factor = style.sizeFactor();
    luPt mySize = context.getAdjustedSize( tstyle, factor );
    TQRect bound;

    if ( m_hasFont ) {
        TQFont font( m_fontFamily );
        font.setPointSizeFloat( context.layoutUnitPtToPt( mySize ) );
        TQFontMetrics fm ( font );
        bound = fm.boundingRect( m_char );
        setWidth( context.ptToLayoutUnitPt( fm.width( m_char ) ) );
    }
    else {
        TQFont font( context.getDefaultFont() );
        font.setPointSizeFloat( context.layoutUnitPtToPt( mySize ) );
        TQFontMetrics fm ( font );
        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 );
    }
}

/**
 * Draws the whole element including its children.
 * The `parentOrigin' is the point this element's parent starts.
 * We can use our parentPosition to get our own origin then.
 */
void GlyphElement::draw( TQPainter& painter, const LuPixelRect& /*r*/,
                        const ContextStyle& context,
                        ContextStyle::TextStyle tstyle,
                        ContextStyle::IndexStyle /*istyle*/,
                        StyleAttributes& style,
                        const LuPixelPoint& parentOrigin )
{
    LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
    //if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
    //    return;

    double factor = style.sizeFactor();
    luPt mySize = context.getAdjustedSize( tstyle, factor );
    TQFont font;
    TQString text;
    
    if ( m_hasFont ) {
        painter.setPen( style.color() );
        setCharStyle( style.charStyle() );
        setCharFamily( style.charFamily() );
        font = TQFont( m_fontFamily );
        text = m_char;
        painter.fillRect( context.layoutUnitToPixelX( myPos.x() ),
                          context.layoutUnitToPixelY( myPos.y() ),
                          context.layoutUnitToPixelX( getWidth() ),
                          context.layoutUnitToPixelY( getHeight() ),
                          style.background() );
    }
    else {
        painter.setPen( context.getErrorColor() );
        font = context.getDefaultFont();
        text = m_alt;
    }
    font.setPointSizeFloat( context.layoutUnitToFontSize( mySize, false ) );
    painter.setFont( font );
    painter.drawText( context.layoutUnitToPixelX( myPos.x() ),
                      context.layoutUnitToPixelY( myPos.y()+getBaseline() ),
                      text );
}
    
void GlyphElement::writeMathMLAttributes( TQDomElement& element ) const
{
    element.setAttribute( "fontfamily", m_fontFamily );
    element.setAttribute( "index", m_char.unicode() );
    element.setAttribute( "alt", m_alt );
}

KFORMULA_NAMESPACE_END