Skip to content

Commit 562a216

Browse files
authored
Merge pull request #272 from Merck/feature/issue-256-simsun-font
Feature/issue 256 simsun font
2 parents f1be22f + 46ae4dd commit 562a216

21 files changed

+260
-68
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ sync_bitbucket.R
99
r2rtf.Rcheck/
1010
r2rtf*.tar.gz
1111
r2rtf*.tgz
12+
revdep/

R/as_rtf_paragraph.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@
3434
as_rtf_paragraph <- function(text, combine = TRUE) {
3535
attr_text <- attributes(text)
3636

37+
# Get use_i18n from text attributes (will be set by obj_rtf_text)
38+
use_i18n <- attr_text$use_i18n %||% FALSE
39+
3740
text_rtftext <- rtf_text(text,
3841
font = attr_text$text_font,
3942
font_size = attr_text$text_font_size,
4043
format = attr_text$text_format,
4144
color = attr_text$text_color,
4245
background_color = attr_text$text_background_color,
43-
text_convert = attr_text$text_convert
46+
text_convert = attr_text$text_convert,
47+
use_i18n = use_i18n
4448
)
4549

4650
if (combine) {

R/content_create.R

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ as_rtf_init <- function() {
3636

3737
#' Create RTF Font Encode
3838
#'
39+
#' @param tbl A data frame with potential page attributes containing use_i18n flag.
40+
#'
3941
#' @section Specification:
4042
#' \if{latex}{
4143
#' \itemize{
@@ -46,8 +48,14 @@ as_rtf_init <- function() {
4648
#' \if{html}{The contents of this section are shown in PDF user manual only.}
4749
#'
4850
#' @noRd
49-
as_rtf_font <- function() {
50-
font_type <- font_type()
51+
as_rtf_font <- function(tbl = NULL) {
52+
# Get use_i18n flag from table attributes if available
53+
use_i18n <- FALSE
54+
if (!is.null(tbl) && !is.null(attr(tbl, "page"))) {
55+
use_i18n <- attr(tbl, "page")$use_i18n %||% FALSE
56+
}
57+
58+
font_type <- font_type(use_i18n = use_i18n)
5159
font_rtf <- factor(seq_along(font_type$type), levels = font_type$type, labels = font_type$rtf_code)
5260
font_style <- factor(seq_along(font_type$type), levels = font_type$type, labels = font_type$style)
5361
font_name <- factor(seq_along(font_type$type), levels = font_type$type, labels = font_type$name)
@@ -317,7 +325,9 @@ as_rtf_footnote <- function(tbl, attr_name = "rtf_footnote") {
317325
)
318326
} else {
319327
if (any(attr(text, "text_convert"))) {
320-
text_matrix <- convert(text)
328+
# Get use_i18n from text attributes
329+
use_i18n <- attr(text, "use_i18n") %||% FALSE
330+
text_matrix <- convert(text, use_i18n = use_i18n)
321331
} else {
322332
text_matrix <- text
323333
}

R/conversion.R

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,43 +70,55 @@ cell_size <- function(col_rel_width, col_total_width) {
7070
#'
7171
#' @noRd
7272
convert <- function(text,
73+
use_i18n = FALSE,
7374
load_stringi = requireNamespace("stringi", quietly = TRUE)) {
7475
# grepl(">|<|=|_|\\^|(\\\\)|(\\n)", c(">", "<", "=", "_", "\n", "\\line", "abc"))
7576
index <- grepl(">|<|=|_|\\^|(\\\\)|(\\n)", text)
7677

77-
if (!any(index)) {
78-
return(text)
79-
}
78+
# Process special characters if they exist
79+
if (any(index)) {
80+
char_rtf <- c(
81+
"^" = "\\super ",
82+
"_" = "\\sub ",
83+
">=" = "\\geq ",
84+
"<=" = "\\leq ",
85+
"\n" = "\\line ",
86+
"\\pagenumber" = "\\chpgn ",
87+
"\\totalpage" = "\\totalpage ",
88+
"\\pagefield" = "{\\field{\\*\\fldinst NUMPAGES }} "
89+
)
8090

81-
char_rtf <- c(
82-
"^" = "\\super ",
83-
"_" = "\\sub ",
84-
">=" = "\\geq ",
85-
"<=" = "\\leq ",
86-
"\n" = "\\line ",
87-
"\\pagenumber" = "\\chpgn ",
88-
"\\totalpage" = "\\totalpage ",
89-
"\\pagefield" = "{\\field{\\*\\fldinst NUMPAGES }} "
90-
)
91+
# Define Pattern for latex code
92+
unicode_int <- as.integer(as.hexmode(unicode_latex$unicode))
93+
char_latex <- ifelse(unicode_int <= 255 & unicode_int != 177, unicode_latex$chr,
94+
sprintf("\\uc1\\u%d*", unicode_int - ifelse(unicode_int < 32768, 0, 65536))
95+
)
9196

92-
# Define Pattern for latex code
97+
names(char_latex) <- unicode_latex$latex
9398

94-
unicode_int <- as.integer(as.hexmode(unicode_latex$unicode))
95-
char_latex <- ifelse(unicode_int <= 255 & unicode_int != 177, unicode_latex$chr,
96-
sprintf("\\uc1\\u%d*", unicode_int - ifelse(unicode_int < 32768, 0, 65536))
97-
)
99+
char_latex <- rev(c(char_latex, char_rtf))
98100

99-
names(char_latex) <- unicode_latex$latex
100-
101-
char_latex <- rev(c(char_latex, char_rtf))
101+
if (load_stringi) {
102+
text[index] <- stringi::stri_replace_all_fixed(text[index], names(char_latex), char_latex,
103+
vectorize_all = FALSE, opts_fixed = list(case_insensitive = FALSE)
104+
)
105+
} else {
106+
for (i in seq_along(char_latex)) {
107+
text[index] <- gsub(names(char_latex[i]), char_latex[i], text[index], fixed = TRUE)
108+
}
109+
}
110+
}
102111

103-
if (load_stringi) {
104-
text[index] <- stringi::stri_replace_all_fixed(text[index], names(char_latex), char_latex,
105-
vectorize_all = FALSE, opts_fixed = list(case_insensitive = FALSE)
106-
)
107-
} else {
108-
for (i in seq_along(char_latex)) {
109-
text[index] <- gsub(names(char_latex[i]), char_latex[i], text[index], fixed = TRUE)
112+
# Apply UTF-8 to RTF conversion for non-ASCII characters when use_i18n is TRUE
113+
if (use_i18n) {
114+
# Check for non-ASCII characters
115+
has_non_ascii <- grepl("[^\x01-\x7F]", text)
116+
if (any(has_non_ascii)) {
117+
text[has_non_ascii] <- vapply(text[has_non_ascii],
118+
utf8Tortf,
119+
character(1),
120+
USE.NAMES = FALSE
121+
)
110122
}
111123
}
112124

@@ -148,3 +160,22 @@ utf8Tortf <- function(text) {
148160

149161
paste0(x_rtf, collapse = "")
150162
}
163+
164+
#' Apply UTF-8 to RTF Conversion for Character Vectors
165+
#'
166+
#' @param text Character vector to convert
167+
#' @param use_i18n Logical indicating whether to apply UTF-8 conversion
168+
#'
169+
#' @noRd
170+
apply_utf8_conversion <- function(text, use_i18n = FALSE) {
171+
if (!use_i18n || is.null(text)) {
172+
return(text)
173+
}
174+
175+
# Apply utf8Tortf to each element in the character vector
176+
if (is.character(text)) {
177+
vapply(text, utf8Tortf, character(1), USE.NAMES = FALSE)
178+
} else {
179+
text
180+
}
181+
}

R/dictionary.R

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,56 @@
1818

1919
#' RTF Text Font Dictionary
2020
#'
21+
#' @param use_i18n Logical. If TRUE, includes SimSun font for CJK support.
22+
#'
2123
#' @section Specification:
2224
#' \if{latex}{
2325
#' \itemize{
2426
#' \item Collect most commonly used fonts (Times New Roman, Times New Roman Greek, and Arial Greek, etc.).
25-
#' \item Define font types from 1 to 10.
27+
#' \item Define font types from 1 to 10 (or 11 with SimSun when use_i18n is TRUE).
2628
#' \item Define font styles.
2729
#' \item Create a mapping between font types and their RTF code.
2830
#' }
2931
#' }
3032
#' \if{html}{The contents of this section are shown in PDF user manual only.}
3133
#'
3234
#' @noRd
33-
font_type <- function() {
34-
data.frame(
35-
type = 1:10,
35+
font_type <- function(use_i18n = TRUE) {
36+
all_fonts <- data.frame(
37+
type = 1:11,
3638
name = c(
3739
"Times New Roman", "Times New Roman Greek", "Arial Greek",
3840
"Arial", "Helvetica", "Calibri", "Georgia",
39-
"Cambria", "Courier New", "Symbol"
41+
"Cambria", "Courier New", "Symbol", "SimSun"
4042
),
4143
style = c(
4244
"\\froman", "\\froman", "\\fswiss",
4345
"\\fswiss", "\\fswiss", "\\fswiss", "\\froman",
44-
"\\ffroman", "\\fmodern", "\\ftech"
46+
"\\ffroman", "\\fmodern", "\\ftech", "\\fnil"
4547
),
4648
rtf_code = c(
4749
"\\f0", "\\f1", "\\f2",
4850
"\\f3", "\\f4", "\\f5", "\\f6",
49-
"\\f7", "\\f8", "\\f9"
51+
"\\f7", "\\f8", "\\f9", "\\f10"
5052
),
5153
family = c(
5254
"Times", "Times", "ArialMT", "ArialMT", "Helvetica",
53-
"Calibri", "Georgia", "Cambria", "Courier", "Times"
55+
"Calibri", "Georgia", "Cambria", "Courier", "Times", "SimSun"
5456
),
5557
charset = c(
5658
"\\fcharset1", "\\fcharset161", "\\fcharset161", "\\fcharset0",
5759
"\\fcharset1", "\\fcharset1", "\\fcharset1", "\\fcharset1",
58-
"\\fcharset0", "\\fcharset2"
60+
"\\fcharset0", "\\fcharset2", "\\fcharset134"
5961
),
60-
width_group = c(1, 1, 4, 4, 4, 1, 9, 4, 9, 9),
62+
width_group = c(1, 1, 4, 4, 4, 1, 9, 4, 9, 9, 1),
6163
stringsAsFactors = FALSE
6264
)
65+
66+
if (use_i18n) {
67+
all_fonts
68+
} else {
69+
all_fonts[1:10, ]
70+
}
6371
}
6472

6573

R/obj_rtf_text.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ obj_rtf_text <- function(text,
5252
text_space_after = 15,
5353
text_new_page = FALSE,
5454
text_hyphenation = TRUE,
55-
text_convert = TRUE) {
55+
text_convert = TRUE,
56+
use_i18n = FALSE) {
5657
# Check argument type
5758
check_args(text, type = c("character", "data.frame"))
5859

@@ -143,6 +144,7 @@ obj_rtf_text <- function(text,
143144
attr(text, "text_new_page") <- text_new_page
144145
attr(text, "text_hyphenation") <- text_hyphenation
145146
attr(text, "text_convert") <- text_convert
147+
attr(text, "use_i18n") <- use_i18n
146148
attr(text, "strwidth") <- rtf_strwidth(text)
147149

148150
# Register Color Use

R/rtf_colheader.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ rtf_colheader <- function(tbl,
105105
# Split input by "|".
106106
colheader <- data.frame(t(trimws(unlist(strsplit(colheader, "|", fixed = TRUE)))))
107107

108+
# Get use_i18n from page attributes
109+
use_i18n <- attr(tbl, "page")$use_i18n %||% FALSE
110+
108111
# Define text attributes
109112
colheader <- obj_rtf_text(colheader,
110113
text_font,
@@ -121,7 +124,8 @@ rtf_colheader <- function(tbl,
121124
text_space_after,
122125
text_new_page = FALSE,
123126
text_hyphenation = text_hyphenation,
124-
text_convert = text_convert
127+
text_convert = text_convert,
128+
use_i18n = use_i18n
125129
)
126130
if (attr(colheader, "use_color")) attr(tbl, "page")$use_color <- TRUE
127131

R/rtf_encode_figure.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ rtf_encode_figure <- function(tbl) {
7070

7171
start_rtf <- paste(
7272
as_rtf_init(),
73-
as_rtf_font(),
73+
as_rtf_font(tbl),
7474
as_rtf_color(tbl),
7575
sep = "\n"
7676
)

R/rtf_encode_table.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ rtf_encode_table <- function(tbl, verbose = FALSE) {
5050
start_rtf <- paste(
5151

5252
as_rtf_init(),
53-
as_rtf_font(),
53+
as_rtf_font(tbl),
5454
as_rtf_color(tbl),
5555
sep = "\n"
5656
)

R/rtf_footnote.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ rtf_footnote <- function(tbl,
162162
tbl <- rtf_page(tbl)
163163
}
164164

165+
# Get use_i18n from page attributes
166+
use_i18n <- attr(tbl, "page")$use_i18n %||% FALSE
167+
165168
# Define text object
166169
footnote <- obj_rtf_text(footnote,
167170
text_font,
@@ -178,7 +181,8 @@ rtf_footnote <- function(tbl,
178181
text_space_after,
179182
text_new_page = FALSE,
180183
text_hyphenation = TRUE,
181-
text_convert = text_convert
184+
text_convert = text_convert,
185+
use_i18n = use_i18n
182186
)
183187

184188
if (attr(footnote, "use_color")) attr(tbl, "page")$use_color <- TRUE

0 commit comments

Comments
 (0)