nchar_ttype: Calculate font-specific string width

View source: R/tostring.R

nchar_ttypeR Documentation

Calculate font-specific string width

Description

This function returns the width of each element x as a multiple of the width of the space character for in declared font, rounded up to the nearest integer. This is used extensively in the text rendering (toString()) and pagination machinery for calculating word wrapping, default column widths, lines per page, etc.

Usage

nchar_ttype(
  x,
  fontspec = font_spec(),
  tol = sqrt(.Machine$double.eps),
  raw = FALSE
)

Arguments

x

(character)
the string(s) to calculate width(s) for.

fontspec

(font_spec or NULL)
if non-NULL, the font to use for the calculations (as returned by font_spec()). Defaults to "Courier", which is a monospace font. If NULL, the width will be returned in number of characters by calling nchar directly.

tol

(numeric(1))
the tolerance to use when determining if a multiple needs to be rounded up to the next integer. See Details.

raw

(logical(1))
whether unrounded widths should be returned. Defaults to FALSE.

Details

String width is defined in terms of spaces within the specified font. For monospace fonts, this definition collapses to the number of characters in the string (nchar()), but for truetype fonts it does not.

For raw = FALSE, non-integer values (the norm in a truetype setting) for the number of spaces a string takes up is rounded up, unless the multiple is less than tol above the last integer before it. E.g., if k - num_spaces < tol for an integer k, k is returned instead of k+1.

See Also

font_spec()

Examples

nchar_ttype("hi there!")

nchar_ttype("hi there!", font_spec("Times"))


formatters documentation built on June 22, 2024, 9:42 a.m.