1
1
# ' Create a Temporary Text Plot
2
2
# '
3
- # ' This function creates a temporary bitmap image file containing a plot of the given text.
3
+ # ' This function creates a temporary png image file containing a plot of the
4
+ # ' given text.
4
5
# '
5
6
# ' @param text A character string to be plotted.
6
7
# ' @param cex A numeric value specifying the relative size of the text. Default is 4.
7
8
# '
8
9
# ' @return
9
- # ' A character vector containing the temporary image file.
10
+ # ' An array containing data from the temporary image file.
10
11
# '
11
- # ' @importFrom grDevices bitmap dev.off
12
+ # ' @importFrom grDevices png dev.off
12
13
# ' @importFrom graphics plot text
13
14
# '
14
15
# ' @noRd
@@ -22,13 +23,13 @@ temporary_text_plot <- function(text, cex = 4) {
22
23
23
24
# Create a temporary file path using a known directory
24
25
temp_dir <- tempdir()
25
- temp_file <- tempfile(tmpdir = temp_dir , fileext = " .ppm " )
26
+ temp_file <- tempfile(tmpdir = temp_dir , fileext = " .png " )
26
27
27
28
# Ensure the temporary file is removed when the function exits
28
29
on.exit(unlink(temp_file ))
29
30
30
31
# Create a bitmap image
31
- bitmap (temp_file , " ppm " , height = 5 , width = 5 )
32
+ png (temp_file , antialias = " none " )
32
33
33
34
# Create a blank plot
34
35
plot(1 , 1 , type = " n" , axes = FALSE , xlab = " " , ylab = " " )
@@ -40,7 +41,7 @@ temporary_text_plot <- function(text, cex = 4) {
40
41
dev.off()
41
42
42
43
# Read the image file
43
- image <- scan (temp_file , " " , sep = " \n " , quiet = TRUE )
44
+ image <- png :: readPNG (temp_file )
44
45
45
46
image
46
47
}
@@ -67,30 +68,21 @@ temporary_text_plot <- function(text, cex = 4) {
67
68
# ' image_data <- process_image(temp_file)
68
69
process_image <- function (image ) {
69
70
70
- # Extract image size from the third line
71
- size <- as.numeric(unlist(strsplit( image [ 3 ], " " )) )
71
+ # Convert the array to integer values between 0 and 255
72
+ img_array <- round( image * 255 )
72
73
73
- # Extract pixel data (skip first 4 lines of metadata)
74
- pixel_data <- image [- (1 : 4 )]
75
- pixel_data <- unlist(lapply(strsplit(pixel_data , " " ), as.numeric ))
76
-
77
- # Convert pixel data to a matrix
78
- pixel_matrix <- matrix (pixel_data , ncol = 3 , byrow = TRUE )
79
-
80
- # Convert to binary (text pixels are where any RGB value is 0)
81
- pixel_matrix <- pixel_matrix [,1 ] == 0 | pixel_matrix [,2 ] == 0 | pixel_matrix [,3 ] == 0
82
- pixel_matrix <- matrix (pixel_matrix , nrow = size [1 ], ncol = size [2 ], byrow = TRUE )
83
-
84
- # Remove empty rows and columns
85
- pixel_matrix <- pixel_matrix [apply(pixel_matrix , 1 , any ), ]
86
- pixel_matrix <- pixel_matrix [, apply(pixel_matrix , 2 , any )]
87
-
88
- # Generate x and y coordinates
89
- x <- col(pixel_matrix )
90
- y <- nrow(pixel_matrix ) + 1 - row(pixel_matrix )
74
+ # Find black points (where all channels are 0)
75
+ activated_points <- which(
76
+ img_array [,,1 ] == 0 &
77
+ img_array [,,2 ] == 0 &
78
+ img_array [,,3 ] == 0 ,
79
+ arr.ind = TRUE )
91
80
92
81
# Return coordinates of text pixels
93
- list (x = x [pixel_matrix ], y = y [pixel_matrix ])
82
+ list (
83
+ x = activated_points [, 2 ], # Column index represents x
84
+ y = nrow(img_array ) - activated_points [, 1 ] + 1 # Row index represents y, but flipped
85
+ )
94
86
}
95
87
96
88
# ' Apply the surreal method to a text string
@@ -106,10 +98,6 @@ process_image <- function(image) {
106
98
# ' @return
107
99
# ' A data.frame containing the results of the surreal method application.
108
100
# '
109
- # ' @details
110
- # ' This function is not supported on Windows due to the `ppm` image format
111
- # ' not being supported by the version of GhostScript included with R.
112
- # '
113
101
# ' @examples
114
102
# ' # Create a surreal plot of the text "R is fun" appearing on one line
115
103
# ' r_is_fun_result <- surreal_text("R is fun", verbose = TRUE)
@@ -128,11 +116,6 @@ surreal_text <- function(text = "hello world",
128
116
n_add_points = 40 ,
129
117
max_iter = 100 , tolerance = 0.01 , verbose = FALSE ) {
130
118
131
- if (.Platform $ OS.type != " unix" ) {
132
- message(" This function is only supported on macOS and Linux versions of R due to limitations in GhostScript." )
133
- return (NULL )
134
- }
135
-
136
119
# Create temporary plot of the text
137
120
temp_file <- temporary_text_plot(text = text , cex = cex )
138
121
0 commit comments