Title: | Bindings to the 'HarfBuzz' and 'Fribidi' Libraries for Text Shaping |
---|---|
Description: | Provides access to the text shaping functionality in the 'HarfBuzz' library and the bidirectional algorithm in the 'Fribidi' library. 'textshaping' is a low-level utility package mainly for graphic devices that expands upon the font tool-set provided by the 'systemfonts' package. |
Authors: | Thomas Lin Pedersen [cre, aut] , Posit, PBC [cph, fnd] |
Maintainer: | Thomas Lin Pedersen <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0.9000 |
Built: | 2025-01-20 11:20:52 UTC |
Source: | https://github.com/r-lib/textshaping |
This is a simply functions that returns the available OpenType feature tags
for one or more fonts. See font_feature()
for
more information on how to use the different feature with a font.
get_font_features( family = "", italic = FALSE, bold = FALSE, path = NULL, index = 0 )
get_font_features( family = "", italic = FALSE, bold = FALSE, path = NULL, index = 0 )
family |
The name of the font families to match |
italic |
logical indicating the font slant |
bold |
logical indicating whether the font weight |
path , index
|
path an index of a font file to circumvent lookup based on family and style |
A list with an element for each of the input fonts containing the supported feature tags for that font.
# Select a random font on the system sys_fonts <- systemfonts::system_fonts() random_font <- sys_fonts$family[sample(nrow(sys_fonts), 1)] # Get the features get_font_features(random_font)
# Select a random font on the system sys_fonts <- systemfonts::system_fonts() random_font <- sys_fonts$family[sample(nrow(sys_fonts), 1)] # Get the features get_font_features(random_font)
Textshaping exists partly to allow all the various scripts that exists in the world to be used in R graphics. This function returns gibberish filler text (lorem ipsum text) in various scripts for testing purpose. Some of these are transliterations of the original lorem ipsum text while others are based an a distribution model.
lorem_text( script = c("latin", "chinese", "arabic", "devanagari", "cyrillic", "kana", "hangul", "greek", "hebrew", "armenian", "georgian"), n = 1 ) lorem_bidi( ltr = c("latin", "chinese", "devanagari", "cyrillic", "kana", "hangul", "greek", "armenian", "georgian"), rtl = c("arabic", "hebrew"), ltr_prop = 0.9, n = 1 )
lorem_text( script = c("latin", "chinese", "arabic", "devanagari", "cyrillic", "kana", "hangul", "greek", "hebrew", "armenian", "georgian"), n = 1 ) lorem_bidi( ltr = c("latin", "chinese", "devanagari", "cyrillic", "kana", "hangul", "greek", "armenian", "georgian"), rtl = c("arabic", "hebrew"), ltr_prop = 0.9, n = 1 )
script |
A string giving the script to fetch gibberish for |
n |
The number of paragraphs to fetch. Each paragraph will be its own element in the returned character vector. |
ltr , rtl
|
scripts to use for left-to-right and right-to-left text |
ltr_prop |
The approximate proportion of left-to-right text in the final string |
a charactor vector of length n
https://generator.lorem-ipsum.info
# Defaults to standard lorem ipsum lorem_text() # Get two paragraphs of hangul (Korean) lorem_text("hangul", 2) # Get gibberish bi-directional text lorem_bidi()
# Defaults to standard lorem ipsum lorem_text() # Get two paragraphs of hangul (Korean) lorem_text("hangul", 2) # Get gibberish bi-directional text lorem_bidi()
This function allows you to preview the layout that shape_text()
calculates. It is purely meant as a sanity check to make sure that the values
calculated are sensible and shouldn't be used as a plotting function for
rendering text on its own.
plot_shape(shape, id = 1)
plot_shape(shape, id = 1)
shape |
The output of a call to |
id |
The index of the text run to show in case |
This function is called for its side effects
arab_text <- lorem_text("arabic", 2) shape <- shape_text( arab_text, max_width = 5, indent = 0.2 ) try( plot_shape(shape) )
arab_text <- lorem_text("arabic", 2) shape <- shape_text( arab_text, max_width = 5, indent = 0.2 ) try( plot_shape(shape) )
Performs advanced text shaping of strings including font fallbacks, bidirectional script support, word wrapping and various character and paragraph level formatting settings.
shape_text( strings, id = NULL, family = "", italic = FALSE, weight = "normal", width = "undefined", features = font_feature(), size = 12, res = 72, lineheight = 1, align = "auto", hjust = 0, vjust = 0, max_width = NA, tracking = 0, indent = 0, hanging = 0, space_before = 0, space_after = 0, direction = "auto", path = NULL, index = 0, bold = deprecated() )
shape_text( strings, id = NULL, family = "", italic = FALSE, weight = "normal", width = "undefined", features = font_feature(), size = 12, res = 72, lineheight = 1, align = "auto", hjust = 0, vjust = 0, max_width = NA, tracking = 0, indent = 0, hanging = 0, space_before = 0, space_after = 0, direction = "auto", path = NULL, index = 0, bold = deprecated() )
strings |
A character vector of strings to shape |
id |
A vector grouping the strings together. If strings share an id the shaping will continue between 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 ( |
width |
The width to query for either in numbers ( |
features |
A |
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. |
lineheight |
A multiplier for the lineheight |
align |
Within text box alignment, either |
hjust , vjust
|
The justification of the textbox surrounding the text |
max_width |
The requested with of the string in inches. Setting this to
something other than |
tracking |
Tracking of the glyphs (space adjustment) measured in 1/1000 em. |
indent |
The indent of the first line in a paragraph measured in inches. |
hanging |
The indent of the remaining lines in a paragraph measured in inches. |
space_before , space_after
|
The spacing above and below a paragraph, measured in points |
direction |
The overall directional flow of the text. The default
( |
path , index
|
path an index of a font file to circumvent lookup based on family and style |
bold |
logical indicating whether the font weight |
A list with two element: shape
contains the position of each glyph,
relative to the origin in the enclosing textbox. metrics
contain metrics
about the full strings.
shape
is a data.frame with the following columns:
The placement of the the first character contributing to the glyph within the string
The index of the glyph in the font file
The index of the string the glyph is part of (referencing a row in the metrics
data.frame)
The index of the string the glyph came from (referencing an element in the strings
input)
The x offset in pixels from the origin of the textbox
The y offset in pixels from the origin of the textbox
The path to the font file used during shaping of the glyph
The index of the font used to shape the glyph in the font file
The size of the font used during shaping
The advancement amount to the next glyph
The ascend of the font used for the glyph. This does not measure the actual glyph
The descend of the font used for the glyph. This does not measure the actual glyph
metrics
is a data.frame with the following columns:
The text the string consist of
The width of the string
The height of the string
The distance from the left edge of the textbox and the leftmost glyph
The distance from the right edge of the textbox and the rightmost glyph
The distance from the top edge of the textbox and the topmost glyph
The distance from the bottom edge of the textbox and the bottommost glyph
The position of the leftmost edge of the textbox related to the origin
The position of the topmost edge of the textbox related to the origin
The horizontal position of the next glyph after the string
The vertical position of the next glyph after the string
The global direction of the string. If TRUE
then it is left-to-right, otherwise it is right-to-left
string <- "This is a long string\nLook; It spans multiple lines\nand all" # Shape with default settings shape_text(string) # Mix styles within the same string string <- c( "This string will have\na ", "very large", " text style\nin the middle" ) shape_text(string, id = c(1, 1, 1), size = c(12, 24, 12))
string <- "This is a long string\nLook; It spans multiple lines\nand all" # Shape with default settings shape_text(string) # Mix styles within the same string string <- c( "This string will have\na ", "very large", " text style\nin the middle" ) shape_text(string, id = c(1, 1, 1), size = c(12, 24, 12))
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
.
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() )
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() )
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 ( |
width |
The width to query for either in numbers ( |
features |
A |
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 |
A numeric vector giving the width of the strings in pixels. Use the
provided res
value to convert it into absolute values.
strings <- c('A short string', 'A very very looong string') text_width(strings)
strings <- c('A short string', 'A very very looong string') text_width(strings)