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.
nchar_ttype(
x,
fontspec = font_spec(),
tol = sqrt(.Machine$double.eps),
raw = FALSE
)
(character
)
the string(s) to calculate width(s) for.
(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.
(numeric(1)
)
the tolerance to use when determining
if a multiple needs to be rounded up to the next integer. See
Details.
(logical(1)
)
whether unrounded widths should be returned. Defaults to FALSE
.
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
.
font_spec()
nchar_ttype("hi there!")
nchar_ttype("hi there!", font_spec("Times"))
Run the code above in your browser using DataLab