ROOT  6.06/09
Reference Guide
TVirtualPS.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Rene Brun 05/09/99
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TVirtualPS
13 TVirtualPS is an abstract interface to Postscript, PDF, SVG. TeX etc... drivers
14 */
15 
16 #include "Riostream.h"
17 #include "TVirtualPS.h"
18 
19 TVirtualPS *gVirtualPS = 0;
20 
21 const Int_t kMaxBuffer = 250;
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// VirtualPS default constructor.
28 
30 {
31  fStream = 0;
32  fNByte = 0;
33  fSizBuffer = kMaxBuffer;
34  fBuffer = new char[fSizBuffer+1];
35  fLenBuffer = 0;
36  fPrinted = kFALSE;
37  fImplicitCREsc = 0;
38 }
39 
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// VirtualPS constructor.
43 
45  : TNamed(name,"Postscript interface")
46 {
47  fStream = 0;
48  fNByte = 0;
50  fBuffer = new char[fSizBuffer+1];
51  fLenBuffer = 0;
52  fPrinted = kFALSE;
53  fImplicitCREsc = 0;
54 }
55 
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// VirtualPS destructor
59 
61 {
62  if (fBuffer) delete [] fBuffer;
63 }
64 
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// Output the string str in the output buffer
68 
69 void TVirtualPS::PrintStr(const char *str)
70 {
71  if (!str || !str[0])
72  return;
73  Int_t len = strlen(str);
74  while (len) {
75  if (str[0] == '@') {
76  if (fLenBuffer) {
77  fStream->write(fBuffer, fLenBuffer);
78  fNByte += fLenBuffer;
79  fLenBuffer = 0;
80  fStream->write("\n", 1);
81  fNByte++;
82  fPrinted = kTRUE;
83  }
84  len--;
85  str++;
86  } else {
87  Int_t lenText = len;
88  if (str[len-1] == '@') lenText--;
89  PrintFast(lenText, str);
90  len -= lenText;
91  str += lenText;
92  }
93  }
94 }
95 
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Fast version of Print
99 
100 void TVirtualPS::PrintFast(Int_t len, const char *str)
101 {
102  if (!len || !str) return;
103  while ((len + fLenBuffer) > kMaxBuffer) {
104  Int_t nWrite = kMaxBuffer;
105  if (fImplicitCREsc) {
106  if (fLenBuffer > 0) nWrite = fLenBuffer;
107  } else {
108  if ((len + fLenBuffer) > nWrite) {
109  // Search for the nearest preceding space to break a line, if there is no instruction to escape the <end-of-line>.
110  while ((nWrite >= fLenBuffer) && (str[nWrite - fLenBuffer] != ' ')) nWrite--;
111  if (nWrite < fLenBuffer) {
112  while ((nWrite >= 0) && (fBuffer[nWrite] != ' ')) nWrite--;
113  }
114  if (nWrite <= 0) {
115  // Cannot find a convenient place to break a line, so we just break at this location.
116  nWrite = kMaxBuffer;
117  }
118  }
119  }
120  if (nWrite >= fLenBuffer) {
121  if (fLenBuffer > 0) {
122  fStream->write(fBuffer, fLenBuffer);
123  fNByte += fLenBuffer;
124  nWrite -= fLenBuffer;
125  fLenBuffer = 0;
126  }
127  if (nWrite > 0) {
128  fStream->write(str, nWrite);
129  len -= nWrite;
130  str += nWrite;
131  fNByte += nWrite;
132  }
133  } else {
134  if (nWrite > 0) {
135  fStream->write(fBuffer, nWrite);
136  fNByte += nWrite;
137  memmove(fBuffer, fBuffer + nWrite, fLenBuffer - nWrite); // not strcpy because source and destination overlap
138  fBuffer[fLenBuffer - nWrite] = 0; // not sure if this is needed, but just in case
139  fLenBuffer -= nWrite;
140  }
141  }
142  if (fImplicitCREsc) {
143  // Write escape characters (if any) before an end-of-line is enforced.
144  // For example, in PostScript the <new line> character must be escaped inside strings.
145  Int_t crlen = strlen(fImplicitCREsc);
146  fStream->write(fImplicitCREsc, crlen);
147  fNByte += crlen;
148  }
149  fStream->write("\n",1);
150  fNByte++;
151  }
152  if (len > 0) {
153  strlcpy(fBuffer + fLenBuffer, str, len+1);
154  fLenBuffer += len;
155  fBuffer[fLenBuffer] = 0;
156  }
157  fPrinted = kTRUE;
158 }
159 
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Write one Integer to the file
163 ///
164 /// n: Integer to be written in the file.
165 /// space: If TRUE, a space in written before the integer.
166 
168 {
169  char str[15];
170  if (space) {
171  snprintf(str,15," %d", n);
172  } else {
173  snprintf(str,15,"%d", n);
174  }
175  PrintStr(str);
176 }
177 
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Write a Real number to the file
181 
183 {
184  char str[15];
185  if (space) {
186  snprintf(str,15," %g", z);
187  } else {
188  snprintf(str,15,"%g", z);
189  }
190  PrintStr(str);
191 }
192 
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Print a raw
196 
197 void TVirtualPS::PrintRaw(Int_t len, const char *str)
198 {
199  fNByte += len;
200  if ((len + fLenBuffer) > kMaxBuffer - 1) {
201  fStream->write(fBuffer, fLenBuffer);
202  while(len > kMaxBuffer-1) {
203  fStream->write(str,kMaxBuffer);
204  len -= kMaxBuffer;
205  str += kMaxBuffer;
206  }
207  memcpy(fBuffer, str, len);
208  fLenBuffer = len;
209  } else {
210  memcpy(fBuffer + fLenBuffer, str, len);
211  fLenBuffer += len;
212  }
213  fPrinted = kTRUE;
214 }
char * fBuffer
Definition: TVirtualPS.h:52
float Float_t
Definition: RtypesCore.h:53
const char * fImplicitCREsc
Definition: TVirtualPS.h:53
virtual void WriteInteger(Int_t i, Bool_t space=kTRUE)
Write one Integer to the file.
Definition: TVirtualPS.cxx:167
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void WriteReal(Float_t r, Bool_t space=kTRUE)
Write a Real number to the file.
Definition: TVirtualPS.cxx:182
virtual void PrintStr(const char *string="")
Output the string str in the output buffer.
Definition: TVirtualPS.cxx:69
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
virtual ~TVirtualPS()
VirtualPS destructor.
Definition: TVirtualPS.cxx:60
Int_t fSizBuffer
Definition: TVirtualPS.h:49
Bool_t fPrinted
Definition: TVirtualPS.h:50
std::ofstream * fStream
Definition: TVirtualPS.h:51
ClassImp(TVirtualPS) TVirtualPS
VirtualPS default constructor.
Definition: TVirtualPS.cxx:23
virtual void PrintRaw(Int_t len, const char *str)
Print a raw.
Definition: TVirtualPS.cxx:197
virtual void PrintFast(Int_t nch, const char *string="")
Fast version of Print.
Definition: TVirtualPS.cxx:100
const Int_t kMaxBuffer
Definition: TVirtualPS.cxx:21
#define name(a, b)
Definition: linkTestLib0.cpp:5
Int_t fLenBuffer
Definition: TVirtualPS.h:48
TVirtualPS is an abstract interface to Postscript, PDF, SVG.
Definition: TVirtualPS.h:40
const Bool_t kTRUE
Definition: Rtypes.h:91
const Int_t n
Definition: legend1.C:16
Int_t fNByte
Definition: TVirtualPS.h:47