|
8 | 8 | #' @inheritParams rmarkdown::pdf_document |
9 | 9 | #' @param page_total If TRUE, the total number of pages is shown in the footer. |
10 | 10 | #' @param show_footer If TRUE, a footer showing your name, document name, and page number. |
| 11 | +#' @param font_scale Numeric multiplier applied to hard-coded font sizes in the |
| 12 | +#' Awesome‑CV class (default = 1). Use values like 0.95–1.10. |
11 | 13 | #' |
12 | 14 | #' @section Preview: |
13 | 15 | #' `r insert_preview("awesomecv")` |
|
18 | 20 | #' ([@posquit0](https://github.com/posquit0)) |
19 | 21 | #' |
20 | 22 | #' @export |
21 | | -awesomecv <- function(..., latex_engine = "xelatex", page_total = FALSE, |
22 | | - show_footer = TRUE) { |
| 23 | +awesomecv <- function(..., |
| 24 | + latex_engine = "xelatex", |
| 25 | + page_total = FALSE, |
| 26 | + show_footer = TRUE, |
| 27 | + font_scale = 1) { |
23 | 28 | template <- system.file("rmarkdown", "templates", "awesomecv", |
24 | 29 | "resources", "awesome-cv.tex", |
25 | 30 | package = "vitae" |
26 | 31 | ) |
27 | 32 | set_entry_formats(awesome_cv_entries) |
28 | 33 | copy_supporting_files("awesomecv") |
| 34 | + font_size_scaling(scale = font_scale) |
29 | 35 | pandoc_vars <- list() |
30 | 36 | if(page_total) pandoc_vars$page_total <- TRUE |
31 | 37 | if(show_footer) pandoc_vars$show_footer <- TRUE |
@@ -61,3 +67,42 @@ awesome_cv_entries <- new_entry_formats( |
61 | 67 | ), collapse = "\n") |
62 | 68 | } |
63 | 69 | ) |
| 70 | + |
| 71 | +# Patches the local copy of awesome-cv.cls. |
| 72 | +# Strategy: inject \usepackage{xfp} and \newcommand\ACVscale{<scale>}, |
| 73 | +# then convert \fontsize{<n>pt}{...} -> \fontsize{\fpeval{<n>*\ACVscale}pt}{...} |
| 74 | +font_size_scaling <- function(scale = 1, file_path = "awesome-cv.cls") { |
| 75 | + if (!file.exists(file_path)) { |
| 76 | + stop("Expected 'awesome-cv.cls' in the working directory after copy_supporting_files().") |
| 77 | + } |
| 78 | + |
| 79 | + stopifnot(is.numeric(scale), length(scale) == 1, scale > 0) |
| 80 | + if (!file.exists(file_path)) stop("File not found: ", file_path) |
| 81 | + |
| 82 | + x <- readLines(file_path, warn = FALSE) |
| 83 | + |
| 84 | + # Insert just after fontspec so \fpeval is available before any uses |
| 85 | + anchor <- grep("\\\\RequirePackage\\[quiet\\]\\{fontspec\\}", x, perl = TRUE)[1] |
| 86 | + if (is.na(anchor)) anchor <- 0L |
| 87 | + |
| 88 | + # Ensure xfp is loaded once |
| 89 | + if (!any(grepl("\\\\usepackage\\{xfp\\}", x, perl = TRUE))) { |
| 90 | + x <- append(x, "\\usepackage{xfp}", after = anchor); anchor <- anchor + 1L |
| 91 | + } |
| 92 | + |
| 93 | + # Replace any prior ACVscale defs, then provide+renew (idempotent update) |
| 94 | + x <- x[!grepl("\\\\(re)?newcommand\\\\ACVscale|\\\\providecommand\\\\ACVscale", x, perl = TRUE)] |
| 95 | + x <- append(x, |
| 96 | + c("\\providecommand\\ACVscale{1}", |
| 97 | + sprintf("\\renewcommand\\ACVscale{%.6f}", scale)), |
| 98 | + after = anchor) |
| 99 | + |
| 100 | + # Rewrite \fontsize{<n>pt}{...} -> \fontsize{\fpeval{<n>*\ACVscale}pt}{...} |
| 101 | + # (Note the doubled backslashes in the R string to yield single backslashes in .cls) |
| 102 | + x <- gsub("\\\\fontsize\\{([0-9.]+)pt\\}\\{", |
| 103 | + "\\\\fontsize{\\\\fpeval{\\1*\\\\ACVscale}pt}{", |
| 104 | + x, perl = TRUE) |
| 105 | + |
| 106 | + writeLines(x, file_path) |
| 107 | + message("Font sizes will be scaled by LaTeX (xfp) using ACVscale = ", scale, ".") |
| 108 | +} |
0 commit comments