@@ -70,43 +70,55 @@ cell_size <- function(col_rel_width, col_total_width) {
7070# '
7171# ' @noRd
7272convert <- 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(" [^\x 01-\x 7F]" , 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+ }
0 commit comments