TVirtualPS.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TVirtualPS.cxx 35947 2010-09-30 16:08:51Z brun $
00002 // Author: Rene Brun   05/09/99
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //______________________________________________________________________________
00013 //
00014 // TVirtualPS is an abstract interface to a Postscript, PDF and SVG drivers
00015 //
00016 
00017 #include "Riostream.h"
00018 #include "TVirtualPS.h"
00019 
00020 TVirtualPS *gVirtualPS = 0;
00021 
00022 const Int_t  kMaxBuffer = 250;
00023 
00024 ClassImp(TVirtualPS)
00025 
00026 
00027 //______________________________________________________________________________
00028 TVirtualPS::TVirtualPS()
00029 {
00030    // VirtualPS default constructor.
00031 
00032    fStream    = 0;
00033    fNByte     = 0;
00034    fSizBuffer = kMaxBuffer;
00035    fBuffer    = new char[fSizBuffer+1];
00036    fLenBuffer = 0;
00037    fPrinted   = kFALSE;
00038    fImplicitCREsc = 0;
00039 }
00040 
00041 
00042 //______________________________________________________________________________
00043 TVirtualPS::TVirtualPS(const char *name, Int_t)
00044           : TNamed(name,"Postscript interface")
00045 {
00046    // VirtualPS constructor.
00047 
00048    fStream    = 0;
00049    fNByte     = 0;
00050    fSizBuffer = kMaxBuffer;
00051    fBuffer    = new char[fSizBuffer+1];
00052    fLenBuffer = 0;
00053    fPrinted   = kFALSE;
00054    fImplicitCREsc = 0;
00055 }
00056 
00057 
00058 //______________________________________________________________________________
00059 TVirtualPS::~TVirtualPS()
00060 {
00061    // VirtualPS destructor
00062 
00063    if (fBuffer) delete [] fBuffer;
00064 }
00065 
00066 
00067 //______________________________________________________________________________
00068 void TVirtualPS::PrintStr(const char *str)
00069 {
00070    // Output the string str in the output buffer
00071 
00072    if (!str || !str[0])
00073       return;
00074    Int_t len = strlen(str);
00075    while (len) {
00076       if (str[0] == '@') {
00077          if (fLenBuffer) {
00078             fStream->write(fBuffer, fLenBuffer);
00079             fNByte += fLenBuffer;
00080             fLenBuffer = 0;
00081             fStream->write("\n", 1);
00082             fNByte++;
00083             fPrinted = kTRUE;
00084          }
00085          len--;
00086          str++;
00087       } else {
00088          Int_t lenText = len;
00089          if (str[len-1] == '@') lenText--;
00090          PrintFast(lenText, str);
00091          len -= lenText;
00092          str += lenText;
00093       }
00094    }
00095 }
00096 
00097 
00098 //______________________________________________________________________________
00099 void TVirtualPS::PrintFast(Int_t len, const char *str)
00100 {
00101    // Fast version of Print
00102    if (!len || !str) return;
00103    while ((len + fLenBuffer) > kMaxBuffer) {
00104       Int_t nWrite = kMaxBuffer;
00105       if (fImplicitCREsc) {
00106          if (fLenBuffer > 0) nWrite = fLenBuffer;
00107       } else {
00108          if ((len + fLenBuffer) > nWrite) {
00109             // Search for the nearest preceeding space to break a line, if there is no instruction to escape the <end-of-line>.
00110             while ((nWrite >= fLenBuffer) && (str[nWrite - fLenBuffer] != ' ')) nWrite--;
00111             if (nWrite < fLenBuffer) {
00112                while ((nWrite >= 0) && (fBuffer[nWrite] != ' ')) nWrite--;
00113             }
00114             if (nWrite <= 0) {
00115                // Cannot find a convenient place to break a line, so we just break at this location.
00116                nWrite = kMaxBuffer;
00117             }
00118          }
00119       }
00120       if (nWrite >= fLenBuffer) {
00121          if (fLenBuffer > 0) {
00122             fStream->write(fBuffer, fLenBuffer);
00123             fNByte += fLenBuffer;
00124             nWrite -= fLenBuffer;
00125             fLenBuffer = 0;
00126          }
00127          if (nWrite > 0) {
00128             fStream->write(str, nWrite);
00129             len -= nWrite;
00130             str += nWrite;
00131             fNByte += nWrite;
00132          }
00133       } else {
00134          if (nWrite > 0) {
00135             fStream->write(fBuffer, nWrite);
00136             fNByte += nWrite;
00137             memmove(fBuffer, fBuffer + nWrite, fLenBuffer - nWrite); // not strcpy because source and destination overlap
00138             fBuffer[fLenBuffer - nWrite] = 0; // not sure if this is needed, but just in case
00139             fLenBuffer -= nWrite;
00140          }
00141       }
00142       if (fImplicitCREsc) {
00143          // Write escape characters (if any) before an end-of-line is enforced.
00144          // For example, in PostScript the <new line> character must be escaped inside strings.
00145          Int_t crlen = strlen(fImplicitCREsc);
00146          fStream->write(fImplicitCREsc, crlen);
00147          fNByte += crlen;
00148       }
00149       fStream->write("\n",1);
00150       fNByte++;
00151    }
00152    if (len > 0) {
00153       strlcpy(fBuffer + fLenBuffer, str, len+1);
00154       fLenBuffer += len;
00155       fBuffer[fLenBuffer] = 0;
00156    }
00157    fPrinted = kTRUE;
00158 }
00159 
00160 
00161 //______________________________________________________________________________
00162 void TVirtualPS::WriteInteger(Int_t n, Bool_t space )
00163 {
00164    // Write one Integer to the file
00165    //
00166    // n: Integer to be written in the file.
00167    // space: If TRUE, a space in written before the integer.
00168 
00169    char str[15];
00170    if (space) {
00171       snprintf(str,15," %d", n);
00172    } else {
00173       snprintf(str,15,"%d", n);
00174    }
00175    PrintStr(str);
00176 }
00177 
00178 
00179 //______________________________________________________________________________
00180 void TVirtualPS::WriteReal(Float_t z)
00181 {
00182    // Write a Real number to the file
00183 
00184    char str[15];
00185    snprintf(str,15," %g", z);
00186    PrintStr(str);
00187 }

Generated on Tue Jul 5 14:11:24 2011 for ROOT_528-00b_version by  doxygen 1.5.1