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) 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 "format.h"
0023 
0024 #include <stdlib.h>
0025 
0026 #include "LatexDebug.h"
0027 #include "column.h"
0028 #include "fileheader.h" /* for the use of _header (color and underlined) */
0029 #include "row.h"
0030 
0031 #include <QTextStream>
0032 
0033 Format::Format()
0034 {
0035     _multirow = -1;
0036     _brushStyle = -1;
0037     _isValidFormat = false;
0038     _bottomBorder = nullptr;
0039     _topBorder = nullptr;
0040     _rightBorder = nullptr;
0041     _leftBorder = nullptr;
0042 }
0043 
0044 Format::~Format()
0045 {
0046     delete _bottomBorder;
0047     delete _topBorder;
0048     delete _leftBorder;
0049     delete _rightBorder;
0050 }
0051 
0052 bool Format::hasTopBorder() const
0053 {
0054     if (_topBorder == nullptr)
0055         return false;
0056     else
0057         return (_topBorder->getStyle() > 0);
0058 }
0059 
0060 bool Format::hasBottomBorder() const
0061 {
0062     if (_bottomBorder == nullptr)
0063         return false;
0064     else
0065         return (_bottomBorder->getStyle() > 0);
0066 }
0067 
0068 bool Format::hasLeftBorder() const
0069 {
0070     if (_leftBorder == nullptr)
0071         return false;
0072     else
0073         return (_leftBorder->getStyle() > 0);
0074 }
0075 
0076 bool Format::hasRightBorder() const
0077 {
0078     if (_rightBorder == nullptr)
0079         return false;
0080     else
0081         return (_rightBorder->getStyle() > 0);
0082 }
0083 
0084 /* Get the set of info about a text format */
0085 void Format::analyze(const QDomNode node)
0086 {
0087     /* <format brushstyle="5" brushcolor="#a70bc3" bgcolor="#ffffff" alignY="2" align="4" > */
0088     if (!getAttr(node, "brushstyle").isEmpty()) {
0089         _isValidFormat = true;
0090         setBrushStyle(getAttr(node, "brushstyle").toInt());
0091         FileHeader::instance()->useColor();
0092         setBrushColor(getAttr(node, "brushcolor"));
0093         setBgColor(getAttr(node, "bgcolor"));
0094         setAlignY(getAttr(node, "alignY").toLong());
0095         setAlign(getAttr(node, "align").toLong());
0096     }
0097     if (isChild(node, "pen"))
0098         analyzePen(getChild(node, "pen"));
0099     if (isChild(node, "bottom-border")) {
0100         debugLatex << "bottom-border";
0101         _isValidFormat = true;
0102         _bottomBorder = new Pen();
0103         _bottomBorder->analyze(getChild(getChild(node, "bottom-border"), "pen"));
0104     }
0105     if (isChild(node, "top-border")) {
0106         debugLatex << "top-border";
0107         _isValidFormat = true;
0108         _topBorder = new Pen();
0109         _topBorder->analyze(getChild(getChild(node, "top-border"), "pen"));
0110     }
0111     if (isChild(node, "left-border")) {
0112         debugLatex << "left-border";
0113         _isValidFormat = true;
0114         _leftBorder = new Pen();
0115         _leftBorder->analyze(getChild(getChild(node, "left-border"), "pen"));
0116     }
0117     if (isChild(node, "right-border")) {
0118         debugLatex << "right-border";
0119         _isValidFormat = true;
0120         _rightBorder = new Pen();
0121         _rightBorder->analyze(getChild(getChild(node, "right-border"), "pen"));
0122     }
0123 }
0124 
0125 void Format::analyzePen(const QDomNode node)
0126 {
0127     /* <pen width="0" style="1" color="#000000" /> */
0128     _isValidFormat = true;
0129     setPenWidth(getAttr(node, "width").toDouble());
0130     setPenStyle(getAttr(node, "style").toInt());
0131     setPenColor(getAttr(node, "color"));
0132 }
0133 
0134 void Format::analyzeFont(const QDomNode node)
0135 {
0136     /* <font size="18" family="Helvetica" weight="50" /> */
0137     setFontSize(getAttr(node, "size").toInt());
0138     setFontFamily(getAttr(node, "family"));
0139     setFontWeight(getAttr(node, "weight").toInt());
0140 }
0141 
0142 void Format::generate(QTextStream& out, Column* col, Row* row)
0143 {
0144     if (hasLeftBorder())
0145         out << "|";
0146     if (isValidFormat() && getBrushStyle() >= 1) {
0147         out << ">{\\columncolor";
0148         generateColor(out);
0149         out << "}";
0150     } else if (col != nullptr) {
0151         if (col->getBrushStyle() >= 1) {
0152             out << ">{\\columncolor";
0153             col->generateColor(out);
0154             out << "}";
0155         }
0156     } else if (row != nullptr) {
0157         if (row->getBrushStyle() >= 1) {
0158             out << ">{\\columncolor";
0159             row->generateColor(out);
0160             out << "}";
0161         }
0162     }
0163     if (col != nullptr)
0164         out << "m{" << col->getWidth() << "pt}";
0165     if (hasRightBorder())
0166         out << "|";
0167 }
0168 
0169 void Format::generateTextFormat(QTextStream& out, QString text)
0170 {
0171     if (getPenStyle() > 0) {
0172         float red   = ((float) getPenColor().red()) / 255;
0173         float green = ((float) getPenColor().green()) / 255;
0174         float blue  = ((float) getPenColor().blue()) / 255;
0175 
0176         out << "\\textcolor[rgb]{" << red << ", " << green <<
0177         ", " << blue << "}{" << text << "}" << endl;
0178     }
0179 }
0180 
0181 void Format::generateColor(QTextStream& out)
0182 {
0183     if (getBrushStyle() >= 1) {
0184         float red   = ((float) getBrushColor().red()) / 255;
0185         float green = ((float) getBrushColor().green()) / 255;
0186         float blue  = ((float) getBrushColor().blue()) / 255;
0187 
0188         out << "[rgb]{" << red << ", " << green <<
0189         ", " << blue << "}%" << endl;
0190     }
0191 }