File indexing completed on 2025-02-16 13:49:56

0001 /*
0002 ** A program to convert the XML rendered by Words into LATEX.
0003 **
0004 ** Copyright (C) 2000, 2002, 2003 Robert JACOLIN
0005 **
0006 ** This library is free software; you can redistribute it and/or
0007 ** modify it under the terms of the GNU Library General Public
0008 ** License as published by the Free Software Foundation; either
0009 ** version 2 of the License, or (at your option) any later version.
0010 **
0011 ** This library is distributed in the hope that it will be useful,
0012 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014 ** Library General Public License for more details.
0015 **
0016 ** To receive a copy of the GNU Library General Public License, write to the
0017 ** Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 **
0020 */
0021 
0022 #include "fileheader.h"
0023 
0024 #include <stdlib.h>
0025 
0026 #include "LatexDebug.h"
0027 #include "config.h"
0028 
0029 #include <QTextStream>
0030 
0031 FileHeader* FileHeader::_instance = 0;
0032 
0033 /*******************************************/
0034 /* Constructor                             */
0035 /*******************************************/
0036 FileHeader::FileHeader()
0037 {
0038     _hasHeader    = false;
0039     _hasFooter    = false;
0040     _hasColor     = false;
0041     _hasUnderline = false;
0042     _hasEnumerate = false;
0043     _hasGraphics  = false;
0044     _hasTable     = false;
0045     _standardPage = 0;
0046     _processing   = TP_NORMAL;
0047     //setFileHeader(this);  /* for xmlParser class. */
0048 }
0049 
0050 /*******************************************/
0051 /* Destructor                              */
0052 /*******************************************/
0053 FileHeader::~FileHeader()
0054 {
0055     debugLatex << "FileHeader Destructor";
0056 }
0057 
0058 /*******************************************/
0059 /* AnalyzePaperParam                       */
0060 /*******************************************/
0061 void FileHeader::analyzePaperParam(const QDomNode node)
0062 {
0063     setFormat(getAttr(node, "format").toInt());
0064     _width = getAttr(node, "width").toInt();
0065     _height = getAttr(node, "height").toInt();
0066     setOrientation(getAttr(node, "orientation").toInt());
0067     setColumns(getAttr(node, "columns").toInt());
0068     _columnSpacing = getAttr(node, "columnspacing").toInt();
0069     setHeadType(getAttr(node, "hType").toInt());
0070     setFootType(getAttr(node, "fType").toInt());
0071     _headBody = getAttr(node, "spHeadBody").toInt();
0072     _footBody = getAttr(node, "spFootBody").toInt();
0073     //getAttr(node, "zoom").toInt();
0074 }
0075 
0076 /*******************************************/
0077 /* AnalyzePaper                            */
0078 /*******************************************/
0079 void FileHeader::analyzePaper(const QDomNode node)
0080 {
0081     analyzePaperParam(node);
0082 
0083     // Analyze child markup --> PAPERBORDERS
0084     QDomNode childNode = getChild(node, "PAPERBORDERS");
0085     _leftBorder = getAttr(childNode, "left").toInt();
0086     _rightBorder = getAttr(childNode, "right").toInt();
0087     _bottomBorder = getAttr(childNode, "bottom").toInt();
0088     _topBorder = getAttr(childNode, "top").toInt();
0089 }
0090 
0091 /*******************************************/
0092 /* AnalyzeAttributes                        */
0093 /*******************************************/
0094 void FileHeader::analyzeAttributes(const QDomNode node)
0095 {
0096     setProcessing(getAttr(node, "processing").toInt());
0097     setStandardPge(getAttr(node, "standardpage").toInt());
0098     setTOC(getAttr(node, "hasTOC").toInt());
0099     _hasHeader = getAttr(node, "hasHeader").toInt();
0100     _hasFooter = getAttr(node, "hasFooter").toInt();
0101     setUnit(getAttr(node, "unit").toInt());
0102 }
0103 
0104 /*******************************************/
0105 /* Generate                                */
0106 /*******************************************/
0107 void FileHeader::generate(QTextStream &out)
0108 {
0109     debugLatex << "GENERATION OF THE FILE HEADER";
0110     if (Config::instance()->mustUseLatin1())
0111         generateLatinPreamble(out);
0112     else if (Config::instance()->mustUseUnicode())
0113         generateUnicodePreamble(out);
0114 
0115     generatePackage(out);
0116     if (getFormat() == TF_CUSTOM)
0117         generatePaper(out);
0118     out << "%%%%%%%%%%%%%%%%%% END OF PREAMBLE %%%%%%%%%%%%%%%%%%" << endl << endl;
0119 }
0120 
0121 /*******************************************/
0122 /* GeneratePaper                           */
0123 /*******************************************/
0124 void FileHeader::generatePaper(QTextStream &out)
0125 {
0126     QString unit;
0127 
0128     out << "% Format of paper" << endl;
0129     debugLatex << "Generate custom size paper";
0130     /* paper size */
0131     out << "\\setlength{\\paperwidth}{"  << _width  << "pt}" << endl;
0132     out << "\\setlength{\\paperheight}{" << _height << "pt}" << endl;
0133     /* FileHeader and footer */
0134     out << "\\setlength{\\headsep}{" << _headBody << "pt}" << endl;
0135     out << "\\setlength{\\footskip}{" << _footBody + _bottomBorder << "pt}" << endl;
0136     /* Margin */
0137     out << "\\setlength{\\topmargin}{" << _topBorder << "pt}" << endl;
0138     out << "\\setlength{\\textwidth}{" << _width - _rightBorder - _leftBorder << "pt}" << endl;
0139     out << endl;
0140 }
0141 
0142 /*******************************************/
0143 /* GenerateLatinPreamble                   */
0144 /*******************************************/
0145 void FileHeader::generateLatinPreamble(QTextStream &out)
0146 {
0147     out << "%% Generated by Calligra Sheets. Don't modify this file but the file *.ods." << endl;
0148     out << "%% Send an email to rjacolin@ifrance.com for bugs, wishes, .... Thank you." << endl;
0149     out << "%% Compile this file with : latex filename.tex" << endl;
0150     out << "%% a dvi file will be generated." << endl;
0151     out << "%% The file uses the latex style (not the words style). " << endl;
0152     out << "\\documentclass[";
0153     switch (getFormat()) {
0154     case TF_A3:
0155         out << "";
0156         break;
0157     case TF_A4:
0158         out << "a4paper, ";
0159         break;
0160     case TF_A5:
0161         out << "a5paper, ";
0162         break;
0163     case TF_USLETTER:
0164         out << "letterpaper, ";
0165         break;
0166     case TF_USLEGAL:
0167         out << "legalpaper, ";
0168         break;
0169     case TF_SCREEN:
0170         out << "";
0171         break;
0172     case TF_CUSTOM:
0173         out << "";
0174         break;
0175     case TF_B3:
0176         out << "";
0177         break;
0178     case TF_USEXECUTIVE:
0179         out << "executivepaper, ";
0180         break;
0181     }
0182     if (getOrientation() == TO_LANDSCAPE)
0183         out << "landscape, ";
0184     /* To change : will use a special latexcommand to able to
0185      * obtain more than one column :))
0186      */
0187     switch (getColumns()) {
0188     case TC_1:
0189         //out << "onecolumn, ";
0190         break;
0191     case TC_2:
0192         out << "twocolumn, ";
0193         break;
0194     case TC_MORE:
0195         out << "";
0196         break;
0197     case TC_NONE:
0198         break;
0199     }
0200 
0201     out << Config::instance()->getDefaultFontSize() << "pt";
0202     if (Config::instance()->getQuality() == "draft")
0203         out << ", draft";
0204     out << "]{";
0205     out << Config::instance()->getClass() << "}" << endl;
0206     out << "\\usepackage[" << Config::instance()->getEncoding() << "]{inputenc}" << endl << endl;
0207 }
0208 
0209 /*******************************************/
0210 /* GenerateUnicodePreamble                 */
0211 /*******************************************/
0212 void FileHeader::generateUnicodePreamble(QTextStream &out)
0213 {
0214     out << "%% Generated by Calligra Sheets. Don't modify this file but the file *.ods." << endl;
0215     out << "%% Send an email to rjacolin@ifrance.com for bugs, wishes, .... Thank you." << endl;
0216     out << "%% Compile this file with : lambda filename.tex" << endl;
0217     out << "%% a dvi file will be generated." << endl;
0218     out << "%% Use odvips to convert it and to see it with gv" << endl;
0219     out << "%% The file uses the latex style (not the words style). " << endl;
0220     out << "\\ocp\\TexUTF=inutf8" << endl;
0221     out << "\\InputTranslation currentfile \\TexUTF" << endl;
0222     out << "\\documentclass[";
0223     switch (getFormat()) {
0224     case TF_A3:
0225         out << "";
0226         break;
0227     case TF_A4:
0228         out << "a4paper, ";
0229         break;
0230     case TF_A5:
0231         out << "a5paper, ";
0232         break;
0233     case TF_USLETTER:
0234         out << "letterpaper, ";
0235         break;
0236     case TF_USLEGAL:
0237         out << "legalpaper, ";
0238         break;
0239     case TF_SCREEN:
0240         out << "";
0241         break;
0242     case TF_CUSTOM:
0243         out << "";
0244         break;
0245     case TF_B3:
0246         out << "";
0247         break;
0248     case TF_USEXECUTIVE:
0249         out << "executivepaper, ";
0250         break;
0251     }
0252     if (getOrientation() == TO_LANDSCAPE)
0253         out << "landscape, ";
0254     /* To change : will use a special latexcommand to able to
0255      * obtain more than one column :))
0256      */
0257     switch (getColumns()) {
0258     case TC_1:
0259         //out << "onecolumn, ";
0260         break;
0261     case TC_2:
0262         out << "twocolumn, ";
0263         break;
0264     case TC_MORE:
0265         out << "";
0266         break;
0267     case TC_NONE:
0268         break;
0269     }
0270 
0271     out << Config::instance()->getDefaultFontSize() << "pt";
0272     if (Config::instance()->getQuality() == "draft")
0273         out << ", draft";
0274     out << "]{";
0275     out << Config::instance()->getClass() << "}" << endl;
0276 }
0277 
0278 
0279 /*******************************************/
0280 /* GeneratePackage                         */
0281 /*******************************************/
0282 void FileHeader::generatePackage(QTextStream &out)
0283 {
0284     out << "% Package(s) to include" << endl;
0285     if (Config::instance()->mustUseUnicode())
0286         out << "\\usepackage{omega}" << endl;
0287     if (getFormat() == TF_A4)
0288         out << "\\usepackage[a4paper]{geometry}" << endl;
0289     if (hasFooter() || hasHeader())
0290         out << "\\usepackage{fancyhdr}" << endl;
0291     if (hasColor())
0292         out << "\\usepackage{colortbl}" << endl;
0293     if (hasUnderline())
0294         out << "\\usepackage{ulem}" << endl;
0295     if (hasEnumerate())
0296         out << "\\usepackage{enumerate}" << endl;
0297     if (hasGraphics())
0298         out << "\\usepackage{graphics}" << endl;
0299     out << "\\usepackage{array}" << endl;
0300     out << "\\usepackage{multirow}" << endl;
0301     out << "\\usepackage{textcomp}" << endl;
0302     out << "\\usepackage{rotating}" << endl;
0303     out << endl;
0304     QStringList langs = Config::instance()->getLanguagesList();
0305     if (langs.count() > 0) {
0306         out << "\\usepackage[" << langs.join(", ") << "]{babel}" << endl;
0307     }
0308     out << "\\usepackage{textcomp}" << endl;
0309     out << endl;
0310 
0311     if (langs.count() > 1)
0312         out << "\\selectlanguage{" << Config::instance()->getDefaultLanguage()
0313         << "}" << endl << endl;
0314 }
0315 
0316 FileHeader* FileHeader::instance()
0317 {
0318     if (_instance == 0)
0319         _instance = new FileHeader();
0320     return _instance;
0321 }
0322