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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/* This file is part of the KDE project
Copyright (C) 2002 Ariya Hidayat <ariya@kde.org>
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 <config.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <tqtextcodec.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqtextstream.h>
#include <KWEFBaseWorker.h>
#include <KWEFKWordLeader.h>
#include <KWEFUtil.h>
#include <wp5.h>
bool WPFiveWorker::doOpenFile(const TQString& filenameOut, const TQString& /*to*/)
{
filename = filenameOut;
outfile.setName( filename );
if( !outfile.open( IO_WriteOnly ) )
return false;
output.setDevice( &outfile );
output.setByteOrder (TQDataStream::LittleEndian);
return true;
}
bool WPFiveWorker::doCloseFile(void)
{
// asssume we're at the end of the file
TQ_UINT32 total_filesize = outfile.at();
// close the file first
outfile.close();
// reopen for read and write
if( !outfile.open( IO_ReadWrite ) )
return false;
output.setDevice( &outfile );
output.setByteOrder (TQDataStream::LittleEndian);
// now it's time to fix-up some header fields
// fix for offset 12, int32, filesize
outfile.at( 20 );
output << total_filesize;
// offset 4, int32, pointer to document area
outfile.at( 4 );
output << document_area_ptr;
outfile.close();
return true;
}
bool WPFiveWorker::doOpenDocument(void)
{
// write WP document header
// note that some fields are still "dummy"
// magic id: -1, "WPC"
TQ_UINT8 magic[] = { 0xff, 0x57, 0x50, 0x43 };
for( int i=0; i<4; i++ ) output << magic[i];
// pointer to document area (dummy, will be fixed later)
TQ_UINT8 docptr[] = { 0x0E, 0x02, 0x00, 0x00 } ;
for( int i=0; i<4; i++ ) output << docptr[i];
// write product type ( 1 = WordPerfect )
TQ_UINT8 product_type = 1;
output << product_type;
// write file type ( 10 = WordPerfect document )
TQ_UINT8 file_type = 10;
output << file_type;
// write version ( 1 = WordPerfect 5.x )
TQ_UINT16 version = 0x0100;
output << version;
// write encryption flag ( 0 = not encrypted )
TQ_UINT16 encrypt = 0;
output << encrypt;
// offset to index header (always 0x200?)
TQ_UINT16 index_header_ptr = 0x200;
output << index_header_ptr;
// beginning of extended file header (always 5)
TQ_UINT32 extheader = 5;
output << extheader;
// filesize (dummy, will be fixed later)
TQ_UINT32 filesize = 0;
output << filesize;
// filler 488 bytes
TQ_UINT8 dummy = 0;
for( int i=0; i<488; i++ ) output << dummy;
// index header (specified 0 index!)
TQ_UINT8 index_header[] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 };
for( int i=0; i<14; i++ ) output << index_header[i];
// document area starts, mark it for the header fields
document_area_ptr = output.device()->at();
return true;
}
bool WPFiveWorker::doCloseDocument(void)
{
return true;
}
// quick-and-dirty escape function for WP 5.x chars
// TODO fix it !
static TQCString WPFiveEscape( const TQString& text )
{
TQCString result;
for( unsigned int i=0; i < text.length(); i++ )
{
int c = text[i].unicode();
if( c < 32 ) result += '.';
else if ( c == 32 ) result += 0x20 ; // hard space
else if ( c < 128 ) result += text[i].latin1();
else result += '.';
}
return result;
}
bool WPFiveWorker::doFullParagraph(const TQString& paraText,
const LayoutData& /*layout*/, const ValueListFormatData& paraFormatDataList)
{
ValueListFormatData::ConstIterator it;
for( it = paraFormatDataList.begin(); it!=paraFormatDataList.end(); ++it )
{
const FormatData& formatData = *it;
// only if the format is for text (id==1)
if( formatData.id == 1 )
{
TQ_UINT8 attr = 0; //invalid
if( formatData.text.weight >= 75 ) attr = 12; // bold
if( formatData.text.italic ) attr = 8;
if( formatData.text.underline )
{
if( formatData.text.underlineValue == "double" )
attr = 11; // double underline
else
attr = 14; // single underline
}
if( formatData.text.verticalAlignment == 1 ) attr = 6; //subscript
if( formatData.text.verticalAlignment == 2 ) attr = 5; //superscript
if( formatData.text.strikeout ) attr = 13;
// due to the file format, before writing the text we must
// write some prefix-code (such as Bold On) and possibly appropriate suffix-code (Bold Off)
// attribute on
if( attr > 0 ) output << (TQ_UINT8)0xc3 << attr << (TQ_UINT8)0xc3;
// set font color
TQColor fgColor = formatData.text.fgColor;
if( fgColor.isValid() )
{
TQ_UINT8 wp_color[] = { 0xd1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0xd1 };
wp_color[7] = (TQ_UINT8) fgColor.red();
wp_color[8] = (TQ_UINT8) fgColor.green();
wp_color[9] = (TQ_UINT8) fgColor.blue();
output.writeRawBytes( (const char*)wp_color, 14 );
}
// the text itself, "escape" it first
TQCString out = WPFiveEscape( paraText.mid( formatData.pos, formatData.len ) );
output.writeRawBytes( (const char*)out, out.length() );
// attribute off
if( attr > 0 ) output << (TQ_UINT8)0xc4 << attr << (TQ_UINT8)0xc4;
}
}
// write hard-return
output << (TQ_UINT8) 0x0a;
return true;
}
|