Logo ROOT  
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\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
28
29
30////////////////////////////////////////////////////////////////////////////////
31/// VirtualPS default constructor.
32
34{
35 fStream = nullptr;
36 fNByte = 0;
38 fBuffer = new char[fSizBuffer+1];
39 fLenBuffer = 0;
41 fImplicitCREsc = nullptr;
42}
43
44
45////////////////////////////////////////////////////////////////////////////////
46/// VirtualPS constructor.
47
49 : TNamed(name,"Postscript interface")
50{
51 fStream = nullptr;
52 fNByte = 0;
54 fBuffer = new char[fSizBuffer+1];
55 fLenBuffer = 0;
57 fImplicitCREsc = nullptr;
58}
59
60
61////////////////////////////////////////////////////////////////////////////////
62/// VirtualPS destructor
63
65{
66 if (fBuffer) delete [] fBuffer;
67}
68
69
70////////////////////////////////////////////////////////////////////////////////
71/// Output the string str in the output buffer
72
73void TVirtualPS::PrintStr(const char *str)
74{
75 if (!str || !str[0])
76 return;
77 Int_t len = strlen(str);
78 while (len) {
79 if (str[0] == '@') {
80 if (fLenBuffer) {
81 fStream->write(fBuffer, fLenBuffer);
83 fLenBuffer = 0;
84 fStream->write("\n", 1);
85 fNByte++;
87 }
88 len--;
89 str++;
90 } else {
91 Int_t lenText = len;
92 if (str[len-1] == '@') lenText--;
93 PrintFast(lenText, str);
94 len -= lenText;
95 str += lenText;
96 }
97 }
98}
99
100
101////////////////////////////////////////////////////////////////////////////////
102/// Fast version of Print
103
104void TVirtualPS::PrintFast(Int_t len, const char *str)
105{
106 if (!len || !str) return;
107 while ((len + fLenBuffer) > kMaxBuffer) {
108 Int_t nWrite = kMaxBuffer;
109 if (fImplicitCREsc) {
110 if (fLenBuffer > 0) nWrite = fLenBuffer;
111 } else {
112 if ((len + fLenBuffer) > nWrite) {
113 // Search for the nearest preceding space to break a line, if there is no instruction to escape the <end-of-line>.
114 while ((nWrite >= fLenBuffer) && (str[nWrite - fLenBuffer] != ' ')) nWrite--;
115 if (nWrite < fLenBuffer) {
116 while ((nWrite >= 0) && (fBuffer[nWrite] != ' ')) nWrite--;
117 }
118 if (nWrite <= 0) {
119 // Cannot find a convenient place to break a line, so we just break at this location.
120 nWrite = kMaxBuffer;
121 }
122 }
123 }
124 if (nWrite >= fLenBuffer) {
125 if (fLenBuffer > 0) {
126 fStream->write(fBuffer, fLenBuffer);
128 nWrite -= fLenBuffer;
129 fLenBuffer = 0;
130 }
131 if (nWrite > 0) {
132 fStream->write(str, nWrite);
133 len -= nWrite;
134 str += nWrite;
135 fNByte += nWrite;
136 }
137 } else {
138 if (nWrite > 0) {
139 fStream->write(fBuffer, nWrite);
140 fNByte += nWrite;
141 memmove(fBuffer, fBuffer + nWrite, fLenBuffer - nWrite); // not strcpy because source and destination overlap
142 fBuffer[fLenBuffer - nWrite] = 0; // not sure if this is needed, but just in case
143 fLenBuffer -= nWrite;
144 }
145 }
146 if (fImplicitCREsc) {
147 // Write escape characters (if any) before an end-of-line is enforced.
148 // For example, in PostScript the <new line> character must be escaped inside strings.
149 Int_t crlen = strlen(fImplicitCREsc);
150 fStream->write(fImplicitCREsc, crlen);
151 fNByte += crlen;
152 }
153 fStream->write("\n",1);
154 fNByte++;
155 }
156 if (len > 0) {
157 strlcpy(fBuffer + fLenBuffer, str, len+1);
158 fLenBuffer += len;
159 fBuffer[fLenBuffer] = 0;
160 }
161 fPrinted = kTRUE;
162}
163
164
165////////////////////////////////////////////////////////////////////////////////
166/// Write one Integer to the file
167///
168/// n: Integer to be written in the file.
169/// space: If TRUE, a space in written before the integer.
170
172{
173 char str[15];
174 if (space) {
175 snprintf(str,15," %d", n);
176 } else {
177 snprintf(str,15,"%d", n);
178 }
179 PrintStr(str);
180}
181
182
183////////////////////////////////////////////////////////////////////////////////
184/// Write a Real number to the file
185
187{
188 char str[15];
189 if (space) {
190 snprintf(str,15," %g", z);
191 } else {
192 snprintf(str,15,"%g", z);
193 }
194 PrintStr(str);
195}
196
197
198////////////////////////////////////////////////////////////////////////////////
199/// Print a raw
200
201void TVirtualPS::PrintRaw(Int_t len, const char *str)
202{
203 fNByte += len;
204 if ((len + fLenBuffer) > kMaxBuffer - 1) {
205 fStream->write(fBuffer, fLenBuffer);
206 while(len > kMaxBuffer-1) {
207 fStream->write(str,kMaxBuffer);
208 len -= kMaxBuffer;
209 str += kMaxBuffer;
210 }
211 memcpy(fBuffer, str, len);
212 fLenBuffer = len;
213 } else {
214 memcpy(fBuffer + fLenBuffer, str, len);
215 fLenBuffer += len;
216 }
217 fPrinted = kTRUE;
218}
int Int_t
Definition: CPyCppyy.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:101
bool Bool_t
Definition: RtypesCore.h:63
float Float_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:364
TVirtualPS * gVirtualPS
Definition: TVirtualPS.cxx:23
const Int_t kMaxBuffer
Definition: TVirtualPS.cxx:25
#define snprintf
Definition: civetweb.c:1540
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.
Definition: TVirtualPS.cxx:171
virtual void PrintRaw(Int_t len, const char *str)
Print a raw.
Definition: TVirtualPS.cxx:201
virtual void PrintStr(const char *string="")
Output the string str in the output buffer.
Definition: TVirtualPS.cxx:73
virtual void PrintFast(Int_t nch, const char *string="")
Fast version of Print.
Definition: TVirtualPS.cxx:104
std::ofstream * fStream
Definition: TVirtualPS.h:41
char * fBuffer
Definition: TVirtualPS.h:42
TVirtualPS()
VirtualPS default constructor.
Definition: TVirtualPS.cxx:33
Int_t fNByte
Definition: TVirtualPS.h:37
const char * fImplicitCREsc
Definition: TVirtualPS.h:43
virtual ~TVirtualPS()
VirtualPS destructor.
Definition: TVirtualPS.cxx:64
virtual void WriteReal(Float_t r, Bool_t space=kTRUE)
Write a Real number to the file.
Definition: TVirtualPS.cxx:186
Bool_t fPrinted
Definition: TVirtualPS.h:40
const Int_t n
Definition: legend1.C:16
static const std::string name("name")