Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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\ingroup Base
14\ingroup PS
15
16TVirtualPS is an abstract interface to Postscript, PDF, SVG. TeX etc... drivers
17*/
18
19#include <fstream>
20#include "strlcpy.h"
21#include "TVirtualPS.h"
22
24
25const Int_t kMaxBuffer = 250;
26
27
28
29////////////////////////////////////////////////////////////////////////////////
30/// VirtualPS default constructor.
31
33{
34 fStream = nullptr;
35 fNByte = 0;
37 fBuffer = new char[fSizBuffer+1];
38 fLenBuffer = 0;
40 fImplicitCREsc = nullptr;
41}
42
43
44////////////////////////////////////////////////////////////////////////////////
45/// VirtualPS constructor.
46
48 : TNamed(name,"Postscript interface")
49{
50 fStream = nullptr;
51 fNByte = 0;
53 fBuffer = new char[fSizBuffer+1];
54 fLenBuffer = 0;
56 fImplicitCREsc = nullptr;
57}
58
59
60////////////////////////////////////////////////////////////////////////////////
61/// VirtualPS destructor
62
64{
65 delete [] fBuffer;
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// Clear content of internal buffer
70
72{
73 for (Int_t i = 0; i < fSizBuffer; ++i)
74 fBuffer[i] = ' ';
75}
76
77
78////////////////////////////////////////////////////////////////////////////////
79/// Open output stream
80
82{
84
85 auto flags = std::ofstream::out;
86#ifdef R__WIN32
87 if (binary)
88 flags = flags | std::ofstream::binary;
89#else
90 (void) binary;
91#endif
92
93 fStream = new std::ofstream(fname, flags);
94 if (!fStream || !fStream->good()) {
95 delete fStream;
96 fStream = nullptr;
97 return kFALSE;
98 }
99
100 return kTRUE;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Close existing stream
105
107{
108 if (fStream) {
109 fStream->close();
110 delete fStream;
111 fStream = nullptr;
112 }
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Output the string str in the output buffer
117
118void TVirtualPS::PrintStr(const char *str)
119{
120 if (!str || !str[0])
121 return;
122 Int_t len = strlen(str);
123 while (len) {
124 if (str[0] == '@') {
125 if (fLenBuffer) {
126 fStream->write(fBuffer, fLenBuffer);
128 fLenBuffer = 0;
129 fStream->write("\n", 1);
130 fNByte++;
131 fPrinted = kTRUE;
132 }
133 len--;
134 str++;
135 } else {
136 Int_t lenText = len;
137 if (str[len-1] == '@') lenText--;
138 PrintFast(lenText, str);
139 len -= lenText;
140 str += lenText;
141 }
142 }
143}
144
145
146////////////////////////////////////////////////////////////////////////////////
147/// Fast version of Print
148
149void TVirtualPS::PrintFast(Int_t len, const char *str)
150{
151 if (!len || !str) return;
152 while ((len + fLenBuffer) > kMaxBuffer) {
154 if (fImplicitCREsc) {
155 if (fLenBuffer > 0) nWrite = fLenBuffer;
156 } else {
157 if ((len + fLenBuffer) > nWrite) {
158 // Search for the nearest preceding space to break a line, if there is no instruction to escape the <end-of-line>.
159 while ((nWrite >= fLenBuffer) && (str[nWrite - fLenBuffer] != ' ')) nWrite--;
160 if (nWrite < fLenBuffer) {
161 while ((nWrite >= 0) && (fBuffer[nWrite] != ' ')) nWrite--;
162 }
163 if (nWrite <= 0) {
164 // Cannot find a convenient place to break a line, so we just break at this location.
166 }
167 }
168 }
169 if (nWrite >= fLenBuffer) {
170 if (fLenBuffer > 0) {
171 fStream->write(fBuffer, fLenBuffer);
174 fLenBuffer = 0;
175 }
176 if (nWrite > 0) {
177 fStream->write(str, nWrite);
178 len -= nWrite;
179 str += nWrite;
180 fNByte += nWrite;
181 }
182 } else {
183 if (nWrite > 0) {
184 fStream->write(fBuffer, nWrite);
185 fNByte += nWrite;
186 memmove(fBuffer, fBuffer + nWrite, fLenBuffer - nWrite); // not strcpy because source and destination overlap
187 fBuffer[fLenBuffer - nWrite] = 0; // not sure if this is needed, but just in case
189 }
190 }
191 if (fImplicitCREsc) {
192 // Write escape characters (if any) before an end-of-line is enforced.
193 // For example, in PostScript the <new line> character must be escaped inside strings.
196 fNByte += crlen;
197 }
198 fStream->write("\n",1);
199 fNByte++;
200 }
201 if (len > 0) {
202 strlcpy(fBuffer + fLenBuffer, str, len+1);
203 fLenBuffer += len;
204 fBuffer[fLenBuffer] = 0;
205 }
206 fPrinted = kTRUE;
207}
208
209
210////////////////////////////////////////////////////////////////////////////////
211/// Write one Integer to the file
212///
213/// n: Integer to be written in the file.
214/// space: If TRUE, a space in written before the integer.
215
217{
218 char str[15];
219 if (space) {
220 snprintf(str,15," %d", n);
221 } else {
222 snprintf(str,15,"%d", n);
223 }
224 PrintStr(str);
225}
226
227
228////////////////////////////////////////////////////////////////////////////////
229/// Write a Real number to the file
230
232{
233 char str[15];
234 if (space) {
235 snprintf(str,15," %g", z);
236 } else {
237 snprintf(str,15,"%g", z);
238 }
239 PrintStr(str);
240}
241
242
243////////////////////////////////////////////////////////////////////////////////
244/// Print a raw
245
246void TVirtualPS::PrintRaw(Int_t len, const char *str)
247{
248 fNByte += len;
249 if ((len + fLenBuffer) > kMaxBuffer - 1) {
250 fStream->write(fBuffer, fLenBuffer);
251 while(len > kMaxBuffer-1) {
252 fStream->write(str,kMaxBuffer);
253 len -= kMaxBuffer;
254 str += kMaxBuffer;
255 }
256 memcpy(fBuffer, str, len);
257 fLenBuffer = len;
258 } else {
259 memcpy(fBuffer + fLenBuffer, str, len);
260 fLenBuffer += len;
261 }
262 fPrinted = kTRUE;
263}
264
265
266////////////////////////////////////////////////////////////////////////////////
267/// Print N segments
268/// \param [in] n number of segments
269/// \param [in] xw array of X coordinates, size 2*n
270/// \param [in] yw array of Y coordinates, size 2*n
271
273{
274 for(Int_t i = 0; i < 2*n; i += 2)
275 if ((xw[i] != xw[i+1]) || (yw[i] != yw[i+1]))
276 DrawPS(2, &xw[i], &yw[i]);
277}
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
char name[80]
Definition TGX11.cxx:148
TVirtualPS * gVirtualPS
const Int_t kMaxBuffer
#define snprintf
Definition civetweb.c:1579
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
TVirtualPS is an abstract interface to Postscript, PDF, SVG.
Definition TVirtualPS.h:30
Int_t fSizBuffer
Definition TVirtualPS.h:39
Int_t fLenBuffer
Definition TVirtualPS.h:38
virtual void WriteInteger(Int_t i, Bool_t space=kTRUE)
Write one Integer to the file.
virtual void PrintRaw(Int_t len, const char *str)
Print a raw.
void CloseStream()
Close existing stream.
virtual void DrawPS(Int_t n, Float_t *xw, Float_t *yw)=0
virtual void PrintStr(const char *string="")
Output the string str in the output buffer.
virtual void PrintFast(Int_t nch, const char *string="")
Fast version of Print.
std::ofstream * fStream
Definition TVirtualPS.h:41
Bool_t OpenStream(const char *fname, Bool_t binary=kFALSE)
Open output stream.
char * fBuffer
Definition TVirtualPS.h:42
TVirtualPS()
VirtualPS default constructor.
Int_t fNByte
Definition TVirtualPS.h:37
void ClearBuffer()
Clear content of internal buffer.
const char * fImplicitCREsc
Definition TVirtualPS.h:43
virtual void DrawSegments(Int_t n, Double_t *xw, Double_t *yw)
Print N segments.
virtual ~TVirtualPS()
VirtualPS destructor.
virtual void WriteReal(Float_t r, Bool_t space=kTRUE)
Write a Real number to the file.
Bool_t fPrinted
Definition TVirtualPS.h:40
const Int_t n
Definition legend1.C:16