Using bitmap fonts

library(lofifonts)

Bitmap font: included fonts

names(font_info$bitmap)
#>  [1] "spleen-12x24"  "spleen-16x32"  "spleen-32x64"  "spleen-5x8"   
#>  [5] "spleen-6x12"   "spleen-8x16"   "Tamzen10x20b"  "Tamzen10x20r" 
#>  [9] "Tamzen5x9b"    "Tamzen5x9r"    "Tamzen6x12b"   "Tamzen6x12r"  
#> [13] "Tamzen7x13b"   "Tamzen7x13r"   "Tamzen7x14b"   "Tamzen7x14r"  
#> [17] "Tamzen8x15b"   "Tamzen8x15r"   "Tamzen8x16b"   "Tamzen8x16r"  
#> [21] "unifont"       "unscii-8"      "unscii-8-thin"
Type Name Sizes Unicode? # glyphs
Bitmap Spleen 5x8, 6x12, 8x16, 12x24, 16x32 Some 450-1000
Bitmap Unifont 16x16 Yes. Plane 0, 1 113446
Bitmap Unscii 8x8, 8x16 Some 3240

Bitmap font functions

  • bitmap_text_coords() returns a data.frame of pixel locations
  • bitmap_text_matrix() returns a binary matrix with pixel locations set to 1
  • bitmap_text_raster() returns a raster image of the text

Bitmap font: Rendering text

Text may be rendered with a bitmap font to

  1. A data.frame of pixel locations
  2. A binary matrix of pixel locations
  3. A simple raster object
library(lofifonts)
bitmap_text_coords("Hello", font = 'unifont') |>
  head()
#>      char_idx codepoint x  y line x0 y0
#> 2524        1        72 7 10    0  7 10
#> 2525        1        72 2 10    0  2 10
#> 2526        1        72 7  9    0  7  9
#> 2527        1        72 2  9    0  2  9
#> 2528        1        72 7  8    0  7  8
#> 2529        1        72 2  8    0  2  8
bitmap_text_matrix("Hello", "unifont") 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0
#>  [2,]    0    1    0    0    0    0    1    0    0     0     0     0     0
#>  [3,]    0    1    0    0    0    0    1    0    0     0     0     0     0
#>  [4,]    0    1    0    0    0    0    1    0    0     0     1     1     1
#>  [5,]    0    1    0    0    0    0    1    0    0     1     0     0     0
#>  [6,]    0    1    1    1    1    1    1    0    0     1     0     0     0
#>  [7,]    0    1    0    0    0    0    1    0    0     1     1     1     1
#>  [8,]    0    1    0    0    0    0    1    0    0     1     0     0     0
#>  [9,]    0    1    0    0    0    0    1    0    0     1     0     0     0
#> [10,]    0    1    0    0    0    0    1    0    0     1     0     0     0
#> [11,]    0    1    0    0    0    0    1    0    0     0     1     1     1
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
#>  [1,]     0     0     0     0     0     0     1     1     0     0     0     0
#>  [2,]     0     0     0     0     0     0     0     1     0     0     0     0
#>  [3,]     0     0     0     0     0     0     0     1     0     0     0     0
#>  [4,]     1     0     0     0     0     0     0     1     0     0     0     0
#>  [5,]     0     1     0     0     0     0     0     1     0     0     0     0
#>  [6,]     0     1     0     0     0     0     0     1     0     0     0     0
#>  [7,]     1     1     0     0     0     0     0     1     0     0     0     0
#>  [8,]     0     0     0     0     0     0     0     1     0     0     0     0
#>  [9,]     0     0     0     0     0     0     0     1     0     0     0     0
#> [10,]     0     1     0     0     0     0     0     1     0     0     0     0
#> [11,]     1     0     0     0     0     1     1     1     1     1     0     0
#>       [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37]
#>  [1,]     0     0     1     1     0     0     0     0     0     0     0     0
#>  [2,]     0     0     0     1     0     0     0     0     0     0     0     0
#>  [3,]     0     0     0     1     0     0     0     0     0     0     0     0
#>  [4,]     0     0     0     1     0     0     0     0     0     1     1     1
#>  [5,]     0     0     0     1     0     0     0     0     1     0     0     0
#>  [6,]     0     0     0     1     0     0     0     0     1     0     0     0
#>  [7,]     0     0     0     1     0     0     0     0     1     0     0     0
#>  [8,]     0     0     0     1     0     0     0     0     1     0     0     0
#>  [9,]     0     0     0     1     0     0     0     0     1     0     0     0
#> [10,]     0     0     0     1     0     0     0     0     1     0     0     0
#> [11,]     0     1     1     1     1     1     0     0     0     1     1     1
#>       [,38] [,39]
#>  [1,]     0     0
#>  [2,]     0     0
#>  [3,]     0     0
#>  [4,]     1     0
#>  [5,]     0     1
#>  [6,]     0     1
#>  [7,]     0     1
#>  [8,]     0     1
#>  [9,]     0     1
#> [10,]     0     1
#> [11,]     1     0
bitmap_text_raster("Hello", "unifont") |>
  plot(interpolate = FALSE)

Bitmap font: Bespoke pixel rendering

This is an example of bespoke rendering of the pixel data for an example string.

Each pixel in the coords data.frame has an x and y coordinates, and to render this font, a small square will be drawn for each pixel.

A gap will be left between the rectangels to highlight the pixelated origin of the text.

library(grid)
coords <- bitmap_text_coords("Hello\n#RStats", "spleen-6x12")
head(coords)
#>     char_idx codepoint x  y line x0 y0
#> 516        1        72 5 20    0  5  8
#> 517        1        72 1 20    0  1  8
#> 518        1        72 5 19    0  5  7
#> 519        1        72 1 19    0  1  7
#> 520        1        72 5 18    0  5  6
#> 521        1        72 1 18    0  1  6
grid.newpage()
grid.rect(
  x = coords$x * 4, 
  y = coords$y * 4, 
  width  = 3, 
  height = 3, 
  default.units = 'mm',
  gp = gpar(fill = rainbow(nrow(coords)), col = NA)
)