filters

ExportBasic.cc

00001 /*
00002    This file is part of the KDE project
00003    Copyright (C) 2001, 2002 Nicolas GOUTTE <goutte@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qstring.h>
00022 #include <qtextcodec.h>
00023 #include <qfile.h>
00024 
00025 #include <klocale.h>
00026 #include <kdebug.h>
00027 
00028 #include <KWEFBaseWorker.h>
00029 
00030 #include "ExportFilter.h"
00031 #include "ExportBasic.h"
00032 
00033 HtmlBasicWorker::HtmlBasicWorker( const QString &cssURL )
00034 {
00035   m_cssURL = cssURL;
00036 }
00037 
00038 QString HtmlBasicWorker::textFormatToCss(const TextFormatting& formatData) const
00039 {// PROVISORY
00040     QString strElement;
00041 
00042     // Font name
00043     QString fontName = formatData.fontName;
00044     if ( !fontName.isEmpty() )
00045     {
00046         strElement+="font-family: ";
00047         strElement+=escapeHtmlText(fontName); // TODO: add alternative font names
00048         strElement+="; ";
00049     }
00050 
00051     const int size=formatData.fontSize;
00052     if (size>0)
00053     {
00054         // We use absolute font sizes.
00055         strElement+="font-size: ";
00056         strElement+=QString::number(size,10);
00057         strElement+="pt; ";
00058     }
00059 
00060     if ( formatData.fgColor.isValid() )
00061     {
00062         // Give colour
00063         strElement+="color: ";
00064         strElement+=formatData.fgColor.name();
00065         strElement+="; ";
00066     }
00067     return strElement;
00068 }
00069 
00070 QString HtmlBasicWorker::getStartOfListOpeningTag(const CounterData::Style typeList, bool& ordered)
00071 {
00072     QString strResult;
00073     switch (typeList)
00074     {
00075     case CounterData::STYLE_CUSTOMBULLET: // We cannot keep the custom type/style
00076     default:
00077         {
00078             ordered=false;
00079             strResult="<ul>\n";
00080             break;
00081         }
00082     case CounterData::STYLE_NONE: // We cannot specify "no bullet"
00083         {
00084             ordered=false;
00085             strResult="<ul>\n";
00086             break;
00087         }
00088     case CounterData::STYLE_CIRCLEBULLET:
00089         {
00090             ordered=false;
00091             strResult="<ul type=\"circle\">\n";
00092             break;
00093         }
00094     case CounterData::STYLE_SQUAREBULLET:
00095         {
00096             ordered=false;
00097             strResult="<ul type=\"square\">\n";
00098             break;
00099         }
00100     case CounterData::STYLE_DISCBULLET:
00101         {
00102             ordered=false;
00103             strResult="<ul type=\"disc\">\n";
00104             break;
00105         }
00106     case CounterData::STYLE_NUM:
00107         {
00108             ordered=true;
00109             strResult="<ol type=\"1\">\n";
00110             break;
00111         }
00112     case CounterData::STYLE_ALPHAB_L:
00113         {
00114             ordered=true;
00115             strResult="<ol type=\"a\">\n";
00116             break;
00117         }
00118     case CounterData::STYLE_ALPHAB_U:
00119         {
00120             ordered=true;
00121             strResult="<ol type=\"A\">\n";
00122             break;
00123         }
00124     case CounterData::STYLE_ROM_NUM_L:
00125         {
00126             ordered=true;
00127             strResult="<ol type=\"i\">\n";
00128             break;
00129         }
00130     case CounterData::STYLE_ROM_NUM_U:
00131         {
00132             ordered=true;
00133             strResult="<ol type=\"I\">\n";
00134             break;
00135         }
00136     case CounterData::STYLE_CUSTOM:
00137         {
00138             // We cannot keep the custom type/style
00139             ordered=true;
00140             strResult="<ol>\n";
00141             break;
00142         }
00143     }
00144     return strResult;
00145 }
00146 
00147 void HtmlBasicWorker::writeDocType(void)
00148 {
00149     // write <!DOCTYPE
00150     *m_streamOut << "<!DOCTYPE ";
00151     if (isXML())
00152     {
00153         *m_streamOut << "html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"";
00154         *m_streamOut << " \"DTD/xhtml1-transitional.dtd\">\n";
00155 
00156     }
00157     else
00158     {
00159         *m_streamOut << "HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"";
00160         *m_streamOut << " \"http://www.w3.org/TR/html4/loose.dtd\">\n";
00161     }
00162 }
00163 
00164 void HtmlBasicWorker::openFormatData(const FormatData& formatOrigin,
00165     const FormatData& format, const bool force,const bool allowBold)
00166 {
00167     bool useCSS = !m_cssURL.isEmpty();
00168     QString attr;
00169 
00170     if( !useCSS && ( force || formatOrigin.text.fontName != format.text.fontName ) && !format.text.fontName.isEmpty() )
00171     {
00172         attr += " face=\"";
00173         attr += escapeHtmlText(format.text.fontName); // TODO: add alternative font names
00174         attr += "\"";
00175     }
00176 
00177     if( !useCSS && ( force || formatOrigin.text.fontSize != format.text.fontSize ) && format.text.fontSize > 0 )
00178     {
00179         // We use absolute font sizes, as relative ones give too many problems.
00180         int size=format.text.fontSize;
00181         // 12pt is considered the normal size
00182         size /= 4;
00183         if (size<1) size=1;
00184         if (size>7) size=7;
00185         attr += " size=\""; // in XML numbers must be quoted!
00186         attr += QString::number(size,10);
00187         attr += "\"";
00188     }
00189 
00190     if( ( force || formatOrigin.text.fgColor != format.text.fgColor ) &&
00191           format.text.fgColor.isValid() )
00192     {
00193         // Give colour
00194         attr += " color=\"";
00195         attr += format.text.fgColor.name();
00196         attr += "\"";
00197     }
00198 
00199     if( !attr.isEmpty() )
00200     {
00201         *m_streamOut << "<font" << attr << ">";
00202     }
00203 
00204     if (force || ((formatOrigin.text.weight>=75)!=(format.text.weight>=75)))
00205     {
00206         if (allowBold && (format.text.weight>=75))
00207         {
00208             *m_streamOut << "<b>";
00209         }
00210     }
00211 
00212     if (force || (formatOrigin.text.italic!=format.text.italic))
00213     {
00214         if (format.text.italic)
00215         {
00216             *m_streamOut << "<i>";
00217         }
00218     }
00219 
00220     if (force || (formatOrigin.text.underline!=format.text.underline))
00221     {
00222         if (format.text.underline)
00223         {
00224             *m_streamOut << "<u>";
00225         }
00226     }
00227 
00228     if (force || (formatOrigin.text.strikeout!=format.text.strikeout))
00229     {
00230         if (format.text.strikeout)
00231         {
00232             *m_streamOut << "<s>";
00233         }
00234     }
00235 
00236     if (force || (formatOrigin.text.verticalAlignment!=format.text.verticalAlignment))
00237     {
00238         if (1==format.text.verticalAlignment)
00239         {
00240             *m_streamOut << "<sub>"; //Subscript
00241         }
00242         else if (2==format.text.verticalAlignment)
00243         {
00244             *m_streamOut << "<sup>"; //Superscript
00245         }
00246     }
00247 }
00248 
00249 void HtmlBasicWorker::closeFormatData(const FormatData& formatOrigin,
00250     const FormatData& format, const bool force,const bool allowBold)
00251 {
00252     if (force || (formatOrigin.text.verticalAlignment!=format.text.verticalAlignment))
00253     {
00254         if (2==format.text.verticalAlignment)
00255         {
00256             *m_streamOut << "</sup>"; //Superscript
00257         }
00258         else if (1==format.text.verticalAlignment)
00259         {
00260             *m_streamOut << "</sub>"; //Subscript
00261         }
00262     }
00263 
00264     if (force || (formatOrigin.text.strikeout!=format.text.strikeout))
00265     {
00266         if (format.text.strikeout)
00267         {
00268             *m_streamOut << "</s>";
00269         }
00270     }
00271 
00272     if (force || (formatOrigin.text.underline!=format.text.underline))
00273     {
00274         if (format.text.underline)
00275         {
00276             *m_streamOut << "</u>";
00277         }
00278     }
00279 
00280     if (force || (formatOrigin.text.italic!=format.text.italic))
00281     {
00282         if (format.text.italic)
00283         {
00284             *m_streamOut << "</i>";
00285         }
00286     }
00287 
00288     if (force || ((formatOrigin.text.weight>=75)!=(format.text.weight>=75)))
00289     {
00290         if (allowBold && (format.text.weight >= 75))
00291         {
00292             *m_streamOut << "</b>";
00293         }
00294     }
00295 
00296     bool fontName =  ( force || formatOrigin.text.fontName != format.text.fontName ) &&
00297                        !format.text.fontName.isEmpty();
00298     bool fontSize =  ( force || formatOrigin.text.fontSize != format.text.fontSize ) &&
00299                        format.text.fontSize>0;
00300     bool fontColor = ( force ||formatOrigin.text.fgColor != format.text.fgColor ) &&
00301                        format.text.fgColor.isValid();
00302 
00303     if( ( m_cssURL.isEmpty() && ( fontName || fontSize ) ) || fontColor )
00304     {
00305         *m_streamOut << "</font>";
00306     }
00307 
00308 }
00309 
00310 void HtmlBasicWorker::openParagraph(const QString& strTag,
00311     const LayoutData& layout, QChar::Direction direction)
00312 {
00313     *m_streamOut << '<' << strTag;
00314 
00315     if ( (layout.alignment=="left") || (layout.alignment== "right")
00316         || (layout.alignment=="center") || (layout.alignment=="justify"))
00317     {
00318         *m_streamOut << " align=\"" << layout.alignment << "\"";
00319         if ( (direction == QChar::DirRLE) || (direction == QChar::DirRLO) )
00320           *m_streamOut << " dir=\"rtl\"";
00321     }
00322     else if ( layout.alignment=="auto")
00323     {
00324         // Do nothing, the user-agent should be more intelligent than us.
00325     }
00326     else
00327     {
00328         kdWarning(30503) << "Unknown alignment: " << layout.alignment << endl;
00329     }
00330 
00331     *m_streamOut << ">";
00332 
00333     // Allow bold only if tag is not a heading!
00334     openFormatData(layout.formatData,layout.formatData,true,(strTag[0]!='h'));
00335 }
00336 
00337 void HtmlBasicWorker::closeParagraph(const QString& strTag,
00338     const LayoutData& layout)
00339 {
00340      // Allow bold only if tag is not a heading!
00341     closeFormatData(layout.formatData,layout.formatData,true,(strTag[0]!='h'));
00342 
00343     *m_streamOut << "</" << strTag << ">\n";
00344 }
00345 
00346 void HtmlBasicWorker::openSpan(const FormatData& formatOrigin, const FormatData& format)
00347 {
00348     openFormatData(formatOrigin,format,false,true);
00349 }
00350 
00351 void HtmlBasicWorker::closeSpan(const FormatData& formatOrigin, const FormatData& format)
00352 {
00353     closeFormatData(formatOrigin,format,false,true);
00354 }
00355 
00356 bool HtmlBasicWorker::doOpenBody(void)
00357 {
00358     // Define the background colour as white!
00359     *m_streamOut << "<body bgcolor=\"#FFFFFF\">\n";
00360     return true;
00361 }
00362 
00363 QString HtmlBasicWorker::customCSSURL(void) const
00364 {
00365   return m_cssURL;
00366 }
KDE Home | KDE Accessibility Home | Description of Access Keys