-
Notifications
You must be signed in to change notification settings - Fork 19
/
rtf_strwidth.R
133 lines (115 loc) · 4.2 KB
/
rtf_strwidth.R
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright (c) 2022 Merck & Co., Inc., Rahway, NJ, USA and its affiliates. All rights reserved.
#
# This file is part of the r2rtf program.
#
# r2rtf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#' @title Calculate String Width in Inches
#'
#' @description
#' Calculate string width in inches based on font (Times New Roman, Arial, etc.), font size, font style (bold, italic, bold-italic etc.), and text indent.
#'
#' @param tbl A data frame
#'
#' @section Specification:
#' \if{latex}{
#' \itemize{
#' \item \code{tbl} is a data frame.
#' \item Return an object with string width corresponding to each cell in the data frame \code{tbl}.
#' }
#' }
#' \if{html}{The contents of this section are shown in PDF user manual only.}
#'
#' @return an object with string width corresponding to each cell in the data frame \code{tbl}
#'
#' @examples
#' library(dplyr)
#' tbl <- data.frame(
#' x = rep("This is a long sentence", 5),
#' y = "short"
#' )
#' tbl %>%
#' rtf_body(text_font = c(1, 3)) %>%
#' r2rtf:::rtf_strwidth()
#'
#' @noRd
rtf_strwidth <- function(tbl) {
# Text matrix
if (!is.null(dim(tbl))) {
text <- apply(tbl, 2, as.character)
} else {
text <- as.character(tbl)
}
# Font size
text_cex <- attr(tbl, "text_font_size") / graphics::par("ps")
# Font format
# text format 1 (plain/normal), 2 (bold), 3 (italic), 4 (bold-italic)
if (!is.null(attr(tbl, "text_format"))) {
text_format <- unlist(lapply(strsplit(attr(tbl, "text_format"), ""), function(x) {
type_check <- c("b", "i") %in% x
sum(type_check * 1:2) + 1
}))
} else {
text_format <- 1
}
# Font family
font_num <- as.numeric(attr(tbl, "text_font"))
# Group font with same style
font_num <- factor(font_num, levels = 1:10, labels = c(1, 1, 4, 4, 4, 1, 9, 4, 9, 9))
font_num <- as.numeric(as.character(font_num))
text_family <- font_type()[font_num, "family"]
text_indent <- (attr(tbl, "text_indent_first")) / 1440 # to inch
db <- data.frame(
cex = as.numeric(text_cex),
font = as.numeric(text_format),
family = as.character(text_family),
stringsAsFactors = FALSE
)
db$text <- as.character(text)
db$id <- 1:nrow(db)
db_dict <- unique(db[, c("cex", "font", "family")])
db_dict$index <- 1:nrow(db_dict)
db <- merge(db, db_dict, all = TRUE)
db_list <- split(db, db$index)
db_list <- lapply(db_list, function(x) {
# Mapping Windows Font
if (.Platform$OS.type == "windows") {
grDevices::windowsFonts("Arial" = grDevices::windowsFont("Arial"))
grDevices::windowsFonts("Times" = grDevices::windowsFont("Times New Roman"))
grDevices::windowsFonts("ArialMT" = grDevices::windowsFont("Arial"))
grDevices::windowsFonts("Helvetica" = grDevices::windowsFont("Helvetica"))
grDevices::windowsFonts("Calibri" = grDevices::windowsFont("Calibri"))
grDevices::windowsFonts("Georgia" = grDevices::windowsFont("Georgia"))
grDevices::windowsFonts("Cambria" = grDevices::windowsFont("Cambria"))
grDevices::windowsFonts("Courier" = grDevices::windowsFont("Courier New"))
}
x$width <- graphics::strwidth(x$text,
units = "inches",
cex = x$cex[1],
font = x$font[1],
family = as.character(x$family[1])
)
x
})
db <- do.call(rbind, db_list)
width <- db$width[order(db$id)]
# Recalculate font size for "Georgia" ,"Courier New", and "Symbol".
width <- ifelse(font_num %in% 9, nchar(text) * attr(tbl, "text_font_size") * 0.52 / 72, width)
# Add indent space
width <- width + text_indent
width[is.na(width)] <- 0
if (!is.null(dim(tbl))) {
width <- matrix(width, nrow = nrow(tbl), ncol = ncol(tbl))
}
width
}