lib

tokenelement.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 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 <algorithm>
00021 
00022 #include <qpainter.h>
00023 
00024 #include <klocale.h>
00025 
00026 #include "entities.h"
00027 #include "elementtype.h"
00028 #include "sequenceelement.h"
00029 #include "textelement.h"
00030 #include "glyphelement.h"
00031 #include "tokenelement.h"
00032 #include "identifierelement.h"
00033 #include "kformulacommand.h"
00034 #include "kformulacontainer.h"
00035 #include "kformuladocument.h"
00036 #include "formulaelement.h"
00037 #include "creationstrategy.h"
00038 
00039 KFORMULA_NAMESPACE_BEGIN
00040 
00041 TokenElement::TokenElement( BasicElement* parent ) : TokenStyleElement( parent ),
00042                                                      m_textOnly( true )
00043 {
00044 }
00045 
00046 int TokenElement::buildChildrenFromMathMLDom(QPtrList<BasicElement>& list, QDomNode n) 
00047 {
00048     while ( ! n.isNull() ) {
00049         if ( n.isText() ) {
00050             QString textelements = n.toText().data();
00051             textelements = textelements.stripWhiteSpace();
00052                 
00053             for (uint i = 0; i < textelements.length(); i++) {
00054                 TextElement* child = new TextElement(textelements[i]);
00055                 child->setParent(this);
00056                 child->setCharFamily( charFamily() );
00057                 child->setCharStyle( charStyle() );
00058                 list.append(child);
00059             }
00060         }
00061         else if ( n.isEntityReference() ) {
00062             QString entity = n.toEntityReference().nodeName();
00063             const entityMap* begin = entities;
00064             const entityMap* end = entities + entityMap::size();
00065             const entityMap* pos = std::lower_bound( begin, end, entity.ascii() );
00066             if ( pos == end || QString( pos->name ) != entity ) {
00067                 kdWarning() << "Invalid entity refererence: " << entity << endl;
00068             }
00069             else {
00070                 TextElement* child = new TextElement( QChar( pos->unicode ) );
00071                 child->setParent(this);
00072                 child->setCharFamily( charFamily() );
00073                 child->setCharStyle( charStyle() );
00074                 list.append(child);
00075             }
00076         }
00077         else if ( n.isElement() ) {
00078             m_textOnly = false;
00079             // Only mglyph element is allowed
00080             QDomElement e = n.toElement();
00081             if ( e.tagName().lower() != "mglyph" ) {
00082                 kdWarning( DEBUGID ) << "Invalid element inside Token Element\n";
00083                 return -1;
00084             }
00085             GlyphElement* child = new GlyphElement();
00086             child->setParent(this);
00087             child->setCharFamily( charFamily() );
00088             child->setCharStyle( charStyle() );
00089             if ( child->buildFromMathMLDom( e ) == -1 ) {
00090                 return -1;
00091             }
00092             list.append( child );
00093         }
00094         else {
00095             kdWarning() << "Invalid content in TokenElement\n";
00096         }
00097         n = n.nextSibling();
00098     }
00099 //  parse();
00100     kdWarning() << "Num of children " << list.count() << endl;
00101     return 1;
00102 }
00103 
00104 luPt TokenElement::getSpaceBefore( const ContextStyle& context, 
00105                                    ContextStyle::TextStyle tstyle,
00106                                    double factor )
00107 {
00108     if ( !context.isScript( tstyle ) ) {
00109         return context.getMediumSpace( tstyle, factor );
00110     }
00111     return 0;
00112 }
00113 
00114 luPt TokenElement::getSpaceAfter( const ContextStyle& context, 
00115                                   ContextStyle::TextStyle tstyle,
00116                                   double factor )
00117 {
00118     if ( !context.isScript( tstyle ) ) {
00119         return context.getThinSpace( tstyle, factor );
00120     }
00121     return 0;
00122 }
00123 
00124 KFORMULA_NAMESPACE_END
KDE Home | KDE Accessibility Home | Description of Access Keys