File indexing completed on 2025-03-16 10:52:55

0001 /*
0002 ** A program to convert the XML rendered by Words into LATEX.
0003 **
0004 ** SPDX-FileCopyrightText: 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     out << "% Format of paper" << endl;
0127     debugLatex << "Generate custom size paper";
0128     /* paper size */
0129     out << "\\setlength{\\paperwidth}{"  << _width  << "pt}" << endl;
0130     out << "\\setlength{\\paperheight}{" << _height << "pt}" << endl;
0131     /* FileHeader and footer */
0132     out << "\\setlength{\\headsep}{" << _headBody << "pt}" << endl;
0133     out << "\\setlength{\\footskip}{" << _footBody + _bottomBorder << "pt}" << endl;
0134     /* Margin */
0135     out << "\\setlength{\\topmargin}{" << _topBorder << "pt}" << endl;
0136     out << "\\setlength{\\textwidth}{" << _width - _rightBorder - _leftBorder << "pt}" << endl;
0137     out << endl;
0138 }
0139 
0140 /*******************************************/
0141 /* GenerateLatinPreamble                   */
0142 /*******************************************/
0143 void FileHeader::generateLatinPreamble(QTextStream &out)
0144 {
0145     out << "%% Generated by Calligra Sheets. Don't modify this file but the file *.ods." << endl;
0146     out << "%% Send an email to rjacolin@ifrance.com for bugs, wishes, .... Thank you." << endl;
0147     out << "%% Compile this file with : latex filename.tex" << endl;
0148     out << "%% a dvi file will be generated." << endl;
0149     out << "%% The file uses the latex style (not the words style). " << endl;
0150     out << "\\documentclass[";
0151     switch (getFormat()) {
0152     case TF_A3:
0153         out << "";
0154         break;
0155     case TF_A4:
0156         out << "a4paper, ";
0157         break;
0158     case TF_A5:
0159         out << "a5paper, ";
0160         break;
0161     case TF_USLETTER:
0162         out << "letterpaper, ";
0163         break;
0164     case TF_USLEGAL:
0165         out << "legalpaper, ";
0166         break;
0167     case TF_SCREEN:
0168         out << "";
0169         break;
0170     case TF_CUSTOM:
0171         out << "";
0172         break;
0173     case TF_B3:
0174         out << "";
0175         break;
0176     case TF_USEXECUTIVE:
0177         out << "executivepaper, ";
0178         break;
0179     }
0180     if (getOrientation() == TO_LANDSCAPE)
0181         out << "landscape, ";
0182     /* To change : will use a special latexcommand to able to
0183      * obtain more than one column :))
0184      */
0185     switch (getColumns()) {
0186     case TC_1:
0187         //out << "onecolumn, ";
0188         break;
0189     case TC_2:
0190         out << "twocolumn, ";
0191         break;
0192     case TC_MORE:
0193         out << "";
0194         break;
0195     case TC_NONE:
0196         break;
0197     }
0198 
0199     out << Config::instance()->getDefaultFontSize() << "pt";
0200     if (Config::instance()->getQuality() == "draft")
0201         out << ", draft";
0202     out << "]{";
0203     out << Config::instance()->getClass() << "}" << endl;
0204     out << "\\usepackage[" << Config::instance()->getEncoding() << "]{inputenc}" << endl << endl;
0205 }
0206 
0207 /*******************************************/
0208 /* GenerateUnicodePreamble                 */
0209 /*******************************************/
0210 void FileHeader::generateUnicodePreamble(QTextStream &out)
0211 {
0212     out << "%% Generated by Calligra Sheets. Don't modify this file but the file *.ods." << endl;
0213     out << "%% Send an email to rjacolin@ifrance.com for bugs, wishes, .... Thank you." << endl;
0214     out << "%% Compile this file with : lambda filename.tex" << endl;
0215     out << "%% a dvi file will be generated." << endl;
0216     out << "%% Use odvips to convert it and to see it with gv" << endl;
0217     out << "%% The file uses the latex style (not the words style). " << endl;
0218     out << "\\ocp\\TexUTF=inutf8" << endl;
0219     out << "\\InputTranslation currentfile \\TexUTF" << endl;
0220     out << "\\documentclass[";
0221     switch (getFormat()) {
0222     case TF_A3:
0223         out << "";
0224         break;
0225     case TF_A4:
0226         out << "a4paper, ";
0227         break;
0228     case TF_A5:
0229         out << "a5paper, ";
0230         break;
0231     case TF_USLETTER:
0232         out << "letterpaper, ";
0233         break;
0234     case TF_USLEGAL:
0235         out << "legalpaper, ";
0236         break;
0237     case TF_SCREEN:
0238         out << "";
0239         break;
0240     case TF_CUSTOM:
0241         out << "";
0242         break;
0243     case TF_B3:
0244         out << "";
0245         break;
0246     case TF_USEXECUTIVE:
0247         out << "executivepaper, ";
0248         break;
0249     }
0250     if (getOrientation() == TO_LANDSCAPE)
0251         out << "landscape, ";
0252     /* To change : will use a special latexcommand to able to
0253      * obtain more than one column :))
0254      */
0255     switch (getColumns()) {
0256     case TC_1:
0257         //out << "onecolumn, ";
0258         break;
0259     case TC_2:
0260         out << "twocolumn, ";
0261         break;
0262     case TC_MORE:
0263         out << "";
0264         break;
0265     case TC_NONE:
0266         break;
0267     }
0268 
0269     out << Config::instance()->getDefaultFontSize() << "pt";
0270     if (Config::instance()->getQuality() == "draft")
0271         out << ", draft";
0272     out << "]{";
0273     out << Config::instance()->getClass() << "}" << endl;
0274 }
0275 
0276 
0277 /*******************************************/
0278 /* GeneratePackage                         */
0279 /*******************************************/
0280 void FileHeader::generatePackage(QTextStream &out)
0281 {
0282     out << "% Package(s) to include" << endl;
0283     if (Config::instance()->mustUseUnicode())
0284         out << "\\usepackage{omega}" << endl;
0285     if (getFormat() == TF_A4)
0286         out << "\\usepackage[a4paper]{geometry}" << endl;
0287     if (hasFooter() || hasHeader())
0288         out << "\\usepackage{fancyhdr}" << endl;
0289     if (hasColor())
0290         out << "\\usepackage{colortbl}" << endl;
0291     if (hasUnderline())
0292         out << "\\usepackage{ulem}" << endl;
0293     if (hasEnumerate())
0294         out << "\\usepackage{enumerate}" << endl;
0295     if (hasGraphics())
0296         out << "\\usepackage{graphics}" << endl;
0297     out << "\\usepackage{array}" << endl;
0298     out << "\\usepackage{multirow}" << endl;
0299     out << "\\usepackage{textcomp}" << endl;
0300     out << "\\usepackage{rotating}" << endl;
0301     out << endl;
0302     QStringList langs = Config::instance()->getLanguagesList();
0303     if (langs.count() > 0) {
0304         out << "\\usepackage[" << langs.join(", ") << "]{babel}" << endl;
0305     }
0306     out << "\\usepackage{textcomp}" << endl;
0307     out << endl;
0308 
0309     if (langs.count() > 1)
0310         out << "\\selectlanguage{" << Config::instance()->getDefaultLanguage()
0311         << "}" << endl << endl;
0312 }
0313 
0314 FileHeader* FileHeader::instance()
0315 {
0316     if (_instance == 0)
0317         _instance = new FileHeader();
0318     return _instance;
0319 }
0320