"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/xterm256generator.cpp" (12 May 2020, 7994 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 "xterm256generator.cpp": 3.56_vs_3.57.

    1 /***************************************************************************
    2                    xterm256generator.cpp  -  description
    3                              -------------------
    4     begin                : Oct 13 2006
    5     copyright            : (C) 2006-2020 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 <stdio.h>
   29 #include <math.h>
   30 #include <stdlib.h>
   31 #include <sstream>
   32 
   33 #include "xterm256generator.h"
   34 #include "charcodes.h"
   35 #include "version.h"
   36 
   37 
   38 namespace highlight
   39 {
   40 Xterm256Generator::Xterm256Generator() :
   41     CodeGenerator ( ESC_XTERM256 ),
   42     use16mColours(false),
   43     canvasPadding(0)
   44 {
   45     newLineTag = "\n";
   46     spacer = " ";
   47 }
   48 
   49 Xterm256Generator::~Xterm256Generator() {}
   50 
   51 string Xterm256Generator::getHeader()
   52 {
   53     return string();
   54 }
   55 
   56 void Xterm256Generator::printBody()
   57 {
   58     processRootState();
   59 }
   60 
   61 string Xterm256Generator::getFooter()
   62 {
   63     //return noTrailingNewLine ? "" : "\033[m";
   64     return string();
   65 }
   66 
   67 string Xterm256Generator::maskCharacter ( unsigned char c )
   68 {
   69     return string ( 1, c );
   70 }
   71 
   72 void Xterm256Generator::initOutputTags ( )
   73 {
   74     if (canvasPadding > 0 ) {
   75 
   76         ostringstream bgs;
   77         Colour bg=docStyle.getBgColour();
   78         unsigned char  bg_rgb[3];
   79         bg_rgb[0] = ( unsigned char ) strtoll ( bg.getRed ( HTML ).c_str(), NULL, 16 );
   80         bg_rgb[1] = ( unsigned char ) strtoll ( bg.getGreen ( HTML ).c_str(), NULL, 16 );
   81         bg_rgb[2] = ( unsigned char ) strtoll ( bg.getBlue ( HTML ).c_str(), NULL, 16 );
   82         
   83         if (use16mColours) {
   84             //use 24bit true colour ("888" colours (aka 16 million))
   85             bgs << "\033[48;2;"<< ( int ) bg_rgb[0] << ";" << ( int ) bg_rgb[1] << ";" << ( int ) bg_rgb[2] << "m";
   86         } else {
   87             int bgApprox=( int ) rgb2xterm ( bg_rgb );
   88             if (!bgApprox) bgApprox=16; // workaround transparent 0 value
   89             bgs << "\033[48;5;"<< bgApprox << "m";
   90         }
   91         canvasColSeq = bgs.str();
   92         maskWsBegin = canvasColSeq;
   93     }    
   94     
   95     openTags.push_back ( getOpenTag ( docStyle.getDefaultStyle() ) );
   96     openTags.push_back ( getOpenTag ( docStyle.getStringStyle() ) );
   97     openTags.push_back ( getOpenTag ( docStyle.getNumberStyle() ) );
   98     openTags.push_back ( getOpenTag ( docStyle.getSingleLineCommentStyle() ) );
   99     openTags.push_back ( getOpenTag ( docStyle.getCommentStyle() ) );
  100     openTags.push_back ( getOpenTag ( docStyle.getEscapeCharStyle() ) );
  101     openTags.push_back ( getOpenTag ( docStyle.getPreProcessorStyle() ) );
  102     openTags.push_back ( getOpenTag ( docStyle.getPreProcStringStyle() ) );
  103     openTags.push_back ( getOpenTag ( docStyle.getLineStyle() ) );
  104     openTags.push_back ( getOpenTag ( docStyle.getOperatorStyle() ) );
  105     openTags.push_back ( getOpenTag ( docStyle.getInterpolationStyle()) );
  106 
  107     for (unsigned int i=0; i<NUMBER_BUILTIN_STATES; i++ ) {
  108         closeTags.push_back ( "\033[m" );
  109     }    
  110 }
  111 
  112 string  Xterm256Generator::getOpenTag ( const ElementStyle &col )
  113 {
  114     Colour c= col.getColour();
  115     unsigned char  rgb[3];
  116     rgb[0] = ( unsigned char ) strtoll ( c.getRed ( HTML ).c_str(), NULL, 16 );
  117     rgb[1] = ( unsigned char ) strtoll ( c.getGreen ( HTML ).c_str(), NULL, 16 );
  118     rgb[2] = ( unsigned char ) strtoll ( c.getBlue ( HTML ).c_str(), NULL, 16 );
  119 
  120     ostringstream s;
  121     
  122     s << canvasColSeq;
  123     
  124     s  << "\033[";
  125 
  126     if ( col.isBold() ) s << "1;";
  127     if ( col.isItalic() ) s << "3;";
  128     if ( col.isUnderline() ) s << "4;";
  129 
  130     if (use16mColours) {
  131         //use 24bit true colour ("888" colours (aka 16 million))
  132         s << "38;2;"<< ( int ) rgb[0] << ";" << ( int ) rgb[1] << ";" << ( int ) rgb[2] << "m";
  133     } else {
  134         // apply color approximation, 256 colour palette (216 colours + 16 ansi + 24 gray) (colors are 24bit)
  135         s << "38;5;"<< ( int ) rgb2xterm ( rgb ) << "m";
  136     }
  137     
  138     return  s.str();
  139 }
  140 
  141 string Xterm256Generator::getKeywordOpenTag ( unsigned int styleID )
  142 {
  143     return getOpenTag ( docStyle.getKeywordStyle ( currentSyntax->getKeywordClasses() [styleID] ) );
  144 }
  145 
  146 string Xterm256Generator::getKeywordCloseTag ( unsigned int styleID )
  147 {
  148     return "\033[m";
  149 }
  150 
  151 string Xterm256Generator::getNewLine()
  152 {
  153     string nlStr;
  154     
  155     if (canvasPadding>0) {
  156         unsigned int lastLineLength=getLastLineLength();
  157         
  158         //adapt to long lines; avoid lag with very long lines
  159         if (lastLineLength<512 && lastLineLength > canvasPadding && lastLineLength)
  160             canvasPadding = lastLineLength;
  161         
  162         nlStr += canvasColSeq;
  163         if (canvasPadding > lastLineLength)
  164             nlStr += string(canvasPadding - lastLineLength, ' ');
  165         nlStr += "\033[m";    
  166     } 
  167     nlStr += (printNewLines) ? newLineTag : "";
  168     return nlStr;
  169 }
  170 
  171  void Xterm256Generator::setESCTrueColor(bool b) { 
  172     use16mColours = b; 
  173     if (b) setOutputType(ESC_TRUECOLOR); 
  174 } 
  175     
  176 void Xterm256Generator::setESCCanvasPadding(unsigned int p) { 
  177     if (p<512) canvasPadding = p; 
  178 }
  179 
  180 /* the following functions are based on Wolfgang Frischs xterm256 converter utility:
  181    http://frexx.de/xterm-256-notes/
  182 */
  183 
  184 void Xterm256Generator::xterm2rgb ( unsigned char color, unsigned char* rgb )
  185 {
  186     // 16 basic colors
  187     if ( color<16 ) {
  188         rgb[0] = basic16[color][0];
  189         rgb[1] = basic16[color][1];
  190         rgb[2] = basic16[color][2];
  191     }
  192 
  193     // color cube color
  194     if ( color>=16 && color<=232 ) {
  195         color-=16;
  196         rgb[0] = valuerange[ ( color/36 ) %6];
  197         rgb[1] = valuerange[ ( color/6 ) %6];
  198         rgb[2] = valuerange[color%6];
  199     }
  200 
  201     // gray tone
  202     if ( color>=233 && color<=253 ) {
  203         rgb[0]=rgb[1]=rgb[2] = 8+ ( color-232 ) *0x0a;
  204     }
  205 }
  206 
  207 void Xterm256Generator::maketable()
  208 {
  209     unsigned char c, rgb[3];
  210     for ( c=0; c<=253; c++ ) {
  211         xterm2rgb ( c,rgb );
  212         colortable[c][0] = rgb[0];
  213         colortable[c][1] = rgb[1];
  214         colortable[c][2] = rgb[2];
  215     }
  216 }
  217 
  218 unsigned char Xterm256Generator::rgb2xterm ( unsigned char* rgb )
  219 {
  220     unsigned char c, best_match=0;
  221     double d, smallest_distance;
  222 
  223     if ( !initialized ) {
  224         maketable();
  225         initialized = true;
  226     }
  227 
  228     smallest_distance = 10000000000.0;
  229 
  230     for ( c=0; c<=253; c++ ) {
  231         d = pow ( colortable[c][0]-rgb[0],2.0 ) +
  232             pow ( colortable[c][1]-rgb[1],2.0 ) +
  233             pow ( colortable[c][2]-rgb[2],2.0 );
  234         if ( d<smallest_distance ) {
  235             smallest_distance = d;
  236             best_match=c;
  237         }
  238     }
  239     return best_match;
  240 }
  241 
  242 bool Xterm256Generator::initialized=false;
  243 
  244 unsigned char Xterm256Generator::colortable[254][3] = { {0} };
  245 
  246 const unsigned char Xterm256Generator::valuerange[] = { 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF };
  247 
  248 const unsigned char Xterm256Generator::basic16[16][3] = {
  249     { 0x00, 0x00, 0x00 }, // 0
  250     { 0xCD, 0x00, 0x00 }, // 1
  251     { 0x00, 0xCD, 0x00 }, // 2
  252     { 0xCD, 0xCD, 0x00 }, // 3
  253     { 0x00, 0x00, 0xEE }, // 4
  254     { 0xCD, 0x00, 0xCD }, // 5
  255     { 0x00, 0xCD, 0xCD }, // 6
  256     { 0xE5, 0xE5, 0xE5 }, // 7
  257     { 0x7F, 0x7F, 0x7F }, // 8
  258     { 0xFF, 0x00, 0x00 }, // 9
  259     { 0x00, 0xFF, 0x00 }, // 10
  260     { 0xFF, 0xFF, 0x00 }, // 11
  261     { 0x5C, 0x5C, 0xFF }, // 12
  262     { 0xFF, 0x00, 0xFF }, // 13
  263     { 0x00, 0xFF, 0xFF }, // 14
  264     { 0xFF, 0xFF, 0xFF }  // 15
  265 };
  266 }