/
PDFStreamX.R
507 lines (420 loc) · 15 KB
/
PDFStreamX.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
valid_pdf_fonts <- c(
'Times-Roman',
'Times-Bold',
'Times-Italic',
'Times-BoldItalic',
'Helvetica',
'Helvetica-Bold',
'Helvetica-Oblique',
'Helvetica-BoldOblique',
'Courier',
'Courier-Bold',
'Courier-Oblique',
'Courier-BoldOblique',
'Symbol',
'ZapfDingbats'
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' PDF Text stream object creator
#'
#' This is the R6 class representing the text stream object.
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDFText <- R6::R6Class(
"PDFText", inherit = PDFStream,
public = list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Initialize a stream object representing a text
#'
#' @param text text
#' @param x,y location
#' @param ... initial named attributes of this object
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initialize = function(text, x, y, ...) {
self$attrib <- list(
text = text,
x = x,
y = y
)
super$initialize(...)
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Fetch the character representation of this geometry
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_geom_spec = function() {
attrib <- self$get_attrib()
trimws(glew(
"
BT
/F1 {fontsize} Tf
{x} {y} Td
{text_mode} Tr
({text}) Tj
ET"
,
attrib))
}
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' PDF Line stream object creator
#'
#' This is the R6 class representing the line stream object.
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDFLine <- R6::R6Class(
"PDFLine", inherit = PDFStream,
public = list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Initialize a stream object representing a line
#'
#'@param x1,y1,x2,y2 line start/end coordinates
#' @param ... initial named attributes of this object
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initialize = function(x1, y1, x2, y2, ...) {
self$attrib <- list(
x1 = x1,
y1 = y1,
x2 = x2,
y2 = y2
)
super$initialize(...)
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Fetch the character representation of this geometry
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_geom_spec = function() {
attrib <- self$get_attrib()
glew(
"{x1} {y1} m {x2} {y2} l s",
attrib
)
}
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' PDF Rect stream object creator
#'
#' This is the R6 class representing the rect stream object.
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDFRect <- R6::R6Class(
"PDFRect", inherit = PDFStream,
public = list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Initialize a stream object representing a rectangle
#'
#' @param x,y,width,height specificaiton of rectangle extents
#' @param ... initial named attributes of this object
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initialize = function(x, y, width, height, ...) {
self$attrib <- list(
x = x,
y = y,
width = width,
height = height
)
super$initialize(...)
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Fetch the character representation of this geometry
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_geom_spec = function() {
attrib <- self$get_attrib()
attrib$paint <- self$paint_spec
glew(
"{x} {y} {width} {height} re {paint}",
attrib
)
}
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' PDF Polyline stream object creator
#'
#' This is the R6 class representing the polyline stream object.
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDFPolyline <- R6::R6Class(
"PDFPolyline", inherit = PDFStream,
public = list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Initialize a stream object representing a polyline
#'
#' @param xs,ys numeric vectors of x, ycoordinates along polyline
#' @param ... initial named attributes of this object
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initialize = function(xs, ys, ...) {
stopifnot(length(xs) > 0, length(xs) == length(ys), is.numeric(xs), is.numeric(ys))
self$attrib <- list(
xs = xs,
ys = ys
)
super$initialize(...)
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Fetch the character representation of this geometry
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_geom_spec = function() {
attrib <- self$get_attrib()
lines <- paste(attrib$xs[-1], attrib$ys[-1], 'l', collapse = ' ')
attrib$lines <- lines
attrib$paint <- self$paint_spec
glew(
"{xs[1]} {ys[1]} m {lines} S",
attrib
)
}
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' PDF Polygon stream object creator
#'
#' This is the R6 class representing the polygon stream object.
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDFPolygon <- R6::R6Class(
"PDFPolygon", inherit = PDFStream,
public = list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Initialize a stream object representing a polygon
#'
#' @param xs,ys coordinates of polygon vertices
#' @param ... initial named attributes of this object
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initialize = function(xs, ys, ...) {
self$attrib <- list(
xs = xs,
ys = ys
)
super$initialize(...)
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Fetch the character representation of this geometry
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_geom_spec = function() {
attrib <- self$get_attrib()
lines <- paste(attrib$xs[-1], attrib$ys[-1], 'l', collapse = ' ')
attrib$lines <- lines
attrib$paint <- self$paint_spec
glew(
"{xs[1]} {ys[1]} m {lines} {paint}",
attrib
)
}
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' PDF Circle stream object creator
#'
#' This is the R6 class representing the circle stream object.
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDFCircle <- R6::R6Class(
"PDFCircle", inherit = PDFStream,
public = list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Initialize a stream object representing a circle
#'
#' @param x,y centre of circle
#' @param r radius
#' @param ... initial named attributes of this object
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initialize = function(x, y, r, ...) {
self$attrib <- list(
x = x,
y = y,
r = r
)
super$initialize(...)
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Fetch the character representation of this geometry
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_geom_spec = function() {
attrib <- self$get_attrib()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Bezier offset. See:
# stackoverflow.com/questions/1734745/how-to-create-circle-with-b%C3%A9zier-curves
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
attrib$b <- 0.552284749831 * attrib$r
attrib$paint <- self$paint_spec
trimws(glew(
"
{x+r} {y} m
{x+r} {y+b} {x+b} {y+r} {x} {y+r} c
{x-b} {y+r} {x-r} {y+b} {x-r} {y} c
{x-r} {y-b} {x-b} {y-r} {x} {y-r} c
{x+b} {y-r} {x+r} {y-b} {x+r} {y} c
{paint}",
attrib))
}
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' PDF ClipRect stream object creator
#'
#' This is the R6 class representing the ClipRect stream object.
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDFClipRect <- R6::R6Class(
"PDFClipRect", inherit = PDFStream,
public = list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Initialize a stream object representing a clipping rectangle
#'
#' @param x,y,width,height rectangle definition
#' @param ... initial named attributes of this object
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initialize = function(x, y, width, height, ...) {
self$attrib <- list(
x = x,
y = y,
width = width,
height = height,
new_graphics_state = FALSE
)
super$initialize(...)
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Fetch the character representation of this geometry
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_geom_spec = function() {
attrib <- self$get_attrib()
trimws(glew(
"
{x } {y } m
{x + width} {y } l
{x + width} {y + height} l
{x } {y + height} l
{x } {y } l W n
", attrib
))
}
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' PDF ClipPolygon stream object creator
#'
#' This is the R6 class representing the ClipPolygon stream object.
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDFClipPolygon <- R6::R6Class(
"PDFClipPolygon", inherit = PDFStream,
public = list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Initialize a stream object representing a cliping polygon
#'
#' @param xs,ys coordinates of polygon vertices
#' @param ... initial named attributes of this object
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initialize = function(xs, ys, ...) {
self$attrib <- list(
xs = xs,
ys = ys,
new_graphics_state = FALSE
)
super$initialize(...)
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Fetch the character representation of this geometry
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_geom_spec = function() {
attrib <- self$get_attrib()
lines <- paste(attrib$xs[-1], attrib$ys[-1], 'l', collapse = ' ')
attrib$lines <- lines
glew(
"{xs[1]} {ys[1]} m {lines} W n",
attrib
)
}
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' PDF Custom stream object creator
#'
#' This is the R6 class representing the custom stream object.
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDFCustom <- R6::R6Class(
"PDFCustom", inherit = PDFStream,
public = list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Initialize a stream object representing a custom object with text
#'
#' @param text text string
#' @param new_graphics_state Should the object be drawn in its own local
#' graphics state? default: TRUE
#' @param ... initial named attributes of this object
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
initialize = function(text, new_graphics_state = TRUE, ...) {
self$attrib <- list(
text = text,
new_graphics_state = new_graphics_state
)
super$initialize(...)
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Update the attributes of this object
#'
#' @param text text
#' @param ... other atttributes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
update = function(text, ...) {
self$attrib$text <- text
invisible(self)
},
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Fetch the character representation of this geometry
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_geom_spec = function() {
attrib <- self$get_attrib()
as.character(attrib$text)
}
)
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Helper for building PDF Stream Objects e.g. Rect, Lines
#'
#'For documentation, see manual for the related R6 function e.g. `PDFRect`
#'
#' @examples
#' \dontrun{
#' ptag$rect(x = 0, y = 0, width = 100, height = 100)
#' ptag$circle(x = 100, y = 100, r = 10, fill = 'black')
#' }
#'
#' @seealso PDFText PDFRect PDFLine PDFPolyline PDFPolygon PDFCircle PDFCustom
#' PDFClipRect PDFClipPolygon
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ptag <- list(
rect = function(x, y, width, height , ...) {do.call(PDFRect$new , find_args(...))},
line = function(x1, y1, x2, y2 , ...) {do.call(PDFLine$new , find_args(...))},
text = function(text, x, y, fontsize, text_mode, ...) {do.call(PDFText$new , find_args(...))},
circle = function(x, y, r , ...) {do.call(PDFCircle$new , find_args(...))},
polygon = function(xs, ys , ...) {do.call(PDFPolygon$new , find_args(...))},
polyline = function(xs, ys , ...) {do.call(PDFPolyline$new , find_args(...))},
custom = function(text , ...) {do.call(PDFCustom$new , find_args(...))}
# clip_rect = function(x, y, width, height , ...) {do.call(PDFClipRect$new , find_args(...))},
# clip_polygon = function(x, y, width, height , ...) {do.call(PDFClipPolygon$new, find_args(...))},
)