You can use the library magick with an empty canvas - annotate text and trim of the margins and it will give

I tried methods described here and here but they used R's devices like tiff but the strwidth / strheight methods were very unsatisfactory, also aligning the text along a grid or even ggplot2 turned out to still leave unwanted margins or even cut off text.
words <- c("The", "Turing", "test,", "originally", "called", "the", "imitation",
"game", "by", "Alan", "Turing", "in", "1949,[2]", "is", "a",
"test", "of", "a", "machine's", "ability", "a", "b", "c", "d",
"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
"r", "s", "t", "u", "v", "w", "x", "y", "z")
library(magick)
text2tiff <- function(
text,
font_size = 500,
font_family = "Arial",
backgroundColor = "white",
textColor = "black")
{
width <- ceiling(strwidth(text, units = "inches", family = font_family,ps = par(ps = font_size)) * 96 + 200)
height <- ceiling(strheight(text, units = "inches", family = font_family, ps = par(ps = font_size)) * 96 + 200)
canvas <- image_blank(width = width, height = height, color = backgroundColor)
text_image <- image_annotate(canvas, # Annotate text
text,
font = font_family,
size = font_size,
color = textColor,
gravity = "center")
image_write(image_trim(text_image), # Trim image to remove margins
path = paste0(gsub("[^[:alnum:]]", "", text), ".tiff"), # take text as img name
format = "tiff")
}
for(i in 1:length(words)) text2tiff(words[i])
- For increased image quality, increase the fontsize
- Adjust the file naming, don't know how long your text / word snippets are, they might exceed your OS' maximum file name