diff --git a/cpdfattach.ml b/cpdfattach.ml index 01ed0f3..7b918e4 100644 --- a/cpdfattach.ml +++ b/cpdfattach.ml @@ -2,22 +2,31 @@ open Pdfutil open Pdfio open Cpdferror -(* Remove characters which might not make good filenames. *) -let remove_unsafe_characters encoding s = - if encoding = Cpdfmetadata.UTF8 then Pdftext.utf8_of_pdfdocstring s else (* For @B bookmarks splitting. *) - if encoding = Cpdfmetadata.Raw then s else - let chars = - lose - (function x -> - match x with - '/' | '?' | '<' | '>' | '\\' | ':' | '*' | '|' | '\"' | '^' | '+' | '=' -> true - | x when int_of_char x < 32 || (int_of_char x > 126 && encoding <> Cpdfmetadata.Stripped) -> true - | _ -> false) - (explode s) - in - match chars with - | '.'::more -> implode more - | chars -> implode chars +(* Remove characters which might not make good filenames. In, UTF8, out UTF8. *) +let remove_unsafe_characters s = + let codepoints = Pdftext.codepoints_of_utf8 s in + let codepoints = + lose + (function x -> + x = int_of_char '/' + || x = int_of_char '?' + || x = int_of_char '<' + || x = int_of_char '>' + || x = int_of_char '\\' + || x = int_of_char ':' + || x = int_of_char '*' + || x = int_of_char '|' + || x = int_of_char '\"' + || x = int_of_char '^' + || x = int_of_char '+' + || x = int_of_char '=' + || x < 32 + || x = 127) + codepoints + in + match codepoints with + | 46::more -> Pdftext.utf8_of_codepoints codepoints (* Don't produce a dotfile *) + | chars -> Pdftext.utf8_of_codepoints codepoints (* Attaching files *) let attach_file ?memory keepversion topage pdf file = @@ -257,7 +266,7 @@ let dump_attachment out pdf (_, embeddedfile) = | _ -> error "Bad embedded file stream" end in - let s = remove_unsafe_characters Cpdfmetadata.UTF8 s in + let s = remove_unsafe_characters (Pdftext.utf8_of_pdfdocstring s) in let filename = if out = "" then s else out ^ Filename.dir_sep ^ s in begin try let fh = open_out_bin filename in diff --git a/cpdfattach.mli b/cpdfattach.mli index a90e04d..0e42441 100644 --- a/cpdfattach.mli +++ b/cpdfattach.mli @@ -5,9 +5,8 @@ type attachment = pagenumber : int; data : unit -> Pdfio.bytes} -(** Remove characters which might not make good filenames. If the encoding is - [Cpdfmetadata.Stripped] we in addition lose any character > 126. *) -val remove_unsafe_characters : Cpdfmetadata.encoding -> string -> string +(** Remove characters which might not make good filenames from a UTF8 string. *) +val remove_unsafe_characters : string -> string (** [attach_file keepversion topage pdf filename] attaches the file in [filename] to the pdf, optionally to a page (rather than document-level). diff --git a/cpdfbookmarks.ml b/cpdfbookmarks.ml index c89e2f1..eca9ee2 100644 --- a/cpdfbookmarks.ml +++ b/cpdfbookmarks.ml @@ -265,7 +265,7 @@ let get_bookmark_name encoding pdf marks splitlevel n _ = let refnums = Pdf.page_reference_numbers pdf in let fastrefnums = hashtable_of_dictionary (combine refnums (indx refnums)) in match keep (function m -> n = Pdfpage.pagenumber_of_target ~fastrefnums pdf m.Pdfmarks.target && m.Pdfmarks.level <= splitlevel) marks with - | {Pdfmarks.text = title}::_ -> Cpdfattach.remove_unsafe_characters encoding title + | {Pdfmarks.text = title}::_ -> Cpdfattach.remove_unsafe_characters (Pdftext.utf8_of_pdfdocstring title) | _ -> "" (* @F means filename without extension *) diff --git a/cpdfmanual.pdf b/cpdfmanual.pdf index 8f8711a..4ac18a6 100644 Binary files a/cpdfmanual.pdf and b/cpdfmanual.pdf differ diff --git a/cpdfmanual.tex b/cpdfmanual.tex index 15b6837..13a9b9a 100644 --- a/cpdfmanual.tex +++ b/cpdfmanual.tex @@ -1303,7 +1303,7 @@ the result is unspecified. The following format operators may be used: \texttt{@N} & Sequence number without padding zeroes \\ \texttt{@S} & Start page of this chunk \\ \texttt{@E} & End page of this chunk \\ - \texttt{@B} & Bookmark name at this page \\ + \texttt{@B} & Bookmark name at this page, if any. \\ \end{tabular} \end{center} @@ -1340,14 +1340,12 @@ one of the output files. \noindent Split \texttt{a.pdf} on bookmark boundaries, using the bookmark text as the filename. \end{framed} -\noindent The bookmark text used for a name is converted from unicode to 7 bit ASCII, and the following characters are removed, in addition to any character with ASCII code less than 32: +\noindent The bookmark text used for a name has the following characters are removed, in addition to any character with ASCII code less than 32 or equal to 126. In addition, names beginning with \texttt{.} are not produced. \begin{framed} \centering \verb! / ? < > \ : * | " ^ + =! \end{framed} -\noindent To prevent this process, and convert bookmark names to UTF8 instead, add \texttt{-utf8} to the command. - \section{Splitting to Maximum Size} The \texttt{-split-max} operation splits a file into chunks of no more than the given size, starting at the beginning. The suffixes kB, KiB, MB, MiB, GB, and GiB may be used to give the size. For example: