text_width: Calculate the width of a string, ignoring new-lines

View source: R/shape_text.R

text_widthR Documentation

Calculate the width of a string, ignoring new-lines

Description

This is a very simple alternative to systemfonts::shape_string() that simply calculates the width of strings without taking any newline into account. As such it is suitable to calculate the width of words or lines that has already been splitted by ⁠\n⁠. Input is recycled to the length of strings.

Usage

text_width(
  strings,
  family = "",
  italic = FALSE,
  weight = "normal",
  width = "undefined",
  features = font_feature(),
  size = 12,
  res = 72,
  include_bearing = TRUE,
  path = NULL,
  index = 0,
  bold = deprecated()
)

Arguments

strings

A character vector of strings

family

The name of the font families to match

italic

logical indicating the font slant

weight

The weight to query for, either in numbers (0, 100, 200, 300, 400, 500, 600, 700, 800, or 900) or strings ("undefined", "thin", "ultralight", "light", "normal", "medium", "semibold", "bold", "ultrabold", or "heavy"). NA will be interpreted as "undefined"/0

width

The width to query for either in numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9) or strings ("undefined", "ultracondensed", "extracondensed", "condensed", "semicondensed", "normal", "semiexpanded", "expanded", "extraexpanded", or "ultraexpanded"). NA will be interpreted as "undefined"/0

features

A systemfonts::font_feature() object or a list of them, giving the OpenType font features to set

size

The size in points to use for the font

res

The resolution to use when doing the shaping. Should optimally match the resolution used when rendering the glyphs.

include_bearing

Logical, should left and right bearing be included in the string width?

path, index

path an index of a font file to circumvent lookup based on family and style

bold

logical indicating whether the font weight

Value

A numeric vector giving the width of the strings in pixels. Use the provided res value to convert it into absolute values.

Examples

strings <- c('A short string', 'A very very looong string')
text_width(strings)


textshaping documentation built on April 4, 2025, 5:24 a.m.