# Using bitmap fonts

    library(lofifonts)

## Bitmap font: included fonts

    get_lofi_names('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 12    0  7 12
    #> 2525        1        72 2 12    0  2 12
    #> 2526        1        72 7 11    0  7 11
    #> 2527        1        72 2 11    0  2 11
    #> 2528        1        72 7 10    0  7 10
    #> 2529        1        72 2 10    0  2 10

    bitmap_text_raster("Hello", "unifont") |>
      plot()

## 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 23    0  5 11
    #> 517        1        72 1 23    0  1 11
    #> 518        1        72 5 22    0  5 10
    #> 519        1        72 1 22    0  1 10
    #> 520        1        72 5 21    0  5  9
    #> 521        1        72 1 21    0  1  9

    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)
    )