File indexing completed on 2025-03-09 04:41:48

0001 /*  This is RTF to HTML converter, implemented as a text filter, generally.
0002     Copyright (C) 2003 Valentin Lavrinenko, vlavrinenko@users.sourceforge.net
0003 
0004     available at http://rtf2html.sf.net
0005 
0006     Original available under the terms of the GNU LGPL2, and according
0007     to those terms, relicensed under the GNU GPL2 for inclusion in Tellico */
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or         *
0012  *   modify it under the terms of the GNU General Public License as        *
0013  *   published by the Free Software Foundation; either version 2 of        *
0014  *   the License or (at your option) version 3 or any later version        *
0015  *   accepted by the membership of KDE e.V. (or its successor approved     *
0016  *   by the membership of KDE e.V.), which shall act as a proxy            *
0017  *   defined in Section 14 of version 3 of the license.                    *
0018  *                                                                         *
0019  *   This program is distributed in the hope that it will be useful,       *
0020  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0021  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0022  *   GNU General Public License for more details.                          *
0023  *                                                                         *
0024  *   You should have received a copy of the GNU General Public License     *
0025  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0026  *                                                                         *
0027  ***************************************************************************/
0028 
0029 #include "fmt_opts.h"
0030 
0031 //krazy:excludeall=doublequote_chars
0032 
0033 using namespace rtf;
0034 
0035 std::string formatting_options::get_par_str() const
0036 {
0037    std::string style;
0038    switch (papAlign)
0039    {
0040    case formatting_options::align_right:
0041       style+="text-align:right;";
0042       break;
0043    case formatting_options::align_center:
0044       style+="text-align:center;";
0045       break;
0046    case formatting_options::align_justify:
0047       style+="text-align:justify;";
0048    default: break;
0049    }
0050    if (papFirst!=0)
0051    {
0052       style+="text-indent:";
0053       style+=from_int(papFirst);
0054       style+="pt;";
0055    }
0056    if (papLeft!=0)
0057    {
0058       style+="margin-left:";
0059       style+=from_int(papLeft);
0060       style+="pt;";
0061    }
0062    if (papRight!=0)
0063    {
0064       style+="margin-right:";
0065       style+=from_int(papRight);
0066       style+="pt;";
0067    }
0068    if (papBefore!=0)
0069    {
0070       style+="margin-top:";
0071       style+=from_int(papBefore);
0072       style+="pt;";
0073    }
0074    if (papAfter!=0)
0075    {
0076       style+="margin-bottom:";
0077       style+=from_int(papAfter);
0078       style+="pt;";
0079    }
0080    if (style.empty())
0081       return std::string("<p>");
0082    else
0083    {
0084       style.insert(0, "<p style=\"");
0085       return style+"\">";
0086    }
0087 }
0088 
0089 std::string formatter::format(const formatting_options &_opt)
0090 {
0091    formatting_options last_opt, opt(_opt);
0092    std::string result;
0093    if (!opt_stack.empty())
0094    {
0095       int cnt=0;
0096       fo_deque::reverse_iterator i;
0097       for (i=opt_stack.rbegin(); i!=opt_stack.rend(); ++i)
0098       {
0099          if (*i==opt)
0100             break;
0101          ++cnt;
0102       }
0103       if (cnt==0)
0104          return "";
0105       if (i!=opt_stack.rend())
0106       {
0107          while (cnt--)
0108          {
0109             result+="</span>";
0110             opt_stack.pop_back();
0111          }
0112          return result;
0113       }
0114       last_opt=opt_stack.back();
0115    }
0116    if (last_opt.chpVAlign!=formatting_options::va_normal
0117        && last_opt.chpVAlign!=opt.chpVAlign)
0118    {
0119       int cnt=0;
0120       fo_deque::reverse_iterator i;
0121       for (i=opt_stack.rbegin(); i!=opt_stack.rend(); ++i)
0122       {
0123          if (i->chpVAlign==formatting_options::va_normal)
0124             break;
0125          ++cnt;
0126       }
0127       while (cnt--)
0128       {
0129          result+="</span>";
0130          opt_stack.pop_back();
0131       }
0132       last_opt=opt_stack.empty()?formatting_options():opt_stack.back();
0133    }
0134    std::string style;
0135    if (opt.chpBold!=last_opt.chpBold)
0136    {
0137       style+="font-weight:";
0138       style+=opt.chpBold?"bold":"normal";
0139       style+=";";
0140    }
0141    if (opt.chpItalic!=last_opt.chpItalic)
0142    {
0143       style+="font-style:";
0144       style+=opt.chpItalic?"italic":"normal";
0145       style+=";";
0146    }
0147    if (opt.chpUnderline!=last_opt.chpUnderline)
0148    {
0149       style+="text-decoration:";
0150       style+=opt.chpUnderline?"underline":"none";
0151       style+=";";
0152    }
0153    if (opt.chpVAlign!=formatting_options::va_normal)
0154       opt.chpFontSize=(int)(0.7*(opt.chpFontSize?opt.chpFontSize:24));
0155    if (opt.chpFontSize!=last_opt.chpFontSize)
0156    {
0157       style+="font-size:";
0158       style+=from_int(opt.chpFontSize/2);
0159       style+="pt;";
0160    }
0161    if (opt.chpVAlign!=last_opt.chpVAlign)
0162    {
0163       style+="vertical-align:";
0164       style+=opt.chpVAlign==formatting_options::va_sub?"sub":"super";
0165       style+=";";
0166    }
0167    if (opt.chpFColor!=last_opt.chpFColor)
0168    {
0169       style+="color:";
0170       style+=opt.chpFColor.r>0?"#"+hex(opt.chpFColor.r&0xFF)
0171                                   +hex(opt.chpFColor.g&0xFF)
0172                                   +hex(opt.chpFColor.b&0xFF)
0173                               :"WindowText";
0174       style+=";";
0175    }
0176    if (opt.chpBColor!=last_opt.chpBColor)
0177    {
0178       style+="background-color:";
0179       style+=opt.chpBColor.r>0?"#"+hex(opt.chpBColor.r&0xFF)
0180                                   +hex(opt.chpBColor.g&0xFF)
0181                                   +hex(opt.chpBColor.b&0xFF)
0182                               :"Window";
0183       style+=";";
0184    }
0185    if (opt.chpHighlight!=last_opt.chpHighlight)
0186    {
0187       style+="background-color:";
0188       switch (opt.chpHighlight)
0189       {
0190       case 0: style+="Window"; break;
0191       case 1: style+="black"; break;
0192       case 2: style+="blue"; break;
0193       case 3: style+="aqua"; break;
0194       case 4: style+="lime"; break;
0195       case 5: style+="fuchsia"; break;
0196       case 6: style+="red"; break;
0197       case 7: style+="yellow"; break;
0198       case 9: style+="navy"; break;
0199       case 10: style+="teal"; break;
0200       case 11: style+="green"; break;
0201       case 12: style+="purple"; break;
0202       case 13: style+="maroon"; break;
0203       case 14: style+="olive"; break;
0204       case 15: style+="gray"; break;
0205       case 16: style+="silver"; break;
0206       }
0207       style+=";";
0208    }
0209    if (opt.chpFont!=last_opt.chpFont)
0210    {
0211       style+="font-family:'";
0212       style+=opt.chpFont.name.empty()?"serif":opt.chpFont.name;
0213       style+="'";
0214       switch (opt.chpFont.family)
0215       {
0216       case font::ff_serif: style+=", serif"; break;
0217       case font::ff_sans_serif: style+=", sans-serif"; break;
0218       case font::ff_cursive: style+=", cursive"; break;
0219       case font::ff_fantasy: style+=", fantasy"; break;
0220       case font::ff_monospace: style+=", monospace"; break;
0221       default: break;
0222       }
0223       style+=";";
0224    }
0225    opt_stack.push_back(opt);
0226    return result+"<span style=\""+style+"\">";
0227 }
0228 
0229 std::string formatter::close()
0230 {
0231    std::string result;
0232    for (fo_deque::iterator i=opt_stack.begin(); i!=opt_stack.end(); ++i)
0233       result+="</span>";
0234    return result;
0235 }