]> git.donarmstrong.com Git - r/CairoHacks.git/blob - R/bookmarks.R
use grid:: and Cairo::
[r/CairoHacks.git] / R / bookmarks.R
1 CairoHacks.env <- new.env()
2 CairoHacks.env$device_set_up <- FALSE
3 CairoHacks.env$current_page <- 0
4
5 ##' Make a bookmark for a PDF for later saving to the pdf with
6 ##' \code{write.pdf.bookmarks}
7 ##'
8 ##' 
9 ##' @title make.bookmark
10 ##' @param text Text of bookmark to create
11 ##' @param level Optional bookmark level which defaults to 1. This
12 ##' enables you to have headings and sub-headings and so forth, simply
13 ##' by increasing the number passed.
14 ##' @param page Optional page number to write the bookmark to. If you
15 ##' are using a Cairo device, this is automatically set for you to be
16 ##' the page which you are currently writing to.
17 ##' @return list of bookmarks to be passed to write.bookmarks or
18 ##' further calls to save.bookmark
19 ##' @author Don Armstrong <don@@donarmstrong.com>
20 ##' @export
21 ##' @examples
22 ##' Cairo::CairoPDF(file="example.pdf",onefile=TRUE)
23 ##' plot(y~x,data.frame(x=1:5,y=1:5))
24 ##' bookmarks <- make.pdf.bookmark("First plot")
25 ##' plot(y~x,data.frame(x=1:5,y=1:5))
26 ##' bookmarks <- bookmarks + make.pdf.bookmark("Second plot")
27 ##' dev.off()
28 ##' write.pdf.bookmarks(file="example.pdf",bookmarks)
29 make.pdf.bookmark <- function(text,level=1,page=NULL) {
30     if (!CairoHacks.env$device_set_up) {
31         Cairo::Cairo.onSave(device = grDevices::dev.cur(),
32                      onSave=function(device,page){
33                          ch <- getNamespace("CairoHacks")
34                          print(ls(envir=ch))
35                          assign("current_page",
36                                 page,
37                                 envir=ch[["CairoHacks.env"]])
38                      })
39         CairoHacks.env$device_set_up <- TRUE
40     }
41     if (missing(page)|| is.null(page)) {
42         page <- CairoHacks.env$current_page
43     }
44     p <- structure(list(
45         bookmarks=list(
46             list(text=text,
47                  level=level,
48                  page=page))
49         ), class = c("CairoHacks_bookmark"))
50     return(p)
51 }
52 ##' Add additional bookmarks to a bookmark
53 ##'
54 ##' Given a bookmarks object created with make.pdf.bookmark, add more
55 ##' bookmarks to it.
56 ##' @title + CairoHacks_bookmark
57 ##' @param b1 An object of class CairoHacks_bookmark.
58 ##' @param b2 An object of class CairoHacks_bookmark
59 ##' @export
60 ##' @method + CairoHacks_bookmark
61 ##' @author Don Armstrong <don@@donarmstrong.com>
62 "+.CairoHacks_bookmark" <- function(b1,b2) {
63     b1[["bookmarks"]] <-
64         c(b1[["bookmarks"]],
65           b2[["bookmarks"]])
66     b1
67 }
68 ##' Write saved bookmarks to the PDF file which was generated
69 ##'
70 ##' Given a set of bookmarks generated with make.pdf.bookmark, write
71 ##' them out to a PDF file using pdftk.
72 ##' @title write.pdf.bookmarks
73 ##' @param file file name of pdf which was saved to disk
74 ##' @param bookmarks list of bookmarks
75 ##' @return list of bookmarks
76 ##' @author Don Armstrong <don@@donarmstrong.com>
77 ##' @export
78 write.pdf.bookmarks <- function(file,bookmarks) {
79     pdf.bookmarks <- ""
80     print(bookmarks[["bookmarks"]])
81     for (bookmark in 1:length(bookmarks[["bookmarks"]])) {
82         pdf.bookmarks <-
83             paste0(pdf.bookmarks,
84                    "BookmarkBegin\n",
85                    "BookmarkTitle: ",bookmarks[["bookmarks"]][[bookmark]][["text"]],"\n",
86                    "BookmarkLevel: ",bookmarks[["bookmarks"]][[bookmark]][["level"]],"\n",
87                    "BookmarkPageNumber: ",bookmarks[["bookmarks"]][[bookmark]][["page"]],"\n")
88     }
89     temp.pdf <- tempfile(pattern=basename(file))
90     temp.pdf.info <- tempfile(pattern=paste0(basename(file),"info_utf8"))
91     cat(file=temp.pdf.info,pdf.bookmarks)
92     system2("pdftk",c(file,'update_info_utf8',temp.pdf.info,'output',temp.pdf))
93     if (file.exists(temp.pdf)) {
94         file.rename(temp.pdf,file)
95     } else {
96         stop("unable to properly create bookmarks")
97     }
98     invisible(bookmarks)
99 }