"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/texgenerator.cpp" (12 May 2020, 7760 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 "texgenerator.cpp": 3.52_vs_3.53.

    1 /***************************************************************************
    2                           TexGenerator.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 <sstream>
   29 
   30 #include "charcodes.h"
   31 #include "version.h"
   32 #include "texgenerator.h"
   33 
   34 namespace highlight
   35 {
   36 
   37 TexGenerator::TexGenerator()
   38     : CodeGenerator ( TEX )
   39 {
   40     /*This makes TeX to use every par it encounters (the \\leavevmode has
   41     no effect when TeX is in horizontal mode and when TeX is in vertical
   42     mode, it switches it to horizontal mode).*/
   43     newLineTag="\\leavevmode\\par\n";
   44 
   45     spacer = "\\ ";
   46     maskWs=true;
   47     excludeWs=true;
   48     maskWsBegin = "{\\hlstd";
   49     maskWsEnd = "}";
   50     styleCommentOpen="%";
   51 }
   52 
   53 TexGenerator::~TexGenerator()
   54 {}
   55 
   56 void TexGenerator::initOutputTags()
   57 {
   58     openTags.push_back ( "{\\hl"+STY_NAME_STD+" " );
   59     openTags.push_back ( "{\\hl"+STY_NAME_STR+" " );
   60     openTags.push_back ( "{\\hl"+STY_NAME_NUM+" " );
   61     openTags.push_back ( "{\\hl"+STY_NAME_SLC+" " );
   62     openTags.push_back ( "{\\hl"+STY_NAME_COM+" " );
   63     openTags.push_back ( "{\\hl"+STY_NAME_ESC+" " );
   64     openTags.push_back ( "{\\hl"+STY_NAME_DIR+" " );
   65     openTags.push_back ( "{\\hl"+STY_NAME_DST+" " );
   66     openTags.push_back ( "{\\hl"+STY_NAME_LIN+" " );
   67     openTags.push_back ( "{\\hl"+STY_NAME_SYM+" " );
   68     openTags.push_back ( "{\\hl"+STY_NAME_IPL+" " );
   69 
   70     for (unsigned int i=0; i<NUMBER_BUILTIN_STATES; i++ ) {
   71         closeTags.push_back ( "}" );
   72     }
   73 }
   74 
   75 string TexGenerator::getAttributes ( const string & elemName,const ElementStyle & elem )
   76 {
   77     ostringstream s;
   78     s << "\\def\\hl"
   79       << elemName
   80       << "{";
   81     if ( elem.isBold() )  s << "\\bf";
   82     if ( elem.isItalic() )  s << "\\it";
   83     s  <<  "\\textColor{"
   84        << ( elem.getColour().getRed ( TEX ) ) <<" "
   85        << ( elem.getColour().getGreen ( TEX ) ) <<" "
   86        << ( elem.getColour().getBlue ( TEX ) ) <<" 0.0}}\n";
   87     return  s.str();
   88 }
   89 
   90 string TexGenerator::getHeader()
   91 {
   92     ostringstream os;
   93     os << styleCommentOpen
   94        << " Document title: " << docTitle << "\n\n";
   95     if ( currentSyntax->highlightingEnabled() ) {
   96         if ( includeStyleDef ) {
   97             os << getStyleDefinition();
   98             os << CodeGenerator::readUserStyleDef();
   99         } else {
  100             os << "\\input "
  101                << getStyleOutputPath()
  102                << "\n\n";
  103         }
  104     }
  105 
  106     return os.str();
  107 }
  108 
  109 void TexGenerator::printBody()
  110 {
  111     *out << "{\n\\"<<getBaseFont() <<"\n";
  112 
  113     processRootState();
  114 
  115     *out << "}\n";
  116 }
  117 
  118 string TexGenerator::getFooter()
  119 {
  120     ostringstream os;
  121     os << "\\bye\n";
  122     
  123     if (!omitVersionComment) {
  124         os  << "% TeX generated by Highlight "
  125             << HIGHLIGHT_VERSION
  126             << ", "
  127             << HIGHLIGHT_URL << "\n";
  128     }
  129     return os.str();
  130 }
  131 
  132 string TexGenerator:: maskCharacter ( unsigned char c )
  133 {
  134     switch ( c ) {
  135     case '{':
  136     case '}': {
  137         string m ( "$\\" );
  138         m += c;
  139         m += '$';
  140         return m;
  141     }
  142     break;
  143     case '^':
  144         return "{\\bf\\^{}}";
  145         break;
  146     case '_':
  147     //  return "\\_{}";
  148     //  break;
  149     case '&':
  150     case '$':
  151     case '#':
  152     case '%': {
  153         string m ( "\\" );
  154         m += c;
  155         m += "{}";
  156         return m;
  157     }
  158     break;
  159     case '\\':
  160         return "$\\backslash$";
  161         break;
  162     case ' ':
  163         return spacer;
  164         break;
  165     case '+':
  166     case '-':
  167     case '<':
  168     case '>':
  169     case '=': {
  170         string m ( "$\\mathord{" );
  171         m += c;
  172         m += "}$";
  173         return m;
  174     }
  175     break;
  176     case AUML_LC:
  177         return "\\\"a";
  178         break;
  179     case OUML_LC:
  180         return "\\\"o";
  181         break;
  182     case UUML_LC:
  183         return "\\\"u";
  184         break;
  185     case AUML_UC:
  186         return "\\\"A";
  187         break;
  188     case OUML_UC:
  189         return "\\\"O";
  190         break;
  191     case UUML_UC:
  192         return "\\\"U";
  193         break;
  194     case AACUTE_LC:
  195         return "\\'a";
  196         break;
  197     case EACUTE_LC:
  198         return "\\'e";
  199         break;
  200     case OACUTE_LC:
  201         return "\\'o";
  202         break;
  203     case UACUTE_LC:
  204         return "\\'u";
  205         break;
  206     case AGRAVE_LC:
  207         return "\\`a";
  208         break;
  209     case EGRAVE_LC:
  210         return "\\`e";
  211         break;
  212     case OGRAVE_LC:
  213         return "\\`o";
  214         break;
  215     case UGRAVE_LC:
  216         return "\\`u";
  217         break;
  218     case AACUTE_UC:
  219         return "\\'A";
  220         break;
  221     case EACUTE_UC:
  222         return "\\'E";
  223         break;
  224     case OACUTE_UC:
  225         return "\\'O";
  226         break;
  227     case UACUTE_UC:
  228         return "\\'U";
  229         break;
  230     case AGRAVE_UC:
  231         return "\\`A";
  232         break;
  233     case EGRAVE_UC:
  234         return "\\`E";
  235         break;
  236     case UGRAVE_UC:
  237         return "\\`O";
  238         break;
  239     case OGRAVE_UC:
  240         return "\\`U";
  241         break;
  242     case SZLIG:
  243         return "\\ss ";
  244         break;
  245 
  246     default :
  247         return string ( 1, c );
  248     }
  249 }
  250 
  251 string TexGenerator::getKeywordOpenTag ( unsigned int styleID )
  252 {
  253     return "{\\hl"+currentSyntax->getKeywordClasses() [styleID]+" ";
  254 }
  255 
  256 string TexGenerator::getKeywordCloseTag ( unsigned int styleID )
  257 {
  258     return "}";
  259 }
  260 
  261 
  262 string TexGenerator::getStyleDefinition()
  263 {
  264     if ( disableStyleCache || styleDefinitionCache.empty() ) {
  265         ostringstream os;
  266         os << "% highlight theme: "<<docStyle.getDescription()<<"\n";
  267         os << getAttributes ( STY_NAME_STD, docStyle.getDefaultStyle() );
  268         os << getAttributes ( STY_NAME_NUM, docStyle.getNumberStyle() );
  269         os << getAttributes ( STY_NAME_ESC, docStyle.getEscapeCharStyle() );
  270         os << getAttributes ( STY_NAME_STR, docStyle.getStringStyle() );
  271         os << getAttributes ( STY_NAME_DST, docStyle.getPreProcStringStyle() );
  272         os << getAttributes ( STY_NAME_SLC, docStyle.getSingleLineCommentStyle() );
  273         os << getAttributes ( STY_NAME_COM, docStyle.getCommentStyle() );
  274         os << getAttributes ( STY_NAME_DIR, docStyle.getPreProcessorStyle() );
  275         os << getAttributes ( STY_NAME_LIN, docStyle.getLineStyle() );
  276         os << getAttributes ( STY_NAME_SYM, docStyle.getOperatorStyle() );
  277         os << getAttributes ( STY_NAME_IPL, docStyle.getInterpolationStyle() );
  278 
  279         KeywordStyles styles = docStyle.getKeywordStyles();
  280         for ( KSIterator it=styles.begin(); it!=styles.end(); it++ ) {
  281             os << getAttributes ( it->first, it->second );
  282         }
  283 
  284         os <<  "% The special option is not supported by all dvi drivers\n"
  285            << "\\special{background rgb "
  286            << docStyle.getBgColour().getRed ( LATEX ) << " "
  287            << docStyle.getBgColour().getGreen ( LATEX ) << " "
  288            << docStyle.getBgColour().getBlue ( LATEX ) << "}";
  289         os << "\n\\nopagenumbers\n"
  290            << "\\input colordvi\n";
  291         styleDefinitionCache=os.str();
  292     }
  293     return styleDefinitionCache;
  294 }
  295 
  296 }