1#### PostScript Goodies f�r R --- `a la /u/sfs/S/ps.goodies.S
2####
3####
4
5## hidden in the name space -- FIXME? maybe more useful ?? ---
6dev.latex <-
7    function(file, DEV, height= 5+ main.space*1.25, width= 9.5,
8             main.space = FALSE, lab.space = main.space,
9             paper = "special", title = NULL,
10             lab = c(10, 10, 7), mgp.lab = c(1.6, 0.7, 0),
11             mar = c(4, 4, 0.9, 1.1), ...)
12{
13  ## Purpose: Setup for 1 LaTeX-includable picture SAVING on white space !
14  ##	Calls  ps.do(.) ; par(.)  [ old par in global 'o.p']; USE  ps.end() !
15  ## -------------------------------------------------------------------------
16  ## Arguments: height & width in INCHES.   (5, 9.5) is 'horizontal look'
17  ##		title: to be used in PostScript (-> for gv/ghostview !)
18  ##		main.space & lab.space: if T, leave space for 'main' & 'x/ylab'
19  ##		lab :  for  par(.);  (10,10,7): use more axis 'labels' ..
20  ##		mgp.lab & mar :	 for par(.): these are values for 'lab.space=T'
21  ## Note: FIRST fiddle with 'main.sp.', 'lab.sp.'  before 'mgp.lab' and 'mar'!
22  ## -------------------------------------------------------------------------
23  ## EXAMPLE:for(m in c(T,F)){str(ps.latex("q.ps",main=m));acf(hstart);ps.end()}
24  ## -------------------------------------------------------------------------
25  ## Author: Martin Maechler, Date: Sep 94; Sept. 95
26
27  ## Cannot use  missing(.) here, as all arg.s *are* specified
28  ## from the calling pdf.latex() etc ..
29  frms <- formals()
30  lab.def     <- identical(lab,     eval(frms[["lab"]]))
31  mar.def     <- identical(mar,     eval(frms[["mar"]]))
32  mgp.lab.def <- identical(mgp.lab, eval(frms[["mgp.lab"]]))
33
34  if(!lab.def && !(length(lab)==3 && is.numeric(lab) && all(lab >=0)))
35    stop("'lab' must be numeric vector >= 0, of length 3")
36  if(!mgp.lab.def && !(length(mgp.lab)==3 && is.numeric(mgp.lab) &&
37			    all(mgp.lab >=0) && all(diff(mgp.lab)<=0)))
38    stop("'mgp.lab' must be non-increasing numeric vector >= 0, of length 3")
39  if(!mar.def && !(length(mar)==4 && is.numeric(mar) && all(mar >=0)))
40    stop("'mar' must be non-negative numeric vector of length 4")
41
42  DEV(file=file, height=height, width=width, paper=paper, title = title, ...)
43  ##=
44
45  ## Now: just do the proper par(...) calls :
46  mar.main.Extra  <- c(0,0, 3.2,0)
47  mar.nolab.Minus <- c(1,1, 0.3,0)
48  if(main.space && mar.def)
49    mar <- mar + mar.main.Extra
50
51  if(!lab.space) {
52    mar <- mar - mar.nolab.Minus
53    if(main.space)
54      warning("'main.space' is TRUE, but 'lab.space' is FALSE ...")
55  }
56  o.p <- par(mar = mar, mgp= mgp.lab)
57  o.p <- c(o.p, par(lab=lab)) # need 2 step for	 bug ?
58  ## "frame 0 / GlobalEnv assignment deprecated: u.assign0("o.par.psl", o.p)
59  invisible(list(old.par=o.p, new.par= par(c("mar","mgp","lab"))))
60}
61
62ps.latex <- function(file, height= 5+ main.space*1.25, width= 9.5,
63		     main.space = FALSE, lab.space = main.space,
64		     paper = "special", title = NULL,
65		     lab = c(10, 10, 7), mgp.lab = c(1.6, 0.7, 0),
66		     mar = c(4, 4, 0.9, 1.1), ...)
67{
68  dev.latex(DEV = ps.do, file=file, height=height, width=width,
69            main.space=main.space, lab.space=lab.space, paper=paper,
70            title=title, lab=lab, mgp.lab=mgp.lab, mar=mar, ...)
71}
72
73pdf.latex <- function(file, height= 5+ main.space*1.25, width= 9.5,
74		     main.space = FALSE, lab.space = main.space,
75		     paper = "special", title = NULL,
76		     lab = c(10, 10, 7), mgp.lab = c(1.6, 0.7, 0),
77		     mar = c(4, 4, 0.9, 1.1), ...)
78{
79  dev.latex(DEV = pdf.do, file=file, height=height, width=width,
80            main.space=main.space, lab.space=lab.space, paper=paper,
81            title=title, lab=lab, mgp.lab=mgp.lab, mar=mar, ...)
82}
83
84
85ps.do <- local({
86    myfile <- NULL
87    function(file, width = -1, height = -1,
88		  onefile = FALSE, horizontal = FALSE, title = NULL, ...)
89{
90  ## Purpose: "Ghostview" device driver. --- to be "closed" by ps.end(..) --
91  ## -------------------------------------------------------------------------
92  ## Arguments: file, width, height : file name and dims in inch; 1 in:=2.54 cm
93  ##		onefile = F  <==> Encapsulated PS  (Splus default: T, simple PS)
94
95  ## -- new Args:  combining former   ps.do(.) and  ps.col(.) :
96
97  ##	...  :	passed to ps.options
98  ## -------------------------------------------------------------------------
99  ## Author: Martin Maechler, 1992-1995
100  ##
101  ## --->>>>>> CONSIDER	  'ps.latex'   instead	for pictures !
102
103    myfile <<- file
104##   if(length(l... <- list(...))) {
105##     ## This does NOT work : pso are the *NEW*, not the *former* ones!
106##     oldop <- ps.options()[names(l...)]
107##     ps.options(...)
108##     on.exit( do.call("ps.options", oldop) ) #- reset ps.options !
109##   }
110
111  if(is.null(title))
112      title <- paste("R", paste(R.version[c("major", "minor")], collapse = "."),
113		     "plot:", basename(file))
114  postscript(file = file, width = width, height = height, horizontal=horizontal,
115	     onefile = onefile, title = title, print.it = FALSE, ...)
116}## ps.do()
117})## local(..)
118
119ps.end <- function(call.gv = NULL, command = getOption("eps_view"),
120		   debug = getOption("verbose"))
121{
122    ## Purpose:	 A "ghostview" device driver (almost).
123    ## Author: Martin Maechler, Date:  May 26 1992, 15:32
124    ## ----------------------------------------------------------------
125    ## Arguments:   call.gv: If TRUE,  call ghostview.
126    ##	  Default:	  Find out if ghostview already runs on this file,
127    ##			  If yes, do not call it again.
128    ## MUST be called after ps.do(..) or ps.latex() !
129    ## Example:	 ps.end(com = "ghostview --media a4")
130    ## ----------------------------------------------------------------
131    ## Only if	postscript is running !! --
132    if( names(dev.cur()) == "postscript")
133	dev.off()
134    if(.Platform $ OS.type == "unix") {
135        .set.eps_view()
136    } else { ## OS.type != "unix"  --- i.e. Windows :
137	warning("using ps, ghostview,...is currently not implemented for non-Unix")
138	return(FALSE)
139    }
140    ..ps.file <- environment(ps.do)$myfile
141    if (is.null(call.gv)) {
142	f <- u.sys(Sys.ps.cmd(), " | grep '", command, "' | grep -v grep")
143	if(debug) { cat("ps.end(): f:\n");print(f) }
144	call.gv <- length(f) == 0
145	if(!call.gv) {
146	    ##--- STILL does NOT work
147	    ##--- if you work with two different pictures simultaneously
148	    for(i in 1:length(f)) { #-- only NOT call if THIS ps.file .. --
149		## find command in 'ps' output line (sub/gsub have no 'fixed=TRUE')
150		ic <- regexpr(command, f[i], fixed=TRUE)
151		## only keep the file name
152		fil <- substr(f[i], ic + attr(ic,"match.length") + 1, 1e4)
153		cat("ps.end(): fil:",fil,"\n")
154		call.gv <- length(fil) < 1 || all(..ps.file != fil)
155		if(!call.gv)
156		    break #-- don't  call ghostview since it runs this file..
157	    }
158	}
159    } else if(identical(call.gv, FALSE))
160	fil <- "<unknown>"
161    if (call.gv) {
162	fil <- ..ps.file
163	u.sys(command, " ", fil, "&", intern=FALSE)
164    } else
165    cat("\n >> switch to", sub(" .*", '', command),
166        "(postscript viewer) window -- updated automagically!\n\n")
167    invisible(fil)
168}
169
170
171###---  Using  pdf()  instead of postscript() --- otherwise "same" :
172
173pdf.do <- local({
174    myfile <- NULL
175    function(file, paper = "default",
176                   width = -1, height = -1, onefile = FALSE,
177                   title = NULL, version = "1.4", quiet=FALSE, ...)
178{
179  ## Purpose: "PDF + view" device driver. --- to be "closed" by pdf.end(..) --
180  ## -------------------------------------------------------------------------
181  ## Arguments: file, width, height : file name and dims in inch; 1 in:=2.54 cm
182  ##		onefile = FALSE <==> "Encapsulated"
183  ##	...  :	passed to pdf.options
184  ## -------------------------------------------------------------------------
185  ## Author: Martin Maechler, April 26, 2007 {built on much older ps.do()}
186
187##   if(length(l... <- list(...))) {
188##       ## ps.options also used for pdf -- in some way
189##     oldop <- ps.options()[names(l...)]
190##     ps.options(...)
191##     on.exit( do.call("ps.options", oldop) ) #- reset ps.options !
192##   }
193    myfile <<- file
194
195  if(missing(paper) && !missing(width) && !missing(height)) {
196      if(!quiet)
197	  message("as 'width' and 'height' are specified, setting 'paper = \"special\"")
198      paper <- "special"
199  }
200
201  if(is.null(title))
202      title <- paste("R", paste(R.version[c("major", "minor")], collapse = "."),
203		     "plot:", basename(file))
204  ## default for 'paper' is now 'missing'
205  pdf(file = file, version = version, paper = paper,
206      width = width, height = height,
207      onefile = onefile, title = title, ...)
208}## pdf.do()
209})## local(..)
210
211
212pdf.end <- function(call.viewer = NULL, command = getOption("pdfviewer"),
213		   debug = getOption("verbose"))
214{
215    ## Purpose:	 A "ghostview" device driver (almost).
216    ## Author: Martin Maechler, Date:  April 26, 2007
217    ## ----------------------------------------------------------------
218    ## Arguments:   call.viewer: If TRUE,  call ghostview.
219    ##	  Default:	  Find out if ghostview already runs on this file,
220    ##			  If yes, do not call it again.
221    ## MUST be called after pdf.do(..) or pdf.latex() !
222    ## Example:	 pdf.end(com = "acroread")
223    ## ----------------------------------------------------------------
224    ## Only if	postscript is running !! --
225    if( names(dev.cur()) == "pdf")
226	dev.off()
227    if(.Platform $ OS.type != "unix") {
228	warning("using ps (process status) is currently not implemented for non-Unix")
229	return(FALSE)
230    }
231    ..pdf.file <- environment(pdf.do)$myfile
232    if (is.null(call.viewer)) {
233        cmd <- basename(command)
234	f <- u.sys(Sys.ps.cmd(), " | grep '", cmd, "' | grep -v grep")
235	if(debug) { cat("pdf.end(): f:\n");print(f) }
236	call.viewer <- length(f) == 0
237	if(!call.viewer) {
238	    ##--- STILL does NOT work
239	    ##--- if you work with two different pictures simultaneously
240	    for(i in 1:length(f)) { #-- only NOT call if THIS pdf.file .. --
241		## find command in 'ps' output line (sub/gsub have no 'fixed=TRUE')
242		ic <- regexpr(cmd, f[i], fixed=TRUE)
243		## only keep the file name
244		fil <- substr(f[i], ic + attr(ic,"match.length") + 1, 1e4)
245		cat("pdf.end(): fil:",fil,"\n")
246		call.viewer <- length(fil) < 1 || all(..pdf.file != fil)
247		if(!call.viewer)
248		    break #-- don't  call ghostview since it runs this file..
249	    }
250	}
251    } else if(identical(call.viewer, FALSE))
252	fil <- "<unknown>"
253    if (call.viewer) {
254	fil <- ..pdf.file
255	u.sys(command, " ", fil, "&", intern=FALSE)
256    } else {
257	msg <- if(length(grep("acroread", command)))
258	    " acroread -- and refresh via C-w M-f 1 !"
259	else "	PDF viewer window and maybe refresh!"
260	cat("\n >> switch to", msg,"\n\n")
261    }
262    invisible(fil)
263}
264
265## Alain Hauser <alain@huschhus.ch> --> ../man/cairoSwd.Rd
266cairoSwd <- function(name, width, height, ...)
267  cairo_pdf(filename = paste(name, "pdf", sep = "."),
268            width = width, height = height)
269