summaryrefslogtreecommitdiffstats
path: root/filters/kword/html/export/ExportBasic.cpp
blob: d222d7df4a7dd488635aa1ccc08287f3e4804061 (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
/*
   This file is part of the KDE project
   Copyright (C) 2001, 2002 Nicolas GOUTTE <goutte@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 <tqstring.h>
#include <tqtextcodec.h>
#include <tqfile.h>

#include <tdelocale.h>
#include <kdebug.h>

#include <KWEFBaseWorker.h>

#include "ExportFilter.h"
#include "ExportBasic.h"

HtmlBasicWorker::HtmlBasicWorker( const TQString &cssURL )
{
  m_cssURL = cssURL;
}

TQString HtmlBasicWorker::textFormatToCss(const TextFormatting& formatData) const
{// PROVISORY
    TQString strElement;

    // Font name
    TQString fontName = formatData.fontName;
    if ( !fontName.isEmpty() )
    {
        strElement+="font-family: ";
        strElement+=escapeHtmlText(fontName); // TODO: add alternative font names
        strElement+="; ";
    }

    const int size=formatData.fontSize;
    if (size>0)
    {
        // We use absolute font sizes.
        strElement+="font-size: ";
        strElement+=TQString::number(size,10);
        strElement+="pt; ";
    }

    if ( formatData.fgColor.isValid() )
    {
        // Give colour
        strElement+="color: ";
        strElement+=formatData.fgColor.name();
        strElement+="; ";
    }
    return strElement;
}

TQString HtmlBasicWorker::getStartOfListOpeningTag(const CounterData::Style typeList, bool& ordered)
{
    TQString strResult;
    switch (typeList)
    {
    case CounterData::STYLE_CUSTOMBULLET: // We cannot keep the custom type/style
    default:
        {
            ordered=false;
            strResult="<ul>\n";
            break;
        }
    case CounterData::STYLE_NONE: // We cannot specify "no bullet"
        {
            ordered=false;
            strResult="<ul>\n";
            break;
        }
    case CounterData::STYLE_CIRCLEBULLET:
        {
            ordered=false;
            strResult="<ul type=\"circle\">\n";
            break;
        }
    case CounterData::STYLE_SQUAREBULLET:
        {
            ordered=false;
            strResult="<ul type=\"square\">\n";
            break;
        }
    case CounterData::STYLE_DISCBULLET:
        {
            ordered=false;
            strResult="<ul type=\"disc\">\n";
            break;
        }
    case CounterData::STYLE_NUM:
        {
            ordered=true;
            strResult="<ol type=\"1\">\n";
            break;
        }
    case CounterData::STYLE_ALPHAB_L:
        {
            ordered=true;
            strResult="<ol type=\"a\">\n";
            break;
        }
    case CounterData::STYLE_ALPHAB_U:
        {
            ordered=true;
            strResult="<ol type=\"A\">\n";
            break;
        }
    case CounterData::STYLE_ROM_NUM_L:
        {
            ordered=true;
            strResult="<ol type=\"i\">\n";
            break;
        }
    case CounterData::STYLE_ROM_NUM_U:
        {
            ordered=true;
            strResult="<ol type=\"I\">\n";
            break;
        }
    case CounterData::STYLE_CUSTOM:
        {
            // We cannot keep the custom type/style
            ordered=true;
            strResult="<ol>\n";
            break;
        }
    }
    return strResult;
}

void HtmlBasicWorker::writeDocType(void)
{
    // write <!DOCTYPE
    *m_streamOut << "<!DOCTYPE ";
    if (isXML())
    {
        *m_streamOut << "html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"";
        *m_streamOut << " \"DTD/xhtml1-transitional.dtd\">\n";

    }
    else
    {
        *m_streamOut << "HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"";
        *m_streamOut << " \"http://www.w3.org/TR/html4/loose.dtd\">\n";
    }
}

void HtmlBasicWorker::openFormatData(const FormatData& formatOrigin,
    const FormatData& format, const bool force,const bool allowBold)
{
    bool useCSS = !m_cssURL.isEmpty();
    TQString attr;

    if( !useCSS && ( force || formatOrigin.text.fontName != format.text.fontName ) && !format.text.fontName.isEmpty() )
    {
        attr += " face=\"";
        attr += escapeHtmlText(format.text.fontName); // TODO: add alternative font names
        attr += "\"";
    }

    if( !useCSS && ( force || formatOrigin.text.fontSize != format.text.fontSize ) && format.text.fontSize > 0 )
    {
        // We use absolute font sizes, as relative ones give too many problems.
        int size=format.text.fontSize;
        // 12pt is considered the normal size
        size /= 4;
        if (size<1) size=1;
        if (size>7) size=7;
        attr += " size=\""; // in XML numbers must be quoted!
        attr += TQString::number(size,10);
        attr += "\"";
    }

    if( ( force || formatOrigin.text.fgColor != format.text.fgColor ) &&
          format.text.fgColor.isValid() )
    {
        // Give colour
        attr += " color=\"";
        attr += format.text.fgColor.name();
        attr += "\"";
    }

    if( !attr.isEmpty() )
    {
        *m_streamOut << "<font" << attr << ">";
    }

    if (force || ((formatOrigin.text.weight>=75)!=(format.text.weight>=75)))
    {
        if (allowBold && (format.text.weight>=75))
        {
            *m_streamOut << "<b>";
        }
    }

    if (force || (formatOrigin.text.italic!=format.text.italic))
    {
        if (format.text.italic)
        {
            *m_streamOut << "<i>";
        }
    }

    if (force || (formatOrigin.text.underline!=format.text.underline))
    {
        if (format.text.underline)
        {
            *m_streamOut << "<u>";
        }
    }

    if (force || (formatOrigin.text.strikeout!=format.text.strikeout))
    {
        if (format.text.strikeout)
        {
            *m_streamOut << "<s>";
        }
    }

    if (force || (formatOrigin.text.verticalAlignment!=format.text.verticalAlignment))
    {
        if (1==format.text.verticalAlignment)
        {
            *m_streamOut << "<sub>"; //Subscript
        }
        else if (2==format.text.verticalAlignment)
        {
            *m_streamOut << "<sup>"; //Superscript
        }
    }
}

void HtmlBasicWorker::closeFormatData(const FormatData& formatOrigin,
    const FormatData& format, const bool force,const bool allowBold)
{
    if (force || (formatOrigin.text.verticalAlignment!=format.text.verticalAlignment))
    {
        if (2==format.text.verticalAlignment)
        {
            *m_streamOut << "</sup>"; //Superscript
        }
        else if (1==format.text.verticalAlignment)
        {
            *m_streamOut << "</sub>"; //Subscript
        }
    }

    if (force || (formatOrigin.text.strikeout!=format.text.strikeout))
    {
        if (format.text.strikeout)
        {
            *m_streamOut << "</s>";
        }
    }

    if (force || (formatOrigin.text.underline!=format.text.underline))
    {
        if (format.text.underline)
        {
            *m_streamOut << "</u>";
        }
    }

    if (force || (formatOrigin.text.italic!=format.text.italic))
    {
        if (format.text.italic)
        {
            *m_streamOut << "</i>";
        }
    }

    if (force || ((formatOrigin.text.weight>=75)!=(format.text.weight>=75)))
    {
        if (allowBold && (format.text.weight >= 75))
        {
            *m_streamOut << "</b>";
        }
    }

    bool fontName =  ( force || formatOrigin.text.fontName != format.text.fontName ) &&
                       !format.text.fontName.isEmpty();
    bool fontSize =  ( force || formatOrigin.text.fontSize != format.text.fontSize ) &&
                       format.text.fontSize>0;
    bool fontColor = ( force ||formatOrigin.text.fgColor != format.text.fgColor ) &&
                       format.text.fgColor.isValid();

    if( ( m_cssURL.isEmpty() && ( fontName || fontSize ) ) || fontColor )
    {
        *m_streamOut << "</font>";
    }

}

void HtmlBasicWorker::openParagraph(const TQString& strTag,
    const LayoutData& layout, TQChar::Direction direction)
{
    *m_streamOut << '<' << strTag;

    if ( (layout.alignment=="left") || (layout.alignment== "right")
        || (layout.alignment=="center") || (layout.alignment=="justify"))
    {
        *m_streamOut << " align=\"" << layout.alignment << "\"";
        if ( (direction == TQChar::DirRLE) || (direction == TQChar::DirRLO) )
          *m_streamOut << " dir=\"rtl\"";
    }
    else if ( layout.alignment=="auto")
    {
        // Do nothing, the user-agent should be more intelligent than us.
    }
    else
    {
        kdWarning(30503) << "Unknown alignment: " << layout.alignment << endl;
    }

    *m_streamOut << ">";

    // Allow bold only if tag is not a heading!
    openFormatData(layout.formatData,layout.formatData,true,(strTag[0]!='h'));
}

void HtmlBasicWorker::closeParagraph(const TQString& strTag,
    const LayoutData& layout)
{
     // Allow bold only if tag is not a heading!
    closeFormatData(layout.formatData,layout.formatData,true,(strTag[0]!='h'));

    *m_streamOut << "</" << strTag << ">\n";
}

void HtmlBasicWorker::openSpan(const FormatData& formatOrigin, const FormatData& format)
{
    openFormatData(formatOrigin,format,false,true);
}

void HtmlBasicWorker::closeSpan(const FormatData& formatOrigin, const FormatData& format)
{
    closeFormatData(formatOrigin,format,false,true);
}

bool HtmlBasicWorker::doOpenBody(void)
{
    // Define the background colour as white!
    *m_streamOut << "<body bgcolor=\"#FFFFFF\">\n";
    return true;
}

TQString HtmlBasicWorker::customCSSURL(void) const
{
  return m_cssURL;
}