00001
00002
00003 #include <QApplication>
00004 #include <kdebug.h>
00005 #include <kglobal.h>
00006 #include <klocale.h>
00007
00008 #include "KoTextFormatter.h"
00009 #include "KoTextFormat.h"
00010 #include "KoTextDocument.h"
00011 #include "KoTextParag.h"
00012 #include "KoTextZoomHandler.h"
00013 #include "KoParagCounter.h"
00014
00015 #include <assert.h>
00016
00017 class KoTextFormatterTest
00018 {
00019 public:
00020 KoTextFormatterTest();
00021 ~KoTextFormatterTest() {
00022 delete zh;
00023 delete doc;
00024 }
00025
00026 void speedTest();
00027 void counterAndBigChar();
00028 void oneLineTest();
00029 void noHeightTest();
00030 void noWidthEverTest();
00031 void tooWideChar();
00032
00033 private:
00034 KoTextZoomHandler* zh;
00035 KoTextDocument* doc;
00036 };
00037
00038
00039 class TextFlow : public KoTextFlow
00040 {
00041 public:
00042 TextFlow( int availableWidth, int availableHeight )
00043 : m_availableWidth( availableWidth ), m_availableHeight( availableHeight ),
00044 m_leftMargin( 0 )
00045 {
00046 setWidth( m_availableWidth );
00047 }
00048 virtual int availableHeight() const {
00049 return m_availableHeight;
00050 }
00051 void setLeftMargin( int lm ) {
00052 m_leftMargin = lm;
00053 }
00054 virtual void adjustMargins( int yp, int h, int reqMinWidth, int& leftMargin, int& rightMargin, int& pageWidth, KoTextParag* ) {
00055 Q_UNUSED(yp);
00056 Q_UNUSED(h);
00057 Q_UNUSED(reqMinWidth);
00058 Q_UNUSED(rightMargin);
00059 if ( m_leftMargin ) {
00060 leftMargin = m_leftMargin;
00061 pageWidth = m_availableWidth - m_leftMargin;
00062 kDebug() << "adjustMargins: returning leftMargin=" << leftMargin << " pageWidth=" << pageWidth << endl;
00063 } else
00064 pageWidth = m_availableWidth;
00065 }
00066 private:
00067 int m_availableWidth;
00068 int m_availableHeight;
00069 int m_leftMargin;
00070 };
00071
00072 KoTextFormatterTest::KoTextFormatterTest()
00073 {
00074 zh = new KoTextZoomHandler;
00075 QFont defaultFont( "helvetica", 12 );
00076 KoTextFormatCollection* fc = new KoTextFormatCollection( defaultFont, Qt::black, "en_US", false );
00077 KoTextFormatter* formatter = new KoTextFormatter;
00078
00079 doc = new KoTextDocument( zh, fc, formatter );
00080 }
00081
00082 void KoTextFormatterTest::speedTest()
00083 {
00084 kDebug() << k_funcinfo << endl;
00085 doc->clear(true);
00086 KoTextParag* parag = doc->firstParag();
00087 parag->append( "They burst into flames when it is time for them to die, and then they are reborn from the ashes" );
00088
00089
00090 for ( uint i = 0 ; i < 50 ; ++i )
00091 {
00092 parag->invalidate(0);
00093 parag->format();
00094 }
00095 doc->clear(false);
00096 }
00097
00098 void KoTextFormatterTest::noHeightTest()
00099 {
00100 kDebug() << k_funcinfo << endl;
00101
00102
00103
00104
00105 doc->setFlow( new TextFlow( 250, 0 ) );
00106 doc->clear(true);
00107 KoTextParag* parag = doc->firstParag();
00108 parag->append( "abcdefghi" );
00109 parag->format();
00110 assert( parag->lines() == 2 );
00111 doc->clear(false);
00112 doc->setFlow( new KoTextFlow );
00113 }
00114
00115 void KoTextFormatterTest::noWidthEverTest()
00116 {
00117 kDebug() << k_funcinfo << endl;
00118
00119
00120
00121
00122
00123
00124
00125 TextFlow* flow = new TextFlow( 1000, 2000 );
00126 flow->setLeftMargin( 1000 );
00127 doc->setFlow( flow );
00128
00129 doc->clear(true);
00130 KoTextParag* parag = doc->firstParag();
00131 parag->append( "abcdefghi" );
00132 parag->format();
00133
00134 assert( !parag->isValid() );
00135 doc->clear(false);
00136 doc->setFlow( new KoTextFlow );
00137 }
00138
00139 void KoTextFormatterTest::tooWideChar()
00140 {
00141 kDebug() << k_funcinfo << endl;
00142
00143
00144
00145
00146 doc->setFlow( new TextFlow( 10, 2000 ) );
00147 doc->clear(true);
00148 KoTextParag* parag = doc->firstParag();
00149 parag->append( "a" );
00150 parag->format();
00151 assert( parag->isValid() );
00152 assert( parag->lines() == 1 );
00153
00154 doc->clear(false);
00155 doc->setFlow( new KoTextFlow );
00156 }
00157
00158 void KoTextFormatterTest::oneLineTest()
00159 {
00160 kDebug() << k_funcinfo << endl;
00161
00162
00163 doc->setFlow( new TextFlow( 2000, 200 ) );
00164 doc->clear(true);
00165 KoTextParag* parag = doc->firstParag();
00166 parag->append( "abcdefghi" );
00167 parag->format();
00168 assert( parag->lines() == 1 );
00169 assert( parag->isValid() );
00170 assert( parag->rect().width() == 2000 );
00171 assert( parag->widthUsed() < 2000 );
00172 doc->clear(false);
00173 doc->setFlow( new KoTextFlow );
00174 }
00175
00176 void KoTextFormatterTest::counterAndBigChar()
00177 {
00178 kDebug() << k_funcinfo << endl;
00179
00180
00181
00182 doc->setFlow( new TextFlow( 2000, 200 ) );
00183 doc->clear(true);
00184 KoTextParag* parag = doc->firstParag();
00185 parag->append( "aB" );
00186 KoTextFormat f( *parag->at( 0 )->format() );
00187 f.setPointSize( 48 );
00188 parag->setFormat( 1, 1, doc->formatCollection()->format( &f ), true );
00189 KoParagCounter counter;
00190 counter.setNumbering( KoParagCounter::NUM_LIST );
00191 counter.setStyle( KoParagCounter::STYLE_NUM );
00192 parag->setCounter( &counter );
00193 parag->format();
00194 assert( parag->lines() == 1 );
00195 assert( parag->isValid() );
00196 assert( parag->rect().width() == 2000 );
00197 assert( parag->widthUsed() < 2000 );
00198 assert( parag->at(0)->x > 0 );
00199 doc->clear(false);
00200 doc->setFlow( new KoTextFlow );
00201 }
00202
00203 int main (int argc, char ** argv)
00204 {
00205 QApplication app(argc, argv);
00206
00207
00208 KGlobal::locale()->setLanguage( QString::fromLatin1( "en_US" ) );
00209 KGlobal::locale()->setCountry( QString::fromLatin1( "C" ) );
00210
00211 KoTextFormatterTest test;
00212
00213 test.oneLineTest();
00214 test.counterAndBigChar();
00215 test.noHeightTest();
00216 test.noWidthEverTest();
00217 test.tooWideChar();
00218
00219 return 0;
00220 }