lofi {lofifonts} | R Documentation |
A description of the 'lofi' font format used to store fonts for this package
Description
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.
Details
Each 'lofi' font is a list object with the following members:
- coords
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.
- codepoint_to_idx
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]
- line_height
Integer. The line height of this font in pixels
- default_codepoing
Integer. The default unicode codepoint to use if the font does not contain a given glyph
- baseline_offset
Numeric value. The offset between the bottom of the font data and the baseline for the text
- name
name of font
- glyph_info
A data.frame of meta-information about each glyph. One row per glyph
- codepoint
Glyph codepoint (integer value)
- npoints
The number of rows of data in 'coords' data.frame which are used to define this font
- row_start
The index of the first row in 'coords' data.frame which contains data for this font
- row_end
The index of the last row in 'coords' data.frame which contains data for this font
- width
Glyph width (in pixels)
Usage
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 ofglyph_info
which contains information for this codepoint.if
row
isNA
this indicates that the font does not support the glyph, and therow
corresponding todefault_codepoint
should be used insteadinfo <- glyph_info[row, ]
Subset
coords
data.frame for the coordinates associated with this glyph:coords[info$row_start:info$row_end, ]
Examples
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()