Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ Suggests:
fontcm
License: GPL-2
URL: https://github.com/wch/extrafont
RoxygenNote: 6.0.1
RoxygenNote: 7.1.1
90 changes: 51 additions & 39 deletions R/load.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
#' not register the font family due to problems or because the font family
#' was already registered. If value is the return value of
#' \code{windowsFonts} for \code{"win"}, \code{\link{postscriptFonts}}
#' for \code{"postscript}}, and \code{\link{pdfFonts}} for \code{"pdf"}.
#' for \code{"postscript"}, and \code{\link{pdfFonts}} for \code{"pdf"}.
#'
#'
#' @seealso \code{\link{embed_fonts}},
#' #ifdef windows
#' \code{\link[grDevice]{windowsFont}}, \code{\link[grDevice]{windowsFonts}},
#' \code{\link[grDevices]{windowsFont}}, \code{\link[grDevices]{windowsFonts}},
#' #endif
#' \code{\link{postscriptFonts}},
#' \code{\link{pdfFonts}}, \code{\link{Type1Font}}.
#' \code{\link[grDevices]{postscriptFonts}},
#' \code{\link[grDevices]{pdfFonts}}, \code{\link[grDevices]{Type1Font}}.
#' @import grDevices
#' @export
loadfonts <- function(device = c("all", "pdf", "postscript", "win"),
quiet = FALSE) {

device <- match.arg(device)
if (device == "all") {
if (.Platform$OS.type == "windows") {
Expand All @@ -52,7 +51,7 @@ loadfonts <- function(device = c("all", "pdf", "postscript", "win"),

register_family_win <- function(family, quiet = FALSE, cfonts = character()) {
ffname <- "windowsFonts"
fontfunc <- windowsFonts
fontfunc <- grDevices::windowsFonts
# Now we can register the font with R, with something like this:
# windowsFonts("Arial" = windowsFont("Arial"))
if (family %in% cfonts) {
Expand All @@ -67,7 +66,7 @@ register_family_win <- function(family, quiet = FALSE, cfonts = character()) {
# Since 'family' is a string containing the name of the argument, we
# need to use do.call
args <- list()
args[[family]] <- windowsFont(family)
args[[family]] <- grDevices::windowsFont(family)
do.call(fontfunc, args)
}

Expand All @@ -80,7 +79,7 @@ loadfonts_win <- function(quiet = FALSE) {
fontdata <- fonttable()
# remove empty FamilyNames
fontdata <- fontdata[fontdata$FamilyName != "", , drop = FALSE]
cfonts <- names(windowsFonts())
cfonts <- names(grDevices::windowsFonts())
families <- unique(fontdata$FamilyName)
lapply(families, register_family_win, cfonts = cfonts, quiet = quiet)
}
Expand All @@ -89,10 +88,10 @@ register_family_afm <- function(family, fd, pdf = TRUE, quiet = FALSE,
cfonts = character()) {
if (pdf) {
ffname <- "pdfFont"
fontfunc <- pdfFonts
fontfunc <- grDevices::pdfFonts
} else {
ffname <- "postscriptFont"
fontfunc <- postscriptFonts
fontfunc <- grDevices::postscriptFonts
}

if (family %in% cfonts) {
Expand All @@ -102,18 +101,20 @@ register_family_afm <- function(family, fd, pdf = TRUE, quiet = FALSE,
return(NULL)
}

regular <- fd$afmfile[!fd$Bold & !fd$Italic]
bold <- fd$afmfile[ fd$Bold & !fd$Italic]
italic <- fd$afmfile[!fd$Bold & fd$Italic]
bolditalic <- fd$afmfile[ fd$Bold & fd$Italic]
regular <- fd$afmfile[!fd$Bold & !fd$Italic]
bold <- fd$afmfile[fd$Bold & !fd$Italic]
italic <- fd$afmfile[!fd$Bold & fd$Italic]
bolditalic <- fd$afmfile[fd$Bold & fd$Italic]

# There should be >1 entry for a given weight of a font only for weird
# fonts like Apple Braille. If found, skip this iteration of the loop.
if (length(regular) > 1 || length(bold) > 1 ||
length(italic) > 1 || length(bolditalic) > 1) {
if (length(regular) > 1 || length(bold) > 1 ||
length(italic) > 1 || length(bolditalic) > 1) {
if (!quiet) {
message("More than one version of regular/bold/italic found for ",
family, ". Skipping setup for this font.")
message(
"More than one version of regular/bold/italic found for ",
family, ". Skipping setup for this font."
)
}
return(NULL)
}
Expand All @@ -122,21 +123,23 @@ register_family_afm <- function(family, fd, pdf = TRUE, quiet = FALSE,
# include Brush Script MT.
if (length(regular) == 0) {
if (!quiet) {
message("No regular (non-bold, non-italic) version of ", family,
". Skipping setup for this font.")
message(
"No regular (non-bold, non-italic) version of ", family,
". Skipping setup for this font."
)
}
return(NULL)
}

# If there aren't bold/italic entries, inherit the afm info from regular
if (length(bold) == 0) bold <- regular
if (length(italic) == 0) italic <- regular
if (length(bold) == 0) bold <- regular
if (length(italic) == 0) italic <- regular
if (length(bolditalic) == 0) bolditalic <- bold

# If there's an afmsymfile entry, use that as the symbol font
# Also check that all in this family have the same afmsymfile entry
if (!is.na(fd$afmsymfile[1]) && fd$afmsymfile[1] != "" &&
all(fd$afmsymfile[1] == fd$afmsymfile)) {
if (!is.na(fd$afmsymfile[1]) && fd$afmsymfile[1] != "" &&
all(fd$afmsymfile[1] == fd$afmsymfile)) {
symbol <- fd$afmsymfile[1]
} else {
symbol <- NULL
Expand All @@ -154,27 +157,32 @@ register_family_afm <- function(family, fd, pdf = TRUE, quiet = FALSE,
# need to use do.call
args <- list()
args[[family]] <-
Type1Font(family, metrics = file.path(metrics_path(),
c(regular, bold, italic,
bolditalic, symbol)))
Type1Font(family, metrics = file.path(
metrics_path(),
c(
regular, bold, italic,
bolditalic, symbol
)
))
do.call(fontfunc, args)
}

loadfonts_pdf_ps <- function(pdf = TRUE, quiet = FALSE) {
if (pdf) {
# Get names of fonts that are already registered
cfonts <- names(pdfFonts())
cfonts <- names(grDevices::pdfFonts())
} else {
cfonts <- names(postscriptFonts())
cfonts <- names(grDevices::postscriptFonts())
}
fontdata <- fonttable()
# remove empty FamilyNames
fontdata <- fontdata[fontdata$FamilyName != "", , drop = FALSE]
# split fontdata into list of family data frames
family_data <- split(fontdata, fontdata$FamilyName)
mapply(register_family_afm, names(family_data), family_data,
MoreArgs = list(pdf = pdf, quiet = quiet, cfonts = cfonts),
SIMPLIFY = FALSE, USE.NAMES = TRUE)
MoreArgs = list(pdf = pdf, quiet = quiet, cfonts = cfonts),
SIMPLIFY = FALSE, USE.NAMES = TRUE
)
}

#' Embeds fonts that are listed in the local Fontmap
Expand All @@ -188,20 +196,21 @@ loadfonts_pdf_ps <- function(pdf = TRUE, quiet = FALSE) {
#'
#' \dontrun{
#' loadfonts()
#' pdf('fonttest.pdf')
#' pdf("fonttest.pdf")
#' library(ggplot2)
#'
#' p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
#' p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
#' geom_point()
#'
#' # Run only the code below that is appropriate for your system
#' # On Mac and Windows, Impact should be available
#' p + opts(axis.title.x=theme_text(size=16, family="Impact", colour="red"))
#' p + opts(axis.title.x = theme_text(size = 16, family = "Impact", colour = "red"))
#'
#' # On Linux, Purisa may be available
#' p + opts(axis.title.x=theme_text(size=16, family="Purisa", colour="red"))
#' p + opts(axis.title.x = theme_text(size = 16, family = "Purisa", colour = "red"))
#' dev.off()
#'
#' embed_fonts('fonttest.pdf', outfile='fonttest-embed.pdf')
#' embed_fonts("fonttest.pdf", outfile = "fonttest-embed.pdf")
#' }
#'
#' @seealso \code{\link{loadfonts}}
Expand Down Expand Up @@ -243,8 +252,11 @@ embed_fonts <- function(file, format, outfile = file, options = "") {
fontmap <- tmpdir
}

embedFonts(file = file, format = format, outfile = outfile,
embedFonts(
file = file, format = format, outfile = outfile,
options = paste(
paste("-I", shQuote(fontmap), sep = ""),
options))
}
options
)
)
}
1 change: 0 additions & 1 deletion man/extrafont.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/font_import.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions man/loadfonts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.