"Fossies" - the Fresh Open Source Software Archive

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

    1 /***************************************************************************
    2                           themereader.cpp  -  description
    3                              -------------------
    4     begin                : Son Nov 10 2002
    5     copyright            : (C) 2002-2019 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 "themereader.h"
   28 #include <Diluculum/LuaState.hpp>
   29 #include <sstream>
   30 #include <iostream>
   31 
   32 namespace highlight
   33 {
   34 
   35 ThemeReader::ThemeReader() : fileOK ( false ), restoreStyles(false), dirtyAttributes(false)
   36 {}
   37 
   38 ThemeReader::~ThemeReader()
   39 {
   40     for (unsigned int i=0; i<pluginChunks.size(); i++) {
   41         delete pluginChunks[i];
   42     }
   43 }
   44 
   45 void ThemeReader::initStyle(ElementStyle& style, const Diluculum::LuaVariable& var)
   46 {
   47     string styleColor="#000000";
   48     bool styleBold=false, styleItalic=false, styleUnderline=false;
   49 
   50     if (var["Colour"].value()!=Diluculum::Nil)
   51         styleColor= var["Colour"].value().asString();
   52     if (var["Bold"].value()!=Diluculum::Nil)
   53         styleBold= var["Bold"].value().asBoolean();
   54     if (var["Italic"].value()!=Diluculum::Nil)
   55         styleItalic= var["Italic"].value().asBoolean();
   56     if (var["Underline"].value()!=Diluculum::Nil)
   57         styleUnderline= var["Underline"].value().asBoolean();
   58 
   59     style.setColour(Colour(styleColor));
   60     style.setBold(styleBold);
   61     style.setItalic(styleItalic);
   62     style.setUnderline(styleUnderline);
   63 }
   64 
   65 bool ThemeReader::load ( const string &styleDefinitionPath , OutputType type)
   66 {
   67     try {
   68         fileOK=true;
   69 
   70         Diluculum::LuaState ls;
   71 
   72         ls["HL_FORMAT_HTML"]=HTML;
   73         ls["HL_FORMAT_XHTML"]=XHTML;
   74         ls["HL_FORMAT_TEX"]=TEX;
   75         ls["HL_FORMAT_LATEX"]=LATEX;
   76         ls["HL_FORMAT_RTF"]=RTF;
   77         ls["HL_FORMAT_ANSI"]=ESC_ANSI;
   78         ls["HL_FORMAT_XTERM256"]=ESC_XTERM256;
   79         ls["HL_FORMAT_TRUECOLOR"]=ESC_TRUECOLOR;
   80         ls["HL_FORMAT_SVG"]=SVG;
   81         ls["HL_FORMAT_BBCODE"]=BBCODE;
   82         ls["HL_FORMAT_PANGO"]=PANGO;
   83         ls["HL_FORMAT_ODT"]=ODTFLAT;
   84         ls["HL_OUTPUT"] = type;
   85         ls.doString("Injections={}");
   86 
   87         ls.doFile (styleDefinitionPath);
   88 
   89         desc = ls["Description"].value().asString();
   90 
   91         if (ls["Categories"].value() !=Diluculum::Nil){
   92 
   93             Diluculum::LuaValueMap categoryMap;
   94             categoryMap = ls["Categories"].value().asTable();
   95                 
   96             for(Diluculum::LuaValueMap::const_iterator it = categoryMap.begin(); it != categoryMap.end(); ++it)
   97             {
   98                 categories.append(it->second.asString());
   99                 if (std::next(it) != categoryMap.end()) {
  100                     categories.append(",");
  101                 }
  102             }
  103         }
  104         
  105         if (pluginChunks.size()) {
  106             Diluculum::LuaValueList params;
  107             params.push_back(desc);
  108             for (unsigned int i=0; i<pluginChunks.size(); i++) {
  109                 ls.call(*pluginChunks[i], params, "theme user function");
  110             }
  111         }
  112 
  113         initStyle(canvas, ls["Canvas"]);
  114         initStyle(defaultElem, ls["Default"]);
  115         initStyle(comment, ls["BlockComment"]);
  116         initStyle(slcomment, ls["LineComment"]);
  117         initStyle(directive, ls["PreProcessor"]);
  118         initStyle(str, ls["String"]);
  119         initStyle(escapeChar, ls["Escape"]);
  120         initStyle(interpolation, ls["Interpolation"]);
  121         initStyle(number, ls["Number"]);
  122         initStyle(dstr, ls["StringPreProc"]);
  123         initStyle(line, ls["LineNum"]);
  124         initStyle(operators, ls["Operator"]);
  125 
  126         int idx=1;
  127         ElementStyle kwStyle;
  128         char kwName[5];
  129         while (ls["Keywords"][idx].value() !=Diluculum::Nil) {
  130             initStyle(kwStyle, ls["Keywords"][idx]);
  131             snprintf(kwName, sizeof(kwName), "kw%c", ('a'+idx-1));
  132             keywordStyles.insert ( make_pair ( string(kwName), kwStyle ));
  133             idx++;
  134         }
  135         
  136         originalStyles=keywordStyles;
  137         
  138         idx=1;
  139         while (ls["Injections"][idx].value() !=Diluculum::Nil) {
  140             themeInjections +=ls["Injections"][idx].value().asString();
  141             idx++;
  142         }
  143 
  144     } catch (Diluculum::LuaFileError &err) {
  145         errorMsg = string(err.what());
  146         return fileOK=false;
  147     } catch (Diluculum::TypeMismatchError &err) {
  148         errorMsg = string(err.what());
  149         return fileOK=false;
  150     } catch (Diluculum::LuaSyntaxError &err) {
  151         errorMsg = "syntax error: "+string(err.what());
  152         return fileOK=false;
  153     }
  154 
  155     return fileOK;
  156 }
  157 
  158 string ThemeReader::getErrorMessage() const
  159 {
  160     return errorMsg;
  161 }
  162 
  163 Colour ThemeReader::getBgColour() const
  164 {
  165     return canvas.getColour();
  166 }
  167 
  168 ElementStyle ThemeReader::getDefaultStyle() const
  169 {
  170     return defaultElem;
  171 }
  172 
  173 ElementStyle ThemeReader::getCommentStyle() const
  174 {
  175     return comment;
  176 }
  177 
  178 ElementStyle ThemeReader::getSingleLineCommentStyle() const
  179 {
  180     return slcomment;
  181 }
  182 
  183 ElementStyle ThemeReader::getStringStyle() const
  184 {
  185     return str;
  186 }
  187 
  188 ElementStyle ThemeReader::getPreProcStringStyle() const
  189 {
  190     return dstr;
  191 }
  192 
  193 ElementStyle ThemeReader::getEscapeCharStyle() const
  194 {
  195     return escapeChar;
  196 }
  197 
  198 ElementStyle ThemeReader::getInterpolationStyle() const
  199 {
  200     return interpolation;
  201 }
  202 
  203 ElementStyle ThemeReader::getNumberStyle() const
  204 {
  205     return number;
  206 }
  207 
  208 ElementStyle ThemeReader::getPreProcessorStyle() const
  209 {
  210     return directive;
  211 }
  212 
  213 ElementStyle ThemeReader::getLineStyle() const
  214 {
  215     return line;
  216 }
  217 
  218 ElementStyle ThemeReader::getOperatorStyle() const
  219 {
  220     return operators;
  221 }
  222 
  223 bool ThemeReader::found () const
  224 {
  225     return fileOK;
  226 }
  227 
  228 ElementStyle ThemeReader::getKeywordStyle ( const string &className )
  229 {
  230     if ( !keywordStyles.count ( className ) ) return defaultElem;
  231     return keywordStyles[className];
  232 }
  233 
  234 vector <string> ThemeReader::getClassNames() const
  235 {
  236     vector <string> kwClassNames;
  237     for ( KSIterator iter = keywordStyles.begin(); iter != keywordStyles.end(); iter++ ) {
  238         kwClassNames.push_back ( ( *iter ).first );
  239     }
  240     return kwClassNames;
  241 }
  242 
  243 KeywordStyles ThemeReader::getKeywordStyles() const
  244 {
  245     return keywordStyles;
  246 }
  247 
  248 string ThemeReader::getInjections() const
  249 {
  250     return themeInjections;
  251 }
  252 
  253 void ThemeReader::overrideAttributes(vector<int>& attributes) {
  254     
  255     if (dirtyAttributes)
  256         keywordStyles=originalStyles;
  257     
  258     for ( std::vector<int>::iterator it = attributes.begin() ; it != attributes.end(); ++it)
  259     {
  260         int kwGroup=*it & 0xf;
  261         char kwName[5];
  262         snprintf(kwName, sizeof(kwName), "kw%c", ('a'+kwGroup-1));
  263         
  264         if (keywordStyles.count ( kwName)) {
  265    
  266             ElementStyle elem = keywordStyles[kwName]; 
  267             if (*it & 128) elem.setBold(true);
  268             if (*it & 256) elem.setItalic(true);
  269             if (*it & 512) elem.setUnderline(true);
  270             if (*it & 1024) elem.setBold(false);
  271             if (*it & 2048) elem.setItalic(false);
  272             if (*it & 4096) elem.setUnderline(false);
  273             keywordStyles[kwName] = elem;
  274             dirtyAttributes = true;
  275         }
  276     }
  277 }
  278 
  279 }