Title: | Utilities for Graphical Rendering and Fonts Management |
---|---|
Description: | Tools are provided to compute metrics of formatted strings and to check the availability of a font. Another set of functions is provided to support the collection of fonts from 'Google Fonts' in a cache. Their use is simple within 'R Markdown' documents and 'shiny' applications but also with graphic productions generated with the 'ggiraph', 'ragg' and 'svglite' packages or with tabular productions from the 'flextable' package. |
Authors: | David Gohel [aut, cre], Hadley Wickham [aut], Lionel Henry [aut], Jeroen Ooms [aut] , Yixuan Qiu [ctb], R Core Team [cph] (Cairo code from X11 device), ArData [cph], RStudio [cph] |
Maintainer: | David Gohel <[email protected]> |
License: | GPL-3 | file LICENSE |
Version: | 0.4.1 |
Built: | 2024-11-05 06:49:47 UTC |
Source: | CRAN |
Add an empty HTML element attached to an 'HTML Dependency' containing the css and the font files so that the font is available in the HTML page. Multiple families are supported.
The htmlDependency is defined with function gfontHtmlDependency()
.
addGFontHtmlDependency(family = "Open Sans", subset = c("latin", "latin-ext"))
addGFontHtmlDependency(family = "Open Sans", subset = c("latin", "latin-ext"))
family |
family name of a 'Google Fonts', for example, "Open Sans", "Roboto", "Fira Code" or "Fira Sans Condensed". Complete list is available with the following command: gfonts::get_all_fonts()$family |> unlist() |> unique() |> sort() |
subset |
font subset, a character vector, it defaults to only "latin" and "latin-ext" and can contains values such as "greek", "emoji", "chinese-traditional", Run the following code to get a complete list: gfonts::get_all_fonts()$subsets |> unlist() |> unique() |> sort() |
It allows users to use fonts from 'Google Fonts' in an HTML page generated by 'shiny' or 'R Markdown'.
At the first request, the font files will be downloaded and stored in a cache on the
user's machine, thus avoiding many useless downloads or allowing to work with
these fonts afterwards without an Internet connection, in a docker image for example.
See fonts_cache_dir()
.
The server delivering the font files should not be too busy. That's
why a one second pause is added after each download to respect the server's
limits. This time can be set with the option GFONTS_DOWNLOAD_SLEEPTIME
which
must be a number of seconds.
an HTML object
Other functions for font management:
fonts_cache_dir()
,
gfontHtmlDependency()
,
install_gfont_script()
,
installed_gfonts()
,
liberationsansHtmlDependency()
,
register_gfont()
,
register_liberationsans()
## Not run: if (check_gfonts()) { dummy_setup() addGFontHtmlDependency(family = "Open Sans") } ## End(Not run)
## Not run: if (check_gfonts()) { dummy_setup() addGFontHtmlDependency(family = "Open Sans") } ## End(Not run)
Check if a font family exists in system fonts.
font_family_exists(font_family = "sans")
font_family_exists(font_family = "sans")
font_family |
font family name (case sensitive) |
A logical value
font_family_exists("sans") font_family_exists("Arial") font_family_exists("Courier")
font_family_exists("sans") font_family_exists("Arial") font_family_exists("Courier")
This function can be used to make fontconfig reload font configuration files.
fontconfig_reinit()
fontconfig_reinit()
Fontconfig is not used anymore and that function will be deprecated in the next release.
Paul Murrell
Initialize or remove font directory used to store downloaded font files.
This directory is managed by R function R_user_dir()
but can also
be defined in a non-user location by setting ENV variable GDTOOLS_CACHE_DIR
or by setting R option GDTOOLS_CACHE_DIR
.
Its value can be read with the fonts_cache_dir()
function.
The directory can be deleted with rm_fonts_cache()
and
created with init_fonts_cache()
.
fonts_cache_dir() rm_fonts_cache() init_fonts_cache()
fonts_cache_dir() rm_fonts_cache() init_fonts_cache()
Other functions for font management:
addGFontHtmlDependency()
,
gfontHtmlDependency()
,
install_gfont_script()
,
installed_gfonts()
,
liberationsansHtmlDependency()
,
register_gfont()
,
register_liberationsans()
fonts_cache_dir() options(GDTOOLS_CACHE_DIR = tempdir()) fonts_cache_dir() options(GDTOOLS_CACHE_DIR = NULL) Sys.setenv(GDTOOLS_CACHE_DIR = tempdir()) fonts_cache_dir() Sys.setenv(GDTOOLS_CACHE_DIR = "") init_fonts_cache() dir.exists(fonts_cache_dir()) rm_fonts_cache() dir.exists(fonts_cache_dir())
fonts_cache_dir() options(GDTOOLS_CACHE_DIR = tempdir()) fonts_cache_dir() options(GDTOOLS_CACHE_DIR = NULL) Sys.setenv(GDTOOLS_CACHE_DIR = tempdir()) fonts_cache_dir() Sys.setenv(GDTOOLS_CACHE_DIR = "") init_fonts_cache() dir.exists(fonts_cache_dir()) rm_fonts_cache() dir.exists(fonts_cache_dir())
Create an HTML dependency ready to be used in 'Shiny' or 'R Markdown'.
gfontHtmlDependency(family = "Open Sans", subset = c("latin", "latin-ext"))
gfontHtmlDependency(family = "Open Sans", subset = c("latin", "latin-ext"))
family |
family name of a 'Google Fonts', for example, "Open Sans", "Roboto", "Fira Code" or "Fira Sans Condensed". Complete list is available with the following command: gfonts::get_all_fonts()$family |> unlist() |> unique() |> sort() |
subset |
font subset, a character vector, it defaults to only "latin" and "latin-ext" and can contains values such as "greek", "emoji", "chinese-traditional", Run the following code to get a complete list: gfonts::get_all_fonts()$subsets |> unlist() |> unique() |> sort() |
It allows users to use fonts from 'Google Fonts' in an HTML page generated by 'shiny' or 'R Markdown'.
At the first request, the font files will be downloaded and stored in a cache on the
user's machine, thus avoiding many useless downloads or allowing to work with
these fonts afterwards without an Internet connection, in a docker image for example.
See fonts_cache_dir()
.
The server delivering the font files should not be too busy. That's
why a one second pause is added after each download to respect the server's
limits. This time can be set with the option GFONTS_DOWNLOAD_SLEEPTIME
which
must be a number of seconds.
an object defined with htmltools::htmlDependency()
.
Other functions for font management:
addGFontHtmlDependency()
,
fonts_cache_dir()
,
install_gfont_script()
,
installed_gfonts()
,
liberationsansHtmlDependency()
,
register_gfont()
,
register_liberationsans()
## Not run: if (check_gfonts()) { dummy_setup() gfontHtmlDependency(family = "Open Sans") } ## End(Not run)
## Not run: if (check_gfonts()) { dummy_setup() gfontHtmlDependency(family = "Open Sans") } ## End(Not run)
Determines if strings contain glyphs not part of a font.
glyphs_match(x, fontname = "sans", bold = FALSE, italic = FALSE, fontfile = "")
glyphs_match(x, fontname = "sans", bold = FALSE, italic = FALSE, fontfile = "")
x |
Character vector of strings |
fontname |
Font name |
bold , italic
|
Is text bold/italic? |
fontfile |
Font file |
a logical vector, if a character element is containing at least a glyph that can not be matched in the font table, FALSE is returned.
glyphs_match(letters) glyphs_match("\u265E", bold = TRUE)
glyphs_match(letters) glyphs_match("\u265E", bold = TRUE)
Create a string containing a system command to execute so that the font from 'Google Fonts' is installed on the system. Its execution may require root permissions, in dockerfile for example.
install_gfont_script( family = "Open Sans", subset = c("latin", "latin-ext"), platform = c("debian", "windows", "macos"), file = NULL )
install_gfont_script( family = "Open Sans", subset = c("latin", "latin-ext"), platform = c("debian", "windows", "macos"), file = NULL )
family |
family name of a 'Google Fonts', for example, "Open Sans", "Roboto", "Fira Code" or "Fira Sans Condensed". Complete list is available with the following command: gfonts::get_all_fonts()$family |> unlist() |> unique() |> sort() |
subset |
font subset, a character vector, it defaults to only "latin" and "latin-ext" and can contains values such as "greek", "emoji", "chinese-traditional", Run the following code to get a complete list: gfonts::get_all_fonts()$subsets |> unlist() |> unique() |> sort() |
platform |
"debian" and "windows" and "macos" are supported. |
file |
script file to generate, optional. If the parameter is specified, a file will be generated ready for execution. If the platform is Windows, administration rights are required to run the script. |
It allows users to use fonts from 'Google Fonts' in an HTML page generated by 'shiny' or 'R Markdown'.
At the first request, the font files will be downloaded and stored in a cache on the
user's machine, thus avoiding many useless downloads or allowing to work with
these fonts afterwards without an Internet connection, in a docker image for example.
See fonts_cache_dir()
.
The server delivering the font files should not be too busy. That's
why a one second pause is added after each download to respect the server's
limits. This time can be set with the option GFONTS_DOWNLOAD_SLEEPTIME
which
must be a number of seconds.
the 'shell' or 'PowerShell' command as a string
Other functions for font management:
addGFontHtmlDependency()
,
fonts_cache_dir()
,
gfontHtmlDependency()
,
installed_gfonts()
,
liberationsansHtmlDependency()
,
register_gfont()
,
register_liberationsans()
## Not run: if (check_gfonts()) { dummy_setup() install_gfont_script(family = "Roboto", platform = "macos") } ## End(Not run)
## Not run: if (check_gfonts()) { dummy_setup() install_gfont_script(family = "Roboto", platform = "macos") } ## End(Not run)
List installed 'Google Fonts' that can be found in the user cache directory.
installed_gfonts()
installed_gfonts()
families names as a character vector
Other functions for font management:
addGFontHtmlDependency()
,
fonts_cache_dir()
,
gfontHtmlDependency()
,
install_gfont_script()
,
liberationsansHtmlDependency()
,
register_gfont()
,
register_liberationsans()
## Not run: if (check_gfonts()) { dummy_setup() register_gfont(family = "Roboto") installed_gfonts() } ## End(Not run)
## Not run: if (check_gfonts()) { dummy_setup() register_gfont(family = "Roboto") installed_gfonts() } ## End(Not run)
Create an HTML dependency ready to be used in 'Shiny' or 'R Markdown' with 'Liberation Sans' Font.
liberationsansHtmlDependency()
liberationsansHtmlDependency()
Other functions for font management:
addGFontHtmlDependency()
,
fonts_cache_dir()
,
gfontHtmlDependency()
,
install_gfont_script()
,
installed_gfonts()
,
register_gfont()
,
register_liberationsans()
For each x
element, determines the width and height of a bounding box that's big enough
to (just) enclose the provided text. Unit is pixel.
m_str_extents( x, fontname = "sans", fontsize = 10, bold = FALSE, italic = FALSE, fontfile = NULL )
m_str_extents( x, fontname = "sans", fontsize = 10, bold = FALSE, italic = FALSE, fontfile = NULL )
x |
Character vector of strings to measure |
fontname |
Font name. A vector of character to match with x. |
fontsize |
Font size. A vector of numeric to match with x. |
bold , italic
|
Is text bold/italic?. A vector of logical to match with x. |
fontfile |
Font file. A vector of character to match with x. |
# The first run can be slow when font caches are missing # as font files are then being scanned to build those font caches. m_str_extents(letters, fontsize = 1:26) m_str_extents(letters[1:3], bold = c(TRUE, FALSE, TRUE), italic = c(FALSE, TRUE, TRUE), fontname = c("sans", "sans", "sans") )
# The first run can be slow when font caches are missing # as font files are then being scanned to build those font caches. m_str_extents(letters, fontsize = 1:26) m_str_extents(letters[1:3], bold = c(TRUE, FALSE, TRUE), italic = c(FALSE, TRUE, TRUE), fontname = c("sans", "sans", "sans") )
match_family()
returns the best font family match.
match_family(font = "sans", bold = TRUE, italic = TRUE, debug = NULL)
match_family(font = "sans", bold = TRUE, italic = TRUE, debug = NULL)
font |
family or face to match. |
bold |
Wheter to match a font featuring a |
italic |
Wheter to match a font featuring an |
debug |
deprecated |
match_family("sans") match_family("serif")
match_family("sans") match_family("serif")
raster_view
is a helper function for testing. It uses htmltools
to render a png as an image with base64 encoded data image.
raster_str(x, width = 480, height = 480, interpolate = FALSE) raster_view(code)
raster_str(x, width = 480, height = 480, interpolate = FALSE) raster_view(code)
x |
A raster object |
width , height
|
Width and height in pixels. |
interpolate |
A logical value indicating whether to linearly interpolate the image. |
code |
base64 code of a raster |
r <- as.raster(matrix(hcl(0, 80, seq(50, 80, 10)), nrow = 4, ncol = 5)) code <- raster_str(r, width = 50, height = 50) if (interactive() && require("htmltools")) { raster_view(code = code) }
r <- as.raster(matrix(hcl(0, 80, seq(50, 80, 10)), nrow = 4, ncol = 5)) code <- raster_str(r, width = 50, height = 50) if (interactive() && require("htmltools")) { raster_view(code = code) }
Draw/preview a raster to a png file
raster_write(x, path, width = 480, height = 480, interpolate = FALSE)
raster_write(x, path, width = 480, height = 480, interpolate = FALSE)
x |
A raster object |
path |
name of the file to create |
width , height
|
Width and height in pixels. |
interpolate |
A logical value indicating whether to linearly interpolate the image. |
r <- as.raster(matrix(hcl(0, 80, seq(50, 80, 10)), nrow = 4, ncol = 5)) filepng <- tempfile(fileext = ".png") raster_write(x = r, path = filepng, width = 50, height = 50)
r <- as.raster(matrix(hcl(0, 80, seq(50, 80, 10)), nrow = 4, ncol = 5)) filepng <- tempfile(fileext = ".png") raster_write(x = r, path = filepng, width = 50, height = 50)
Register a font from 'Google Fonts' so that it can be used with devices using the 'systemfonts' package, i.e. the 'flextable' package and graphic outputs generated with the 'ragg', 'svglite' and 'ggiraph' packages.
register_gfont(family = "Open Sans", subset = c("latin", "latin-ext"))
register_gfont(family = "Open Sans", subset = c("latin", "latin-ext"))
family |
family name of a 'Google Fonts', for example, "Open Sans", "Roboto", "Fira Code" or "Fira Sans Condensed". Complete list is available with the following command: gfonts::get_all_fonts()$family |> unlist() |> unique() |> sort() |
subset |
font subset, a character vector, it defaults to only "latin" and "latin-ext" and can contains values such as "greek", "emoji", "chinese-traditional", Run the following code to get a complete list: gfonts::get_all_fonts()$subsets |> unlist() |> unique() |> sort() |
It allows users to use fonts from 'Google Fonts' in an HTML page generated by 'shiny' or 'R Markdown'.
At the first request, the font files will be downloaded and stored in a cache on the
user's machine, thus avoiding many useless downloads or allowing to work with
these fonts afterwards without an Internet connection, in a docker image for example.
See fonts_cache_dir()
.
The server delivering the font files should not be too busy. That's
why a one second pause is added after each download to respect the server's
limits. This time can be set with the option GFONTS_DOWNLOAD_SLEEPTIME
which
must be a number of seconds.
TRUE if the operation went ok.
Other functions for font management:
addGFontHtmlDependency()
,
fonts_cache_dir()
,
gfontHtmlDependency()
,
install_gfont_script()
,
installed_gfonts()
,
liberationsansHtmlDependency()
,
register_liberationsans()
## Not run: if (check_gfonts()) { dummy_setup() register_gfont(family = "Roboto") } ## End(Not run)
## Not run: if (check_gfonts()) { dummy_setup() register_gfont(family = "Roboto") } ## End(Not run)
Register font 'Liberation Sans' so that it can be used with devices using the 'systemfonts' package, i.e. the 'flextable' package and graphic outputs generated with the 'ragg', 'svglite' and 'ggiraph' packages.
register_liberationsans()
register_liberationsans()
TRUE if the operation went ok.
Other functions for font management:
addGFontHtmlDependency()
,
fonts_cache_dir()
,
gfontHtmlDependency()
,
install_gfont_script()
,
installed_gfonts()
,
liberationsansHtmlDependency()
,
register_gfont()
Set and unset a minimalistic Fontconfig configuration
set_dummy_conf() unset_dummy_conf()
set_dummy_conf() unset_dummy_conf()
Fontconfig is not used anymore and these functions will be deprecated in the next release.
Determines the width and height of a bounding box that's big enough to (just) enclose the provided text.
str_extents( x, fontname = "sans", fontsize = 12, bold = FALSE, italic = FALSE, fontfile = "" )
str_extents( x, fontname = "sans", fontsize = 12, bold = FALSE, italic = FALSE, fontfile = "" )
x |
Character vector of strings to measure |
fontname |
Font name |
fontsize |
Font size |
bold , italic
|
Is text bold/italic? |
fontfile |
Font file |
str_extents(letters) str_extents("Hello World!", bold = TRUE, italic = FALSE, fontname = "sans", fontsize = 12)
str_extents(letters) str_extents("Hello World!", bold = TRUE, italic = FALSE, fontname = "sans", fontsize = 12)
Get font metrics for a string.
str_metrics( x, fontname = "sans", fontsize = 12, bold = FALSE, italic = FALSE, fontfile = "" )
str_metrics( x, fontname = "sans", fontsize = 12, bold = FALSE, italic = FALSE, fontfile = "" )
x |
Character vector of strings to measure |
fontname |
Font name |
fontsize |
Font size |
bold , italic
|
Is text bold/italic? |
fontfile |
Font file |
A named numeric vector
str_metrics("Hello World!")
str_metrics("Hello World!")
List system and registryfonts details into a data.frame containing columns foundry, family, file, slant and weight.
sys_fonts()
sys_fonts()
sys_fonts()
sys_fonts()
version_cairo()
and version_freetype()
return the
runtime version. These helpers return version objects as with
packageVersion()
.
version_freetype() version_cairo()
version_freetype() version_cairo()