filters

wp5.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Ariya Hidayat <ariya@kde.org>
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 <config.h>
00021 
00022 #ifdef HAVE_UNISTD_H
00023 #include <unistd.h>
00024 #endif
00025 
00026 #include <qtextcodec.h>
00027 #include <qfile.h>
00028 #include <qfileinfo.h>
00029 #include <qtextstream.h>
00030 
00031 #include <KWEFBaseWorker.h>
00032 #include <KWEFKWordLeader.h>
00033 #include <KWEFUtil.h>
00034 
00035 #include <wp5.h>
00036 
00037 bool WPFiveWorker::doOpenFile(const QString& filenameOut, const QString& /*to*/)
00038 {
00039   filename = filenameOut;
00040   outfile.setName( filename );
00041   if( !outfile.open( IO_WriteOnly ) )
00042     return false;
00043 
00044   output.setDevice( &outfile );
00045   output.setByteOrder (QDataStream::LittleEndian);
00046   return true;
00047 }
00048 
00049 bool WPFiveWorker::doCloseFile(void)
00050 {
00051   // asssume we're at the end of the file
00052   Q_UINT32 total_filesize = outfile.at();
00053 
00054   // close the file first
00055   outfile.close();
00056 
00057   // reopen for read and write
00058   if( !outfile.open( IO_ReadWrite ) )
00059     return false;
00060   output.setDevice( &outfile );
00061   output.setByteOrder (QDataStream::LittleEndian);
00062 
00063   // now it's time to fix-up some header fields
00064 
00065   // fix for offset 12, int32, filesize
00066   outfile.at( 20 );
00067   output << total_filesize;
00068 
00069   // offset 4, int32, pointer to document area
00070   outfile.at( 4 );
00071   output << document_area_ptr;
00072 
00073   outfile.close();
00074   return true;
00075 }
00076 
00077 bool WPFiveWorker::doOpenDocument(void)
00078 {
00079   // write WP document header
00080   // note that some fields are still "dummy"
00081 
00082   // magic id: -1, "WPC"
00083   Q_UINT8 magic[] = { 0xff, 0x57, 0x50, 0x43 };
00084   for( int i=0; i<4; i++ ) output << magic[i];
00085 
00086   // pointer to document area (dummy, will be fixed later)
00087   Q_UINT8 docptr[] = { 0x0E, 0x02, 0x00, 0x00 } ;
00088   for( int i=0; i<4; i++ ) output << docptr[i];
00089 
00090   // write product type ( 1 = WordPerfect )
00091   Q_UINT8 product_type = 1;
00092   output << product_type;
00093 
00094   // write file type ( 10 = WordPerfect document )
00095   Q_UINT8 file_type = 10;
00096   output << file_type;
00097 
00098   // write version ( 1 = WordPerfect 5.x )
00099   Q_UINT16 version = 0x0100;
00100   output << version;
00101 
00102   // write encryption flag ( 0 = not encrypted )
00103   Q_UINT16 encrypt = 0;
00104   output << encrypt;
00105 
00106   // offset to index header (always 0x200?)
00107   Q_UINT16 index_header_ptr = 0x200;
00108   output << index_header_ptr;
00109 
00110   // beginning of extended file header (always 5)
00111   Q_UINT32 extheader = 5;
00112   output << extheader;
00113 
00114   // filesize (dummy, will be fixed later)
00115   Q_UINT32 filesize = 0;
00116   output << filesize;
00117 
00118   // filler 488 bytes
00119   Q_UINT8 dummy = 0;
00120   for( int i=0; i<488; i++ ) output << dummy;
00121 
00122   // index header (specified 0 index!)
00123   Q_UINT8 index_header[] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00124     0, 0, 0, 0 };
00125   for( int i=0; i<14; i++ ) output << index_header[i];
00126 
00127   // document area starts, mark it for the header fields
00128   document_area_ptr = output.device()->at();
00129 
00130   return true;
00131 }
00132 
00133 bool WPFiveWorker::doCloseDocument(void)
00134 {
00135   return true;
00136 }
00137 
00138 // quick-and-dirty escape function for WP 5.x chars
00139 // TODO fix it !
00140 static QCString WPFiveEscape( const QString& text )
00141 {
00142   QCString result;
00143 
00144   for( unsigned int i=0; i < text.length(); i++ )
00145   {
00146     int c = text[i].unicode();
00147     if( c < 32 ) result += '.';
00148     else if ( c == 32 ) result += 0x20 ; // hard space
00149     else if ( c < 128 ) result += text[i].latin1();
00150     else result += '.';
00151   }
00152 
00153   return result;
00154 }
00155 
00156 
00157 bool WPFiveWorker::doFullParagraph(const QString& paraText, 
00158   const LayoutData& /*layout*/, const ValueListFormatData& paraFormatDataList)
00159 {
00160   ValueListFormatData::ConstIterator it;
00161   for( it = paraFormatDataList.begin(); it!=paraFormatDataList.end(); ++it )
00162   {
00163     const FormatData& formatData = *it;
00164 
00165     // only if the format is for text (id==1)
00166     if( formatData.id == 1 )
00167     {
00168 
00169        Q_UINT8 attr = 0; //invalid
00170        if( formatData.text.weight >= 75 ) attr = 12; // bold
00171        if( formatData.text.italic ) attr = 8;
00172        if( formatData.text.underline )
00173        {
00174          if( formatData.text.underlineValue == "double" )
00175            attr = 11; // double underline
00176          else
00177            attr = 14; // single underline
00178        }
00179        if( formatData.text.verticalAlignment == 1 ) attr = 6; //subscript
00180        if( formatData.text.verticalAlignment == 2 ) attr = 5; //superscript
00181        if( formatData.text.strikeout ) attr = 13;
00182 
00183        // due to the file format, before writing the text we must
00184        // write some prefix-code (such as Bold On) and possibly appropriate suffix-code (Bold Off)
00185 
00186        // attribute on
00187        if( attr > 0 ) output << (Q_UINT8)0xc3 << attr << (Q_UINT8)0xc3;
00188 
00189        // set font color
00190        QColor fgColor = formatData.text.fgColor;
00191        if( fgColor.isValid() )
00192        {
00193          Q_UINT8 wp_color[] = { 0xd1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0xd1 };
00194          wp_color[7] = (Q_UINT8) fgColor.red();
00195          wp_color[8] = (Q_UINT8) fgColor.green();
00196          wp_color[9] = (Q_UINT8) fgColor.blue();
00197          output.writeRawBytes( (const char*)wp_color, 14 );
00198        }
00199 
00200        // the text itself, "escape" it first
00201        QCString out = WPFiveEscape( paraText.mid( formatData.pos, formatData.len ) );
00202        output.writeRawBytes( (const char*)out, out.length() );
00203 
00204        // attribute off
00205        if( attr > 0 ) output << (Q_UINT8)0xc4 << attr << (Q_UINT8)0xc4;
00206     }
00207   }
00208 
00209   // write hard-return 
00210   output << (Q_UINT8) 0x0a;
00211 
00212   return true;
00213 }
00214 
KDE Home | KDE Accessibility Home | Description of Access Keys