filters

ExportBasic.cpp
1/*
2 This file is part of the KDE project
3 Copyright (C) 2001, 2002 Nicolas GOUTTE <goutte@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tqstring.h>
22#include <tqtextcodec.h>
23#include <tqfile.h>
24
25#include <tdelocale.h>
26#include <kdebug.h>
27
28#include <KWEFBaseWorker.h>
29
30#include "ExportFilter.h"
31#include "ExportBasic.h"
32
33HtmlBasicWorker::HtmlBasicWorker( const TQString &cssURL )
34{
35 m_cssURL = cssURL;
36}
37
38TQString HtmlBasicWorker::textFormatToCss(const TextFormatting& formatData) const
39{// PROVISORY
40 TQString strElement;
41
42 // Font name
43 TQString fontName = formatData.fontName;
44 if ( !fontName.isEmpty() )
45 {
46 strElement+="font-family: ";
47 strElement+=escapeHtmlText(fontName); // TODO: add alternative font names
48 strElement+="; ";
49 }
50
51 const int size=formatData.fontSize;
52 if (size>0)
53 {
54 // We use absolute font sizes.
55 strElement+="font-size: ";
56 strElement+=TQString::number(size,10);
57 strElement+="pt; ";
58 }
59
60 if ( formatData.fgColor.isValid() )
61 {
62 // Give colour
63 strElement+="color: ";
64 strElement+=formatData.fgColor.name();
65 strElement+="; ";
66 }
67 return strElement;
68}
69
70TQString HtmlBasicWorker::getStartOfListOpeningTag(const CounterData::Style typeList, bool& ordered)
71{
72 TQString strResult;
73 switch (typeList)
74 {
75 case CounterData::STYLE_CUSTOMBULLET: // We cannot keep the custom type/style
76 default:
77 {
78 ordered=false;
79 strResult="<ul>\n";
80 break;
81 }
82 case CounterData::STYLE_NONE: // We cannot specify "no bullet"
83 {
84 ordered=false;
85 strResult="<ul>\n";
86 break;
87 }
88 case CounterData::STYLE_CIRCLEBULLET:
89 {
90 ordered=false;
91 strResult="<ul type=\"circle\">\n";
92 break;
93 }
94 case CounterData::STYLE_SQUAREBULLET:
95 {
96 ordered=false;
97 strResult="<ul type=\"square\">\n";
98 break;
99 }
100 case CounterData::STYLE_DISCBULLET:
101 {
102 ordered=false;
103 strResult="<ul type=\"disc\">\n";
104 break;
105 }
106 case CounterData::STYLE_NUM:
107 {
108 ordered=true;
109 strResult="<ol type=\"1\">\n";
110 break;
111 }
112 case CounterData::STYLE_ALPHAB_L:
113 {
114 ordered=true;
115 strResult="<ol type=\"a\">\n";
116 break;
117 }
118 case CounterData::STYLE_ALPHAB_U:
119 {
120 ordered=true;
121 strResult="<ol type=\"A\">\n";
122 break;
123 }
124 case CounterData::STYLE_ROM_NUM_L:
125 {
126 ordered=true;
127 strResult="<ol type=\"i\">\n";
128 break;
129 }
130 case CounterData::STYLE_ROM_NUM_U:
131 {
132 ordered=true;
133 strResult="<ol type=\"I\">\n";
134 break;
135 }
136 case CounterData::STYLE_CUSTOM:
137 {
138 // We cannot keep the custom type/style
139 ordered=true;
140 strResult="<ol>\n";
141 break;
142 }
143 }
144 return strResult;
145}
146
147void HtmlBasicWorker::writeDocType(void)
148{
149 // write <!DOCTYPE
150 *m_streamOut << "<!DOCTYPE ";
151 if (isXML())
152 {
153 *m_streamOut << "html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"";
154 *m_streamOut << " \"DTD/xhtml1-transitional.dtd\">\n";
155
156 }
157 else
158 {
159 *m_streamOut << "HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"";
160 *m_streamOut << " \"http://www.w3.org/TR/html4/loose.dtd\">\n";
161 }
162}
163
164void HtmlBasicWorker::openFormatData(const FormatData& formatOrigin,
165 const FormatData& format, const bool force,const bool allowBold)
166{
167 bool useCSS = !m_cssURL.isEmpty();
168 TQString attr;
169
170 if( !useCSS && ( force || formatOrigin.text.fontName != format.text.fontName ) && !format.text.fontName.isEmpty() )
171 {
172 attr += " face=\"";
173 attr += escapeHtmlText(format.text.fontName); // TODO: add alternative font names
174 attr += "\"";
175 }
176
177 if( !useCSS && ( force || formatOrigin.text.fontSize != format.text.fontSize ) && format.text.fontSize > 0 )
178 {
179 // We use absolute font sizes, as relative ones give too many problems.
180 int size=format.text.fontSize;
181 // 12pt is considered the normal size
182 size /= 4;
183 if (size<1) size=1;
184 if (size>7) size=7;
185 attr += " size=\""; // in XML numbers must be quoted!
186 attr += TQString::number(size,10);
187 attr += "\"";
188 }
189
190 if( ( force || formatOrigin.text.fgColor != format.text.fgColor ) &&
191 format.text.fgColor.isValid() )
192 {
193 // Give colour
194 attr += " color=\"";
195 attr += format.text.fgColor.name();
196 attr += "\"";
197 }
198
199 if( !attr.isEmpty() )
200 {
201 *m_streamOut << "<font" << attr << ">";
202 }
203
204 if (force || ((formatOrigin.text.weight>=75)!=(format.text.weight>=75)))
205 {
206 if (allowBold && (format.text.weight>=75))
207 {
208 *m_streamOut << "<b>";
209 }
210 }
211
212 if (force || (formatOrigin.text.italic!=format.text.italic))
213 {
214 if (format.text.italic)
215 {
216 *m_streamOut << "<i>";
217 }
218 }
219
220 if (force || (formatOrigin.text.underline!=format.text.underline))
221 {
222 if (format.text.underline)
223 {
224 *m_streamOut << "<u>";
225 }
226 }
227
228 if (force || (formatOrigin.text.strikeout!=format.text.strikeout))
229 {
230 if (format.text.strikeout)
231 {
232 *m_streamOut << "<s>";
233 }
234 }
235
236 if (force || (formatOrigin.text.verticalAlignment!=format.text.verticalAlignment))
237 {
238 if (1==format.text.verticalAlignment)
239 {
240 *m_streamOut << "<sub>"; //Subscript
241 }
242 else if (2==format.text.verticalAlignment)
243 {
244 *m_streamOut << "<sup>"; //Superscript
245 }
246 }
247}
248
249void HtmlBasicWorker::closeFormatData(const FormatData& formatOrigin,
250 const FormatData& format, const bool force,const bool allowBold)
251{
252 if (force || (formatOrigin.text.verticalAlignment!=format.text.verticalAlignment))
253 {
254 if (2==format.text.verticalAlignment)
255 {
256 *m_streamOut << "</sup>"; //Superscript
257 }
258 else if (1==format.text.verticalAlignment)
259 {
260 *m_streamOut << "</sub>"; //Subscript
261 }
262 }
263
264 if (force || (formatOrigin.text.strikeout!=format.text.strikeout))
265 {
266 if (format.text.strikeout)
267 {
268 *m_streamOut << "</s>";
269 }
270 }
271
272 if (force || (formatOrigin.text.underline!=format.text.underline))
273 {
274 if (format.text.underline)
275 {
276 *m_streamOut << "</u>";
277 }
278 }
279
280 if (force || (formatOrigin.text.italic!=format.text.italic))
281 {
282 if (format.text.italic)
283 {
284 *m_streamOut << "</i>";
285 }
286 }
287
288 if (force || ((formatOrigin.text.weight>=75)!=(format.text.weight>=75)))
289 {
290 if (allowBold && (format.text.weight >= 75))
291 {
292 *m_streamOut << "</b>";
293 }
294 }
295
296 bool fontName = ( force || formatOrigin.text.fontName != format.text.fontName ) &&
297 !format.text.fontName.isEmpty();
298 bool fontSize = ( force || formatOrigin.text.fontSize != format.text.fontSize ) &&
299 format.text.fontSize>0;
300 bool fontColor = ( force ||formatOrigin.text.fgColor != format.text.fgColor ) &&
301 format.text.fgColor.isValid();
302
303 if( ( m_cssURL.isEmpty() && ( fontName || fontSize ) ) || fontColor )
304 {
305 *m_streamOut << "</font>";
306 }
307
308}
309
310void HtmlBasicWorker::openParagraph(const TQString& strTag,
311 const LayoutData& layout, TQChar::Direction direction)
312{
313 *m_streamOut << '<' << strTag;
314
315 if ( (layout.alignment=="left") || (layout.alignment== "right")
316 || (layout.alignment=="center") || (layout.alignment=="justify"))
317 {
318 *m_streamOut << " align=\"" << layout.alignment << "\"";
319 if ( (direction == TQChar::DirRLE) || (direction == TQChar::DirRLO) )
320 *m_streamOut << " dir=\"rtl\"";
321 }
322 else if ( layout.alignment=="auto")
323 {
324 // Do nothing, the user-agent should be more intelligent than us.
325 }
326 else
327 {
328 kdWarning(30503) << "Unknown alignment: " << layout.alignment << endl;
329 }
330
331 *m_streamOut << ">";
332
333 // Allow bold only if tag is not a heading!
334 openFormatData(layout.formatData,layout.formatData,true,(strTag[0]!='h'));
335}
336
337void HtmlBasicWorker::closeParagraph(const TQString& strTag,
338 const LayoutData& layout)
339{
340 // Allow bold only if tag is not a heading!
341 closeFormatData(layout.formatData,layout.formatData,true,(strTag[0]!='h'));
342
343 *m_streamOut << "</" << strTag << ">\n";
344}
345
346void HtmlBasicWorker::openSpan(const FormatData& formatOrigin, const FormatData& format)
347{
348 openFormatData(formatOrigin,format,false,true);
349}
350
351void HtmlBasicWorker::closeSpan(const FormatData& formatOrigin, const FormatData& format)
352{
353 closeFormatData(formatOrigin,format,false,true);
354}
355
357{
358 // Define the background colour as white!
359 *m_streamOut << "<body bgcolor=\"#FFFFFF\">\n";
360 return true;
361}
362
363TQString HtmlBasicWorker::customCSSURL(void) const
364{
365 return m_cssURL;
366}
virtual bool doOpenBody(void)
Like HTML's <BODY>.
Paragraph layout.
TQString alignment
left, right, center, justify
TextFormatting is a container for data retrieved from the <FORMAT> tag and its subtags to be used in ...
bool underline
Any underline? (If true, we do not know if simple or double or what else).
bool strikeout
true if strikeout