Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for copying embedded fonts settings #557

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 24 additions & 2 deletions R/docx_settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ docx_settings <- function(zoom = 1,
decimal_symbol = ".",
list_separator = ";",
compatibility_mode = "15",
even_and_odd_headers = FALSE
even_and_odd_headers = FALSE,
embed_ttf = FALSE,
embed_system = FALSE,
embed_subset = FALSE
) {
x <- list(
zoom = zoom,
Expand All @@ -13,7 +16,10 @@ docx_settings <- function(zoom = 1,
decimal_symbol = decimal_symbol,
list_separator = list_separator,
even_and_odd_headers = even_and_odd_headers,
compatibility_mode = compatibility_mode
compatibility_mode = compatibility_mode,
embed_ttf = embed_ttf,
embed_system = embed_system,
embed_subset = embed_subset
)
class(x) <- "docx_settings"
x
Expand All @@ -29,7 +35,11 @@ update.docx_settings <- function(object,
compatibility_mode = NULL,
even_and_odd_headers = NULL,
file = NULL,
embed_ttf = NULL,
embed_system = NULL,
embed_subset = NULL,
...) {

if (!is.null(zoom)) {
object$zoom <- zoom
}
Expand All @@ -51,6 +61,15 @@ update.docx_settings <- function(object,
if (!is.null(even_and_odd_headers)) {
object$even_and_odd_headers <- even_and_odd_headers
}
if (!is.null(embed_ttf)) {
object$embed_ttf <- embed_ttf
}
if (!is.null(embed_system)) {
object$embed_system <- embed_system
}
if (!is.null(embed_subset)) {
object$embed_subset <- embed_subset
}
if (!is.null(file) && file.exists(file)) {
node_doc <- read_xml(file)

Expand Down Expand Up @@ -94,6 +113,9 @@ to_wml.docx_settings <- function(x, add_ns = FALSE, ...) {
sprintf("<w:decimalSymbol w:val=\"%s\"/>", x$decimal_symbol),
sprintf("<w:listSeparator w:val=\"%s\"/>", x$list_separator),
if (x$even_and_odd_headers) "<w:evenAndOddHeaders/>",
if (x$embed_ttf) "<w:embedTrueTypeFonts/>",
if (x$embed_system) "<w:embedSystemFonts/>",
if (x$embed_subset) "<w:saveSubsetFonts/>",
"</w:settings>"
)
out
Expand Down
19 changes: 18 additions & 1 deletion R/read_docx.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,25 @@ read_docx <- function(path = NULL) {
class = "rdocx"
)

# Copy embedded font settings iff fonts are embedded in the current document
fonts <- list()
if (dir.exists(file.path(package_dir, "word/fonts"))) {
node_doc <- read_xml(file.path(package_dir, "word/settings.xml"))
if (!inherits(xml_child(node_doc, "w:embedTrueTypeFonts"), "xml_missing")) {
fonts$ttf <- TRUE
} else fonts$ttf <- FALSE
if (!inherits(xml_child(node_doc, "w:embedSystemFonts"), "xml_missing")) {
fonts$system <- TRUE
} else fonts$system <- FALSE
if (!inherits(xml_child(node_doc, "w:saveSubsetFonts"), "xml_missing")) {
fonts$subset <- TRUE
} else fonts$subset <- FALSE
}

obj$settings <- update(
object = docx_settings(),
object = docx_settings(embed_ttf = fonts$ttf,
embed_system = fonts$system,
embed_subset = fonts$subset),
file = file.path(package_dir, "word", "settings.xml")
)

Expand Down
Loading