"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/latexgenerator.cpp" (12 May 2020, 10075 Bytes) of package /windows/www/highlight-3.57-x64.zip:


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. See also the last Fossies "Diffs" side-by-side code changes report for "latexgenerator.cpp": 3.52_vs_3.53.

    1 /***************************************************************************
    2                           LatexCode.cpp  -  description
    3                              -------------------
    4     begin                : Mit Jul 24 2002
    5     copyright            : (C) 2002 by Andre Simon
    6     email                : a.simon@mailbox.org
    7  ***************************************************************************/
    8 
    9 
   10 /*
   11 This file is part of Highlight.
   12 
   13 Highlight is free software: you can redistribute it and/or modify
   14 it under the terms of the GNU General Public License as published by
   15 the Free Software Foundation, either version 3 of the License, or
   16 (at your option) any later version.
   17 
   18 Highlight is distributed in the hope that it will be useful,
   19 but WITHOUT ANY WARRANTY; without even the implied warranty of
   20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   21 GNU General Public License for more details.
   22 
   23 You should have received a copy of the GNU General Public License
   24 along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
   25 */
   26 
   27 
   28 #include "latexgenerator.h"
   29 
   30 namespace highlight
   31 {
   32 
   33 LatexGenerator::LatexGenerator()
   34     : CodeGenerator ( LATEX ),
   35       replaceQuotes ( false ),
   36       disableBabelShortHand ( false ),
   37       prettySymbols ( false ),
   38       beamerMode ( false )
   39 {
   40     // avoid "Underfull \hbox (badness 10000)" warnings
   41     newLineTag = "\\\\\n";
   42     longLineTag = "\\hspace*{\\fill}" + newLineTag;
   43     spacer = "\\ ";
   44     maskWs=true;
   45     maskWsBegin = "\\hlstd{";
   46     maskWsEnd = "}";
   47     excludeWs=true;
   48     styleCommentOpen="%";
   49 }
   50 
   51 LatexGenerator::~LatexGenerator()
   52 {}
   53 
   54 void LatexGenerator::printBody()
   55 {
   56    // if (!beamerMode)
   57         *out << "\\noindent\n" ;
   58     if ( ! this->getBaseFont().empty() )
   59         *out << "\\" << this->getBaseFont() << "\n" ;
   60     else
   61         *out << "\\ttfamily\n";
   62     if ( ! this->getBaseFontSize().empty() && this->getBaseFontSize()!="10")
   63         *out << "\\" << this->getBaseFontSize() << "\n" ;
   64     if ( disableBabelShortHand )
   65         *out << "\\shorthandoff{\"}\n";
   66 
   67     processRootState();
   68 
   69     *out << "\\mbox{}\n"
   70          << "\\normalfont\n";
   71     if ( ! this->getBaseFontSize().empty() )
   72         *out << "\\normalsize\n" ;
   73     if ( disableBabelShortHand )
   74         *out << "\\shorthandon{\"}\n";
   75 }
   76 
   77 string LatexGenerator::getHeader()
   78 {
   79     ostringstream os;
   80 
   81     os << (beamerMode ? "\\documentclass{beamer}\n" : "\\documentclass{article}\n")
   82        << "\\usepackage{color}\n"
   83        << "\\usepackage{alltt}\n"
   84        << "\\usepackage[T1]{fontenc}\n";
   85        
   86     //if (beamerMode)
   87     //    os  << "\\usepackage{beamerarticle}\n";
   88     
   89     if ( StringTools::change_case ( encoding ) == "utf-8" ) {
   90         os << "\\usepackage{ucs}\n\\usepackage[utf8x]{inputenc}\n";
   91     } else if ( encodingDefined() ) {
   92         os << "\\usepackage[latin1]{inputenc}\n";
   93     }
   94 
   95     //needed for Righttorque symbol
   96     if ( preFormatter.isEnabled() ) {
   97         os << "\\usepackage{marvosym}\n";
   98     }
   99 
  100     if ( currentSyntax->highlightingEnabled() ) {
  101         if ( includeStyleDef ) {
  102             os << "\n"<<getStyleDefinition();
  103             os << CodeGenerator::readUserStyleDef();
  104         } else {
  105             os << "\n\\input {"
  106                << getStyleOutputPath()
  107                << "}\n";
  108         }
  109     }
  110 
  111     os << "\n\\title{" << docTitle << "}\n"
  112        << "\\begin{document}\n"
  113        << "\\pagecolor{bgcolor}\n";
  114        
  115     if (beamerMode)
  116       os  << "\\frame{\\frametitle{Code section}\n";
  117 
  118     if ( prettySymbols ) {
  119         os<<"\\newsavebox{\\hlboxopenbrace}\n"
  120           <<"\\newsavebox{\\hlboxclosebrace}\n"
  121           <<"\\newsavebox{\\hlboxlessthan}\n"
  122           <<"\\newsavebox{\\hlboxgreaterthan}\n"
  123           <<"\\newsavebox{\\hlboxdollar}\n"
  124           <<"\\newsavebox{\\hlboxunderscore}\n"
  125           <<"\\newsavebox{\\hlboxand}\n"
  126           <<"\\newsavebox{\\hlboxhash}\n"
  127           <<"\\newsavebox{\\hlboxat}\n"
  128           <<"\\newsavebox{\\hlboxbackslash}\n"
  129           <<"\\newsavebox{\\hlboxpercent}\n"
  130           <<"\\newsavebox{\\hlboxhat}\n"
  131 
  132           <<"\\setbox\\hlboxopenbrace=\\hbox{\\verb.{.}\n"
  133           <<"\\setbox\\hlboxclosebrace=\\hbox{\\verb.}.}\n"
  134           <<"\\setbox\\hlboxlessthan=\\hbox{\\verb.<.}\n"
  135           <<"\\setbox\\hlboxgreaterthan=\\hbox{\\verb.>.}\n"
  136 
  137           <<"\\setbox\\hlboxdollar=\\hbox{\\verb.$.}\n"
  138           <<"\\setbox\\hlboxunderscore=\\hbox{\\verb._.}\n"
  139           <<"\\setbox\\hlboxand=\\hbox{\\verb.&.}\n"
  140           <<"\\setbox\\hlboxhash=\\hbox{\\verb.#.}\n"
  141           <<"\\setbox\\hlboxat=\\hbox{\\verb.@.}\n"
  142           <<"\\setbox\\hlboxbackslash=\\hbox{\\verb.\\.}\n"
  143           <<"\\setbox\\hlboxpercent=\\hbox{\\verb.\\%.}\n"
  144           <<"\\setbox\\hlboxhat=\\hbox{\\verb.^.}\n"
  145 
  146           <<"\\def\\urltilda{\\kern -.15em\\lower .7ex\\hbox{\\~{}}\\kern .04em}\n";
  147     }
  148 
  149     return os.str();
  150 }
  151 
  152 string LatexGenerator::getFooter()
  153 {
  154     ostringstream os;
  155     
  156     if (beamerMode)
  157       os  << "}\n";
  158 
  159     os << "\\end {document}\n";
  160     
  161     if (!omitVersionComment) {
  162      os  << "(* LaTeX generated by highlight "
  163        << HIGHLIGHT_VERSION
  164        << ", "
  165        << HIGHLIGHT_URL
  166        << " *)\n";
  167     }
  168     return os.str();
  169 }
  170 
  171 
  172 void LatexGenerator::initOutputTags()
  173 {
  174 
  175     openTags.push_back ( "\\hl"+STY_NAME_STD+"{" );
  176     openTags.push_back ( "\\hl"+STY_NAME_STR+"{" );
  177     openTags.push_back ( "\\hl"+STY_NAME_NUM+"{" );
  178     openTags.push_back ( "\\hl"+STY_NAME_SLC+"{" );
  179     openTags.push_back ( "\\hl"+STY_NAME_COM+"{" );
  180     openTags.push_back ( "\\hl"+STY_NAME_ESC+"{" );
  181     openTags.push_back ( "\\hl"+STY_NAME_DIR+"{" );
  182     openTags.push_back ( "\\hl"+STY_NAME_DST+"{" );
  183     openTags.push_back ( "\\hl"+STY_NAME_LIN+"{" );
  184     openTags.push_back ( "\\hl"+STY_NAME_SYM+"{" );
  185     openTags.push_back ( "\\hl"+STY_NAME_IPL+"{" );
  186 
  187     for (unsigned int i=0; i<NUMBER_BUILTIN_STATES; i++ ) {
  188         closeTags.push_back ( "}" );
  189     }
  190 }
  191 
  192 string LatexGenerator::getAttributes ( const string & elemName,
  193                                        const ElementStyle &elem )
  194 {
  195     ostringstream s;
  196     s  << "\\newcommand{\\hl"
  197        << elemName
  198        << "}[1]{\\textcolor[rgb]{"
  199        << elem.getColour().getRed ( LATEX ) << ","
  200        << elem.getColour().getGreen ( LATEX ) << ","
  201        << elem.getColour().getBlue ( LATEX )
  202        << "}{";
  203 
  204     if ( elem.isBold() )
  205         s << "\\bf{";
  206     if ( elem.isItalic() )
  207         s << "\\it{";
  208 
  209     s  <<"#1";
  210 
  211     if ( elem.isBold() )
  212         s << "}";
  213     if ( elem.isItalic() )
  214         s << "}";
  215 
  216     s  <<"}}\n";
  217     return s.str();
  218 }
  219 
  220 
  221 string LatexGenerator::getNewLine()
  222 {
  223     string nl;
  224 
  225     // set wrapping arrow if previous line was wrapped
  226     if ( preFormatter.isWrappedLine ( lineNumber-1 ) ) {
  227         nl = "\\Righttorque";
  228     }
  229     nl += ( showLineNumbers ) ? newLineTag:longLineTag;
  230     return nl;
  231 }
  232 
  233 string LatexGenerator::maskCharacter ( unsigned char c )
  234 {
  235     switch ( c ) {
  236     case ' ':
  237         return spacer;
  238         break;
  239 
  240     case '<' :
  241         return prettySymbols ? "\\usebox{\\hlboxlessthan}" : "$<$";
  242         break;
  243     case '>' :
  244         return prettySymbols ? "\\usebox{\\hlboxgreaterthan}" : "$>$";
  245         break;
  246     case '{':
  247         return prettySymbols ? "\\usebox{\\hlboxopenbrace}" : "\\{";
  248         break;
  249     case '}':
  250         return prettySymbols ? "\\usebox{\\hlboxclosebrace}" : "\\}";
  251         break;
  252 
  253     case '&':
  254     case '$':
  255     case '#':
  256     case '%': {
  257         string m ( "\\" );
  258         m += c;
  259         return m;
  260     }
  261     break;
  262     case '\"':
  263         return ( replaceQuotes ) ?"\\dq{}":"\"";
  264         break;
  265     case '_':
  266         return "\\textunderscore ";
  267         break;
  268     case '^':
  269         return "\\textasciicircum ";
  270         break;
  271     case '\\':
  272         return "$\\backslash$";
  273         break;
  274     case '~':
  275         return prettySymbols ? "\\urltilda " : "$\\sim$";
  276         break;
  277     case '|':
  278         return "\\textbar ";
  279         break;
  280     // avoid latex compilation failure if [ or * follows a line break (\\)
  281     case '*':
  282     case '[':
  283     case ']':
  284     // avoid "merging" of consecutive '-' chars when included in bold font ( \bf )
  285     case '-': {
  286         string m ( 1, '{' );
  287         m += c;
  288         m += '}';
  289         return m;
  290     }
  291     break;
  292     default :
  293         return string ( 1, c );
  294     }
  295 }
  296 
  297 string LatexGenerator::getKeywordOpenTag ( unsigned int styleID )
  298 {
  299     return "\\hl"+currentSyntax->getKeywordClasses() [styleID]+"{";
  300 }
  301 
  302 string LatexGenerator::getKeywordCloseTag ( unsigned int styleID )
  303 {
  304     return "}";
  305 }
  306 
  307 string LatexGenerator::getStyleDefinition()
  308 {
  309     if ( disableStyleCache || styleDefinitionCache.empty() ) {
  310         ostringstream os;
  311         
  312         os << "% highlight theme: "<<docStyle.getDescription()<<"\n";
  313 
  314         os << getAttributes ( STY_NAME_STD, docStyle.getDefaultStyle() );
  315         os << getAttributes ( STY_NAME_NUM, docStyle.getNumberStyle() );
  316         os << getAttributes ( STY_NAME_ESC, docStyle.getEscapeCharStyle() );
  317         os << getAttributes ( STY_NAME_STR, docStyle.getStringStyle() );
  318         os << getAttributes ( STY_NAME_DST, docStyle.getPreProcStringStyle() );
  319         os << getAttributes ( STY_NAME_SLC, docStyle.getSingleLineCommentStyle() );
  320         os << getAttributes ( STY_NAME_COM, docStyle.getCommentStyle() );
  321         os << getAttributes ( STY_NAME_DIR, docStyle.getPreProcessorStyle() );
  322         os << getAttributes ( STY_NAME_SYM, docStyle.getOperatorStyle() );
  323         os << getAttributes ( STY_NAME_IPL, docStyle.getInterpolationStyle() );
  324         os << getAttributes ( STY_NAME_LIN, docStyle.getLineStyle() );
  325 
  326         KeywordStyles styles = docStyle.getKeywordStyles();
  327         for ( KSIterator it=styles.begin(); it!=styles.end(); it++ ) {
  328             os << getAttributes ( it->first, it->second );
  329         }
  330         os << "\\definecolor{bgcolor}{rgb}{"
  331            << docStyle.getBgColour().getRed ( LATEX ) << ","
  332            << docStyle.getBgColour().getGreen ( LATEX ) << ","
  333            << docStyle.getBgColour().getBlue ( LATEX )
  334            << "}\n";
  335 
  336         styleDefinitionCache=os.str();
  337     }
  338     return styleDefinitionCache;
  339 }
  340 
  341 }