wp5.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Ariya Hidayat <ariya@kde.org>
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 <config.h>
21
22#ifdef HAVE_UNISTD_H
23#include <unistd.h>
24#endif
25
26#include <tqtextcodec.h>
27#include <tqfile.h>
28#include <tqfileinfo.h>
29#include <tqtextstream.h>
30
31#include <KWEFBaseWorker.h>
32#include <KWEFKWordLeader.h>
33#include <KWEFUtil.h>
34
35#include <wp5.h>
36
37bool WPFiveWorker::doOpenFile(const TQString& filenameOut, const TQString& /*to*/)
38{
39 filename = filenameOut;
40 outfile.setName( filename );
41 if( !outfile.open( IO_WriteOnly ) )
42 return false;
43
44 output.setDevice( &outfile );
45 output.setByteOrder (TQDataStream::LittleEndian);
46 return true;
47}
48
49bool WPFiveWorker::doCloseFile(void)
50{
51 // asssume we're at the end of the file
52 TQ_UINT32 total_filesize = outfile.at();
53
54 // close the file first
55 outfile.close();
56
57 // reopen for read and write
58 if( !outfile.open( IO_ReadWrite ) )
59 return false;
60 output.setDevice( &outfile );
61 output.setByteOrder (TQDataStream::LittleEndian);
62
63 // now it's time to fix-up some header fields
64
65 // fix for offset 12, int32, filesize
66 outfile.at( 20 );
67 output << total_filesize;
68
69 // offset 4, int32, pointer to document area
70 outfile.at( 4 );
71 output << document_area_ptr;
72
73 outfile.close();
74 return true;
75}
76
77bool WPFiveWorker::doOpenDocument(void)
78{
79 // write WP document header
80 // note that some fields are still "dummy"
81
82 // magic id: -1, "WPC"
83 TQ_UINT8 magic[] = { 0xff, 0x57, 0x50, 0x43 };
84 for( int i=0; i<4; i++ ) output << magic[i];
85
86 // pointer to document area (dummy, will be fixed later)
87 TQ_UINT8 docptr[] = { 0x0E, 0x02, 0x00, 0x00 } ;
88 for( int i=0; i<4; i++ ) output << docptr[i];
89
90 // write product type ( 1 = WordPerfect )
91 TQ_UINT8 product_type = 1;
92 output << product_type;
93
94 // write file type ( 10 = WordPerfect document )
95 TQ_UINT8 file_type = 10;
96 output << file_type;
97
98 // write version ( 1 = WordPerfect 5.x )
99 TQ_UINT16 version = 0x0100;
100 output << version;
101
102 // write encryption flag ( 0 = not encrypted )
103 TQ_UINT16 encrypt = 0;
104 output << encrypt;
105
106 // offset to index header (always 0x200?)
107 TQ_UINT16 index_header_ptr = 0x200;
108 output << index_header_ptr;
109
110 // beginning of extended file header (always 5)
111 TQ_UINT32 extheader = 5;
112 output << extheader;
113
114 // filesize (dummy, will be fixed later)
115 TQ_UINT32 filesize = 0;
116 output << filesize;
117
118 // filler 488 bytes
119 TQ_UINT8 dummy = 0;
120 for( int i=0; i<488; i++ ) output << dummy;
121
122 // index header (specified 0 index!)
123 TQ_UINT8 index_header[] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124 0, 0, 0, 0 };
125 for( int i=0; i<14; i++ ) output << index_header[i];
126
127 // document area starts, mark it for the header fields
128 document_area_ptr = output.device()->at();
129
130 return true;
131}
132
133bool WPFiveWorker::doCloseDocument(void)
134{
135 return true;
136}
137
138// quick-and-dirty escape function for WP 5.x chars
139// TODO fix it !
140static TQCString WPFiveEscape( const TQString& text )
141{
142 TQCString result;
143
144 for( unsigned int i=0; i < text.length(); i++ )
145 {
146 int c = text[i].unicode();
147 if( c < 32 ) result += '.';
148 else if ( c == 32 ) result += 0x20 ; // hard space
149 else if ( c < 128 ) result += text[i].latin1();
150 else result += '.';
151 }
152
153 return result;
154}
155
156
157bool WPFiveWorker::doFullParagraph(const TQString& paraText,
158 const LayoutData& /*layout*/, const ValueListFormatData& paraFormatDataList)
159{
160 ValueListFormatData::ConstIterator it;
161 for( it = paraFormatDataList.begin(); it!=paraFormatDataList.end(); ++it )
162 {
163 const FormatData& formatData = *it;
164
165 // only if the format is for text (id==1)
166 if( formatData.id == 1 )
167 {
168
169 TQ_UINT8 attr = 0; //invalid
170 if( formatData.text.weight >= 75 ) attr = 12; // bold
171 if( formatData.text.italic ) attr = 8;
172 if( formatData.text.underline )
173 {
174 if( formatData.text.underlineValue == "double" )
175 attr = 11; // double underline
176 else
177 attr = 14; // single underline
178 }
179 if( formatData.text.verticalAlignment == 1 ) attr = 6; //subscript
180 if( formatData.text.verticalAlignment == 2 ) attr = 5; //superscript
181 if( formatData.text.strikeout ) attr = 13;
182
183 // due to the file format, before writing the text we must
184 // write some prefix-code (such as Bold On) and possibly appropriate suffix-code (Bold Off)
185
186 // attribute on
187 if( attr > 0 ) output << (TQ_UINT8)0xc3 << attr << (TQ_UINT8)0xc3;
188
189 // set font color
190 TQColor fgColor = formatData.text.fgColor;
191 if( fgColor.isValid() )
192 {
193 TQ_UINT8 wp_color[] = { 0xd1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0xd1 };
194 wp_color[7] = (TQ_UINT8) fgColor.red();
195 wp_color[8] = (TQ_UINT8) fgColor.green();
196 wp_color[9] = (TQ_UINT8) fgColor.blue();
197 output.writeRawBytes( (const char*)wp_color, 14 );
198 }
199
200 // the text itself, "escape" it first
201 TQCString out = WPFiveEscape( paraText.mid( formatData.pos, formatData.len ) );
202 output.writeRawBytes( (const char*)out, out.length() );
203
204 // attribute off
205 if( attr > 0 ) output << (TQ_UINT8)0xc4 << attr << (TQ_UINT8)0xc4;
206 }
207 }
208
209 // write hard-return
210 output << (TQ_UINT8) 0x0a;
211
212 return true;
213}
214
Paragraph layout.