Title: | Text Rendering with Bitmap and Vector Fonts |
---|---|
Description: | Alternate font rendering is useful when rendering text to novel graphics outputs where modern font rendering is not available or where bespoke text positioning is required. Bitmap and vector fonts allow for custom layout and rendering using pixel coordinates and line drawing. Formatted text is created as a data.frame of pixel coordinates (for bitmap fonts) or stroke coordinates (for vector fonts). All text can be easily previewed as a matrix or raster image. A selection of fonts is included with this package. |
Authors: | Mike Cheng [aut, cre, cph], June Choe [ctb] (Contributed character positioning code), Frederic Cambus [cph, tyd] (Creator of 'spleen' font), GNU Unifont authors [cph, tyd] (Creators of 'unifont'), Suraj Kurapati [cph, tyd] (Creator of 'Tamzen' font), Scott Fial [cph, tyd] (Creator of 'Tamsyn' font upon which 'Tamzen' is based), Anders Hoff [cph, tyd] (Creator of 'gridfont' font) |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2024-11-07 00:51:25 UTC |
Source: | https://github.com/coolbutuseless/lofifonts |
Create a data.frame of pixel coordinate information of the rendered text
bitmap_text_coords(text, font = "unifont", dx = 0L, dy = 0L, missing = NULL)
bitmap_text_coords(text, font = "unifont", dx = 0L, dy = 0L, missing = NULL)
text |
Single text string. Can include carriage returns to split text over multiple lines. |
font |
Name of bitmap font, or a 'lofi' font object. Default: 'unifont'.
Use |
dx |
Additional character spacing in the horizontal direction. Default: 0 |
dy |
Additional character spacing in the vertical direction i.e. between rows of text. Default: 0 |
missing |
Codepoint to use if glyph not found in font. Default: NULL means to use the default specified by the font internally. Otherwise it will default to the codepoint for '?' |
data.frame of coordinate information
char_idx
The index of the glyph within the provided text
string
codepoint
Unicode codepoint (integer)
x
Pixel coordinate x value for display
y
Pixel coordinate y value for display
line
Line number within input text
where this character appears
x0
Original untransformed x-coordinate
y0
Original untransformed y-coordinate
Other bitmap text functions:
bitmap_text_matrix()
,
bitmap_text_raster()
bitmap_text_coords('Hi')
bitmap_text_coords('Hi')
Create a binary matrix of the rendered text
bitmap_text_matrix( text, font = "unifont", dx = 0L, dy = 0L, scale_matrix = 1, missing = NULL )
bitmap_text_matrix( text, font = "unifont", dx = 0L, dy = 0L, scale_matrix = 1, missing = NULL )
text |
Single text string. Can include carriage returns to split text over multiple lines. |
font |
Name of bitmap font, or a 'lofi' font object. Default: 'unifont'.
Use |
dx |
Additional character spacing in the horizontal direction. Default: 0 |
dy |
Additional character spacing in the vertical direction i.e. between rows of text. Default: 0 |
scale_matrix |
Integer size scale factor. Default: 1. Must be an integer value >= 1. Scale up the matrix or raster result by this factor |
missing |
Codepoint to use if glyph not found in font. Default: NULL means to use the default specified by the font internally. Otherwise it will default to the codepoint for '?' |
Binary matrix representation of the rendered text
Other bitmap text functions:
bitmap_text_coords()
,
bitmap_text_raster()
bitmap_text_matrix('Hi')
bitmap_text_matrix('Hi')
Create a raster image of the rendered text
bitmap_text_raster( text, font = "unifont", dx = 0L, dy = 0L, scale_matrix = 1, missing = NULL )
bitmap_text_raster( text, font = "unifont", dx = 0L, dy = 0L, scale_matrix = 1, missing = NULL )
text |
Single text string. Can include carriage returns to split text over multiple lines. |
font |
Name of bitmap font, or a 'lofi' font object. Default: 'unifont'.
Use |
dx |
Additional character spacing in the horizontal direction. Default: 0 |
dy |
Additional character spacing in the vertical direction i.e. between rows of text. Default: 0 |
scale_matrix |
Integer size scale factor. Default: 1. Must be an integer value >= 1. Scale up the matrix or raster result by this factor |
missing |
Codepoint to use if glyph not found in font. Default: NULL means to use the default specified by the font internally. Otherwise it will default to the codepoint for '?' |
Raster image representation of the rendered text
Other bitmap text functions:
bitmap_text_coords()
,
bitmap_text_matrix()
ras <- bitmap_text_raster('Hi') plot(ras)
ras <- bitmap_text_raster('Hi') plot(ras)
Convert a 'bittermelon' 'bm_font' to a lofi font
convert_bm_font_to_lofi(font, font_name = "Unknown")
convert_bm_font_to_lofi(font, font_name = "Unknown")
font |
font object of class |
font_name |
Name of font |
lofi font object
filename <- system.file("fonts/spleen/spleen-8x16.hex.gz", package = "bittermelon") bmfont <- bittermelon::read_hex(filename) lofi <- convert_bm_font_to_lofi(bmfont) lofi
filename <- system.file("fonts/spleen/spleen-8x16.hex.gz", package = "bittermelon") bmfont <- bittermelon::read_hex(filename) lofi <- convert_bm_font_to_lofi(bmfont) lofi
Fetch an included 'lofi' font
get_lofi_font(font_name)
get_lofi_font(font_name)
font_name |
Name of font e.g. 'unifont' |
'lofi' font object. See lofi
for details of this
data structure
get_lofi_font('unifont')
get_lofi_font('unifont')
Return the names of all included fonts
get_lofi_names(type)
get_lofi_names(type)
type |
font type. Either 'bitmap', 'vector' or 'all' |
List of two elements: names of bitmap fonts, names of vector fonts
get_lofi_names('bitmap')
get_lofi_names('bitmap')
This package uses a custom data structure to store font information. This data structure is optimized for access to random sequences of glyphs which can be quickly assembled into a data.frame of points (for bitmap fonts) or strokes (for vector fonts). This data structure also needs to be compact and avoid unnecessary repetition. This is because the 'unifont' font contains pixel coordinates for over 100,000 codepoints and all this data must not exceed package size limitations for CRAN.
Each 'lofi' font is a list object with the following members:
A data frame of 'x', 'y' coordinates. For vector fonts this also includes a 'stroke_idx' to delineate the individual strokes within a single glyph. This is a simple concatenation of all points (or strokes) in a font. Extracting this font data for a particular codepoint requires the use of other indexing elements in the 'lofi' structure. NOTE: For bitmap fonts, (x, y) coordinates must be numeric, integer or raw values with no values below zero.
An integer vector. Use a codepoint (integer)
to access the row index into the 'glyph_info' data.frame which holds the
meta-information about this glyph. Because codepoints are indexed from 0,
but R indexes vectors from 1, to access the row index: codepoint_to_idx[codepoint + 1]
Integer. The line height of this font in pixels
Integer. The default unicode codepoint to use if the font does not contain a given glyph
Numeric value. The offset between the bottom of the font data and the baseline for the text
name of font
A data.frame of meta-information about each glyph. One row per glyph
Glyph codepoint (integer value)
The number of rows of data in 'coords' data.frame which are used to define this font
The index of the first row in 'coords' data.frame which contains data for this font
The index of the last row in 'coords' data.frame which contains data for this font
Glyph width (in pixels)
This section describes the process of extracting the data for a single glyph
Convert the glyph to an integer codepoint using codepoint <- utf8ToInt(x)
Use row <- codepoint_to_idx[codepoint + 1]
to determine the row index of
glyph_info
which contains information for this codepoint.
if row
is NA
this indicates that the font does not support
the glyph, and the row
corresponding to default_codepoint
should
be used instead
info <- glyph_info[row, ]
Subset coords
data.frame for the coordinates associated with
this glyph: coords[info$row_start:info$row_end, ]
lifo <- get_lofi_font('unifont') x <- 'a' codepoint <- utf8ToInt(x) row <- lifo$codepoint_to_idx[codepoint + 1] info <- lifo$glyph_info[row,] coords <- lifo$coords[seq(info$row_start, info$row_end), ] coords plot(coords$x, coords$y, asp = 1, ann = FALSE, axes = FALSE) # Regular users should just use the functions provided in this package which # add extra font information and layout sequences of characters # over multiple lines bitmap_text_coords('a', 'unifont') bitmap_text_raster('a', 'unifont') |> plot()
lifo <- get_lofi_font('unifont') x <- 'a' codepoint <- utf8ToInt(x) row <- lifo$codepoint_to_idx[codepoint + 1] info <- lifo$glyph_info[row,] coords <- lifo$coords[seq(info$row_start, info$row_end), ] coords plot(coords$x, coords$y, asp = 1, ann = FALSE, axes = FALSE) # Regular users should just use the functions provided in this package which # add extra font information and layout sequences of characters # over multiple lines bitmap_text_coords('a', 'unifont') bitmap_text_raster('a', 'unifont') |> plot()
Plot a lofi raster
## S3 method for class ''lofi-raster'' plot(x, interpolate = FALSE, ...)
## S3 method for class ''lofi-raster'' plot(x, interpolate = FALSE, ...)
x |
lofi raster rendering |
interpolate |
default: FALSE |
... |
extra arguments passed to |
None
ras <- bitmap_text_raster("Hi") plot(ras)
ras <- bitmap_text_raster("Hi") plot(ras)
Print summary information about a lofi font
## S3 method for class 'lofi' print(x, ...)
## S3 method for class 'lofi' print(x, ...)
x |
lofi font object |
... |
other arguments ignored |
None
font <- get_lofi_font('unscii-8') print(font)
font <- get_lofi_font('unscii-8') print(font)
Text input can contain multiple lines separated by carriage returns
vector_text_coords( text, font = c("gridfont_smooth", "gridfont", "arcade"), dx = 0L, dy = 0L, missing = utf8ToInt("?") )
vector_text_coords( text, font = c("gridfont_smooth", "gridfont", "arcade"), dx = 0L, dy = 0L, missing = utf8ToInt("?") )
text |
Single text string. Can include carriage returns to split text over multiple lines. |
font |
Name of vector font, or a vector 'lofi' font object. Default: 'gridfont_smooth'.
Use |
dx |
Additional character spacing in the horizontal direction. Default: 0 |
dy |
Additional character spacing in the vertical direction i.e. between rows of text. Default: 0 |
missing |
Codepoint to use if glyph not found in font. Default: NULL means to use the default specified by the font internally. Otherwise it will default to the codepoint for '?' |
data.frame of stroke information
char_idx
The index of the character within the provided text
string
codepoint
Unicode codepoint (integer)
stroke_idx
Index of the stroke within each character
x
Pixel coordinate x value for display
y
Pixel coordinate y value for display
line
Line number within input text
where this character appears
x0
Original untransformed x-coordinate
y0
Original untransformed y-coordinate
Other vector text functions:
vector_text_matrix()
,
vector_text_raster()
vector_text_coords('Hi')
vector_text_coords('Hi')
Create a binary matrix of the rendered text
vector_text_matrix( text, font = c("gridfont_smooth", "gridfont", "arcade"), scale_coords = 1, scale_matrix = 1, dx = NULL, dy = NULL, missing = utf8ToInt("?") )
vector_text_matrix( text, font = c("gridfont_smooth", "gridfont", "arcade"), scale_coords = 1, scale_matrix = 1, dx = NULL, dy = NULL, missing = utf8ToInt("?") )
text |
Single text string. Can include carriage returns to split text over multiple lines. |
font |
Name of vector font, or a vector 'lofi' font object. Default: 'gridfont_smooth'.
Use |
scale_coords |
Scale factor for text rendering. Numeric value greater than zero. Default: 1 |
scale_matrix |
Integer size scale factor. Default: 1. Must be an integer value >= 1. Scale up the matrix or raster result by this factor after rendering the coordinates. |
dx |
Additional character spacing in the horizontal direction. Default: 0 |
dy |
Additional character spacing in the vertical direction i.e. between rows of text. Default: 0 |
missing |
Codepoint to use if glyph not found in font. Default: NULL means to use the default specified by the font internally. Otherwise it will default to the codepoint for '?' |
Binary matrix rendering of the font
Other vector text functions:
vector_text_coords()
,
vector_text_raster()
vector_text_matrix("Hi")
vector_text_matrix("Hi")
Create a raster image of the rendered text
vector_text_raster( text, font = c("gridfont_smooth", "gridfont", "arcade"), scale_coords = 10, scale_matrix = 1, dx = NULL, dy = NULL, missing = utf8ToInt("?") )
vector_text_raster( text, font = c("gridfont_smooth", "gridfont", "arcade"), scale_coords = 10, scale_matrix = 1, dx = NULL, dy = NULL, missing = utf8ToInt("?") )
text |
Single text string. Can include carriage returns to split text over multiple lines. |
font |
Name of vector font, or a vector 'lofi' font object. Default: 'gridfont_smooth'.
Use |
scale_coords |
Scale factor for text rendering. Numeric value greater than zero. Default: 1 |
scale_matrix |
Integer size scale factor. Default: 1. Must be an integer value >= 1. Scale up the matrix or raster result by this factor after rendering the coordinates. |
dx |
Additional character spacing in the horizontal direction. Default: 0 |
dy |
Additional character spacing in the vertical direction i.e. between rows of text. Default: 0 |
missing |
Codepoint to use if glyph not found in font. Default: NULL means to use the default specified by the font internally. Otherwise it will default to the codepoint for '?' |
Raster image of rendered text
Other vector text functions:
vector_text_coords()
,
vector_text_matrix()
ras <- vector_text_raster("Hi") plot(ras)
ras <- vector_text_raster("Hi") plot(ras)