add.fonts: Add new font families for SWF device

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/font.R

Description

This function registers new font families that can be used by SWF device. Currently supported formats are ttf/ttc fonts.

Usage

1
2

Arguments

family

a character string of maximum 200-byte size, indicating the family name of the fonts you want to add. See "Details" for further explanation.

regular

path of the font file for "regular" font face. This argument must be specified as a character string and cannot be missing.

bold

path of the font file for "bold" font face. If it is NULL, the function will use the value of argument regular.

italic,bolditalic,symbol

ditto

Details

In R graphics device, there are two parameters combined together to select a font to show texts. par("family") is a character string giving a name to a series of font faces. Here series implies that there may be different fonts with the same family name, and actually they are distinguished by the parameter par("font"), indicating whether it is regular, bold or italic, etc. In R, par("font") is an integer from 1 to 5 representing regular, bold, italic, bold italic and symbol respectively.

In SWF device, there are three default font families, sans, serif and mono, along with those 5 font faces, that can be used immediately. If you want to use other font families, you could call add.fonts() to register new fonts. Notice that the family argument in this function can be an arbitrary string which doesn't need to be the real font name. You will use the specified family name in functions like par(family = "myfont") and text("Some text", family = "myfont"). The "Examples" section shows a complete demonstration of the usage.

To find the font file of argument regular (and the same for other font faces), this function will first check the existence of the specified path. If not found, file will be searched in the directories returned by font.paths() in turn. If the file cannot be found in any of the locations, an error will be issued.

Value

A character vector (invisible) of current available font family names

Author(s)

Yixuan Qiu <http://yixuan.cos.name/>

See Also

See par() for explanation of the parameters family and font

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
## Not run: 
# Example: download the font file of WenQuanYi Micro Hei,
#          add it to SWF device, and use it to draw text in swf()
# WenQuanYi Micro Hei is an open source and high quality Chinese (and CJKV) font
wd = setwd(tempdir());
ft.url = "http://sourceforge.net/projects/wqy/files/wqy-microhei";
ft.url = paste(ft.url, "0.2.0-beta/wqy-microhei-0.2.0-beta.tar.gz",
               sep = "/");
download.file(ft.url, basename(ft.url));
# Extract it into R2SWF/fonts and add the directory to search path
ft.dir = system.file("fonts", package = "R2SWF");
untar(basename(ft.url), exdir = ft.dir, compressed = "gzip");
font.paths(file.path(ft.dir, "wqy-microhei"));
# Register this font file and assign the family name "wqy".
# Other font faces will be the same with regular by default
add.fonts("wqy", regular = "wqy-microhei.ttc");

# A more concise way to add font is using absolute path,
# without calling font.paths()
# add.fonts("wqy", file.path(ft.dir, "wqy-microhei/wqy-microhei.ttc"));

# List available font families
list.fonts();
# Now it shows that we can use the family "wqy" in swf()
swf("testfont.swf");
# Select font family globally
op = par(family = "serif", font.lab = 2);
# Inline selecting font
plot(1, type = "n");
text(1, 1, intToUtf8(c(20013, 25991)), family = "wqy", font = 1, cex = 2);
dev.off();
swf2html("testfont.swf");
setwd(wd);

## End(Not run)

yixuan/R2SWF-archive documentation built on May 4, 2019, 5:28 p.m.