"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/rtfgenerator.cpp" (12 May 2020, 13350 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 "rtfgenerator.cpp": 3.53_vs_3.54.

    1 /***************************************************************************
    2                           rtfcode.cpp  -  description
    3                              -------------------
    4     begin                : Die Jul 9 2002
    5     copyright            : (C) 2002-2016 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 #include <string>
   30 
   31 #include "charcodes.h"
   32 #include "version.h"
   33 #include "rtfgenerator.h"
   34 
   35 namespace highlight
   36 {
   37 
   38 RtfGenerator::RtfGenerator()
   39     : CodeGenerator ( RTF ),
   40       pageSize ( "a4" ), // Default: DIN A4
   41       addCharStyles ( false ),
   42       addPageColor(false),
   43       isUtf8(false),
   44       utf16Char(0),
   45       utf8SeqLen(0)
   46 {
   47     newLineTag = "}\\par\\pard\n\\cbpat1{";
   48     spacer = " ";
   49 
   50     // Page dimensions
   51     psMap["a3"] = PageSize ( 16837,23811 );
   52     psMap["a4"] = PageSize ( 11905,16837 );
   53     psMap["a5"] = PageSize ( 8390,11905 );
   54 
   55     psMap["b4"] = PageSize ( 14173,20012 );
   56     psMap["b5"] = PageSize ( 9977,14173 );
   57     psMap["b6"] = PageSize ( 7086,9977 );
   58 
   59     psMap["letter"] = PageSize ( 12240,15840 );
   60     psMap["legal"] = PageSize ( 12240,20163 );
   61 }
   62 
   63 RtfGenerator::~RtfGenerator()
   64 {}
   65 
   66 string RtfGenerator::getHeader()
   67 {
   68     return string();
   69 }
   70 
   71 string RtfGenerator::getAttributes ( const ElementStyle & col )
   72 {
   73     stringstream s;
   74     s  << "\\red"<< col.getColour().getRed ( RTF )
   75        << "\\green"<<col.getColour().getGreen ( RTF )
   76        << "\\blue"<<col.getColour().getBlue ( RTF )
   77        << ";";
   78     return s.str();
   79 }
   80 
   81 string  RtfGenerator::getOpenTag ( int styleNumber,const ElementStyle & elem )
   82 {
   83     ostringstream s;
   84     s << "{";
   85     if ( addCharStyles ) {
   86         s<<"\\*\\cs"<< ( styleNumber+2 );
   87     }
   88     s << "\\cf"<< ( styleNumber+2 ) <<"{";
   89 
   90     if ( elem.isBold() ) s << "\\b ";
   91     if ( elem.isItalic() ) s << "\\i ";
   92     if ( elem.isUnderline() ) s << "\\ul ";
   93     return  s.str();
   94 }
   95 
   96 
   97 string  RtfGenerator::getCloseTag ( const ElementStyle &elem )
   98 {
   99     ostringstream s;
  100     if ( elem.isBold() ) s << "\\b0 ";
  101     if ( elem.isItalic() ) s << "\\i0 ";
  102     if ( elem.isUnderline() ) s << "\\ul0 ";
  103     s << "}}";
  104     return  s.str();
  105 }
  106 
  107 string RtfGenerator::getCharStyle ( int styleNumber,const ElementStyle &elem,
  108                                     const string&styleName )
  109 {
  110     ostringstream s;
  111     s << "{\\*\\cs"<< ( styleNumber+2 ) <<"\\additive\\cf"<< ( styleNumber+2 )
  112       << "\\f1\\fs";
  113     int fontSize=0;
  114     StringTools::str2num<int> ( fontSize, this->getBaseFontSize(), std::dec );
  115     s << ( ( fontSize ) ? fontSize*2: 20 );  // RTF needs double amount
  116     if ( elem.isBold() ) s << "\\b";
  117     if ( elem.isItalic() ) s << "\\i";
  118     if ( elem.isUnderline() ) s << "\\ul";
  119     s << "\\sbasedon222\\snext0 "<< styleName << ";}\n";
  120     return  s.str();
  121 }
  122 // {\*\cs2\additive\cf2\f1\fs20\sbasedon222\snext0 HL Default;}
  123 
  124 
  125 void RtfGenerator::printBody()
  126 {
  127     isUtf8 = StringTools::change_case ( encoding ) == "utf-8";
  128 
  129     *out << "{\\rtf1\\ansi \\deff1" // drop \\uc0 because of unicode output
  130          << "{\\fonttbl{\\f1\\fmodern\\fprq1\\fcharset0 " ;
  131     *out << this->getBaseFont() ;
  132     *out << ";}}"
  133          << "{\\colortbl;";
  134 
  135     *out << "\\red"    << ( docStyle.getBgColour().getRed ( RTF ) );
  136     *out << "\\green"    << ( docStyle.getBgColour().getGreen ( RTF ) );
  137     *out << "\\blue"    << ( docStyle.getBgColour().getBlue ( RTF ) );
  138     *out << ";";
  139 
  140     *out << getAttributes ( docStyle.getDefaultStyle() );
  141 
  142     *out << getAttributes ( docStyle.getStringStyle() );
  143     *out << getAttributes ( docStyle.getNumberStyle() );
  144     *out << getAttributes ( docStyle.getSingleLineCommentStyle() );
  145 
  146     *out << getAttributes ( docStyle.getCommentStyle() );
  147     *out << getAttributes ( docStyle.getEscapeCharStyle() );
  148     *out << getAttributes ( docStyle.getPreProcessorStyle() );
  149 
  150     *out << getAttributes ( docStyle.getPreProcStringStyle() );
  151     *out << getAttributes ( docStyle.getLineStyle() );
  152     *out << getAttributes ( docStyle.getOperatorStyle() );
  153     *out << getAttributes ( docStyle.getInterpolationStyle() );
  154 
  155     /* For output formats which can refer to external styles it is more safe
  156        to use the colour theme's keyword class names, since the language
  157        definitions (which may change during a batch conversion) do not have to define
  158        all keyword classes, that are needed to highlight all input files correctly.
  159        It is ok for RTF to use the language definition's class names, because RTF
  160        does not refer to external styles.
  161        We cannot use the theme's class names, because KSIterator returns an
  162        alphabetically ordered list, which is not good because RTF is dependent
  163        on the order. We access the keyword style with an ID, which is calculated
  164        ignoring the alphabetic order.
  165     */
  166     
  167     ///FIXME: nested syntax may be highlighted incorrectly if it contains more 
  168     //        keyword definitions than the hosting syntax.
  169     //        Workaround: Add keyword group to hosting syntax (see html.lang)
  170     vector<string>  keywordClasses = currentSyntax->getKeywordClasses();
  171         
  172     for ( unsigned int i=0; i<keywordClasses.size(); i++ ) {
  173         *out << getAttributes ( docStyle.getKeywordStyle ( keywordClasses[i] ) );
  174     }
  175 
  176     *out << "}\n";
  177 
  178     if ( addCharStyles ) {
  179         *out << "{\\stylesheet{\n";
  180         *out << getCharStyle ( STANDARD, docStyle.getDefaultStyle(), "HL Default" );
  181         *out << getCharStyle ( STRING, docStyle.getStringStyle(), "HL String" );
  182         *out << getCharStyle ( NUMBER, docStyle.getNumberStyle(), "HL Number" );
  183         *out << getCharStyle ( SL_COMMENT, docStyle.getSingleLineCommentStyle(), "HL SL Comment" );
  184         *out << getCharStyle ( ML_COMMENT, docStyle.getCommentStyle(), "HL ML Comment" );
  185         *out << getCharStyle ( ESC_CHAR, docStyle.getEscapeCharStyle(), "HL Escape Character" );
  186         *out << getCharStyle ( DIRECTIVE, docStyle.getPreProcessorStyle(), "HL Directive" );
  187         *out << getCharStyle ( DIRECTIVE_STRING, docStyle.getPreProcStringStyle(), "HL Directive String" );
  188         *out << getCharStyle ( LINENUMBER, docStyle.getLineStyle(), "HL Line" );
  189         *out << getCharStyle ( SYMBOL, docStyle.getOperatorStyle(), "HL Operator" );
  190         *out << getCharStyle ( STRING_INTERPOLATION, docStyle.getInterpolationStyle(), "HL Interpolation" );
  191         char styleName[20];
  192         for ( unsigned int i=0; i<keywordClasses.size(); i++ ) {
  193             sprintf ( styleName, "HL Keyword %c", 'A'+i ); //maybe better simple numbering
  194             *out << getCharStyle ( KEYWORD+i, docStyle.getKeywordStyle ( keywordClasses[i] ), string ( styleName ) );
  195         }
  196         *out << "}}\n";
  197     }
  198 
  199     if (addPageColor) {
  200         long svVal =  docStyle.getBgColour().getRed() +
  201                       docStyle.getBgColour().getGreen() * 256+
  202                       docStyle.getBgColour().getBlue() * 256 * 256;
  203         *out<<"\\viewbksp1\\ilfomacatclnup0{\\*\\background{\\shp{{\\sp{\\sn fillColor}{\\sv "<<svVal<<"}}}}}\n";
  204     }
  205 
  206     *out  << "\\paperw"<< psMap[pageSize].width <<"\\paperh"<< psMap[pageSize].height
  207           << "\\margl1134\\margr1134\\margt1134\\margb1134\\sectd" // page margins
  208           << "\\plain\\f1\\fs" ;  // Font formatting
  209     int fontSize=0;
  210     StringTools::str2num<int> ( fontSize, this->getBaseFontSize(), std::dec );
  211     *out << ( ( fontSize ) ? fontSize*2: 20 );  // RTF needs double amount
  212     *out << "\n\\pard \\cbpat1{";
  213 
  214     processRootState();
  215 
  216     *out << "}}"<<endl;
  217 }
  218 
  219 string RtfGenerator::getFooter()
  220 {
  221     return string();
  222 }
  223 
  224 void RtfGenerator::initOutputTags ( )
  225 {
  226     openTags.push_back ( getOpenTag ( STANDARD, docStyle.getDefaultStyle() ) );
  227     openTags.push_back ( getOpenTag ( STRING, docStyle.getStringStyle() ) );
  228     openTags.push_back ( getOpenTag ( NUMBER, docStyle.getNumberStyle() ) );
  229     openTags.push_back ( getOpenTag ( SL_COMMENT, docStyle.getSingleLineCommentStyle() ) );
  230     openTags.push_back ( getOpenTag ( ML_COMMENT,docStyle.getCommentStyle() ) );
  231     openTags.push_back ( getOpenTag ( ESC_CHAR, docStyle.getEscapeCharStyle() ) );
  232     openTags.push_back ( getOpenTag ( DIRECTIVE, docStyle.getPreProcessorStyle() ) );
  233     openTags.push_back ( getOpenTag ( DIRECTIVE_STRING, docStyle.getPreProcStringStyle() ) );
  234     openTags.push_back ( getOpenTag ( LINENUMBER, docStyle.getLineStyle() ) );
  235     openTags.push_back ( getOpenTag ( SYMBOL, docStyle.getOperatorStyle() ) );
  236     openTags.push_back ( getOpenTag ( STRING_INTERPOLATION, docStyle.getInterpolationStyle()) );
  237 
  238     closeTags.push_back ( getCloseTag ( docStyle.getDefaultStyle() ) );
  239     closeTags.push_back ( getCloseTag ( docStyle.getStringStyle() ) );
  240     closeTags.push_back ( getCloseTag ( docStyle.getNumberStyle() ) );
  241     closeTags.push_back ( getCloseTag ( docStyle.getSingleLineCommentStyle() ) );
  242     closeTags.push_back ( getCloseTag ( docStyle.getCommentStyle() ) );
  243     closeTags.push_back ( getCloseTag ( docStyle.getEscapeCharStyle() ) );
  244     closeTags.push_back ( getCloseTag ( docStyle.getPreProcessorStyle() ) );
  245     closeTags.push_back ( getCloseTag ( docStyle.getPreProcStringStyle() ) );
  246     closeTags.push_back ( getCloseTag ( docStyle.getLineStyle() ) );
  247     closeTags.push_back ( getCloseTag ( docStyle.getOperatorStyle() ) );
  248     closeTags.push_back ( getCloseTag ( docStyle.getInterpolationStyle() ) );
  249 }
  250 
  251 string RtfGenerator::maskCharacter ( unsigned char c )
  252 {
  253     if (isUtf8 && c > 0x7f  && utf8SeqLen==0) {
  254 
  255         //http://stackoverflow.com/questions/7153935/how-to-convert-utf-8-stdstring-to-utf-16-stdwstring
  256 
  257         if (c <= 0xDF) {
  258             utf16Char = c&0x1F;
  259             utf8SeqLen = 1;
  260         } else if (c <= 0xEF) {
  261             utf16Char = c&0x0F;
  262             utf8SeqLen = 2;
  263         } else if (c <= 0xF7) {
  264             utf16Char = c&0x07;
  265             utf8SeqLen = 3;
  266         } else {
  267             utf8SeqLen = 0;
  268         }
  269         return "";
  270     }
  271 
  272     if (utf8SeqLen) {
  273         utf16Char <<= 6;
  274         utf16Char += c & 0x3f;
  275         --utf8SeqLen;
  276 
  277         if (!utf8SeqLen) {
  278             string m ( "\\u" );
  279             m += to_string(utf16Char);
  280             m += '?';
  281             utf16Char=0L;
  282             return m;
  283         } else {
  284             return "";
  285         }
  286     }
  287 
  288     switch ( c ) {
  289     case '}' :
  290     case '{' :
  291     case '\\' : {
  292         string m ( "\\" );
  293         return m += c;
  294     }
  295     break;
  296     case '0':
  297     case '1':
  298     case '2':
  299     case '3':
  300     case '4':
  301     case '5':
  302     case '6':
  303     case '7':
  304     case '8':
  305     case '9': {
  306         string m ( 1, '{' );
  307         m += c;
  308         m += '}';
  309         return m;
  310     }
  311     break;
  312 
  313     case AUML_LC:
  314         return "\\'e4";
  315         break;
  316     case OUML_LC:
  317         return "\\'f6";
  318         break;
  319     case UUML_LC:
  320         return "\\'fc";
  321         break;
  322     case AUML_UC:
  323         return "\\'c4";
  324         break;
  325     case OUML_UC:
  326         return "\\'d6";
  327         break;
  328     case UUML_UC:
  329         return "\\'dc";
  330         break;
  331 
  332     case AACUTE_LC:
  333         return "\\'e1";
  334         break;
  335     case EACUTE_LC:
  336         return "\\'e9";
  337         break;
  338     case OACUTE_LC:
  339         return "\\'f3";
  340         break;
  341     case UACUTE_LC:
  342         return "\\'fa";
  343         break;
  344 
  345     case AGRAVE_LC:
  346         return "\\'e0";
  347         break;
  348     case EGRAVE_LC:
  349         return "\\'e8";
  350         break;
  351     case OGRAVE_LC:
  352         return "\\'f2";
  353         break;
  354     case UGRAVE_LC:
  355         return "\\'f9";
  356         break;
  357 
  358     case AACUTE_UC:
  359         return "\\'c1";
  360         break;
  361     case EACUTE_UC:
  362         return "\\'c9";
  363         break;
  364     case OACUTE_UC:
  365         return "\\'d3";
  366         break;
  367     case UACUTE_UC:
  368         return "\\'da";
  369         break;
  370     case AGRAVE_UC:
  371         return "\\'c0";
  372         break;
  373     case EGRAVE_UC:
  374         return "\\'c8";
  375         break;
  376     case OGRAVE_UC:
  377         return "\\'d2";
  378         break;
  379     case UGRAVE_UC:
  380         return "\\'d9";
  381         break;
  382 
  383     case SZLIG:
  384         return "\\'df";
  385         break;
  386 
  387     default :
  388         return string ( 1, c );
  389     }
  390 }
  391 
  392 string RtfGenerator::getKeywordOpenTag ( unsigned int styleID )
  393 {
  394     return getOpenTag ( KEYWORD+styleID,
  395                         docStyle.getKeywordStyle ( currentSyntax->getKeywordClasses() [styleID] ) );
  396 }
  397 
  398 string RtfGenerator::getKeywordCloseTag ( unsigned int styleID )
  399 {
  400     return getCloseTag ( docStyle.getKeywordStyle ( currentSyntax->getKeywordClasses() [styleID] ) );
  401 }
  402 
  403 void RtfGenerator::setRTFPageSize ( const string & ps )
  404 {
  405     if ( psMap.count ( ps ) ) pageSize = ps;
  406 }
  407 
  408 void RtfGenerator::setRTFCharStyles ( bool cs )
  409 {
  410     addCharStyles = cs;
  411 }
  412 
  413 void RtfGenerator::setRTFPageColor ( bool pc )
  414 {
  415     addPageColor = pc;
  416 }
  417 
  418 
  419 }