summaryrefslogtreecommitdiffstats
path: root/filters/kword/wordperfect/import/kwordfilter.cpp
blob: de9246cc4a7d7177fc22a6f706da0f2f55b34c10 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* This file is part of the KDE project
   Copyright (C) 2001 Ariya Hidayat <ariyahidayat@yahoo.de>

   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 "kwordfilter.h"
#include "parser.h"

#include <tqfileinfo.h>
#include <tqptrlist.h>
#include <tqstring.h>
#include <tqregexp.h>

using namespace WP;

// NOTE: 1 wpu = 1/1200 inch and 1 inch = 72 point
static double WPUToPoint( unsigned wpu )
{
    return wpu * 72 / 1200;
}

// helper class
class KWordFormat
{
  public:
    bool bold, italic, underline, doubleunderline;
    bool striked, superscript, subscript, redline;
    bool color, highlight;
    int red, green, blue;
    int bgred, bggreen, bgblue;
    double fontsize;
    TQString fontface;

    KWordFormat();
    TQString asXML();
};

KWordFormat::KWordFormat()
{
  bold = italic = underline = doubleunderline = FALSE;
  striked = superscript = subscript = redline = FALSE;
  color = highlight = FALSE;
  red = green = blue = 0;
  bgred = bggreen = bgblue = 255;
  fontsize = 0.0;
  fontface = "";
}

TQString KWordFormat::asXML()
{
  TQString result;

  if( bold ) result.append( "    <WEIGHT value=\"75\" />\n" );
  if( italic ) result.append( "    <ITALIC value=\"1\" />\n" );
  if( underline ) result.append( "    <UNDERLINE value=\"1\" />\n" );
  if( doubleunderline ) result.append( "    <UNDERLINE value=\"double\" />\n" );
  if( striked ) result.append( "    <STRIKEOUT value=\"1\" />\n" );
  if( subscript ) result.append( "<VERTALIGN value=\"1\" />\n" );
  if( superscript ) result.append( "<VERTALIGN value=\"2\" />\n" );

  if( !fontface.isEmpty() )
    result.append( "<FONT name=\"" + fontface + "\" />\n" );

  if( fontsize > 0.0 )
    result.append( "    <SIZE value=\"" + TQString::number(fontsize) + "\" />\n" );

  if( color )
    result.append( "    <COLOR red=\"" + TQString::number(red) +
                   "\" green=\"" + TQString::number(green) +
                   "\" blue=\"" + TQString::number(blue) + "\" />\n" );
  
  if( highlight )
    result.append( "    <TEXTBACKGROUNDCOLOR red=\"" + TQString::number(bgred) +
                   "\" green=\"" + TQString::number(bggreen) +
                   "\" blue=\"" + TQString::number(bgblue) + "\" />\n" );

  return result;
}

static TQString mapAlign( Token::Align align )
{
  switch( align )
  {
  case Token::Left: return "left";
  case Token::Right: return "right";
  case Token::Center: return "center";
  case Token::Full: return "justify";
  case Token::All: return "justify";
  }
  return "left";
}

// NOTE: KWord value for linespace: 72=one, 144=double, ..
// Special case: "0" is normal, "oneandhalf" is 108, "double" is 144.
static TQString mapLinespace( double linespace )
{
  return TQString::number( linespace );
}

KWordFilter::KWordFilter ():Parser ()
{
}

