Skip to content

Render text as nativeRaster images

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

coolbutuseless/naratext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

naratext

R-CMD-check

{naratext} renders text to nativeRaster images in memory.

This relies of glyph rendering features in the {grid} package.

What’s in the box

  • nr_text() to render text to in-memory nativeRaster images

ToDo

  • Reconcile the different font handling mechanisms between textshaping and grDevices::glyphFont()
  • Tightly crop the returned nativeRaster
  • Return baseline offset from bottom of raster so that other compositors (e.g. {nara}) now how to place text correctly
  • Move to textshaping’s C interface?

Installation from GitHub

Also requires installation of:

  • CRAN packages:
    • {textshaping} to calculate positions of rendered glyph
    • {ragg} as a graphics device that allows direct capture of the output to an image in memory (and supports glyph rendering)
    • ‘{bittermelon}’ for handling bitmap fonts
  • GitHub packages
    • {nara} for nativeRaster utilities
install.packages('remotes')
remotes::install_github('coolbutuseless/nara')
install.packages(c('textshaping', 'ragg'))

remotes::install_github('coolbutuseless/naratext')

Example - full font rendering

library(grid)
library(nara)
library(naratext)

strings <- c('Ping', ' #RStats')
font_files <- c(
  system.file("fonts", "Montserrat", "static", "Montserrat-BoldItalic.ttf", package="grDevices"),
  system.file("fonts", "Montserrat", "static", "Montserrat-Medium.ttf"    , package="grDevices")
)
color <- c('red', 'blue')
txt <- nr_text(strings, font_files = font_files, color = color, scale = 10, background = 'lightblue')
dim(txt)
#> [1] 147 801
class(txt)
#> [1] "nativeRaster"
grid.raster(txt, interpolate = FALSE)

Example - Bitmap font rendering

library(grid)
library(nara)
library(naratext)

font_file <- system.file("fonts/spleen/spleen-8x16.hex.gz", package = "bittermelon") 
font <- bittermelon::read_hex(font_file)
txt <- nr_text_bitmap("Ping #RStats", font = font, fill = 'lightpink')
dim(txt)
#> [1] 16 96
grid::grid.newpage(); grid::grid.raster(txt, interpolate = FALSE)

font <- bittermelon::read_monobit("~/projectsdata/spleen/spleen-32x64.bdf", quietly = TRUE)
txt <- nr_text_bitmap("Ping #RStats", font = font, fill = 'lightgreen')
dim(txt)
#> [1]  64 384
grid::grid.newpage(); grid::grid.raster(txt, interpolate = FALSE)

About

Render text as nativeRaster images

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages