"Fossies" - the Fresh Open Source Software Archive

Member "ansifilter-2.18/src/latexgenerator.cpp" (30 Jan 2021, 4973 Bytes) of package /linux/privat/ansifilter-2.18.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "latexgenerator.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.17_vs_2.18.

    1 /***************************************************************************
    2                      texgenerator.cpp  -  description
    3                              -------------------
    4 
    5     copyright            : (C) 2008 by Hans Meine
    6     email                : hans_meine@gmx.net
    7  ***************************************************************************/
    8 
    9 /*
   10 This file is part of ANSIFilter.
   11 
   12 ANSIFilter is free software: you can redistribute it and/or modify
   13 it under the terms of the GNU General Public License as published by
   14 the Free Software Foundation, either version 3 of the License, or
   15 (at your option) any later version.
   16 
   17 ANSIFilter is distributed in the hope that it will be useful,
   18 but WITHOUT ANY WARRANTY; without even the implied warranty of
   19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   20 GNU General Public License for more details.
   21 
   22 You should have received a copy of the GNU General Public License
   23 along with ANSIFilter.  If not, see <http://www.gnu.org/licenses/>.
   24 */
   25 
   26 #include <fstream>
   27 #include <iostream>
   28 #include <sstream>
   29 
   30 #include "latexgenerator.h"
   31 #include "version.h"
   32 
   33 namespace ansifilter
   34 {
   35 
   36 LaTeXGenerator::LaTeXGenerator ():
   37     CodeGenerator(LATEX),
   38     fileSuffix(".tex")
   39 {
   40     newLineTag="\\hspace*{\\fill}\\\\\n"; //avoid errors with empty input lines
   41     styleCommentOpen="/*";
   42     styleCommentClose="*/";
   43     spacer="\\ws{\\ }";
   44 }
   45 
   46 string LaTeXGenerator::getOpenTag()
   47 {
   48 
   49     ostringstream fmtStream;
   50 
   51 
   52     if (elementStyle.isBold()) {
   53         fmtStream<< "\\bfseries{}";
   54     }
   55     if (elementStyle.isItalic()) {
   56         fmtStream<< "\\itshape{}";
   57     }
   58 
   59     if (elementStyle.isFgColorSet()) {
   60         fmtStream << "\\color[rgb]{"
   61                   << elementStyle.getFgColour().getRed(LATEX) << ","
   62                   << elementStyle.getFgColour().getGreen(LATEX) << ","
   63                   << elementStyle.getFgColour().getBlue(LATEX)
   64                   << "}";
   65     }
   66 
   67     return "{" + fmtStream.str();
   68 }
   69 
   70 string LaTeXGenerator::getCloseTag()
   71 {
   72     return "}";
   73 }
   74 
   75 string LaTeXGenerator::getHyperlink(string uri, string txt){
   76     ostringstream os;
   77     os <<"\\href{"<<uri<<"}{"<<txt<<"}";
   78     return os.str();
   79 }
   80 
   81 string LaTeXGenerator::getGeneratorComment()
   82 {
   83     ostringstream s;
   84     s <<"% LaTeX generated by ansifilter "
   85       << ANSIFILTER_VERSION << ", " <<  ANSIFILTER_URL <<"\n";
   86 
   87     return s.str();
   88 }
   89 
   90 string LaTeXGenerator::getHeader()
   91 {
   92     ostringstream os;
   93     os << "\\documentclass{article}\n";
   94     os << "\\usepackage{color}\n";
   95     os << "\\usepackage{hyperref}\n";
   96 
   97     os << "\\newcommand{\\ws}[1]{\\textcolor[rgb]{0,0,0}{#1}}\n";
   98     if (encodingDefined()) {
   99         os << "%\\usepackage[" << encoding << "]{inputenc}\n";
  100     }
  101     if (!styleSheetPath.empty()) {
  102         os <<  "\\input {"<<styleSheetPath<<"}\n";
  103     }
  104     os << "\\begin{document}\n";
  105     os << "\\section*{" << docTitle << "}\n";
  106     os << "\\ttfamily%\n";
  107     return os.str();
  108 }
  109 
  110 string LaTeXGenerator::getFooter()
  111 {
  112     string footer;
  113     footer = "\\end{document}\n";
  114 
  115     if (!omitVersionInfo)
  116         footer += getGeneratorComment();
  117 
  118     return footer;
  119 }
  120 
  121 void LaTeXGenerator::printBody()
  122 {
  123     processInput();
  124 }
  125 
  126 string LaTeXGenerator::maskCharacter(unsigned char c)
  127 {
  128 
  129     switch (c) {
  130 
  131     case ' ':
  132         return spacer;
  133         break;
  134     case '<' :
  135         return "$<$";
  136         break;
  137     case '>' :
  138         return "$>$";
  139         break;
  140     case '{':
  141     case '}':
  142     case '&':
  143     case '$':
  144     case '#':
  145     case '%': {
  146         string m( "\\" );
  147         m += c;
  148         return m;
  149     }
  150     break;
  151     // case '\"':
  152     //  return (fragmentOutput && replaceQuotes)?"\\dq{}":"\"";
  153     //   break;
  154     case '_':
  155         return "\\textunderscore ";
  156         break;
  157     case '^':
  158         return "\\textasciicircum ";
  159         break;
  160     case '\\':
  161         return "$\\backslash$";
  162         break;
  163     case '~':
  164         return "$\\sim$";
  165         break;
  166     case '|':
  167         return "\\textbar ";
  168         break;
  169     // avoid latex compilation failure if [ or * follows a line break (\\)
  170     case '*':
  171     case '[':
  172     case ']':
  173     // avoid "merging" of consecutive '-' chars when included in bold font ( \bf )
  174     case '-': {
  175         string m( 1, '{' );
  176         m += c;
  177         m += '}';
  178         return m;
  179     }
  180     break;
  181 
  182     case '\t' : // see deletion of nonprintable chars below
  183         return "\t";
  184         break;
  185 
  186     default :
  187         if (c>0x1f ) { // printable?
  188             return string( 1, c );
  189         } else {
  190             return "";
  191         }
  192     }
  193 
  194 }
  195 
  196 void LaTeXGenerator::insertLineNumber ()
  197 {
  198     if ( showLineNumbers ) {
  199 
  200         ostringstream lnum;
  201         lnum << setw ( 5 ) << right;
  202         if( numberCurrentLine ) {
  203             if (lineNumber>1)
  204               *out << getCloseTag();
  205             lnum << lineNumber;
  206             *out <<"{\\color[rgb]{0,0,0} "<<lnum.str()<<"}"<<spacer;
  207             *out << getOpenTag();
  208         } else {
  209             *out << lnum.str(); //for indentation
  210         }
  211     }
  212 }
  213 
  214 }