bool
KWordFilter::parse (const TQString & filename)
{
  int frameLeftMargin = 36, frameRightMargin = 36; // quick hack

  if (!Parser::parse (filename))
    return FALSE;

  // this will force very last text and formatting to be flushed as well
  tokens.append( new Token( Token::HardReturn ) );

  TQString text;
  TQString tqlayout;
  TQString formats;
  int LeftMargin = 0;
  int TopMargin = 36;
  int RightMargin = 0;
  int BottomMargin = 36;
  int LeftMarginAdjust = 0;
  int RightMarginAdjust = 0;
  int lm = 0, rm = 0;
  Token::Align align = Token::Left;
  double linespace = 1.0;

  root = "";

  KWordFormat flag;
  int format_pos;
  TQString fmt;

  // FIXME replace with doc initial code or default style
  format_pos = 0;
  fmt = flag.asXML();

  for (TQPtrListIterator < Token > it (tokens); it; ++it)
    {
      unsigned int ucode;
      int attr;
      int len;
      Token *t = it.current ();
      Token::Type type = t->type ();

      switch (type)
        {

        case Token::Text:
          text.append( t->text() );
          break;

        case Token::SoftSpace:
        case Token::HardSpace:
          text.append( " " );
          break;

        case Token::SoftReturn:
          // ignore
          break;

        case Token::AttrOff:
        case Token::AttrOn:
        case Token::FontColor:
        case Token::FontSize:
        case Token::FontFace:
        case Token::HighlightOn:
        case Token::HighlightOff:

          if( type == Token::FontColor )
          {
            flag.color = true;
            flag.red = t->red();
            flag.green = t->green();
            flag.blue= t->blue();
          }
          else if( type == Token::HighlightOn )
          {
            flag.highlight = true;
            flag.bgred = t->red();
            flag.bggreen = t->green();
            flag.bgblue = t->blue();
          }
          else if( type == Token::HighlightOff )
          {
            // RGB in the data is last used highlight color
            // to go back to normal color, simply XOR would do the trick
            flag.highlight = false;
            flag.bgred ^= t->red();
            flag.bggreen ^= t->green();
            flag.bgblue ^= t->blue();
          }
          else if( type == Token::FontSize )
          {
            // WP font size is 1/3600th inch
            // NOTE 72 pt is 1 inch
            if( t->value() > 50 )
              flag.fontsize = t->value()*72.0/3600;
          }
          else if( type == Token::FontFace )
          {
            flag.fontface = t->fontface();
          }
          else
          {
            attr = t->attr();
            if( attr == Token::Bold ) flag.bold = ( type == Token::AttrOn );
            if( attr == Token::Italic ) flag.italic = ( type == Token::AttrOn );
            if( attr == Token::Underline) flag.underline = ( type == Token::AttrOn );
            if( attr == Token::DoubleUnderline ) flag.doubleunderline = ( type == Token::AttrOn );
            if( attr == Token::StrikedOut ) flag.striked = ( type == Token::AttrOn );
            if( attr == Token::Subscript ) flag.subscript = ( type == Token::AttrOn );
            if( attr == Token::Superscript ) flag.superscript = ( type == Token::AttrOn );
            if( attr == Token::Redline ) flag.redline = ( type == Token::AttrOn );
          }

          // process previous fmt first
          len = text.length() - format_pos;
          formats.append ( "<FORMAT id=\"1\" pos=\"" + TQString::number( format_pos ) +
                             "\" len=\"" + TQString::number( len )+ "\">\n" );
          formats.append( fmt );
          formats.append ( "</FORMAT>\n" );

          // now current format
          fmt = flag.asXML();
          format_pos= text.length();

          break;

        case Token::HardReturn:
        case Token::DormantHardReturn:

          // last formatting not flushed
          // SEE ABOVE
          len = text.length() - format_pos;
          formats.append ( "  <FORMAT id=\"1\" pos=\"" + TQString::number( format_pos ) +
                             "\" len=\"" + TQString::number( len )+ "\">\n" );
          formats.append( "  " + fmt );
          formats.append ( "  </FORMAT>\n" );

          tqlayout = "";
          tqlayout.append( "<LAYOUT>\n" );
          tqlayout.append( "  <NAME value=\"Standard\" />\n" );
          tqlayout.append( "  <FLOW align=\"" + mapAlign( align ) + "\" />\n" );
          tqlayout.append( "  <LINESPACING value=\"" + mapLinespace( linespace) + "\" />\n" );
          tqlayout.append( "  <LEFTBORDER width=\"0\" style=\"0\" />\n" );
          tqlayout.append( "  <RIGHTBORDER width=\"0\" style=\"0\" />\n" );
          tqlayout.append( "  <TOPBORDER width=\"0\" style=\"0\" />\n" );
          tqlayout.append( "  <BOTTOMBORDER width=\"0\" style=\"0\" />\n" );
          lm = LeftMargin + LeftMarginAdjust - frameLeftMargin;
          rm = RightMargin + RightMarginAdjust - frameRightMargin;
          tqlayout.append( "  <INDENTS left=\"" + TQString::number( TQMAX( 0, lm ) ) + "\"" + 
                         " right=\"" + TQString::number( TQMAX( 0 , rm ) ) + "\"" +
                         " first=\"0\" />\n" );
          tqlayout.append( "  <OFFSETS />\n" );
          tqlayout.append( "  <PAGEBREAKING />\n" );
          tqlayout.append( "  <COUNTER />\n" );
          tqlayout.append( "  <FORMAT id=\"1\">\n" );
          tqlayout.append( "    <WEIGHT value=\"50\" />\n" );
          tqlayout.append( "    <ITALIC value=\"0\" />\n" );
          tqlayout.append( "    <UNDERLINE value=\"0\" />\n" );
          tqlayout.append( "    <STRIKEOUT value=\"0\" />\n" );
          tqlayout.append( "    <CHARSET value=\"0\" />\n" );
          tqlayout.append( "    <VERTALIGN value=\"0\" />\n" );
          tqlayout.append( "  </FORMAT>\n" );
          tqlayout.append( "</LAYOUT>\n" );

          // encode text for XML-ness
          // FIXME could be faster without TQRegExp
          text.replace( TQRegExp("&"), "&amp;" );
          text.replace( TQRegExp("<"), "&lt;" );
          text.replace( TQRegExp(">"), "&gt;" );
          text.replace( TQRegExp("\""), "&quot;" );
          text.replace( TQRegExp("'"), "&apos;" );

          // construct the <PARAGRAPH>
          root.append( "<PARAGRAPH>\n" );
          root.append( "<TEXT>" + text + "</TEXT>\n" );
          root.append( "<FORMATS>\n");
          root.append( formats );
          root.append( "</FORMATS>\n");
          root.append( tqlayout );
          root.append( "</PARAGRAPH>\n" );

          // for the next paragraph
          text = "";
          formats = "";
          format_pos = 0;
          fmt = flag.asXML();

          break;

        case Token::HardHyphen:
          text.append( "-" );
          break;

        case Token::LeftMargin:
          LeftMargin = (int) WPUToPoint( t->value() );
          break;

        case Token::RightMargin:
          RightMargin = (int) WPUToPoint( t->value() );
          break;

        case Token::TopMargin:
          TopMargin = (int) WPUToPoint( t->value() );
          break;

        case Token::BottomMargin:
          BottomMargin = (int) WPUToPoint( t->value() );
          break;

        case Token::LeftMarginAdjust:
          LeftMarginAdjust = (int)WPUToPoint( t->value() );
          break;

        case Token::RightMarginAdjust:
          RightMarginAdjust = (int)WPUToPoint( t->value() );
          break;

        case Token::Justification:
          align = t->align();
          break;

        case Token::Linespace:
          // NOTE assume 1.0 = 12 pt, 2.0 = 24 pt, 1.5=18
          // from parser.cpp, linespace is stored as 1/65536th inch
          linespace = t->value() * 12.0 / 65536;
          break;

        case Token::ExtChar:
          ucode = Parser::ExtCharToUnicode (t->charset (), t->charcode ());
          if (ucode == 0) ucode = 32;
          text.append( TQChar (ucode) );
          break;

        case Token::TabHardFlushRight:
          // FIXME
          text.append( "    " );
          break;

        case Token::None:
        default:
          break;
        };

    }

  TQString content = root;

  root = "<!DOCTYPE DOC>\n";
  root.append( "<DOC mime=\"application/x-kword\" syntaxVersion=\"2\" editor=\"KWord\">\n");

  // quick hack, think of something better in the future
  LeftMargin = RightMargin = 36;

  // paper definition
  root.append( "<PAPER width=\"595\" height=\"841\" format=\"1\" fType=\"0\" orientation=\"0\" hType=\"0\" columns=\"1\">\n" );
  root.append( " <PAPERBORDERS left=\"" + TQString::number(frameLeftMargin) +
               "\" right=\"" + TQString::number(frameRightMargin) +
               "\" top=\"" + TQString::number(TopMargin) +
               "\" bottom=\"" + TQString::number(BottomMargin) + "\" />\n" );
  root.append( "</PAPER>\n" );

  root.append( "<ATTRIBUTES standardpage=\"1\" hasFooter=\"0\" hasHeader=\"0\" processing=\"0\" />\n" );

  root.append( "<FRAMESETS>\n" );
  root.append( "<FRAMESET removable=\"0\" frameType=\"1\" frameInfo=\"0\" autoCreateNewFrame=\"1\">\n" );
  root.append( "<FRAME right=\"567\" left=\"28\" top=\"42\" bottom=\"799\" />\n" );
  root.append( content );
  root.append( "</FRAMESET>\n" );
  root.append( "</FRAMESETS>\n" );

  root.append( "</DOC>\n" );

  // in case no document summary is available, then make default
  // set so that basename of the filename becomes the document title
  // e.g /home/ariya/test/resume.wpd will have 'resume' as the title
  if( docTitle.isEmpty() )
  {
    TQFileInfo info( filename );
    docTitle = info.baseName();
  }

  // create document information
  documentInfo = "<!DOCTYPE document-info>\n";

  documentInfo += "<document-info>\n";
  documentInfo += "<log><text></text></log>\n";

  documentInfo += "<author>\n";
  documentInfo += "<full-name>" + docAuthor + "</full-name>\n";
  documentInfo += "<title></title>\n";
  documentInfo += "<company></company>\n";
  documentInfo += "<email></email>\n";
  documentInfo += "<telephone></telephone>\n";
  documentInfo += "</author>\n";

  documentInfo += "<about>\n";
  documentInfo += "<abstract><![CDATA[" + docAbstract + "]]></abstract>\n";
  documentInfo += "<title>" + docTitle + "</title>\n";
  documentInfo += "</about>\n";

  documentInfo += "</document-info>";

  return TRUE;
}