"Fossies" - the Fresh Open Source Software Archive

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

    1 /***************************************************************************
    2                     odtgenerator.cpp  -  description
    3                              -------------------
    4     begin                : Jul 19 2012
    5     copyright            : (C) 2012 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 #include <sstream>
   28 #include "odtgenerator.h"
   29 
   30 namespace highlight
   31 {
   32 
   33 ODTGenerator::ODTGenerator() : CodeGenerator ( ODTFLAT )
   34 {
   35     newLineTag = "</text:p>\n<text:p text:style-name=\"Standard\">";
   36     spacer = "<text:s text:c=\"1\"/>"; // TODO insert number of spaces
   37     maskWs=true;
   38     if ( !preFormatter.getReplaceTabs()) { // TODO use <text:tab/>
   39 
   40         preFormatter.setReplaceTabs ( true );
   41         preFormatter.setNumberSpaces ( 4 );
   42     }
   43 }
   44 
   45 ODTGenerator::~ODTGenerator() {}
   46 
   47 string ODTGenerator::getStyleDefinition()
   48 {
   49     if ( disableStyleCache || styleDefinitionCache.empty() ) {
   50         ostringstream os;
   51 
   52         os  << "<office:styles>\n<style:style style:name=\"Standard\" style:family=\"paragraph\" style:class=\"text\">\n"
   53             << "  <style:text-properties style:font-name=\""<< getBaseFont()<<"\" fo:font-size=\""<<getBaseFontSize()<<"pt\"/>"
   54             << "</style:style>\n</office:styles>"
   55             << "<office:automatic-styles>\n";
   56 
   57         os << getAttributes ( STY_NAME_STD, docStyle.getDefaultStyle() )
   58            << getAttributes ( STY_NAME_NUM, docStyle.getNumberStyle() )
   59            << getAttributes ( STY_NAME_ESC, docStyle.getEscapeCharStyle() )
   60            << getAttributes ( STY_NAME_STR, docStyle.getStringStyle() )
   61            << getAttributes ( STY_NAME_DST, docStyle.getPreProcStringStyle() )
   62            << getAttributes ( STY_NAME_SLC, docStyle.getSingleLineCommentStyle() )
   63            << getAttributes ( STY_NAME_COM, docStyle.getCommentStyle() )
   64            << getAttributes ( STY_NAME_DIR, docStyle.getPreProcessorStyle() )
   65            << getAttributes ( STY_NAME_SYM, docStyle.getOperatorStyle() )
   66            << getAttributes ( STY_NAME_IPL, docStyle.getInterpolationStyle() )
   67            << getAttributes ( STY_NAME_LIN, docStyle.getLineStyle() );
   68 
   69         KeywordStyles styles = docStyle.getKeywordStyles();
   70         for ( KSIterator it=styles.begin(); it!=styles.end(); it++ ) {
   71             os << getAttributes (it->first, it->second );
   72         }
   73 
   74         os << "<style:page-layout style:name=\"pm1\">"
   75            << "<style:page-layout-properties fo:page-width=\"21.001cm\" fo:page-height=\"29.7cm\" style:num-format=\"1\""
   76            << " style:print-orientation=\"portrait\" fo:margin=\"2cm\" fo:margin-top=\"2cm\" fo:margin-bottom=\"2cm\" fo:margin-left=\"2cm\" fo:margin-right=\"2cm\" "
   77            << "fo:background-color=\"#"
   78            << ( docStyle.getBgColour().getRed ( HTML ) )
   79            << ( docStyle.getBgColour().getGreen ( HTML ) )
   80            << ( docStyle.getBgColour().getBlue ( HTML ) )
   81            << "\" style:writing-mode=\"lr-tb\" style:footnote-max-height=\"0cm\">\n"
   82            << " <style:background-image/>\n"
   83            << " <style:footnote-sep style:width=\"0.018cm\" style:distance-before-sep=\"0.101cm\" style:distance-after-sep=\"0.101cm\" style:line-style=\"solid\""
   84            << " style:adjustment=\"left\" style:rel-width=\"25%\" style:color=\"#000000\"/>"
   85            << "</style:page-layout-properties>\n"
   86            << "<style:header-style/>\n"
   87            << "<style:footer-style/>\n"
   88            << "</style:page-layout>\n"
   89            << "</office:automatic-styles>\n"
   90            << "<office:master-styles>\n"
   91            << " <style:master-page style:name=\"Standard\" style:page-layout-name=\"pm1\"/>\n"
   92            << "</office:master-styles>\n";
   93 
   94         styleDefinitionCache=os.str();
   95     }
   96     return styleDefinitionCache;
   97 }
   98 
   99 string ODTGenerator::getAttributes ( const string & elemName,
  100                                      const ElementStyle & elem )
  101 {
  102     ostringstream s;
  103     s << "<style:style style:name=\""<<elemName<<"\" style:family=\"text\">\n"
  104       << "<style:text-properties fo:color=\"#"
  105       << ( elem.getColour().getRed ( HTML ) )
  106       << ( elem.getColour().getGreen ( HTML ) )
  107       << ( elem.getColour().getBlue ( HTML ) )
  108       << "\""
  109       << ( elem.isBold() ?     " fo:font-weight=\"bold\"" :"" )
  110       << ( elem.isItalic() ?   " fo:font-style=\"italic\"" :"" )
  111       << ( elem.isUnderline() ? " style:text-underline-style=\"solid\" style:text-underline-width=\"auto\" style:text-underline-color=\"font-color\"" :"" )
  112       <<"/>\n</style:style>\n" ;
  113 
  114     return s.str();
  115 }
  116 
  117 string ODTGenerator::getHeader()
  118 {
  119       string enc = "UTF-8";
  120      if ( encodingDefined() ) {
  121         enc = encoding;
  122     }
  123     ostringstream header;
  124     header << "<?xml version=\"1.0\" encoding=\""<<enc<<"\"?>\n"\
  125            "<office:document xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\""\
  126            " xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\" xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\""\
  127            " xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\" xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\""\
  128            " xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""\
  129            " xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\" xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\""\
  130            " xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\" xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\""\
  131            " xmlns:math=\"http://www.w3.org/1998/Math/MathML\" xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\" xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\""\
  132            " xmlns:config=\"urn:oasis:names:tc:opendocument:xmlns:config:1.0\" xmlns:ooo=\"http://openoffice.org/2004/office\" xmlns:ooow=\"http://openoffice.org/2004/writer\""\
  133            " xmlns:oooc=\"http://openoffice.org/2004/calc\" xmlns:dom=\"http://www.w3.org/2001/xml-events\" xmlns:xforms=\"http://www.w3.org/2002/xforms\""\
  134            " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:rpt=\"http://openoffice.org/2005/report\""\
  135            " xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" xmlns:grddl=\"http://www.w3.org/2003/g/data-view#\""\
  136            " xmlns:tableooo=\"http://openoffice.org/2009/table\" xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\""\
  137            " xmlns:formx=\"urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0\" xmlns:css3t=\"http://www.w3.org/TR/css3-text/\" office:version=\"1.2\""\
  138            " office:mimetype=\"application/vnd.oasis.opendocument.text\">\n"\
  139            " <office:font-face-decls>\n"\
  140            "  <style:font-face style:name=\""<< getBaseFont()<<"\" svg:font-family=\""<< getBaseFont()<<"\" style:font-family-generic=\"modern\" style:font-pitch=\"fixed\"/>\n"\
  141            " </office:font-face-decls>\n";
  142 
  143     header << getStyleDefinition();
  144 
  145     header  << " <office:body>\n"
  146             << "  <office:text>\n"
  147             << "<text:p text:style-name=\"Standard\">";
  148 
  149     return header.str();
  150 }
  151 
  152 void ODTGenerator::printBody()
  153 {
  154     processRootState();
  155 }
  156 
  157 string ODTGenerator::getFooter()
  158 {
  159     return "</text:p>\n  </office:text>\n </office:body>\n</office:document>";
  160 }
  161 
  162 string  ODTGenerator::getOpenTag ( const string& styleName )
  163 {
  164     return "<text:span text:style-name=\""+styleName+"\">";
  165 }
  166 
  167 void ODTGenerator::initOutputTags ()
  168 {
  169     openTags.push_back ( "" );
  170     openTags.push_back ( getOpenTag ( STY_NAME_STR ) );
  171     openTags.push_back ( getOpenTag ( STY_NAME_NUM ) );
  172     openTags.push_back ( getOpenTag ( STY_NAME_SLC ) );
  173     openTags.push_back ( getOpenTag ( STY_NAME_COM ) );
  174     openTags.push_back ( getOpenTag ( STY_NAME_ESC ) );
  175     openTags.push_back ( getOpenTag ( STY_NAME_DIR ) );
  176     openTags.push_back ( getOpenTag ( STY_NAME_DST ) );
  177     openTags.push_back ( getOpenTag ( STY_NAME_LIN ) );
  178     openTags.push_back ( getOpenTag ( STY_NAME_SYM ) );
  179     openTags.push_back ( getOpenTag ( STY_NAME_IPL ) );
  180 
  181     closeTags.push_back ( "" );
  182     for (unsigned int i=1; i<NUMBER_BUILTIN_STATES; i++ ) {
  183         closeTags.push_back ( "</text:span>" );
  184     }
  185 
  186 }
  187 
  188 string ODTGenerator::maskCharacter ( unsigned char c )
  189 {
  190     switch ( c ) {
  191     case '<' :
  192         return "&lt;";
  193         break;
  194     case '>' :
  195         return "&gt;";
  196         break;
  197     case '&' :
  198         return "&amp;";
  199         break;
  200     case '\"' :
  201         return "&quot;";
  202         break;
  203     case ' ':
  204         return spacer;
  205         break;
  206     //  case '\t':
  207     //      return "<text:tab/>";
  208     //      break;
  209     default:
  210         return string ( 1, c );
  211     }
  212 }
  213 
  214 string ODTGenerator::getKeywordOpenTag ( unsigned int styleID )
  215 {
  216     return getOpenTag ( currentSyntax->getKeywordClasses() [styleID] );
  217 }
  218 
  219 string ODTGenerator::getKeywordCloseTag ( unsigned int styleID )
  220 {
  221     return "</text:span>";
  222 }
  223 
  224 }