"Fossies" - the Fresh Open Source Software Archive

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

    1 /***************************************************************************
    2                      svggenerator.cpp  -  description
    3                              -------------------
    4     begin                : Mo 23.06.2008
    5     copyright            : (C) 2008-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 <string>
   29 #include <sstream>
   30 
   31 #include "version.h"
   32 #include "svggenerator.h"
   33 
   34 namespace highlight
   35 {
   36 
   37 SVGGenerator::SVGGenerator()
   38     : CodeGenerator ( SVG )
   39 {
   40     //spacer = " ";
   41     spacer = "&#160;";
   42     maskWs=true;
   43     newLineTag = "\n";
   44     styleCommentOpen="/*";
   45     styleCommentClose="*/";
   46 }
   47 
   48 SVGGenerator::~SVGGenerator() {}
   49 
   50 void SVGGenerator::initOutputTags()
   51 {
   52     openTags.push_back ( "" );
   53     openTags.push_back ( getOpenTag ( STY_NAME_STR ) );
   54     openTags.push_back ( getOpenTag ( STY_NAME_NUM ) );
   55     openTags.push_back ( getOpenTag ( STY_NAME_SLC ) );
   56     openTags.push_back ( getOpenTag ( STY_NAME_COM ) );
   57     openTags.push_back ( getOpenTag ( STY_NAME_ESC ) );
   58     openTags.push_back ( getOpenTag ( STY_NAME_DIR ) );
   59     openTags.push_back ( getOpenTag ( STY_NAME_DST ) );
   60     openTags.push_back ( getOpenTag ( STY_NAME_LIN ) );
   61     openTags.push_back ( getOpenTag ( STY_NAME_SYM ) );
   62     openTags.push_back ( getOpenTag ( STY_NAME_IPL ) );
   63 
   64     closeTags.push_back ( "" );
   65     for (unsigned int i=1; i<NUMBER_BUILTIN_STATES; i++ ) {
   66         closeTags.push_back ( "</tspan>" );
   67     }
   68 }
   69 
   70 string SVGGenerator::getStyleDefinition()
   71 {
   72     if (disableStyleCache || styleDefinitionCache.empty() ) {
   73         ostringstream os;
   74         if ( includeStyleDef ) {
   75             os << "<style type=\"text/css\">\n"
   76                << "<![CDATA[\n";
   77         }
   78         os << "rect { fill:#"
   79            << ( docStyle.getBgColour().getRed ( SVG ) )
   80            << ( docStyle.getBgColour().getGreen ( SVG ) )
   81            << ( docStyle.getBgColour().getBlue ( SVG ) )
   82            << "; } \n";
   83         os << "g { font-size: " << getBaseFontSize();
   84         os << "; font-family: " << getBaseFont() << "; white-space: pre;}\n";
   85         os << getAttributes ( "text", docStyle.getDefaultStyle() )
   86            << getAttributes ( "tspan."+STY_NAME_NUM, docStyle.getNumberStyle() )
   87            << getAttributes ( "tspan."+STY_NAME_ESC, docStyle.getEscapeCharStyle() )
   88            << getAttributes ( "tspan."+STY_NAME_STR, docStyle.getStringStyle() )
   89            << getAttributes ( "tspan."+STY_NAME_DST, docStyle.getPreProcStringStyle() )
   90            << getAttributes ( "tspan."+STY_NAME_SLC, docStyle.getSingleLineCommentStyle() )
   91            << getAttributes ( "tspan."+STY_NAME_COM, docStyle.getCommentStyle() )
   92            << getAttributes ( "tspan."+STY_NAME_DIR, docStyle.getPreProcessorStyle() )
   93            << getAttributes ( "tspan."+STY_NAME_SYM, docStyle.getOperatorStyle() )
   94            << getAttributes ( "tspan."+STY_NAME_IPL, docStyle.getInterpolationStyle() )
   95            << getAttributes ( "tspan."+STY_NAME_LIN, docStyle.getLineStyle() );
   96 
   97         KeywordStyles styles = docStyle.getKeywordStyles();
   98         for ( KSIterator it=styles.begin(); it!=styles.end(); it++ ) {
   99             os << getAttributes ( "tspan."+it->first, it->second );
  100         }
  101         if ( includeStyleDef ) {
  102             os << "]]>\n"
  103                << "</style>";
  104         }
  105         styleDefinitionCache=os.str();
  106     }
  107     return styleDefinitionCache;
  108 }
  109 
  110 
  111 string SVGGenerator::getAttributes ( const string & elemName,
  112                                      const ElementStyle & elem )
  113 {
  114     ostringstream s;
  115     if ( !elemName.empty() ) {
  116         s << /*cssClassName<<"."<<*/ elemName<<" { ";
  117     }
  118     s << "fill:#"
  119       << ( elem.getColour().getRed ( HTML ) )
  120       << ( elem.getColour().getGreen ( HTML ) )
  121       << ( elem.getColour().getBlue ( HTML ) )
  122       << ( elem.isBold() ?     "; font-weight:bold" :"" )
  123       << ( elem.isItalic() ?   "; font-style:italic" :"" )
  124       << ( elem.isUnderline() ? "; text-decoration:underline" :"" );
  125     if ( !elemName.empty() ) {
  126         s << "; }\n" ;
  127     }
  128     return s.str();
  129 }
  130 
  131 string  SVGGenerator::getOpenTag ( const string& styleName )
  132 {
  133     return "<tspan class=\""+styleName+"\">";
  134 }
  135 
  136 string SVGGenerator::getHeader()
  137 {
  138     ostringstream header;
  139     header << "<?xml version=\"1.0\"";
  140     if ( encodingDefined() ) {
  141         header << " encoding=\"" << encoding << "\"";
  142     }
  143     header << "?>\n";
  144     if ( !includeStyleDef ) {
  145         header  << "<?xml-stylesheet type=\"text/css\" href=\""
  146                 << getStyleOutputPath()
  147                 << "\"?>\n";
  148     }
  149     header << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.2//EN\" "
  150            << "\"http://www.w3.org/Graphics/SVG/1.2/DTD/svg12.dtd\">\n";
  151     header << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.2\" "
  152            << "baseProfile=\"full\" xml:space=\"preserve\"";
  153     if ( width.size() ) header << " width=\""<<width<<"\"";
  154     if ( height.size() ) header << " height=\""<<height<<"\"";
  155     //viewBox=\"0 0 800 600\"
  156     header << ">\n<desc>" << docTitle << "</desc>\n";
  157     if ( includeStyleDef ) {
  158         header << "<defs>\n";
  159         header << getStyleDefinition();
  160         header << "\n</defs>\n";
  161     }
  162     return header.str();
  163 }
  164 
  165 void SVGGenerator::printBody()
  166 {
  167     *out << "<g>\n<rect x=\"0\" y=\"0\" width=\"100%\" height=\"100%\"/>"; // rect: background color
  168     int fontSize=0;
  169     StringTools::str2num<int> ( fontSize, getBaseFontSize(), std::dec );
  170     *out << "\n<text x=\"10\" y=\""<<fontSize*2<<"\">";
  171     processRootState();
  172     *out << "</text>\n</g>\n";
  173 }
  174 
  175 
  176 string SVGGenerator::getFooter()
  177 {
  178     ostringstream os;
  179     os <<"</svg>\n";
  180     
  181     if (!omitVersionComment) {
  182         os  << "<!-- SVG generated by Highlight "
  183             << HIGHLIGHT_VERSION
  184             << ", "
  185             << HIGHLIGHT_URL
  186             <<" -->\n";
  187     }
  188     return os.str();
  189 }
  190 
  191 string SVGGenerator::maskCharacter ( unsigned  char c )
  192 {
  193     switch ( c ) {
  194     case ' ' :
  195         return spacer;
  196         break;
  197     case '<' :
  198         return "&lt;";
  199         break;
  200     case '>' :
  201         return "&gt;";
  202         break;
  203     case '&' :
  204         return "&amp;";
  205         break;
  206     case '\"' :
  207         return "&quot;";
  208         break;
  209     default:
  210         return string ( 1, c );
  211     }
  212 }
  213 
  214 string SVGGenerator::getKeywordOpenTag ( unsigned int styleID )
  215 {
  216     return getOpenTag ( currentSyntax->getKeywordClasses() [styleID] );
  217 }
  218 
  219 string SVGGenerator::getKeywordCloseTag ( unsigned int styleID )
  220 {
  221     return "</tspan>";
  222 }
  223 
  224 string SVGGenerator::getNewLine()
  225 {
  226 
  227     if ( lineNumber>1 ) {
  228         ostringstream os;
  229         int fontSize=0;
  230         StringTools::str2num<int> ( fontSize, getBaseFontSize(), std::dec );
  231         os<< "</text>\n<text x=\"10\" y=\""<< ( lineNumber*fontSize*2 ) <<"\">";
  232         return os.str();
  233     } else {
  234         return "";
  235     }
  236 }
  237 
  238 void SVGGenerator::setSVGSize ( const string& w, const string& h )
  239 {
  240     width=w;
  241     height=h;
  242 }
  243 
  244 }